Fix typos; move stray paragraph to Feature Test Macros.
[glibc/history.git] / sysdeps / mach / hurd / fork.c
blob5306ddf484d32f51474d272303a724e43ff4a609
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <hurd.h>
24 const struct
26 size_t n;
27 error_t (*fn[0]) (task_t);
28 } _hurd_fork_hook;
30 /* Clone the calling process, creating an exact copy.
31 Return -1 for errors, 0 to the new process,
32 and the process ID of the new process to the old process. */
33 int
34 DEFUN_VOID(__fork)
36 jmp_buf env;
38 if (setjmp (env))
39 /* Child fork. Return zero. */
40 return (pid_t) 0;
41 else
43 error_t err;
44 process_t newproc;
45 task_t newtask;
46 mach_port_t sigport;
47 thread_t *threads;
48 size_t nthreads;
49 char state[_hurd_thread_state_count];
50 pid_t child;
52 __mutex_lock (&_hurd_lock);
54 newtask = MACH_PORT_NULL;
55 sigport = MACH_PORT_NULL;
56 newproc = MACH_PORT_NULL;
58 if (err = __task_create (__mach_task_self (), 1, &newtask))
59 goto lose;
60 if (err = __mach_port_allocate (__mach_task_self (),
61 MACH_PORT_RIGHT_RECEIVE, &sigport))
62 goto lose;
64 if (err = __proc_register (_hurd_proc, newtask, sigport, &newproc))
65 goto lose;
67 if (err = __mach_port_insert_right (newtask, _hurd_sigport,
68 sigport, MACH_PORT_MOVE_RECEIVE))
69 goto lose;
71 if (err = __mach_port_insert_right (newtask, _hurd_proc, newproc,
72 MACH_PORT_MOVE_SEND))
73 goto lose;
75 #define cpysnd(port) \
76 if (err = __mach_port_insert_right (newtask, (port), (port), \
77 MACH_PORT_COPY_SEND)) \
78 goto lose; else
80 cpysnd (_hurd_ccdir);
81 cpysnd (_hurd_crdir);
82 cpysnd (_hurd_cwdir);
84 __mutex_unlock (&_hurd_lock);
86 for (i = 0; i < _hurd_fork_hook.n; ++i)
87 if (err = (*_hurd_fork_hook.fn[i]) (newtask))
88 goto lose;
90 __proc_dostop (_hurd_proc, __mach_thread_self ());
92 if (err = __task_threads (__mach_task_self (), &threads, &nthreads))
94 /* This is majorly bad. We've stopped all other threads and now
95 have no way to start them up. We can at least try to resume
96 the signal thread. */
97 __thread_resume (_hurd_sigport_thread);
98 goto lose;
101 /* Suspend the task so we can resume the threads as we create them,
102 but have them all start at once at the end. */
103 __task_suspend (newtask);
104 while (nthreads-- > 0)
106 thread_t = *threads++;
107 thread_t new = MACH_PORT_NULL;
109 if (!err)
110 err = __thread_create (newtask, &new);
111 if (!err && t == __mach_thread_self ())
112 /* Make the thread in the new task do a longjmp to ENV. */
113 err = _hurd_thread_longjmp (new, env, 1);
114 if (!err && t == _hurd_sigport_thread)
115 /* Give the thread port for the sigport thread the
116 same name in the child, so its copy of _hurd_sigport_thread
117 will be right. */
118 err = __mach_port_insert_right (newtask, t, new,
119 MACH_PORT_COPY_SEND);
121 if (!err)
122 err = _hurd_thread_state (t, state);
123 __thread_resume (t);
124 if (!err)
126 err = _hurd_thread_set_state (new, state);
127 __thread_resume (new);
129 __mach_port_deallocate (__mach_task_self (), t);
130 if (new != MACH_PORT_NULL)
131 __mach_port_deallocate (__mach_task_self (), new);
133 if (!err)
134 /* Start the task up. */
135 err = __task_resume (newtask);
137 if (!err)
138 err = __proc_task2pid (_hurd_proc, newtask, &child);
140 lose:
141 if (newtask != MACH_PORT_NULL)
143 if (err)
144 __task_terminate (newtask);
145 __mach_port_deallocate (__mach_task_self (), newtask);
147 if (sigport != MACH_PORT_NULL)
148 __mach_port_deallocate (__mach_task_self (), sigport);
149 if (newproc != MACH_PORT_NULL)
150 __mach_port_deallocate (__mach_task_self (), newproc);
151 if (err)
152 return __hurd_fail (err);
153 return child;