]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
e5568f75 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
e5568f75 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ | |
23 | /* | |
24 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 | |
25 | * The Regents of the University of California. All rights reserved. | |
26 | * (c) UNIX System Laboratories, Inc. | |
27 | * All or some portions of this file are derived from material licensed | |
28 | * to the University of California by American Telephone and Telegraph | |
29 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
30 | * the permission of UNIX System Laboratories, Inc. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95 | |
61 | */ | |
62 | ||
55e303ae | 63 | #include <kern/assert.h> |
1c79356b A |
64 | #include <sys/param.h> |
65 | #include <sys/systm.h> | |
66 | #include <sys/filedesc.h> | |
67 | #include <sys/kernel.h> | |
68 | #include <sys/malloc.h> | |
69 | #include <sys/proc.h> | |
70 | #include <sys/user.h> | |
71 | #include <sys/resourcevar.h> | |
72 | #include <sys/vnode.h> | |
73 | #include <sys/file.h> | |
74 | #include <sys/acct.h> | |
e5568f75 A |
75 | #include <sys/wait.h> |
76 | ||
77 | #include <bsm/audit_kernel.h> | |
78 | ||
9bccf70c | 79 | #if KTRACE |
1c79356b | 80 | #include <sys/ktrace.h> |
e5568f75 | 81 | #include <sys/ubc.h> |
9bccf70c | 82 | #endif |
1c79356b A |
83 | |
84 | #include <mach/mach_types.h> | |
85 | #include <kern/mach_param.h> | |
86 | ||
87 | #include <machine/spl.h> | |
88 | ||
9bccf70c | 89 | thread_act_t cloneproc(struct proc *, int); |
0b4e3aa0 | 90 | struct proc * forkproc(struct proc *, int); |
9bccf70c | 91 | thread_act_t procdup(); |
1c79356b A |
92 | |
93 | #define DOFORK 0x1 /* fork() system call */ | |
94 | #define DOVFORK 0x2 /* vfork() system call */ | |
95 | static int fork1(struct proc *, long, register_t *); | |
96 | ||
97 | /* | |
98 | * fork system call. | |
99 | */ | |
100 | int | |
101 | fork(p, uap, retval) | |
102 | struct proc *p; | |
103 | void *uap; | |
104 | register_t *retval; | |
105 | { | |
106 | return (fork1(p, (long)DOFORK, retval)); | |
107 | } | |
108 | ||
109 | /* | |
110 | * vfork system call | |
111 | */ | |
112 | int | |
113 | vfork(p, uap, retval) | |
114 | struct proc *p; | |
115 | void *uap; | |
116 | register_t *retval; | |
117 | { | |
0b4e3aa0 A |
118 | register struct proc * newproc; |
119 | register uid_t uid; | |
120 | thread_act_t cur_act = (thread_act_t)current_act(); | |
121 | int count; | |
122 | task_t t; | |
123 | uthread_t ut; | |
124 | ||
125 | /* | |
126 | * Although process entries are dynamically created, we still keep | |
127 | * a global limit on the maximum number we will create. Don't allow | |
128 | * a nonprivileged user to use the last process; don't let root | |
129 | * exceed the limit. The variable nprocs is the current number of | |
130 | * processes, maxproc is the limit. | |
131 | */ | |
132 | uid = p->p_cred->p_ruid; | |
133 | if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { | |
134 | tablefull("proc"); | |
135 | retval[1] = 0; | |
136 | return (EAGAIN); | |
137 | } | |
138 | ||
139 | /* | |
140 | * Increment the count of procs running with this uid. Don't allow | |
141 | * a nonprivileged user to exceed their current limit. | |
142 | */ | |
143 | count = chgproccnt(uid, 1); | |
144 | if (uid != 0 && count > p->p_rlimit[RLIMIT_NPROC].rlim_cur) { | |
145 | (void)chgproccnt(uid, -1); | |
146 | return (EAGAIN); | |
147 | } | |
148 | ||
149 | ut = (struct uthread *)get_bsdthread_info(cur_act); | |
150 | if (ut->uu_flag & P_VFORK) { | |
151 | printf("vfork called recursively by %s\n", p->p_comm); | |
55e303ae | 152 | (void)chgproccnt(uid, -1); |
0b4e3aa0 A |
153 | return (EINVAL); |
154 | } | |
155 | p->p_flag |= P_VFORK; | |
156 | p->p_vforkcnt++; | |
157 | ||
158 | /* The newly created process comes with signal lock held */ | |
159 | newproc = (struct proc *)forkproc(p,1); | |
160 | ||
e5568f75 A |
161 | AUDIT_ARG(pid, newproc->p_pid); |
162 | ||
0b4e3aa0 A |
163 | LIST_INSERT_AFTER(p, newproc, p_pglist); |
164 | newproc->p_pptr = p; | |
165 | newproc->task = p->task; | |
166 | LIST_INSERT_HEAD(&p->p_children, newproc, p_sibling); | |
167 | LIST_INIT(&newproc->p_children); | |
168 | LIST_INSERT_HEAD(&allproc, newproc, p_list); | |
169 | LIST_INSERT_HEAD(PIDHASH(newproc->p_pid), newproc, p_hash); | |
170 | TAILQ_INIT(& newproc->p_evlist); | |
171 | newproc->p_stat = SRUN; | |
172 | newproc->p_flag |= P_INVFORK; | |
173 | newproc->p_vforkact = cur_act; | |
174 | ||
175 | ut->uu_flag |= P_VFORK; | |
176 | ut->uu_proc = newproc; | |
177 | ut->uu_userstate = (void *)act_thread_csave(); | |
9bccf70c | 178 | ut->uu_vforkmask = ut->uu_sigmask; |
0b4e3aa0 A |
179 | |
180 | thread_set_child(cur_act, newproc->p_pid); | |
181 | ||
182 | newproc->p_stats->p_start = time; | |
183 | newproc->p_acflag = AFORK; | |
184 | ||
185 | /* | |
186 | * Preserve synchronization semantics of vfork. If waiting for | |
187 | * child to exec or exit, set P_PPWAIT on child, and sleep on our | |
188 | * proc (in case of exit). | |
189 | */ | |
190 | newproc->p_flag |= P_PPWAIT; | |
191 | ||
192 | /* drop the signal lock on the child */ | |
193 | signal_unlock(newproc); | |
194 | ||
195 | retval[0] = newproc->p_pid; | |
196 | retval[1] = 1; /* mark child */ | |
197 | ||
198 | return (0); | |
1c79356b A |
199 | } |
200 | ||
0b4e3aa0 A |
201 | /* |
202 | * Return to parent vfork ehread() | |
203 | */ | |
204 | void | |
205 | vfork_return(th_act, p, p2, retval) | |
206 | thread_act_t th_act; | |
207 | struct proc * p; | |
208 | struct proc *p2; | |
209 | register_t *retval; | |
210 | { | |
211 | long flags; | |
212 | register uid_t uid; | |
0b4e3aa0 A |
213 | int s, count; |
214 | task_t t; | |
215 | uthread_t ut; | |
216 | ||
e5568f75 | 217 | ut = (struct uthread *)get_bsdthread_info(th_act); |
0b4e3aa0 A |
218 | |
219 | act_thread_catt(ut->uu_userstate); | |
220 | ||
221 | /* Make sure only one at this time */ | |
e5568f75 A |
222 | if (p) { |
223 | p->p_vforkcnt--; | |
224 | if (p->p_vforkcnt <0) | |
225 | panic("vfork cnt is -ve"); | |
226 | if (p->p_vforkcnt <=0) | |
227 | p->p_flag &= ~P_VFORK; | |
228 | } | |
0b4e3aa0 A |
229 | ut->uu_userstate = 0; |
230 | ut->uu_flag &= ~P_VFORK; | |
231 | ut->uu_proc = 0; | |
9bccf70c | 232 | ut->uu_sigmask = ut->uu_vforkmask; |
0b4e3aa0 A |
233 | p2->p_flag &= ~P_INVFORK; |
234 | p2->p_vforkact = (void *)0; | |
235 | ||
e5568f75 | 236 | thread_set_parent(th_act, p2->p_pid); |
0b4e3aa0 A |
237 | |
238 | if (retval) { | |
239 | retval[0] = p2->p_pid; | |
240 | retval[1] = 0; /* mark parent */ | |
241 | } | |
242 | ||
243 | return; | |
244 | } | |
245 | ||
9bccf70c | 246 | thread_act_t |
0b4e3aa0 A |
247 | procdup( |
248 | struct proc *child, | |
249 | struct proc *parent) | |
250 | { | |
9bccf70c | 251 | thread_act_t thread; |
0b4e3aa0 A |
252 | task_t task; |
253 | kern_return_t result; | |
55e303ae | 254 | pmap_t pmap; |
0b4e3aa0 A |
255 | extern task_t kernel_task; |
256 | ||
257 | if (parent->task == kernel_task) | |
55e303ae | 258 | result = task_create_internal(TASK_NULL, FALSE, &task); |
0b4e3aa0 | 259 | else |
55e303ae | 260 | result = task_create_internal(parent->task, TRUE, &task); |
0b4e3aa0 A |
261 | if (result != KERN_SUCCESS) |
262 | printf("fork/procdup: task_create failed. Code: 0x%x\n", result); | |
263 | child->task = task; | |
264 | /* task->proc = child; */ | |
265 | set_bsdtask_info(task, child); | |
266 | if (child->p_nice != 0) | |
267 | resetpriority(child); | |
55e303ae | 268 | |
0b4e3aa0 A |
269 | result = thread_create(task, &thread); |
270 | if (result != KERN_SUCCESS) | |
271 | printf("fork/procdup: thread_create failed. Code: 0x%x\n", result); | |
272 | ||
273 | return(thread); | |
274 | } | |
275 | ||
276 | ||
1c79356b A |
277 | static int |
278 | fork1(p1, flags, retval) | |
279 | struct proc *p1; | |
280 | long flags; | |
281 | register_t *retval; | |
282 | { | |
283 | register struct proc *p2; | |
284 | register uid_t uid; | |
9bccf70c | 285 | thread_act_t newth; |
1c79356b A |
286 | int s, count; |
287 | task_t t; | |
288 | ||
289 | /* | |
290 | * Although process entries are dynamically created, we still keep | |
291 | * a global limit on the maximum number we will create. Don't allow | |
292 | * a nonprivileged user to use the last process; don't let root | |
293 | * exceed the limit. The variable nprocs is the current number of | |
294 | * processes, maxproc is the limit. | |
295 | */ | |
296 | uid = p1->p_cred->p_ruid; | |
297 | if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { | |
298 | tablefull("proc"); | |
299 | retval[1] = 0; | |
300 | return (EAGAIN); | |
301 | } | |
302 | ||
303 | /* | |
304 | * Increment the count of procs running with this uid. Don't allow | |
305 | * a nonprivileged user to exceed their current limit. | |
306 | */ | |
307 | count = chgproccnt(uid, 1); | |
308 | if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) { | |
309 | (void)chgproccnt(uid, -1); | |
310 | return (EAGAIN); | |
311 | } | |
312 | ||
313 | /* The newly created process comes with signal lock held */ | |
314 | newth = cloneproc(p1, 1); | |
9bccf70c | 315 | thread_dup(newth); |
1c79356b A |
316 | /* p2 = newth->task->proc; */ |
317 | p2 = (struct proc *)(get_bsdtask_info(get_threadtask(newth))); | |
318 | ||
e5568f75 A |
319 | AUDIT_ARG(pid, p2->p_pid); |
320 | ||
1c79356b A |
321 | thread_set_child(newth, p2->p_pid); |
322 | ||
323 | s = splhigh(); | |
324 | p2->p_stats->p_start = time; | |
325 | splx(s); | |
326 | p2->p_acflag = AFORK; | |
327 | ||
328 | /* | |
329 | * Preserve synchronization semantics of vfork. If waiting for | |
330 | * child to exec or exit, set P_PPWAIT on child, and sleep on our | |
331 | * proc (in case of exit). | |
332 | */ | |
333 | if (flags == DOVFORK) | |
334 | p2->p_flag |= P_PPWAIT; | |
335 | /* drop the signal lock on the child */ | |
336 | signal_unlock(p2); | |
337 | ||
338 | (void) thread_resume(newth); | |
339 | ||
340 | /* drop the extra references we got during the creation */ | |
0b4e3aa0 | 341 | if (t = (task_t)get_threadtask(newth)) { |
1c79356b A |
342 | task_deallocate(t); |
343 | } | |
344 | act_deallocate(newth); | |
345 | ||
55e303ae A |
346 | KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); |
347 | ||
1c79356b A |
348 | while (p2->p_flag & P_PPWAIT) |
349 | tsleep(p1, PWAIT, "ppwait", 0); | |
350 | ||
351 | retval[0] = p2->p_pid; | |
352 | retval[1] = 0; /* mark parent */ | |
353 | ||
354 | return (0); | |
355 | } | |
356 | ||
357 | /* | |
358 | * cloneproc() | |
359 | * | |
360 | * Create a new process from a specified process. | |
361 | * On return newly created child process has signal | |
362 | * lock held to block delivery of signal to it if called with | |
363 | * lock set. fork() code needs to explicity remove this lock | |
364 | * before signals can be delivered | |
365 | */ | |
9bccf70c | 366 | thread_act_t |
1c79356b A |
367 | cloneproc(p1, lock) |
368 | register struct proc *p1; | |
369 | register int lock; | |
0b4e3aa0 A |
370 | { |
371 | register struct proc *p2; | |
9bccf70c | 372 | thread_act_t th; |
0b4e3aa0 A |
373 | |
374 | p2 = (struct proc *)forkproc(p1,lock); | |
9bccf70c A |
375 | |
376 | ||
0b4e3aa0 A |
377 | th = procdup(p2, p1); /* child, parent */ |
378 | ||
379 | LIST_INSERT_AFTER(p1, p2, p_pglist); | |
380 | p2->p_pptr = p1; | |
381 | LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling); | |
382 | LIST_INIT(&p2->p_children); | |
383 | LIST_INSERT_HEAD(&allproc, p2, p_list); | |
384 | LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); | |
385 | TAILQ_INIT(&p2->p_evlist); | |
386 | /* | |
387 | * Make child runnable, set start time. | |
388 | */ | |
389 | p2->p_stat = SRUN; | |
390 | ||
391 | return(th); | |
392 | } | |
393 | ||
394 | struct proc * | |
395 | forkproc(p1, lock) | |
396 | register struct proc *p1; | |
397 | register int lock; | |
1c79356b A |
398 | { |
399 | register struct proc *p2, *newproc; | |
400 | static int nextpid = 0, pidchecked = 0; | |
401 | thread_t th; | |
402 | ||
403 | /* Allocate new proc. */ | |
404 | MALLOC_ZONE(newproc, struct proc *, | |
405 | sizeof *newproc, M_PROC, M_WAITOK); | |
406 | MALLOC_ZONE(newproc->p_cred, struct pcred *, | |
407 | sizeof *newproc->p_cred, M_SUBPROC, M_WAITOK); | |
408 | MALLOC_ZONE(newproc->p_stats, struct pstats *, | |
409 | sizeof *newproc->p_stats, M_SUBPROC, M_WAITOK); | |
410 | MALLOC_ZONE(newproc->p_sigacts, struct sigacts *, | |
411 | sizeof *newproc->p_sigacts, M_SUBPROC, M_WAITOK); | |
412 | ||
413 | /* | |
414 | * Find an unused process ID. We remember a range of unused IDs | |
415 | * ready to use (from nextpid+1 through pidchecked-1). | |
416 | */ | |
417 | nextpid++; | |
418 | retry: | |
419 | /* | |
420 | * If the process ID prototype has wrapped around, | |
421 | * restart somewhat above 0, as the low-numbered procs | |
422 | * tend to include daemons that don't exit. | |
423 | */ | |
424 | if (nextpid >= PID_MAX) { | |
425 | nextpid = 100; | |
426 | pidchecked = 0; | |
427 | } | |
428 | if (nextpid >= pidchecked) { | |
429 | int doingzomb = 0; | |
430 | ||
431 | pidchecked = PID_MAX; | |
432 | /* | |
433 | * Scan the active and zombie procs to check whether this pid | |
434 | * is in use. Remember the lowest pid that's greater | |
435 | * than nextpid, so we can avoid checking for a while. | |
436 | */ | |
437 | p2 = allproc.lh_first; | |
438 | again: | |
439 | for (; p2 != 0; p2 = p2->p_list.le_next) { | |
440 | while (p2->p_pid == nextpid || | |
9bccf70c A |
441 | p2->p_pgrp->pg_id == nextpid || |
442 | p2->p_session->s_sid == nextpid) { | |
1c79356b A |
443 | nextpid++; |
444 | if (nextpid >= pidchecked) | |
445 | goto retry; | |
446 | } | |
447 | if (p2->p_pid > nextpid && pidchecked > p2->p_pid) | |
448 | pidchecked = p2->p_pid; | |
449 | if (p2->p_pgrp && p2->p_pgrp->pg_id > nextpid && | |
450 | pidchecked > p2->p_pgrp->pg_id) | |
451 | pidchecked = p2->p_pgrp->pg_id; | |
9bccf70c A |
452 | if (p2->p_session->s_sid > nextpid && |
453 | pidchecked > p2->p_session->s_sid) | |
454 | pidchecked = p2->p_session->s_sid; | |
1c79356b A |
455 | } |
456 | if (!doingzomb) { | |
457 | doingzomb = 1; | |
458 | p2 = zombproc.lh_first; | |
459 | goto again; | |
460 | } | |
461 | } | |
462 | ||
463 | nprocs++; | |
464 | p2 = newproc; | |
465 | p2->p_stat = SIDL; | |
466 | p2->p_pid = nextpid; | |
467 | ||
4a249263 | 468 | p2->p_shutdownstate = 0; |
1c79356b A |
469 | /* |
470 | * Make a proc table entry for the new process. | |
471 | * Start by zeroing the section of proc that is zero-initialized, | |
472 | * then copy the section that is copied directly from the parent. | |
473 | */ | |
474 | bzero(&p2->p_startzero, | |
475 | (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); | |
476 | bcopy(&p1->p_startcopy, &p2->p_startcopy, | |
477 | (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); | |
478 | p2->vm_shm = (void *)NULL; /* Make sure it is zero */ | |
479 | ||
55e303ae A |
480 | /* |
481 | * Copy the audit info. | |
482 | */ | |
483 | audit_proc_fork(p1, p2); | |
484 | ||
1c79356b A |
485 | /* |
486 | * Duplicate sub-structures as needed. | |
487 | * Increase reference counts on shared objects. | |
488 | * The p_stats and p_sigacts substructs are set in vm_fork. | |
489 | */ | |
490 | p2->p_flag = P_INMEM; | |
55e303ae A |
491 | p2->p_flag |= (p1->p_flag & P_CLASSIC); // copy from parent |
492 | p2->p_flag |= (p1->p_flag & P_AFFINITY); // copy from parent | |
1c79356b A |
493 | if (p1->p_flag & P_PROFIL) |
494 | startprofclock(p2); | |
495 | bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); | |
496 | p2->p_cred->p_refcnt = 1; | |
497 | crhold(p1->p_ucred); | |
498 | lockinit(&p2->p_cred->pc_lock, PLOCK, "proc cred", 0, 0); | |
55e303ae | 499 | klist_init(&p2->p_klist); |
1c79356b | 500 | |
9bccf70c | 501 | /* bump references to the text vnode */ |
1c79356b A |
502 | p2->p_textvp = p1->p_textvp; |
503 | if (p2->p_textvp) | |
504 | VREF(p2->p_textvp); | |
505 | ||
506 | p2->p_fd = fdcopy(p1); | |
507 | if (p1->vm_shm) { | |
508 | shmfork(p1,p2); | |
509 | } | |
510 | /* | |
511 | * If p_limit is still copy-on-write, bump refcnt, | |
512 | * otherwise get a copy that won't be modified. | |
513 | * (If PL_SHAREMOD is clear, the structure is shared | |
514 | * copy-on-write.) | |
515 | */ | |
516 | if (p1->p_limit->p_lflags & PL_SHAREMOD) | |
517 | p2->p_limit = limcopy(p1->p_limit); | |
518 | else { | |
519 | p2->p_limit = p1->p_limit; | |
520 | p2->p_limit->p_refcnt++; | |
521 | } | |
522 | ||
523 | bzero(&p2->p_stats->pstat_startzero, | |
524 | (unsigned) ((caddr_t)&p2->p_stats->pstat_endzero - | |
525 | (caddr_t)&p2->p_stats->pstat_startzero)); | |
526 | bcopy(&p1->p_stats->pstat_startcopy, &p2->p_stats->pstat_startcopy, | |
527 | ((caddr_t)&p2->p_stats->pstat_endcopy - | |
528 | (caddr_t)&p2->p_stats->pstat_startcopy)); | |
529 | ||
530 | if (p1->p_sigacts != NULL) | |
531 | (void)memcpy(p2->p_sigacts, | |
532 | p1->p_sigacts, sizeof *p2->p_sigacts); | |
533 | else | |
534 | (void)memset(p2->p_sigacts, 0, sizeof *p2->p_sigacts); | |
535 | ||
536 | if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) | |
537 | p2->p_flag |= P_CONTROLT; | |
538 | ||
55e303ae A |
539 | p2->p_argslen = p1->p_argslen; |
540 | p2->p_argc = p1->p_argc; | |
1c79356b A |
541 | p2->p_xstat = 0; |
542 | p2->p_ru = NULL; | |
543 | ||
544 | p2->p_debugger = 0; /* don't inherit */ | |
545 | lockinit(&p2->signal_lock, PVM, "signal", 0, 0); | |
546 | /* block all signals to reach the process */ | |
547 | if (lock) | |
548 | signal_lock(p2); | |
549 | p2->sigwait = FALSE; | |
550 | p2->sigwait_thread = NULL; | |
551 | p2->exit_thread = NULL; | |
552 | p2->user_stack = p1->user_stack; | |
0b4e3aa0 A |
553 | p2->p_vforkcnt = 0; |
554 | p2->p_vforkact = 0; | |
9bccf70c | 555 | TAILQ_INIT(&p2->p_uthlist); |
55e303ae A |
556 | TAILQ_INIT(&p2->aio_activeq); |
557 | TAILQ_INIT(&p2->aio_doneq); | |
558 | p2->aio_active_count = 0; | |
559 | p2->aio_done_count = 0; | |
1c79356b A |
560 | |
561 | #if KTRACE | |
562 | /* | |
563 | * Copy traceflag and tracefile if enabled. | |
564 | * If not inherited, these were zeroed above. | |
565 | */ | |
566 | if (p1->p_traceflag&KTRFAC_INHERIT) { | |
567 | p2->p_traceflag = p1->p_traceflag; | |
e5568f75 A |
568 | if ((p2->p_tracep = p1->p_tracep) != NULL) { |
569 | if (UBCINFOEXISTS(p2->p_tracep)) | |
570 | ubc_hold(p2->p_tracep); | |
1c79356b | 571 | VREF(p2->p_tracep); |
e5568f75 | 572 | } |
1c79356b A |
573 | } |
574 | #endif | |
0b4e3aa0 | 575 | return(p2); |
1c79356b | 576 | |
1c79356b A |
577 | } |
578 | ||
579 | #include <kern/zalloc.h> | |
580 | ||
581 | struct zone *uthread_zone; | |
582 | int uthread_zone_inited = 0; | |
583 | ||
584 | void | |
585 | uthread_zone_init() | |
586 | { | |
587 | if (!uthread_zone_inited) { | |
588 | uthread_zone = zinit(sizeof(struct uthread), | |
589 | THREAD_MAX * sizeof(struct uthread), | |
590 | THREAD_CHUNK * sizeof(struct uthread), | |
591 | "uthreads"); | |
592 | uthread_zone_inited = 1; | |
593 | } | |
594 | } | |
595 | ||
596 | void * | |
9bccf70c | 597 | uthread_alloc(task_t task, thread_act_t thr_act ) |
1c79356b | 598 | { |
9bccf70c A |
599 | struct proc *p; |
600 | struct uthread *uth, *uth_parent; | |
1c79356b | 601 | void *ut; |
9bccf70c A |
602 | extern task_t kernel_task; |
603 | boolean_t funnel_state; | |
1c79356b A |
604 | |
605 | if (!uthread_zone_inited) | |
606 | uthread_zone_init(); | |
607 | ||
608 | ut = (void *)zalloc(uthread_zone); | |
609 | bzero(ut, sizeof(struct uthread)); | |
9bccf70c A |
610 | |
611 | if (task != kernel_task) { | |
612 | uth = (struct uthread *)ut; | |
55e303ae | 613 | p = (struct proc *) get_bsdtask_info(task); |
9bccf70c A |
614 | |
615 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
616 | uth_parent = (struct uthread *)get_bsdthread_info(current_act()); | |
617 | if (uth_parent) { | |
618 | if (uth_parent->uu_flag & USAS_OLDMASK) | |
619 | uth->uu_sigmask = uth_parent->uu_oldmask; | |
620 | else | |
621 | uth->uu_sigmask = uth_parent->uu_sigmask; | |
622 | } | |
623 | uth->uu_act = thr_act; | |
624 | //signal_lock(p); | |
625 | if (p) | |
626 | TAILQ_INSERT_TAIL(&p->p_uthlist, uth, uu_list); | |
627 | //signal_unlock(p); | |
628 | (void)thread_funnel_set(kernel_flock, funnel_state); | |
629 | } | |
630 | ||
1c79356b A |
631 | return (ut); |
632 | } | |
633 | ||
0b4e3aa0 | 634 | |
1c79356b | 635 | void |
e5568f75 | 636 | uthread_free(task_t task, thread_t act, void *uthread, void * bsd_info) |
1c79356b A |
637 | { |
638 | struct _select *sel; | |
639 | struct uthread *uth = (struct uthread *)uthread; | |
9bccf70c A |
640 | struct proc * p = (struct proc *)bsd_info; |
641 | extern task_t kernel_task; | |
0b4e3aa0 | 642 | int size; |
9bccf70c | 643 | boolean_t funnel_state; |
55e303ae | 644 | struct nlminfo *nlmp; |
e5568f75 | 645 | struct proc * vproc; |
55e303ae A |
646 | |
647 | /* | |
648 | * Per-thread audit state should never last beyond system | |
649 | * call return. Since we don't audit the thread creation/ | |
650 | * removal, the thread state pointer should never be | |
651 | * non-NULL when we get here. | |
652 | */ | |
653 | assert(uth->uu_ar == NULL); | |
1c79356b A |
654 | |
655 | sel = &uth->uu_state.ss_select; | |
656 | /* cleanup the select bit space */ | |
657 | if (sel->nbytes) { | |
658 | FREE(sel->ibits, M_TEMP); | |
659 | FREE(sel->obits, M_TEMP); | |
660 | } | |
661 | ||
0b4e3aa0 A |
662 | if (sel->allocsize && uth->uu_wqsub){ |
663 | kfree(uth->uu_wqsub, sel->allocsize); | |
664 | sel->count = sel->nfcount = 0; | |
665 | sel->allocsize = 0; | |
666 | uth->uu_wqsub = 0; | |
667 | sel->wql = 0; | |
668 | } | |
669 | ||
55e303ae A |
670 | if ((nlmp = uth->uu_nlminfo)) { |
671 | uth->uu_nlminfo = 0; | |
672 | FREE(nlmp, M_LOCKF); | |
673 | } | |
674 | ||
e5568f75 A |
675 | if ((task != kernel_task) ) { |
676 | int vfork_exit(struct proc *, int); | |
677 | ||
9bccf70c | 678 | funnel_state = thread_funnel_set(kernel_flock, TRUE); |
e5568f75 A |
679 | if (p) |
680 | TAILQ_REMOVE(&p->p_uthlist, uth, uu_list); | |
681 | if ((uth->uu_flag & P_VFORK) && (vproc = uth->uu_proc) | |
682 | && (vproc->p_flag & P_INVFORK)) { | |
683 | if (!vfork_exit(vproc, W_EXITCODE(0, SIGKILL))) | |
684 | vfork_return(act, p, vproc, NULL); | |
685 | ||
686 | } | |
9bccf70c A |
687 | (void)thread_funnel_set(kernel_flock, funnel_state); |
688 | } | |
1c79356b A |
689 | /* and free the uthread itself */ |
690 | zfree(uthread_zone, (vm_offset_t)uthread); | |
691 | } |