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>
87 #include <sys/malloc.h>
90 #include <sys/ioctl.h>
92 #include <sys/signalvar.h>
93 #include <sys/syslog.h>
94 #include <sys/sysctl.h>
95 #include <sys/sysproto.h>
96 #include <sys/kauth.h>
97 #include <sys/codesign.h>
98 #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>
104 #include <vm/vm_map.h> /* vm_map_switch_protect() */
105 #include <mach/task.h>
108 #include <security/mac_framework.h>
111 #include <libkern/crypto/sha1.h>
114 * Structure associated with user cacheing.
117 LIST_ENTRY(uidinfo
) ui_hash
;
121 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
122 LIST_HEAD(uihashhead
, uidinfo
) *uihashtbl
;
123 u_long uihash
; /* size of hash table - 1 */
126 * Other process lists
128 struct pidhashhead
*pidhashtbl
;
130 struct pgrphashhead
*pgrphashtbl
;
132 struct sesshashhead
*sesshashtbl
;
135 struct proclist allproc
;
136 struct proclist zombproc
;
137 extern struct tty cons
;
143 static pid_t lastlcid
= 1;
144 static int alllctx_cnt
;
146 #define LCID_MAX 8192 /* Does this really need to be large? */
147 static int maxlcid
= LCID_MAX
;
149 LIST_HEAD(lctxlist
, lctx
);
150 static struct lctxlist alllctx
;
152 lck_mtx_t alllctx_lock
;
153 lck_grp_t
* lctx_lck_grp
;
154 lck_grp_attr_t
* lctx_lck_grp_attr
;
155 lck_attr_t
* lctx_lck_attr
;
157 static void lctxinit(void);
160 int cs_debug
; /* declared further down in this file */
163 #define __PROC_INTERNAL_DEBUG 1
165 /* Name to give to core files */
166 __private_extern__
char corefilename
[MAXPATHLEN
+1] = {"/cores/core.%P"};
168 static void orphanpg(struct pgrp
*pg
);
169 void proc_name_kdp(task_t t
, char * buf
, int size
);
170 char *proc_name_address(void *p
);
172 static void pgrp_add(struct pgrp
* pgrp
, proc_t parent
, proc_t child
);
173 static void pgrp_remove(proc_t p
);
174 static void pgrp_replace(proc_t p
, struct pgrp
*pgrp
);
175 static void pgdelete_dropref(struct pgrp
*pgrp
);
176 extern void pg_rele_dropref(struct pgrp
* pgrp
);
178 struct fixjob_iterargs
{
180 struct session
* mysession
;
184 int fixjob_callback(proc_t
, void *);
187 * Initialize global process hashing structures.
193 LIST_INIT(&zombproc
);
194 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
195 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
196 sesshashtbl
= hashinit(maxproc
/ 4, M_PROC
, &sesshash
);
197 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
204 * Change the count associated with number of processes
205 * a given user is using. This routine protects the uihash
209 chgproccnt(uid_t uid
, int diff
)
212 struct uidinfo
*newuip
= NULL
;
213 struct uihashhead
*uipp
;
219 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
)
220 if (uip
->ui_uid
== uid
)
223 uip
->ui_proccnt
+= diff
;
224 if (uip
->ui_proccnt
> 0) {
225 retval
= uip
->ui_proccnt
;
229 if (uip
->ui_proccnt
< 0)
230 panic("chgproccnt: procs < 0");
231 LIST_REMOVE(uip
, ui_hash
);
234 FREE_ZONE(uip
, sizeof(*uip
), M_PROC
);
243 panic("chgproccnt: lost user");
245 if (newuip
!= NULL
) {
248 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
250 uip
->ui_proccnt
= diff
;
256 MALLOC_ZONE(newuip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
258 panic("chgproccnt: M_PROC zone depleted");
262 FREE_ZONE(newuip
, sizeof(*uip
), M_PROC
);
267 * Is p an inferior of the current process?
275 for (; p
!= current_proc(); p
= p
->p_pptr
)
285 * Is p an inferior of t ?
288 isinferior(proc_t p
, proc_t t
)
294 /* if p==t they are not inferior */
299 for (; p
!= t
; p
= p
->p_pptr
) {
302 /* Detect here if we're in a cycle */
303 if ((p
->p_pid
== 0) || (p
->p_pptr
== start
) || (nchecked
>= nprocs
))
313 proc_isinferior(int pid1
, int pid2
)
315 proc_t p
= PROC_NULL
;
316 proc_t t
= PROC_NULL
;
319 if (((p
= proc_find(pid1
)) != (proc_t
)0 ) && ((t
= proc_find(pid2
)) != (proc_t
)0))
320 retval
= isinferior(p
, t
);
333 return(proc_findinternal(pid
, 0));
337 proc_findinternal(int pid
, int locked
)
339 proc_t p
= PROC_NULL
;
345 p
= pfind_locked(pid
);
346 if ((p
== PROC_NULL
) || (p
!= proc_ref_locked(p
)))
374 if (p
!= proc_ref_locked(p
))
382 proc_ref_locked(proc_t p
)
386 /* if process still in creation return failure */
387 if ((p
== PROC_NULL
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0))
389 /* do not return process marked for termination */
390 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))
399 proc_rele_locked(proc_t p
)
402 if (p
->p_refcount
> 0) {
404 if ((p
->p_refcount
== 0) && ((p
->p_listflag
& P_LIST_DRAINWAIT
) == P_LIST_DRAINWAIT
)) {
405 p
->p_listflag
&= ~P_LIST_DRAINWAIT
;
406 wakeup(&p
->p_refcount
);
409 panic("proc_rele_locked -ve ref\n");
414 proc_find_zombref(int pid
)
416 proc_t p1
= PROC_NULL
;
417 proc_t p
= PROC_NULL
;
421 p
= pfind_locked(pid
);
423 /* if process still in creation return NULL */
424 if ((p
== PROC_NULL
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0)) {
429 /* if process has not started exit or is being reaped, return NULL */
430 if (((p
->p_listflag
& P_LIST_EXITED
) != 0) && ((p
->p_listflag
& P_LIST_WAITING
) == 0)) {
431 p
->p_listflag
|= P_LIST_WAITING
;
442 proc_drop_zombref(proc_t p
)
445 if ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
446 p
->p_listflag
&= ~P_LIST_WAITING
;
454 proc_refdrain(proc_t p
)
459 p
->p_listflag
|= P_LIST_DRAIN
;
460 while (p
->p_refcount
) {
461 p
->p_listflag
|= P_LIST_DRAINWAIT
;
462 msleep(&p
->p_refcount
, proc_list_mlock
, 0, "proc_refdrain", 0) ;
464 p
->p_listflag
&= ~P_LIST_DRAIN
;
465 p
->p_listflag
|= P_LIST_DEAD
;
473 proc_parentholdref(proc_t p
)
475 proc_t parent
= PROC_NULL
;
483 if ((pp
== PROC_NULL
) || (pp
->p_stat
== SZOMB
) || ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
))) {
488 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == P_LIST_CHILDDRSTART
) {
489 pp
->p_listflag
|= P_LIST_CHILDDRWAIT
;
490 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
499 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == 0) {
510 proc_parentdropref(proc_t p
, int listlocked
)
515 if (p
->p_parentref
> 0) {
517 if ((p
->p_parentref
== 0) && ((p
->p_listflag
& P_LIST_PARENTREFWAIT
) == P_LIST_PARENTREFWAIT
)) {
518 p
->p_listflag
&= ~P_LIST_PARENTREFWAIT
;
519 wakeup(&p
->p_parentref
);
522 panic("proc_parentdropref -ve ref\n");
530 proc_childdrainstart(proc_t p
)
532 #if __PROC_INTERNAL_DEBUG
533 if ((p
->p_listflag
& P_LIST_CHILDDRSTART
) == P_LIST_CHILDDRSTART
)
534 panic("proc_childdrainstart: childdrain already started\n");
536 p
->p_listflag
|= P_LIST_CHILDDRSTART
;
537 /* wait for all that hold parentrefs to drop */
538 while (p
->p_parentref
> 0) {
539 p
->p_listflag
|= P_LIST_PARENTREFWAIT
;
540 msleep(&p
->p_parentref
, proc_list_mlock
, 0, "proc_childdrainstart", 0) ;
546 proc_childdrainend(proc_t p
)
548 #if __PROC_INTERNAL_DEBUG
549 if (p
->p_childrencnt
> 0)
550 panic("exiting: children stil hanging around\n");
552 p
->p_listflag
|= P_LIST_CHILDDRAINED
;
553 if ((p
->p_listflag
& (P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
)) != 0) {
554 p
->p_listflag
&= ~(P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
);
555 wakeup(&p
->p_childrencnt
);
560 proc_checkdeadrefs(__unused proc_t p
)
562 #if __PROC_INTERNAL_DEBUG
563 if ((p
->p_listflag
& P_LIST_INHASH
) != 0)
564 panic("proc being freed and still in hash %p: %u\n", p
, p
->p_listflag
);
565 if (p
->p_childrencnt
!= 0)
566 panic("proc being freed and pending children cnt %p:%d\n", p
, p
->p_childrencnt
);
567 if (p
->p_refcount
!= 0)
568 panic("proc being freed and pending refcount %p:%d\n", p
, p
->p_refcount
);
569 if (p
->p_parentref
!= 0)
570 panic("proc being freed and pending parentrefs %p:%d\n", p
, p
->p_parentref
);
589 proc_t p
= current_proc();
596 proc_t p
= current_proc();
601 proc_parent(proc_t p
)
609 parent
= proc_ref_locked(pp
);
610 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)){
611 pp
->p_listflag
|= P_LIST_CHILDLKWAIT
;
612 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
621 proc_name(int pid
, char * buf
, int size
)
625 if ((p
= proc_find(pid
)) != PROC_NULL
) {
626 strlcpy(buf
, &p
->p_comm
[0], size
);
632 proc_name_kdp(task_t t
, char * buf
, int size
)
634 proc_t p
= get_bsdtask_info(t
);
637 strlcpy(buf
, &p
->p_comm
[0], size
);
641 proc_name_address(void *p
)
643 return &((proc_t
)p
)->p_comm
[0];
647 proc_selfname(char * buf
, int size
)
651 if ((p
= current_proc())!= (proc_t
)0) {
652 strlcpy(buf
, &p
->p_comm
[0], size
);
657 proc_signal(int pid
, int signum
)
661 if ((p
= proc_find(pid
)) != PROC_NULL
) {
668 proc_issignal(int pid
, sigset_t mask
)
673 if ((p
= proc_find(pid
)) != PROC_NULL
) {
674 error
= proc_pendingsignals(p
, mask
);
682 proc_noremotehang(proc_t p
)
687 retval
= p
->p_flag
& P_NOREMOTEHANG
;
688 return(retval
? 1: 0);
693 proc_exiting(proc_t p
)
698 retval
= p
->p_lflag
& P_LEXIT
;
699 return(retval
? 1: 0);
703 proc_forcequota(proc_t p
)
708 retval
= p
->p_flag
& P_FORCEQUOTA
;
709 return(retval
? 1: 0);
719 retval
= p
->p_flag
& P_TBE
;
720 return(retval
? 1: 0);
727 kauth_cred_t my_cred
;
730 my_cred
= kauth_cred_proc_ref(p
);
731 error
= suser(my_cred
, &p
->p_acflag
);
732 kauth_cred_unref(&my_cred
);
737 * Obtain the first thread in a process
739 * XXX This is a bad thing to do; it exists predominantly to support the
740 * XXX use of proc_t's in places that should really be using
741 * XXX thread_t's instead. This maintains historical behaviour, but really
742 * XXX needs an audit of the context (proxy vs. not) to clean up.
745 proc_thread(proc_t proc
)
747 uthread_t uth
= TAILQ_FIRST(&proc
->p_uthlist
);
750 return(uth
->uu_context
.vc_thread
);
764 thread_t th
= current_thread();
766 return((struct uthread
*)get_bsdthread_info(th
));
771 proc_is64bit(proc_t p
)
773 return(IS_64BIT_PROCESS(p
));
777 proc_pidversion(proc_t p
)
779 return(p
->p_idversion
);
783 proc_uniqueid(proc_t p
)
785 return(p
->p_uniqueid
);
789 proc_selfuniqueid(void)
791 proc_t p
= current_proc();
792 return(p
->p_uniqueid
);
796 proc_getcdhash(proc_t p
, unsigned char *cdhash
)
798 return vn_getcdhash(p
->p_textvp
, p
->p_textoff
, cdhash
);
802 proc_getexecutableuuid(proc_t p
, unsigned char *uuidbuf
, unsigned long size
)
804 if (size
>= sizeof(p
->p_uuid
)) {
805 memcpy(uuidbuf
, p
->p_uuid
, sizeof(p
->p_uuid
));
811 bsd_set_dependency_capable(task_t task
)
813 proc_t p
= get_bsdtask_info(task
);
816 OSBitOrAtomic(P_DEPENDENCY_CAPABLE
, &p
->p_flag
);
822 IS_64BIT_PROCESS(proc_t p
)
824 if (p
&& (p
->p_flag
& P_LP64
))
831 * Locate a process by number
834 pfind_locked(pid_t pid
)
844 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
) {
845 if (p
->p_pid
== pid
) {
847 for (q
= p
->p_hash
.le_next
; q
!= 0; q
= q
->p_hash
.le_next
) {
848 if ((p
!=q
) && (q
->p_pid
== pid
))
849 panic("two procs with same pid %p:%p:%d:%d\n", p
, q
, p
->p_pid
, q
->p_pid
);
859 * Locate a zombie by PID
861 __private_extern__ proc_t
869 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
879 * Locate a process group by number
888 pgrp
= pgfind_internal(pgid
);
889 if ((pgrp
== NULL
) || ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) != 0))
900 pgfind_internal(pid_t pgid
)
904 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
)
905 if (pgrp
->pg_id
== pgid
)
911 pg_rele(struct pgrp
* pgrp
)
913 if(pgrp
== PGRP_NULL
)
915 pg_rele_dropref(pgrp
);
919 pg_rele_dropref(struct pgrp
* pgrp
)
922 if ((pgrp
->pg_refcount
== 1) && ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) == PGRP_FLAG_TERMINATE
)) {
924 pgdelete_dropref(pgrp
);
933 session_find_internal(pid_t sessid
)
935 struct session
*sess
;
937 for (sess
= SESSHASH(sessid
)->lh_first
; sess
!= 0; sess
= sess
->s_hash
.le_next
)
938 if (sess
->s_sid
== sessid
)
945 * Make a new process ready to become a useful member of society by making it
946 * visible in all the right places and initialize its own lists to empty.
948 * Parameters: parent The parent of the process to insert
949 * child The child process to insert
953 * Notes: Insert a child process into the parents process group, assign
954 * the child the parent process pointer and PPID of the parent,
955 * place it on the parents p_children list as a sibling,
956 * initialize its own child list, place it in the allproc list,
957 * insert it in the proper hash bucket, and initialize its
961 pinsertchild(proc_t parent
, proc_t child
)
965 LIST_INIT(&child
->p_children
);
966 TAILQ_INIT(&child
->p_evlist
);
967 child
->p_pptr
= parent
;
968 child
->p_ppid
= parent
->p_pid
;
970 pg
= proc_pgrp(parent
);
971 pgrp_add(pg
, parent
, child
);
975 parent
->p_childrencnt
++;
976 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
978 LIST_INSERT_HEAD(&allproc
, child
, p_list
);
979 /* mark the completion of proc creation */
980 child
->p_listflag
&= ~P_LIST_INCREATE
;
987 * Move p to a new or existing process group (and session)
990 * ESRCH No such process
993 enterpgrp(proc_t p
, pid_t pgid
, int mksess
)
997 struct session
* procsp
;
1000 mypgrp
= proc_pgrp(p
);
1001 procsp
= proc_session(p
);
1004 if (pgrp
!= NULL
&& mksess
) /* firewalls */
1005 panic("enterpgrp: setsid into non-empty pgrp");
1006 if (SESS_LEADER(p
, procsp
))
1007 panic("enterpgrp: session leader attempted setpgrp");
1009 if (pgrp
== PGRP_NULL
) {
1010 pid_t savepid
= p
->p_pid
;
1011 proc_t np
= PROC_NULL
;
1016 if (p
->p_pid
!= pgid
)
1017 panic("enterpgrp: new pgrp and pid != pgid");
1019 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
1022 panic("enterpgrp: M_PGRP zone depleted");
1023 if ((np
= proc_find(savepid
)) == NULL
|| np
!= p
) {
1024 if (np
!= PROC_NULL
)
1026 if (mypgrp
!= PGRP_NULL
)
1028 if (procsp
!= SESSION_NULL
)
1029 session_rele(procsp
);
1030 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
1035 struct session
*sess
;
1040 MALLOC_ZONE(sess
, struct session
*,
1041 sizeof(struct session
), M_SESSION
, M_WAITOK
);
1043 panic("enterpgrp: M_SESSION zone depleted");
1045 sess
->s_sid
= p
->p_pid
;
1047 sess
->s_ttyvp
= NULL
;
1048 sess
->s_ttyp
= TTY_NULL
;
1050 sess
->s_listflags
= 0;
1051 sess
->s_ttypgrpid
= NO_PID
;
1052 #if CONFIG_FINE_LOCK_GROUPS
1053 lck_mtx_init(&sess
->s_mlock
, proc_mlock_grp
, proc_lck_attr
);
1055 lck_mtx_init(&sess
->s_mlock
, proc_lck_grp
, proc_lck_attr
);
1057 bcopy(procsp
->s_login
, sess
->s_login
,
1058 sizeof(sess
->s_login
));
1059 OSBitAndAtomic(~((uint32_t)P_CONTROLT
), &p
->p_flag
);
1061 LIST_INSERT_HEAD(SESSHASH(sess
->s_sid
), sess
, s_hash
);
1063 pgrp
->pg_session
= sess
;
1065 if (p
!= current_proc())
1066 panic("enterpgrp: mksession and p != curproc");
1070 pgrp
->pg_session
= procsp
;
1072 if ((pgrp
->pg_session
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1073 panic("enterpgrp: providing ref to terminating session ");
1074 pgrp
->pg_session
->s_count
++;
1078 #if CONFIG_FINE_LOCK_GROUPS
1079 lck_mtx_init(&pgrp
->pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
1081 lck_mtx_init(&pgrp
->pg_mlock
, proc_lck_grp
, proc_lck_attr
);
1083 LIST_INIT(&pgrp
->pg_members
);
1084 pgrp
->pg_membercnt
= 0;
1087 pgrp
->pg_refcount
= 1;
1088 pgrp
->pg_listflags
= 0;
1089 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
1091 } else if (pgrp
== mypgrp
) {
1095 if (procsp
!= SESSION_NULL
)
1096 session_rele(procsp
);
1100 if (procsp
!= SESSION_NULL
)
1101 session_rele(procsp
);
1103 * Adjust eligibility of affected pgrps to participate in job control.
1104 * Increment eligibility counts before decrementing, otherwise we
1105 * could reach 0 spuriously during the first call.
1107 fixjobc(p
, pgrp
, 1);
1108 fixjobc(p
, mypgrp
, 0);
1110 if(mypgrp
!= PGRP_NULL
)
1112 pgrp_replace(p
, pgrp
);
1119 * remove process from process group
1130 * delete a process group
1133 pgdelete_dropref(struct pgrp
*pgrp
)
1137 struct session
*sessp
;
1141 if (pgrp
->pg_membercnt
!= 0) {
1147 pgrp
->pg_refcount
--;
1148 if ((emptypgrp
== 0) || (pgrp
->pg_membercnt
!= 0)) {
1153 pgrp
->pg_listflags
|= PGRP_FLAG_TERMINATE
;
1155 if (pgrp
->pg_refcount
> 0) {
1160 pgrp
->pg_listflags
|= PGRP_FLAG_DEAD
;
1161 LIST_REMOVE(pgrp
, pg_hash
);
1165 ttyp
= SESSION_TP(pgrp
->pg_session
);
1166 if (ttyp
!= TTY_NULL
) {
1167 if (ttyp
->t_pgrp
== pgrp
) {
1169 /* Re-check after acquiring the lock */
1170 if (ttyp
->t_pgrp
== pgrp
) {
1171 ttyp
->t_pgrp
= NULL
;
1172 pgrp
->pg_session
->s_ttypgrpid
= NO_PID
;
1180 sessp
= pgrp
->pg_session
;
1181 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1182 panic("pg_deleteref: manipulating refs of already terminating session");
1183 if (--sessp
->s_count
== 0) {
1184 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1185 panic("pg_deleteref: terminating already terminated session");
1186 sessp
->s_listflags
|= S_LIST_TERM
;
1187 ttyp
= SESSION_TP(sessp
);
1188 LIST_REMOVE(sessp
, s_hash
);
1190 if (ttyp
!= TTY_NULL
) {
1192 if (ttyp
->t_session
== sessp
)
1193 ttyp
->t_session
= NULL
;
1197 sessp
->s_listflags
|= S_LIST_DEAD
;
1198 if (sessp
->s_count
!= 0)
1199 panic("pg_deleteref: freeing session in use");
1201 #if CONFIG_FINE_LOCK_GROUPS
1202 lck_mtx_destroy(&sessp
->s_mlock
, proc_mlock_grp
);
1204 lck_mtx_destroy(&sessp
->s_mlock
, proc_lck_grp
);
1206 FREE_ZONE(sessp
, sizeof(struct session
), M_SESSION
);
1209 #if CONFIG_FINE_LOCK_GROUPS
1210 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_mlock_grp
);
1212 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_lck_grp
);
1214 FREE_ZONE(pgrp
, sizeof(*pgrp
), M_PGRP
);
1219 * Adjust pgrp jobc counters when specified process changes process group.
1220 * We count the number of processes in each process group that "qualify"
1221 * the group for terminal job control (those with a parent in a different
1222 * process group of the same session). If that count reaches zero, the
1223 * process group becomes orphaned. Check both the specified process'
1224 * process group and that of its children.
1225 * entering == 0 => p is leaving specified group.
1226 * entering == 1 => p is entering specified group.
1229 fixjob_callback(proc_t p
, void * arg
)
1231 struct fixjob_iterargs
*fp
;
1232 struct pgrp
* pg
, *hispg
;
1233 struct session
* mysession
, *hissess
;
1236 fp
= (struct fixjob_iterargs
*)arg
;
1238 mysession
= fp
->mysession
;
1239 entering
= fp
->entering
;
1241 hispg
= proc_pgrp(p
);
1242 hissess
= proc_session(p
);
1244 if ((hispg
!= pg
) &&
1245 (hissess
== mysession
)) {
1250 } else if (--hispg
->pg_jobc
== 0) {
1256 if (hissess
!= SESSION_NULL
)
1257 session_rele(hissess
);
1258 if (hispg
!= PGRP_NULL
)
1261 return(PROC_RETURNED
);
1265 fixjobc(proc_t p
, struct pgrp
*pgrp
, int entering
)
1267 struct pgrp
*hispgrp
= PGRP_NULL
;
1268 struct session
*hissess
= SESSION_NULL
;
1269 struct session
*mysession
= pgrp
->pg_session
;
1271 struct fixjob_iterargs fjarg
;
1273 parent
= proc_parent(p
);
1274 if (parent
!= PROC_NULL
) {
1275 hispgrp
= proc_pgrp(parent
);
1276 hissess
= proc_session(parent
);
1282 * Check p's parent to see whether p qualifies its own process
1283 * group; if so, adjust count for p's process group.
1285 if ((hispgrp
!= pgrp
) &&
1286 (hissess
== mysession
)) {
1291 }else if (--pgrp
->pg_jobc
== 0) {
1298 if (hissess
!= SESSION_NULL
)
1299 session_rele(hissess
);
1300 if (hispgrp
!= PGRP_NULL
)
1304 * Check this process' children to see whether they qualify
1305 * their process groups; if so, adjust counts for children's
1309 fjarg
.mysession
= mysession
;
1310 fjarg
.entering
= entering
;
1311 proc_childrenwalk(p
, fixjob_callback
, &fjarg
);
1315 * A process group has become orphaned;
1316 * if there are any stopped processes in the group,
1317 * hang-up all process in that group.
1320 orphanpg(struct pgrp
* pgrp
)
1324 int count
, pidcount
, i
, alloc_count
;
1326 if (pgrp
== PGRP_NULL
)
1330 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
1331 if (p
->p_stat
== SSTOP
) {
1332 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1333 p
= p
->p_pglist
.le_next
)
1335 break; /* ??? stops after finding one.. */
1341 if (count
> hard_maxproc
)
1342 count
= hard_maxproc
;
1343 alloc_count
= count
* sizeof(pid_t
);
1344 pid_list
= (pid_t
*)kalloc(alloc_count
);
1345 bzero(pid_list
, alloc_count
);
1349 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1350 p
= p
->p_pglist
.le_next
) {
1351 if (p
->p_stat
== SSTOP
) {
1352 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1353 p
= p
->p_pglist
.le_next
) {
1354 pid_list
[pidcount
] = p
->p_pid
;
1356 if (pidcount
>= count
)
1359 break; /* ??? stops after finding one.. */
1368 for (i
= 0; i
< pidcount
; i
++) {
1369 /* No handling or proc0 */
1370 if (pid_list
[i
] == 0)
1372 p
= proc_find(pid_list
[i
]);
1374 proc_transwait(p
, 0);
1377 psignal(p
, SIGCONT
);
1382 kfree(pid_list
, alloc_count
);
1388 /* XXX should be __private_extern__ */
1390 proc_is_classic(proc_t p
)
1392 return (p
->p_flag
& P_TRANSLATED
) ? 1 : 0;
1395 /* XXX Why does this function exist? Need to kill it off... */
1397 current_proc_EXTERNAL(void)
1399 return (current_proc());
1403 * proc_core_name(name, uid, pid)
1404 * Expand the name described in corefilename, using name, uid, and pid.
1405 * corefilename is a printf-like string, with three format specifiers:
1406 * %N name of process ("name")
1407 * %P process id (pid)
1409 * For example, "%N.core" is the default; they can be disabled completely
1410 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1411 * This is controlled by the sysctl variable kern.corefile (see above).
1413 __private_extern__
int
1414 proc_core_name(const char *name
, uid_t uid
, pid_t pid
, char *cf_name
,
1417 const char *format
, *appendstr
;
1418 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
1421 if (cf_name
== NULL
)
1424 format
= corefilename
;
1425 for (i
= 0, n
= 0; n
< cf_name_len
&& format
[i
]; i
++) {
1426 switch (format
[i
]) {
1427 case '%': /* Format character */
1429 switch (format
[i
]) {
1433 case 'N': /* process name */
1436 case 'P': /* process id */
1437 snprintf(id_buf
, sizeof(id_buf
), "%u", pid
);
1440 case 'U': /* user id */
1441 snprintf(id_buf
, sizeof(id_buf
), "%u", uid
);
1447 "Unknown format character %c in `%s'\n",
1450 l
= strlen(appendstr
);
1451 if ((n
+ l
) >= cf_name_len
)
1453 bcopy(appendstr
, cf_name
+ n
, l
);
1457 cf_name
[n
++] = format
[i
];
1460 if (format
[i
] != '\0')
1464 log(LOG_ERR
, "pid %ld (%s), uid (%u): corename is too long\n",
1465 (long)pid
, name
, (uint32_t)uid
);
1474 LIST_INIT(&alllctx
);
1477 /* allocate lctx lock group attribute and group */
1478 lctx_lck_grp_attr
= lck_grp_attr_alloc_init();
1479 lck_grp_attr_setstat(lctx_lck_grp_attr
);
1481 lctx_lck_grp
= lck_grp_alloc_init("lctx", lctx_lck_grp_attr
);
1482 /* Allocate lctx lock attribute */
1483 lctx_lck_attr
= lck_attr_alloc_init();
1485 lck_mtx_init(&alllctx_lock
, lctx_lck_grp
, lctx_lck_attr
);
1489 * Locate login context by number.
1497 LIST_FOREACH(l
, &alllctx
, lc_list
) {
1498 if (l
->lc_id
== lcid
) {
1510 if (lastlcid > maxlcid) \
1520 /* Not very efficient but this isn't a common operation. */
1521 while ((l
= lcfind(lastlcid
)) != NULL
) {
1528 MALLOC(l
, struct lctx
*, sizeof(struct lctx
), M_LCTX
, M_WAITOK
|M_ZERO
);
1530 LIST_INIT(&l
->lc_members
);
1531 lck_mtx_init(&l
->lc_mtx
, lctx_lck_grp
, lctx_lck_attr
);
1533 l
->lc_label
= mac_lctx_label_alloc();
1536 LIST_INSERT_HEAD(&alllctx
, l
, lc_list
);
1544 * Call with proc protected (either by being invisible
1545 * or by having the all-login-context lock held) and
1548 * Will unlock lctx on return.
1551 enterlctx (proc_t p
, struct lctx
*l
, __unused
int create
)
1557 LIST_INSERT_HEAD(&l
->lc_members
, p
, p_lclist
);
1562 mac_lctx_notify_create(p
, l
);
1564 mac_lctx_notify_join(p
, l
);
1572 * Remove process from login context (if any). Called with p protected by
1576 leavelctx (proc_t p
)
1580 if (p
->p_lctx
== NULL
) {
1584 LCTX_LOCK(p
->p_lctx
);
1587 LIST_REMOVE(p
, p_lclist
);
1590 mac_lctx_notify_leave(p
, l
);
1592 if (LIST_EMPTY(&l
->lc_members
)) {
1593 LIST_REMOVE(l
, lc_list
);
1596 lck_mtx_destroy(&l
->lc_mtx
, lctx_lck_grp
);
1598 mac_lctx_label_free(l
->lc_label
);
1609 sysctl_kern_lctx SYSCTL_HANDLER_ARGS
1611 int *name
= (int*) arg1
;
1612 u_int namelen
= arg2
;
1613 struct kinfo_lctx kil
;
1619 switch (oidp
->oid_number
) {
1622 /* Request for size. */
1624 error
= SYSCTL_OUT(req
, 0,
1625 sizeof(struct kinfo_lctx
) * (alllctx_cnt
+ 1));
1630 case KERN_LCTX_LCID
:
1632 if (req
->oldlen
< sizeof(struct kinfo_lctx
))
1637 /* No login context */
1638 l
= lcfind((pid_t
)name
[0]);
1644 return (SYSCTL_OUT(req
, (caddr_t
)&kil
, sizeof(kil
)));
1650 /* Provided buffer is too small. */
1651 if (req
->oldlen
< (sizeof(struct kinfo_lctx
) * alllctx_cnt
)) {
1656 LIST_FOREACH(l
, &alllctx
, lc_list
) {
1661 error
= SYSCTL_OUT(req
, (caddr_t
)&kil
, sizeof(kil
));
1671 SYSCTL_NODE(_kern
, KERN_LCTX
, lctx
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Login Context");
1673 SYSCTL_PROC(_kern_lctx
, KERN_LCTX_ALL
, all
, CTLFLAG_RD
|CTLTYPE_STRUCT
| CTLFLAG_LOCKED
,
1674 0, 0, sysctl_kern_lctx
, "S,lctx",
1675 "Return entire login context table");
1676 SYSCTL_NODE(_kern_lctx
, KERN_LCTX_LCID
, lcid
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
1677 sysctl_kern_lctx
, "Login Context Table");
1678 SYSCTL_INT(_kern_lctx
, OID_AUTO
, last
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &lastlcid
, 0, "");
1679 SYSCTL_INT(_kern_lctx
, OID_AUTO
, count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &alllctx_cnt
, 0, "");
1680 SYSCTL_INT(_kern_lctx
, OID_AUTO
, max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxlcid
, 0, "");
1684 /* Code Signing related routines */
1687 csops(__unused proc_t p
, struct csops_args
*uap
, __unused
int32_t *retval
)
1690 pid_t pid
= uap
->pid
;
1691 user_addr_t uaddr
= uap
->useraddr
;
1692 size_t usize
= (size_t)CAST_DOWN(size_t, uap
->usersize
);
1700 unsigned char cdhash
[SHA1_RESULTLEN
];
1702 forself
= error
= 0;
1705 pid
= proc_selfpid();
1706 if (pid
== proc_selfpid())
1710 /* Pre flight checks for CS_OPS_PIDPATH */
1711 if (ops
== CS_OPS_PIDPATH
) {
1712 /* usize is unsigned.. */
1713 if (usize
> 4 * PATH_MAX
)
1715 if (kauth_cred_issuser(kauth_cred_get()) != TRUE
)
1717 } else if ((forself
== 0) && ((ops
!= CS_OPS_STATUS
) && (ops
!= CS_OPS_CDHASH
) && (ops
!= CS_OPS_PIDOFFSET
) && (kauth_cred_issuser(kauth_cred_get()) != TRUE
))) {
1721 pt
= proc_find(pid
);
1722 if (pt
== PROC_NULL
)
1730 retflags
= pt
->p_csflags
;
1731 if (uaddr
!= USER_ADDR_NULL
)
1732 error
= copyout(&retflags
, uaddr
, sizeof(uint32_t));
1735 case CS_OPS_MARKINVALID
:
1737 if ((pt
->p_csflags
& CS_VALID
) == CS_VALID
) { /* is currently valid */
1738 pt
->p_csflags
&= ~CS_VALID
; /* set invalid */
1739 if ((pt
->p_csflags
& CS_KILL
) == CS_KILL
) {
1740 pt
->p_csflags
|= CS_KILLED
;
1743 printf("CODE SIGNING: marked invalid by pid %d: "
1744 "p=%d[%s] honoring CS_KILL, final status 0x%x\n",
1745 proc_selfpid(), pt
->p_pid
, pt
->p_comm
, pt
->p_csflags
);
1747 psignal(pt
, SIGKILL
);
1755 case CS_OPS_MARKHARD
:
1757 pt
->p_csflags
|= CS_HARD
;
1758 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1759 /* @@@ allow? reject? kill? @@@ */
1767 case CS_OPS_MARKKILL
:
1769 pt
->p_csflags
|= CS_KILL
;
1770 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1772 psignal(pt
, SIGKILL
);
1777 case CS_OPS_PIDPATH
:
1779 vid
= vnode_vid(tvp
);
1781 if (tvp
== NULLVP
) {
1786 buf
= (char *)kalloc(usize
);
1793 error
= vnode_getwithvid(tvp
, vid
);
1797 error
= vn_getpath(tvp
, buf
, &len
);
1800 error
= copyout(buf
, uaddr
, usize
);
1809 case CS_OPS_PIDOFFSET
:
1810 toff
= pt
->p_textoff
;
1812 error
= copyout(&toff
, uaddr
, sizeof(toff
));
1817 /* pt already holds a reference on its p_textvp */
1819 toff
= pt
->p_textoff
;
1821 if (tvp
== NULLVP
|| usize
!= SHA1_RESULTLEN
) {
1826 error
= vn_getcdhash(tvp
, toff
, cdhash
);
1830 error
= copyout(cdhash
, uaddr
, sizeof (cdhash
));
1835 case CS_OPS_ENTITLEMENTS_BLOB
: {
1836 char zeros
[8] = { 0 };
1840 if (0 != (error
= cs_entitlements_blob_get(pt
,
1843 if (usize
< sizeof(zeros
) || usize
< length
) {
1847 if (NULL
== start
) {
1849 length
= sizeof(zeros
);
1851 error
= copyout(start
, uaddr
, length
);
1855 case CS_OPS_MARKRESTRICT
:
1857 pt
->p_csflags
|= CS_RESTRICT
;
1872 proc_iterate(flags
, callout
, arg
, filterfn
, filterarg
)
1874 int (*callout
)(proc_t
, void *);
1876 int (*filterfn
)(proc_t
, void *);
1881 int count
, pidcount
, alloc_count
, i
, retval
;
1884 if (count
> hard_maxproc
)
1885 count
= hard_maxproc
;
1886 alloc_count
= count
* sizeof(pid_t
);
1887 pid_list
= (pid_t
*)kalloc(alloc_count
);
1888 bzero(pid_list
, alloc_count
);
1895 if (flags
& PROC_ALLPROCLIST
) {
1896 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
1897 if (p
->p_stat
== SIDL
)
1899 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
1900 pid_list
[pidcount
] = p
->p_pid
;
1902 if (pidcount
>= count
)
1907 if ((pidcount
< count
) && (flags
& PROC_ZOMBPROCLIST
)) {
1908 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1909 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
1910 pid_list
[pidcount
] = p
->p_pid
;
1912 if (pidcount
>= count
)
1922 for (i
= 0; i
< pidcount
; i
++) {
1923 p
= proc_find(pid_list
[i
]);
1925 if ((flags
& PROC_NOWAITTRANS
) == 0)
1926 proc_transwait(p
, 0);
1927 retval
= callout(p
, arg
);
1931 case PROC_RETURNED_DONE
:
1933 if (retval
== PROC_RETURNED_DONE
) {
1938 case PROC_CLAIMED_DONE
:
1944 } else if (flags
& PROC_ZOMBPROCLIST
) {
1945 p
= proc_find_zombref(pid_list
[i
]);
1946 if (p
!= PROC_NULL
) {
1947 retval
= callout(p
, arg
);
1951 case PROC_RETURNED_DONE
:
1952 proc_drop_zombref(p
);
1953 if (retval
== PROC_RETURNED_DONE
) {
1958 case PROC_CLAIMED_DONE
:
1969 kfree(pid_list
, alloc_count
);
1976 /* This is for iteration in case of trivial non blocking callouts */
1978 proc_scanall(flags
, callout
, arg
)
1980 int (*callout
)(proc_t
, void *);
1990 if (flags
& PROC_ALLPROCLIST
) {
1991 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
1992 retval
= callout(p
, arg
);
1993 if (retval
== PROC_RETURNED_DONE
)
1997 if (flags
& PROC_ZOMBPROCLIST
) {
1998 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1999 retval
= callout(p
, arg
);
2000 if (retval
== PROC_RETURNED_DONE
)
2014 proc_rebootscan(callout
, arg
, filterfn
, filterarg
)
2015 int (*callout
)(proc_t
, void *);
2017 int (*filterfn
)(proc_t
, void *);
2021 int lockheld
= 0, retval
;
2023 proc_shutdown_exitcount
= 0;
2031 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
2032 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
2033 p
= proc_ref_locked(p
);
2039 proc_transwait(p
, 0);
2040 retval
= callout(p
, arg
);
2044 case PROC_RETURNED_DONE
:
2045 case PROC_CLAIMED_DONE
:
2049 goto ps_allprocscan
;
2051 } /* allproc walk thru */
2053 if (lockheld
== 1) {
2065 proc_childrenwalk(parent
, callout
, arg
)
2066 struct proc
* parent
;
2067 int (*callout
)(proc_t
, void *);
2070 register struct proc
*p
;
2072 int count
, pidcount
, alloc_count
, i
, retval
;
2075 if (count
> hard_maxproc
)
2076 count
= hard_maxproc
;
2077 alloc_count
= count
* sizeof(pid_t
);
2078 pid_list
= (pid_t
*)kalloc(alloc_count
);
2079 bzero(pid_list
, alloc_count
);
2086 for (p
= parent
->p_children
.lh_first
; (p
!= 0); p
= p
->p_sibling
.le_next
) {
2087 if (p
->p_stat
== SIDL
)
2089 pid_list
[pidcount
] = p
->p_pid
;
2091 if (pidcount
>= count
)
2097 for (i
= 0; i
< pidcount
; i
++) {
2098 p
= proc_find(pid_list
[i
]);
2100 proc_transwait(p
, 0);
2101 retval
= callout(p
, arg
);
2105 case PROC_RETURNED_DONE
:
2107 if (retval
== PROC_RETURNED_DONE
) {
2112 case PROC_CLAIMED_DONE
:
2122 kfree(pid_list
, alloc_count
);
2129 /* PGRP_BLOCKITERATE is not implemented yet */
2131 pgrp_iterate(pgrp
, flags
, callout
, arg
, filterfn
, filterarg
)
2134 int (*callout
)(proc_t
, void *);
2136 int (*filterfn
)(proc_t
, void *);
2141 int count
, pidcount
, i
, alloc_count
;
2144 int dropref
= flags
& PGRP_DROPREF
;
2146 int serialize
= flags
& PGRP_BLOCKITERATE
;
2153 count
= pgrp
->pg_membercnt
+ 10;
2154 if (count
> hard_maxproc
)
2155 count
= hard_maxproc
;
2156 alloc_count
= count
* sizeof(pid_t
);
2157 pid_list
= (pid_t
*)kalloc(alloc_count
);
2158 bzero(pid_list
, alloc_count
);
2161 if (serialize
!= 0) {
2162 while ((pgrp
->pg_listflags
& PGRP_FLAG_ITERABEGIN
) == PGRP_FLAG_ITERABEGIN
) {
2163 pgrp
->pg_listflags
|= PGRP_FLAG_ITERWAIT
;
2164 msleep(&pgrp
->pg_listflags
, &pgrp
->pg_mlock
, 0, "pgrp_iterate", 0);
2166 pgrp
->pg_listflags
|= PGRP_FLAG_ITERABEGIN
;
2172 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
2173 p
= p
->p_pglist
.le_next
) {
2174 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
2175 pid_list
[pidcount
] = p
->p_pid
;
2177 if (pidcount
>= count
)
2184 if ((serialize
== 0) && (dropref
!= 0))
2188 for (i
= 0; i
< pidcount
; i
++) {
2189 /* No handling or proc0 */
2190 if (pid_list
[i
] == 0)
2192 p
= proc_find(pid_list
[i
]);
2194 if (p
->p_pgrpid
!= pgid
) {
2198 proc_transwait(p
, 0);
2199 retval
= callout(p
, arg
);
2203 case PROC_RETURNED_DONE
:
2205 if (retval
== PROC_RETURNED_DONE
) {
2210 case PROC_CLAIMED_DONE
:
2219 if (serialize
!= 0) {
2221 pgrp
->pg_listflags
&= ~PGRP_FLAG_ITERABEGIN
;
2222 if ((pgrp
->pg_listflags
& PGRP_FLAG_ITERWAIT
) == PGRP_FLAG_ITERWAIT
) {
2223 pgrp
->pg_listflags
&= ~PGRP_FLAG_ITERWAIT
;
2224 wakeup(&pgrp
->pg_listflags
);
2230 kfree(pid_list
, alloc_count
);
2235 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2238 child
->p_pgrp
= pgrp
;
2239 child
->p_pgrpid
= pgrp
->pg_id
;
2240 child
->p_listflag
|= P_LIST_INPGRP
;
2242 * When pgrp is being freed , a process can still
2243 * request addition using setpgid from bash when
2244 * login is terminated (login cycler) return ESRCH
2245 * Safe to hold lock due to refcount on pgrp
2247 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2248 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2251 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2252 panic("pgrp_add : pgrp is dead adding process");
2256 pgrp
->pg_membercnt
++;
2257 if ( parent
!= PROC_NULL
) {
2258 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2260 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2265 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2266 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2272 pgrp_remove(struct proc
* p
)
2279 #if __PROC_INTERNAL_DEBUG
2280 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2281 panic("removing from pglist but no named ref\n");
2283 p
->p_pgrpid
= PGRPID_DEAD
;
2284 p
->p_listflag
&= ~P_LIST_INPGRP
;
2288 if (pg
== PGRP_NULL
)
2289 panic("pgrp_remove: pg is NULL");
2293 if (pg
->pg_membercnt
< 0)
2294 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",pg
, p
);
2296 LIST_REMOVE(p
, p_pglist
);
2297 if (pg
->pg_members
.lh_first
== 0) {
2299 pgdelete_dropref(pg
);
2307 /* cannot use proc_pgrp as it maybe stalled */
2309 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2311 struct pgrp
* oldpg
;
2317 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2318 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2319 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2322 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2325 if (oldpg
== PGRP_NULL
)
2326 panic("pgrp_replace: oldpg NULL");
2327 oldpg
->pg_refcount
++;
2328 #if __PROC_INTERNAL_DEBUG
2329 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2330 panic("removing from pglist but no named ref\n");
2332 p
->p_pgrpid
= PGRPID_DEAD
;
2333 p
->p_listflag
&= ~P_LIST_INPGRP
;
2339 oldpg
->pg_membercnt
--;
2340 if (oldpg
->pg_membercnt
< 0)
2341 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",oldpg
, p
);
2342 LIST_REMOVE(p
, p_pglist
);
2343 if (oldpg
->pg_members
.lh_first
== 0) {
2345 pgdelete_dropref(oldpg
);
2353 p
->p_pgrpid
= newpg
->pg_id
;
2354 p
->p_listflag
|= P_LIST_INPGRP
;
2356 * When pgrp is being freed , a process can still
2357 * request addition using setpgid from bash when
2358 * login is terminated (login cycler) return ESRCH
2359 * Safe to hold lock due to refcount on pgrp
2361 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2362 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2365 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2366 panic("pgrp_add : pgrp is dead adding process");
2370 newpg
->pg_membercnt
++;
2371 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
2375 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
2376 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2379 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
2380 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
2381 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
2382 wakeup(&p
->p_pgrpid
);
2389 pgrp_lock(struct pgrp
* pgrp
)
2391 lck_mtx_lock(&pgrp
->pg_mlock
);
2395 pgrp_unlock(struct pgrp
* pgrp
)
2397 lck_mtx_unlock(&pgrp
->pg_mlock
);
2401 session_lock(struct session
* sess
)
2403 lck_mtx_lock(&sess
->s_mlock
);
2408 session_unlock(struct session
* sess
)
2410 lck_mtx_unlock(&sess
->s_mlock
);
2422 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2423 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2424 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2429 assert(pgrp
!= NULL
);
2431 if (pgrp
!= PGRP_NULL
) {
2432 pgrp
->pg_refcount
++;
2433 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0)
2434 panic("proc_pgrp: ref being povided for dead pgrp");
2443 tty_pgrp(struct tty
* tp
)
2445 struct pgrp
* pg
= PGRP_NULL
;
2450 if (pg
!= PGRP_NULL
) {
2451 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0)
2452 panic("tty_pgrp: ref being povided for dead pgrp");
2461 proc_session(proc_t p
)
2463 struct session
* sess
= SESSION_NULL
;
2466 return(SESSION_NULL
);
2470 /* wait during transitions */
2471 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2472 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2473 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2476 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
2477 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2478 panic("proc_session:returning sesssion ref on terminating session");
2486 session_rele(struct session
*sess
)
2489 if (--sess
->s_count
== 0) {
2490 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2491 panic("session_rele: terminating already terminated session");
2492 sess
->s_listflags
|= S_LIST_TERM
;
2493 LIST_REMOVE(sess
, s_hash
);
2494 sess
->s_listflags
|= S_LIST_DEAD
;
2495 if (sess
->s_count
!= 0)
2496 panic("session_rele: freeing session in use");
2498 #if CONFIG_FINE_LOCK_GROUPS
2499 lck_mtx_destroy(&sess
->s_mlock
, proc_mlock_grp
);
2501 lck_mtx_destroy(&sess
->s_mlock
, proc_lck_grp
);
2503 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
2509 proc_transstart(proc_t p
, int locked
)
2513 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2514 if ((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
) {
2519 p
->p_lflag
|= P_LTRANSWAIT
;
2520 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2522 p
->p_lflag
|= P_LINTRANSIT
;
2523 p
->p_transholder
= current_thread();
2530 proc_transcommit(proc_t p
, int locked
)
2535 assert ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
);
2536 assert (p
->p_transholder
== current_thread());
2537 p
->p_lflag
|= P_LTRANSCOMMIT
;
2539 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2540 p
->p_lflag
&= ~P_LTRANSWAIT
;
2541 wakeup(&p
->p_lflag
);
2548 proc_transend(proc_t p
, int locked
)
2553 p
->p_lflag
&= ~( P_LINTRANSIT
| P_LTRANSCOMMIT
);
2554 p
->p_transholder
= NULL
;
2556 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2557 p
->p_lflag
&= ~P_LTRANSWAIT
;
2558 wakeup(&p
->p_lflag
);
2565 proc_transwait(proc_t p
, int locked
)
2569 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2570 if ((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
&& current_proc() == p
) {
2575 p
->p_lflag
|= P_LTRANSWAIT
;
2576 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2584 proc_klist_lock(void)
2586 lck_mtx_lock(proc_klist_mlock
);
2590 proc_klist_unlock(void)
2592 lck_mtx_unlock(proc_klist_mlock
);
2596 proc_knote(struct proc
* p
, long hint
)
2599 KNOTE(&p
->p_klist
, hint
);
2600 proc_klist_unlock();
2604 proc_knote_drain(struct proc
*p
)
2606 struct knote
*kn
= NULL
;
2609 * Clear the proc's klist to avoid references after the proc is reaped.
2612 while ((kn
= SLIST_FIRST(&p
->p_klist
))) {
2613 kn
->kn_ptr
.p_proc
= PROC_NULL
;
2614 KNOTE_DETACH(&p
->p_klist
, kn
);
2616 proc_klist_unlock();
2619 unsigned long cs_procs_killed
= 0;
2620 unsigned long cs_procs_invalidated
= 0;
2621 int cs_force_kill
= 0;
2622 int cs_force_hard
= 0;
2624 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_kill
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_force_kill
, 0, "");
2625 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_hard
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_force_hard
, 0, "");
2626 SYSCTL_INT(_vm
, OID_AUTO
, cs_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_debug
, 0, "");
2629 cs_allow_invalid(struct proc
*p
)
2632 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
2634 #if CONFIG_MACF && CONFIG_ENFORCE_SIGNED_CODE
2635 /* There needs to be a MAC policy to implement this hook, or else the
2636 * kill bits will be cleared here every time. If we have
2637 * CONFIG_ENFORCE_SIGNED_CODE, we can assume there is a policy
2638 * implementing the hook.
2640 if( 0 != mac_proc_check_run_cs_invalid(p
)) {
2641 if(cs_debug
) printf("CODE SIGNING: cs_allow_invalid() "
2642 "not allowed: pid %d\n",
2646 if(cs_debug
) printf("CODE SIGNING: cs_allow_invalid() "
2647 "allowed: pid %d\n",
2650 p
->p_csflags
&= ~(CS_KILL
| CS_HARD
| CS_VALID
);
2652 vm_map_switch_protect(get_task_map(p
->task
), FALSE
);
2654 return (p
->p_csflags
& (CS_KILL
| CS_HARD
)) == 0;
2667 * XXX revisit locking when proc is no longer protected
2668 * by the kernel funnel...
2671 /* XXX for testing */
2674 p
->p_csflags
|= CS_KILL
;
2676 p
->p_csflags
|= CS_HARD
;
2678 /* CS_KILL triggers us to send a kill signal. Nothing else. */
2679 if (p
->p_csflags
& CS_KILL
) {
2680 p
->p_csflags
|= CS_KILLED
;
2683 printf("CODE SIGNING: cs_invalid_page(0x%llx): "
2684 "p=%d[%s] honoring CS_KILL, final status 0x%x\n",
2685 vaddr
, p
->p_pid
, p
->p_comm
, p
->p_csflags
);
2688 psignal(p
, SIGKILL
);
2692 /* CS_HARD means fail the mapping operation so the process stays valid. */
2693 if (p
->p_csflags
& CS_HARD
) {
2696 printf("CODE SIGNING: cs_invalid_page(0x%llx): "
2697 "p=%d[%s] honoring CS_HARD\n",
2698 vaddr
, p
->p_pid
, p
->p_comm
);
2702 if (p
->p_csflags
& CS_VALID
) {
2703 p
->p_csflags
&= ~CS_VALID
;
2706 cs_procs_invalidated
++;
2707 printf("CODE SIGNING: cs_invalid_page(0x%llx): "
2708 "p=%d[%s] clearing CS_VALID\n",
2709 vaddr
, p
->p_pid
, p
->p_comm
);
2721 proc_setregister(proc_t p
)
2724 p
->p_lflag
|= P_LREGISTER
;
2729 proc_resetregister(proc_t p
)
2732 p
->p_lflag
&= ~P_LREGISTER
;
2737 proc_pgrpid(proc_t p
)
2745 return current_proc()->p_pgrpid
;
2749 /* return control and action states */
2751 proc_getpcontrol(int pid
, int * pcontrolp
)
2758 if (pcontrolp
!= NULL
)
2759 *pcontrolp
= p
->p_pcaction
;
2766 proc_dopcontrol(proc_t p
, void *num_found
)
2772 pcontrol
= PROC_CONTROL_STATE(p
);
2774 if (PROC_ACTION_STATE(p
) ==0) {
2777 PROC_SETACTION_STATE(p
);
2779 printf("low swap: throttling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2780 (*(int *)num_found
)++;
2784 PROC_SETACTION_STATE(p
);
2786 printf("low swap: suspending pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2787 task_suspend(p
->task
);
2788 (*(int *)num_found
)++;
2792 PROC_SETACTION_STATE(p
);
2794 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2795 psignal(p
, SIGKILL
);
2796 (*(int *)num_found
)++;
2806 return(PROC_RETURNED
);
2811 * Resume a throttled or suspended process. This is an internal interface that's only
2812 * used by the user level code that presents the GUI when we run out of swap space and
2813 * hence is restricted to processes with superuser privileges.
2817 proc_resetpcontrol(int pid
)
2822 proc_t self
= current_proc();
2824 /* if the process has been validated to handle resource control or root is valid one */
2825 if (((self
->p_lflag
& P_LVMRSRCOWNER
) == 0) && (error
= suser(kauth_cred_get(), 0)))
2834 pcontrol
= PROC_CONTROL_STATE(p
);
2836 if(PROC_ACTION_STATE(p
) !=0) {
2839 PROC_RESETACTION_STATE(p
);
2841 printf("low swap: unthrottling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2845 PROC_RESETACTION_STATE(p
);
2847 printf("low swap: resuming pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2848 task_resume(p
->task
);
2853 PROC_SETACTION_STATE(p
);
2855 printf("low swap: attempt to unkill pid %d (%s) ignored\n", p
->p_pid
, p
->p_comm
);
2871 * Return true if the specified process has an action state specified for it and it isn't
2872 * already in an action state and it's using more physical memory than the specified threshold.
2873 * Note: the memory_threshold argument is specified in bytes and is of type uint64_t.
2877 proc_pcontrol_filter(proc_t p
, void *memory_thresholdp
)
2880 return PROC_CONTROL_STATE(p
) && /* if there's an action state specified... */
2881 (PROC_ACTION_STATE(p
) == 0) && /* and we're not in the action state yet... */
2882 (get_task_resident_size(p
->task
) > *((uint64_t *)memory_thresholdp
)); /* and this proc is over the mem threshold, */
2883 /* then return true to take action on this proc */
2889 * Deal with the out of swap space condition. This routine gets called when
2890 * we want to swap something out but there's no more space left. Since this
2891 * creates a memory deadlock situtation, we need to take action to free up
2892 * some memory resources in order to prevent the system from hanging completely.
2893 * The action we take is based on what the system processes running at user level
2894 * have specified. Processes are marked in one of four categories: ones that
2895 * can be killed immediately, ones that should be suspended, ones that should
2896 * be throttled, and all the rest which are basically none of the above. Which
2897 * processes are marked as being in which category is a user level policy decision;
2898 * we just take action based on those decisions here.
2901 #define STARTING_PERCENTAGE 50 /* memory threshold expressed as a percentage */
2902 /* of physical memory */
2904 struct timeval last_no_space_action
= {0, 0};
2907 no_paging_space_action(void)
2910 uint64_t memory_threshold
;
2915 * Throttle how often we come through here. Once every 20 seconds should be plenty.
2920 if (now
.tv_sec
<= last_no_space_action
.tv_sec
+ 20)
2923 last_no_space_action
= now
;
2926 * Examine all processes and find those that have been marked to have some action
2927 * taken when swap space runs out. Of those processes, select one or more and
2928 * apply the specified action to them. The idea is to only take action against
2929 * a few processes rather than hitting too many at once. If the low swap condition
2930 * persists, this routine will get called again and we'll take action against more
2933 * Of the processes that have been marked, we choose which ones to take action
2934 * against according to how much physical memory they're presently using. We
2935 * start with the STARTING_THRESHOLD and any processes using more physical memory
2936 * than the percentage threshold will have action taken against it. If there
2937 * are no processes over the threshold, then the threshold is cut in half and we
2938 * look again for processes using more than this threshold. We continue in
2939 * this fashion until we find at least one process to take action against. This
2940 * iterative approach is less than ideally efficient, however we only get here
2941 * when the system is almost in a memory deadlock and is pretty much just
2942 * thrashing if it's doing anything at all. Therefore, the cpu overhead of
2943 * potentially multiple passes here probably isn't revelant.
2946 memory_threshold
= (sane_size
* STARTING_PERCENTAGE
) / 100; /* resident threshold in bytes */
2948 for (num_found
= 0; num_found
== 0; memory_threshold
= memory_threshold
/ 2) {
2949 proc_iterate(PROC_ALLPROCLIST
, proc_dopcontrol
, (void *)&num_found
, proc_pcontrol_filter
, (void *)&memory_threshold
);
2952 * If we just looked with memory_threshold == 0, then there's no need to iterate any further since
2953 * we won't find any eligible processes at this point.
2956 if (memory_threshold
== 0) {
2957 if (num_found
== 0) /* log that we couldn't do anything in this case */
2958 printf("low swap: unable to find any eligible processes to take action on\n");