]>
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))); | |
a3d08fcd | 318 | set_security_token(p2); /* propagate change of PID */ |
1c79356b | 319 | |
e5568f75 A |
320 | AUDIT_ARG(pid, p2->p_pid); |
321 | ||
1c79356b A |
322 | thread_set_child(newth, p2->p_pid); |
323 | ||
324 | s = splhigh(); | |
325 | p2->p_stats->p_start = time; | |
326 | splx(s); | |
327 | p2->p_acflag = AFORK; | |
328 | ||
329 | /* | |
330 | * Preserve synchronization semantics of vfork. If waiting for | |
331 | * child to exec or exit, set P_PPWAIT on child, and sleep on our | |
332 | * proc (in case of exit). | |
333 | */ | |
334 | if (flags == DOVFORK) | |
335 | p2->p_flag |= P_PPWAIT; | |
336 | /* drop the signal lock on the child */ | |
337 | signal_unlock(p2); | |
338 | ||
339 | (void) thread_resume(newth); | |
340 | ||
341 | /* drop the extra references we got during the creation */ | |
0b4e3aa0 | 342 | if (t = (task_t)get_threadtask(newth)) { |
1c79356b A |
343 | task_deallocate(t); |
344 | } | |
345 | act_deallocate(newth); | |
346 | ||
55e303ae A |
347 | KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); |
348 | ||
1c79356b A |
349 | while (p2->p_flag & P_PPWAIT) |
350 | tsleep(p1, PWAIT, "ppwait", 0); | |
351 | ||
352 | retval[0] = p2->p_pid; | |
353 | retval[1] = 0; /* mark parent */ | |
354 | ||
355 | return (0); | |
356 | } | |
357 | ||
358 | /* | |
359 | * cloneproc() | |
360 | * | |
361 | * Create a new process from a specified process. | |
362 | * On return newly created child process has signal | |
363 | * lock held to block delivery of signal to it if called with | |
364 | * lock set. fork() code needs to explicity remove this lock | |
365 | * before signals can be delivered | |
366 | */ | |
9bccf70c | 367 | thread_act_t |
1c79356b A |
368 | cloneproc(p1, lock) |
369 | register struct proc *p1; | |
370 | register int lock; | |
0b4e3aa0 A |
371 | { |
372 | register struct proc *p2; | |
9bccf70c | 373 | thread_act_t th; |
0b4e3aa0 A |
374 | |
375 | p2 = (struct proc *)forkproc(p1,lock); | |
9bccf70c A |
376 | |
377 | ||
0b4e3aa0 A |
378 | th = procdup(p2, p1); /* child, parent */ |
379 | ||
380 | LIST_INSERT_AFTER(p1, p2, p_pglist); | |
381 | p2->p_pptr = p1; | |
382 | LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling); | |
383 | LIST_INIT(&p2->p_children); | |
384 | LIST_INSERT_HEAD(&allproc, p2, p_list); | |
385 | LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); | |
386 | TAILQ_INIT(&p2->p_evlist); | |
387 | /* | |
388 | * Make child runnable, set start time. | |
389 | */ | |
390 | p2->p_stat = SRUN; | |
391 | ||
392 | return(th); | |
393 | } | |
394 | ||
395 | struct proc * | |
396 | forkproc(p1, lock) | |
397 | register struct proc *p1; | |
398 | register int lock; | |
1c79356b A |
399 | { |
400 | register struct proc *p2, *newproc; | |
401 | static int nextpid = 0, pidchecked = 0; | |
402 | thread_t th; | |
403 | ||
404 | /* Allocate new proc. */ | |
405 | MALLOC_ZONE(newproc, struct proc *, | |
406 | sizeof *newproc, M_PROC, M_WAITOK); | |
407 | MALLOC_ZONE(newproc->p_cred, struct pcred *, | |
408 | sizeof *newproc->p_cred, M_SUBPROC, M_WAITOK); | |
409 | MALLOC_ZONE(newproc->p_stats, struct pstats *, | |
410 | sizeof *newproc->p_stats, M_SUBPROC, M_WAITOK); | |
411 | MALLOC_ZONE(newproc->p_sigacts, struct sigacts *, | |
412 | sizeof *newproc->p_sigacts, M_SUBPROC, M_WAITOK); | |
413 | ||
414 | /* | |
415 | * Find an unused process ID. We remember a range of unused IDs | |
416 | * ready to use (from nextpid+1 through pidchecked-1). | |
417 | */ | |
418 | nextpid++; | |
419 | retry: | |
420 | /* | |
421 | * If the process ID prototype has wrapped around, | |
422 | * restart somewhat above 0, as the low-numbered procs | |
423 | * tend to include daemons that don't exit. | |
424 | */ | |
425 | if (nextpid >= PID_MAX) { | |
426 | nextpid = 100; | |
427 | pidchecked = 0; | |
428 | } | |
429 | if (nextpid >= pidchecked) { | |
430 | int doingzomb = 0; | |
431 | ||
432 | pidchecked = PID_MAX; | |
433 | /* | |
434 | * Scan the active and zombie procs to check whether this pid | |
435 | * is in use. Remember the lowest pid that's greater | |
436 | * than nextpid, so we can avoid checking for a while. | |
437 | */ | |
438 | p2 = allproc.lh_first; | |
439 | again: | |
440 | for (; p2 != 0; p2 = p2->p_list.le_next) { | |
441 | while (p2->p_pid == nextpid || | |
9bccf70c A |
442 | p2->p_pgrp->pg_id == nextpid || |
443 | p2->p_session->s_sid == nextpid) { | |
1c79356b A |
444 | nextpid++; |
445 | if (nextpid >= pidchecked) | |
446 | goto retry; | |
447 | } | |
448 | if (p2->p_pid > nextpid && pidchecked > p2->p_pid) | |
449 | pidchecked = p2->p_pid; | |
450 | if (p2->p_pgrp && p2->p_pgrp->pg_id > nextpid && | |
451 | pidchecked > p2->p_pgrp->pg_id) | |
452 | pidchecked = p2->p_pgrp->pg_id; | |
9bccf70c A |
453 | if (p2->p_session->s_sid > nextpid && |
454 | pidchecked > p2->p_session->s_sid) | |
455 | pidchecked = p2->p_session->s_sid; | |
1c79356b A |
456 | } |
457 | if (!doingzomb) { | |
458 | doingzomb = 1; | |
459 | p2 = zombproc.lh_first; | |
460 | goto again; | |
461 | } | |
462 | } | |
463 | ||
464 | nprocs++; | |
465 | p2 = newproc; | |
466 | p2->p_stat = SIDL; | |
467 | p2->p_pid = nextpid; | |
468 | ||
4a249263 | 469 | p2->p_shutdownstate = 0; |
1c79356b A |
470 | /* |
471 | * Make a proc table entry for the new process. | |
472 | * Start by zeroing the section of proc that is zero-initialized, | |
473 | * then copy the section that is copied directly from the parent. | |
474 | */ | |
475 | bzero(&p2->p_startzero, | |
476 | (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); | |
477 | bcopy(&p1->p_startcopy, &p2->p_startcopy, | |
478 | (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); | |
479 | p2->vm_shm = (void *)NULL; /* Make sure it is zero */ | |
480 | ||
55e303ae A |
481 | /* |
482 | * Copy the audit info. | |
483 | */ | |
484 | audit_proc_fork(p1, p2); | |
485 | ||
1c79356b A |
486 | /* |
487 | * Duplicate sub-structures as needed. | |
488 | * Increase reference counts on shared objects. | |
489 | * The p_stats and p_sigacts substructs are set in vm_fork. | |
490 | */ | |
491 | p2->p_flag = P_INMEM; | |
55e303ae A |
492 | p2->p_flag |= (p1->p_flag & P_CLASSIC); // copy from parent |
493 | p2->p_flag |= (p1->p_flag & P_AFFINITY); // copy from parent | |
1c79356b A |
494 | if (p1->p_flag & P_PROFIL) |
495 | startprofclock(p2); | |
496 | bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); | |
497 | p2->p_cred->p_refcnt = 1; | |
498 | crhold(p1->p_ucred); | |
499 | lockinit(&p2->p_cred->pc_lock, PLOCK, "proc cred", 0, 0); | |
55e303ae | 500 | klist_init(&p2->p_klist); |
1c79356b | 501 | |
9bccf70c | 502 | /* bump references to the text vnode */ |
1c79356b A |
503 | p2->p_textvp = p1->p_textvp; |
504 | if (p2->p_textvp) | |
505 | VREF(p2->p_textvp); | |
506 | ||
507 | p2->p_fd = fdcopy(p1); | |
508 | if (p1->vm_shm) { | |
509 | shmfork(p1,p2); | |
510 | } | |
511 | /* | |
512 | * If p_limit is still copy-on-write, bump refcnt, | |
513 | * otherwise get a copy that won't be modified. | |
514 | * (If PL_SHAREMOD is clear, the structure is shared | |
515 | * copy-on-write.) | |
516 | */ | |
517 | if (p1->p_limit->p_lflags & PL_SHAREMOD) | |
518 | p2->p_limit = limcopy(p1->p_limit); | |
519 | else { | |
520 | p2->p_limit = p1->p_limit; | |
521 | p2->p_limit->p_refcnt++; | |
522 | } | |
523 | ||
524 | bzero(&p2->p_stats->pstat_startzero, | |
525 | (unsigned) ((caddr_t)&p2->p_stats->pstat_endzero - | |
526 | (caddr_t)&p2->p_stats->pstat_startzero)); | |
527 | bcopy(&p1->p_stats->pstat_startcopy, &p2->p_stats->pstat_startcopy, | |
528 | ((caddr_t)&p2->p_stats->pstat_endcopy - | |
529 | (caddr_t)&p2->p_stats->pstat_startcopy)); | |
530 | ||
531 | if (p1->p_sigacts != NULL) | |
532 | (void)memcpy(p2->p_sigacts, | |
533 | p1->p_sigacts, sizeof *p2->p_sigacts); | |
534 | else | |
535 | (void)memset(p2->p_sigacts, 0, sizeof *p2->p_sigacts); | |
536 | ||
537 | if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) | |
538 | p2->p_flag |= P_CONTROLT; | |
539 | ||
55e303ae A |
540 | p2->p_argslen = p1->p_argslen; |
541 | p2->p_argc = p1->p_argc; | |
1c79356b A |
542 | p2->p_xstat = 0; |
543 | p2->p_ru = NULL; | |
544 | ||
545 | p2->p_debugger = 0; /* don't inherit */ | |
546 | lockinit(&p2->signal_lock, PVM, "signal", 0, 0); | |
547 | /* block all signals to reach the process */ | |
548 | if (lock) | |
549 | signal_lock(p2); | |
550 | p2->sigwait = FALSE; | |
551 | p2->sigwait_thread = NULL; | |
552 | p2->exit_thread = NULL; | |
553 | p2->user_stack = p1->user_stack; | |
0b4e3aa0 A |
554 | p2->p_vforkcnt = 0; |
555 | p2->p_vforkact = 0; | |
9bccf70c | 556 | TAILQ_INIT(&p2->p_uthlist); |
55e303ae A |
557 | TAILQ_INIT(&p2->aio_activeq); |
558 | TAILQ_INIT(&p2->aio_doneq); | |
559 | p2->aio_active_count = 0; | |
560 | p2->aio_done_count = 0; | |
1c79356b A |
561 | |
562 | #if KTRACE | |
563 | /* | |
564 | * Copy traceflag and tracefile if enabled. | |
565 | * If not inherited, these were zeroed above. | |
566 | */ | |
567 | if (p1->p_traceflag&KTRFAC_INHERIT) { | |
568 | p2->p_traceflag = p1->p_traceflag; | |
e5568f75 A |
569 | if ((p2->p_tracep = p1->p_tracep) != NULL) { |
570 | if (UBCINFOEXISTS(p2->p_tracep)) | |
571 | ubc_hold(p2->p_tracep); | |
1c79356b | 572 | VREF(p2->p_tracep); |
e5568f75 | 573 | } |
1c79356b A |
574 | } |
575 | #endif | |
0b4e3aa0 | 576 | return(p2); |
1c79356b | 577 | |
1c79356b A |
578 | } |
579 | ||
580 | #include <kern/zalloc.h> | |
581 | ||
582 | struct zone *uthread_zone; | |
583 | int uthread_zone_inited = 0; | |
584 | ||
585 | void | |
586 | uthread_zone_init() | |
587 | { | |
588 | if (!uthread_zone_inited) { | |
589 | uthread_zone = zinit(sizeof(struct uthread), | |
590 | THREAD_MAX * sizeof(struct uthread), | |
591 | THREAD_CHUNK * sizeof(struct uthread), | |
592 | "uthreads"); | |
593 | uthread_zone_inited = 1; | |
594 | } | |
595 | } | |
596 | ||
597 | void * | |
9bccf70c | 598 | uthread_alloc(task_t task, thread_act_t thr_act ) |
1c79356b | 599 | { |
9bccf70c A |
600 | struct proc *p; |
601 | struct uthread *uth, *uth_parent; | |
1c79356b | 602 | void *ut; |
9bccf70c A |
603 | extern task_t kernel_task; |
604 | boolean_t funnel_state; | |
1c79356b A |
605 | |
606 | if (!uthread_zone_inited) | |
607 | uthread_zone_init(); | |
608 | ||
609 | ut = (void *)zalloc(uthread_zone); | |
610 | bzero(ut, sizeof(struct uthread)); | |
9bccf70c A |
611 | |
612 | if (task != kernel_task) { | |
613 | uth = (struct uthread *)ut; | |
55e303ae | 614 | p = (struct proc *) get_bsdtask_info(task); |
9bccf70c A |
615 | |
616 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
617 | uth_parent = (struct uthread *)get_bsdthread_info(current_act()); | |
618 | if (uth_parent) { | |
619 | if (uth_parent->uu_flag & USAS_OLDMASK) | |
620 | uth->uu_sigmask = uth_parent->uu_oldmask; | |
621 | else | |
622 | uth->uu_sigmask = uth_parent->uu_sigmask; | |
623 | } | |
624 | uth->uu_act = thr_act; | |
625 | //signal_lock(p); | |
626 | if (p) | |
627 | TAILQ_INSERT_TAIL(&p->p_uthlist, uth, uu_list); | |
628 | //signal_unlock(p); | |
629 | (void)thread_funnel_set(kernel_flock, funnel_state); | |
630 | } | |
631 | ||
1c79356b A |
632 | return (ut); |
633 | } | |
634 | ||
0b4e3aa0 | 635 | |
1c79356b | 636 | void |
e5568f75 | 637 | uthread_free(task_t task, thread_t act, void *uthread, void * bsd_info) |
1c79356b A |
638 | { |
639 | struct _select *sel; | |
640 | struct uthread *uth = (struct uthread *)uthread; | |
9bccf70c A |
641 | struct proc * p = (struct proc *)bsd_info; |
642 | extern task_t kernel_task; | |
0b4e3aa0 | 643 | int size; |
9bccf70c | 644 | boolean_t funnel_state; |
55e303ae | 645 | struct nlminfo *nlmp; |
e5568f75 | 646 | struct proc * vproc; |
55e303ae A |
647 | |
648 | /* | |
649 | * Per-thread audit state should never last beyond system | |
650 | * call return. Since we don't audit the thread creation/ | |
651 | * removal, the thread state pointer should never be | |
652 | * non-NULL when we get here. | |
653 | */ | |
654 | assert(uth->uu_ar == NULL); | |
1c79356b A |
655 | |
656 | sel = &uth->uu_state.ss_select; | |
657 | /* cleanup the select bit space */ | |
658 | if (sel->nbytes) { | |
659 | FREE(sel->ibits, M_TEMP); | |
660 | FREE(sel->obits, M_TEMP); | |
661 | } | |
662 | ||
0b4e3aa0 A |
663 | if (sel->allocsize && uth->uu_wqsub){ |
664 | kfree(uth->uu_wqsub, sel->allocsize); | |
665 | sel->count = sel->nfcount = 0; | |
666 | sel->allocsize = 0; | |
667 | uth->uu_wqsub = 0; | |
668 | sel->wql = 0; | |
669 | } | |
670 | ||
55e303ae A |
671 | if ((nlmp = uth->uu_nlminfo)) { |
672 | uth->uu_nlminfo = 0; | |
673 | FREE(nlmp, M_LOCKF); | |
674 | } | |
675 | ||
e5568f75 A |
676 | if ((task != kernel_task) ) { |
677 | int vfork_exit(struct proc *, int); | |
678 | ||
9bccf70c | 679 | funnel_state = thread_funnel_set(kernel_flock, TRUE); |
e5568f75 A |
680 | if (p) |
681 | TAILQ_REMOVE(&p->p_uthlist, uth, uu_list); | |
682 | if ((uth->uu_flag & P_VFORK) && (vproc = uth->uu_proc) | |
683 | && (vproc->p_flag & P_INVFORK)) { | |
684 | if (!vfork_exit(vproc, W_EXITCODE(0, SIGKILL))) | |
685 | vfork_return(act, p, vproc, NULL); | |
686 | ||
687 | } | |
9bccf70c A |
688 | (void)thread_funnel_set(kernel_flock, funnel_state); |
689 | } | |
1c79356b A |
690 | /* and free the uthread itself */ |
691 | zfree(uthread_zone, (vm_offset_t)uthread); | |
692 | } |