2 * Copyright (c) 2000-2019 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>
114 #include <sys/sysent.h>
115 #include <sys/reason.h>
116 #include <IOKit/IOBSD.h> /* IOTaskHasEntitlement() */
118 #ifdef CONFIG_32BIT_TELEMETRY
119 #include <sys/kasl.h>
120 #endif /* CONFIG_32BIT_TELEMETRY */
126 #if CONFIG_MEMORYSTATUS
127 #include <sys/kern_memorystatus.h>
131 #include <security/mac_framework.h>
134 #include <libkern/crypto/sha1.h>
136 #ifdef CONFIG_32BIT_TELEMETRY
137 #define MAX_32BIT_EXEC_SIG_SIZE 160
138 #endif /* CONFIG_32BIT_TELEMETRY */
141 * Structure associated with user cacheing.
144 LIST_ENTRY(uidinfo
) ui_hash
;
148 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
149 LIST_HEAD(uihashhead
, uidinfo
) * uihashtbl
;
150 u_long uihash
; /* size of hash table - 1 */
153 * Other process lists
155 struct pidhashhead
*pidhashtbl
;
157 struct pgrphashhead
*pgrphashtbl
;
159 struct sesshashhead
*sesshashtbl
;
162 struct proclist allproc
;
163 struct proclist zombproc
;
164 extern struct tty cons
;
168 #if DEVELOPMENT || DEBUG
169 int syscallfilter_disable
= 0;
170 #endif // DEVELOPMENT || DEBUG
173 #define __PROC_INTERNAL_DEBUG 1
176 /* Name to give to core files */
177 #if defined(XNU_TARGET_OS_BRIDGE)
178 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+ 1] = {"/private/var/internal/%N.core"};
179 #elif CONFIG_EMBEDDED
180 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+ 1] = {"/private/var/cores/%N.core"};
182 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+ 1] = {"/cores/core.%P"};
187 #include <kern/backtrace.h>
190 typedef uint64_t unaligned_u64
__attribute__((aligned(1)));
192 static void orphanpg(struct pgrp
* pg
);
193 void proc_name_kdp(task_t t
, char * buf
, int size
);
194 boolean_t
proc_binary_uuid_kdp(task_t task
, uuid_t uuid
);
195 int proc_threadname_kdp(void * uth
, char * buf
, size_t size
);
196 void proc_starttime_kdp(void * p
, unaligned_u64
*tv_sec
, unaligned_u64
*tv_usec
, unaligned_u64
*abstime
);
197 char * proc_name_address(void * p
);
199 static void pgrp_add(struct pgrp
* pgrp
, proc_t parent
, proc_t child
);
200 static void pgrp_remove(proc_t p
);
201 static void pgrp_replace(proc_t p
, struct pgrp
*pgrp
);
202 static void pgdelete_dropref(struct pgrp
*pgrp
);
203 extern void pg_rele_dropref(struct pgrp
* pgrp
);
204 static int csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaddittoken
);
205 static boolean_t
proc_parent_is_currentproc(proc_t p
);
207 struct fixjob_iterargs
{
209 struct session
* mysession
;
213 int fixjob_callback(proc_t
, void *);
216 get_current_unique_pid(void)
218 proc_t p
= current_proc();
221 return p
->p_uniqueid
;
228 * Initialize global process hashing structures.
234 LIST_INIT(&zombproc
);
235 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
236 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
237 sesshashtbl
= hashinit(maxproc
/ 4, M_PROC
, &sesshash
);
238 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
240 personas_bootstrap();
245 * Change the count associated with number of processes
246 * a given user is using. This routine protects the uihash
250 chgproccnt(uid_t uid
, int diff
)
253 struct uidinfo
*newuip
= NULL
;
254 struct uihashhead
*uipp
;
260 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
) {
261 if (uip
->ui_uid
== uid
) {
266 uip
->ui_proccnt
+= diff
;
267 if (uip
->ui_proccnt
> 0) {
268 retval
= uip
->ui_proccnt
;
272 if (uip
->ui_proccnt
< 0) {
273 panic("chgproccnt: procs < 0");
275 LIST_REMOVE(uip
, ui_hash
);
278 FREE_ZONE(uip
, sizeof(*uip
), M_PROC
);
287 panic("chgproccnt: lost user");
289 if (newuip
!= NULL
) {
292 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
294 uip
->ui_proccnt
= diff
;
300 MALLOC_ZONE(newuip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
301 if (newuip
== NULL
) {
302 panic("chgproccnt: M_PROC zone depleted");
306 if (newuip
!= NULL
) {
307 FREE_ZONE(newuip
, sizeof(*uip
), M_PROC
);
313 * Is p an inferior of the current process?
321 for (; p
!= current_proc(); p
= p
->p_pptr
) {
333 * Is p an inferior of t ?
336 isinferior(proc_t p
, proc_t t
)
342 /* if p==t they are not inferior */
348 for (; p
!= t
; p
= p
->p_pptr
) {
351 /* Detect here if we're in a cycle */
352 if ((p
->p_pid
== 0) || (p
->p_pptr
== start
) || (nchecked
>= nprocs
)) {
363 proc_isinferior(int pid1
, int pid2
)
365 proc_t p
= PROC_NULL
;
366 proc_t t
= PROC_NULL
;
369 if (((p
= proc_find(pid1
)) != (proc_t
)0) && ((t
= proc_find(pid2
)) != (proc_t
)0)) {
370 retval
= isinferior(p
, t
);
373 if (p
!= PROC_NULL
) {
376 if (t
!= PROC_NULL
) {
386 return proc_findinternal(pid
, 0);
390 proc_findinternal(int pid
, int locked
)
392 proc_t p
= PROC_NULL
;
398 p
= pfind_locked(pid
);
399 if ((p
== PROC_NULL
) || (p
!= proc_ref_locked(p
))) {
411 proc_findthread(thread_t thread
)
413 proc_t p
= PROC_NULL
;
417 uth
= get_bsdthread_info(thread
);
418 if (uth
&& (uth
->uu_flag
& UT_VFORK
)) {
421 p
= (proc_t
)(get_bsdthreadtask_info(thread
));
423 p
= proc_ref_locked(p
);
429 uthread_reset_proc_refcount(void *uthread
)
433 uth
= (uthread_t
) uthread
;
434 uth
->uu_proc_refcount
= 0;
437 if (proc_ref_tracking_disabled
) {
447 uthread_get_proc_refcount(void *uthread
)
451 if (proc_ref_tracking_disabled
) {
455 uth
= (uthread_t
) uthread
;
457 return uth
->uu_proc_refcount
;
462 record_procref(proc_t p __unused
, int count
)
466 uth
= current_uthread();
467 uth
->uu_proc_refcount
+= count
;
470 if (proc_ref_tracking_disabled
) {
474 if (uth
->uu_pindex
< NUM_PROC_REFS_TO_TRACK
) {
475 backtrace((uintptr_t *) &uth
->uu_proc_pcs
[uth
->uu_pindex
],
476 PROC_REF_STACK_DEPTH
, NULL
);
478 uth
->uu_proc_ps
[uth
->uu_pindex
] = p
;
485 uthread_needs_to_wait_in_proc_refwait(void)
487 uthread_t uth
= current_uthread();
490 * Allow threads holding no proc refs to wait
491 * in proc_refwait, allowing threads holding
492 * proc refs to wait in proc_refwait causes
493 * deadlocks and makes proc_find non-reentrant.
495 if (uth
->uu_proc_refcount
== 0) {
520 if (p
!= proc_ref_locked(p
)) {
529 proc_ref_locked(proc_t p
)
532 int pid
= proc_pid(p
);
536 * if process still in creation or proc got recycled
537 * during msleep then return failure.
539 if ((p
== PROC_NULL
) || (p1
!= p
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0)) {
544 * Do not return process marked for termination
545 * or proc_refdrain called without ref wait.
546 * Wait for proc_refdrain_with_refwait to complete if
547 * process in refdrain and refwait flag is set, unless
548 * the current thread is holding to a proc_ref
551 if ((p
->p_stat
!= SZOMB
) &&
552 ((p
->p_listflag
& P_LIST_EXITED
) == 0) &&
553 ((p
->p_listflag
& P_LIST_DEAD
) == 0) &&
554 (((p
->p_listflag
& (P_LIST_DRAIN
| P_LIST_DRAINWAIT
)) == 0) ||
555 ((p
->p_listflag
& P_LIST_REFWAIT
) != 0))) {
556 if ((p
->p_listflag
& P_LIST_REFWAIT
) != 0 && uthread_needs_to_wait_in_proc_refwait()) {
557 msleep(&p
->p_listflag
, proc_list_mlock
, 0, "proc_refwait", 0);
559 * the proc might have been recycled since we dropped
560 * the proc list lock, get the proc again.
562 p
= pfind_locked(pid
);
566 record_procref(p
, 1);
575 proc_rele_locked(proc_t p
)
577 if (p
->p_refcount
> 0) {
579 record_procref(p
, -1);
580 if ((p
->p_refcount
== 0) && ((p
->p_listflag
& P_LIST_DRAINWAIT
) == P_LIST_DRAINWAIT
)) {
581 p
->p_listflag
&= ~P_LIST_DRAINWAIT
;
582 wakeup(&p
->p_refcount
);
585 panic("proc_rele_locked -ve ref\n");
590 proc_find_zombref(int pid
)
597 p
= pfind_locked(pid
);
599 /* should we bail? */
600 if ((p
== PROC_NULL
) /* not found */
601 || ((p
->p_listflag
& P_LIST_INCREATE
) != 0) /* not created yet */
602 || ((p
->p_listflag
& P_LIST_EXITED
) == 0)) { /* not started exit */
607 /* If someone else is controlling the (unreaped) zombie - wait */
608 if ((p
->p_listflag
& P_LIST_WAITING
) != 0) {
609 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
612 p
->p_listflag
|= P_LIST_WAITING
;
620 proc_drop_zombref(proc_t p
)
623 if ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
624 p
->p_listflag
&= ~P_LIST_WAITING
;
632 proc_refdrain(proc_t p
)
634 proc_refdrain_with_refwait(p
, FALSE
);
638 proc_refdrain_with_refwait(proc_t p
, boolean_t get_ref_and_allow_wait
)
640 boolean_t initexec
= FALSE
;
643 p
->p_listflag
|= P_LIST_DRAIN
;
644 if (get_ref_and_allow_wait
) {
646 * All the calls to proc_ref_locked will wait
647 * for the flag to get cleared before returning a ref,
648 * unless the current thread is holding to a proc ref
651 p
->p_listflag
|= P_LIST_REFWAIT
;
657 /* Do not wait in ref drain for launchd exec */
658 while (p
->p_refcount
&& !initexec
) {
659 p
->p_listflag
|= P_LIST_DRAINWAIT
;
660 msleep(&p
->p_refcount
, proc_list_mlock
, 0, "proc_refdrain", 0);
663 p
->p_listflag
&= ~P_LIST_DRAIN
;
664 if (!get_ref_and_allow_wait
) {
665 p
->p_listflag
|= P_LIST_DEAD
;
667 /* Return a ref to the caller */
669 record_procref(p
, 1);
674 if (get_ref_and_allow_wait
) {
681 proc_refwake(proc_t p
)
684 p
->p_listflag
&= ~P_LIST_REFWAIT
;
685 wakeup(&p
->p_listflag
);
690 proc_parentholdref(proc_t p
)
692 proc_t parent
= PROC_NULL
;
700 if ((pp
== PROC_NULL
) || (pp
->p_stat
== SZOMB
) || ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
))) {
705 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == P_LIST_CHILDDRSTART
) {
706 pp
->p_listflag
|= P_LIST_CHILDDRWAIT
;
707 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
716 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == 0) {
727 proc_parentdropref(proc_t p
, int listlocked
)
729 if (listlocked
== 0) {
733 if (p
->p_parentref
> 0) {
735 if ((p
->p_parentref
== 0) && ((p
->p_listflag
& P_LIST_PARENTREFWAIT
) == P_LIST_PARENTREFWAIT
)) {
736 p
->p_listflag
&= ~P_LIST_PARENTREFWAIT
;
737 wakeup(&p
->p_parentref
);
740 panic("proc_parentdropref -ve ref\n");
742 if (listlocked
== 0) {
750 proc_childdrainstart(proc_t p
)
752 #if __PROC_INTERNAL_DEBUG
753 if ((p
->p_listflag
& P_LIST_CHILDDRSTART
) == P_LIST_CHILDDRSTART
) {
754 panic("proc_childdrainstart: childdrain already started\n");
757 p
->p_listflag
|= P_LIST_CHILDDRSTART
;
758 /* wait for all that hold parentrefs to drop */
759 while (p
->p_parentref
> 0) {
760 p
->p_listflag
|= P_LIST_PARENTREFWAIT
;
761 msleep(&p
->p_parentref
, proc_list_mlock
, 0, "proc_childdrainstart", 0);
767 proc_childdrainend(proc_t p
)
769 #if __PROC_INTERNAL_DEBUG
770 if (p
->p_childrencnt
> 0) {
771 panic("exiting: children stil hanging around\n");
774 p
->p_listflag
|= P_LIST_CHILDDRAINED
;
775 if ((p
->p_listflag
& (P_LIST_CHILDLKWAIT
| P_LIST_CHILDDRWAIT
)) != 0) {
776 p
->p_listflag
&= ~(P_LIST_CHILDLKWAIT
| P_LIST_CHILDDRWAIT
);
777 wakeup(&p
->p_childrencnt
);
782 proc_checkdeadrefs(__unused proc_t p
)
784 #if __PROC_INTERNAL_DEBUG
785 if ((p
->p_listflag
& P_LIST_INHASH
) != 0) {
786 panic("proc being freed and still in hash %p: %u\n", p
, p
->p_listflag
);
788 if (p
->p_childrencnt
!= 0) {
789 panic("proc being freed and pending children cnt %p:%d\n", p
, p
->p_childrencnt
);
791 if (p
->p_refcount
!= 0) {
792 panic("proc being freed and pending refcount %p:%d\n", p
, p
->p_refcount
);
794 if (p
->p_parentref
!= 0) {
795 panic("proc being freed and pending parentrefs %p:%d\n", p
, p
->p_parentref
);
819 proc_original_ppid(proc_t p
)
822 return p
->p_original_ppid
;
830 return current_proc()->p_pid
;
836 return current_proc()->p_ppid
;
840 proc_selfcsflags(void)
842 return current_proc()->p_csflags
;
846 proc_platform(proc_t p
)
849 return p
->p_platform
;
865 dtrace_current_proc_vforking(void)
867 thread_t th
= current_thread();
868 struct uthread
*ut
= get_bsdthread_info(th
);
871 ((ut
->uu_flag
& (UT_VFORK
| UT_VFORKING
)) == (UT_VFORK
| UT_VFORKING
))) {
873 * Handle the narrow window where we're in the vfork syscall,
874 * but we're not quite ready to claim (in particular, to DTrace)
875 * that we're running as the child.
877 return get_bsdtask_info(get_threadtask(th
));
879 return current_proc();
883 dtrace_proc_selfpid(void)
885 return dtrace_current_proc_vforking()->p_pid
;
889 dtrace_proc_selfppid(void)
891 return dtrace_current_proc_vforking()->p_ppid
;
895 dtrace_proc_selfruid(void)
897 return dtrace_current_proc_vforking()->p_ruid
;
899 #endif /* CONFIG_DTRACE */
902 proc_parent(proc_t p
)
910 parent
= proc_ref_locked(pp
);
911 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)) {
912 pp
->p_listflag
|= P_LIST_CHILDLKWAIT
;
913 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
921 proc_parent_is_currentproc(proc_t p
)
923 boolean_t ret
= FALSE
;
926 if (p
->p_pptr
== current_proc()) {
935 proc_name(int pid
, char * buf
, int size
)
939 if ((p
= proc_find(pid
)) != PROC_NULL
) {
940 strlcpy(buf
, &p
->p_comm
[0], size
);
946 proc_name_kdp(task_t t
, char * buf
, int size
)
948 proc_t p
= get_bsdtask_info(t
);
949 if (p
== PROC_NULL
) {
953 if ((size_t)size
> sizeof(p
->p_comm
)) {
954 strlcpy(buf
, &p
->p_name
[0], MIN((int)sizeof(p
->p_name
), size
));
956 strlcpy(buf
, &p
->p_comm
[0], MIN((int)sizeof(p
->p_comm
), size
));
961 proc_binary_uuid_kdp(task_t task
, uuid_t uuid
)
963 proc_t p
= get_bsdtask_info(task
);
964 if (p
== PROC_NULL
) {
968 proc_getexecutableuuid(p
, uuid
, sizeof(uuid_t
));
974 proc_threadname_kdp(void * uth
, char * buf
, size_t size
)
976 if (size
< MAXTHREADNAMESIZE
) {
977 /* this is really just a protective measure for the future in
978 * case the thread name size in stackshot gets out of sync with
979 * the BSD max thread name size. Note that bsd_getthreadname
980 * doesn't take input buffer size into account. */
985 bsd_getthreadname(uth
, buf
);
991 /* note that this function is generally going to be called from stackshot,
992 * and the arguments will be coming from a struct which is declared packed
993 * thus the input arguments will in general be unaligned. We have to handle
996 proc_starttime_kdp(void *p
, unaligned_u64
*tv_sec
, unaligned_u64
*tv_usec
, unaligned_u64
*abstime
)
998 proc_t pp
= (proc_t
)p
;
999 if (pp
!= PROC_NULL
) {
1000 if (tv_sec
!= NULL
) {
1001 *tv_sec
= pp
->p_start
.tv_sec
;
1003 if (tv_usec
!= NULL
) {
1004 *tv_usec
= pp
->p_start
.tv_usec
;
1006 if (abstime
!= NULL
) {
1007 if (pp
->p_stats
!= NULL
) {
1008 *abstime
= pp
->p_stats
->ps_start
;
1017 proc_name_address(void *p
)
1019 return &((proc_t
)p
)->p_comm
[0];
1023 proc_best_name(proc_t p
)
1025 if (p
->p_name
[0] != 0) {
1026 return &p
->p_name
[0];
1028 return &p
->p_comm
[0];
1032 proc_selfname(char * buf
, int size
)
1036 if ((p
= current_proc()) != (proc_t
)0) {
1037 strlcpy(buf
, &p
->p_comm
[0], size
);
1042 proc_signal(int pid
, int signum
)
1046 if ((p
= proc_find(pid
)) != PROC_NULL
) {
1053 proc_issignal(int pid
, sigset_t mask
)
1058 if ((p
= proc_find(pid
)) != PROC_NULL
) {
1059 error
= proc_pendingsignals(p
, mask
);
1067 proc_noremotehang(proc_t p
)
1072 retval
= p
->p_flag
& P_NOREMOTEHANG
;
1074 return retval
? 1: 0;
1078 proc_exiting(proc_t p
)
1083 retval
= p
->p_lflag
& P_LEXIT
;
1085 return retval
? 1: 0;
1089 proc_in_teardown(proc_t p
)
1094 retval
= p
->p_lflag
& P_LPEXIT
;
1096 return retval
? 1: 0;
1100 proc_forcequota(proc_t p
)
1105 retval
= p
->p_flag
& P_FORCEQUOTA
;
1107 return retval
? 1: 0;
1111 proc_suser(proc_t p
)
1113 kauth_cred_t my_cred
;
1116 my_cred
= kauth_cred_proc_ref(p
);
1117 error
= suser(my_cred
, &p
->p_acflag
);
1118 kauth_cred_unref(&my_cred
);
1123 proc_task(proc_t proc
)
1125 return (task_t
)proc
->task
;
1129 * Obtain the first thread in a process
1131 * XXX This is a bad thing to do; it exists predominantly to support the
1132 * XXX use of proc_t's in places that should really be using
1133 * XXX thread_t's instead. This maintains historical behaviour, but really
1134 * XXX needs an audit of the context (proxy vs. not) to clean up.
1137 proc_thread(proc_t proc
)
1139 uthread_t uth
= TAILQ_FIRST(&proc
->p_uthlist
);
1142 return uth
->uu_context
.vc_thread
;
1149 proc_ucred(proc_t p
)
1157 thread_t th
= current_thread();
1159 return (struct uthread
*)get_bsdthread_info(th
);
1164 proc_is64bit(proc_t p
)
1166 return IS_64BIT_PROCESS(p
);
1170 proc_is64bit_data(proc_t p
)
1173 return (int)task_get_64bit_data(p
->task
);
1177 proc_pidversion(proc_t p
)
1179 return p
->p_idversion
;
1183 proc_persona_id(proc_t p
)
1185 return (uint32_t)persona_id_from_proc(p
);
1189 proc_getuid(proc_t p
)
1195 proc_getgid(proc_t p
)
1201 proc_uniqueid(proc_t p
)
1203 return p
->p_uniqueid
;
1207 proc_puniqueid(proc_t p
)
1209 return p
->p_puniqueid
;
1213 proc_coalitionids(__unused proc_t p
, __unused
uint64_t ids
[COALITION_NUM_TYPES
])
1215 #if CONFIG_COALITIONS
1216 task_coalition_ids(p
->task
, ids
);
1218 memset(ids
, 0, sizeof(uint64_t[COALITION_NUM_TYPES
]));
1224 proc_was_throttled(proc_t p
)
1226 return p
->was_throttled
;
1230 proc_did_throttle(proc_t p
)
1232 return p
->did_throttle
;
1236 proc_getcdhash(proc_t p
, unsigned char *cdhash
)
1238 return vn_getcdhash(p
->p_textvp
, p
->p_textoff
, cdhash
);
1242 proc_exitstatus(proc_t p
)
1244 return p
->p_xstat
& 0xffff;
1248 proc_getexecutableuuid(proc_t p
, unsigned char *uuidbuf
, unsigned long size
)
1250 if (size
>= sizeof(p
->p_uuid
)) {
1251 memcpy(uuidbuf
, p
->p_uuid
, sizeof(p
->p_uuid
));
1255 /* Return vnode for executable with an iocount. Must be released with vnode_put() */
1257 proc_getexecutablevnode(proc_t p
)
1259 vnode_t tvp
= p
->p_textvp
;
1261 if (tvp
!= NULLVP
) {
1262 if (vnode_getwithref(tvp
) == 0) {
1271 proc_selfexecutableargs(uint8_t *buf
, size_t *buflen
)
1273 proc_t p
= current_proc();
1275 // buflen must always be provided
1276 if (buflen
== NULL
) {
1280 // If a buf is provided, there must be at least enough room to fit argc
1281 if (buf
&& *buflen
< sizeof(p
->p_argc
)) {
1285 if (!p
->user_stack
) {
1290 *buflen
= p
->p_argslen
+ sizeof(p
->p_argc
);
1294 // Copy in argc to the first 4 bytes
1295 memcpy(buf
, &p
->p_argc
, sizeof(p
->p_argc
));
1297 if (*buflen
> sizeof(p
->p_argc
) && p
->p_argslen
> 0) {
1298 // See memory layout comment in kern_exec.c:exec_copyout_strings()
1299 // We want to copy starting from `p_argslen` bytes away from top of stack
1300 return copyin(p
->user_stack
- p
->p_argslen
,
1301 buf
+ sizeof(p
->p_argc
),
1302 MIN(p
->p_argslen
, *buflen
- sizeof(p
->p_argc
)));
1309 proc_getexecutableoffset(proc_t p
)
1311 return p
->p_textoff
;
1315 bsd_set_dependency_capable(task_t task
)
1317 proc_t p
= get_bsdtask_info(task
);
1320 OSBitOrAtomic(P_DEPENDENCY_CAPABLE
, &p
->p_flag
);
1327 IS_64BIT_PROCESS(proc_t p
)
1329 if (p
&& (p
->p_flag
& P_LP64
)) {
1338 * Locate a process by number
1341 pfind_locked(pid_t pid
)
1352 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
) {
1353 if (p
->p_pid
== pid
) {
1355 for (q
= p
->p_hash
.le_next
; q
!= 0; q
= q
->p_hash
.le_next
) {
1356 if ((p
!= q
) && (q
->p_pid
== pid
)) {
1357 panic("two procs with same pid %p:%p:%d:%d\n", p
, q
, p
->p_pid
, q
->p_pid
);
1368 * Locate a zombie by PID
1370 __private_extern__ proc_t
1378 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1379 if (p
->p_pid
== pid
) {
1390 * Locate a process group by number
1399 pgrp
= pgfind_internal(pgid
);
1400 if ((pgrp
== NULL
) || ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) != 0)) {
1403 pgrp
->pg_refcount
++;
1412 pgfind_internal(pid_t pgid
)
1416 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
) {
1417 if (pgrp
->pg_id
== pgid
) {
1425 pg_rele(struct pgrp
* pgrp
)
1427 if (pgrp
== PGRP_NULL
) {
1430 pg_rele_dropref(pgrp
);
1434 pg_rele_dropref(struct pgrp
* pgrp
)
1437 if ((pgrp
->pg_refcount
== 1) && ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) == PGRP_FLAG_TERMINATE
)) {
1439 pgdelete_dropref(pgrp
);
1443 pgrp
->pg_refcount
--;
1448 session_find_internal(pid_t sessid
)
1450 struct session
*sess
;
1452 for (sess
= SESSHASH(sessid
)->lh_first
; sess
!= 0; sess
= sess
->s_hash
.le_next
) {
1453 if (sess
->s_sid
== sessid
) {
1462 * Make a new process ready to become a useful member of society by making it
1463 * visible in all the right places and initialize its own lists to empty.
1465 * Parameters: parent The parent of the process to insert
1466 * child The child process to insert
1470 * Notes: Insert a child process into the parents process group, assign
1471 * the child the parent process pointer and PPID of the parent,
1472 * place it on the parents p_children list as a sibling,
1473 * initialize its own child list, place it in the allproc list,
1474 * insert it in the proper hash bucket, and initialize its
1478 pinsertchild(proc_t parent
, proc_t child
)
1482 LIST_INIT(&child
->p_children
);
1483 TAILQ_INIT(&child
->p_evlist
);
1484 child
->p_pptr
= parent
;
1485 child
->p_ppid
= parent
->p_pid
;
1486 child
->p_original_ppid
= parent
->p_pid
;
1487 child
->p_puniqueid
= parent
->p_uniqueid
;
1488 child
->p_xhighbits
= 0;
1490 pg
= proc_pgrp(parent
);
1491 pgrp_add(pg
, parent
, child
);
1496 #if CONFIG_MEMORYSTATUS
1497 memorystatus_add(child
, TRUE
);
1500 parent
->p_childrencnt
++;
1501 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1503 LIST_INSERT_HEAD(&allproc
, child
, p_list
);
1504 /* mark the completion of proc creation */
1505 child
->p_listflag
&= ~P_LIST_INCREATE
;
1511 * Move p to a new or existing process group (and session)
1513 * Returns: 0 Success
1514 * ESRCH No such process
1517 enterpgrp(proc_t p
, pid_t pgid
, int mksess
)
1520 struct pgrp
*mypgrp
;
1521 struct session
* procsp
;
1523 pgrp
= pgfind(pgid
);
1524 mypgrp
= proc_pgrp(p
);
1525 procsp
= proc_session(p
);
1528 if (pgrp
!= NULL
&& mksess
) { /* firewalls */
1529 panic("enterpgrp: setsid into non-empty pgrp");
1531 if (SESS_LEADER(p
, procsp
)) {
1532 panic("enterpgrp: session leader attempted setpgrp");
1535 if (pgrp
== PGRP_NULL
) {
1536 pid_t savepid
= p
->p_pid
;
1537 proc_t np
= PROC_NULL
;
1542 if (p
->p_pid
!= pgid
) {
1543 panic("enterpgrp: new pgrp and pid != pgid");
1546 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
1549 panic("enterpgrp: M_PGRP zone depleted");
1551 if ((np
= proc_find(savepid
)) == NULL
|| np
!= p
) {
1552 if (np
!= PROC_NULL
) {
1555 if (mypgrp
!= PGRP_NULL
) {
1558 if (procsp
!= SESSION_NULL
) {
1559 session_rele(procsp
);
1561 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
1566 struct session
*sess
;
1571 MALLOC_ZONE(sess
, struct session
*,
1572 sizeof(struct session
), M_SESSION
, M_WAITOK
);
1574 panic("enterpgrp: M_SESSION zone depleted");
1577 sess
->s_sid
= p
->p_pid
;
1579 sess
->s_ttyvp
= NULL
;
1580 sess
->s_ttyp
= TTY_NULL
;
1582 sess
->s_listflags
= 0;
1583 sess
->s_ttypgrpid
= NO_PID
;
1585 lck_mtx_init(&sess
->s_mlock
, proc_mlock_grp
, proc_lck_attr
);
1587 bcopy(procsp
->s_login
, sess
->s_login
,
1588 sizeof(sess
->s_login
));
1589 OSBitAndAtomic(~((uint32_t)P_CONTROLT
), &p
->p_flag
);
1591 LIST_INSERT_HEAD(SESSHASH(sess
->s_sid
), sess
, s_hash
);
1593 pgrp
->pg_session
= sess
;
1595 if (p
!= current_proc()) {
1596 panic("enterpgrp: mksession and p != curproc");
1601 pgrp
->pg_session
= procsp
;
1603 if ((pgrp
->pg_session
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0) {
1604 panic("enterpgrp: providing ref to terminating session ");
1606 pgrp
->pg_session
->s_count
++;
1611 lck_mtx_init(&pgrp
->pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
1613 LIST_INIT(&pgrp
->pg_members
);
1614 pgrp
->pg_membercnt
= 0;
1617 pgrp
->pg_refcount
= 1;
1618 pgrp
->pg_listflags
= 0;
1619 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
1621 } else if (pgrp
== mypgrp
) {
1623 if (mypgrp
!= NULL
) {
1626 if (procsp
!= SESSION_NULL
) {
1627 session_rele(procsp
);
1632 if (procsp
!= SESSION_NULL
) {
1633 session_rele(procsp
);
1636 * Adjust eligibility of affected pgrps to participate in job control.
1637 * Increment eligibility counts before decrementing, otherwise we
1638 * could reach 0 spuriously during the first call.
1640 fixjobc(p
, pgrp
, 1);
1641 fixjobc(p
, mypgrp
, 0);
1643 if (mypgrp
!= PGRP_NULL
) {
1646 pgrp_replace(p
, pgrp
);
1653 * remove process from process group
1663 * delete a process group
1666 pgdelete_dropref(struct pgrp
*pgrp
)
1670 struct session
*sessp
;
1674 if (pgrp
->pg_membercnt
!= 0) {
1680 pgrp
->pg_refcount
--;
1681 if ((emptypgrp
== 0) || (pgrp
->pg_membercnt
!= 0)) {
1686 pgrp
->pg_listflags
|= PGRP_FLAG_TERMINATE
;
1688 if (pgrp
->pg_refcount
> 0) {
1693 pgrp
->pg_listflags
|= PGRP_FLAG_DEAD
;
1694 LIST_REMOVE(pgrp
, pg_hash
);
1698 ttyp
= SESSION_TP(pgrp
->pg_session
);
1699 if (ttyp
!= TTY_NULL
) {
1700 if (ttyp
->t_pgrp
== pgrp
) {
1702 /* Re-check after acquiring the lock */
1703 if (ttyp
->t_pgrp
== pgrp
) {
1704 ttyp
->t_pgrp
= NULL
;
1705 pgrp
->pg_session
->s_ttypgrpid
= NO_PID
;
1713 sessp
= pgrp
->pg_session
;
1714 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0) {
1715 panic("pg_deleteref: manipulating refs of already terminating session");
1717 if (--sessp
->s_count
== 0) {
1718 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0) {
1719 panic("pg_deleteref: terminating already terminated session");
1721 sessp
->s_listflags
|= S_LIST_TERM
;
1722 ttyp
= SESSION_TP(sessp
);
1723 LIST_REMOVE(sessp
, s_hash
);
1725 if (ttyp
!= TTY_NULL
) {
1727 if (ttyp
->t_session
== sessp
) {
1728 ttyp
->t_session
= NULL
;
1733 sessp
->s_listflags
|= S_LIST_DEAD
;
1734 if (sessp
->s_count
!= 0) {
1735 panic("pg_deleteref: freeing session in use");
1738 lck_mtx_destroy(&sessp
->s_mlock
, proc_mlock_grp
);
1740 FREE_ZONE(sessp
, sizeof(struct session
), M_SESSION
);
1744 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_mlock_grp
);
1745 FREE_ZONE(pgrp
, sizeof(*pgrp
), M_PGRP
);
1750 * Adjust pgrp jobc counters when specified process changes process group.
1751 * We count the number of processes in each process group that "qualify"
1752 * the group for terminal job control (those with a parent in a different
1753 * process group of the same session). If that count reaches zero, the
1754 * process group becomes orphaned. Check both the specified process'
1755 * process group and that of its children.
1756 * entering == 0 => p is leaving specified group.
1757 * entering == 1 => p is entering specified group.
1760 fixjob_callback(proc_t p
, void * arg
)
1762 struct fixjob_iterargs
*fp
;
1763 struct pgrp
* pg
, *hispg
;
1764 struct session
* mysession
, *hissess
;
1767 fp
= (struct fixjob_iterargs
*)arg
;
1769 mysession
= fp
->mysession
;
1770 entering
= fp
->entering
;
1772 hispg
= proc_pgrp(p
);
1773 hissess
= proc_session(p
);
1775 if ((hispg
!= pg
) &&
1776 (hissess
== mysession
)) {
1781 } else if (--hispg
->pg_jobc
== 0) {
1788 if (hissess
!= SESSION_NULL
) {
1789 session_rele(hissess
);
1791 if (hispg
!= PGRP_NULL
) {
1795 return PROC_RETURNED
;
1799 fixjobc(proc_t p
, struct pgrp
*pgrp
, int entering
)
1801 struct pgrp
*hispgrp
= PGRP_NULL
;
1802 struct session
*hissess
= SESSION_NULL
;
1803 struct session
*mysession
= pgrp
->pg_session
;
1805 struct fixjob_iterargs fjarg
;
1806 boolean_t proc_parent_self
;
1809 * Check if p's parent is current proc, if yes then no need to take
1810 * a ref; calling proc_parent with current proc as parent may
1811 * deadlock if current proc is exiting.
1813 proc_parent_self
= proc_parent_is_currentproc(p
);
1814 if (proc_parent_self
) {
1815 parent
= current_proc();
1817 parent
= proc_parent(p
);
1820 if (parent
!= PROC_NULL
) {
1821 hispgrp
= proc_pgrp(parent
);
1822 hissess
= proc_session(parent
);
1823 if (!proc_parent_self
) {
1830 * Check p's parent to see whether p qualifies its own process
1831 * group; if so, adjust count for p's process group.
1833 if ((hispgrp
!= pgrp
) &&
1834 (hissess
== mysession
)) {
1839 } else if (--pgrp
->pg_jobc
== 0) {
1847 if (hissess
!= SESSION_NULL
) {
1848 session_rele(hissess
);
1850 if (hispgrp
!= PGRP_NULL
) {
1855 * Check this process' children to see whether they qualify
1856 * their process groups; if so, adjust counts for children's
1860 fjarg
.mysession
= mysession
;
1861 fjarg
.entering
= entering
;
1862 proc_childrenwalk(p
, fixjob_callback
, &fjarg
);
1866 * The pidlist_* routines support the functions in this file that
1867 * walk lists of processes applying filters and callouts to the
1868 * elements of the list.
1870 * A prior implementation used a single linear array, which can be
1871 * tricky to allocate on large systems. This implementation creates
1872 * an SLIST of modestly sized arrays of PIDS_PER_ENTRY elements.
1874 * The array should be sized large enough to keep the overhead of
1875 * walking the list low, but small enough that blocking allocations of
1876 * pidlist_entry_t structures always succeed.
1879 #define PIDS_PER_ENTRY 1021
1881 typedef struct pidlist_entry
{
1882 SLIST_ENTRY(pidlist_entry
) pe_link
;
1884 pid_t pe_pid
[PIDS_PER_ENTRY
];
1888 SLIST_HEAD(, pidlist_entry
) pl_head
;
1889 struct pidlist_entry
*pl_active
;
1893 static __inline__ pidlist_t
*
1894 pidlist_init(pidlist_t
*pl
)
1896 SLIST_INIT(&pl
->pl_head
);
1897 pl
->pl_active
= NULL
;
1903 pidlist_alloc(pidlist_t
*pl
, u_int needed
)
1905 while (pl
->pl_nalloc
< needed
) {
1906 pidlist_entry_t
*pe
= kalloc(sizeof(*pe
));
1908 panic("no space for pidlist entry");
1911 SLIST_INSERT_HEAD(&pl
->pl_head
, pe
, pe_link
);
1912 pl
->pl_nalloc
+= (sizeof(pe
->pe_pid
) / sizeof(pe
->pe_pid
[0]));
1914 return pl
->pl_nalloc
;
1918 pidlist_free(pidlist_t
*pl
)
1920 pidlist_entry_t
*pe
;
1921 while (NULL
!= (pe
= SLIST_FIRST(&pl
->pl_head
))) {
1922 SLIST_FIRST(&pl
->pl_head
) = SLIST_NEXT(pe
, pe_link
);
1923 kfree(pe
, sizeof(*pe
));
1928 static __inline__
void
1929 pidlist_set_active(pidlist_t
*pl
)
1931 pl
->pl_active
= SLIST_FIRST(&pl
->pl_head
);
1932 assert(pl
->pl_active
);
1936 pidlist_add_pid(pidlist_t
*pl
, pid_t pid
)
1938 pidlist_entry_t
*pe
= pl
->pl_active
;
1939 if (pe
->pe_nused
>= sizeof(pe
->pe_pid
) / sizeof(pe
->pe_pid
[0])) {
1940 if (NULL
== (pe
= SLIST_NEXT(pe
, pe_link
))) {
1941 panic("pidlist allocation exhausted");
1945 pe
->pe_pid
[pe
->pe_nused
++] = pid
;
1948 static __inline__ u_int
1949 pidlist_nalloc(const pidlist_t
*pl
)
1951 return pl
->pl_nalloc
;
1955 * A process group has become orphaned; if there are any stopped processes in
1956 * the group, hang-up all process in that group.
1959 orphanpg(struct pgrp
*pgrp
)
1961 pidlist_t pid_list
, *pl
= pidlist_init(&pid_list
);
1962 u_int pid_count_available
= 0;
1965 /* allocate outside of the pgrp_lock */
1969 boolean_t should_iterate
= FALSE
;
1970 pid_count_available
= 0;
1972 PGMEMBERS_FOREACH(pgrp
, p
) {
1973 pid_count_available
++;
1974 if (p
->p_stat
== SSTOP
) {
1975 should_iterate
= TRUE
;
1978 if (pid_count_available
== 0 || !should_iterate
) {
1980 goto out
; /* no orphaned processes OR nothing stopped */
1982 if (pidlist_nalloc(pl
) >= pid_count_available
) {
1987 pidlist_alloc(pl
, pid_count_available
);
1989 pidlist_set_active(pl
);
1991 u_int pid_count
= 0;
1992 PGMEMBERS_FOREACH(pgrp
, p
) {
1993 pidlist_add_pid(pl
, proc_pid(p
));
1994 if (++pid_count
>= pid_count_available
) {
2000 const pidlist_entry_t
*pe
;
2001 SLIST_FOREACH(pe
, &(pl
->pl_head
), pe_link
) {
2002 for (u_int i
= 0; i
< pe
->pe_nused
; i
++) {
2003 const pid_t pid
= pe
->pe_pid
[i
];
2005 continue; /* skip kernproc */
2011 proc_transwait(p
, 0);
2014 psignal(p
, SIGCONT
);
2023 proc_is_classic(proc_t p __unused
)
2028 /* XXX Why does this function exist? Need to kill it off... */
2030 current_proc_EXTERNAL(void)
2032 return current_proc();
2036 proc_is_forcing_hfs_case_sensitivity(proc_t p
)
2038 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
) ? 1 : 0;
2043 * proc_core_name(name, uid, pid)
2044 * Expand the name described in corefilename, using name, uid, and pid.
2045 * corefilename is a printf-like string, with three format specifiers:
2046 * %N name of process ("name")
2047 * %P process id (pid)
2049 * For example, "%N.core" is the default; they can be disabled completely
2050 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2051 * This is controlled by the sysctl variable kern.corefile (see above).
2053 __private_extern__
int
2054 proc_core_name(const char *name
, uid_t uid
, pid_t pid
, char *cf_name
,
2057 const char *format
, *appendstr
;
2058 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
2061 if (cf_name
== NULL
) {
2065 format
= corefilename
;
2066 for (i
= 0, n
= 0; n
< cf_name_len
&& format
[i
]; i
++) {
2067 switch (format
[i
]) {
2068 case '%': /* Format character */
2070 switch (format
[i
]) {
2074 case 'N': /* process name */
2077 case 'P': /* process id */
2078 snprintf(id_buf
, sizeof(id_buf
), "%u", pid
);
2081 case 'U': /* user id */
2082 snprintf(id_buf
, sizeof(id_buf
), "%u", uid
);
2085 case '\0': /* format string ended in % symbol */
2090 "Unknown format character %c in `%s'\n",
2093 l
= strlen(appendstr
);
2094 if ((n
+ l
) >= cf_name_len
) {
2097 bcopy(appendstr
, cf_name
+ n
, l
);
2101 cf_name
[n
++] = format
[i
];
2104 if (format
[i
] != '\0') {
2109 log(LOG_ERR
, "pid %ld (%s), uid (%u): corename is too long\n",
2110 (long)pid
, name
, (uint32_t)uid
);
2113 log(LOG_ERR
, "pid %ld (%s), uid (%u): unexpected end of string after %% token\n",
2114 (long)pid
, name
, (uint32_t)uid
);
2117 #endif /* CONFIG_COREDUMP */
2119 /* Code Signing related routines */
2122 csops(__unused proc_t p
, struct csops_args
*uap
, __unused
int32_t *retval
)
2124 return csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
2125 uap
->usersize
, USER_ADDR_NULL
);
2129 csops_audittoken(__unused proc_t p
, struct csops_audittoken_args
*uap
, __unused
int32_t *retval
)
2131 if (uap
->uaudittoken
== USER_ADDR_NULL
) {
2134 return csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
2135 uap
->usersize
, uap
->uaudittoken
);
2139 csops_copy_token(void *start
, size_t length
, user_size_t usize
, user_addr_t uaddr
)
2141 char fakeheader
[8] = { 0 };
2144 if (usize
< sizeof(fakeheader
)) {
2148 /* if no blob, fill in zero header */
2149 if (NULL
== start
) {
2151 length
= sizeof(fakeheader
);
2152 } else if (usize
< length
) {
2153 /* ... if input too short, copy out length of entitlement */
2154 uint32_t length32
= htonl((uint32_t)length
);
2155 memcpy(&fakeheader
[4], &length32
, sizeof(length32
));
2157 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
2159 return ERANGE
; /* input buffer to short, ERANGE signals that */
2163 return copyout(start
, uaddr
, length
);
2167 csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaudittoken
)
2169 size_t usize
= (size_t)CAST_DOWN(size_t, usersize
);
2175 unsigned char cdhash
[SHA1_RESULTLEN
];
2176 audit_token_t token
;
2177 unsigned int upid
= 0, uidversion
= 0;
2179 forself
= error
= 0;
2182 pid
= proc_selfpid();
2184 if (pid
== proc_selfpid()) {
2192 case CS_OPS_PIDOFFSET
:
2193 case CS_OPS_ENTITLEMENTS_BLOB
:
2194 case CS_OPS_IDENTITY
:
2197 case CS_OPS_CLEAR_LV
:
2198 break; /* not restricted to root */
2200 if (forself
== 0 && kauth_cred_issuser(kauth_cred_get()) != TRUE
) {
2206 pt
= proc_find(pid
);
2207 if (pt
== PROC_NULL
) {
2212 uidversion
= pt
->p_idversion
;
2213 if (uaudittoken
!= USER_ADDR_NULL
) {
2214 error
= copyin(uaudittoken
, &token
, sizeof(audit_token_t
));
2218 /* verify the audit token pid/idversion matches with proc */
2219 if ((token
.val
[5] != upid
) || (token
.val
[7] != uidversion
)) {
2227 case CS_OPS_MARKINVALID
:
2228 case CS_OPS_MARKHARD
:
2229 case CS_OPS_MARKKILL
:
2230 case CS_OPS_MARKRESTRICT
:
2231 case CS_OPS_SET_STATUS
:
2232 case CS_OPS_CLEARINSTALLER
:
2233 case CS_OPS_CLEARPLATFORM
:
2234 case CS_OPS_CLEAR_LV
:
2235 if ((error
= mac_proc_check_set_cs_info(current_proc(), pt
, ops
))) {
2240 if ((error
= mac_proc_check_get_cs_info(current_proc(), pt
, ops
))) {
2247 case CS_OPS_STATUS
: {
2251 retflags
= pt
->p_csflags
;
2252 if (cs_process_enforcement(pt
)) {
2253 retflags
|= CS_ENFORCEMENT
;
2255 if (csproc_get_platform_binary(pt
)) {
2256 retflags
|= CS_PLATFORM_BINARY
;
2258 if (csproc_get_platform_path(pt
)) {
2259 retflags
|= CS_PLATFORM_PATH
;
2261 //Don't return CS_REQUIRE_LV if we turned it on with CS_FORCED_LV but still report CS_FORCED_LV
2262 if ((pt
->p_csflags
& CS_FORCED_LV
) == CS_FORCED_LV
) {
2263 retflags
&= (~CS_REQUIRE_LV
);
2267 if (uaddr
!= USER_ADDR_NULL
) {
2268 error
= copyout(&retflags
, uaddr
, sizeof(uint32_t));
2272 case CS_OPS_MARKINVALID
:
2274 if ((pt
->p_csflags
& CS_VALID
) == CS_VALID
) { /* is currently valid */
2275 pt
->p_csflags
&= ~CS_VALID
; /* set invalid */
2276 if ((pt
->p_csflags
& CS_KILL
) == CS_KILL
) {
2277 pt
->p_csflags
|= CS_KILLED
;
2280 printf("CODE SIGNING: marked invalid by pid %d: "
2281 "p=%d[%s] honoring CS_KILL, final status 0x%x\n",
2282 proc_selfpid(), pt
->p_pid
, pt
->p_comm
, pt
->p_csflags
);
2284 psignal(pt
, SIGKILL
);
2294 case CS_OPS_MARKHARD
:
2296 pt
->p_csflags
|= CS_HARD
;
2297 if ((pt
->p_csflags
& CS_VALID
) == 0) {
2298 /* @@@ allow? reject? kill? @@@ */
2307 case CS_OPS_MARKKILL
:
2309 pt
->p_csflags
|= CS_KILL
;
2310 if ((pt
->p_csflags
& CS_VALID
) == 0) {
2312 psignal(pt
, SIGKILL
);
2318 case CS_OPS_PIDOFFSET
:
2319 toff
= pt
->p_textoff
;
2321 error
= copyout(&toff
, uaddr
, sizeof(toff
));
2326 /* pt already holds a reference on its p_textvp */
2328 toff
= pt
->p_textoff
;
2330 if (tvp
== NULLVP
|| usize
!= SHA1_RESULTLEN
) {
2335 error
= vn_getcdhash(tvp
, toff
, cdhash
);
2339 error
= copyout(cdhash
, uaddr
, sizeof(cdhash
));
2344 case CS_OPS_ENTITLEMENTS_BLOB
: {
2350 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2356 error
= cs_entitlements_blob_get(pt
, &start
, &length
);
2362 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2365 case CS_OPS_MARKRESTRICT
:
2367 pt
->p_csflags
|= CS_RESTRICT
;
2371 case CS_OPS_SET_STATUS
: {
2374 if (usize
< sizeof(flags
)) {
2379 error
= copyin(uaddr
, &flags
, sizeof(flags
));
2384 /* only allow setting a subset of all code sign flags */
2386 CS_HARD
| CS_EXEC_SET_HARD
|
2387 CS_KILL
| CS_EXEC_SET_KILL
|
2390 CS_ENFORCEMENT
| CS_EXEC_SET_ENFORCEMENT
;
2393 if (pt
->p_csflags
& CS_VALID
) {
2394 pt
->p_csflags
|= flags
;
2402 case CS_OPS_CLEAR_LV
: {
2404 * This option is used to remove library validation from
2405 * a running process. This is used in plugin architectures
2406 * when a program needs to load untrusted libraries. This
2407 * allows the process to maintain library validation as
2408 * long as possible, then drop it only when required.
2409 * Once a process has loaded the untrusted library,
2410 * relying on library validation in the future will
2411 * not be effective. An alternative is to re-exec
2412 * your application without library validation, or
2413 * fork an untrusted child.
2415 #ifdef CONFIG_EMBEDDED
2416 // On embedded platforms, we don't support dropping LV
2420 * if we have the flag set, and the caller wants
2421 * to remove it, and they're entitled to, then
2422 * we remove it from the csflags
2424 * NOTE: We are fine to poke into the task because
2425 * we get a ref to pt when we do the proc_find
2426 * at the beginning of this function.
2428 * We also only allow altering ourselves.
2430 if (forself
== 1 && IOTaskHasEntitlement(pt
->task
, CLEAR_LV_ENTITLEMENT
)) {
2432 pt
->p_csflags
&= (~(CS_REQUIRE_LV
& CS_FORCED_LV
));
2446 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2452 error
= cs_blob_get(pt
, &start
, &length
);
2458 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2461 case CS_OPS_IDENTITY
:
2462 case CS_OPS_TEAMID
: {
2463 const char *identity
;
2464 uint8_t fakeheader
[8];
2469 * Make identity have a blob header to make it
2470 * easier on userland to guess the identity
2473 if (usize
< sizeof(fakeheader
)) {
2477 memset(fakeheader
, 0, sizeof(fakeheader
));
2480 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2486 identity
= ops
== CS_OPS_TEAMID
? csproc_get_teamid(pt
) : cs_identity_get(pt
);
2488 if (identity
== NULL
) {
2493 length
= strlen(identity
) + 1; /* include NUL */
2494 idlen
= htonl(length
+ sizeof(fakeheader
));
2495 memcpy(&fakeheader
[4], &idlen
, sizeof(idlen
));
2497 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
2502 if (usize
< sizeof(fakeheader
) + length
) {
2504 } else if (usize
> sizeof(fakeheader
)) {
2505 error
= copyout(identity
, uaddr
+ sizeof(fakeheader
), length
);
2511 case CS_OPS_CLEARINSTALLER
:
2513 pt
->p_csflags
&= ~(CS_INSTALLER
| CS_DATAVAULT_CONTROLLER
| CS_EXEC_INHERIT_SIP
);
2517 case CS_OPS_CLEARPLATFORM
:
2518 #if DEVELOPMENT || DEBUG
2519 if (cs_process_global_enforcement()) {
2525 if (csr_check(CSR_ALLOW_APPLE_INTERNAL
) != 0) {
2532 pt
->p_csflags
&= ~(CS_PLATFORM_BINARY
| CS_PLATFORM_PATH
);
2533 csproc_clear_platform_binary(pt
);
2539 #endif /* !DEVELOPMENT || DEBUG */
2553 proc_iterate_fn_t callout
,
2555 proc_iterate_fn_t filterfn
,
2558 pidlist_t pid_list
, *pl
= pidlist_init(&pid_list
);
2559 u_int pid_count_available
= 0;
2561 assert(callout
!= NULL
);
2563 /* allocate outside of the proc_list_lock */
2566 pid_count_available
= nprocs
+ 1; /* kernel_task not counted in nprocs */
2567 assert(pid_count_available
> 0);
2568 if (pidlist_nalloc(pl
) > pid_count_available
) {
2573 pidlist_alloc(pl
, pid_count_available
);
2575 pidlist_set_active(pl
);
2577 /* filter pids into the pid_list */
2579 u_int pid_count
= 0;
2580 if (flags
& PROC_ALLPROCLIST
) {
2582 ALLPROC_FOREACH(p
) {
2583 /* ignore processes that are being forked */
2584 if (p
->p_stat
== SIDL
) {
2587 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2590 pidlist_add_pid(pl
, proc_pid(p
));
2591 if (++pid_count
>= pid_count_available
) {
2597 if ((pid_count
< pid_count_available
) &&
2598 (flags
& PROC_ZOMBPROCLIST
)) {
2600 ZOMBPROC_FOREACH(p
) {
2601 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2604 pidlist_add_pid(pl
, proc_pid(p
));
2605 if (++pid_count
>= pid_count_available
) {
2613 /* call callout on processes in the pid_list */
2615 const pidlist_entry_t
*pe
;
2616 SLIST_FOREACH(pe
, &(pl
->pl_head
), pe_link
) {
2617 for (u_int i
= 0; i
< pe
->pe_nused
; i
++) {
2618 const pid_t pid
= pe
->pe_pid
[i
];
2619 proc_t p
= proc_find(pid
);
2621 if ((flags
& PROC_NOWAITTRANS
) == 0) {
2622 proc_transwait(p
, 0);
2624 const int callout_ret
= callout(p
, arg
);
2626 switch (callout_ret
) {
2627 case PROC_RETURNED_DONE
:
2630 case PROC_CLAIMED_DONE
:
2639 panic("%s: callout =%d for pid %d",
2640 __func__
, callout_ret
, pid
);
2643 } else if (flags
& PROC_ZOMBPROCLIST
) {
2644 p
= proc_find_zombref(pid
);
2648 const int callout_ret
= callout(p
, arg
);
2650 switch (callout_ret
) {
2651 case PROC_RETURNED_DONE
:
2652 proc_drop_zombref(p
);
2654 case PROC_CLAIMED_DONE
:
2658 proc_drop_zombref(p
);
2663 panic("%s: callout =%d for zombie %d",
2664 __func__
, callout_ret
, pid
);
2676 proc_iterate_fn_t callout
,
2678 proc_iterate_fn_t filterfn
,
2683 assert(callout
!= NULL
);
2685 proc_shutdown_exitcount
= 0;
2691 ALLPROC_FOREACH(p
) {
2692 if ((filterfn
!= NULL
) && filterfn(p
, filterarg
) == 0) {
2695 p
= proc_ref_locked(p
);
2702 proc_transwait(p
, 0);
2703 (void)callout(p
, arg
);
2706 goto restart_foreach
;
2715 proc_iterate_fn_t callout
,
2718 pidlist_t pid_list
, *pl
= pidlist_init(&pid_list
);
2719 u_int pid_count_available
= 0;
2721 assert(parent
!= NULL
);
2722 assert(callout
!= NULL
);
2726 pid_count_available
= parent
->p_childrencnt
;
2727 if (pid_count_available
== 0) {
2731 if (pidlist_nalloc(pl
) > pid_count_available
) {
2736 pidlist_alloc(pl
, pid_count_available
);
2738 pidlist_set_active(pl
);
2740 u_int pid_count
= 0;
2742 PCHILDREN_FOREACH(parent
, p
) {
2743 if (p
->p_stat
== SIDL
) {
2746 pidlist_add_pid(pl
, proc_pid(p
));
2747 if (++pid_count
>= pid_count_available
) {
2754 const pidlist_entry_t
*pe
;
2755 SLIST_FOREACH(pe
, &(pl
->pl_head
), pe_link
) {
2756 for (u_int i
= 0; i
< pe
->pe_nused
; i
++) {
2757 const pid_t pid
= pe
->pe_pid
[i
];
2762 const int callout_ret
= callout(p
, arg
);
2764 switch (callout_ret
) {
2765 case PROC_RETURNED_DONE
:
2768 case PROC_CLAIMED_DONE
:
2777 panic("%s: callout =%d for pid %d",
2778 __func__
, callout_ret
, pid
);
2791 proc_iterate_fn_t callout
,
2793 proc_iterate_fn_t filterfn
,
2796 pidlist_t pid_list
, *pl
= pidlist_init(&pid_list
);
2797 u_int pid_count_available
= 0;
2799 assert(pgrp
!= NULL
);
2800 assert(callout
!= NULL
);
2804 pid_count_available
= pgrp
->pg_membercnt
;
2805 if (pid_count_available
== 0) {
2807 if (flags
& PGRP_DROPREF
) {
2812 if (pidlist_nalloc(pl
) > pid_count_available
) {
2817 pidlist_alloc(pl
, pid_count_available
);
2819 pidlist_set_active(pl
);
2821 const pid_t pgid
= pgrp
->pg_id
;
2822 u_int pid_count
= 0;
2824 PGMEMBERS_FOREACH(pgrp
, p
) {
2825 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2828 pidlist_add_pid(pl
, proc_pid(p
));
2829 if (++pid_count
>= pid_count_available
) {
2836 if (flags
& PGRP_DROPREF
) {
2840 const pidlist_entry_t
*pe
;
2841 SLIST_FOREACH(pe
, &(pl
->pl_head
), pe_link
) {
2842 for (u_int i
= 0; i
< pe
->pe_nused
; i
++) {
2843 const pid_t pid
= pe
->pe_pid
[i
];
2845 continue; /* skip kernproc */
2851 if (p
->p_pgrpid
!= pgid
) {
2855 const int callout_ret
= callout(p
, arg
);
2857 switch (callout_ret
) {
2863 case PROC_RETURNED_DONE
:
2866 case PROC_CLAIMED_DONE
:
2870 panic("%s: callout =%d for pid %d",
2871 __func__
, callout_ret
, pid
);
2881 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2884 child
->p_pgrp
= pgrp
;
2885 child
->p_pgrpid
= pgrp
->pg_id
;
2886 child
->p_listflag
|= P_LIST_INPGRP
;
2888 * When pgrp is being freed , a process can still
2889 * request addition using setpgid from bash when
2890 * login is terminated (login cycler) return ESRCH
2891 * Safe to hold lock due to refcount on pgrp
2893 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2894 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2897 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
) {
2898 panic("pgrp_add : pgrp is dead adding process");
2903 pgrp
->pg_membercnt
++;
2904 if (parent
!= PROC_NULL
) {
2905 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2907 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2912 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2913 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2919 pgrp_remove(struct proc
* p
)
2926 #if __PROC_INTERNAL_DEBUG
2927 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0) {
2928 panic("removing from pglist but no named ref\n");
2931 p
->p_pgrpid
= PGRPID_DEAD
;
2932 p
->p_listflag
&= ~P_LIST_INPGRP
;
2936 if (pg
== PGRP_NULL
) {
2937 panic("pgrp_remove: pg is NULL");
2942 if (pg
->pg_membercnt
< 0) {
2943 panic("pgprp: -ve membercnt pgprp:%p p:%p\n", pg
, p
);
2946 LIST_REMOVE(p
, p_pglist
);
2947 if (pg
->pg_members
.lh_first
== 0) {
2949 pgdelete_dropref(pg
);
2957 /* cannot use proc_pgrp as it maybe stalled */
2959 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2961 struct pgrp
* oldpg
;
2967 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2968 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2969 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2972 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2975 if (oldpg
== PGRP_NULL
) {
2976 panic("pgrp_replace: oldpg NULL");
2978 oldpg
->pg_refcount
++;
2979 #if __PROC_INTERNAL_DEBUG
2980 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0) {
2981 panic("removing from pglist but no named ref\n");
2984 p
->p_pgrpid
= PGRPID_DEAD
;
2985 p
->p_listflag
&= ~P_LIST_INPGRP
;
2991 oldpg
->pg_membercnt
--;
2992 if (oldpg
->pg_membercnt
< 0) {
2993 panic("pgprp: -ve membercnt pgprp:%p p:%p\n", oldpg
, p
);
2995 LIST_REMOVE(p
, p_pglist
);
2996 if (oldpg
->pg_members
.lh_first
== 0) {
2998 pgdelete_dropref(oldpg
);
3006 p
->p_pgrpid
= newpg
->pg_id
;
3007 p
->p_listflag
|= P_LIST_INPGRP
;
3009 * When pgrp is being freed , a process can still
3010 * request addition using setpgid from bash when
3011 * login is terminated (login cycler) return ESRCH
3012 * Safe to hold lock due to refcount on pgrp
3014 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
3015 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
3018 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
) {
3019 panic("pgrp_add : pgrp is dead adding process");
3024 newpg
->pg_membercnt
++;
3025 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
3029 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
3030 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
3033 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
3034 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
3035 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
3036 wakeup(&p
->p_pgrpid
);
3042 pgrp_lock(struct pgrp
* pgrp
)
3044 lck_mtx_lock(&pgrp
->pg_mlock
);
3048 pgrp_unlock(struct pgrp
* pgrp
)
3050 lck_mtx_unlock(&pgrp
->pg_mlock
);
3054 session_lock(struct session
* sess
)
3056 lck_mtx_lock(&sess
->s_mlock
);
3061 session_unlock(struct session
* sess
)
3063 lck_mtx_unlock(&sess
->s_mlock
);
3071 if (p
== PROC_NULL
) {
3076 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
3077 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
3078 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
3083 assert(pgrp
!= NULL
);
3085 if (pgrp
!= PGRP_NULL
) {
3086 pgrp
->pg_refcount
++;
3087 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0) {
3088 panic("proc_pgrp: ref being povided for dead pgrp");
3098 tty_pgrp(struct tty
* tp
)
3100 struct pgrp
* pg
= PGRP_NULL
;
3105 if (pg
!= PGRP_NULL
) {
3106 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0) {
3107 panic("tty_pgrp: ref being povided for dead pgrp");
3117 proc_session(proc_t p
)
3119 struct session
* sess
= SESSION_NULL
;
3121 if (p
== PROC_NULL
) {
3122 return SESSION_NULL
;
3127 /* wait during transitions */
3128 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
3129 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
3130 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
3133 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
3134 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0) {
3135 panic("proc_session:returning sesssion ref on terminating session");
3144 session_rele(struct session
*sess
)
3147 if (--sess
->s_count
== 0) {
3148 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0) {
3149 panic("session_rele: terminating already terminated session");
3151 sess
->s_listflags
|= S_LIST_TERM
;
3152 LIST_REMOVE(sess
, s_hash
);
3153 sess
->s_listflags
|= S_LIST_DEAD
;
3154 if (sess
->s_count
!= 0) {
3155 panic("session_rele: freeing session in use");
3158 lck_mtx_destroy(&sess
->s_mlock
, proc_mlock_grp
);
3159 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
3166 proc_transstart(proc_t p
, int locked
, int non_blocking
)
3171 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
3172 if (((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
) || non_blocking
) {
3178 p
->p_lflag
|= P_LTRANSWAIT
;
3179 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
3181 p
->p_lflag
|= P_LINTRANSIT
;
3182 p
->p_transholder
= current_thread();
3190 proc_transcommit(proc_t p
, int locked
)
3196 assert((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
);
3197 assert(p
->p_transholder
== current_thread());
3198 p
->p_lflag
|= P_LTRANSCOMMIT
;
3200 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
3201 p
->p_lflag
&= ~P_LTRANSWAIT
;
3202 wakeup(&p
->p_lflag
);
3210 proc_transend(proc_t p
, int locked
)
3216 p
->p_lflag
&= ~(P_LINTRANSIT
| P_LTRANSCOMMIT
);
3217 p
->p_transholder
= NULL
;
3219 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
3220 p
->p_lflag
&= ~P_LTRANSWAIT
;
3221 wakeup(&p
->p_lflag
);
3229 proc_transwait(proc_t p
, int locked
)
3234 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
3235 if ((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
&& current_proc() == p
) {
3241 p
->p_lflag
|= P_LTRANSWAIT
;
3242 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
3251 proc_klist_lock(void)
3253 lck_mtx_lock(proc_klist_mlock
);
3257 proc_klist_unlock(void)
3259 lck_mtx_unlock(proc_klist_mlock
);
3263 proc_knote(struct proc
* p
, long hint
)
3266 KNOTE(&p
->p_klist
, hint
);
3267 proc_klist_unlock();
3271 proc_knote_drain(struct proc
*p
)
3273 struct knote
*kn
= NULL
;
3276 * Clear the proc's klist to avoid references after the proc is reaped.
3279 while ((kn
= SLIST_FIRST(&p
->p_klist
))) {
3280 kn
->kn_proc
= PROC_NULL
;
3281 KNOTE_DETACH(&p
->p_klist
, kn
);
3283 proc_klist_unlock();
3287 proc_setregister(proc_t p
)
3290 p
->p_lflag
|= P_LREGISTER
;
3295 proc_resetregister(proc_t p
)
3298 p
->p_lflag
&= ~P_LREGISTER
;
3303 proc_pgrpid(proc_t p
)
3309 proc_sessionid(proc_t p
)
3312 struct session
* sessp
= proc_session(p
);
3314 if (sessp
!= SESSION_NULL
) {
3316 session_rele(sessp
);
3325 return current_proc()->p_pgrpid
;
3329 /* return control and action states */
3331 proc_getpcontrol(int pid
, int * pcontrolp
)
3336 if (p
== PROC_NULL
) {
3339 if (pcontrolp
!= NULL
) {
3340 *pcontrolp
= p
->p_pcaction
;
3348 proc_dopcontrol(proc_t p
)
3351 os_reason_t kill_reason
;
3355 pcontrol
= PROC_CONTROL_STATE(p
);
3357 if (PROC_ACTION_STATE(p
) == 0) {
3360 PROC_SETACTION_STATE(p
);
3362 printf("low swap: throttling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3366 PROC_SETACTION_STATE(p
);
3368 printf("low swap: suspending pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3369 task_suspend(p
->task
);
3373 PROC_SETACTION_STATE(p
);
3375 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3376 kill_reason
= os_reason_create(OS_REASON_JETSAM
, JETSAM_REASON_LOWSWAP
);
3377 psignal_with_reason(p
, SIGKILL
, kill_reason
);
3387 return PROC_RETURNED
;
3392 * Resume a throttled or suspended process. This is an internal interface that's only
3393 * used by the user level code that presents the GUI when we run out of swap space and
3394 * hence is restricted to processes with superuser privileges.
3398 proc_resetpcontrol(int pid
)
3403 proc_t self
= current_proc();
3405 /* if the process has been validated to handle resource control or root is valid one */
3406 if (((self
->p_lflag
& P_LVMRSRCOWNER
) == 0) && (error
= suser(kauth_cred_get(), 0))) {
3411 if (p
== PROC_NULL
) {
3417 pcontrol
= PROC_CONTROL_STATE(p
);
3419 if (PROC_ACTION_STATE(p
) != 0) {
3422 PROC_RESETACTION_STATE(p
);
3424 printf("low swap: unthrottling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3428 PROC_RESETACTION_STATE(p
);
3430 printf("low swap: resuming pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3431 task_resume(p
->task
);
3436 PROC_SETACTION_STATE(p
);
3438 printf("low swap: attempt to unkill pid %d (%s) ignored\n", p
->p_pid
, p
->p_comm
);
3454 struct no_paging_space
{
3455 uint64_t pcs_max_size
;
3456 uint64_t pcs_uniqueid
;
3459 uint64_t pcs_total_size
;
3461 uint64_t npcs_max_size
;
3462 uint64_t npcs_uniqueid
;
3464 int npcs_proc_count
;
3465 uint64_t npcs_total_size
;
3467 int apcs_proc_count
;
3468 uint64_t apcs_total_size
;
3473 proc_pcontrol_filter(proc_t p
, void *arg
)
3475 struct no_paging_space
*nps
;
3476 uint64_t compressed
;
3478 nps
= (struct no_paging_space
*)arg
;
3480 compressed
= get_task_compressed(p
->task
);
3482 if (PROC_CONTROL_STATE(p
)) {
3483 if (PROC_ACTION_STATE(p
) == 0) {
3484 if (compressed
> nps
->pcs_max_size
) {
3485 nps
->pcs_pid
= p
->p_pid
;
3486 nps
->pcs_uniqueid
= p
->p_uniqueid
;
3487 nps
->pcs_max_size
= compressed
;
3489 nps
->pcs_total_size
+= compressed
;
3490 nps
->pcs_proc_count
++;
3492 nps
->apcs_total_size
+= compressed
;
3493 nps
->apcs_proc_count
++;
3496 if (compressed
> nps
->npcs_max_size
) {
3497 nps
->npcs_pid
= p
->p_pid
;
3498 nps
->npcs_uniqueid
= p
->p_uniqueid
;
3499 nps
->npcs_max_size
= compressed
;
3501 nps
->npcs_total_size
+= compressed
;
3502 nps
->npcs_proc_count
++;
3509 proc_pcontrol_null(__unused proc_t p
, __unused
void *arg
)
3511 return PROC_RETURNED
;
3516 * Deal with the low on compressor pool space condition... this function
3517 * gets called when we are approaching the limits of the compressor pool or
3518 * we are unable to create a new swap file.
3519 * Since this eventually creates a memory deadlock situtation, we need to take action to free up
3520 * memory resources (both compressed and uncompressed) in order to prevent the system from hanging completely.
3521 * There are 2 categories of processes to deal with. Those that have an action
3522 * associated with them by the task itself and those that do not. Actionable
3523 * tasks can have one of three categories specified: ones that
3524 * can be killed immediately, ones that should be suspended, and ones that should
3525 * be throttled. Processes that do not have an action associated with them are normally
3526 * ignored unless they are utilizing such a large percentage of the compressor pool (currently 50%)
3527 * that only by killing them can we hope to put the system back into a usable state.
3530 #define NO_PAGING_SPACE_DEBUG 0
3532 extern uint64_t vm_compressor_pages_compressed(void);
3534 struct timeval last_no_space_action
= {.tv_sec
= 0, .tv_usec
= 0};
3536 #if DEVELOPMENT || DEBUG
3537 extern boolean_t kill_on_no_paging_space
;
3538 #endif /* DEVELOPMENT || DEBUG */
3540 #define MB_SIZE (1024 * 1024ULL)
3541 boolean_t
memorystatus_kill_on_VM_compressor_space_shortage(boolean_t
);
3543 extern int32_t max_kill_priority
;
3544 extern int memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index
);
3547 no_paging_space_action()
3550 struct no_paging_space nps
;
3552 os_reason_t kill_reason
;
3555 * Throttle how often we come through here. Once every 5 seconds should be plenty.
3559 if (now
.tv_sec
<= last_no_space_action
.tv_sec
+ 5) {
3564 * Examine all processes and find the biggest (biggest is based on the number of pages this
3565 * task has in the compressor pool) that has been marked to have some action
3566 * taken when swap space runs out... we also find the biggest that hasn't been marked for
3569 * If the biggest non-actionable task is over the "dangerously big" threashold (currently 50% of
3570 * the total number of pages held by the compressor, we go ahead and kill it since no other task
3571 * can have any real effect on the situation. Otherwise, we go after the actionable process.
3573 bzero(&nps
, sizeof(nps
));
3575 proc_iterate(PROC_ALLPROCLIST
, proc_pcontrol_null
, (void *)NULL
, proc_pcontrol_filter
, (void *)&nps
);
3577 #if NO_PAGING_SPACE_DEBUG
3578 printf("low swap: npcs_proc_count = %d, npcs_total_size = %qd, npcs_max_size = %qd\n",
3579 nps
.npcs_proc_count
, nps
.npcs_total_size
, nps
.npcs_max_size
);
3580 printf("low swap: pcs_proc_count = %d, pcs_total_size = %qd, pcs_max_size = %qd\n",
3581 nps
.pcs_proc_count
, nps
.pcs_total_size
, nps
.pcs_max_size
);
3582 printf("low swap: apcs_proc_count = %d, apcs_total_size = %qd\n",
3583 nps
.apcs_proc_count
, nps
.apcs_total_size
);
3585 if (nps
.npcs_max_size
> (vm_compressor_pages_compressed() * 50) / 100) {
3587 * for now we'll knock out any task that has more then 50% of the pages
3588 * held by the compressor
3590 if ((p
= proc_find(nps
.npcs_pid
)) != PROC_NULL
) {
3591 if (nps
.npcs_uniqueid
== p
->p_uniqueid
) {
3593 * verify this is still the same process
3594 * in case the proc exited and the pid got reused while
3595 * we were finishing the proc_iterate and getting to this point
3597 last_no_space_action
= now
;
3599 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
));
3600 kill_reason
= os_reason_create(OS_REASON_JETSAM
, JETSAM_REASON_LOWSWAP
);
3601 psignal_with_reason(p
, SIGKILL
, kill_reason
);
3613 * We have some processes within our jetsam bands of consideration and hence can be killed.
3614 * So we will invoke the memorystatus thread to go ahead and kill something.
3616 if (memorystatus_get_proccnt_upto_priority(max_kill_priority
) > 0) {
3617 last_no_space_action
= now
;
3618 memorystatus_kill_on_VM_compressor_space_shortage(TRUE
/* async */);
3623 * No eligible processes to kill. So let's suspend/kill the largest
3624 * process depending on its policy control specifications.
3627 if (nps
.pcs_max_size
> 0) {
3628 if ((p
= proc_find(nps
.pcs_pid
)) != PROC_NULL
) {
3629 if (nps
.pcs_uniqueid
== p
->p_uniqueid
) {
3631 * verify this is still the same process
3632 * in case the proc exited and the pid got reused while
3633 * we were finishing the proc_iterate and getting to this point
3635 last_no_space_action
= now
;
3647 last_no_space_action
= now
;
3649 printf("low swap: unable to find any eligible processes to take action on\n");
3655 proc_trace_log(__unused proc_t p
, struct proc_trace_log_args
*uap
, __unused
int *retval
)
3658 proc_t target_proc
= PROC_NULL
;
3659 pid_t target_pid
= uap
->pid
;
3660 uint64_t target_uniqueid
= uap
->uniqueid
;
3661 task_t target_task
= NULL
;
3663 if (priv_check_cred(kauth_cred_get(), PRIV_PROC_TRACE_INSPECT
, 0)) {
3667 target_proc
= proc_find(target_pid
);
3668 if (target_proc
!= PROC_NULL
) {
3669 if (target_uniqueid
!= proc_uniqueid(target_proc
)) {
3674 target_task
= proc_task(target_proc
);
3675 if (task_send_trace_memory(target_task
, target_pid
, target_uniqueid
)) {
3684 if (target_proc
!= PROC_NULL
) {
3685 proc_rele(target_proc
);
3690 #if VM_SCAN_FOR_SHADOW_CHAIN
3691 extern int vm_map_shadow_max(vm_map_t map
);
3692 int proc_shadow_max(void);
3694 proc_shadow_max(void)
3703 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
3704 if (p
->p_stat
== SIDL
) {
3711 map
= get_task_map(task
);
3715 retval
= vm_map_shadow_max(map
);
3723 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */
3725 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
);
3727 proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
)
3729 if (target_proc
!= NULL
) {
3730 target_proc
->p_responsible_pid
= responsible_pid
;
3736 proc_chrooted(proc_t p
)
3742 retval
= (p
->p_fd
->fd_rdir
!= NULL
) ? 1 : 0;
3750 proc_send_synchronous_EXC_RESOURCE(proc_t p
)
3752 if (p
== PROC_NULL
) {
3756 /* Send sync EXC_RESOURCE if the process is traced */
3757 if (ISSET(p
->p_lflag
, P_LTRACED
)) {
3764 proc_get_syscall_filter_mask_size(int which
)
3766 if (which
== SYSCALL_MASK_UNIX
) {
3774 proc_set_syscall_filter_mask(proc_t p
, int which
, unsigned char *maskptr
, size_t masklen
)
3776 #if DEVELOPMENT || DEBUG
3777 if (syscallfilter_disable
) {
3778 printf("proc_set_syscall_filter_mask: attempt to set policy for pid %d, but disabled by boot-arg\n", proc_pid(p
));
3779 return KERN_SUCCESS
;
3781 #endif // DEVELOPMENT || DEBUG
3783 if (which
!= SYSCALL_MASK_UNIX
||
3784 (maskptr
!= NULL
&& masklen
!= nsysent
)) {
3788 p
->syscall_filter_mask
= maskptr
;
3790 return KERN_SUCCESS
;
3793 #ifdef CONFIG_32BIT_TELEMETRY
3795 proc_log_32bit_telemetry(proc_t p
)
3798 char signature_buf
[MAX_32BIT_EXEC_SIG_SIZE
] = { 0 };
3799 char * signature_cur_end
= &signature_buf
[0];
3800 char * signature_buf_end
= &signature_buf
[MAX_32BIT_EXEC_SIG_SIZE
- 1];
3801 int bytes_printed
= 0;
3803 const char * teamid
= NULL
;
3804 const char * identity
= NULL
;
3805 struct cs_blob
* csblob
= NULL
;
3810 * Get proc name and parent proc name; if the parent execs, we'll get a
3813 bytes_printed
= scnprintf(signature_cur_end
,
3814 signature_buf_end
- signature_cur_end
,
3815 "%s,%s,", p
->p_name
,
3816 (p
->p_pptr
? p
->p_pptr
->p_name
: ""));
3818 if (bytes_printed
> 0) {
3819 signature_cur_end
+= bytes_printed
;
3824 /* Get developer info. */
3825 vnode_t v
= proc_getexecutablevnode(p
);
3828 csblob
= csvnode_get_blob(v
, 0);
3831 teamid
= csblob_get_teamid(csblob
);
3832 identity
= csblob_get_identity(csblob
);
3836 if (teamid
== NULL
) {
3840 if (identity
== NULL
) {
3844 bytes_printed
= scnprintf(signature_cur_end
,
3845 signature_buf_end
- signature_cur_end
,
3846 "%s,%s", teamid
, identity
);
3848 if (bytes_printed
> 0) {
3849 signature_cur_end
+= bytes_printed
;
3857 * We may want to rate limit here, although the SUMMARIZE key should
3858 * help us aggregate events in userspace.
3862 kern_asl_msg(LOG_DEBUG
, "messagetracer", 3,
3863 /* 0 */ "com.apple.message.domain", "com.apple.kernel.32bit_exec",
3864 /* 1 */ "com.apple.message.signature", signature_buf
,
3865 /* 2 */ "com.apple.message.summarize", "YES",
3868 #endif /* CONFIG_32BIT_TELEMETRY */