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