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. */
27 error_t (*fn
[0]) (task_t
);
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. */
39 /* Child fork. Return zero. */
49 char state
[_hurd_thread_state_count
];
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
))
60 if (err
= __mach_port_allocate (__mach_task_self (),
61 MACH_PORT_RIGHT_RECEIVE
, &sigport
))
64 if (err
= __proc_register (_hurd_proc
, newtask
, sigport
, &newproc
))
67 if (err
= __mach_port_insert_right (newtask
, _hurd_sigport
,
68 sigport
, MACH_PORT_MOVE_RECEIVE
))
71 if (err
= __mach_port_insert_right (newtask
, _hurd_proc
, newproc
,
75 #define cpysnd(port) \
76 if (err = __mach_port_insert_right (newtask, (port), (port), \
77 MACH_PORT_COPY_SEND)) \
84 __mutex_unlock (&_hurd_lock
);
86 for (i
= 0; i
< _hurd_fork_hook
.n
; ++i
)
87 if (err
= (*_hurd_fork_hook
.fn
[i
]) (newtask
))
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
97 __thread_resume (_hurd_sigport_thread
);
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
;
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
118 err
= __mach_port_insert_right (newtask
, t
, new,
119 MACH_PORT_COPY_SEND
);
122 err
= _hurd_thread_state (t
, state
);
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);
134 /* Start the task up. */
135 err
= __task_resume (newtask
);
138 err
= __proc_task2pid (_hurd_proc
, newtask
, &child
);
141 if (newtask
!= MACH_PORT_NULL
)
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
);
152 return __hurd_fail (err
);