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/coalition.h>
103 #include <sys/coalition.h>
104 #include <kern/assert.h>
105 #include <vm/vm_protos.h>
106 #include <vm/vm_map.h> /* vm_map_switch_protect() */
107 #include <vm/vm_pageout.h>
108 #include <mach/task.h>
109 #include <mach/message.h>
110 #include <sys/priv.h>
111 #include <sys/proc_info.h>
112 #include <sys/bsdtask_info.h>
114 #if CONFIG_MEMORYSTATUS
115 #include <sys/kern_memorystatus.h>
119 #include <security/mac_framework.h>
122 #include <libkern/crypto/sha1.h>
125 * Structure associated with user cacheing.
128 LIST_ENTRY(uidinfo
) ui_hash
;
132 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
133 LIST_HEAD(uihashhead
, uidinfo
) *uihashtbl
;
134 u_long uihash
; /* size of hash table - 1 */
137 * Other process lists
139 struct pidhashhead
*pidhashtbl
;
141 struct pgrphashhead
*pgrphashtbl
;
143 struct sesshashhead
*sesshashtbl
;
146 struct proclist allproc
;
147 struct proclist zombproc
;
148 extern struct tty cons
;
153 #define __PROC_INTERNAL_DEBUG 1
155 /* Name to give to core files */
156 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/cores/core.%P"};
159 extern uint32_t fastbacktrace(uintptr_t* bt
, uint32_t max_frames
) __attribute__((noinline
));
162 static void orphanpg(struct pgrp
*pg
);
163 void proc_name_kdp(task_t t
, char * buf
, int size
);
164 int proc_threadname_kdp(void *uth
, char *buf
, size_t size
);
165 void proc_starttime_kdp(void *p
, uint64_t *tv_sec
, uint64_t *tv_usec
);
166 char *proc_name_address(void *p
);
168 static void pgrp_add(struct pgrp
* pgrp
, proc_t parent
, proc_t child
);
169 static void pgrp_remove(proc_t p
);
170 static void pgrp_replace(proc_t p
, struct pgrp
*pgrp
);
171 static void pgdelete_dropref(struct pgrp
*pgrp
);
172 extern void pg_rele_dropref(struct pgrp
* pgrp
);
173 static int csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaddittoken
);
174 static boolean_t
proc_parent_is_currentproc(proc_t p
);
176 struct fixjob_iterargs
{
178 struct session
* mysession
;
182 int fixjob_callback(proc_t
, void *);
185 * Initialize global process hashing structures.
191 LIST_INIT(&zombproc
);
192 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
193 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
194 sesshashtbl
= hashinit(maxproc
/ 4, M_PROC
, &sesshash
);
195 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
199 * Change the count associated with number of processes
200 * a given user is using. This routine protects the uihash
204 chgproccnt(uid_t uid
, int diff
)
207 struct uidinfo
*newuip
= NULL
;
208 struct uihashhead
*uipp
;
214 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
)
215 if (uip
->ui_uid
== uid
)
218 uip
->ui_proccnt
+= diff
;
219 if (uip
->ui_proccnt
> 0) {
220 retval
= uip
->ui_proccnt
;
224 if (uip
->ui_proccnt
< 0)
225 panic("chgproccnt: procs < 0");
226 LIST_REMOVE(uip
, ui_hash
);
229 FREE_ZONE(uip
, sizeof(*uip
), M_PROC
);
238 panic("chgproccnt: lost user");
240 if (newuip
!= NULL
) {
243 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
245 uip
->ui_proccnt
= diff
;
251 MALLOC_ZONE(newuip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
253 panic("chgproccnt: M_PROC zone depleted");
257 FREE_ZONE(newuip
, sizeof(*uip
), M_PROC
);
262 * Is p an inferior of the current process?
270 for (; p
!= current_proc(); p
= p
->p_pptr
)
280 * Is p an inferior of t ?
283 isinferior(proc_t p
, proc_t t
)
289 /* if p==t they are not inferior */
294 for (; p
!= t
; p
= p
->p_pptr
) {
297 /* Detect here if we're in a cycle */
298 if ((p
->p_pid
== 0) || (p
->p_pptr
== start
) || (nchecked
>= nprocs
))
308 proc_isinferior(int pid1
, int pid2
)
310 proc_t p
= PROC_NULL
;
311 proc_t t
= PROC_NULL
;
314 if (((p
= proc_find(pid1
)) != (proc_t
)0 ) && ((t
= proc_find(pid2
)) != (proc_t
)0))
315 retval
= isinferior(p
, t
);
328 return(proc_findinternal(pid
, 0));
332 proc_findinternal(int pid
, int locked
)
334 proc_t p
= PROC_NULL
;
340 p
= pfind_locked(pid
);
341 if ((p
== PROC_NULL
) || (p
!= proc_ref_locked(p
)))
352 proc_findthread(thread_t thread
)
354 proc_t p
= PROC_NULL
;
358 uth
= get_bsdthread_info(thread
);
359 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
362 p
= (proc_t
)(get_bsdthreadtask_info(thread
));
363 p
= proc_ref_locked(p
);
370 uthread_reset_proc_refcount(void *uthread
) {
373 if (proc_ref_tracking_disabled
) {
377 uth
= (uthread_t
) uthread
;
379 uth
->uu_proc_refcount
= 0;
384 uthread_get_proc_refcount(void *uthread
) {
387 if (proc_ref_tracking_disabled
) {
391 uth
= (uthread_t
) uthread
;
393 return uth
->uu_proc_refcount
;
397 record_procref(proc_t p
, int count
) {
400 if (proc_ref_tracking_disabled
) {
404 uth
= current_uthread();
405 uth
->uu_proc_refcount
+= count
;
408 if (uth
->uu_pindex
< NUM_PROC_REFS_TO_TRACK
) {
409 fastbacktrace((uintptr_t *) &uth
->uu_proc_pcs
[uth
->uu_pindex
], PROC_REF_STACK_DEPTH
);
411 uth
->uu_proc_ps
[uth
->uu_pindex
] = p
;
436 if (p
!= proc_ref_locked(p
))
444 proc_ref_locked(proc_t p
)
448 /* if process still in creation return failure */
449 if ((p
== PROC_NULL
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0))
451 /* do not return process marked for termination */
452 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)) {
455 record_procref(p
, 1);
465 proc_rele_locked(proc_t p
)
468 if (p
->p_refcount
> 0) {
471 record_procref(p
, -1);
473 if ((p
->p_refcount
== 0) && ((p
->p_listflag
& P_LIST_DRAINWAIT
) == P_LIST_DRAINWAIT
)) {
474 p
->p_listflag
&= ~P_LIST_DRAINWAIT
;
475 wakeup(&p
->p_refcount
);
478 panic("proc_rele_locked -ve ref\n");
483 proc_find_zombref(int pid
)
490 p
= pfind_locked(pid
);
492 /* should we bail? */
493 if ((p
== PROC_NULL
) /* not found */
494 || ((p
->p_listflag
& P_LIST_INCREATE
) != 0) /* not created yet */
495 || ((p
->p_listflag
& P_LIST_EXITED
) == 0)) { /* not started exit */
501 /* If someone else is controlling the (unreaped) zombie - wait */
502 if ((p
->p_listflag
& P_LIST_WAITING
) != 0) {
503 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
506 p
->p_listflag
|= P_LIST_WAITING
;
514 proc_drop_zombref(proc_t p
)
517 if ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
518 p
->p_listflag
&= ~P_LIST_WAITING
;
526 proc_refdrain(proc_t p
)
531 p
->p_listflag
|= P_LIST_DRAIN
;
532 while (p
->p_refcount
) {
533 p
->p_listflag
|= P_LIST_DRAINWAIT
;
534 msleep(&p
->p_refcount
, proc_list_mlock
, 0, "proc_refdrain", 0) ;
536 p
->p_listflag
&= ~P_LIST_DRAIN
;
537 p
->p_listflag
|= P_LIST_DEAD
;
545 proc_parentholdref(proc_t p
)
547 proc_t parent
= PROC_NULL
;
555 if ((pp
== PROC_NULL
) || (pp
->p_stat
== SZOMB
) || ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
))) {
560 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == P_LIST_CHILDDRSTART
) {
561 pp
->p_listflag
|= P_LIST_CHILDDRWAIT
;
562 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
571 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == 0) {
582 proc_parentdropref(proc_t p
, int listlocked
)
587 if (p
->p_parentref
> 0) {
589 if ((p
->p_parentref
== 0) && ((p
->p_listflag
& P_LIST_PARENTREFWAIT
) == P_LIST_PARENTREFWAIT
)) {
590 p
->p_listflag
&= ~P_LIST_PARENTREFWAIT
;
591 wakeup(&p
->p_parentref
);
594 panic("proc_parentdropref -ve ref\n");
602 proc_childdrainstart(proc_t p
)
604 #if __PROC_INTERNAL_DEBUG
605 if ((p
->p_listflag
& P_LIST_CHILDDRSTART
) == P_LIST_CHILDDRSTART
)
606 panic("proc_childdrainstart: childdrain already started\n");
608 p
->p_listflag
|= P_LIST_CHILDDRSTART
;
609 /* wait for all that hold parentrefs to drop */
610 while (p
->p_parentref
> 0) {
611 p
->p_listflag
|= P_LIST_PARENTREFWAIT
;
612 msleep(&p
->p_parentref
, proc_list_mlock
, 0, "proc_childdrainstart", 0) ;
618 proc_childdrainend(proc_t p
)
620 #if __PROC_INTERNAL_DEBUG
621 if (p
->p_childrencnt
> 0)
622 panic("exiting: children stil hanging around\n");
624 p
->p_listflag
|= P_LIST_CHILDDRAINED
;
625 if ((p
->p_listflag
& (P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
)) != 0) {
626 p
->p_listflag
&= ~(P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
);
627 wakeup(&p
->p_childrencnt
);
632 proc_checkdeadrefs(__unused proc_t p
)
634 #if __PROC_INTERNAL_DEBUG
635 if ((p
->p_listflag
& P_LIST_INHASH
) != 0)
636 panic("proc being freed and still in hash %p: %u\n", p
, p
->p_listflag
);
637 if (p
->p_childrencnt
!= 0)
638 panic("proc being freed and pending children cnt %p:%d\n", p
, p
->p_childrencnt
);
639 if (p
->p_refcount
!= 0)
640 panic("proc being freed and pending refcount %p:%d\n", p
, p
->p_refcount
);
641 if (p
->p_parentref
!= 0)
642 panic("proc being freed and pending parentrefs %p:%d\n", p
, p
->p_parentref
);
665 return (current_proc()->p_pid
);
671 return (current_proc()->p_ppid
);
676 dtrace_current_proc_vforking(void)
678 thread_t th
= current_thread();
679 struct uthread
*ut
= get_bsdthread_info(th
);
682 ((ut
->uu_flag
& (UT_VFORK
|UT_VFORKING
)) == (UT_VFORK
|UT_VFORKING
))) {
684 * Handle the narrow window where we're in the vfork syscall,
685 * but we're not quite ready to claim (in particular, to DTrace)
686 * that we're running as the child.
688 return (get_bsdtask_info(get_threadtask(th
)));
690 return (current_proc());
694 dtrace_proc_selfpid(void)
696 return (dtrace_current_proc_vforking()->p_pid
);
700 dtrace_proc_selfppid(void)
702 return (dtrace_current_proc_vforking()->p_ppid
);
706 dtrace_proc_selfruid(void)
708 return (dtrace_current_proc_vforking()->p_ruid
);
710 #endif /* CONFIG_DTRACE */
713 proc_parent(proc_t p
)
721 parent
= proc_ref_locked(pp
);
722 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)){
723 pp
->p_listflag
|= P_LIST_CHILDLKWAIT
;
724 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
732 proc_parent_is_currentproc(proc_t p
)
734 boolean_t ret
= FALSE
;
737 if (p
->p_pptr
== current_proc())
745 proc_name(int pid
, char * buf
, int size
)
749 if ((p
= proc_find(pid
)) != PROC_NULL
) {
750 strlcpy(buf
, &p
->p_comm
[0], size
);
756 proc_name_kdp(task_t t
, char * buf
, int size
)
758 proc_t p
= get_bsdtask_info(t
);
762 if ((size_t)size
> sizeof(p
->p_comm
))
763 strlcpy(buf
, &p
->p_name
[0], MIN((int)sizeof(p
->p_name
), size
));
765 strlcpy(buf
, &p
->p_comm
[0], MIN((int)sizeof(p
->p_comm
), size
));
770 proc_threadname_kdp(void *uth
, char *buf
, size_t size
)
772 if (size
< MAXTHREADNAMESIZE
) {
773 /* this is really just a protective measure for the future in
774 * case the thread name size in stackshot gets out of sync with
775 * the BSD max thread name size. Note that bsd_getthreadname
776 * doesn't take input buffer size into account. */
781 bsd_getthreadname(uth
, buf
);
786 /* note that this function is generally going to be called from stackshot,
787 * and the arguments will be coming from a struct which is declared packed
788 * thus the input arguments will in general be unaligned. We have to handle
791 proc_starttime_kdp(void *p
, uint64_t *tv_sec
, uint64_t *tv_usec
)
793 proc_t pp
= (proc_t
)p
;
796 } __attribute__((packed
));
798 if (pp
!= PROC_NULL
) {
800 ((struct uint64p
*)tv_sec
)->val
= pp
->p_start
.tv_sec
;
802 ((struct uint64p
*)tv_usec
)->val
= pp
->p_start
.tv_usec
;
807 proc_name_address(void *p
)
809 return &((proc_t
)p
)->p_comm
[0];
813 proc_selfname(char * buf
, int size
)
817 if ((p
= current_proc())!= (proc_t
)0) {
818 strlcpy(buf
, &p
->p_comm
[0], size
);
823 proc_signal(int pid
, int signum
)
827 if ((p
= proc_find(pid
)) != PROC_NULL
) {
834 proc_issignal(int pid
, sigset_t mask
)
839 if ((p
= proc_find(pid
)) != PROC_NULL
) {
840 error
= proc_pendingsignals(p
, mask
);
848 proc_noremotehang(proc_t p
)
853 retval
= p
->p_flag
& P_NOREMOTEHANG
;
854 return(retval
? 1: 0);
859 proc_exiting(proc_t p
)
864 retval
= p
->p_lflag
& P_LEXIT
;
865 return(retval
? 1: 0);
869 proc_forcequota(proc_t p
)
874 retval
= p
->p_flag
& P_FORCEQUOTA
;
875 return(retval
? 1: 0);
882 kauth_cred_t my_cred
;
885 my_cred
= kauth_cred_proc_ref(p
);
886 error
= suser(my_cred
, &p
->p_acflag
);
887 kauth_cred_unref(&my_cred
);
892 proc_task(proc_t proc
)
894 return (task_t
)proc
->task
;
898 * Obtain the first thread in a process
900 * XXX This is a bad thing to do; it exists predominantly to support the
901 * XXX use of proc_t's in places that should really be using
902 * XXX thread_t's instead. This maintains historical behaviour, but really
903 * XXX needs an audit of the context (proxy vs. not) to clean up.
906 proc_thread(proc_t proc
)
908 uthread_t uth
= TAILQ_FIRST(&proc
->p_uthlist
);
911 return(uth
->uu_context
.vc_thread
);
925 thread_t th
= current_thread();
927 return((struct uthread
*)get_bsdthread_info(th
));
932 proc_is64bit(proc_t p
)
934 return(IS_64BIT_PROCESS(p
));
938 proc_pidversion(proc_t p
)
940 return(p
->p_idversion
);
944 proc_uniqueid(proc_t p
)
946 return(p
->p_uniqueid
);
950 proc_puniqueid(proc_t p
)
952 return(p
->p_puniqueid
);
956 proc_coalitionids(__unused proc_t p
, __unused
uint64_t ids
[COALITION_NUM_TYPES
])
958 #if CONFIG_COALITIONS
959 task_coalition_ids(p
->task
, ids
);
961 memset(ids
, 0, sizeof(uint64_t [COALITION_NUM_TYPES
]));
967 proc_was_throttled(proc_t p
)
969 return (p
->was_throttled
);
973 proc_did_throttle(proc_t p
)
975 return (p
->did_throttle
);
979 proc_getcdhash(proc_t p
, unsigned char *cdhash
)
981 return vn_getcdhash(p
->p_textvp
, p
->p_textoff
, cdhash
);
985 proc_getexecutableuuid(proc_t p
, unsigned char *uuidbuf
, unsigned long size
)
987 if (size
>= sizeof(p
->p_uuid
)) {
988 memcpy(uuidbuf
, p
->p_uuid
, sizeof(p
->p_uuid
));
992 /* Return vnode for executable with an iocount. Must be released with vnode_put() */
994 proc_getexecutablevnode(proc_t p
)
996 vnode_t tvp
= p
->p_textvp
;
998 if ( tvp
!= NULLVP
) {
999 if (vnode_getwithref(tvp
) == 0) {
1009 bsd_set_dependency_capable(task_t task
)
1011 proc_t p
= get_bsdtask_info(task
);
1014 OSBitOrAtomic(P_DEPENDENCY_CAPABLE
, &p
->p_flag
);
1020 IS_64BIT_PROCESS(proc_t p
)
1022 if (p
&& (p
->p_flag
& P_LP64
))
1029 * Locate a process by number
1032 pfind_locked(pid_t pid
)
1042 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
) {
1043 if (p
->p_pid
== pid
) {
1045 for (q
= p
->p_hash
.le_next
; q
!= 0; q
= q
->p_hash
.le_next
) {
1046 if ((p
!=q
) && (q
->p_pid
== pid
))
1047 panic("two procs with same pid %p:%p:%d:%d\n", p
, q
, p
->p_pid
, q
->p_pid
);
1057 * Locate a zombie by PID
1059 __private_extern__ proc_t
1067 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
1068 if (p
->p_pid
== pid
)
1077 * Locate a process group by number
1086 pgrp
= pgfind_internal(pgid
);
1087 if ((pgrp
== NULL
) || ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) != 0))
1090 pgrp
->pg_refcount
++;
1098 pgfind_internal(pid_t pgid
)
1102 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
)
1103 if (pgrp
->pg_id
== pgid
)
1109 pg_rele(struct pgrp
* pgrp
)
1111 if(pgrp
== PGRP_NULL
)
1113 pg_rele_dropref(pgrp
);
1117 pg_rele_dropref(struct pgrp
* pgrp
)
1120 if ((pgrp
->pg_refcount
== 1) && ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) == PGRP_FLAG_TERMINATE
)) {
1122 pgdelete_dropref(pgrp
);
1126 pgrp
->pg_refcount
--;
1131 session_find_internal(pid_t sessid
)
1133 struct session
*sess
;
1135 for (sess
= SESSHASH(sessid
)->lh_first
; sess
!= 0; sess
= sess
->s_hash
.le_next
)
1136 if (sess
->s_sid
== sessid
)
1143 * Make a new process ready to become a useful member of society by making it
1144 * visible in all the right places and initialize its own lists to empty.
1146 * Parameters: parent The parent of the process to insert
1147 * child The child process to insert
1151 * Notes: Insert a child process into the parents process group, assign
1152 * the child the parent process pointer and PPID of the parent,
1153 * place it on the parents p_children list as a sibling,
1154 * initialize its own child list, place it in the allproc list,
1155 * insert it in the proper hash bucket, and initialize its
1159 pinsertchild(proc_t parent
, proc_t child
)
1163 LIST_INIT(&child
->p_children
);
1164 TAILQ_INIT(&child
->p_evlist
);
1165 child
->p_pptr
= parent
;
1166 child
->p_ppid
= parent
->p_pid
;
1167 child
->p_puniqueid
= parent
->p_uniqueid
;
1169 pg
= proc_pgrp(parent
);
1170 pgrp_add(pg
, parent
, child
);
1175 #if CONFIG_MEMORYSTATUS
1176 memorystatus_add(child
, TRUE
);
1179 parent
->p_childrencnt
++;
1180 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1182 LIST_INSERT_HEAD(&allproc
, child
, p_list
);
1183 /* mark the completion of proc creation */
1184 child
->p_listflag
&= ~P_LIST_INCREATE
;
1190 * Move p to a new or existing process group (and session)
1192 * Returns: 0 Success
1193 * ESRCH No such process
1196 enterpgrp(proc_t p
, pid_t pgid
, int mksess
)
1199 struct pgrp
*mypgrp
;
1200 struct session
* procsp
;
1202 pgrp
= pgfind(pgid
);
1203 mypgrp
= proc_pgrp(p
);
1204 procsp
= proc_session(p
);
1207 if (pgrp
!= NULL
&& mksess
) /* firewalls */
1208 panic("enterpgrp: setsid into non-empty pgrp");
1209 if (SESS_LEADER(p
, procsp
))
1210 panic("enterpgrp: session leader attempted setpgrp");
1212 if (pgrp
== PGRP_NULL
) {
1213 pid_t savepid
= p
->p_pid
;
1214 proc_t np
= PROC_NULL
;
1219 if (p
->p_pid
!= pgid
)
1220 panic("enterpgrp: new pgrp and pid != pgid");
1222 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
1225 panic("enterpgrp: M_PGRP zone depleted");
1226 if ((np
= proc_find(savepid
)) == NULL
|| np
!= p
) {
1227 if (np
!= PROC_NULL
)
1229 if (mypgrp
!= PGRP_NULL
)
1231 if (procsp
!= SESSION_NULL
)
1232 session_rele(procsp
);
1233 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
1238 struct session
*sess
;
1243 MALLOC_ZONE(sess
, struct session
*,
1244 sizeof(struct session
), M_SESSION
, M_WAITOK
);
1246 panic("enterpgrp: M_SESSION zone depleted");
1248 sess
->s_sid
= p
->p_pid
;
1250 sess
->s_ttyvp
= NULL
;
1251 sess
->s_ttyp
= TTY_NULL
;
1253 sess
->s_listflags
= 0;
1254 sess
->s_ttypgrpid
= NO_PID
;
1255 #if CONFIG_FINE_LOCK_GROUPS
1256 lck_mtx_init(&sess
->s_mlock
, proc_mlock_grp
, proc_lck_attr
);
1258 lck_mtx_init(&sess
->s_mlock
, proc_lck_grp
, proc_lck_attr
);
1260 bcopy(procsp
->s_login
, sess
->s_login
,
1261 sizeof(sess
->s_login
));
1262 OSBitAndAtomic(~((uint32_t)P_CONTROLT
), &p
->p_flag
);
1264 LIST_INSERT_HEAD(SESSHASH(sess
->s_sid
), sess
, s_hash
);
1266 pgrp
->pg_session
= sess
;
1268 if (p
!= current_proc())
1269 panic("enterpgrp: mksession and p != curproc");
1273 pgrp
->pg_session
= procsp
;
1275 if ((pgrp
->pg_session
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1276 panic("enterpgrp: providing ref to terminating session ");
1277 pgrp
->pg_session
->s_count
++;
1281 #if CONFIG_FINE_LOCK_GROUPS
1282 lck_mtx_init(&pgrp
->pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
1284 lck_mtx_init(&pgrp
->pg_mlock
, proc_lck_grp
, proc_lck_attr
);
1286 LIST_INIT(&pgrp
->pg_members
);
1287 pgrp
->pg_membercnt
= 0;
1290 pgrp
->pg_refcount
= 1;
1291 pgrp
->pg_listflags
= 0;
1292 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
1294 } else if (pgrp
== mypgrp
) {
1298 if (procsp
!= SESSION_NULL
)
1299 session_rele(procsp
);
1303 if (procsp
!= SESSION_NULL
)
1304 session_rele(procsp
);
1306 * Adjust eligibility of affected pgrps to participate in job control.
1307 * Increment eligibility counts before decrementing, otherwise we
1308 * could reach 0 spuriously during the first call.
1310 fixjobc(p
, pgrp
, 1);
1311 fixjobc(p
, mypgrp
, 0);
1313 if(mypgrp
!= PGRP_NULL
)
1315 pgrp_replace(p
, pgrp
);
1322 * remove process from process group
1333 * delete a process group
1336 pgdelete_dropref(struct pgrp
*pgrp
)
1340 struct session
*sessp
;
1344 if (pgrp
->pg_membercnt
!= 0) {
1350 pgrp
->pg_refcount
--;
1351 if ((emptypgrp
== 0) || (pgrp
->pg_membercnt
!= 0)) {
1356 pgrp
->pg_listflags
|= PGRP_FLAG_TERMINATE
;
1358 if (pgrp
->pg_refcount
> 0) {
1363 pgrp
->pg_listflags
|= PGRP_FLAG_DEAD
;
1364 LIST_REMOVE(pgrp
, pg_hash
);
1368 ttyp
= SESSION_TP(pgrp
->pg_session
);
1369 if (ttyp
!= TTY_NULL
) {
1370 if (ttyp
->t_pgrp
== pgrp
) {
1372 /* Re-check after acquiring the lock */
1373 if (ttyp
->t_pgrp
== pgrp
) {
1374 ttyp
->t_pgrp
= NULL
;
1375 pgrp
->pg_session
->s_ttypgrpid
= NO_PID
;
1383 sessp
= pgrp
->pg_session
;
1384 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1385 panic("pg_deleteref: manipulating refs of already terminating session");
1386 if (--sessp
->s_count
== 0) {
1387 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1388 panic("pg_deleteref: terminating already terminated session");
1389 sessp
->s_listflags
|= S_LIST_TERM
;
1390 ttyp
= SESSION_TP(sessp
);
1391 LIST_REMOVE(sessp
, s_hash
);
1393 if (ttyp
!= TTY_NULL
) {
1395 if (ttyp
->t_session
== sessp
)
1396 ttyp
->t_session
= NULL
;
1400 sessp
->s_listflags
|= S_LIST_DEAD
;
1401 if (sessp
->s_count
!= 0)
1402 panic("pg_deleteref: freeing session in use");
1404 #if CONFIG_FINE_LOCK_GROUPS
1405 lck_mtx_destroy(&sessp
->s_mlock
, proc_mlock_grp
);
1407 lck_mtx_destroy(&sessp
->s_mlock
, proc_lck_grp
);
1409 FREE_ZONE(sessp
, sizeof(struct session
), M_SESSION
);
1412 #if CONFIG_FINE_LOCK_GROUPS
1413 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_mlock_grp
);
1415 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_lck_grp
);
1417 FREE_ZONE(pgrp
, sizeof(*pgrp
), M_PGRP
);
1422 * Adjust pgrp jobc counters when specified process changes process group.
1423 * We count the number of processes in each process group that "qualify"
1424 * the group for terminal job control (those with a parent in a different
1425 * process group of the same session). If that count reaches zero, the
1426 * process group becomes orphaned. Check both the specified process'
1427 * process group and that of its children.
1428 * entering == 0 => p is leaving specified group.
1429 * entering == 1 => p is entering specified group.
1432 fixjob_callback(proc_t p
, void * arg
)
1434 struct fixjob_iterargs
*fp
;
1435 struct pgrp
* pg
, *hispg
;
1436 struct session
* mysession
, *hissess
;
1439 fp
= (struct fixjob_iterargs
*)arg
;
1441 mysession
= fp
->mysession
;
1442 entering
= fp
->entering
;
1444 hispg
= proc_pgrp(p
);
1445 hissess
= proc_session(p
);
1447 if ((hispg
!= pg
) &&
1448 (hissess
== mysession
)) {
1453 } else if (--hispg
->pg_jobc
== 0) {
1459 if (hissess
!= SESSION_NULL
)
1460 session_rele(hissess
);
1461 if (hispg
!= PGRP_NULL
)
1464 return(PROC_RETURNED
);
1468 fixjobc(proc_t p
, struct pgrp
*pgrp
, int entering
)
1470 struct pgrp
*hispgrp
= PGRP_NULL
;
1471 struct session
*hissess
= SESSION_NULL
;
1472 struct session
*mysession
= pgrp
->pg_session
;
1474 struct fixjob_iterargs fjarg
;
1475 boolean_t proc_parent_self
;
1478 * Check if p's parent is current proc, if yes then no need to take
1479 * a ref; calling proc_parent with current proc as parent may
1480 * deadlock if current proc is exiting.
1482 proc_parent_self
= proc_parent_is_currentproc(p
);
1483 if (proc_parent_self
)
1484 parent
= current_proc();
1486 parent
= proc_parent(p
);
1488 if (parent
!= PROC_NULL
) {
1489 hispgrp
= proc_pgrp(parent
);
1490 hissess
= proc_session(parent
);
1491 if (!proc_parent_self
)
1497 * Check p's parent to see whether p qualifies its own process
1498 * group; if so, adjust count for p's process group.
1500 if ((hispgrp
!= pgrp
) &&
1501 (hissess
== mysession
)) {
1506 }else if (--pgrp
->pg_jobc
== 0) {
1513 if (hissess
!= SESSION_NULL
)
1514 session_rele(hissess
);
1515 if (hispgrp
!= PGRP_NULL
)
1519 * Check this process' children to see whether they qualify
1520 * their process groups; if so, adjust counts for children's
1524 fjarg
.mysession
= mysession
;
1525 fjarg
.entering
= entering
;
1526 proc_childrenwalk(p
, fixjob_callback
, &fjarg
);
1530 * A process group has become orphaned;
1531 * if there are any stopped processes in the group,
1532 * hang-up all process in that group.
1535 orphanpg(struct pgrp
* pgrp
)
1539 int count
, pidcount
, i
, alloc_count
;
1541 if (pgrp
== PGRP_NULL
)
1545 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
1546 if (p
->p_stat
== SSTOP
) {
1547 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1548 p
= p
->p_pglist
.le_next
)
1550 break; /* ??? stops after finding one.. */
1556 if (count
> hard_maxproc
)
1557 count
= hard_maxproc
;
1558 alloc_count
= count
* sizeof(pid_t
);
1559 pid_list
= (pid_t
*)kalloc(alloc_count
);
1560 bzero(pid_list
, alloc_count
);
1564 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1565 p
= p
->p_pglist
.le_next
) {
1566 if (p
->p_stat
== SSTOP
) {
1567 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1568 p
= p
->p_pglist
.le_next
) {
1569 pid_list
[pidcount
] = p
->p_pid
;
1571 if (pidcount
>= count
)
1574 break; /* ??? stops after finding one.. */
1583 for (i
= 0; i
< pidcount
; i
++) {
1584 /* No handling or proc0 */
1585 if (pid_list
[i
] == 0)
1587 p
= proc_find(pid_list
[i
]);
1589 proc_transwait(p
, 0);
1592 psignal(p
, SIGCONT
);
1597 kfree(pid_list
, alloc_count
);
1602 proc_is_classic(proc_t p __unused
)
1607 /* XXX Why does this function exist? Need to kill it off... */
1609 current_proc_EXTERNAL(void)
1611 return (current_proc());
1615 proc_is_forcing_hfs_case_sensitivity(proc_t p
)
1617 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
) ? 1 : 0;
1621 * proc_core_name(name, uid, pid)
1622 * Expand the name described in corefilename, using name, uid, and pid.
1623 * corefilename is a printf-like string, with three format specifiers:
1624 * %N name of process ("name")
1625 * %P process id (pid)
1627 * For example, "%N.core" is the default; they can be disabled completely
1628 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1629 * This is controlled by the sysctl variable kern.corefile (see above).
1631 __private_extern__
int
1632 proc_core_name(const char *name
, uid_t uid
, pid_t pid
, char *cf_name
,
1635 const char *format
, *appendstr
;
1636 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
1639 if (cf_name
== NULL
)
1642 format
= corefilename
;
1643 for (i
= 0, n
= 0; n
< cf_name_len
&& format
[i
]; i
++) {
1644 switch (format
[i
]) {
1645 case '%': /* Format character */
1647 switch (format
[i
]) {
1651 case 'N': /* process name */
1654 case 'P': /* process id */
1655 snprintf(id_buf
, sizeof(id_buf
), "%u", pid
);
1658 case 'U': /* user id */
1659 snprintf(id_buf
, sizeof(id_buf
), "%u", uid
);
1665 "Unknown format character %c in `%s'\n",
1668 l
= strlen(appendstr
);
1669 if ((n
+ l
) >= cf_name_len
)
1671 bcopy(appendstr
, cf_name
+ n
, l
);
1675 cf_name
[n
++] = format
[i
];
1678 if (format
[i
] != '\0')
1682 log(LOG_ERR
, "pid %ld (%s), uid (%u): corename is too long\n",
1683 (long)pid
, name
, (uint32_t)uid
);
1687 /* Code Signing related routines */
1690 csops(__unused proc_t p
, struct csops_args
*uap
, __unused
int32_t *retval
)
1692 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1693 uap
->usersize
, USER_ADDR_NULL
));
1697 csops_audittoken(__unused proc_t p
, struct csops_audittoken_args
*uap
, __unused
int32_t *retval
)
1699 if (uap
->uaudittoken
== USER_ADDR_NULL
)
1701 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1702 uap
->usersize
, uap
->uaudittoken
));
1706 csops_copy_token(void *start
, size_t length
, user_size_t usize
, user_addr_t uaddr
)
1708 char fakeheader
[8] = { 0 };
1711 if (usize
< sizeof(fakeheader
))
1714 /* if no blob, fill in zero header */
1715 if (NULL
== start
) {
1717 length
= sizeof(fakeheader
);
1718 } else if (usize
< length
) {
1719 /* ... if input too short, copy out length of entitlement */
1720 uint32_t length32
= htonl((uint32_t)length
);
1721 memcpy(&fakeheader
[4], &length32
, sizeof(length32
));
1723 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
1725 return ERANGE
; /* input buffer to short, ERANGE signals that */
1728 return copyout(start
, uaddr
, length
);
1732 csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaudittoken
)
1734 size_t usize
= (size_t)CAST_DOWN(size_t, usersize
);
1740 unsigned char cdhash
[SHA1_RESULTLEN
];
1741 audit_token_t token
;
1742 unsigned int upid
=0, uidversion
= 0;
1744 forself
= error
= 0;
1747 pid
= proc_selfpid();
1748 if (pid
== proc_selfpid())
1755 case CS_OPS_PIDOFFSET
:
1756 case CS_OPS_ENTITLEMENTS_BLOB
:
1757 case CS_OPS_IDENTITY
:
1759 break; /* unrestricted */
1761 if (forself
== 0 && kauth_cred_issuser(kauth_cred_get()) != TRUE
)
1766 pt
= proc_find(pid
);
1767 if (pt
== PROC_NULL
)
1771 uidversion
= pt
->p_idversion
;
1772 if (uaudittoken
!= USER_ADDR_NULL
) {
1774 error
= copyin(uaudittoken
, &token
, sizeof(audit_token_t
));
1777 /* verify the audit token pid/idversion matches with proc */
1778 if ((token
.val
[5] != upid
) || (token
.val
[7] != uidversion
)) {
1786 case CS_OPS_STATUS
: {
1790 retflags
= pt
->p_csflags
;
1791 if (cs_enforcement(pt
))
1792 retflags
|= CS_ENFORCEMENT
;
1793 if (csproc_get_platform_binary(pt
))
1794 retflags
|= CS_PLATFORM_BINARY
;
1797 if (uaddr
!= USER_ADDR_NULL
)
1798 error
= copyout(&retflags
, uaddr
, sizeof(uint32_t));
1801 case CS_OPS_MARKINVALID
:
1803 if ((pt
->p_csflags
& CS_VALID
) == CS_VALID
) { /* is currently valid */
1804 pt
->p_csflags
&= ~CS_VALID
; /* set invalid */
1805 if ((pt
->p_csflags
& CS_KILL
) == CS_KILL
) {
1806 pt
->p_csflags
|= CS_KILLED
;
1809 printf("CODE SIGNING: marked invalid by pid %d: "
1810 "p=%d[%s] honoring CS_KILL, final status 0x%x\n",
1811 proc_selfpid(), pt
->p_pid
, pt
->p_comm
, pt
->p_csflags
);
1813 psignal(pt
, SIGKILL
);
1821 case CS_OPS_MARKHARD
:
1823 pt
->p_csflags
|= CS_HARD
;
1824 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1825 /* @@@ allow? reject? kill? @@@ */
1833 case CS_OPS_MARKKILL
:
1835 pt
->p_csflags
|= CS_KILL
;
1836 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1838 psignal(pt
, SIGKILL
);
1843 case CS_OPS_PIDOFFSET
:
1844 toff
= pt
->p_textoff
;
1846 error
= copyout(&toff
, uaddr
, sizeof(toff
));
1851 /* pt already holds a reference on its p_textvp */
1853 toff
= pt
->p_textoff
;
1855 if (tvp
== NULLVP
|| usize
!= SHA1_RESULTLEN
) {
1860 error
= vn_getcdhash(tvp
, toff
, cdhash
);
1864 error
= copyout(cdhash
, uaddr
, sizeof (cdhash
));
1869 case CS_OPS_ENTITLEMENTS_BLOB
: {
1875 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1881 error
= cs_entitlements_blob_get(pt
, &start
, &length
);
1886 error
= csops_copy_token(start
, length
, usize
, uaddr
);
1889 case CS_OPS_MARKRESTRICT
:
1891 pt
->p_csflags
|= CS_RESTRICT
;
1895 case CS_OPS_SET_STATUS
: {
1898 if (usize
< sizeof(flags
)) {
1903 error
= copyin(uaddr
, &flags
, sizeof(flags
));
1907 /* only allow setting a subset of all code sign flags */
1909 CS_HARD
| CS_EXEC_SET_HARD
|
1910 CS_KILL
| CS_EXEC_SET_KILL
|
1913 CS_ENFORCEMENT
| CS_EXEC_SET_ENFORCEMENT
|
1914 CS_ENTITLEMENTS_VALIDATED
;
1917 if (pt
->p_csflags
& CS_VALID
)
1918 pt
->p_csflags
|= flags
;
1930 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1936 error
= cs_blob_get(pt
, &start
, &length
);
1941 error
= csops_copy_token(start
, length
, usize
, uaddr
);
1944 case CS_OPS_IDENTITY
: {
1945 const char *identity
;
1946 uint8_t fakeheader
[8];
1951 * Make identity have a blob header to make it
1952 * easier on userland to guess the identity
1955 if (usize
< sizeof(fakeheader
)) {
1959 memset(fakeheader
, 0, sizeof(fakeheader
));
1962 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1968 identity
= cs_identity_get(pt
);
1970 if (identity
== NULL
) {
1975 length
= strlen(identity
) + 1; /* include NUL */
1976 idlen
= htonl(length
+ sizeof(fakeheader
));
1977 memcpy(&fakeheader
[4], &idlen
, sizeof(idlen
));
1979 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
1983 if (usize
< sizeof(fakeheader
) + length
)
1985 else if (usize
> sizeof(fakeheader
))
1986 error
= copyout(identity
, uaddr
+ sizeof(fakeheader
), length
);
2001 proc_iterate(flags
, callout
, arg
, filterfn
, filterarg
)
2003 int (*callout
)(proc_t
, void *);
2005 int (*filterfn
)(proc_t
, void *);
2010 int count
, pidcount
, alloc_count
, i
, retval
;
2013 if (count
> hard_maxproc
)
2014 count
= hard_maxproc
;
2015 alloc_count
= count
* sizeof(pid_t
);
2016 pid_list
= (pid_t
*)kalloc(alloc_count
);
2017 bzero(pid_list
, alloc_count
);
2024 if (flags
& PROC_ALLPROCLIST
) {
2025 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
2026 if (p
->p_stat
== SIDL
)
2028 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
2029 pid_list
[pidcount
] = p
->p_pid
;
2031 if (pidcount
>= count
)
2036 if ((pidcount
< count
) && (flags
& PROC_ZOMBPROCLIST
)) {
2037 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
2038 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
2039 pid_list
[pidcount
] = p
->p_pid
;
2041 if (pidcount
>= count
)
2051 for (i
= 0; i
< pidcount
; i
++) {
2052 p
= proc_find(pid_list
[i
]);
2054 if ((flags
& PROC_NOWAITTRANS
) == 0)
2055 proc_transwait(p
, 0);
2056 retval
= callout(p
, arg
);
2062 case PROC_RETURNED_DONE
:
2065 case PROC_CLAIMED_DONE
:
2071 } else if (flags
& PROC_ZOMBPROCLIST
) {
2072 p
= proc_find_zombref(pid_list
[i
]);
2073 if (p
!= PROC_NULL
) {
2074 retval
= callout(p
, arg
);
2078 proc_drop_zombref(p
);
2080 case PROC_RETURNED_DONE
:
2081 proc_drop_zombref(p
);
2083 case PROC_CLAIMED_DONE
:
2094 kfree(pid_list
, alloc_count
);
2101 /* This is for iteration in case of trivial non blocking callouts */
2103 proc_scanall(flags
, callout
, arg
)
2105 int (*callout
)(proc_t
, void *);
2115 if (flags
& PROC_ALLPROCLIST
) {
2116 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
2117 retval
= callout(p
, arg
);
2118 if (retval
== PROC_RETURNED_DONE
)
2122 if (flags
& PROC_ZOMBPROCLIST
) {
2123 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
2124 retval
= callout(p
, arg
);
2125 if (retval
== PROC_RETURNED_DONE
)
2139 proc_rebootscan(callout
, arg
, filterfn
, filterarg
)
2140 int (*callout
)(proc_t
, void *);
2142 int (*filterfn
)(proc_t
, void *);
2146 int lockheld
= 0, retval
;
2148 proc_shutdown_exitcount
= 0;
2156 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
2157 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
2158 p
= proc_ref_locked(p
);
2164 proc_transwait(p
, 0);
2165 retval
= callout(p
, arg
);
2169 case PROC_RETURNED_DONE
:
2170 case PROC_CLAIMED_DONE
:
2174 goto ps_allprocscan
;
2176 } /* allproc walk thru */
2178 if (lockheld
== 1) {
2190 proc_childrenwalk(parent
, callout
, arg
)
2191 struct proc
* parent
;
2192 int (*callout
)(proc_t
, void *);
2195 register struct proc
*p
;
2197 int count
, pidcount
, alloc_count
, i
, retval
;
2200 if (count
> hard_maxproc
)
2201 count
= hard_maxproc
;
2202 alloc_count
= count
* sizeof(pid_t
);
2203 pid_list
= (pid_t
*)kalloc(alloc_count
);
2204 bzero(pid_list
, alloc_count
);
2211 for (p
= parent
->p_children
.lh_first
; (p
!= 0); p
= p
->p_sibling
.le_next
) {
2212 if (p
->p_stat
== SIDL
)
2214 pid_list
[pidcount
] = p
->p_pid
;
2216 if (pidcount
>= count
)
2222 for (i
= 0; i
< pidcount
; i
++) {
2223 p
= proc_find(pid_list
[i
]);
2225 proc_transwait(p
, 0);
2226 retval
= callout(p
, arg
);
2230 case PROC_RETURNED_DONE
:
2232 if (retval
== PROC_RETURNED_DONE
) {
2237 case PROC_CLAIMED_DONE
:
2247 kfree(pid_list
, alloc_count
);
2254 /* PGRP_BLOCKITERATE is not implemented yet */
2256 pgrp_iterate(pgrp
, flags
, callout
, arg
, filterfn
, filterarg
)
2259 int (*callout
)(proc_t
, void *);
2261 int (*filterfn
)(proc_t
, void *);
2266 int count
, pidcount
, i
, alloc_count
;
2269 int dropref
= flags
& PGRP_DROPREF
;
2271 int serialize
= flags
& PGRP_BLOCKITERATE
;
2278 count
= pgrp
->pg_membercnt
+ 10;
2279 if (count
> hard_maxproc
)
2280 count
= hard_maxproc
;
2281 alloc_count
= count
* sizeof(pid_t
);
2282 pid_list
= (pid_t
*)kalloc(alloc_count
);
2283 bzero(pid_list
, alloc_count
);
2286 if (serialize
!= 0) {
2287 while ((pgrp
->pg_listflags
& PGRP_FLAG_ITERABEGIN
) == PGRP_FLAG_ITERABEGIN
) {
2288 pgrp
->pg_listflags
|= PGRP_FLAG_ITERWAIT
;
2289 msleep(&pgrp
->pg_listflags
, &pgrp
->pg_mlock
, 0, "pgrp_iterate", 0);
2291 pgrp
->pg_listflags
|= PGRP_FLAG_ITERABEGIN
;
2297 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
2298 p
= p
->p_pglist
.le_next
) {
2299 if ( (filterfn
== 0 ) || (filterfn(p
, filterarg
) != 0)) {
2300 pid_list
[pidcount
] = p
->p_pid
;
2302 if (pidcount
>= count
)
2309 if ((serialize
== 0) && (dropref
!= 0))
2313 for (i
= 0; i
< pidcount
; i
++) {
2314 /* No handling or proc0 */
2315 if (pid_list
[i
] == 0)
2317 p
= proc_find(pid_list
[i
]);
2319 if (p
->p_pgrpid
!= pgid
) {
2323 proc_transwait(p
, 0);
2324 retval
= callout(p
, arg
);
2328 case PROC_RETURNED_DONE
:
2330 if (retval
== PROC_RETURNED_DONE
) {
2335 case PROC_CLAIMED_DONE
:
2344 if (serialize
!= 0) {
2346 pgrp
->pg_listflags
&= ~PGRP_FLAG_ITERABEGIN
;
2347 if ((pgrp
->pg_listflags
& PGRP_FLAG_ITERWAIT
) == PGRP_FLAG_ITERWAIT
) {
2348 pgrp
->pg_listflags
&= ~PGRP_FLAG_ITERWAIT
;
2349 wakeup(&pgrp
->pg_listflags
);
2355 kfree(pid_list
, alloc_count
);
2360 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2363 child
->p_pgrp
= pgrp
;
2364 child
->p_pgrpid
= pgrp
->pg_id
;
2365 child
->p_listflag
|= P_LIST_INPGRP
;
2367 * When pgrp is being freed , a process can still
2368 * request addition using setpgid from bash when
2369 * login is terminated (login cycler) return ESRCH
2370 * Safe to hold lock due to refcount on pgrp
2372 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2373 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2376 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2377 panic("pgrp_add : pgrp is dead adding process");
2381 pgrp
->pg_membercnt
++;
2382 if ( parent
!= PROC_NULL
) {
2383 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2385 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2390 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2391 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2397 pgrp_remove(struct proc
* p
)
2404 #if __PROC_INTERNAL_DEBUG
2405 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2406 panic("removing from pglist but no named ref\n");
2408 p
->p_pgrpid
= PGRPID_DEAD
;
2409 p
->p_listflag
&= ~P_LIST_INPGRP
;
2413 if (pg
== PGRP_NULL
)
2414 panic("pgrp_remove: pg is NULL");
2418 if (pg
->pg_membercnt
< 0)
2419 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",pg
, p
);
2421 LIST_REMOVE(p
, p_pglist
);
2422 if (pg
->pg_members
.lh_first
== 0) {
2424 pgdelete_dropref(pg
);
2432 /* cannot use proc_pgrp as it maybe stalled */
2434 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2436 struct pgrp
* oldpg
;
2442 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2443 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2444 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2447 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2450 if (oldpg
== PGRP_NULL
)
2451 panic("pgrp_replace: oldpg NULL");
2452 oldpg
->pg_refcount
++;
2453 #if __PROC_INTERNAL_DEBUG
2454 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2455 panic("removing from pglist but no named ref\n");
2457 p
->p_pgrpid
= PGRPID_DEAD
;
2458 p
->p_listflag
&= ~P_LIST_INPGRP
;
2464 oldpg
->pg_membercnt
--;
2465 if (oldpg
->pg_membercnt
< 0)
2466 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",oldpg
, p
);
2467 LIST_REMOVE(p
, p_pglist
);
2468 if (oldpg
->pg_members
.lh_first
== 0) {
2470 pgdelete_dropref(oldpg
);
2478 p
->p_pgrpid
= newpg
->pg_id
;
2479 p
->p_listflag
|= P_LIST_INPGRP
;
2481 * When pgrp is being freed , a process can still
2482 * request addition using setpgid from bash when
2483 * login is terminated (login cycler) return ESRCH
2484 * Safe to hold lock due to refcount on pgrp
2486 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2487 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2490 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2491 panic("pgrp_add : pgrp is dead adding process");
2495 newpg
->pg_membercnt
++;
2496 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
2500 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
2501 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2504 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
2505 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
2506 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
2507 wakeup(&p
->p_pgrpid
);
2514 pgrp_lock(struct pgrp
* pgrp
)
2516 lck_mtx_lock(&pgrp
->pg_mlock
);
2520 pgrp_unlock(struct pgrp
* pgrp
)
2522 lck_mtx_unlock(&pgrp
->pg_mlock
);
2526 session_lock(struct session
* sess
)
2528 lck_mtx_lock(&sess
->s_mlock
);
2533 session_unlock(struct session
* sess
)
2535 lck_mtx_unlock(&sess
->s_mlock
);
2547 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2548 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2549 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2554 assert(pgrp
!= NULL
);
2556 if (pgrp
!= PGRP_NULL
) {
2557 pgrp
->pg_refcount
++;
2558 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0)
2559 panic("proc_pgrp: ref being povided for dead pgrp");
2568 tty_pgrp(struct tty
* tp
)
2570 struct pgrp
* pg
= PGRP_NULL
;
2575 if (pg
!= PGRP_NULL
) {
2576 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0)
2577 panic("tty_pgrp: ref being povided for dead pgrp");
2586 proc_session(proc_t p
)
2588 struct session
* sess
= SESSION_NULL
;
2591 return(SESSION_NULL
);
2595 /* wait during transitions */
2596 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2597 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2598 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2601 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
2602 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2603 panic("proc_session:returning sesssion ref on terminating session");
2611 session_rele(struct session
*sess
)
2614 if (--sess
->s_count
== 0) {
2615 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2616 panic("session_rele: terminating already terminated session");
2617 sess
->s_listflags
|= S_LIST_TERM
;
2618 LIST_REMOVE(sess
, s_hash
);
2619 sess
->s_listflags
|= S_LIST_DEAD
;
2620 if (sess
->s_count
!= 0)
2621 panic("session_rele: freeing session in use");
2623 #if CONFIG_FINE_LOCK_GROUPS
2624 lck_mtx_destroy(&sess
->s_mlock
, proc_mlock_grp
);
2626 lck_mtx_destroy(&sess
->s_mlock
, proc_lck_grp
);
2628 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
2634 proc_transstart(proc_t p
, int locked
, int non_blocking
)
2638 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2639 if (((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
) || non_blocking
) {
2644 p
->p_lflag
|= P_LTRANSWAIT
;
2645 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2647 p
->p_lflag
|= P_LINTRANSIT
;
2648 p
->p_transholder
= current_thread();
2655 proc_transcommit(proc_t p
, int locked
)
2660 assert ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
);
2661 assert (p
->p_transholder
== current_thread());
2662 p
->p_lflag
|= P_LTRANSCOMMIT
;
2664 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2665 p
->p_lflag
&= ~P_LTRANSWAIT
;
2666 wakeup(&p
->p_lflag
);
2673 proc_transend(proc_t p
, int locked
)
2678 p
->p_lflag
&= ~( P_LINTRANSIT
| P_LTRANSCOMMIT
);
2679 p
->p_transholder
= NULL
;
2681 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2682 p
->p_lflag
&= ~P_LTRANSWAIT
;
2683 wakeup(&p
->p_lflag
);
2690 proc_transwait(proc_t p
, int locked
)
2694 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2695 if ((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
&& current_proc() == p
) {
2700 p
->p_lflag
|= P_LTRANSWAIT
;
2701 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2709 proc_klist_lock(void)
2711 lck_mtx_lock(proc_klist_mlock
);
2715 proc_klist_unlock(void)
2717 lck_mtx_unlock(proc_klist_mlock
);
2721 proc_knote(struct proc
* p
, long hint
)
2724 KNOTE(&p
->p_klist
, hint
);
2725 proc_klist_unlock();
2729 proc_knote_drain(struct proc
*p
)
2731 struct knote
*kn
= NULL
;
2734 * Clear the proc's klist to avoid references after the proc is reaped.
2737 while ((kn
= SLIST_FIRST(&p
->p_klist
))) {
2738 kn
->kn_ptr
.p_proc
= PROC_NULL
;
2739 KNOTE_DETACH(&p
->p_klist
, kn
);
2741 proc_klist_unlock();
2745 proc_setregister(proc_t p
)
2748 p
->p_lflag
|= P_LREGISTER
;
2753 proc_resetregister(proc_t p
)
2756 p
->p_lflag
&= ~P_LREGISTER
;
2761 proc_pgrpid(proc_t p
)
2769 return current_proc()->p_pgrpid
;
2773 /* return control and action states */
2775 proc_getpcontrol(int pid
, int * pcontrolp
)
2782 if (pcontrolp
!= NULL
)
2783 *pcontrolp
= p
->p_pcaction
;
2790 proc_dopcontrol(proc_t p
)
2796 pcontrol
= PROC_CONTROL_STATE(p
);
2798 if (PROC_ACTION_STATE(p
) == 0) {
2801 PROC_SETACTION_STATE(p
);
2803 printf("low swap: throttling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2807 PROC_SETACTION_STATE(p
);
2809 printf("low swap: suspending pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2810 task_suspend(p
->task
);
2814 PROC_SETACTION_STATE(p
);
2816 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2817 psignal(p
, SIGKILL
);
2827 return(PROC_RETURNED
);
2832 * Resume a throttled or suspended process. This is an internal interface that's only
2833 * used by the user level code that presents the GUI when we run out of swap space and
2834 * hence is restricted to processes with superuser privileges.
2838 proc_resetpcontrol(int pid
)
2843 proc_t self
= current_proc();
2845 /* if the process has been validated to handle resource control or root is valid one */
2846 if (((self
->p_lflag
& P_LVMRSRCOWNER
) == 0) && (error
= suser(kauth_cred_get(), 0)))
2855 pcontrol
= PROC_CONTROL_STATE(p
);
2857 if(PROC_ACTION_STATE(p
) !=0) {
2860 PROC_RESETACTION_STATE(p
);
2862 printf("low swap: unthrottling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2866 PROC_RESETACTION_STATE(p
);
2868 printf("low swap: resuming pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2869 task_resume(p
->task
);
2874 PROC_SETACTION_STATE(p
);
2876 printf("low swap: attempt to unkill pid %d (%s) ignored\n", p
->p_pid
, p
->p_comm
);
2892 struct no_paging_space
2894 uint64_t pcs_max_size
;
2895 uint64_t pcs_uniqueid
;
2898 uint64_t pcs_total_size
;
2900 uint64_t npcs_max_size
;
2901 uint64_t npcs_uniqueid
;
2903 int npcs_proc_count
;
2904 uint64_t npcs_total_size
;
2906 int apcs_proc_count
;
2907 uint64_t apcs_total_size
;
2912 proc_pcontrol_filter(proc_t p
, void *arg
)
2914 struct no_paging_space
*nps
;
2915 uint64_t compressed
;
2917 nps
= (struct no_paging_space
*)arg
;
2919 compressed
= get_task_compressed(p
->task
);
2921 if (PROC_CONTROL_STATE(p
)) {
2922 if (PROC_ACTION_STATE(p
) == 0) {
2923 if (compressed
> nps
->pcs_max_size
) {
2924 nps
->pcs_pid
= p
->p_pid
;
2925 nps
->pcs_uniqueid
= p
->p_uniqueid
;
2926 nps
->pcs_max_size
= compressed
;
2928 nps
->pcs_total_size
+= compressed
;
2929 nps
->pcs_proc_count
++;
2931 nps
->apcs_total_size
+= compressed
;
2932 nps
->apcs_proc_count
++;
2935 if (compressed
> nps
->npcs_max_size
) {
2936 nps
->npcs_pid
= p
->p_pid
;
2937 nps
->npcs_uniqueid
= p
->p_uniqueid
;
2938 nps
->npcs_max_size
= compressed
;
2940 nps
->npcs_total_size
+= compressed
;
2941 nps
->npcs_proc_count
++;
2949 proc_pcontrol_null(__unused proc_t p
, __unused
void *arg
)
2951 return(PROC_RETURNED
);
2956 * Deal with the low on compressor pool space condition... this function
2957 * gets called when we are approaching the limits of the compressor pool or
2958 * we are unable to create a new swap file.
2959 * Since this eventually creates a memory deadlock situtation, we need to take action to free up
2960 * memory resources (both compressed and uncompressed) in order to prevent the system from hanging completely.
2961 * There are 2 categories of processes to deal with. Those that have an action
2962 * associated with them by the task itself and those that do not. Actionable
2963 * tasks can have one of three categories specified: ones that
2964 * can be killed immediately, ones that should be suspended, and ones that should
2965 * be throttled. Processes that do not have an action associated with them are normally
2966 * ignored unless they are utilizing such a large percentage of the compressor pool (currently 50%)
2967 * that only by killing them can we hope to put the system back into a usable state.
2970 #define NO_PAGING_SPACE_DEBUG 0
2972 extern uint64_t vm_compressor_pages_compressed(void);
2974 struct timeval last_no_space_action
= {0, 0};
2977 no_paging_space_action()
2980 struct no_paging_space nps
;
2984 * Throttle how often we come through here. Once every 5 seconds should be plenty.
2988 if (now
.tv_sec
<= last_no_space_action
.tv_sec
+ 5)
2992 * Examine all processes and find the biggest (biggest is based on the number of pages this
2993 * task has in the compressor pool) that has been marked to have some action
2994 * taken when swap space runs out... we also find the biggest that hasn't been marked for
2997 * If the biggest non-actionable task is over the "dangerously big" threashold (currently 50% of
2998 * the total number of pages held by the compressor, we go ahead and kill it since no other task
2999 * can have any real effect on the situation. Otherwise, we go after the actionable process.
3001 bzero(&nps
, sizeof(nps
));
3003 proc_iterate(PROC_ALLPROCLIST
, proc_pcontrol_null
, (void *)NULL
, proc_pcontrol_filter
, (void *)&nps
);
3005 #if NO_PAGING_SPACE_DEBUG
3006 printf("low swap: npcs_proc_count = %d, npcs_total_size = %qd, npcs_max_size = %qd\n",
3007 nps
.npcs_proc_count
, nps
.npcs_total_size
, nps
.npcs_max_size
);
3008 printf("low swap: pcs_proc_count = %d, pcs_total_size = %qd, pcs_max_size = %qd\n",
3009 nps
.pcs_proc_count
, nps
.pcs_total_size
, nps
.pcs_max_size
);
3010 printf("low swap: apcs_proc_count = %d, apcs_total_size = %qd\n",
3011 nps
.apcs_proc_count
, nps
.apcs_total_size
);
3013 if (nps
.npcs_max_size
> (vm_compressor_pages_compressed() * 50) / 100) {
3015 * for now we'll knock out any task that has more then 50% of the pages
3016 * held by the compressor
3018 if ((p
= proc_find(nps
.npcs_pid
)) != PROC_NULL
) {
3020 if (nps
.npcs_uniqueid
== p
->p_uniqueid
) {
3022 * verify this is still the same process
3023 * in case the proc exited and the pid got reused while
3024 * we were finishing the proc_iterate and getting to this point
3026 last_no_space_action
= now
;
3028 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3029 psignal(p
, SIGKILL
);
3040 if (nps
.pcs_max_size
> 0) {
3041 if ((p
= proc_find(nps
.pcs_pid
)) != PROC_NULL
) {
3043 if (nps
.pcs_uniqueid
== p
->p_uniqueid
) {
3045 * verify this is still the same process
3046 * in case the proc exited and the pid got reused while
3047 * we were finishing the proc_iterate and getting to this point
3049 last_no_space_action
= now
;
3061 last_no_space_action
= now
;
3063 printf("low swap: unable to find any eligible processes to take action on\n");
3069 proc_trace_log(__unused proc_t p
, struct proc_trace_log_args
*uap
, __unused
int *retval
)
3072 proc_t target_proc
= PROC_NULL
;
3073 pid_t target_pid
= uap
->pid
;
3074 uint64_t target_uniqueid
= uap
->uniqueid
;
3075 task_t target_task
= NULL
;
3077 if (priv_check_cred(kauth_cred_get(), PRIV_PROC_TRACE_INSPECT
, 0)) {
3081 target_proc
= proc_find(target_pid
);
3082 if (target_proc
!= PROC_NULL
) {
3083 if (target_uniqueid
!= proc_uniqueid(target_proc
)) {
3088 target_task
= proc_task(target_proc
);
3089 if (task_send_trace_memory(target_task
, target_pid
, target_uniqueid
)) {
3097 if (target_proc
!= PROC_NULL
)
3098 proc_rele(target_proc
);
3102 #if VM_SCAN_FOR_SHADOW_CHAIN
3103 extern int vm_map_shadow_max(vm_map_t map
);
3104 int proc_shadow_max(void);
3105 int proc_shadow_max(void)
3114 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
3115 if (p
->p_stat
== SIDL
)
3121 map
= get_task_map(task
);
3125 retval
= vm_map_shadow_max(map
);
3133 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */
3135 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
);
3136 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
)
3138 if (target_proc
!= NULL
) {
3139 target_proc
->p_responsible_pid
= responsible_pid
;
3145 proc_chrooted(proc_t p
)
3151 retval
= (p
->p_fd
->fd_rdir
!= NULL
) ? 1 : 0;