2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 * 04-Aug-97 Umesh Vaishampayan (umeshv@apple.com)
71 * Added current_proc_EXTERNAL() function for the use of kernel
74 * 05-Jun-95 Mac Gillon (mgillon) at NeXT
75 * New version based on 3.3NS and 4.4
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/proc_internal.h>
85 #include <sys/file_internal.h>
86 #include <ufs/ufs/quota.h>
88 #include <sys/malloc.h>
91 #include <sys/ioctl.h>
93 #include <sys/signalvar.h>
94 #include <sys/syslog.h>
95 #include <sys/sysctl.h>
96 #include <sys/sysproto.h>
97 #include <sys/kauth.h>
98 #include <sys/codesign.h>
99 #include <sys/kernel_types.h>
100 #include <kern/kalloc.h>
101 #include <kern/task.h>
102 #include <kern/assert.h>
103 #include <vm/vm_protos.h>
106 #include <security/mac_framework.h>
109 #include <libkern/crypto/sha1.h>
112 * Structure associated with user cacheing.
115 LIST_ENTRY(uidinfo
) ui_hash
;
119 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
120 LIST_HEAD(uihashhead
, uidinfo
) *uihashtbl
;
121 u_long uihash
; /* size of hash table - 1 */
124 * Other process lists
126 struct pidhashhead
*pidhashtbl
;
128 struct pgrphashhead
*pgrphashtbl
;
130 struct sesshashhead
*sesshashtbl
;
133 struct proclist allproc
;
134 struct proclist zombproc
;
135 extern struct tty cons
;
141 static pid_t lastlcid
= 1;
142 static int alllctx_cnt
;
144 #define LCID_MAX 8192 /* Does this really need to be large? */
145 static int maxlcid
= LCID_MAX
;
147 LIST_HEAD(lctxlist
, lctx
);
148 static struct lctxlist alllctx
;
150 lck_mtx_t alllctx_lock
;
151 lck_grp_t
* lctx_lck_grp
;
152 lck_grp_attr_t
* lctx_lck_grp_attr
;
153 lck_attr_t
* lctx_lck_attr
;
155 static void lctxinit(void);
158 #define __PROC_INTERNAL_DEBUG 1
159 /* Name to give to core files */
160 __private_extern__
char corefilename
[MAXPATHLEN
+1] = {"/cores/core.%P"};
162 static void orphanpg(struct pgrp
*pg
);
163 void proc_name_kdp(task_t t
, char * buf
, int size
);
164 char *proc_name_address(void *p
);
166 static proc_t
proc_refinternal_locked(proc_t p
);
167 static void pgrp_add(struct pgrp
* pgrp
, proc_t parent
, proc_t child
);
168 static void pgrp_remove(proc_t p
);
169 static void pgrp_replace(proc_t p
, struct pgrp
*pgrp
);
170 static void pgdelete_dropref(struct pgrp
*pgrp
);
171 static proc_t
proc_find_zombref(int pid
);
172 static void proc_drop_zombref(proc_t p
);
173 extern void pg_rele_dropref(struct pgrp
* pgrp
);
175 struct fixjob_iterargs
{
177 struct session
* mysession
;
181 int fixjob_callback(proc_t
, void *);
184 * Initialize global process hashing structures.
190 LIST_INIT(&zombproc
);
191 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
192 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
193 sesshashtbl
= hashinit(maxproc
/ 4, M_PROC
, &sesshash
);
194 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
201 * Change the count associated with number of processes
202 * a given user is using. This routine protects the uihash
206 chgproccnt(uid_t uid
, int diff
)
209 struct uidinfo
*newuip
= NULL
;
210 struct uihashhead
*uipp
;
216 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
)
217 if (uip
->ui_uid
== uid
)
220 uip
->ui_proccnt
+= diff
;
221 if (uip
->ui_proccnt
> 0) {
222 retval
= uip
->ui_proccnt
;
226 if (uip
->ui_proccnt
< 0)
227 panic("chgproccnt: procs < 0");
228 LIST_REMOVE(uip
, ui_hash
);
231 FREE_ZONE(uip
, sizeof(*uip
), M_PROC
);
240 panic("chgproccnt: lost user");
242 if (newuip
!= NULL
) {
245 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
247 uip
->ui_proccnt
= diff
;
253 MALLOC_ZONE(newuip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
255 panic("chgproccnt: M_PROC zone depleted");
259 FREE_ZONE(newuip
, sizeof(*uip
), M_PROC
);
264 * Is p an inferior of the current process?
272 for (; p
!= current_proc(); p
= p
->p_pptr
)
282 * Is p an inferior of t ?
285 isinferior(proc_t p
, proc_t t
)
289 /* if p==t they are not inferior */
294 for (; p
!= t
; p
= p
->p_pptr
)
304 proc_isinferior(int pid1
, int pid2
)
306 proc_t p
= PROC_NULL
;
307 proc_t t
= PROC_NULL
;
310 if (((p
= proc_find(pid1
)) != (proc_t
)0 ) && ((t
= proc_find(pid2
)) != (proc_t
)0))
311 retval
= isinferior(p
, t
);
324 return(proc_findinternal(pid
, 0));
328 proc_findinternal(int pid
, int locked
)
330 proc_t p
= PROC_NULL
;
336 p
= pfind_locked(pid
);
337 if ((p
== PROC_NULL
) || (p
!= proc_refinternal_locked(p
)))
365 if (p
!= proc_refinternal_locked(p
))
373 proc_refinternal_locked(proc_t p
)
377 /* if process still in creation return failure */
378 if ((p
== PROC_NULL
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0))
380 /* do not return process marked for termination */
381 if ((p
->p_stat
!= SZOMB
) && ((p
->p_listflag
& P_LIST_EXITED
) == 0) && ((p
->p_listflag
& (P_LIST_DRAINWAIT
| P_LIST_DRAIN
| P_LIST_DEAD
)) == 0))
390 proc_rele_locked(proc_t p
)
393 if (p
->p_refcount
> 0) {
395 if ((p
->p_refcount
== 0) && ((p
->p_listflag
& P_LIST_DRAINWAIT
) == P_LIST_DRAINWAIT
)) {
396 p
->p_listflag
&= ~P_LIST_DRAINWAIT
;
397 wakeup(&p
->p_refcount
);
400 panic("proc_rele_locked -ve ref\n");
405 proc_find_zombref(int pid
)
407 proc_t p1
= PROC_NULL
;
408 proc_t p
= PROC_NULL
;
412 p
= pfind_locked(pid
);
414 /* if process still in creation return NULL */
415 if ((p
== PROC_NULL
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0)) {
420 /* if process has not started exit or is being reaped, return NULL */
421 if (((p
->p_listflag
& P_LIST_EXITED
) != 0) && ((p
->p_listflag
& P_LIST_WAITING
) == 0)) {
422 p
->p_listflag
|= P_LIST_WAITING
;
433 proc_drop_zombref(proc_t p
)
436 if ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
437 p
->p_listflag
&= ~P_LIST_WAITING
;
445 proc_refdrain(proc_t p
)
450 p
->p_listflag
|= P_LIST_DRAIN
;
451 while (p
->p_refcount
) {
452 p
->p_listflag
|= P_LIST_DRAINWAIT
;
453 msleep(&p
->p_refcount
, proc_list_mlock
, 0, "proc_refdrain", 0) ;
455 p
->p_listflag
&= ~P_LIST_DRAIN
;
456 p
->p_listflag
|= P_LIST_DEAD
;
464 proc_parentholdref(proc_t p
)
466 proc_t parent
= PROC_NULL
;
474 if ((pp
== PROC_NULL
) || (pp
->p_stat
== SZOMB
) || ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
))) {
479 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == P_LIST_CHILDDRSTART
) {
480 pp
->p_listflag
|= P_LIST_CHILDDRWAIT
;
481 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
490 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == 0) {
501 proc_parentdropref(proc_t p
, int listlocked
)
506 if (p
->p_parentref
> 0) {
508 if ((p
->p_parentref
== 0) && ((p
->p_listflag
& P_LIST_PARENTREFWAIT
) == P_LIST_PARENTREFWAIT
)) {
509 p
->p_listflag
&= ~P_LIST_PARENTREFWAIT
;
510 wakeup(&p
->p_parentref
);
513 panic("proc_parentdropref -ve ref\n");
521 proc_childdrainstart(proc_t p
)
523 #if __PROC_INTERNAL_DEBUG
524 if ((p
->p_listflag
& P_LIST_CHILDDRSTART
) == P_LIST_CHILDDRSTART
)
525 panic("proc_childdrainstart: childdrain already started\n");
527 p
->p_listflag
|= P_LIST_CHILDDRSTART
;
528 /* wait for all that hold parentrefs to drop */
529 while (p
->p_parentref
> 0) {
530 p
->p_listflag
|= P_LIST_PARENTREFWAIT
;
531 msleep(&p
->p_parentref
, proc_list_mlock
, 0, "proc_childdrainstart", 0) ;
537 proc_childdrainend(proc_t p
)
539 #if __PROC_INTERNAL_DEBUG
540 if (p
->p_childrencnt
> 0)
541 panic("exiting: children stil hanging around\n");
543 p
->p_listflag
|= P_LIST_CHILDDRAINED
;
544 if ((p
->p_listflag
& (P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
)) != 0) {
545 p
->p_listflag
&= ~(P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
);
546 wakeup(&p
->p_childrencnt
);
551 proc_checkdeadrefs(proc_t p
)
553 //#if __PROC_INTERNAL_DEBUG
554 if ((p
->p_listflag
& P_LIST_INHASH
) != 0)
555 panic("proc being freed and still in hash %x: %x\n", (unsigned int)p
, (unsigned int)p
->p_listflag
);
556 if (p
->p_childrencnt
!= 0)
557 panic("proc being freed and pending children cnt %x:%x\n", (unsigned int)p
, (unsigned int)p
->p_childrencnt
);
558 if (p
->p_refcount
!= 0)
559 panic("proc being freed and pending refcount %x:%x\n", (unsigned int)p
, (unsigned int)p
->p_refcount
);
560 if (p
->p_parentref
!= 0)
561 panic("proc being freed and pending parentrefs %x:%x\n", (unsigned int)p
, (unsigned int)p
->p_parentref
);
580 proc_t p
= current_proc();
587 proc_t p
= current_proc();
592 proc_parent(proc_t p
)
600 parent
= proc_refinternal_locked(pp
);
601 if ((parent
== PROC_NULL
) && (pp
!= PROC_NULL
) && (pp
->p_stat
!= SZOMB
) && ((pp
->p_listflag
& P_LIST_EXITED
) != 0) && ((pp
->p_listflag
& P_LIST_CHILDDRAINED
)== 0)){
602 pp
->p_listflag
|= P_LIST_CHILDLKWAIT
;
603 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
612 proc_name(int pid
, char * buf
, int size
)
616 if ((p
= proc_find(pid
)) != PROC_NULL
) {
617 strlcpy(buf
, &p
->p_comm
[0], size
);
623 proc_name_kdp(task_t t
, char * buf
, int size
)
625 proc_t p
= get_bsdtask_info(t
);
628 strlcpy(buf
, &p
->p_comm
[0], size
);
632 proc_name_address(void *p
)
634 return &((proc_t
)p
)->p_comm
[0];
638 proc_selfname(char * buf
, int size
)
642 if ((p
= current_proc())!= (proc_t
)0) {
643 strlcpy(buf
, &p
->p_comm
[0], size
);
648 proc_signal(int pid
, int signum
)
652 if ((p
= proc_find(pid
)) != PROC_NULL
) {
659 proc_issignal(int pid
, sigset_t mask
)
664 if ((p
= proc_find(pid
)) != PROC_NULL
) {
665 error
= proc_pendingsignals(p
, mask
);
673 proc_noremotehang(proc_t p
)
678 retval
= p
->p_flag
& P_NOREMOTEHANG
;
679 return(retval
? 1: 0);
684 proc_exiting(proc_t p
)
689 retval
= p
->p_lflag
& P_LEXIT
;
690 return(retval
? 1: 0);
694 proc_forcequota(proc_t p
)
699 retval
= p
->p_flag
& P_FORCEQUOTA
;
700 return(retval
? 1: 0);
710 retval
= p
->p_flag
& P_TBE
;
711 return(retval
? 1: 0);
718 kauth_cred_t my_cred
;
721 my_cred
= kauth_cred_proc_ref(p
);
722 error
= suser(my_cred
, &p
->p_acflag
);
723 kauth_cred_unref(&my_cred
);
728 * Obtain the first thread in a process
730 * XXX This is a bad thing to do; it exists predominantly to support the
731 * XXX use of proc_t's in places that should really be using
732 * XXX thread_t's instead. This maintains historical behaviour, but really
733 * XXX needs an audit of the context (proxy vs. not) to clean up.
736 proc_thread(proc_t proc
)
738 uthread_t uth
= TAILQ_FIRST(&proc
->p_uthlist
);
741 return(uth
->uu_context
.vc_thread
);
753 proc_is64bit(proc_t p
)
755 return(IS_64BIT_PROCESS(p
));
759 bsd_set_dependency_capable(task_t task
)
761 proc_t p
= get_bsdtask_info(task
);
764 OSBitOrAtomic(P_DEPENDENCY_CAPABLE
, (UInt32
*)&p
->p_flag
);
769 /* LP64todo - figure out how to identify 64-bit processes if NULL procp */
771 IS_64BIT_PROCESS(proc_t p
)
773 if (p
&& (p
->p_flag
& P_LP64
))
780 * Locate a process by number
783 pfind_locked(pid_t pid
)
793 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
) {
794 if (p
->p_pid
== pid
) {
796 for (q
= p
->p_hash
.le_next
; q
!= 0; q
= q
->p_hash
.le_next
) {
797 if ((p
!=q
) && (q
->p_pid
== pid
))
798 panic("two procs with same pid %x:%x:%d:%d\n", (unsigned int)p
, (unsigned int)q
, p
->p_pid
, q
->p_pid
);
808 * Locate a zombie by PID
810 __private_extern__ proc_t
818 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
828 * Locate a process group by number
837 pgrp
= pgfind_internal(pgid
);
838 if ((pgrp
== NULL
) || ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) != 0))
849 pgfind_internal(pid_t pgid
)
853 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
)
854 if (pgrp
->pg_id
== pgid
)
860 pg_rele(struct pgrp
* pgrp
)
862 if(pgrp
== PGRP_NULL
)
864 pg_rele_dropref(pgrp
);
868 pg_rele_dropref(struct pgrp
* pgrp
)
871 if ((pgrp
->pg_refcount
== 1) && ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) == PGRP_FLAG_TERMINATE
)) {
873 pgdelete_dropref(pgrp
);
882 session_find_internal(pid_t sessid
)
884 struct session
*sess
;
886 for (sess
= SESSHASH(sessid
)->lh_first
; sess
!= 0; sess
= sess
->s_hash
.le_next
)
887 if (sess
->s_sid
== sessid
)
894 * Make a new process ready to become a useful member of society by making it
895 * visible in all the right places and initialize its own lists to empty.
897 * Parameters: parent The parent of the process to insert
898 * child The child process to insert
902 * Notes: Insert a child process into the parents process group, assign
903 * the child the parent process pointer and PPID of the parent,
904 * place it on the parents p_children list as a sibling,
905 * initialize its own child list, place it in the allproc list,
906 * insert it in the proper hash bucket, and initialize its
910 pinsertchild(proc_t parent
, proc_t child
)
914 LIST_INIT(&child
->p_children
);
915 TAILQ_INIT(&child
->p_evlist
);
916 child
->p_pptr
= parent
;
917 child
->p_ppid
= parent
->p_pid
;
919 pg
= proc_pgrp(parent
);
920 pgrp_add(pg
, parent
, child
);
924 parent
->p_childrencnt
++;
925 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
927 LIST_INSERT_HEAD(&allproc
, child
, p_list
);
928 /* mark the completion of proc creation */
929 child
->p_listflag
&= ~P_LIST_INCREATE
;
936 * Move p to a new or existing process group (and session)
939 * ESRCH No such process
942 enterpgrp(proc_t p
, pid_t pgid
, int mksess
)
946 struct session
* procsp
;
949 mypgrp
= proc_pgrp(p
);
950 procsp
= proc_session(p
);
953 if (pgrp
!= NULL
&& mksess
) /* firewalls */
954 panic("enterpgrp: setsid into non-empty pgrp");
955 if (SESS_LEADER(p
, procsp
))
956 panic("enterpgrp: session leader attempted setpgrp");
958 if (pgrp
== PGRP_NULL
) {
959 pid_t savepid
= p
->p_pid
;
960 proc_t np
= PROC_NULL
;
965 if (p
->p_pid
!= pgid
)
966 panic("enterpgrp: new pgrp and pid != pgid");
968 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
971 panic("enterpgrp: M_PGRP zone depleted");
972 if ((np
= proc_find(savepid
)) == NULL
|| np
!= p
) {
975 if (mypgrp
!= PGRP_NULL
)
977 if (procsp
!= SESSION_NULL
)
978 session_rele(procsp
);
979 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
984 struct session
*sess
;
989 MALLOC_ZONE(sess
, struct session
*,
990 sizeof(struct session
), M_SESSION
, M_WAITOK
);
992 panic("enterpgrp: M_SESSION zone depleted");
994 sess
->s_sid
= p
->p_pid
;
996 sess
->s_ttyvp
= NULL
;
999 sess
->s_listflags
= 0;
1000 sess
->s_ttypgrpid
= NO_PID
;
1001 lck_mtx_init(&sess
->s_mlock
, proc_lck_grp
, proc_lck_attr
);
1002 bcopy(procsp
->s_login
, sess
->s_login
,
1003 sizeof(sess
->s_login
));
1004 OSBitAndAtomic(~((uint32_t)P_CONTROLT
), (UInt32
*)&p
->p_flag
);
1006 LIST_INSERT_HEAD(SESSHASH(sess
->s_sid
), sess
, s_hash
);
1008 pgrp
->pg_session
= sess
;
1010 if (p
!= current_proc())
1011 panic("enterpgrp: mksession and p != curproc");
1015 pgrp
->pg_session
= procsp
;
1017 if ((pgrp
->pg_session
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1018 panic("enterpgrp: providing ref to terminating session ");
1019 pgrp
->pg_session
->s_count
++;
1023 lck_mtx_init(&pgrp
->pg_mlock
, proc_lck_grp
, proc_lck_attr
);
1024 LIST_INIT(&pgrp
->pg_members
);
1025 pgrp
->pg_membercnt
= 0;
1028 pgrp
->pg_refcount
= 1;
1029 pgrp
->pg_listflags
= 0;
1030 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
1032 } else if (pgrp
== mypgrp
) {
1036 if (procsp
!= SESSION_NULL
)
1037 session_rele(procsp
);
1041 if (procsp
!= SESSION_NULL
)
1042 session_rele(procsp
);
1044 * Adjust eligibility of affected pgrps to participate in job control.
1045 * Increment eligibility counts before decrementing, otherwise we
1046 * could reach 0 spuriously during the first call.
1048 fixjobc(p
, pgrp
, 1);
1049 fixjobc(p
, mypgrp
, 0);
1051 if(mypgrp
!= PGRP_NULL
)
1053 pgrp_replace(p
, pgrp
);
1060 * remove process from process group
1071 * delete a process group
1074 pgdelete_dropref(struct pgrp
*pgrp
)
1079 struct session
*sessp
;
1083 if (pgrp
->pg_membercnt
!= 0) {
1089 pgrp
->pg_refcount
--;
1090 if ((emptypgrp
== 0) || (pgrp
->pg_membercnt
!= 0)) {
1095 pgrp
->pg_listflags
|= PGRP_FLAG_TERMINATE
;
1097 if (pgrp
->pg_refcount
> 0) {
1102 pgrp
->pg_listflags
|= PGRP_FLAG_DEAD
;
1103 LIST_REMOVE(pgrp
, pg_hash
);
1107 fstate
= thread_funnel_set(kernel_flock
, TRUE
);
1109 ttyp
= pgrp
->pg_session
->s_ttyp
;
1110 if ((ttyp
!= NULL
) && (pgrp
->pg_session
->s_ttyp
->t_pgrp
== pgrp
)) {
1111 pgrp
->pg_session
->s_ttyp
->t_pgrp
= NULL
;
1112 pgrp
->pg_session
->s_ttypgrpid
= NO_PID
;
1114 (void) thread_funnel_set(kernel_flock
, fstate
);
1118 sessp
= pgrp
->pg_session
;
1119 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1120 panic("pg_deleteref: manipulating refs of already terminating session");
1121 if (--sessp
->s_count
== 0) {
1122 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1123 panic("pg_deleteref: terminating already terminated session");
1124 sessp
->s_listflags
|= S_LIST_TERM
;
1125 ttyp
= sessp
->s_ttyp
;
1126 LIST_REMOVE(sessp
, s_hash
);
1128 fstate
= thread_funnel_set(kernel_flock
, TRUE
);
1129 if (ttyp
!= NULL
&& ttyp
->t_session
== sessp
)
1130 ttyp
->t_session
= NULL
;
1131 (void) thread_funnel_set(kernel_flock
, fstate
);
1133 sessp
->s_listflags
|= S_LIST_DEAD
;
1134 if (sessp
->s_count
!= 0)
1135 panic("pg_deleteref: freeing session in use");
1137 lck_mtx_destroy(&sessp
->s_mlock
, proc_lck_grp
);
1138 FREE_ZONE(sessp
, sizeof(struct session
), M_SESSION
);
1141 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_lck_grp
);
1142 FREE_ZONE(pgrp
, sizeof(*pgrp
), M_PGRP
);
1147 * Adjust pgrp jobc counters when specified process changes process group.
1148 * We count the number of processes in each process group that "qualify"
1149 * the group for terminal job control (those with a parent in a different
1150 * process group of the same session). If that count reaches zero, the
1151 * process group becomes orphaned. Check both the specified process'
1152 * process group and that of its children.
1153 * entering == 0 => p is leaving specified group.
1154 * entering == 1 => p is entering specified group.
1157 fixjob_callback(proc_t p
, void * arg
)
1159 struct fixjob_iterargs
*fp
;
1160 struct pgrp
* pg
, *hispg
;
1161 struct session
* mysession
, *hissess
;
1164 fp
= (struct fixjob_iterargs
*)arg
;
1166 mysession
= fp
->mysession
;
1167 entering
= fp
->entering
;
1169 hispg
= proc_pgrp(p
);
1170 hissess
= proc_session(p
);
1172 if ((hispg
!= pg
) &&
1173 (hissess
== mysession
)) {
1178 } else if (--hispg
->pg_jobc
== 0) {
1184 if (hissess
!= SESSION_NULL
)
1185 session_rele(hissess
);
1186 if (hispg
!= PGRP_NULL
)
1189 return(PROC_RETURNED
);
1193 fixjobc(proc_t p
, struct pgrp
*pgrp
, int entering
)
1195 struct pgrp
*hispgrp
= PGRP_NULL
;
1196 struct session
*hissess
= SESSION_NULL
;
1197 struct session
*mysession
= pgrp
->pg_session
;
1199 struct fixjob_iterargs fjarg
;
1201 parent
= proc_parent(p
);
1202 if (parent
!= PROC_NULL
) {
1203 hispgrp
= proc_pgrp(parent
);
1204 hissess
= proc_session(parent
);
1210 * Check p's parent to see whether p qualifies its own process
1211 * group; if so, adjust count for p's process group.
1213 if ((hispgrp
!= pgrp
) &&
1214 (hissess
== mysession
)) {
1219 }else if (--pgrp
->pg_jobc
== 0) {
1226 if (hissess
!= SESSION_NULL
)
1227 session_rele(hissess
);
1228 if (hispgrp
!= PGRP_NULL
)
1232 * Check this process' children to see whether they qualify
1233 * their process groups; if so, adjust counts for children's
1237 fjarg
.mysession
= mysession
;
1238 fjarg
.entering
= entering
;
1239 proc_childrenwalk(p
, fixjob_callback
, &fjarg
);
1243 * A process group has become orphaned;
1244 * if there are any stopped processes in the group,
1245 * hang-up all process in that group.
1248 orphanpg(struct pgrp
* pgrp
)
1252 int count
, pidcount
, i
, alloc_count
;
1254 if (pgrp
== PGRP_NULL
)
1258 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
1259 if (p
->p_stat
== SSTOP
) {
1260 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1261 p
= p
->p_pglist
.le_next
)
1263 break; /* ??? stops after finding one.. */
1269 if (count
> hard_maxproc
)
1270 count
= hard_maxproc
;
1271 alloc_count
= count
* sizeof(pid_t
);
1272 pid_list
= (pid_t
*)kalloc(alloc_count
);
1273 bzero(pid_list
, alloc_count
);
1277 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1278 p
= p
->p_pglist
.le_next
) {
1279 if (p
->p_stat
== SSTOP
) {
1280 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1281 p
= p
->p_pglist
.le_next
) {
1282 pid_list
[pidcount
] = p
->p_pid
;
1284 if (pidcount
>= count
)
1287 break; /* ??? stops after finding one.. */
1296 for (i
= 0; i
< pidcount
; i
++) {
1297 /* No handling or proc0 */
1298 if (pid_list
[i
] == 0)
1300 p
= proc_find(pid_list
[i
]);
1302 proc_transwait(p
, 0);
1305 psignal(p
, SIGCONT
);
1310 kfree(pid_list
, alloc_count
);
1316 /* XXX should be __private_extern__ */
1318 proc_is_classic(proc_t p
)
1320 return (p
->p_flag
& P_TRANSLATED
) ? 1 : 0;
1323 /* XXX Why does this function exist? Need to kill it off... */
1325 current_proc_EXTERNAL(void)
1327 return (current_proc());
1331 * proc_core_name(name, uid, pid)
1332 * Expand the name described in corefilename, using name, uid, and pid.
1333 * corefilename is a printf-like string, with three format specifiers:
1334 * %N name of process ("name")
1335 * %P process id (pid)
1337 * For example, "%N.core" is the default; they can be disabled completely
1338 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1339 * This is controlled by the sysctl variable kern.corefile (see above).
1341 __private_extern__
int
1342 proc_core_name(const char *name
, uid_t uid
, pid_t pid
, char *cf_name
,
1345 const char *format
, *appendstr
;
1346 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
1349 if (cf_name
== NULL
)
1352 format
= corefilename
;
1353 for (i
= 0, n
= 0; n
< cf_name_len
&& format
[i
]; i
++) {
1354 switch (format
[i
]) {
1355 case '%': /* Format character */
1357 switch (format
[i
]) {
1361 case 'N': /* process name */
1364 case 'P': /* process id */
1365 snprintf(id_buf
, sizeof(id_buf
), "%u", pid
);
1368 case 'U': /* user id */
1369 snprintf(id_buf
, sizeof(id_buf
), "%u", uid
);
1375 "Unknown format character %c in `%s'\n",
1378 l
= strlen(appendstr
);
1379 if ((n
+ l
) >= cf_name_len
)
1381 bcopy(appendstr
, cf_name
+ n
, l
);
1385 cf_name
[n
++] = format
[i
];
1388 if (format
[i
] != '\0')
1392 log(LOG_ERR
, "pid %ld (%s), uid (%lu): corename is too long\n",
1393 (long)pid
, name
, (u_long
)uid
);
1402 LIST_INIT(&alllctx
);
1405 /* allocate lctx lock group attribute and group */
1406 lctx_lck_grp_attr
= lck_grp_attr_alloc_init();
1407 lck_grp_attr_setstat(lctx_lck_grp_attr
);
1409 lctx_lck_grp
= lck_grp_alloc_init("lctx", lctx_lck_grp_attr
);
1410 /* Allocate lctx lock attribute */
1411 lctx_lck_attr
= lck_attr_alloc_init();
1413 lck_mtx_init(&alllctx_lock
, lctx_lck_grp
, lctx_lck_attr
);
1417 * Locate login context by number.
1425 LIST_FOREACH(l
, &alllctx
, lc_list
) {
1426 if (l
->lc_id
== lcid
) {
1438 if (lastlcid > maxlcid) \
1448 /* Not very efficient but this isn't a common operation. */
1449 while ((l
= lcfind(lastlcid
)) != NULL
) {
1456 MALLOC(l
, struct lctx
*, sizeof(struct lctx
), M_LCTX
, M_WAITOK
|M_ZERO
);
1458 LIST_INIT(&l
->lc_members
);
1459 lck_mtx_init(&l
->lc_mtx
, lctx_lck_grp
, lctx_lck_attr
);
1461 l
->lc_label
= mac_lctx_label_alloc();
1464 LIST_INSERT_HEAD(&alllctx
, l
, lc_list
);
1472 * Call with proc protected (either by being invisible
1473 * or by having the all-login-context lock held) and
1476 * Will unlock lctx on return.
1479 enterlctx (proc_t p
, struct lctx
*l
, __unused
int create
)
1485 LIST_INSERT_HEAD(&l
->lc_members
, p
, p_lclist
);
1490 mac_lctx_notify_create(p
, l
);
1492 mac_lctx_notify_join(p
, l
);
1500 * Remove process from login context (if any). Called with p protected by
1504 leavelctx (proc_t p
)
1508 if (p
->p_lctx
== NULL
) {
1512 LCTX_LOCK(p
->p_lctx
);
1515 LIST_REMOVE(p
, p_lclist
);
1518 mac_lctx_notify_leave(p
, l
);
1520 if (LIST_EMPTY(&l
->lc_members
)) {
1521 LIST_REMOVE(l
, lc_list
);
1524 lck_mtx_destroy(&l
->lc_mtx
, lctx_lck_grp
);
1526 mac_lctx_label_free(l
->lc_label
);
1537 sysctl_kern_lctx SYSCTL_HANDLER_ARGS
1539 int *name
= (int*) arg1
;
1540 u_int namelen
= arg2
;
1541 struct kinfo_lctx kil
;
1547 switch (oidp
->oid_number
) {
1550 /* Request for size. */
1552 error
= SYSCTL_OUT(req
, 0,
1553 sizeof(struct kinfo_lctx
) * (alllctx_cnt
+ 1));
1558 case KERN_LCTX_LCID
:
1560 if (req
->oldlen
< sizeof(struct kinfo_lctx
))
1565 /* No login context */
1566 l
= lcfind((pid_t
)name
[0]);
1572 return (SYSCTL_OUT(req
, (caddr_t
)&kil
, sizeof(kil
)));
1578 /* Provided buffer is too small. */
1579 if (req
->oldlen
< (sizeof(struct kinfo_lctx
) * alllctx_cnt
)) {
1584 LIST_FOREACH(l
, &alllctx
, lc_list
) {
1589 error
= SYSCTL_OUT(req
, (caddr_t
)&kil
, sizeof(kil
));
1599 SYSCTL_NODE(_kern
, KERN_LCTX
, lctx
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Login Context");
1601 SYSCTL_PROC(_kern_lctx
, KERN_LCTX_ALL
, all
, CTLFLAG_RD
|CTLTYPE_STRUCT
,
1602 0, 0, sysctl_kern_lctx
, "S,lctx",
1603 "Return entire login context table");
1604 SYSCTL_NODE(_kern_lctx
, KERN_LCTX_LCID
, lcid
, CTLFLAG_RD
,
1605 sysctl_kern_lctx
, "Login Context Table");
1606 SYSCTL_INT(_kern_lctx
, OID_AUTO
, last
, CTLFLAG_RD
, &lastlcid
, 0, "");
1607 SYSCTL_INT(_kern_lctx
, OID_AUTO
, count
, CTLFLAG_RD
, &alllctx_cnt
, 0, "");
1608 SYSCTL_INT(_kern_lctx
, OID_AUTO
, max
, CTLFLAG_RW
, &maxlcid
, 0, "");
1612 /* Code Signing related routines */
1615 csops(__unused proc_t p
, struct csops_args
*uap
, __unused register_t
*retval
)
1618 pid_t pid
= uap
->pid
;
1619 user_addr_t uaddr
= uap
->useraddr
;
1620 size_t usize
= (size_t)CAST_DOWN(size_t, uap
->usersize
);
1628 unsigned char cdhash
[SHA1_RESULTLEN
];
1630 forself
= error
= 0;
1633 pid
= proc_selfpid();
1634 if (pid
== proc_selfpid())
1638 /* Pre flight checks for CS_OPS_PIDPATH */
1639 if (ops
== CS_OPS_PIDPATH
) {
1640 /* usize is unsigned.. */
1641 if (usize
> 4 * PATH_MAX
)
1643 if (kauth_cred_issuser(kauth_cred_get()) != TRUE
)
1645 } else if ((forself
== 0) && ((ops
!= CS_OPS_STATUS
) && (ops
!= CS_OPS_CDHASH
) && (kauth_cred_issuser(kauth_cred_get()) != TRUE
))) {
1649 pt
= proc_find(pid
);
1650 if (pt
== PROC_NULL
)
1658 retflags
= pt
->p_csflags
;
1659 if (uaddr
!= USER_ADDR_NULL
)
1660 error
= copyout(&retflags
, uaddr
, sizeof(uint32_t));
1663 case CS_OPS_MARKINVALID
:
1665 if ((pt
->p_csflags
& CS_VALID
) == CS_VALID
) { /* is currently valid */
1666 pt
->p_csflags
&= ~CS_VALID
; /* set invalid */
1667 if ((pt
->p_csflags
& CS_KILL
) == CS_KILL
) {
1669 psignal(pt
, SIGKILL
);
1677 case CS_OPS_MARKHARD
:
1679 pt
->p_csflags
|= CS_HARD
;
1680 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1681 /* @@@ allow? reject? kill? @@@ */
1689 case CS_OPS_MARKKILL
:
1691 pt
->p_csflags
|= CS_KILL
;
1692 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1694 psignal(pt
, SIGKILL
);
1699 case CS_OPS_PIDPATH
:
1701 vid
= vnode_vid(tvp
);
1705 buf
= (char *)kalloc(usize
);
1710 error
= vnode_getwithvid(tvp
, vid
);
1714 error
= vn_getpath(tvp
, buf
, &len
);
1717 error
= copyout(buf
, uaddr
, usize
);
1724 if (usize
!= SHA1_RESULTLEN
) {
1729 /* pt already holds a reference on its p_textvp */
1731 toff
= pt
->p_textoff
;
1733 error
= vn_getcdhash(tvp
, toff
, cdhash
);
1737 error
= copyout(cdhash
, uaddr
, sizeof (cdhash
));
1753 proc_iterate(flags
, callout
, arg
, filterfn
, filterarg
)
1755 int (*callout
)(proc_t
, void *);
1757 int (*filterfn
)(proc_t
, void *);
1762 int count
, pidcount
, alloc_count
, i
, retval
;
1765 if (count
> hard_maxproc
)
1766 count
= hard_maxproc
;
1767 alloc_count
= count
* sizeof(pid_t
);
1768 pid_list
= (pid_t
*)kalloc(alloc_count
);
1769 bzero(pid_list
, alloc_count
);
1776 if (flags
& PROC_ALLPROCLIST
) {
1777 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
1778 if (p
->p_stat
== SIDL
)
1780 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
1781 pid_list
[pidcount
] = p
->p_pid
;
1783 if (pidcount
>= count
)
1788 if ((pidcount
< count
) && (flags
& PROC_ZOMBPROCLIST
)) {
1789 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1790 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
1791 pid_list
[pidcount
] = p
->p_pid
;
1793 if (pidcount
>= count
)
1803 for (i
= 0; i
< pidcount
; i
++) {
1804 p
= proc_find(pid_list
[i
]);
1806 if ((flags
& PROC_NOWAITTRANS
) == 0)
1807 proc_transwait(p
, 0);
1808 retval
= callout(p
, arg
);
1812 case PROC_RETURNED_DONE
:
1814 if (retval
== PROC_RETURNED_DONE
) {
1819 case PROC_CLAIMED_DONE
:
1825 } else if (flags
& PROC_ZOMBPROCLIST
) {
1826 p
= proc_find_zombref(pid_list
[i
]);
1827 if (p
!= PROC_NULL
) {
1828 retval
= callout(p
, arg
);
1832 case PROC_RETURNED_DONE
:
1833 proc_drop_zombref(p
);
1834 if (retval
== PROC_RETURNED_DONE
) {
1839 case PROC_CLAIMED_DONE
:
1850 kfree(pid_list
, alloc_count
);
1857 /* This is for iteration in case of trivial non blocking callouts */
1859 proc_scanall(flags
, callout
, arg
)
1861 int (*callout
)(proc_t
, void *);
1871 if (flags
& PROC_ALLPROCLIST
) {
1872 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
1873 retval
= callout(p
, arg
);
1874 if (retval
== PROC_RETURNED_DONE
)
1878 if (flags
& PROC_ZOMBPROCLIST
) {
1879 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1880 retval
= callout(p
, arg
);
1881 if (retval
== PROC_RETURNED_DONE
)
1895 proc_rebootscan(callout
, arg
, filterfn
, filterarg
)
1896 int (*callout
)(proc_t
, void *);
1898 int (*filterfn
)(proc_t
, void *);
1902 int lockheld
= 0, retval
;
1909 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
1910 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
1911 p
= proc_refinternal_locked(p
);
1917 proc_transwait(p
, 0);
1918 retval
= callout(p
, arg
);
1922 case PROC_RETURNED_DONE
:
1923 case PROC_CLAIMED_DONE
:
1927 goto ps_allprocscan
;
1929 } /* allproc walk thru */
1931 if (lockheld
== 1) {
1943 proc_childrenwalk(parent
, callout
, arg
)
1944 struct proc
* parent
;
1945 int (*callout
)(proc_t
, void *);
1948 register struct proc
*p
;
1950 int count
, pidcount
, alloc_count
, i
, retval
;
1953 if (count
> hard_maxproc
)
1954 count
= hard_maxproc
;
1955 alloc_count
= count
* sizeof(pid_t
);
1956 pid_list
= (pid_t
*)kalloc(alloc_count
);
1957 bzero(pid_list
, alloc_count
);
1964 for (p
= parent
->p_children
.lh_first
; (p
!= 0); p
= p
->p_sibling
.le_next
) {
1965 if (p
->p_stat
== SIDL
)
1967 pid_list
[pidcount
] = p
->p_pid
;
1969 if (pidcount
>= count
)
1975 for (i
= 0; i
< pidcount
; i
++) {
1976 p
= proc_find(pid_list
[i
]);
1978 proc_transwait(p
, 0);
1979 retval
= callout(p
, arg
);
1983 case PROC_RETURNED_DONE
:
1985 if (retval
== PROC_RETURNED_DONE
) {
1990 case PROC_CLAIMED_DONE
:
2000 kfree(pid_list
, alloc_count
);
2007 /* PGRP_BLOCKITERATE is not implemented yet */
2009 pgrp_iterate(pgrp
, flags
, callout
, arg
, filterfn
, filterarg
)
2012 int (*callout
)(proc_t
, void *);
2014 int (*filterfn
)(proc_t
, void *);
2019 int count
, pidcount
, i
, alloc_count
;
2022 int dropref
= flags
& PGRP_DROPREF
;
2024 int serialize
= flags
& PGRP_BLOCKITERATE
;
2031 count
= pgrp
->pg_membercnt
+ 10;
2032 if (count
> hard_maxproc
)
2033 count
= hard_maxproc
;
2034 alloc_count
= count
* sizeof(pid_t
);
2035 pid_list
= (pid_t
*)kalloc(alloc_count
);
2036 bzero(pid_list
, alloc_count
);
2039 if (serialize
!= 0) {
2040 while ((pgrp
->pg_listflags
& PGRP_FLAG_ITERABEGIN
) == PGRP_FLAG_ITERABEGIN
) {
2041 pgrp
->pg_listflags
|= PGRP_FLAG_ITERWAIT
;
2042 msleep(&pgrp
->pg_listflags
, &pgrp
->pg_mlock
, 0, "pgrp_iterate", 0);
2044 pgrp
->pg_listflags
|= PGRP_FLAG_ITERABEGIN
;
2050 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
2051 p
= p
->p_pglist
.le_next
) {
2052 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
2053 pid_list
[pidcount
] = p
->p_pid
;
2055 if (pidcount
>= count
)
2062 if ((serialize
== 0) && (dropref
!= 0))
2066 for (i
= 0; i
< pidcount
; i
++) {
2067 /* No handling or proc0 */
2068 if (pid_list
[i
] == 0)
2070 p
= proc_find(pid_list
[i
]);
2072 if (p
->p_pgrpid
!= pgid
) {
2076 proc_transwait(p
, 0);
2077 retval
= callout(p
, arg
);
2081 case PROC_RETURNED_DONE
:
2083 if (retval
== PROC_RETURNED_DONE
) {
2088 case PROC_CLAIMED_DONE
:
2097 if (serialize
!= 0) {
2099 pgrp
->pg_listflags
&= ~PGRP_FLAG_ITERABEGIN
;
2100 if ((pgrp
->pg_listflags
& PGRP_FLAG_ITERWAIT
) == PGRP_FLAG_ITERWAIT
) {
2101 pgrp
->pg_listflags
&= ~PGRP_FLAG_ITERWAIT
;
2102 wakeup(&pgrp
->pg_listflags
);
2108 kfree(pid_list
, alloc_count
);
2113 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2116 child
->p_pgrp
= pgrp
;
2117 child
->p_pgrpid
= pgrp
->pg_id
;
2118 child
->p_listflag
|= P_LIST_INPGRP
;
2120 * When pgrp is being freed , a process can still
2121 * request addition using setpgid from bash when
2122 * login is terminated (login cycler) return ESRCH
2123 * Safe to hold lock due to refcount on pgrp
2125 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2126 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2129 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2130 panic("pgrp_add : pgrp is dead adding process");
2134 pgrp
->pg_membercnt
++;
2135 if ( parent
!= PROC_NULL
) {
2136 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2138 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2143 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2144 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2150 pgrp_remove(struct proc
* p
)
2157 #if __PROC_INTERNAL_DEBUG
2158 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2159 panic("removing from pglist but no named ref\n");
2161 p
->p_pgrpid
= PGRPID_DEAD
;
2162 p
->p_listflag
&= ~P_LIST_INPGRP
;
2166 if (pg
== PGRP_NULL
)
2167 panic("pgrp_remove: pg is NULL");
2171 if (pg
->pg_membercnt
< 0)
2172 panic("pgprp: -ve membercnt pgprp:%x p:%x\n",(unsigned int)pg
, (unsigned int)p
);
2174 LIST_REMOVE(p
, p_pglist
);
2175 if (pg
->pg_members
.lh_first
== 0) {
2177 pgdelete_dropref(pg
);
2185 /* cannot use proc_pgrp as it maybe stalled */
2187 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2189 struct pgrp
* oldpg
;
2195 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2196 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2197 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2200 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2203 if (oldpg
== PGRP_NULL
)
2204 panic("pgrp_replace: oldpg NULL");
2205 oldpg
->pg_refcount
++;
2206 #if __PROC_INTERNAL_DEBUG
2207 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2208 panic("removing from pglist but no named ref\n");
2210 p
->p_pgrpid
= PGRPID_DEAD
;
2211 p
->p_listflag
&= ~P_LIST_INPGRP
;
2217 oldpg
->pg_membercnt
--;
2218 if (oldpg
->pg_membercnt
< 0)
2219 panic("pgprp: -ve membercnt pgprp:%x p:%x\n",(unsigned int)oldpg
, (unsigned int)p
);
2220 LIST_REMOVE(p
, p_pglist
);
2221 if (oldpg
->pg_members
.lh_first
== 0) {
2223 pgdelete_dropref(oldpg
);
2231 p
->p_pgrpid
= newpg
->pg_id
;
2232 p
->p_listflag
|= P_LIST_INPGRP
;
2234 * When pgrp is being freed , a process can still
2235 * request addition using setpgid from bash when
2236 * login is terminated (login cycler) return ESRCH
2237 * Safe to hold lock due to refcount on pgrp
2239 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2240 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2243 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2244 panic("pgrp_add : pgrp is dead adding process");
2248 newpg
->pg_membercnt
++;
2249 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
2253 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
2254 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2257 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
2258 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
2259 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
2260 wakeup(&p
->p_pgrpid
);
2267 pgrp_lock(struct pgrp
* pgrp
)
2269 lck_mtx_lock(&pgrp
->pg_mlock
);
2273 pgrp_unlock(struct pgrp
* pgrp
)
2275 lck_mtx_unlock(&pgrp
->pg_mlock
);
2279 session_lock(struct session
* sess
)
2281 lck_mtx_lock(&sess
->s_mlock
);
2286 session_unlock(struct session
* sess
)
2288 lck_mtx_unlock(&sess
->s_mlock
);
2300 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2301 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2302 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2307 assert(pgrp
!= NULL
);
2309 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0)
2310 panic("proc_pgrp: ref being povided for dead pgrp");
2312 if (pgrp
!= PGRP_NULL
)
2313 pgrp
->pg_refcount
++;
2320 tty_pgrp(struct tty
* tp
)
2322 struct pgrp
* pg
= PGRP_NULL
;
2327 if (pg
!= PGRP_NULL
) {
2328 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0)
2329 panic("tty_pgrp: ref being povided for dead pgrp");
2338 proc_session(proc_t p
)
2340 struct session
* sess
= SESSION_NULL
;
2343 return(SESSION_NULL
);
2347 /* wait during transitions */
2348 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2349 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2350 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2353 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
2354 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2355 panic("proc_session:returning sesssion ref on terminating session");
2363 session_rele(struct session
*sess
)
2366 if (--sess
->s_count
== 0) {
2367 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2368 panic("session_rele: terminating already terminated session");
2369 sess
->s_listflags
|= S_LIST_TERM
;
2370 LIST_REMOVE(sess
, s_hash
);
2371 sess
->s_listflags
|= S_LIST_DEAD
;
2372 if (sess
->s_count
!= 0)
2373 panic("session_rele: freeing session in use");
2375 lck_mtx_destroy(&sess
->s_mlock
, proc_lck_grp
);
2376 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
2382 proc_transstart(proc_t p
, int locked
)
2386 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2387 p
->p_lflag
|= P_LTRANSWAIT
;
2388 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2390 p
->p_lflag
|= P_LINTRANSIT
;
2391 p
->p_transholder
= current_thread();
2399 proc_transend(proc_t p
, int locked
)
2403 p
->p_lflag
&= ~P_LINTRANSIT
;
2405 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2406 p
->p_lflag
&= ~P_LTRANSWAIT
;
2407 wakeup(&p
->p_lflag
);
2409 p
->p_transholder
= NULL
;
2415 proc_transwait(proc_t p
, int locked
)
2419 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2420 p
->p_lflag
|= P_LTRANSWAIT
;
2421 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2428 proc_klist_lock(void)
2430 lck_mtx_lock(proc_klist_mlock
);
2434 proc_klist_unlock(void)
2436 lck_mtx_unlock(proc_klist_mlock
);
2440 proc_knote(struct proc
* p
, long hint
)
2443 KNOTE(&p
->p_klist
, hint
);
2444 proc_klist_unlock();
2448 unsigned long cs_procs_killed
= 0;
2449 unsigned long cs_procs_invalidated
= 0;
2450 int cs_force_kill
= 0;
2451 int cs_force_hard
= 0;
2453 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_kill
, CTLFLAG_RW
, &cs_force_kill
, 0, "");
2454 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_hard
, CTLFLAG_RW
, &cs_force_hard
, 0, "");
2455 SYSCTL_INT(_vm
, OID_AUTO
, cs_debug
, CTLFLAG_RW
, &cs_debug
, 0, "");
2458 cs_invalid_page(void)
2466 * XXX revisit locking when proc is no longer protected
2467 * by the kernel funnel...
2470 /* XXX for testing */
2473 p
->p_csflags
|= CS_KILL
;
2475 p
->p_csflags
|= CS_HARD
;
2477 if (p
->p_csflags
& CS_VALID
) {
2478 p
->p_csflags
&= ~CS_VALID
;
2481 cs_procs_invalidated
++;
2482 printf("CODE SIGNING: cs_invalid_page: "
2483 "p=%d[%s] clearing CS_VALID\n",
2484 p
->p_pid
, p
->p_comm
);
2488 if (p
->p_csflags
& CS_KILL
) {
2491 printf("CODE SIGNING: cs_invalid_page: "
2492 "p=%d[%s] honoring CS_KILL\n",
2493 p
->p_pid
, p
->p_comm
);
2496 psignal(p
, SIGKILL
);
2500 if (p
->p_csflags
& CS_HARD
) {
2503 printf("CODE SIGNING: cs_invalid_page: "
2504 "p=%d[%s] honoring CS_HARD\n",
2505 p
->p_pid
, p
->p_comm
);
2515 printf("CODE SIGNING: cs_invalid_page: "
2516 "p=%d[%s] ignored...\n",
2517 p
->p_pid
, p
->p_comm
);