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
);
1711 error
= vnode_getwithvid(tvp
, vid
);
1715 error
= vn_getpath(tvp
, buf
, &len
);
1718 error
= copyout(buf
, uaddr
, usize
);
1725 if (usize
!= SHA1_RESULTLEN
) {
1730 /* pt already holds a reference on its p_textvp */
1732 toff
= pt
->p_textoff
;
1734 error
= vn_getcdhash(tvp
, toff
, cdhash
);
1738 error
= copyout(cdhash
, uaddr
, sizeof (cdhash
));
1754 proc_iterate(flags
, callout
, arg
, filterfn
, filterarg
)
1756 int (*callout
)(proc_t
, void *);
1758 int (*filterfn
)(proc_t
, void *);
1763 int count
, pidcount
, alloc_count
, i
, retval
;
1766 if (count
> hard_maxproc
)
1767 count
= hard_maxproc
;
1768 alloc_count
= count
* sizeof(pid_t
);
1769 pid_list
= (pid_t
*)kalloc(alloc_count
);
1770 bzero(pid_list
, alloc_count
);
1777 if (flags
& PROC_ALLPROCLIST
) {
1778 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
1779 if (p
->p_stat
== SIDL
)
1781 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
1782 pid_list
[pidcount
] = p
->p_pid
;
1784 if (pidcount
>= count
)
1789 if ((pidcount
< count
) && (flags
& PROC_ZOMBPROCLIST
)) {
1790 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1791 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
1792 pid_list
[pidcount
] = p
->p_pid
;
1794 if (pidcount
>= count
)
1804 for (i
= 0; i
< pidcount
; i
++) {
1805 p
= proc_find(pid_list
[i
]);
1807 if ((flags
& PROC_NOWAITTRANS
) == 0)
1808 proc_transwait(p
, 0);
1809 retval
= callout(p
, arg
);
1813 case PROC_RETURNED_DONE
:
1815 if (retval
== PROC_RETURNED_DONE
) {
1820 case PROC_CLAIMED_DONE
:
1826 } else if (flags
& PROC_ZOMBPROCLIST
) {
1827 p
= proc_find_zombref(pid_list
[i
]);
1828 if (p
!= PROC_NULL
) {
1829 retval
= callout(p
, arg
);
1833 case PROC_RETURNED_DONE
:
1834 proc_drop_zombref(p
);
1835 if (retval
== PROC_RETURNED_DONE
) {
1840 case PROC_CLAIMED_DONE
:
1851 kfree(pid_list
, alloc_count
);
1858 /* This is for iteration in case of trivial non blocking callouts */
1860 proc_scanall(flags
, callout
, arg
)
1862 int (*callout
)(proc_t
, void *);
1872 if (flags
& PROC_ALLPROCLIST
) {
1873 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
1874 retval
= callout(p
, arg
);
1875 if (retval
== PROC_RETURNED_DONE
)
1879 if (flags
& PROC_ZOMBPROCLIST
) {
1880 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1881 retval
= callout(p
, arg
);
1882 if (retval
== PROC_RETURNED_DONE
)
1896 proc_rebootscan(callout
, arg
, filterfn
, filterarg
)
1897 int (*callout
)(proc_t
, void *);
1899 int (*filterfn
)(proc_t
, void *);
1903 int lockheld
= 0, retval
;
1910 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
1911 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
1912 p
= proc_refinternal_locked(p
);
1918 proc_transwait(p
, 0);
1919 retval
= callout(p
, arg
);
1923 case PROC_RETURNED_DONE
:
1924 case PROC_CLAIMED_DONE
:
1928 goto ps_allprocscan
;
1930 } /* allproc walk thru */
1932 if (lockheld
== 1) {
1944 proc_childrenwalk(parent
, callout
, arg
)
1945 struct proc
* parent
;
1946 int (*callout
)(proc_t
, void *);
1949 register struct proc
*p
;
1951 int count
, pidcount
, alloc_count
, i
, retval
;
1954 if (count
> hard_maxproc
)
1955 count
= hard_maxproc
;
1956 alloc_count
= count
* sizeof(pid_t
);
1957 pid_list
= (pid_t
*)kalloc(alloc_count
);
1958 bzero(pid_list
, alloc_count
);
1965 for (p
= parent
->p_children
.lh_first
; (p
!= 0); p
= p
->p_sibling
.le_next
) {
1966 if (p
->p_stat
== SIDL
)
1968 pid_list
[pidcount
] = p
->p_pid
;
1970 if (pidcount
>= count
)
1976 for (i
= 0; i
< pidcount
; i
++) {
1977 p
= proc_find(pid_list
[i
]);
1979 proc_transwait(p
, 0);
1980 retval
= callout(p
, arg
);
1984 case PROC_RETURNED_DONE
:
1986 if (retval
== PROC_RETURNED_DONE
) {
1991 case PROC_CLAIMED_DONE
:
2001 kfree(pid_list
, alloc_count
);
2008 /* PGRP_BLOCKITERATE is not implemented yet */
2010 pgrp_iterate(pgrp
, flags
, callout
, arg
, filterfn
, filterarg
)
2013 int (*callout
)(proc_t
, void *);
2015 int (*filterfn
)(proc_t
, void *);
2020 int count
, pidcount
, i
, alloc_count
;
2023 int dropref
= flags
& PGRP_DROPREF
;
2025 int serialize
= flags
& PGRP_BLOCKITERATE
;
2032 count
= pgrp
->pg_membercnt
+ 10;
2033 if (count
> hard_maxproc
)
2034 count
= hard_maxproc
;
2035 alloc_count
= count
* sizeof(pid_t
);
2036 pid_list
= (pid_t
*)kalloc(alloc_count
);
2037 bzero(pid_list
, alloc_count
);
2040 if (serialize
!= 0) {
2041 while ((pgrp
->pg_listflags
& PGRP_FLAG_ITERABEGIN
) == PGRP_FLAG_ITERABEGIN
) {
2042 pgrp
->pg_listflags
|= PGRP_FLAG_ITERWAIT
;
2043 msleep(&pgrp
->pg_listflags
, &pgrp
->pg_mlock
, 0, "pgrp_iterate", 0);
2045 pgrp
->pg_listflags
|= PGRP_FLAG_ITERABEGIN
;
2051 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
2052 p
= p
->p_pglist
.le_next
) {
2053 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
2054 pid_list
[pidcount
] = p
->p_pid
;
2056 if (pidcount
>= count
)
2063 if ((serialize
== 0) && (dropref
!= 0))
2067 for (i
= 0; i
< pidcount
; i
++) {
2068 /* No handling or proc0 */
2069 if (pid_list
[i
] == 0)
2071 p
= proc_find(pid_list
[i
]);
2073 if (p
->p_pgrpid
!= pgid
) {
2077 proc_transwait(p
, 0);
2078 retval
= callout(p
, arg
);
2082 case PROC_RETURNED_DONE
:
2084 if (retval
== PROC_RETURNED_DONE
) {
2089 case PROC_CLAIMED_DONE
:
2098 if (serialize
!= 0) {
2100 pgrp
->pg_listflags
&= ~PGRP_FLAG_ITERABEGIN
;
2101 if ((pgrp
->pg_listflags
& PGRP_FLAG_ITERWAIT
) == PGRP_FLAG_ITERWAIT
) {
2102 pgrp
->pg_listflags
&= ~PGRP_FLAG_ITERWAIT
;
2103 wakeup(&pgrp
->pg_listflags
);
2109 kfree(pid_list
, alloc_count
);
2114 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2117 child
->p_pgrp
= pgrp
;
2118 child
->p_pgrpid
= pgrp
->pg_id
;
2119 child
->p_listflag
|= P_LIST_INPGRP
;
2121 * When pgrp is being freed , a process can still
2122 * request addition using setpgid from bash when
2123 * login is terminated (login cycler) return ESRCH
2124 * Safe to hold lock due to refcount on pgrp
2126 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2127 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2130 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2131 panic("pgrp_add : pgrp is dead adding process");
2135 pgrp
->pg_membercnt
++;
2136 if ( parent
!= PROC_NULL
) {
2137 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2139 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2144 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2145 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2151 pgrp_remove(struct proc
* p
)
2158 #if __PROC_INTERNAL_DEBUG
2159 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2160 panic("removing from pglist but no named ref\n");
2162 p
->p_pgrpid
= PGRPID_DEAD
;
2163 p
->p_listflag
&= ~P_LIST_INPGRP
;
2167 if (pg
== PGRP_NULL
)
2168 panic("pgrp_remove: pg is NULL");
2172 if (pg
->pg_membercnt
< 0)
2173 panic("pgprp: -ve membercnt pgprp:%x p:%x\n",(unsigned int)pg
, (unsigned int)p
);
2175 LIST_REMOVE(p
, p_pglist
);
2176 if (pg
->pg_members
.lh_first
== 0) {
2178 pgdelete_dropref(pg
);
2186 /* cannot use proc_pgrp as it maybe stalled */
2188 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2190 struct pgrp
* oldpg
;
2196 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2197 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2198 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2201 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2204 if (oldpg
== PGRP_NULL
)
2205 panic("pgrp_replace: oldpg NULL");
2206 oldpg
->pg_refcount
++;
2207 #if __PROC_INTERNAL_DEBUG
2208 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2209 panic("removing from pglist but no named ref\n");
2211 p
->p_pgrpid
= PGRPID_DEAD
;
2212 p
->p_listflag
&= ~P_LIST_INPGRP
;
2218 oldpg
->pg_membercnt
--;
2219 if (oldpg
->pg_membercnt
< 0)
2220 panic("pgprp: -ve membercnt pgprp:%x p:%x\n",(unsigned int)oldpg
, (unsigned int)p
);
2221 LIST_REMOVE(p
, p_pglist
);
2222 if (oldpg
->pg_members
.lh_first
== 0) {
2224 pgdelete_dropref(oldpg
);
2232 p
->p_pgrpid
= newpg
->pg_id
;
2233 p
->p_listflag
|= P_LIST_INPGRP
;
2235 * When pgrp is being freed , a process can still
2236 * request addition using setpgid from bash when
2237 * login is terminated (login cycler) return ESRCH
2238 * Safe to hold lock due to refcount on pgrp
2240 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2241 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2244 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2245 panic("pgrp_add : pgrp is dead adding process");
2249 newpg
->pg_membercnt
++;
2250 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
2254 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
2255 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2258 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
2259 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
2260 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
2261 wakeup(&p
->p_pgrpid
);
2268 pgrp_lock(struct pgrp
* pgrp
)
2270 lck_mtx_lock(&pgrp
->pg_mlock
);
2274 pgrp_unlock(struct pgrp
* pgrp
)
2276 lck_mtx_unlock(&pgrp
->pg_mlock
);
2280 session_lock(struct session
* sess
)
2282 lck_mtx_lock(&sess
->s_mlock
);
2287 session_unlock(struct session
* sess
)
2289 lck_mtx_unlock(&sess
->s_mlock
);
2301 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2302 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2303 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2308 assert(pgrp
!= NULL
);
2310 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0)
2311 panic("proc_pgrp: ref being povided for dead pgrp");
2313 if (pgrp
!= PGRP_NULL
)
2314 pgrp
->pg_refcount
++;
2321 tty_pgrp(struct tty
* tp
)
2323 struct pgrp
* pg
= PGRP_NULL
;
2328 if (pg
!= PGRP_NULL
) {
2329 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0)
2330 panic("tty_pgrp: ref being povided for dead pgrp");
2339 proc_session(proc_t p
)
2341 struct session
* sess
= SESSION_NULL
;
2344 return(SESSION_NULL
);
2348 /* wait during transitions */
2349 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2350 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2351 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2354 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
2355 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2356 panic("proc_session:returning sesssion ref on terminating session");
2364 session_rele(struct session
*sess
)
2367 if (--sess
->s_count
== 0) {
2368 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2369 panic("session_rele: terminating already terminated session");
2370 sess
->s_listflags
|= S_LIST_TERM
;
2371 LIST_REMOVE(sess
, s_hash
);
2372 sess
->s_listflags
|= S_LIST_DEAD
;
2373 if (sess
->s_count
!= 0)
2374 panic("session_rele: freeing session in use");
2376 lck_mtx_destroy(&sess
->s_mlock
, proc_lck_grp
);
2377 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
2383 proc_transstart(proc_t p
, int locked
)
2387 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2388 p
->p_lflag
|= P_LTRANSWAIT
;
2389 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2391 p
->p_lflag
|= P_LINTRANSIT
;
2392 p
->p_transholder
= current_thread();
2400 proc_transend(proc_t p
, int locked
)
2404 p
->p_lflag
&= ~P_LINTRANSIT
;
2406 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2407 p
->p_lflag
&= ~P_LTRANSWAIT
;
2408 wakeup(&p
->p_lflag
);
2410 p
->p_transholder
= NULL
;
2416 proc_transwait(proc_t p
, int locked
)
2420 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2421 p
->p_lflag
|= P_LTRANSWAIT
;
2422 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2429 proc_klist_lock(void)
2431 lck_mtx_lock(proc_klist_mlock
);
2435 proc_klist_unlock(void)
2437 lck_mtx_unlock(proc_klist_mlock
);
2441 proc_knote(struct proc
* p
, long hint
)
2444 KNOTE(&p
->p_klist
, hint
);
2445 proc_klist_unlock();
2449 unsigned long cs_procs_killed
= 0;
2450 unsigned long cs_procs_invalidated
= 0;
2451 int cs_force_kill
= 0;
2452 int cs_force_hard
= 0;
2454 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_kill
, CTLFLAG_RW
, &cs_force_kill
, 0, "");
2455 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_hard
, CTLFLAG_RW
, &cs_force_hard
, 0, "");
2456 SYSCTL_INT(_vm
, OID_AUTO
, cs_debug
, CTLFLAG_RW
, &cs_debug
, 0, "");
2459 cs_invalid_page(void)
2467 * XXX revisit locking when proc is no longer protected
2468 * by the kernel funnel...
2471 /* XXX for testing */
2474 p
->p_csflags
|= CS_KILL
;
2476 p
->p_csflags
|= CS_HARD
;
2478 if (p
->p_csflags
& CS_VALID
) {
2479 p
->p_csflags
&= ~CS_VALID
;
2482 cs_procs_invalidated
++;
2483 printf("CODE SIGNING: cs_invalid_page: "
2484 "p=%d[%s] clearing CS_VALID\n",
2485 p
->p_pid
, p
->p_comm
);
2489 if (p
->p_csflags
& CS_KILL
) {
2492 printf("CODE SIGNING: cs_invalid_page: "
2493 "p=%d[%s] honoring CS_KILL\n",
2494 p
->p_pid
, p
->p_comm
);
2497 psignal(p
, SIGKILL
);
2501 if (p
->p_csflags
& CS_HARD
) {
2504 printf("CODE SIGNING: cs_invalid_page: "
2505 "p=%d[%s] honoring CS_HARD\n",
2506 p
->p_pid
, p
->p_comm
);
2516 printf("CODE SIGNING: cs_invalid_page: "
2517 "p=%d[%s] ignored...\n",
2518 p
->p_pid
, p
->p_comm
);