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>
113 #include <sys/persona.h>
119 #if CONFIG_MEMORYSTATUS
120 #include <sys/kern_memorystatus.h>
124 #include <security/mac_framework.h>
127 #include <libkern/crypto/sha1.h>
130 * Structure associated with user cacheing.
133 LIST_ENTRY(uidinfo
) ui_hash
;
137 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
138 LIST_HEAD(uihashhead
, uidinfo
) *uihashtbl
;
139 u_long uihash
; /* size of hash table - 1 */
142 * Other process lists
144 struct pidhashhead
*pidhashtbl
;
146 struct pgrphashhead
*pgrphashtbl
;
148 struct sesshashhead
*sesshashtbl
;
151 struct proclist allproc
;
152 struct proclist zombproc
;
153 extern struct tty cons
;
157 #if DEVELOPMENT || DEBUG
158 extern int cs_enforcement_enable
;
162 #define __PROC_INTERNAL_DEBUG 1
165 /* Name to give to core files */
166 #if defined(XNU_TARGET_OS_BRIDGE)
167 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/private/var/internal/%N.core"};
168 #elif CONFIG_EMBEDDED
169 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/private/var/cores/%N.core"};
171 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/cores/core.%P"};
176 #include <kern/backtrace.h>
179 static void orphanpg(struct pgrp
* pg
);
180 void proc_name_kdp(task_t t
, char * buf
, int size
);
181 void * proc_get_uthread_uu_threadlist(void * uthread_v
);
182 int proc_threadname_kdp(void * uth
, char * buf
, size_t size
);
183 void proc_starttime_kdp(void * p
, uint64_t * tv_sec
, uint64_t * tv_usec
, uint64_t * abstime
);
184 char * proc_name_address(void * p
);
186 /* TODO: make a header that's exported and usable in osfmk */
187 char* proc_best_name(proc_t p
);
189 static void pgrp_add(struct pgrp
* pgrp
, proc_t parent
, proc_t child
);
190 static void pgrp_remove(proc_t p
);
191 static void pgrp_replace(proc_t p
, struct pgrp
*pgrp
);
192 static void pgdelete_dropref(struct pgrp
*pgrp
);
193 extern void pg_rele_dropref(struct pgrp
* pgrp
);
194 static int csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaddittoken
);
195 static boolean_t
proc_parent_is_currentproc(proc_t p
);
197 struct fixjob_iterargs
{
199 struct session
* mysession
;
203 int fixjob_callback(proc_t
, void *);
205 uint64_t get_current_unique_pid(void);
209 get_current_unique_pid(void)
211 proc_t p
= current_proc();
214 return p
->p_uniqueid
;
220 * Initialize global process hashing structures.
226 LIST_INIT(&zombproc
);
227 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
228 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
229 sesshashtbl
= hashinit(maxproc
/ 4, M_PROC
, &sesshash
);
230 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
232 personas_bootstrap();
237 * Change the count associated with number of processes
238 * a given user is using. This routine protects the uihash
242 chgproccnt(uid_t uid
, int diff
)
245 struct uidinfo
*newuip
= NULL
;
246 struct uihashhead
*uipp
;
252 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
)
253 if (uip
->ui_uid
== uid
)
256 uip
->ui_proccnt
+= diff
;
257 if (uip
->ui_proccnt
> 0) {
258 retval
= uip
->ui_proccnt
;
262 if (uip
->ui_proccnt
< 0)
263 panic("chgproccnt: procs < 0");
264 LIST_REMOVE(uip
, ui_hash
);
267 FREE_ZONE(uip
, sizeof(*uip
), M_PROC
);
276 panic("chgproccnt: lost user");
278 if (newuip
!= NULL
) {
281 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
283 uip
->ui_proccnt
= diff
;
289 MALLOC_ZONE(newuip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
291 panic("chgproccnt: M_PROC zone depleted");
295 FREE_ZONE(newuip
, sizeof(*uip
), M_PROC
);
300 * Is p an inferior of the current process?
308 for (; p
!= current_proc(); p
= p
->p_pptr
)
318 * Is p an inferior of t ?
321 isinferior(proc_t p
, proc_t t
)
327 /* if p==t they are not inferior */
332 for (; p
!= t
; p
= p
->p_pptr
) {
335 /* Detect here if we're in a cycle */
336 if ((p
->p_pid
== 0) || (p
->p_pptr
== start
) || (nchecked
>= nprocs
))
346 proc_isinferior(int pid1
, int pid2
)
348 proc_t p
= PROC_NULL
;
349 proc_t t
= PROC_NULL
;
352 if (((p
= proc_find(pid1
)) != (proc_t
)0 ) && ((t
= proc_find(pid2
)) != (proc_t
)0))
353 retval
= isinferior(p
, t
);
366 return(proc_findinternal(pid
, 0));
370 proc_findinternal(int pid
, int locked
)
372 proc_t p
= PROC_NULL
;
378 p
= pfind_locked(pid
);
379 if ((p
== PROC_NULL
) || (p
!= proc_ref_locked(p
)))
390 proc_findthread(thread_t thread
)
392 proc_t p
= PROC_NULL
;
396 uth
= get_bsdthread_info(thread
);
397 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
400 p
= (proc_t
)(get_bsdthreadtask_info(thread
));
401 p
= proc_ref_locked(p
);
407 uthread_reset_proc_refcount(void *uthread
) {
410 uth
= (uthread_t
) uthread
;
411 uth
->uu_proc_refcount
= 0;
414 if (proc_ref_tracking_disabled
) {
424 uthread_get_proc_refcount(void *uthread
) {
427 if (proc_ref_tracking_disabled
) {
431 uth
= (uthread_t
) uthread
;
433 return uth
->uu_proc_refcount
;
438 record_procref(proc_t p __unused
, int count
) {
441 uth
= current_uthread();
442 uth
->uu_proc_refcount
+= count
;
445 if (proc_ref_tracking_disabled
) {
450 if (uth
->uu_pindex
< NUM_PROC_REFS_TO_TRACK
) {
451 backtrace((uintptr_t *) &uth
->uu_proc_pcs
[uth
->uu_pindex
], PROC_REF_STACK_DEPTH
);
453 uth
->uu_proc_ps
[uth
->uu_pindex
] = p
;
461 uthread_needs_to_wait_in_proc_refwait(void) {
462 uthread_t uth
= current_uthread();
465 * Allow threads holding no proc refs to wait
466 * in proc_refwait, allowing threads holding
467 * proc refs to wait in proc_refwait causes
468 * deadlocks and makes proc_find non-reentrant.
470 if (uth
->uu_proc_refcount
== 0)
494 if (p
!= proc_ref_locked(p
))
502 proc_ref_locked(proc_t p
)
505 int pid
= proc_pid(p
);
509 * if process still in creation or proc got recycled
510 * during msleep then return failure.
512 if ((p
== PROC_NULL
) || (p1
!= p
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0))
516 * Do not return process marked for termination
517 * or proc_refdrain called without ref wait.
518 * Wait for proc_refdrain_with_refwait to complete if
519 * process in refdrain and refwait flag is set, unless
520 * the current thread is holding to a proc_ref
523 if ((p
->p_stat
!= SZOMB
) &&
524 ((p
->p_listflag
& P_LIST_EXITED
) == 0) &&
525 ((p
->p_listflag
& P_LIST_DEAD
) == 0) &&
526 (((p
->p_listflag
& (P_LIST_DRAIN
| P_LIST_DRAINWAIT
)) == 0) ||
527 ((p
->p_listflag
& P_LIST_REFWAIT
) != 0))) {
528 if ((p
->p_listflag
& P_LIST_REFWAIT
) != 0 && uthread_needs_to_wait_in_proc_refwait()) {
529 msleep(&p
->p_listflag
, proc_list_mlock
, 0, "proc_refwait", 0) ;
531 * the proc might have been recycled since we dropped
532 * the proc list lock, get the proc again.
534 p
= pfind_locked(pid
);
538 record_procref(p
, 1);
547 proc_rele_locked(proc_t p
)
550 if (p
->p_refcount
> 0) {
552 record_procref(p
, -1);
553 if ((p
->p_refcount
== 0) && ((p
->p_listflag
& P_LIST_DRAINWAIT
) == P_LIST_DRAINWAIT
)) {
554 p
->p_listflag
&= ~P_LIST_DRAINWAIT
;
555 wakeup(&p
->p_refcount
);
558 panic("proc_rele_locked -ve ref\n");
563 proc_find_zombref(int pid
)
570 p
= pfind_locked(pid
);
572 /* should we bail? */
573 if ((p
== PROC_NULL
) /* not found */
574 || ((p
->p_listflag
& P_LIST_INCREATE
) != 0) /* not created yet */
575 || ((p
->p_listflag
& P_LIST_EXITED
) == 0)) { /* not started exit */
581 /* If someone else is controlling the (unreaped) zombie - wait */
582 if ((p
->p_listflag
& P_LIST_WAITING
) != 0) {
583 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
586 p
->p_listflag
|= P_LIST_WAITING
;
594 proc_drop_zombref(proc_t p
)
597 if ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
598 p
->p_listflag
&= ~P_LIST_WAITING
;
606 proc_refdrain(proc_t p
)
608 proc_refdrain_with_refwait(p
, FALSE
);
612 proc_refdrain_with_refwait(proc_t p
, boolean_t get_ref_and_allow_wait
)
614 boolean_t initexec
= FALSE
;
617 p
->p_listflag
|= P_LIST_DRAIN
;
618 if (get_ref_and_allow_wait
) {
620 * All the calls to proc_ref_locked will wait
621 * for the flag to get cleared before returning a ref,
622 * unless the current thread is holding to a proc ref
625 p
->p_listflag
|= P_LIST_REFWAIT
;
631 /* Do not wait in ref drain for launchd exec */
632 while (p
->p_refcount
&& !initexec
) {
633 p
->p_listflag
|= P_LIST_DRAINWAIT
;
634 msleep(&p
->p_refcount
, proc_list_mlock
, 0, "proc_refdrain", 0) ;
637 p
->p_listflag
&= ~P_LIST_DRAIN
;
638 if (!get_ref_and_allow_wait
) {
639 p
->p_listflag
|= P_LIST_DEAD
;
641 /* Return a ref to the caller */
643 record_procref(p
, 1);
648 if (get_ref_and_allow_wait
) {
655 proc_refwake(proc_t p
)
658 p
->p_listflag
&= ~P_LIST_REFWAIT
;
659 wakeup(&p
->p_listflag
);
664 proc_parentholdref(proc_t p
)
666 proc_t parent
= PROC_NULL
;
674 if ((pp
== PROC_NULL
) || (pp
->p_stat
== SZOMB
) || ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
))) {
679 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == P_LIST_CHILDDRSTART
) {
680 pp
->p_listflag
|= P_LIST_CHILDDRWAIT
;
681 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
690 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == 0) {
701 proc_parentdropref(proc_t p
, int listlocked
)
706 if (p
->p_parentref
> 0) {
708 if ((p
->p_parentref
== 0) && ((p
->p_listflag
& P_LIST_PARENTREFWAIT
) == P_LIST_PARENTREFWAIT
)) {
709 p
->p_listflag
&= ~P_LIST_PARENTREFWAIT
;
710 wakeup(&p
->p_parentref
);
713 panic("proc_parentdropref -ve ref\n");
721 proc_childdrainstart(proc_t p
)
723 #if __PROC_INTERNAL_DEBUG
724 if ((p
->p_listflag
& P_LIST_CHILDDRSTART
) == P_LIST_CHILDDRSTART
)
725 panic("proc_childdrainstart: childdrain already started\n");
727 p
->p_listflag
|= P_LIST_CHILDDRSTART
;
728 /* wait for all that hold parentrefs to drop */
729 while (p
->p_parentref
> 0) {
730 p
->p_listflag
|= P_LIST_PARENTREFWAIT
;
731 msleep(&p
->p_parentref
, proc_list_mlock
, 0, "proc_childdrainstart", 0) ;
737 proc_childdrainend(proc_t p
)
739 #if __PROC_INTERNAL_DEBUG
740 if (p
->p_childrencnt
> 0)
741 panic("exiting: children stil hanging around\n");
743 p
->p_listflag
|= P_LIST_CHILDDRAINED
;
744 if ((p
->p_listflag
& (P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
)) != 0) {
745 p
->p_listflag
&= ~(P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
);
746 wakeup(&p
->p_childrencnt
);
751 proc_checkdeadrefs(__unused proc_t p
)
753 #if __PROC_INTERNAL_DEBUG
754 if ((p
->p_listflag
& P_LIST_INHASH
) != 0)
755 panic("proc being freed and still in hash %p: %u\n", p
, p
->p_listflag
);
756 if (p
->p_childrencnt
!= 0)
757 panic("proc being freed and pending children cnt %p:%d\n", p
, p
->p_childrencnt
);
758 if (p
->p_refcount
!= 0)
759 panic("proc being freed and pending refcount %p:%d\n", p
, p
->p_refcount
);
760 if (p
->p_parentref
!= 0)
761 panic("proc being freed and pending parentrefs %p:%d\n", p
, p
->p_parentref
);
784 return (current_proc()->p_pid
);
790 return (current_proc()->p_ppid
);
794 proc_selfcsflags(void)
796 return (current_proc()->p_csflags
);
801 dtrace_current_proc_vforking(void)
803 thread_t th
= current_thread();
804 struct uthread
*ut
= get_bsdthread_info(th
);
807 ((ut
->uu_flag
& (UT_VFORK
|UT_VFORKING
)) == (UT_VFORK
|UT_VFORKING
))) {
809 * Handle the narrow window where we're in the vfork syscall,
810 * but we're not quite ready to claim (in particular, to DTrace)
811 * that we're running as the child.
813 return (get_bsdtask_info(get_threadtask(th
)));
815 return (current_proc());
819 dtrace_proc_selfpid(void)
821 return (dtrace_current_proc_vforking()->p_pid
);
825 dtrace_proc_selfppid(void)
827 return (dtrace_current_proc_vforking()->p_ppid
);
831 dtrace_proc_selfruid(void)
833 return (dtrace_current_proc_vforking()->p_ruid
);
835 #endif /* CONFIG_DTRACE */
838 proc_parent(proc_t p
)
846 parent
= proc_ref_locked(pp
);
847 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)){
848 pp
->p_listflag
|= P_LIST_CHILDLKWAIT
;
849 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
857 proc_parent_is_currentproc(proc_t p
)
859 boolean_t ret
= FALSE
;
862 if (p
->p_pptr
== current_proc())
870 proc_name(int pid
, char * buf
, int size
)
874 if ((p
= proc_find(pid
)) != PROC_NULL
) {
875 strlcpy(buf
, &p
->p_comm
[0], size
);
881 proc_name_kdp(task_t t
, char * buf
, int size
)
883 proc_t p
= get_bsdtask_info(t
);
887 if ((size_t)size
> sizeof(p
->p_comm
))
888 strlcpy(buf
, &p
->p_name
[0], MIN((int)sizeof(p
->p_name
), size
));
890 strlcpy(buf
, &p
->p_comm
[0], MIN((int)sizeof(p
->p_comm
), size
));
894 proc_threadname_kdp(void * uth
, char * buf
, size_t size
)
896 if (size
< MAXTHREADNAMESIZE
) {
897 /* this is really just a protective measure for the future in
898 * case the thread name size in stackshot gets out of sync with
899 * the BSD max thread name size. Note that bsd_getthreadname
900 * doesn't take input buffer size into account. */
905 bsd_getthreadname(uth
, buf
);
910 /* note that this function is generally going to be called from stackshot,
911 * and the arguments will be coming from a struct which is declared packed
912 * thus the input arguments will in general be unaligned. We have to handle
915 proc_starttime_kdp(void *p
, uint64_t *tv_sec
, uint64_t *tv_usec
, uint64_t *abstime
)
917 proc_t pp
= (proc_t
)p
;
920 } __attribute__((packed
));
922 if (pp
!= PROC_NULL
) {
924 ((struct uint64p
*)tv_sec
)->val
= pp
->p_start
.tv_sec
;
926 ((struct uint64p
*)tv_usec
)->val
= pp
->p_start
.tv_usec
;
927 if (abstime
!= NULL
) {
928 if (pp
->p_stats
!= NULL
)
929 *abstime
= pp
->p_stats
->ps_start
;
937 proc_name_address(void *p
)
939 return &((proc_t
)p
)->p_comm
[0];
943 proc_best_name(proc_t p
)
945 if (p
->p_name
[0] != 0)
946 return (&p
->p_name
[0]);
947 return (&p
->p_comm
[0]);
951 proc_selfname(char * buf
, int size
)
955 if ((p
= current_proc())!= (proc_t
)0) {
956 strlcpy(buf
, &p
->p_comm
[0], size
);
961 proc_signal(int pid
, int signum
)
965 if ((p
= proc_find(pid
)) != PROC_NULL
) {
972 proc_issignal(int pid
, sigset_t mask
)
977 if ((p
= proc_find(pid
)) != PROC_NULL
) {
978 error
= proc_pendingsignals(p
, mask
);
986 proc_noremotehang(proc_t p
)
991 retval
= p
->p_flag
& P_NOREMOTEHANG
;
992 return(retval
? 1: 0);
997 proc_exiting(proc_t p
)
1002 retval
= p
->p_lflag
& P_LEXIT
;
1003 return(retval
? 1: 0);
1007 proc_forcequota(proc_t p
)
1012 retval
= p
->p_flag
& P_FORCEQUOTA
;
1013 return(retval
? 1: 0);
1018 proc_suser(proc_t p
)
1020 kauth_cred_t my_cred
;
1023 my_cred
= kauth_cred_proc_ref(p
);
1024 error
= suser(my_cred
, &p
->p_acflag
);
1025 kauth_cred_unref(&my_cred
);
1030 proc_task(proc_t proc
)
1032 return (task_t
)proc
->task
;
1036 * Obtain the first thread in a process
1038 * XXX This is a bad thing to do; it exists predominantly to support the
1039 * XXX use of proc_t's in places that should really be using
1040 * XXX thread_t's instead. This maintains historical behaviour, but really
1041 * XXX needs an audit of the context (proxy vs. not) to clean up.
1044 proc_thread(proc_t proc
)
1046 uthread_t uth
= TAILQ_FIRST(&proc
->p_uthlist
);
1049 return(uth
->uu_context
.vc_thread
);
1055 proc_ucred(proc_t p
)
1063 thread_t th
= current_thread();
1065 return((struct uthread
*)get_bsdthread_info(th
));
1070 proc_is64bit(proc_t p
)
1072 return(IS_64BIT_PROCESS(p
));
1076 proc_pidversion(proc_t p
)
1078 return(p
->p_idversion
);
1082 proc_persona_id(proc_t p
)
1084 return (uint32_t)persona_id_from_proc(p
);
1088 proc_getuid(proc_t p
)
1094 proc_getgid(proc_t p
)
1100 proc_uniqueid(proc_t p
)
1102 return(p
->p_uniqueid
);
1106 proc_puniqueid(proc_t p
)
1108 return(p
->p_puniqueid
);
1112 proc_coalitionids(__unused proc_t p
, __unused
uint64_t ids
[COALITION_NUM_TYPES
])
1114 #if CONFIG_COALITIONS
1115 task_coalition_ids(p
->task
, ids
);
1117 memset(ids
, 0, sizeof(uint64_t [COALITION_NUM_TYPES
]));
1123 proc_was_throttled(proc_t p
)
1125 return (p
->was_throttled
);
1129 proc_did_throttle(proc_t p
)
1131 return (p
->did_throttle
);
1135 proc_getcdhash(proc_t p
, unsigned char *cdhash
)
1137 return vn_getcdhash(p
->p_textvp
, p
->p_textoff
, cdhash
);
1141 proc_getexecutableuuid(proc_t p
, unsigned char *uuidbuf
, unsigned long size
)
1143 if (size
>= sizeof(p
->p_uuid
)) {
1144 memcpy(uuidbuf
, p
->p_uuid
, sizeof(p
->p_uuid
));
1148 /* Return vnode for executable with an iocount. Must be released with vnode_put() */
1150 proc_getexecutablevnode(proc_t p
)
1152 vnode_t tvp
= p
->p_textvp
;
1154 if ( tvp
!= NULLVP
) {
1155 if (vnode_getwithref(tvp
) == 0) {
1165 bsd_set_dependency_capable(task_t task
)
1167 proc_t p
= get_bsdtask_info(task
);
1170 OSBitOrAtomic(P_DEPENDENCY_CAPABLE
, &p
->p_flag
);
1177 IS_64BIT_PROCESS(proc_t p
)
1179 if (p
&& (p
->p_flag
& P_LP64
))
1187 * Locate a process by number
1190 pfind_locked(pid_t pid
)
1200 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
) {
1201 if (p
->p_pid
== pid
) {
1203 for (q
= p
->p_hash
.le_next
; q
!= 0; q
= q
->p_hash
.le_next
) {
1204 if ((p
!=q
) && (q
->p_pid
== pid
))
1205 panic("two procs with same pid %p:%p:%d:%d\n", p
, q
, p
->p_pid
, q
->p_pid
);
1215 * Locate a zombie by PID
1217 __private_extern__ proc_t
1225 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
1226 if (p
->p_pid
== pid
)
1235 * Locate a process group by number
1244 pgrp
= pgfind_internal(pgid
);
1245 if ((pgrp
== NULL
) || ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) != 0))
1248 pgrp
->pg_refcount
++;
1256 pgfind_internal(pid_t pgid
)
1260 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
)
1261 if (pgrp
->pg_id
== pgid
)
1267 pg_rele(struct pgrp
* pgrp
)
1269 if(pgrp
== PGRP_NULL
)
1271 pg_rele_dropref(pgrp
);
1275 pg_rele_dropref(struct pgrp
* pgrp
)
1278 if ((pgrp
->pg_refcount
== 1) && ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) == PGRP_FLAG_TERMINATE
)) {
1280 pgdelete_dropref(pgrp
);
1284 pgrp
->pg_refcount
--;
1289 session_find_internal(pid_t sessid
)
1291 struct session
*sess
;
1293 for (sess
= SESSHASH(sessid
)->lh_first
; sess
!= 0; sess
= sess
->s_hash
.le_next
)
1294 if (sess
->s_sid
== sessid
)
1301 * Make a new process ready to become a useful member of society by making it
1302 * visible in all the right places and initialize its own lists to empty.
1304 * Parameters: parent The parent of the process to insert
1305 * child The child process to insert
1309 * Notes: Insert a child process into the parents process group, assign
1310 * the child the parent process pointer and PPID of the parent,
1311 * place it on the parents p_children list as a sibling,
1312 * initialize its own child list, place it in the allproc list,
1313 * insert it in the proper hash bucket, and initialize its
1317 pinsertchild(proc_t parent
, proc_t child
)
1321 LIST_INIT(&child
->p_children
);
1322 TAILQ_INIT(&child
->p_evlist
);
1323 child
->p_pptr
= parent
;
1324 child
->p_ppid
= parent
->p_pid
;
1325 child
->p_puniqueid
= parent
->p_uniqueid
;
1326 child
->p_xhighbits
= 0;
1328 pg
= proc_pgrp(parent
);
1329 pgrp_add(pg
, parent
, child
);
1334 #if CONFIG_MEMORYSTATUS
1335 memorystatus_add(child
, TRUE
);
1338 parent
->p_childrencnt
++;
1339 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1341 LIST_INSERT_HEAD(&allproc
, child
, p_list
);
1342 /* mark the completion of proc creation */
1343 child
->p_listflag
&= ~P_LIST_INCREATE
;
1349 * Move p to a new or existing process group (and session)
1351 * Returns: 0 Success
1352 * ESRCH No such process
1355 enterpgrp(proc_t p
, pid_t pgid
, int mksess
)
1358 struct pgrp
*mypgrp
;
1359 struct session
* procsp
;
1361 pgrp
= pgfind(pgid
);
1362 mypgrp
= proc_pgrp(p
);
1363 procsp
= proc_session(p
);
1366 if (pgrp
!= NULL
&& mksess
) /* firewalls */
1367 panic("enterpgrp: setsid into non-empty pgrp");
1368 if (SESS_LEADER(p
, procsp
))
1369 panic("enterpgrp: session leader attempted setpgrp");
1371 if (pgrp
== PGRP_NULL
) {
1372 pid_t savepid
= p
->p_pid
;
1373 proc_t np
= PROC_NULL
;
1378 if (p
->p_pid
!= pgid
)
1379 panic("enterpgrp: new pgrp and pid != pgid");
1381 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
1384 panic("enterpgrp: M_PGRP zone depleted");
1385 if ((np
= proc_find(savepid
)) == NULL
|| np
!= p
) {
1386 if (np
!= PROC_NULL
)
1388 if (mypgrp
!= PGRP_NULL
)
1390 if (procsp
!= SESSION_NULL
)
1391 session_rele(procsp
);
1392 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
1397 struct session
*sess
;
1402 MALLOC_ZONE(sess
, struct session
*,
1403 sizeof(struct session
), M_SESSION
, M_WAITOK
);
1405 panic("enterpgrp: M_SESSION zone depleted");
1407 sess
->s_sid
= p
->p_pid
;
1409 sess
->s_ttyvp
= NULL
;
1410 sess
->s_ttyp
= TTY_NULL
;
1412 sess
->s_listflags
= 0;
1413 sess
->s_ttypgrpid
= NO_PID
;
1414 #if CONFIG_FINE_LOCK_GROUPS
1415 lck_mtx_init(&sess
->s_mlock
, proc_mlock_grp
, proc_lck_attr
);
1417 lck_mtx_init(&sess
->s_mlock
, proc_lck_grp
, proc_lck_attr
);
1419 bcopy(procsp
->s_login
, sess
->s_login
,
1420 sizeof(sess
->s_login
));
1421 OSBitAndAtomic(~((uint32_t)P_CONTROLT
), &p
->p_flag
);
1423 LIST_INSERT_HEAD(SESSHASH(sess
->s_sid
), sess
, s_hash
);
1425 pgrp
->pg_session
= sess
;
1427 if (p
!= current_proc())
1428 panic("enterpgrp: mksession and p != curproc");
1432 pgrp
->pg_session
= procsp
;
1434 if ((pgrp
->pg_session
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1435 panic("enterpgrp: providing ref to terminating session ");
1436 pgrp
->pg_session
->s_count
++;
1440 #if CONFIG_FINE_LOCK_GROUPS
1441 lck_mtx_init(&pgrp
->pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
1443 lck_mtx_init(&pgrp
->pg_mlock
, proc_lck_grp
, proc_lck_attr
);
1445 LIST_INIT(&pgrp
->pg_members
);
1446 pgrp
->pg_membercnt
= 0;
1449 pgrp
->pg_refcount
= 1;
1450 pgrp
->pg_listflags
= 0;
1451 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
1453 } else if (pgrp
== mypgrp
) {
1457 if (procsp
!= SESSION_NULL
)
1458 session_rele(procsp
);
1462 if (procsp
!= SESSION_NULL
)
1463 session_rele(procsp
);
1465 * Adjust eligibility of affected pgrps to participate in job control.
1466 * Increment eligibility counts before decrementing, otherwise we
1467 * could reach 0 spuriously during the first call.
1469 fixjobc(p
, pgrp
, 1);
1470 fixjobc(p
, mypgrp
, 0);
1472 if(mypgrp
!= PGRP_NULL
)
1474 pgrp_replace(p
, pgrp
);
1481 * remove process from process group
1492 * delete a process group
1495 pgdelete_dropref(struct pgrp
*pgrp
)
1499 struct session
*sessp
;
1503 if (pgrp
->pg_membercnt
!= 0) {
1509 pgrp
->pg_refcount
--;
1510 if ((emptypgrp
== 0) || (pgrp
->pg_membercnt
!= 0)) {
1515 pgrp
->pg_listflags
|= PGRP_FLAG_TERMINATE
;
1517 if (pgrp
->pg_refcount
> 0) {
1522 pgrp
->pg_listflags
|= PGRP_FLAG_DEAD
;
1523 LIST_REMOVE(pgrp
, pg_hash
);
1527 ttyp
= SESSION_TP(pgrp
->pg_session
);
1528 if (ttyp
!= TTY_NULL
) {
1529 if (ttyp
->t_pgrp
== pgrp
) {
1531 /* Re-check after acquiring the lock */
1532 if (ttyp
->t_pgrp
== pgrp
) {
1533 ttyp
->t_pgrp
= NULL
;
1534 pgrp
->pg_session
->s_ttypgrpid
= NO_PID
;
1542 sessp
= pgrp
->pg_session
;
1543 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1544 panic("pg_deleteref: manipulating refs of already terminating session");
1545 if (--sessp
->s_count
== 0) {
1546 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1547 panic("pg_deleteref: terminating already terminated session");
1548 sessp
->s_listflags
|= S_LIST_TERM
;
1549 ttyp
= SESSION_TP(sessp
);
1550 LIST_REMOVE(sessp
, s_hash
);
1552 if (ttyp
!= TTY_NULL
) {
1554 if (ttyp
->t_session
== sessp
)
1555 ttyp
->t_session
= NULL
;
1559 sessp
->s_listflags
|= S_LIST_DEAD
;
1560 if (sessp
->s_count
!= 0)
1561 panic("pg_deleteref: freeing session in use");
1563 #if CONFIG_FINE_LOCK_GROUPS
1564 lck_mtx_destroy(&sessp
->s_mlock
, proc_mlock_grp
);
1566 lck_mtx_destroy(&sessp
->s_mlock
, proc_lck_grp
);
1568 FREE_ZONE(sessp
, sizeof(struct session
), M_SESSION
);
1571 #if CONFIG_FINE_LOCK_GROUPS
1572 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_mlock_grp
);
1574 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_lck_grp
);
1576 FREE_ZONE(pgrp
, sizeof(*pgrp
), M_PGRP
);
1581 * Adjust pgrp jobc counters when specified process changes process group.
1582 * We count the number of processes in each process group that "qualify"
1583 * the group for terminal job control (those with a parent in a different
1584 * process group of the same session). If that count reaches zero, the
1585 * process group becomes orphaned. Check both the specified process'
1586 * process group and that of its children.
1587 * entering == 0 => p is leaving specified group.
1588 * entering == 1 => p is entering specified group.
1591 fixjob_callback(proc_t p
, void * arg
)
1593 struct fixjob_iterargs
*fp
;
1594 struct pgrp
* pg
, *hispg
;
1595 struct session
* mysession
, *hissess
;
1598 fp
= (struct fixjob_iterargs
*)arg
;
1600 mysession
= fp
->mysession
;
1601 entering
= fp
->entering
;
1603 hispg
= proc_pgrp(p
);
1604 hissess
= proc_session(p
);
1606 if ((hispg
!= pg
) &&
1607 (hissess
== mysession
)) {
1612 } else if (--hispg
->pg_jobc
== 0) {
1618 if (hissess
!= SESSION_NULL
)
1619 session_rele(hissess
);
1620 if (hispg
!= PGRP_NULL
)
1623 return(PROC_RETURNED
);
1627 fixjobc(proc_t p
, struct pgrp
*pgrp
, int entering
)
1629 struct pgrp
*hispgrp
= PGRP_NULL
;
1630 struct session
*hissess
= SESSION_NULL
;
1631 struct session
*mysession
= pgrp
->pg_session
;
1633 struct fixjob_iterargs fjarg
;
1634 boolean_t proc_parent_self
;
1637 * Check if p's parent is current proc, if yes then no need to take
1638 * a ref; calling proc_parent with current proc as parent may
1639 * deadlock if current proc is exiting.
1641 proc_parent_self
= proc_parent_is_currentproc(p
);
1642 if (proc_parent_self
)
1643 parent
= current_proc();
1645 parent
= proc_parent(p
);
1647 if (parent
!= PROC_NULL
) {
1648 hispgrp
= proc_pgrp(parent
);
1649 hissess
= proc_session(parent
);
1650 if (!proc_parent_self
)
1656 * Check p's parent to see whether p qualifies its own process
1657 * group; if so, adjust count for p's process group.
1659 if ((hispgrp
!= pgrp
) &&
1660 (hissess
== mysession
)) {
1665 }else if (--pgrp
->pg_jobc
== 0) {
1672 if (hissess
!= SESSION_NULL
)
1673 session_rele(hissess
);
1674 if (hispgrp
!= PGRP_NULL
)
1678 * Check this process' children to see whether they qualify
1679 * their process groups; if so, adjust counts for children's
1683 fjarg
.mysession
= mysession
;
1684 fjarg
.entering
= entering
;
1685 proc_childrenwalk(p
, fixjob_callback
, &fjarg
);
1689 * A process group has become orphaned; if there are any stopped processes in
1690 * the group, hang-up all process in that group.
1693 orphanpg(struct pgrp
*pgrp
)
1697 vm_size_t pid_list_size
= 0;
1698 vm_size_t pid_list_size_needed
= 0;
1700 int pid_count_available
= 0;
1702 assert(pgrp
!= NULL
);
1704 /* allocate outside of the pgrp_lock */
1708 boolean_t should_iterate
= FALSE
;
1709 pid_count_available
= 0;
1711 PGMEMBERS_FOREACH(pgrp
, p
) {
1712 pid_count_available
++;
1714 if (p
->p_stat
== SSTOP
) {
1715 should_iterate
= TRUE
;
1719 if (pid_count_available
== 0 || !should_iterate
) {
1724 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
1725 if (pid_list_size
>= pid_list_size_needed
) {
1730 if (pid_list_size
!= 0) {
1731 kfree(pid_list
, pid_list_size
);
1733 pid_list
= kalloc(pid_list_size_needed
);
1737 pid_list_size
= pid_list_size_needed
;
1740 /* no orphaned processes */
1741 if (pid_list_size
== 0) {
1746 PGMEMBERS_FOREACH(pgrp
, p
) {
1747 pid_list
[pid_count
++] = proc_pid(p
);
1748 if (pid_count
>= pid_count_available
) {
1754 if (pid_count
== 0) {
1758 for (int i
= 0; i
< pid_count
; i
++) {
1759 /* do not handle kernproc */
1760 if (pid_list
[i
] == 0) {
1763 p
= proc_find(pid_list
[i
]);
1768 proc_transwait(p
, 0);
1771 psignal(p
, SIGCONT
);
1776 kfree(pid_list
, pid_list_size
);
1781 proc_is_classic(proc_t p __unused
)
1786 /* XXX Why does this function exist? Need to kill it off... */
1788 current_proc_EXTERNAL(void)
1790 return (current_proc());
1794 proc_is_forcing_hfs_case_sensitivity(proc_t p
)
1796 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
) ? 1 : 0;
1801 * proc_core_name(name, uid, pid)
1802 * Expand the name described in corefilename, using name, uid, and pid.
1803 * corefilename is a printf-like string, with three format specifiers:
1804 * %N name of process ("name")
1805 * %P process id (pid)
1807 * For example, "%N.core" is the default; they can be disabled completely
1808 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1809 * This is controlled by the sysctl variable kern.corefile (see above).
1811 __private_extern__
int
1812 proc_core_name(const char *name
, uid_t uid
, pid_t pid
, char *cf_name
,
1815 const char *format
, *appendstr
;
1816 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
1819 if (cf_name
== NULL
)
1822 format
= corefilename
;
1823 for (i
= 0, n
= 0; n
< cf_name_len
&& format
[i
]; i
++) {
1824 switch (format
[i
]) {
1825 case '%': /* Format character */
1827 switch (format
[i
]) {
1831 case 'N': /* process name */
1834 case 'P': /* process id */
1835 snprintf(id_buf
, sizeof(id_buf
), "%u", pid
);
1838 case 'U': /* user id */
1839 snprintf(id_buf
, sizeof(id_buf
), "%u", uid
);
1842 case '\0': /* format string ended in % symbol */
1847 "Unknown format character %c in `%s'\n",
1850 l
= strlen(appendstr
);
1851 if ((n
+ l
) >= cf_name_len
)
1853 bcopy(appendstr
, cf_name
+ n
, l
);
1857 cf_name
[n
++] = format
[i
];
1860 if (format
[i
] != '\0')
1864 log(LOG_ERR
, "pid %ld (%s), uid (%u): corename is too long\n",
1865 (long)pid
, name
, (uint32_t)uid
);
1868 log(LOG_ERR
, "pid %ld (%s), uid (%u): unexpected end of string after %% token\n",
1869 (long)pid
, name
, (uint32_t)uid
);
1872 #endif /* CONFIG_COREDUMP */
1874 /* Code Signing related routines */
1877 csops(__unused proc_t p
, struct csops_args
*uap
, __unused
int32_t *retval
)
1879 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1880 uap
->usersize
, USER_ADDR_NULL
));
1884 csops_audittoken(__unused proc_t p
, struct csops_audittoken_args
*uap
, __unused
int32_t *retval
)
1886 if (uap
->uaudittoken
== USER_ADDR_NULL
)
1888 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1889 uap
->usersize
, uap
->uaudittoken
));
1893 csops_copy_token(void *start
, size_t length
, user_size_t usize
, user_addr_t uaddr
)
1895 char fakeheader
[8] = { 0 };
1898 if (usize
< sizeof(fakeheader
))
1901 /* if no blob, fill in zero header */
1902 if (NULL
== start
) {
1904 length
= sizeof(fakeheader
);
1905 } else if (usize
< length
) {
1906 /* ... if input too short, copy out length of entitlement */
1907 uint32_t length32
= htonl((uint32_t)length
);
1908 memcpy(&fakeheader
[4], &length32
, sizeof(length32
));
1910 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
1912 return ERANGE
; /* input buffer to short, ERANGE signals that */
1915 return copyout(start
, uaddr
, length
);
1919 csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaudittoken
)
1921 size_t usize
= (size_t)CAST_DOWN(size_t, usersize
);
1927 unsigned char cdhash
[SHA1_RESULTLEN
];
1928 audit_token_t token
;
1929 unsigned int upid
=0, uidversion
= 0;
1931 forself
= error
= 0;
1934 pid
= proc_selfpid();
1935 if (pid
== proc_selfpid())
1942 case CS_OPS_PIDOFFSET
:
1943 case CS_OPS_ENTITLEMENTS_BLOB
:
1944 case CS_OPS_IDENTITY
:
1946 break; /* not restricted to root */
1948 if (forself
== 0 && kauth_cred_issuser(kauth_cred_get()) != TRUE
)
1953 pt
= proc_find(pid
);
1954 if (pt
== PROC_NULL
)
1958 uidversion
= pt
->p_idversion
;
1959 if (uaudittoken
!= USER_ADDR_NULL
) {
1961 error
= copyin(uaudittoken
, &token
, sizeof(audit_token_t
));
1964 /* verify the audit token pid/idversion matches with proc */
1965 if ((token
.val
[5] != upid
) || (token
.val
[7] != uidversion
)) {
1973 case CS_OPS_MARKINVALID
:
1974 case CS_OPS_MARKHARD
:
1975 case CS_OPS_MARKKILL
:
1976 case CS_OPS_MARKRESTRICT
:
1977 case CS_OPS_SET_STATUS
:
1978 case CS_OPS_CLEARINSTALLER
:
1979 case CS_OPS_CLEARPLATFORM
:
1980 if ((error
= mac_proc_check_set_cs_info(current_proc(), pt
, ops
)))
1984 if ((error
= mac_proc_check_get_cs_info(current_proc(), pt
, ops
)))
1991 case CS_OPS_STATUS
: {
1995 retflags
= pt
->p_csflags
;
1996 if (cs_enforcement(pt
))
1997 retflags
|= CS_ENFORCEMENT
;
1998 if (csproc_get_platform_binary(pt
))
1999 retflags
|= CS_PLATFORM_BINARY
;
2000 if (csproc_get_platform_path(pt
))
2001 retflags
|= CS_PLATFORM_PATH
;
2004 if (uaddr
!= USER_ADDR_NULL
)
2005 error
= copyout(&retflags
, uaddr
, sizeof(uint32_t));
2008 case CS_OPS_MARKINVALID
:
2010 if ((pt
->p_csflags
& CS_VALID
) == CS_VALID
) { /* is currently valid */
2011 pt
->p_csflags
&= ~CS_VALID
; /* set invalid */
2012 if ((pt
->p_csflags
& CS_KILL
) == CS_KILL
) {
2013 pt
->p_csflags
|= CS_KILLED
;
2016 printf("CODE SIGNING: marked invalid by pid %d: "
2017 "p=%d[%s] honoring CS_KILL, final status 0x%x\n",
2018 proc_selfpid(), pt
->p_pid
, pt
->p_comm
, pt
->p_csflags
);
2020 psignal(pt
, SIGKILL
);
2028 case CS_OPS_MARKHARD
:
2030 pt
->p_csflags
|= CS_HARD
;
2031 if ((pt
->p_csflags
& CS_VALID
) == 0) {
2032 /* @@@ allow? reject? kill? @@@ */
2040 case CS_OPS_MARKKILL
:
2042 pt
->p_csflags
|= CS_KILL
;
2043 if ((pt
->p_csflags
& CS_VALID
) == 0) {
2045 psignal(pt
, SIGKILL
);
2050 case CS_OPS_PIDOFFSET
:
2051 toff
= pt
->p_textoff
;
2053 error
= copyout(&toff
, uaddr
, sizeof(toff
));
2058 /* pt already holds a reference on its p_textvp */
2060 toff
= pt
->p_textoff
;
2062 if (tvp
== NULLVP
|| usize
!= SHA1_RESULTLEN
) {
2067 error
= vn_getcdhash(tvp
, toff
, cdhash
);
2071 error
= copyout(cdhash
, uaddr
, sizeof (cdhash
));
2076 case CS_OPS_ENTITLEMENTS_BLOB
: {
2082 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2088 error
= cs_entitlements_blob_get(pt
, &start
, &length
);
2093 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2096 case CS_OPS_MARKRESTRICT
:
2098 pt
->p_csflags
|= CS_RESTRICT
;
2102 case CS_OPS_SET_STATUS
: {
2105 if (usize
< sizeof(flags
)) {
2110 error
= copyin(uaddr
, &flags
, sizeof(flags
));
2114 /* only allow setting a subset of all code sign flags */
2116 CS_HARD
| CS_EXEC_SET_HARD
|
2117 CS_KILL
| CS_EXEC_SET_KILL
|
2120 CS_ENFORCEMENT
| CS_EXEC_SET_ENFORCEMENT
;
2123 if (pt
->p_csflags
& CS_VALID
)
2124 pt
->p_csflags
|= flags
;
2136 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2142 error
= cs_blob_get(pt
, &start
, &length
);
2147 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2150 case CS_OPS_IDENTITY
: {
2151 const char *identity
;
2152 uint8_t fakeheader
[8];
2157 * Make identity have a blob header to make it
2158 * easier on userland to guess the identity
2161 if (usize
< sizeof(fakeheader
)) {
2165 memset(fakeheader
, 0, sizeof(fakeheader
));
2168 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2174 identity
= cs_identity_get(pt
);
2176 if (identity
== NULL
) {
2181 length
= strlen(identity
) + 1; /* include NUL */
2182 idlen
= htonl(length
+ sizeof(fakeheader
));
2183 memcpy(&fakeheader
[4], &idlen
, sizeof(idlen
));
2185 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
2189 if (usize
< sizeof(fakeheader
) + length
)
2191 else if (usize
> sizeof(fakeheader
))
2192 error
= copyout(identity
, uaddr
+ sizeof(fakeheader
), length
);
2197 case CS_OPS_CLEARINSTALLER
:
2199 pt
->p_csflags
&= ~(CS_INSTALLER
| CS_DATAVAULT_CONTROLLER
| CS_EXEC_INHERIT_SIP
);
2203 case CS_OPS_CLEARPLATFORM
:
2204 #if DEVELOPMENT || DEBUG
2205 if (cs_enforcement_enable
) {
2211 if (csr_check(CSR_ALLOW_APPLE_INTERNAL
) != 0) {
2218 pt
->p_csflags
&= ~(CS_PLATFORM_BINARY
|CS_PLATFORM_PATH
);
2219 csproc_clear_platform_binary(pt
);
2225 #endif /* !DEVELOPMENT || DEBUG */
2239 proc_iterate_fn_t callout
,
2241 proc_iterate_fn_t filterfn
,
2245 vm_size_t pid_list_size
= 0;
2246 vm_size_t pid_list_size_needed
= 0;
2248 int pid_count_available
= 0;
2250 assert(callout
!= NULL
);
2252 /* allocate outside of the proc_list_lock */
2256 pid_count_available
= nprocs
+ 1; //kernel_task is not counted in nprocs
2257 assert(pid_count_available
> 0);
2259 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2260 if (pid_list_size
>= pid_list_size_needed
) {
2265 if (pid_list_size
!= 0) {
2266 kfree(pid_list
, pid_list_size
);
2268 pid_list
= kalloc(pid_list_size_needed
);
2272 pid_list_size
= pid_list_size_needed
;
2275 /* filter pids into pid_list */
2277 if (flags
& PROC_ALLPROCLIST
) {
2279 ALLPROC_FOREACH(p
) {
2280 /* ignore processes that are being forked */
2281 if (p
->p_stat
== SIDL
) {
2284 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2288 pid_list
[pid_count
++] = proc_pid(p
);
2289 if (pid_count
>= pid_count_available
) {
2295 if ((pid_count
< pid_count_available
) &&
2296 (flags
& PROC_ZOMBPROCLIST
))
2299 ZOMBPROC_FOREACH(p
) {
2300 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2304 pid_list
[pid_count
++] = proc_pid(p
);
2305 if (pid_count
>= pid_count_available
) {
2313 /* call callout on processes in the pid_list */
2315 for (int i
= 0; i
< pid_count
; i
++) {
2316 proc_t p
= proc_find(pid_list
[i
]);
2318 if ((flags
& PROC_NOWAITTRANS
) == 0) {
2319 proc_transwait(p
, 0);
2321 int callout_ret
= callout(p
, arg
);
2323 switch (callout_ret
) {
2324 case PROC_RETURNED_DONE
:
2327 case PROC_CLAIMED_DONE
:
2337 panic("proc_iterate: callout returned %d for pid %d",
2338 callout_ret
, pid_list
[i
]);
2341 } else if (flags
& PROC_ZOMBPROCLIST
) {
2342 p
= proc_find_zombref(pid_list
[i
]);
2346 int callout_ret
= callout(p
, arg
);
2348 switch (callout_ret
) {
2349 case PROC_RETURNED_DONE
:
2350 proc_drop_zombref(p
);
2352 case PROC_CLAIMED_DONE
:
2356 proc_drop_zombref(p
);
2362 panic("proc_iterate: callout returned %d for zombie pid %d",
2363 callout_ret
, pid_list
[i
]);
2370 kfree(pid_list
, pid_list_size
);
2377 proc_iterate_fn_t callout
,
2379 proc_iterate_fn_t filterfn
,
2384 assert(callout
!= NULL
);
2386 proc_shutdown_exitcount
= 0;
2392 ALLPROC_FOREACH(p
) {
2393 if ((filterfn
!= NULL
) && filterfn(p
, filterarg
) == 0) {
2396 p
= proc_ref_locked(p
);
2403 proc_transwait(p
, 0);
2404 (void)callout(p
, arg
);
2407 goto restart_foreach
;
2416 proc_iterate_fn_t callout
,
2420 vm_size_t pid_list_size
= 0;
2421 vm_size_t pid_list_size_needed
= 0;
2423 int pid_count_available
= 0;
2425 assert(parent
!= NULL
);
2426 assert(callout
!= NULL
);
2431 pid_count_available
= parent
->p_childrencnt
;
2432 if (pid_count_available
== 0) {
2437 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2438 if (pid_list_size
>= pid_list_size_needed
) {
2443 if (pid_list_size
!= 0) {
2444 kfree(pid_list
, pid_list_size
);
2446 pid_list
= kalloc(pid_list_size_needed
);
2450 pid_list_size
= pid_list_size_needed
;
2454 PCHILDREN_FOREACH(parent
, p
) {
2455 if (p
->p_stat
== SIDL
) {
2459 pid_list
[pid_count
++] = proc_pid(p
);
2460 if (pid_count
>= pid_count_available
) {
2467 for (int i
= 0; i
< pid_count
; i
++) {
2468 p
= proc_find(pid_list
[i
]);
2473 int callout_ret
= callout(p
, arg
);
2475 switch (callout_ret
) {
2476 case PROC_RETURNED_DONE
:
2479 case PROC_CLAIMED_DONE
:
2488 panic("proc_childrenwalk: callout returned %d for pid %d",
2489 callout_ret
, pid_list
[i
]);
2495 kfree(pid_list
, pid_list_size
);
2503 proc_iterate_fn_t callout
,
2505 proc_iterate_fn_t filterfn
,
2510 vm_size_t pid_list_size
= 0;
2511 vm_size_t pid_list_size_needed
= 0;
2513 int pid_count_available
= 0;
2517 assert(pgrp
!= NULL
);
2518 assert(callout
!= NULL
);
2523 pid_count_available
= pgrp
->pg_membercnt
;
2524 if (pid_count_available
== 0) {
2529 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2530 if (pid_list_size
>= pid_list_size_needed
) {
2535 if (pid_list_size
!= 0) {
2536 kfree(pid_list
, pid_list_size
);
2538 pid_list
= kalloc(pid_list_size_needed
);
2542 pid_list_size
= pid_list_size_needed
;
2547 PGMEMBERS_FOREACH(pgrp
, p
) {
2548 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2551 pid_list
[pid_count
++] = proc_pid(p
);
2552 if (pid_count
>= pid_count_available
) {
2559 if (flags
& PGRP_DROPREF
) {
2563 for (int i
= 0; i
< pid_count
; i
++) {
2564 /* do not handle kernproc */
2565 if (pid_list
[i
] == 0) {
2568 p
= proc_find(pid_list
[i
]);
2572 if (p
->p_pgrpid
!= pgid
) {
2577 int callout_ret
= callout(p
, arg
);
2579 switch (callout_ret
) {
2586 case PROC_RETURNED_DONE
:
2589 case PROC_CLAIMED_DONE
:
2593 panic("pgrp_iterate: callout returned %d for pid %d",
2594 callout_ret
, pid_list
[i
]);
2599 kfree(pid_list
, pid_list_size
);
2604 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2607 child
->p_pgrp
= pgrp
;
2608 child
->p_pgrpid
= pgrp
->pg_id
;
2609 child
->p_listflag
|= P_LIST_INPGRP
;
2611 * When pgrp is being freed , a process can still
2612 * request addition using setpgid from bash when
2613 * login is terminated (login cycler) return ESRCH
2614 * Safe to hold lock due to refcount on pgrp
2616 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2617 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2620 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2621 panic("pgrp_add : pgrp is dead adding process");
2625 pgrp
->pg_membercnt
++;
2626 if ( parent
!= PROC_NULL
) {
2627 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2629 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2634 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2635 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2641 pgrp_remove(struct proc
* p
)
2648 #if __PROC_INTERNAL_DEBUG
2649 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2650 panic("removing from pglist but no named ref\n");
2652 p
->p_pgrpid
= PGRPID_DEAD
;
2653 p
->p_listflag
&= ~P_LIST_INPGRP
;
2657 if (pg
== PGRP_NULL
)
2658 panic("pgrp_remove: pg is NULL");
2662 if (pg
->pg_membercnt
< 0)
2663 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",pg
, p
);
2665 LIST_REMOVE(p
, p_pglist
);
2666 if (pg
->pg_members
.lh_first
== 0) {
2668 pgdelete_dropref(pg
);
2676 /* cannot use proc_pgrp as it maybe stalled */
2678 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2680 struct pgrp
* oldpg
;
2686 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2687 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2688 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2691 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2694 if (oldpg
== PGRP_NULL
)
2695 panic("pgrp_replace: oldpg NULL");
2696 oldpg
->pg_refcount
++;
2697 #if __PROC_INTERNAL_DEBUG
2698 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2699 panic("removing from pglist but no named ref\n");
2701 p
->p_pgrpid
= PGRPID_DEAD
;
2702 p
->p_listflag
&= ~P_LIST_INPGRP
;
2708 oldpg
->pg_membercnt
--;
2709 if (oldpg
->pg_membercnt
< 0)
2710 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",oldpg
, p
);
2711 LIST_REMOVE(p
, p_pglist
);
2712 if (oldpg
->pg_members
.lh_first
== 0) {
2714 pgdelete_dropref(oldpg
);
2722 p
->p_pgrpid
= newpg
->pg_id
;
2723 p
->p_listflag
|= P_LIST_INPGRP
;
2725 * When pgrp is being freed , a process can still
2726 * request addition using setpgid from bash when
2727 * login is terminated (login cycler) return ESRCH
2728 * Safe to hold lock due to refcount on pgrp
2730 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2731 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2734 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2735 panic("pgrp_add : pgrp is dead adding process");
2739 newpg
->pg_membercnt
++;
2740 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
2744 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
2745 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2748 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
2749 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
2750 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
2751 wakeup(&p
->p_pgrpid
);
2758 pgrp_lock(struct pgrp
* pgrp
)
2760 lck_mtx_lock(&pgrp
->pg_mlock
);
2764 pgrp_unlock(struct pgrp
* pgrp
)
2766 lck_mtx_unlock(&pgrp
->pg_mlock
);
2770 session_lock(struct session
* sess
)
2772 lck_mtx_lock(&sess
->s_mlock
);
2777 session_unlock(struct session
* sess
)
2779 lck_mtx_unlock(&sess
->s_mlock
);
2791 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2792 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2793 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2798 assert(pgrp
!= NULL
);
2800 if (pgrp
!= PGRP_NULL
) {
2801 pgrp
->pg_refcount
++;
2802 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0)
2803 panic("proc_pgrp: ref being povided for dead pgrp");
2812 tty_pgrp(struct tty
* tp
)
2814 struct pgrp
* pg
= PGRP_NULL
;
2819 if (pg
!= PGRP_NULL
) {
2820 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0)
2821 panic("tty_pgrp: ref being povided for dead pgrp");
2830 proc_session(proc_t p
)
2832 struct session
* sess
= SESSION_NULL
;
2835 return(SESSION_NULL
);
2839 /* wait during transitions */
2840 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2841 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2842 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2845 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
2846 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2847 panic("proc_session:returning sesssion ref on terminating session");
2855 session_rele(struct session
*sess
)
2858 if (--sess
->s_count
== 0) {
2859 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2860 panic("session_rele: terminating already terminated session");
2861 sess
->s_listflags
|= S_LIST_TERM
;
2862 LIST_REMOVE(sess
, s_hash
);
2863 sess
->s_listflags
|= S_LIST_DEAD
;
2864 if (sess
->s_count
!= 0)
2865 panic("session_rele: freeing session in use");
2867 #if CONFIG_FINE_LOCK_GROUPS
2868 lck_mtx_destroy(&sess
->s_mlock
, proc_mlock_grp
);
2870 lck_mtx_destroy(&sess
->s_mlock
, proc_lck_grp
);
2872 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
2878 proc_transstart(proc_t p
, int locked
, int non_blocking
)
2882 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2883 if (((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
) || non_blocking
) {
2888 p
->p_lflag
|= P_LTRANSWAIT
;
2889 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2891 p
->p_lflag
|= P_LINTRANSIT
;
2892 p
->p_transholder
= current_thread();
2899 proc_transcommit(proc_t p
, int locked
)
2904 assert ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
);
2905 assert (p
->p_transholder
== current_thread());
2906 p
->p_lflag
|= P_LTRANSCOMMIT
;
2908 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2909 p
->p_lflag
&= ~P_LTRANSWAIT
;
2910 wakeup(&p
->p_lflag
);
2917 proc_transend(proc_t p
, int locked
)
2922 p
->p_lflag
&= ~( P_LINTRANSIT
| P_LTRANSCOMMIT
);
2923 p
->p_transholder
= NULL
;
2925 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2926 p
->p_lflag
&= ~P_LTRANSWAIT
;
2927 wakeup(&p
->p_lflag
);
2934 proc_transwait(proc_t p
, int locked
)
2938 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2939 if ((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
&& current_proc() == p
) {
2944 p
->p_lflag
|= P_LTRANSWAIT
;
2945 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2953 proc_klist_lock(void)
2955 lck_mtx_lock(proc_klist_mlock
);
2959 proc_klist_unlock(void)
2961 lck_mtx_unlock(proc_klist_mlock
);
2965 proc_knote(struct proc
* p
, long hint
)
2968 KNOTE(&p
->p_klist
, hint
);
2969 proc_klist_unlock();
2973 proc_knote_drain(struct proc
*p
)
2975 struct knote
*kn
= NULL
;
2978 * Clear the proc's klist to avoid references after the proc is reaped.
2981 while ((kn
= SLIST_FIRST(&p
->p_klist
))) {
2982 kn
->kn_ptr
.p_proc
= PROC_NULL
;
2983 KNOTE_DETACH(&p
->p_klist
, kn
);
2985 proc_klist_unlock();
2989 proc_setregister(proc_t p
)
2992 p
->p_lflag
|= P_LREGISTER
;
2997 proc_resetregister(proc_t p
)
3000 p
->p_lflag
&= ~P_LREGISTER
;
3005 proc_pgrpid(proc_t p
)
3013 return current_proc()->p_pgrpid
;
3017 /* return control and action states */
3019 proc_getpcontrol(int pid
, int * pcontrolp
)
3026 if (pcontrolp
!= NULL
)
3027 *pcontrolp
= p
->p_pcaction
;
3034 proc_dopcontrol(proc_t p
)
3040 pcontrol
= PROC_CONTROL_STATE(p
);
3042 if (PROC_ACTION_STATE(p
) == 0) {
3045 PROC_SETACTION_STATE(p
);
3047 printf("low swap: throttling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3051 PROC_SETACTION_STATE(p
);
3053 printf("low swap: suspending pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3054 task_suspend(p
->task
);
3058 PROC_SETACTION_STATE(p
);
3060 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3061 psignal(p
, SIGKILL
);
3071 return(PROC_RETURNED
);
3076 * Resume a throttled or suspended process. This is an internal interface that's only
3077 * used by the user level code that presents the GUI when we run out of swap space and
3078 * hence is restricted to processes with superuser privileges.
3082 proc_resetpcontrol(int pid
)
3087 proc_t self
= current_proc();
3089 /* if the process has been validated to handle resource control or root is valid one */
3090 if (((self
->p_lflag
& P_LVMRSRCOWNER
) == 0) && (error
= suser(kauth_cred_get(), 0)))
3099 pcontrol
= PROC_CONTROL_STATE(p
);
3101 if(PROC_ACTION_STATE(p
) !=0) {
3104 PROC_RESETACTION_STATE(p
);
3106 printf("low swap: unthrottling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3110 PROC_RESETACTION_STATE(p
);
3112 printf("low swap: resuming pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3113 task_resume(p
->task
);
3118 PROC_SETACTION_STATE(p
);
3120 printf("low swap: attempt to unkill pid %d (%s) ignored\n", p
->p_pid
, p
->p_comm
);
3136 struct no_paging_space
3138 uint64_t pcs_max_size
;
3139 uint64_t pcs_uniqueid
;
3142 uint64_t pcs_total_size
;
3144 uint64_t npcs_max_size
;
3145 uint64_t npcs_uniqueid
;
3147 int npcs_proc_count
;
3148 uint64_t npcs_total_size
;
3150 int apcs_proc_count
;
3151 uint64_t apcs_total_size
;
3156 proc_pcontrol_filter(proc_t p
, void *arg
)
3158 struct no_paging_space
*nps
;
3159 uint64_t compressed
;
3161 nps
= (struct no_paging_space
*)arg
;
3163 compressed
= get_task_compressed(p
->task
);
3165 if (PROC_CONTROL_STATE(p
)) {
3166 if (PROC_ACTION_STATE(p
) == 0) {
3167 if (compressed
> nps
->pcs_max_size
) {
3168 nps
->pcs_pid
= p
->p_pid
;
3169 nps
->pcs_uniqueid
= p
->p_uniqueid
;
3170 nps
->pcs_max_size
= compressed
;
3172 nps
->pcs_total_size
+= compressed
;
3173 nps
->pcs_proc_count
++;
3175 nps
->apcs_total_size
+= compressed
;
3176 nps
->apcs_proc_count
++;
3179 if (compressed
> nps
->npcs_max_size
) {
3180 nps
->npcs_pid
= p
->p_pid
;
3181 nps
->npcs_uniqueid
= p
->p_uniqueid
;
3182 nps
->npcs_max_size
= compressed
;
3184 nps
->npcs_total_size
+= compressed
;
3185 nps
->npcs_proc_count
++;
3193 proc_pcontrol_null(__unused proc_t p
, __unused
void *arg
)
3195 return(PROC_RETURNED
);
3200 * Deal with the low on compressor pool space condition... this function
3201 * gets called when we are approaching the limits of the compressor pool or
3202 * we are unable to create a new swap file.
3203 * Since this eventually creates a memory deadlock situtation, we need to take action to free up
3204 * memory resources (both compressed and uncompressed) in order to prevent the system from hanging completely.
3205 * There are 2 categories of processes to deal with. Those that have an action
3206 * associated with them by the task itself and those that do not. Actionable
3207 * tasks can have one of three categories specified: ones that
3208 * can be killed immediately, ones that should be suspended, and ones that should
3209 * be throttled. Processes that do not have an action associated with them are normally
3210 * ignored unless they are utilizing such a large percentage of the compressor pool (currently 50%)
3211 * that only by killing them can we hope to put the system back into a usable state.
3214 #define NO_PAGING_SPACE_DEBUG 0
3216 extern uint64_t vm_compressor_pages_compressed(void);
3218 struct timeval last_no_space_action
= {0, 0};
3220 #if DEVELOPMENT || DEBUG
3221 extern boolean_t kill_on_no_paging_space
;
3222 #endif /* DEVELOPMENT || DEBUG */
3224 #define MB_SIZE (1024 * 1024ULL)
3225 boolean_t
memorystatus_kill_on_VM_thrashing(boolean_t
);
3227 extern int32_t max_kill_priority
;
3228 extern int memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index
);
3231 no_paging_space_action()
3234 struct no_paging_space nps
;
3238 * Throttle how often we come through here. Once every 5 seconds should be plenty.
3242 if (now
.tv_sec
<= last_no_space_action
.tv_sec
+ 5)
3246 * Examine all processes and find the biggest (biggest is based on the number of pages this
3247 * task has in the compressor pool) that has been marked to have some action
3248 * taken when swap space runs out... we also find the biggest that hasn't been marked for
3251 * If the biggest non-actionable task is over the "dangerously big" threashold (currently 50% of
3252 * the total number of pages held by the compressor, we go ahead and kill it since no other task
3253 * can have any real effect on the situation. Otherwise, we go after the actionable process.
3255 bzero(&nps
, sizeof(nps
));
3257 proc_iterate(PROC_ALLPROCLIST
, proc_pcontrol_null
, (void *)NULL
, proc_pcontrol_filter
, (void *)&nps
);
3259 #if NO_PAGING_SPACE_DEBUG
3260 printf("low swap: npcs_proc_count = %d, npcs_total_size = %qd, npcs_max_size = %qd\n",
3261 nps
.npcs_proc_count
, nps
.npcs_total_size
, nps
.npcs_max_size
);
3262 printf("low swap: pcs_proc_count = %d, pcs_total_size = %qd, pcs_max_size = %qd\n",
3263 nps
.pcs_proc_count
, nps
.pcs_total_size
, nps
.pcs_max_size
);
3264 printf("low swap: apcs_proc_count = %d, apcs_total_size = %qd\n",
3265 nps
.apcs_proc_count
, nps
.apcs_total_size
);
3267 if (nps
.npcs_max_size
> (vm_compressor_pages_compressed() * 50) / 100) {
3269 * for now we'll knock out any task that has more then 50% of the pages
3270 * held by the compressor
3272 if ((p
= proc_find(nps
.npcs_pid
)) != PROC_NULL
) {
3274 if (nps
.npcs_uniqueid
== p
->p_uniqueid
) {
3276 * verify this is still the same process
3277 * in case the proc exited and the pid got reused while
3278 * we were finishing the proc_iterate and getting to this point
3280 last_no_space_action
= now
;
3282 printf("low swap: killing largest compressed process with pid %d (%s) and size %llu MB\n", p
->p_pid
, p
->p_comm
, (nps
.pcs_max_size
/MB_SIZE
));
3283 psignal(p
, SIGKILL
);
3295 * We have some processes within our jetsam bands of consideration and hence can be killed.
3296 * So we will invoke the memorystatus thread to go ahead and kill something.
3298 if (memorystatus_get_proccnt_upto_priority(max_kill_priority
) > 0) {
3300 last_no_space_action
= now
;
3301 memorystatus_kill_on_VM_thrashing(TRUE
/* async */);
3306 * No eligible processes to kill. So let's suspend/kill the largest
3307 * process depending on its policy control specifications.
3310 if (nps
.pcs_max_size
> 0) {
3311 if ((p
= proc_find(nps
.pcs_pid
)) != PROC_NULL
) {
3313 if (nps
.pcs_uniqueid
== p
->p_uniqueid
) {
3315 * verify this is still the same process
3316 * in case the proc exited and the pid got reused while
3317 * we were finishing the proc_iterate and getting to this point
3319 last_no_space_action
= now
;
3331 last_no_space_action
= now
;
3333 printf("low swap: unable to find any eligible processes to take action on\n");
3339 proc_trace_log(__unused proc_t p
, struct proc_trace_log_args
*uap
, __unused
int *retval
)
3342 proc_t target_proc
= PROC_NULL
;
3343 pid_t target_pid
= uap
->pid
;
3344 uint64_t target_uniqueid
= uap
->uniqueid
;
3345 task_t target_task
= NULL
;
3347 if (priv_check_cred(kauth_cred_get(), PRIV_PROC_TRACE_INSPECT
, 0)) {
3351 target_proc
= proc_find(target_pid
);
3352 if (target_proc
!= PROC_NULL
) {
3353 if (target_uniqueid
!= proc_uniqueid(target_proc
)) {
3358 target_task
= proc_task(target_proc
);
3359 if (task_send_trace_memory(target_task
, target_pid
, target_uniqueid
)) {
3367 if (target_proc
!= PROC_NULL
)
3368 proc_rele(target_proc
);
3372 #if VM_SCAN_FOR_SHADOW_CHAIN
3373 extern int vm_map_shadow_max(vm_map_t map
);
3374 int proc_shadow_max(void);
3375 int proc_shadow_max(void)
3384 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
3385 if (p
->p_stat
== SIDL
)
3391 map
= get_task_map(task
);
3395 retval
= vm_map_shadow_max(map
);
3403 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */
3405 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
);
3406 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
)
3408 if (target_proc
!= NULL
) {
3409 target_proc
->p_responsible_pid
= responsible_pid
;
3415 proc_chrooted(proc_t p
)
3421 retval
= (p
->p_fd
->fd_rdir
!= NULL
) ? 1 : 0;
3429 proc_get_uthread_uu_threadlist(void * uthread_v
)
3431 uthread_t uth
= (uthread_t
)uthread_v
;
3432 return (uth
!= NULL
) ? uth
->uu_threadlist
: NULL
;