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>
115 #ifdef CONFIG_32BIT_TELEMETRY
116 #include <sys/kasl.h>
117 #endif /* CONFIG_32BIT_TELEMETRY */
123 #if CONFIG_MEMORYSTATUS
124 #include <sys/kern_memorystatus.h>
128 #include <security/mac_framework.h>
131 #include <libkern/crypto/sha1.h>
133 #ifdef CONFIG_32BIT_TELEMETRY
134 #define MAX_32BIT_EXEC_SIG_SIZE 160
135 #endif /* CONFIG_32BIT_TELEMETRY */
138 * Structure associated with user cacheing.
141 LIST_ENTRY(uidinfo
) ui_hash
;
145 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
146 LIST_HEAD(uihashhead
, uidinfo
) *uihashtbl
;
147 u_long uihash
; /* size of hash table - 1 */
150 * Other process lists
152 struct pidhashhead
*pidhashtbl
;
154 struct pgrphashhead
*pgrphashtbl
;
156 struct sesshashhead
*sesshashtbl
;
159 struct proclist allproc
;
160 struct proclist zombproc
;
161 extern struct tty cons
;
165 #if DEVELOPMENT || DEBUG
166 extern int cs_enforcement_enable
;
170 #define __PROC_INTERNAL_DEBUG 1
173 /* Name to give to core files */
174 #if defined(XNU_TARGET_OS_BRIDGE)
175 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/private/var/internal/%N.core"};
176 #elif CONFIG_EMBEDDED
177 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/private/var/cores/%N.core"};
179 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/cores/core.%P"};
184 #include <kern/backtrace.h>
187 typedef uint64_t unaligned_u64
__attribute__((aligned(1)));
189 static void orphanpg(struct pgrp
* pg
);
190 void proc_name_kdp(task_t t
, char * buf
, int size
);
191 void * proc_get_uthread_uu_threadlist(void * uthread_v
);
192 int proc_threadname_kdp(void * uth
, char * buf
, size_t size
);
193 void proc_starttime_kdp(void * p
, unaligned_u64
*tv_sec
, unaligned_u64
*tv_usec
, unaligned_u64
*abstime
);
194 char * proc_name_address(void * p
);
196 /* TODO: make a header that's exported and usable in osfmk */
197 char* proc_best_name(proc_t 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 *);
215 uint64_t get_current_unique_pid(void);
219 get_current_unique_pid(void)
221 proc_t p
= current_proc();
224 return p
->p_uniqueid
;
230 * Initialize global process hashing structures.
236 LIST_INIT(&zombproc
);
237 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
238 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
239 sesshashtbl
= hashinit(maxproc
/ 4, M_PROC
, &sesshash
);
240 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
242 personas_bootstrap();
247 * Change the count associated with number of processes
248 * a given user is using. This routine protects the uihash
252 chgproccnt(uid_t uid
, int diff
)
255 struct uidinfo
*newuip
= NULL
;
256 struct uihashhead
*uipp
;
262 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
)
263 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");
274 LIST_REMOVE(uip
, ui_hash
);
277 FREE_ZONE(uip
, sizeof(*uip
), M_PROC
);
286 panic("chgproccnt: lost user");
288 if (newuip
!= NULL
) {
291 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
293 uip
->ui_proccnt
= diff
;
299 MALLOC_ZONE(newuip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
301 panic("chgproccnt: M_PROC zone depleted");
305 FREE_ZONE(newuip
, sizeof(*uip
), M_PROC
);
310 * Is p an inferior of the current process?
318 for (; p
!= current_proc(); p
= p
->p_pptr
)
328 * Is p an inferior of t ?
331 isinferior(proc_t p
, proc_t t
)
337 /* if p==t they are not inferior */
342 for (; p
!= t
; p
= p
->p_pptr
) {
345 /* Detect here if we're in a cycle */
346 if ((p
->p_pid
== 0) || (p
->p_pptr
== start
) || (nchecked
>= nprocs
))
356 proc_isinferior(int pid1
, int pid2
)
358 proc_t p
= PROC_NULL
;
359 proc_t t
= PROC_NULL
;
362 if (((p
= proc_find(pid1
)) != (proc_t
)0 ) && ((t
= proc_find(pid2
)) != (proc_t
)0))
363 retval
= isinferior(p
, t
);
376 return(proc_findinternal(pid
, 0));
380 proc_findinternal(int pid
, int locked
)
382 proc_t p
= PROC_NULL
;
388 p
= pfind_locked(pid
);
389 if ((p
== PROC_NULL
) || (p
!= proc_ref_locked(p
)))
400 proc_findthread(thread_t thread
)
402 proc_t p
= PROC_NULL
;
406 uth
= get_bsdthread_info(thread
);
407 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
410 p
= (proc_t
)(get_bsdthreadtask_info(thread
));
411 p
= proc_ref_locked(p
);
417 uthread_reset_proc_refcount(void *uthread
) {
420 uth
= (uthread_t
) uthread
;
421 uth
->uu_proc_refcount
= 0;
424 if (proc_ref_tracking_disabled
) {
434 uthread_get_proc_refcount(void *uthread
) {
437 if (proc_ref_tracking_disabled
) {
441 uth
= (uthread_t
) uthread
;
443 return uth
->uu_proc_refcount
;
448 record_procref(proc_t p __unused
, int count
) {
451 uth
= current_uthread();
452 uth
->uu_proc_refcount
+= count
;
455 if (proc_ref_tracking_disabled
) {
460 if (uth
->uu_pindex
< NUM_PROC_REFS_TO_TRACK
) {
461 backtrace((uintptr_t *) &uth
->uu_proc_pcs
[uth
->uu_pindex
], PROC_REF_STACK_DEPTH
);
463 uth
->uu_proc_ps
[uth
->uu_pindex
] = p
;
471 uthread_needs_to_wait_in_proc_refwait(void) {
472 uthread_t uth
= current_uthread();
475 * Allow threads holding no proc refs to wait
476 * in proc_refwait, allowing threads holding
477 * proc refs to wait in proc_refwait causes
478 * deadlocks and makes proc_find non-reentrant.
480 if (uth
->uu_proc_refcount
== 0)
504 if (p
!= proc_ref_locked(p
))
512 proc_ref_locked(proc_t p
)
515 int pid
= proc_pid(p
);
519 * if process still in creation or proc got recycled
520 * during msleep then return failure.
522 if ((p
== PROC_NULL
) || (p1
!= p
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0))
526 * Do not return process marked for termination
527 * or proc_refdrain called without ref wait.
528 * Wait for proc_refdrain_with_refwait to complete if
529 * process in refdrain and refwait flag is set, unless
530 * the current thread is holding to a proc_ref
533 if ((p
->p_stat
!= SZOMB
) &&
534 ((p
->p_listflag
& P_LIST_EXITED
) == 0) &&
535 ((p
->p_listflag
& P_LIST_DEAD
) == 0) &&
536 (((p
->p_listflag
& (P_LIST_DRAIN
| P_LIST_DRAINWAIT
)) == 0) ||
537 ((p
->p_listflag
& P_LIST_REFWAIT
) != 0))) {
538 if ((p
->p_listflag
& P_LIST_REFWAIT
) != 0 && uthread_needs_to_wait_in_proc_refwait()) {
539 msleep(&p
->p_listflag
, proc_list_mlock
, 0, "proc_refwait", 0) ;
541 * the proc might have been recycled since we dropped
542 * the proc list lock, get the proc again.
544 p
= pfind_locked(pid
);
548 record_procref(p
, 1);
557 proc_rele_locked(proc_t p
)
560 if (p
->p_refcount
> 0) {
562 record_procref(p
, -1);
563 if ((p
->p_refcount
== 0) && ((p
->p_listflag
& P_LIST_DRAINWAIT
) == P_LIST_DRAINWAIT
)) {
564 p
->p_listflag
&= ~P_LIST_DRAINWAIT
;
565 wakeup(&p
->p_refcount
);
568 panic("proc_rele_locked -ve ref\n");
573 proc_find_zombref(int pid
)
580 p
= pfind_locked(pid
);
582 /* should we bail? */
583 if ((p
== PROC_NULL
) /* not found */
584 || ((p
->p_listflag
& P_LIST_INCREATE
) != 0) /* not created yet */
585 || ((p
->p_listflag
& P_LIST_EXITED
) == 0)) { /* not started exit */
591 /* If someone else is controlling the (unreaped) zombie - wait */
592 if ((p
->p_listflag
& P_LIST_WAITING
) != 0) {
593 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
596 p
->p_listflag
|= P_LIST_WAITING
;
604 proc_drop_zombref(proc_t p
)
607 if ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
608 p
->p_listflag
&= ~P_LIST_WAITING
;
616 proc_refdrain(proc_t p
)
618 proc_refdrain_with_refwait(p
, FALSE
);
622 proc_refdrain_with_refwait(proc_t p
, boolean_t get_ref_and_allow_wait
)
624 boolean_t initexec
= FALSE
;
627 p
->p_listflag
|= P_LIST_DRAIN
;
628 if (get_ref_and_allow_wait
) {
630 * All the calls to proc_ref_locked will wait
631 * for the flag to get cleared before returning a ref,
632 * unless the current thread is holding to a proc ref
635 p
->p_listflag
|= P_LIST_REFWAIT
;
641 /* Do not wait in ref drain for launchd exec */
642 while (p
->p_refcount
&& !initexec
) {
643 p
->p_listflag
|= P_LIST_DRAINWAIT
;
644 msleep(&p
->p_refcount
, proc_list_mlock
, 0, "proc_refdrain", 0) ;
647 p
->p_listflag
&= ~P_LIST_DRAIN
;
648 if (!get_ref_and_allow_wait
) {
649 p
->p_listflag
|= P_LIST_DEAD
;
651 /* Return a ref to the caller */
653 record_procref(p
, 1);
658 if (get_ref_and_allow_wait
) {
665 proc_refwake(proc_t p
)
668 p
->p_listflag
&= ~P_LIST_REFWAIT
;
669 wakeup(&p
->p_listflag
);
674 proc_parentholdref(proc_t p
)
676 proc_t parent
= PROC_NULL
;
684 if ((pp
== PROC_NULL
) || (pp
->p_stat
== SZOMB
) || ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
))) {
689 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == P_LIST_CHILDDRSTART
) {
690 pp
->p_listflag
|= P_LIST_CHILDDRWAIT
;
691 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
700 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == 0) {
711 proc_parentdropref(proc_t p
, int listlocked
)
716 if (p
->p_parentref
> 0) {
718 if ((p
->p_parentref
== 0) && ((p
->p_listflag
& P_LIST_PARENTREFWAIT
) == P_LIST_PARENTREFWAIT
)) {
719 p
->p_listflag
&= ~P_LIST_PARENTREFWAIT
;
720 wakeup(&p
->p_parentref
);
723 panic("proc_parentdropref -ve ref\n");
731 proc_childdrainstart(proc_t p
)
733 #if __PROC_INTERNAL_DEBUG
734 if ((p
->p_listflag
& P_LIST_CHILDDRSTART
) == P_LIST_CHILDDRSTART
)
735 panic("proc_childdrainstart: childdrain already started\n");
737 p
->p_listflag
|= P_LIST_CHILDDRSTART
;
738 /* wait for all that hold parentrefs to drop */
739 while (p
->p_parentref
> 0) {
740 p
->p_listflag
|= P_LIST_PARENTREFWAIT
;
741 msleep(&p
->p_parentref
, proc_list_mlock
, 0, "proc_childdrainstart", 0) ;
747 proc_childdrainend(proc_t p
)
749 #if __PROC_INTERNAL_DEBUG
750 if (p
->p_childrencnt
> 0)
751 panic("exiting: children stil hanging around\n");
753 p
->p_listflag
|= P_LIST_CHILDDRAINED
;
754 if ((p
->p_listflag
& (P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
)) != 0) {
755 p
->p_listflag
&= ~(P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
);
756 wakeup(&p
->p_childrencnt
);
761 proc_checkdeadrefs(__unused proc_t p
)
763 #if __PROC_INTERNAL_DEBUG
764 if ((p
->p_listflag
& P_LIST_INHASH
) != 0)
765 panic("proc being freed and still in hash %p: %u\n", p
, p
->p_listflag
);
766 if (p
->p_childrencnt
!= 0)
767 panic("proc being freed and pending children cnt %p:%d\n", p
, p
->p_childrencnt
);
768 if (p
->p_refcount
!= 0)
769 panic("proc being freed and pending refcount %p:%d\n", p
, p
->p_refcount
);
770 if (p
->p_parentref
!= 0)
771 panic("proc being freed and pending parentrefs %p:%d\n", p
, p
->p_parentref
);
794 return (current_proc()->p_pid
);
800 return (current_proc()->p_ppid
);
804 proc_selfcsflags(void)
806 return (current_proc()->p_csflags
);
811 dtrace_current_proc_vforking(void)
813 thread_t th
= current_thread();
814 struct uthread
*ut
= get_bsdthread_info(th
);
817 ((ut
->uu_flag
& (UT_VFORK
|UT_VFORKING
)) == (UT_VFORK
|UT_VFORKING
))) {
819 * Handle the narrow window where we're in the vfork syscall,
820 * but we're not quite ready to claim (in particular, to DTrace)
821 * that we're running as the child.
823 return (get_bsdtask_info(get_threadtask(th
)));
825 return (current_proc());
829 dtrace_proc_selfpid(void)
831 return (dtrace_current_proc_vforking()->p_pid
);
835 dtrace_proc_selfppid(void)
837 return (dtrace_current_proc_vforking()->p_ppid
);
841 dtrace_proc_selfruid(void)
843 return (dtrace_current_proc_vforking()->p_ruid
);
845 #endif /* CONFIG_DTRACE */
848 proc_parent(proc_t p
)
856 parent
= proc_ref_locked(pp
);
857 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)){
858 pp
->p_listflag
|= P_LIST_CHILDLKWAIT
;
859 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
867 proc_parent_is_currentproc(proc_t p
)
869 boolean_t ret
= FALSE
;
872 if (p
->p_pptr
== current_proc())
880 proc_name(int pid
, char * buf
, int size
)
884 if ((p
= proc_find(pid
)) != PROC_NULL
) {
885 strlcpy(buf
, &p
->p_comm
[0], size
);
891 proc_name_kdp(task_t t
, char * buf
, int size
)
893 proc_t p
= get_bsdtask_info(t
);
897 if ((size_t)size
> sizeof(p
->p_comm
))
898 strlcpy(buf
, &p
->p_name
[0], MIN((int)sizeof(p
->p_name
), size
));
900 strlcpy(buf
, &p
->p_comm
[0], MIN((int)sizeof(p
->p_comm
), size
));
904 proc_threadname_kdp(void * uth
, char * buf
, size_t size
)
906 if (size
< MAXTHREADNAMESIZE
) {
907 /* this is really just a protective measure for the future in
908 * case the thread name size in stackshot gets out of sync with
909 * the BSD max thread name size. Note that bsd_getthreadname
910 * doesn't take input buffer size into account. */
915 bsd_getthreadname(uth
, buf
);
921 /* note that this function is generally going to be called from stackshot,
922 * and the arguments will be coming from a struct which is declared packed
923 * thus the input arguments will in general be unaligned. We have to handle
926 proc_starttime_kdp(void *p
, unaligned_u64
*tv_sec
, unaligned_u64
*tv_usec
, unaligned_u64
*abstime
)
928 proc_t pp
= (proc_t
)p
;
929 if (pp
!= PROC_NULL
) {
931 *tv_sec
= pp
->p_start
.tv_sec
;
933 *tv_usec
= pp
->p_start
.tv_usec
;
934 if (abstime
!= NULL
) {
935 if (pp
->p_stats
!= NULL
)
936 *abstime
= pp
->p_stats
->ps_start
;
944 proc_name_address(void *p
)
946 return &((proc_t
)p
)->p_comm
[0];
950 proc_best_name(proc_t p
)
952 if (p
->p_name
[0] != 0)
953 return (&p
->p_name
[0]);
954 return (&p
->p_comm
[0]);
958 proc_selfname(char * buf
, int size
)
962 if ((p
= current_proc())!= (proc_t
)0) {
963 strlcpy(buf
, &p
->p_comm
[0], size
);
968 proc_signal(int pid
, int signum
)
972 if ((p
= proc_find(pid
)) != PROC_NULL
) {
979 proc_issignal(int pid
, sigset_t mask
)
984 if ((p
= proc_find(pid
)) != PROC_NULL
) {
985 error
= proc_pendingsignals(p
, mask
);
993 proc_noremotehang(proc_t p
)
998 retval
= p
->p_flag
& P_NOREMOTEHANG
;
999 return(retval
? 1: 0);
1004 proc_exiting(proc_t p
)
1009 retval
= p
->p_lflag
& P_LEXIT
;
1010 return(retval
? 1: 0);
1014 proc_forcequota(proc_t p
)
1019 retval
= p
->p_flag
& P_FORCEQUOTA
;
1020 return(retval
? 1: 0);
1025 proc_suser(proc_t p
)
1027 kauth_cred_t my_cred
;
1030 my_cred
= kauth_cred_proc_ref(p
);
1031 error
= suser(my_cred
, &p
->p_acflag
);
1032 kauth_cred_unref(&my_cred
);
1037 proc_task(proc_t proc
)
1039 return (task_t
)proc
->task
;
1043 * Obtain the first thread in a process
1045 * XXX This is a bad thing to do; it exists predominantly to support the
1046 * XXX use of proc_t's in places that should really be using
1047 * XXX thread_t's instead. This maintains historical behaviour, but really
1048 * XXX needs an audit of the context (proxy vs. not) to clean up.
1051 proc_thread(proc_t proc
)
1053 uthread_t uth
= TAILQ_FIRST(&proc
->p_uthlist
);
1056 return(uth
->uu_context
.vc_thread
);
1062 proc_ucred(proc_t p
)
1070 thread_t th
= current_thread();
1072 return((struct uthread
*)get_bsdthread_info(th
));
1077 proc_is64bit(proc_t p
)
1079 return(IS_64BIT_PROCESS(p
));
1083 proc_pidversion(proc_t p
)
1085 return(p
->p_idversion
);
1089 proc_persona_id(proc_t p
)
1091 return (uint32_t)persona_id_from_proc(p
);
1095 proc_getuid(proc_t p
)
1101 proc_getgid(proc_t p
)
1107 proc_uniqueid(proc_t p
)
1109 return(p
->p_uniqueid
);
1113 proc_puniqueid(proc_t p
)
1115 return(p
->p_puniqueid
);
1119 proc_coalitionids(__unused proc_t p
, __unused
uint64_t ids
[COALITION_NUM_TYPES
])
1121 #if CONFIG_COALITIONS
1122 task_coalition_ids(p
->task
, ids
);
1124 memset(ids
, 0, sizeof(uint64_t [COALITION_NUM_TYPES
]));
1130 proc_was_throttled(proc_t p
)
1132 return (p
->was_throttled
);
1136 proc_did_throttle(proc_t p
)
1138 return (p
->did_throttle
);
1142 proc_getcdhash(proc_t p
, unsigned char *cdhash
)
1144 return vn_getcdhash(p
->p_textvp
, p
->p_textoff
, cdhash
);
1148 proc_getexecutableuuid(proc_t p
, unsigned char *uuidbuf
, unsigned long size
)
1150 if (size
>= sizeof(p
->p_uuid
)) {
1151 memcpy(uuidbuf
, p
->p_uuid
, sizeof(p
->p_uuid
));
1155 /* Return vnode for executable with an iocount. Must be released with vnode_put() */
1157 proc_getexecutablevnode(proc_t p
)
1159 vnode_t tvp
= p
->p_textvp
;
1161 if ( tvp
!= NULLVP
) {
1162 if (vnode_getwithref(tvp
) == 0) {
1172 bsd_set_dependency_capable(task_t task
)
1174 proc_t p
= get_bsdtask_info(task
);
1177 OSBitOrAtomic(P_DEPENDENCY_CAPABLE
, &p
->p_flag
);
1184 IS_64BIT_PROCESS(proc_t p
)
1186 if (p
&& (p
->p_flag
& P_LP64
))
1194 * Locate a process by number
1197 pfind_locked(pid_t pid
)
1207 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
) {
1208 if (p
->p_pid
== pid
) {
1210 for (q
= p
->p_hash
.le_next
; q
!= 0; q
= q
->p_hash
.le_next
) {
1211 if ((p
!=q
) && (q
->p_pid
== pid
))
1212 panic("two procs with same pid %p:%p:%d:%d\n", p
, q
, p
->p_pid
, q
->p_pid
);
1222 * Locate a zombie by PID
1224 __private_extern__ proc_t
1232 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
1233 if (p
->p_pid
== pid
)
1242 * Locate a process group by number
1251 pgrp
= pgfind_internal(pgid
);
1252 if ((pgrp
== NULL
) || ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) != 0))
1255 pgrp
->pg_refcount
++;
1263 pgfind_internal(pid_t pgid
)
1267 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
)
1268 if (pgrp
->pg_id
== pgid
)
1274 pg_rele(struct pgrp
* pgrp
)
1276 if(pgrp
== PGRP_NULL
)
1278 pg_rele_dropref(pgrp
);
1282 pg_rele_dropref(struct pgrp
* pgrp
)
1285 if ((pgrp
->pg_refcount
== 1) && ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) == PGRP_FLAG_TERMINATE
)) {
1287 pgdelete_dropref(pgrp
);
1291 pgrp
->pg_refcount
--;
1296 session_find_internal(pid_t sessid
)
1298 struct session
*sess
;
1300 for (sess
= SESSHASH(sessid
)->lh_first
; sess
!= 0; sess
= sess
->s_hash
.le_next
)
1301 if (sess
->s_sid
== sessid
)
1308 * Make a new process ready to become a useful member of society by making it
1309 * visible in all the right places and initialize its own lists to empty.
1311 * Parameters: parent The parent of the process to insert
1312 * child The child process to insert
1316 * Notes: Insert a child process into the parents process group, assign
1317 * the child the parent process pointer and PPID of the parent,
1318 * place it on the parents p_children list as a sibling,
1319 * initialize its own child list, place it in the allproc list,
1320 * insert it in the proper hash bucket, and initialize its
1324 pinsertchild(proc_t parent
, proc_t child
)
1328 LIST_INIT(&child
->p_children
);
1329 TAILQ_INIT(&child
->p_evlist
);
1330 child
->p_pptr
= parent
;
1331 child
->p_ppid
= parent
->p_pid
;
1332 child
->p_puniqueid
= parent
->p_uniqueid
;
1333 child
->p_xhighbits
= 0;
1335 pg
= proc_pgrp(parent
);
1336 pgrp_add(pg
, parent
, child
);
1341 #if CONFIG_MEMORYSTATUS
1342 memorystatus_add(child
, TRUE
);
1345 parent
->p_childrencnt
++;
1346 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1348 LIST_INSERT_HEAD(&allproc
, child
, p_list
);
1349 /* mark the completion of proc creation */
1350 child
->p_listflag
&= ~P_LIST_INCREATE
;
1356 * Move p to a new or existing process group (and session)
1358 * Returns: 0 Success
1359 * ESRCH No such process
1362 enterpgrp(proc_t p
, pid_t pgid
, int mksess
)
1365 struct pgrp
*mypgrp
;
1366 struct session
* procsp
;
1368 pgrp
= pgfind(pgid
);
1369 mypgrp
= proc_pgrp(p
);
1370 procsp
= proc_session(p
);
1373 if (pgrp
!= NULL
&& mksess
) /* firewalls */
1374 panic("enterpgrp: setsid into non-empty pgrp");
1375 if (SESS_LEADER(p
, procsp
))
1376 panic("enterpgrp: session leader attempted setpgrp");
1378 if (pgrp
== PGRP_NULL
) {
1379 pid_t savepid
= p
->p_pid
;
1380 proc_t np
= PROC_NULL
;
1385 if (p
->p_pid
!= pgid
)
1386 panic("enterpgrp: new pgrp and pid != pgid");
1388 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
1391 panic("enterpgrp: M_PGRP zone depleted");
1392 if ((np
= proc_find(savepid
)) == NULL
|| np
!= p
) {
1393 if (np
!= PROC_NULL
)
1395 if (mypgrp
!= PGRP_NULL
)
1397 if (procsp
!= SESSION_NULL
)
1398 session_rele(procsp
);
1399 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
1404 struct session
*sess
;
1409 MALLOC_ZONE(sess
, struct session
*,
1410 sizeof(struct session
), M_SESSION
, M_WAITOK
);
1412 panic("enterpgrp: M_SESSION zone depleted");
1414 sess
->s_sid
= p
->p_pid
;
1416 sess
->s_ttyvp
= NULL
;
1417 sess
->s_ttyp
= TTY_NULL
;
1419 sess
->s_listflags
= 0;
1420 sess
->s_ttypgrpid
= NO_PID
;
1421 #if CONFIG_FINE_LOCK_GROUPS
1422 lck_mtx_init(&sess
->s_mlock
, proc_mlock_grp
, proc_lck_attr
);
1424 lck_mtx_init(&sess
->s_mlock
, proc_lck_grp
, proc_lck_attr
);
1426 bcopy(procsp
->s_login
, sess
->s_login
,
1427 sizeof(sess
->s_login
));
1428 OSBitAndAtomic(~((uint32_t)P_CONTROLT
), &p
->p_flag
);
1430 LIST_INSERT_HEAD(SESSHASH(sess
->s_sid
), sess
, s_hash
);
1432 pgrp
->pg_session
= sess
;
1434 if (p
!= current_proc())
1435 panic("enterpgrp: mksession and p != curproc");
1439 pgrp
->pg_session
= procsp
;
1441 if ((pgrp
->pg_session
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1442 panic("enterpgrp: providing ref to terminating session ");
1443 pgrp
->pg_session
->s_count
++;
1447 #if CONFIG_FINE_LOCK_GROUPS
1448 lck_mtx_init(&pgrp
->pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
1450 lck_mtx_init(&pgrp
->pg_mlock
, proc_lck_grp
, proc_lck_attr
);
1452 LIST_INIT(&pgrp
->pg_members
);
1453 pgrp
->pg_membercnt
= 0;
1456 pgrp
->pg_refcount
= 1;
1457 pgrp
->pg_listflags
= 0;
1458 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
1460 } else if (pgrp
== mypgrp
) {
1464 if (procsp
!= SESSION_NULL
)
1465 session_rele(procsp
);
1469 if (procsp
!= SESSION_NULL
)
1470 session_rele(procsp
);
1472 * Adjust eligibility of affected pgrps to participate in job control.
1473 * Increment eligibility counts before decrementing, otherwise we
1474 * could reach 0 spuriously during the first call.
1476 fixjobc(p
, pgrp
, 1);
1477 fixjobc(p
, mypgrp
, 0);
1479 if(mypgrp
!= PGRP_NULL
)
1481 pgrp_replace(p
, pgrp
);
1488 * remove process from process group
1499 * delete a process group
1502 pgdelete_dropref(struct pgrp
*pgrp
)
1506 struct session
*sessp
;
1510 if (pgrp
->pg_membercnt
!= 0) {
1516 pgrp
->pg_refcount
--;
1517 if ((emptypgrp
== 0) || (pgrp
->pg_membercnt
!= 0)) {
1522 pgrp
->pg_listflags
|= PGRP_FLAG_TERMINATE
;
1524 if (pgrp
->pg_refcount
> 0) {
1529 pgrp
->pg_listflags
|= PGRP_FLAG_DEAD
;
1530 LIST_REMOVE(pgrp
, pg_hash
);
1534 ttyp
= SESSION_TP(pgrp
->pg_session
);
1535 if (ttyp
!= TTY_NULL
) {
1536 if (ttyp
->t_pgrp
== pgrp
) {
1538 /* Re-check after acquiring the lock */
1539 if (ttyp
->t_pgrp
== pgrp
) {
1540 ttyp
->t_pgrp
= NULL
;
1541 pgrp
->pg_session
->s_ttypgrpid
= NO_PID
;
1549 sessp
= pgrp
->pg_session
;
1550 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1551 panic("pg_deleteref: manipulating refs of already terminating session");
1552 if (--sessp
->s_count
== 0) {
1553 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1554 panic("pg_deleteref: terminating already terminated session");
1555 sessp
->s_listflags
|= S_LIST_TERM
;
1556 ttyp
= SESSION_TP(sessp
);
1557 LIST_REMOVE(sessp
, s_hash
);
1559 if (ttyp
!= TTY_NULL
) {
1561 if (ttyp
->t_session
== sessp
)
1562 ttyp
->t_session
= NULL
;
1566 sessp
->s_listflags
|= S_LIST_DEAD
;
1567 if (sessp
->s_count
!= 0)
1568 panic("pg_deleteref: freeing session in use");
1570 #if CONFIG_FINE_LOCK_GROUPS
1571 lck_mtx_destroy(&sessp
->s_mlock
, proc_mlock_grp
);
1573 lck_mtx_destroy(&sessp
->s_mlock
, proc_lck_grp
);
1575 FREE_ZONE(sessp
, sizeof(struct session
), M_SESSION
);
1578 #if CONFIG_FINE_LOCK_GROUPS
1579 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_mlock_grp
);
1581 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_lck_grp
);
1583 FREE_ZONE(pgrp
, sizeof(*pgrp
), M_PGRP
);
1588 * Adjust pgrp jobc counters when specified process changes process group.
1589 * We count the number of processes in each process group that "qualify"
1590 * the group for terminal job control (those with a parent in a different
1591 * process group of the same session). If that count reaches zero, the
1592 * process group becomes orphaned. Check both the specified process'
1593 * process group and that of its children.
1594 * entering == 0 => p is leaving specified group.
1595 * entering == 1 => p is entering specified group.
1598 fixjob_callback(proc_t p
, void * arg
)
1600 struct fixjob_iterargs
*fp
;
1601 struct pgrp
* pg
, *hispg
;
1602 struct session
* mysession
, *hissess
;
1605 fp
= (struct fixjob_iterargs
*)arg
;
1607 mysession
= fp
->mysession
;
1608 entering
= fp
->entering
;
1610 hispg
= proc_pgrp(p
);
1611 hissess
= proc_session(p
);
1613 if ((hispg
!= pg
) &&
1614 (hissess
== mysession
)) {
1619 } else if (--hispg
->pg_jobc
== 0) {
1625 if (hissess
!= SESSION_NULL
)
1626 session_rele(hissess
);
1627 if (hispg
!= PGRP_NULL
)
1630 return(PROC_RETURNED
);
1634 fixjobc(proc_t p
, struct pgrp
*pgrp
, int entering
)
1636 struct pgrp
*hispgrp
= PGRP_NULL
;
1637 struct session
*hissess
= SESSION_NULL
;
1638 struct session
*mysession
= pgrp
->pg_session
;
1640 struct fixjob_iterargs fjarg
;
1641 boolean_t proc_parent_self
;
1644 * Check if p's parent is current proc, if yes then no need to take
1645 * a ref; calling proc_parent with current proc as parent may
1646 * deadlock if current proc is exiting.
1648 proc_parent_self
= proc_parent_is_currentproc(p
);
1649 if (proc_parent_self
)
1650 parent
= current_proc();
1652 parent
= proc_parent(p
);
1654 if (parent
!= PROC_NULL
) {
1655 hispgrp
= proc_pgrp(parent
);
1656 hissess
= proc_session(parent
);
1657 if (!proc_parent_self
)
1663 * Check p's parent to see whether p qualifies its own process
1664 * group; if so, adjust count for p's process group.
1666 if ((hispgrp
!= pgrp
) &&
1667 (hissess
== mysession
)) {
1672 }else if (--pgrp
->pg_jobc
== 0) {
1679 if (hissess
!= SESSION_NULL
)
1680 session_rele(hissess
);
1681 if (hispgrp
!= PGRP_NULL
)
1685 * Check this process' children to see whether they qualify
1686 * their process groups; if so, adjust counts for children's
1690 fjarg
.mysession
= mysession
;
1691 fjarg
.entering
= entering
;
1692 proc_childrenwalk(p
, fixjob_callback
, &fjarg
);
1696 * A process group has become orphaned; if there are any stopped processes in
1697 * the group, hang-up all process in that group.
1700 orphanpg(struct pgrp
*pgrp
)
1704 vm_size_t pid_list_size
= 0;
1705 vm_size_t pid_list_size_needed
= 0;
1707 int pid_count_available
= 0;
1709 assert(pgrp
!= NULL
);
1711 /* allocate outside of the pgrp_lock */
1715 boolean_t should_iterate
= FALSE
;
1716 pid_count_available
= 0;
1718 PGMEMBERS_FOREACH(pgrp
, p
) {
1719 pid_count_available
++;
1721 if (p
->p_stat
== SSTOP
) {
1722 should_iterate
= TRUE
;
1726 if (pid_count_available
== 0 || !should_iterate
) {
1731 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
1732 if (pid_list_size
>= pid_list_size_needed
) {
1737 if (pid_list_size
!= 0) {
1738 kfree(pid_list
, pid_list_size
);
1740 pid_list
= kalloc(pid_list_size_needed
);
1744 pid_list_size
= pid_list_size_needed
;
1747 /* no orphaned processes */
1748 if (pid_list_size
== 0) {
1753 PGMEMBERS_FOREACH(pgrp
, p
) {
1754 pid_list
[pid_count
++] = proc_pid(p
);
1755 if (pid_count
>= pid_count_available
) {
1761 if (pid_count
== 0) {
1765 for (int i
= 0; i
< pid_count
; i
++) {
1766 /* do not handle kernproc */
1767 if (pid_list
[i
] == 0) {
1770 p
= proc_find(pid_list
[i
]);
1775 proc_transwait(p
, 0);
1778 psignal(p
, SIGCONT
);
1783 kfree(pid_list
, pid_list_size
);
1788 proc_is_classic(proc_t p __unused
)
1793 /* XXX Why does this function exist? Need to kill it off... */
1795 current_proc_EXTERNAL(void)
1797 return (current_proc());
1801 proc_is_forcing_hfs_case_sensitivity(proc_t p
)
1803 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
) ? 1 : 0;
1808 * proc_core_name(name, uid, pid)
1809 * Expand the name described in corefilename, using name, uid, and pid.
1810 * corefilename is a printf-like string, with three format specifiers:
1811 * %N name of process ("name")
1812 * %P process id (pid)
1814 * For example, "%N.core" is the default; they can be disabled completely
1815 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1816 * This is controlled by the sysctl variable kern.corefile (see above).
1818 __private_extern__
int
1819 proc_core_name(const char *name
, uid_t uid
, pid_t pid
, char *cf_name
,
1822 const char *format
, *appendstr
;
1823 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
1826 if (cf_name
== NULL
)
1829 format
= corefilename
;
1830 for (i
= 0, n
= 0; n
< cf_name_len
&& format
[i
]; i
++) {
1831 switch (format
[i
]) {
1832 case '%': /* Format character */
1834 switch (format
[i
]) {
1838 case 'N': /* process name */
1841 case 'P': /* process id */
1842 snprintf(id_buf
, sizeof(id_buf
), "%u", pid
);
1845 case 'U': /* user id */
1846 snprintf(id_buf
, sizeof(id_buf
), "%u", uid
);
1849 case '\0': /* format string ended in % symbol */
1854 "Unknown format character %c in `%s'\n",
1857 l
= strlen(appendstr
);
1858 if ((n
+ l
) >= cf_name_len
)
1860 bcopy(appendstr
, cf_name
+ n
, l
);
1864 cf_name
[n
++] = format
[i
];
1867 if (format
[i
] != '\0')
1871 log(LOG_ERR
, "pid %ld (%s), uid (%u): corename is too long\n",
1872 (long)pid
, name
, (uint32_t)uid
);
1875 log(LOG_ERR
, "pid %ld (%s), uid (%u): unexpected end of string after %% token\n",
1876 (long)pid
, name
, (uint32_t)uid
);
1879 #endif /* CONFIG_COREDUMP */
1881 /* Code Signing related routines */
1884 csops(__unused proc_t p
, struct csops_args
*uap
, __unused
int32_t *retval
)
1886 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1887 uap
->usersize
, USER_ADDR_NULL
));
1891 csops_audittoken(__unused proc_t p
, struct csops_audittoken_args
*uap
, __unused
int32_t *retval
)
1893 if (uap
->uaudittoken
== USER_ADDR_NULL
)
1895 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1896 uap
->usersize
, uap
->uaudittoken
));
1900 csops_copy_token(void *start
, size_t length
, user_size_t usize
, user_addr_t uaddr
)
1902 char fakeheader
[8] = { 0 };
1905 if (usize
< sizeof(fakeheader
))
1908 /* if no blob, fill in zero header */
1909 if (NULL
== start
) {
1911 length
= sizeof(fakeheader
);
1912 } else if (usize
< length
) {
1913 /* ... if input too short, copy out length of entitlement */
1914 uint32_t length32
= htonl((uint32_t)length
);
1915 memcpy(&fakeheader
[4], &length32
, sizeof(length32
));
1917 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
1919 return ERANGE
; /* input buffer to short, ERANGE signals that */
1922 return copyout(start
, uaddr
, length
);
1926 csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaudittoken
)
1928 size_t usize
= (size_t)CAST_DOWN(size_t, usersize
);
1934 unsigned char cdhash
[SHA1_RESULTLEN
];
1935 audit_token_t token
;
1936 unsigned int upid
=0, uidversion
= 0;
1938 forself
= error
= 0;
1941 pid
= proc_selfpid();
1942 if (pid
== proc_selfpid())
1949 case CS_OPS_PIDOFFSET
:
1950 case CS_OPS_ENTITLEMENTS_BLOB
:
1951 case CS_OPS_IDENTITY
:
1953 break; /* not restricted to root */
1955 if (forself
== 0 && kauth_cred_issuser(kauth_cred_get()) != TRUE
)
1960 pt
= proc_find(pid
);
1961 if (pt
== PROC_NULL
)
1965 uidversion
= pt
->p_idversion
;
1966 if (uaudittoken
!= USER_ADDR_NULL
) {
1968 error
= copyin(uaudittoken
, &token
, sizeof(audit_token_t
));
1971 /* verify the audit token pid/idversion matches with proc */
1972 if ((token
.val
[5] != upid
) || (token
.val
[7] != uidversion
)) {
1980 case CS_OPS_MARKINVALID
:
1981 case CS_OPS_MARKHARD
:
1982 case CS_OPS_MARKKILL
:
1983 case CS_OPS_MARKRESTRICT
:
1984 case CS_OPS_SET_STATUS
:
1985 case CS_OPS_CLEARINSTALLER
:
1986 case CS_OPS_CLEARPLATFORM
:
1987 if ((error
= mac_proc_check_set_cs_info(current_proc(), pt
, ops
)))
1991 if ((error
= mac_proc_check_get_cs_info(current_proc(), pt
, ops
)))
1998 case CS_OPS_STATUS
: {
2002 retflags
= pt
->p_csflags
;
2003 if (cs_enforcement(pt
))
2004 retflags
|= CS_ENFORCEMENT
;
2005 if (csproc_get_platform_binary(pt
))
2006 retflags
|= CS_PLATFORM_BINARY
;
2007 if (csproc_get_platform_path(pt
))
2008 retflags
|= CS_PLATFORM_PATH
;
2011 if (uaddr
!= USER_ADDR_NULL
)
2012 error
= copyout(&retflags
, uaddr
, sizeof(uint32_t));
2015 case CS_OPS_MARKINVALID
:
2017 if ((pt
->p_csflags
& CS_VALID
) == CS_VALID
) { /* is currently valid */
2018 pt
->p_csflags
&= ~CS_VALID
; /* set invalid */
2019 if ((pt
->p_csflags
& CS_KILL
) == CS_KILL
) {
2020 pt
->p_csflags
|= CS_KILLED
;
2023 printf("CODE SIGNING: marked invalid by pid %d: "
2024 "p=%d[%s] honoring CS_KILL, final status 0x%x\n",
2025 proc_selfpid(), pt
->p_pid
, pt
->p_comm
, pt
->p_csflags
);
2027 psignal(pt
, SIGKILL
);
2035 case CS_OPS_MARKHARD
:
2037 pt
->p_csflags
|= CS_HARD
;
2038 if ((pt
->p_csflags
& CS_VALID
) == 0) {
2039 /* @@@ allow? reject? kill? @@@ */
2047 case CS_OPS_MARKKILL
:
2049 pt
->p_csflags
|= CS_KILL
;
2050 if ((pt
->p_csflags
& CS_VALID
) == 0) {
2052 psignal(pt
, SIGKILL
);
2057 case CS_OPS_PIDOFFSET
:
2058 toff
= pt
->p_textoff
;
2060 error
= copyout(&toff
, uaddr
, sizeof(toff
));
2065 /* pt already holds a reference on its p_textvp */
2067 toff
= pt
->p_textoff
;
2069 if (tvp
== NULLVP
|| usize
!= SHA1_RESULTLEN
) {
2074 error
= vn_getcdhash(tvp
, toff
, cdhash
);
2078 error
= copyout(cdhash
, uaddr
, sizeof (cdhash
));
2083 case CS_OPS_ENTITLEMENTS_BLOB
: {
2089 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2095 error
= cs_entitlements_blob_get(pt
, &start
, &length
);
2100 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2103 case CS_OPS_MARKRESTRICT
:
2105 pt
->p_csflags
|= CS_RESTRICT
;
2109 case CS_OPS_SET_STATUS
: {
2112 if (usize
< sizeof(flags
)) {
2117 error
= copyin(uaddr
, &flags
, sizeof(flags
));
2121 /* only allow setting a subset of all code sign flags */
2123 CS_HARD
| CS_EXEC_SET_HARD
|
2124 CS_KILL
| CS_EXEC_SET_KILL
|
2127 CS_ENFORCEMENT
| CS_EXEC_SET_ENFORCEMENT
;
2130 if (pt
->p_csflags
& CS_VALID
)
2131 pt
->p_csflags
|= flags
;
2143 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2149 error
= cs_blob_get(pt
, &start
, &length
);
2154 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2157 case CS_OPS_IDENTITY
: {
2158 const char *identity
;
2159 uint8_t fakeheader
[8];
2164 * Make identity have a blob header to make it
2165 * easier on userland to guess the identity
2168 if (usize
< sizeof(fakeheader
)) {
2172 memset(fakeheader
, 0, sizeof(fakeheader
));
2175 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2181 identity
= cs_identity_get(pt
);
2183 if (identity
== NULL
) {
2188 length
= strlen(identity
) + 1; /* include NUL */
2189 idlen
= htonl(length
+ sizeof(fakeheader
));
2190 memcpy(&fakeheader
[4], &idlen
, sizeof(idlen
));
2192 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
2196 if (usize
< sizeof(fakeheader
) + length
)
2198 else if (usize
> sizeof(fakeheader
))
2199 error
= copyout(identity
, uaddr
+ sizeof(fakeheader
), length
);
2204 case CS_OPS_CLEARINSTALLER
:
2206 pt
->p_csflags
&= ~(CS_INSTALLER
| CS_DATAVAULT_CONTROLLER
| CS_EXEC_INHERIT_SIP
);
2210 case CS_OPS_CLEARPLATFORM
:
2211 #if DEVELOPMENT || DEBUG
2212 if (cs_enforcement_enable
) {
2218 if (csr_check(CSR_ALLOW_APPLE_INTERNAL
) != 0) {
2225 pt
->p_csflags
&= ~(CS_PLATFORM_BINARY
|CS_PLATFORM_PATH
);
2226 csproc_clear_platform_binary(pt
);
2232 #endif /* !DEVELOPMENT || DEBUG */
2246 proc_iterate_fn_t callout
,
2248 proc_iterate_fn_t filterfn
,
2252 vm_size_t pid_list_size
= 0;
2253 vm_size_t pid_list_size_needed
= 0;
2255 int pid_count_available
= 0;
2257 assert(callout
!= NULL
);
2259 /* allocate outside of the proc_list_lock */
2263 pid_count_available
= nprocs
+ 1; //kernel_task is not counted in nprocs
2264 assert(pid_count_available
> 0);
2266 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2267 if (pid_list_size
>= pid_list_size_needed
) {
2272 if (pid_list_size
!= 0) {
2273 kfree(pid_list
, pid_list_size
);
2275 pid_list
= kalloc(pid_list_size_needed
);
2279 pid_list_size
= pid_list_size_needed
;
2282 /* filter pids into pid_list */
2284 if (flags
& PROC_ALLPROCLIST
) {
2286 ALLPROC_FOREACH(p
) {
2287 /* ignore processes that are being forked */
2288 if (p
->p_stat
== SIDL
) {
2291 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2295 pid_list
[pid_count
++] = proc_pid(p
);
2296 if (pid_count
>= pid_count_available
) {
2302 if ((pid_count
< pid_count_available
) &&
2303 (flags
& PROC_ZOMBPROCLIST
))
2306 ZOMBPROC_FOREACH(p
) {
2307 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2311 pid_list
[pid_count
++] = proc_pid(p
);
2312 if (pid_count
>= pid_count_available
) {
2320 /* call callout on processes in the pid_list */
2322 for (int i
= 0; i
< pid_count
; i
++) {
2323 proc_t p
= proc_find(pid_list
[i
]);
2325 if ((flags
& PROC_NOWAITTRANS
) == 0) {
2326 proc_transwait(p
, 0);
2328 int callout_ret
= callout(p
, arg
);
2330 switch (callout_ret
) {
2331 case PROC_RETURNED_DONE
:
2334 case PROC_CLAIMED_DONE
:
2344 panic("proc_iterate: callout returned %d for pid %d",
2345 callout_ret
, pid_list
[i
]);
2348 } else if (flags
& PROC_ZOMBPROCLIST
) {
2349 p
= proc_find_zombref(pid_list
[i
]);
2353 int callout_ret
= callout(p
, arg
);
2355 switch (callout_ret
) {
2356 case PROC_RETURNED_DONE
:
2357 proc_drop_zombref(p
);
2359 case PROC_CLAIMED_DONE
:
2363 proc_drop_zombref(p
);
2369 panic("proc_iterate: callout returned %d for zombie pid %d",
2370 callout_ret
, pid_list
[i
]);
2377 kfree(pid_list
, pid_list_size
);
2384 proc_iterate_fn_t callout
,
2386 proc_iterate_fn_t filterfn
,
2391 assert(callout
!= NULL
);
2393 proc_shutdown_exitcount
= 0;
2399 ALLPROC_FOREACH(p
) {
2400 if ((filterfn
!= NULL
) && filterfn(p
, filterarg
) == 0) {
2403 p
= proc_ref_locked(p
);
2410 proc_transwait(p
, 0);
2411 (void)callout(p
, arg
);
2414 goto restart_foreach
;
2423 proc_iterate_fn_t callout
,
2427 vm_size_t pid_list_size
= 0;
2428 vm_size_t pid_list_size_needed
= 0;
2430 int pid_count_available
= 0;
2432 assert(parent
!= NULL
);
2433 assert(callout
!= NULL
);
2438 pid_count_available
= parent
->p_childrencnt
;
2439 if (pid_count_available
== 0) {
2444 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2445 if (pid_list_size
>= pid_list_size_needed
) {
2450 if (pid_list_size
!= 0) {
2451 kfree(pid_list
, pid_list_size
);
2453 pid_list
= kalloc(pid_list_size_needed
);
2457 pid_list_size
= pid_list_size_needed
;
2461 PCHILDREN_FOREACH(parent
, p
) {
2462 if (p
->p_stat
== SIDL
) {
2466 pid_list
[pid_count
++] = proc_pid(p
);
2467 if (pid_count
>= pid_count_available
) {
2474 for (int i
= 0; i
< pid_count
; i
++) {
2475 p
= proc_find(pid_list
[i
]);
2480 int callout_ret
= callout(p
, arg
);
2482 switch (callout_ret
) {
2483 case PROC_RETURNED_DONE
:
2486 case PROC_CLAIMED_DONE
:
2495 panic("proc_childrenwalk: callout returned %d for pid %d",
2496 callout_ret
, pid_list
[i
]);
2502 kfree(pid_list
, pid_list_size
);
2510 proc_iterate_fn_t callout
,
2512 proc_iterate_fn_t filterfn
,
2517 vm_size_t pid_list_size
= 0;
2518 vm_size_t pid_list_size_needed
= 0;
2520 int pid_count_available
= 0;
2524 assert(pgrp
!= NULL
);
2525 assert(callout
!= NULL
);
2530 pid_count_available
= pgrp
->pg_membercnt
;
2531 if (pid_count_available
== 0) {
2536 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2537 if (pid_list_size
>= pid_list_size_needed
) {
2542 if (pid_list_size
!= 0) {
2543 kfree(pid_list
, pid_list_size
);
2545 pid_list
= kalloc(pid_list_size_needed
);
2549 pid_list_size
= pid_list_size_needed
;
2554 PGMEMBERS_FOREACH(pgrp
, p
) {
2555 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2558 pid_list
[pid_count
++] = proc_pid(p
);
2559 if (pid_count
>= pid_count_available
) {
2566 if (flags
& PGRP_DROPREF
) {
2570 for (int i
= 0; i
< pid_count
; i
++) {
2571 /* do not handle kernproc */
2572 if (pid_list
[i
] == 0) {
2575 p
= proc_find(pid_list
[i
]);
2579 if (p
->p_pgrpid
!= pgid
) {
2584 int callout_ret
= callout(p
, arg
);
2586 switch (callout_ret
) {
2593 case PROC_RETURNED_DONE
:
2596 case PROC_CLAIMED_DONE
:
2600 panic("pgrp_iterate: callout returned %d for pid %d",
2601 callout_ret
, pid_list
[i
]);
2606 kfree(pid_list
, pid_list_size
);
2611 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2614 child
->p_pgrp
= pgrp
;
2615 child
->p_pgrpid
= pgrp
->pg_id
;
2616 child
->p_listflag
|= P_LIST_INPGRP
;
2618 * When pgrp is being freed , a process can still
2619 * request addition using setpgid from bash when
2620 * login is terminated (login cycler) return ESRCH
2621 * Safe to hold lock due to refcount on pgrp
2623 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2624 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2627 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2628 panic("pgrp_add : pgrp is dead adding process");
2632 pgrp
->pg_membercnt
++;
2633 if ( parent
!= PROC_NULL
) {
2634 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2636 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2641 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2642 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2648 pgrp_remove(struct proc
* p
)
2655 #if __PROC_INTERNAL_DEBUG
2656 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2657 panic("removing from pglist but no named ref\n");
2659 p
->p_pgrpid
= PGRPID_DEAD
;
2660 p
->p_listflag
&= ~P_LIST_INPGRP
;
2664 if (pg
== PGRP_NULL
)
2665 panic("pgrp_remove: pg is NULL");
2669 if (pg
->pg_membercnt
< 0)
2670 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",pg
, p
);
2672 LIST_REMOVE(p
, p_pglist
);
2673 if (pg
->pg_members
.lh_first
== 0) {
2675 pgdelete_dropref(pg
);
2683 /* cannot use proc_pgrp as it maybe stalled */
2685 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2687 struct pgrp
* oldpg
;
2693 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2694 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2695 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2698 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2701 if (oldpg
== PGRP_NULL
)
2702 panic("pgrp_replace: oldpg NULL");
2703 oldpg
->pg_refcount
++;
2704 #if __PROC_INTERNAL_DEBUG
2705 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2706 panic("removing from pglist but no named ref\n");
2708 p
->p_pgrpid
= PGRPID_DEAD
;
2709 p
->p_listflag
&= ~P_LIST_INPGRP
;
2715 oldpg
->pg_membercnt
--;
2716 if (oldpg
->pg_membercnt
< 0)
2717 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",oldpg
, p
);
2718 LIST_REMOVE(p
, p_pglist
);
2719 if (oldpg
->pg_members
.lh_first
== 0) {
2721 pgdelete_dropref(oldpg
);
2729 p
->p_pgrpid
= newpg
->pg_id
;
2730 p
->p_listflag
|= P_LIST_INPGRP
;
2732 * When pgrp is being freed , a process can still
2733 * request addition using setpgid from bash when
2734 * login is terminated (login cycler) return ESRCH
2735 * Safe to hold lock due to refcount on pgrp
2737 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2738 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2741 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2742 panic("pgrp_add : pgrp is dead adding process");
2746 newpg
->pg_membercnt
++;
2747 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
2751 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
2752 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2755 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
2756 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
2757 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
2758 wakeup(&p
->p_pgrpid
);
2765 pgrp_lock(struct pgrp
* pgrp
)
2767 lck_mtx_lock(&pgrp
->pg_mlock
);
2771 pgrp_unlock(struct pgrp
* pgrp
)
2773 lck_mtx_unlock(&pgrp
->pg_mlock
);
2777 session_lock(struct session
* sess
)
2779 lck_mtx_lock(&sess
->s_mlock
);
2784 session_unlock(struct session
* sess
)
2786 lck_mtx_unlock(&sess
->s_mlock
);
2798 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2799 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2800 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2805 assert(pgrp
!= NULL
);
2807 if (pgrp
!= PGRP_NULL
) {
2808 pgrp
->pg_refcount
++;
2809 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0)
2810 panic("proc_pgrp: ref being povided for dead pgrp");
2819 tty_pgrp(struct tty
* tp
)
2821 struct pgrp
* pg
= PGRP_NULL
;
2826 if (pg
!= PGRP_NULL
) {
2827 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0)
2828 panic("tty_pgrp: ref being povided for dead pgrp");
2837 proc_session(proc_t p
)
2839 struct session
* sess
= SESSION_NULL
;
2842 return(SESSION_NULL
);
2846 /* wait during transitions */
2847 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2848 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2849 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2852 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
2853 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2854 panic("proc_session:returning sesssion ref on terminating session");
2862 session_rele(struct session
*sess
)
2865 if (--sess
->s_count
== 0) {
2866 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2867 panic("session_rele: terminating already terminated session");
2868 sess
->s_listflags
|= S_LIST_TERM
;
2869 LIST_REMOVE(sess
, s_hash
);
2870 sess
->s_listflags
|= S_LIST_DEAD
;
2871 if (sess
->s_count
!= 0)
2872 panic("session_rele: freeing session in use");
2874 #if CONFIG_FINE_LOCK_GROUPS
2875 lck_mtx_destroy(&sess
->s_mlock
, proc_mlock_grp
);
2877 lck_mtx_destroy(&sess
->s_mlock
, proc_lck_grp
);
2879 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
2885 proc_transstart(proc_t p
, int locked
, int non_blocking
)
2889 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2890 if (((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
) || non_blocking
) {
2895 p
->p_lflag
|= P_LTRANSWAIT
;
2896 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2898 p
->p_lflag
|= P_LINTRANSIT
;
2899 p
->p_transholder
= current_thread();
2906 proc_transcommit(proc_t p
, int locked
)
2911 assert ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
);
2912 assert (p
->p_transholder
== current_thread());
2913 p
->p_lflag
|= P_LTRANSCOMMIT
;
2915 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2916 p
->p_lflag
&= ~P_LTRANSWAIT
;
2917 wakeup(&p
->p_lflag
);
2924 proc_transend(proc_t p
, int locked
)
2929 p
->p_lflag
&= ~( P_LINTRANSIT
| P_LTRANSCOMMIT
);
2930 p
->p_transholder
= NULL
;
2932 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2933 p
->p_lflag
&= ~P_LTRANSWAIT
;
2934 wakeup(&p
->p_lflag
);
2941 proc_transwait(proc_t p
, int locked
)
2945 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2946 if ((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
&& current_proc() == p
) {
2951 p
->p_lflag
|= P_LTRANSWAIT
;
2952 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2960 proc_klist_lock(void)
2962 lck_mtx_lock(proc_klist_mlock
);
2966 proc_klist_unlock(void)
2968 lck_mtx_unlock(proc_klist_mlock
);
2972 proc_knote(struct proc
* p
, long hint
)
2975 KNOTE(&p
->p_klist
, hint
);
2976 proc_klist_unlock();
2980 proc_knote_drain(struct proc
*p
)
2982 struct knote
*kn
= NULL
;
2985 * Clear the proc's klist to avoid references after the proc is reaped.
2988 while ((kn
= SLIST_FIRST(&p
->p_klist
))) {
2989 kn
->kn_ptr
.p_proc
= PROC_NULL
;
2990 KNOTE_DETACH(&p
->p_klist
, kn
);
2992 proc_klist_unlock();
2996 proc_setregister(proc_t p
)
2999 p
->p_lflag
|= P_LREGISTER
;
3004 proc_resetregister(proc_t p
)
3007 p
->p_lflag
&= ~P_LREGISTER
;
3012 proc_pgrpid(proc_t p
)
3020 return current_proc()->p_pgrpid
;
3024 /* return control and action states */
3026 proc_getpcontrol(int pid
, int * pcontrolp
)
3033 if (pcontrolp
!= NULL
)
3034 *pcontrolp
= p
->p_pcaction
;
3041 proc_dopcontrol(proc_t p
)
3047 pcontrol
= PROC_CONTROL_STATE(p
);
3049 if (PROC_ACTION_STATE(p
) == 0) {
3052 PROC_SETACTION_STATE(p
);
3054 printf("low swap: throttling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3058 PROC_SETACTION_STATE(p
);
3060 printf("low swap: suspending pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3061 task_suspend(p
->task
);
3065 PROC_SETACTION_STATE(p
);
3067 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3068 psignal(p
, SIGKILL
);
3078 return(PROC_RETURNED
);
3083 * Resume a throttled or suspended process. This is an internal interface that's only
3084 * used by the user level code that presents the GUI when we run out of swap space and
3085 * hence is restricted to processes with superuser privileges.
3089 proc_resetpcontrol(int pid
)
3094 proc_t self
= current_proc();
3096 /* if the process has been validated to handle resource control or root is valid one */
3097 if (((self
->p_lflag
& P_LVMRSRCOWNER
) == 0) && (error
= suser(kauth_cred_get(), 0)))
3106 pcontrol
= PROC_CONTROL_STATE(p
);
3108 if(PROC_ACTION_STATE(p
) !=0) {
3111 PROC_RESETACTION_STATE(p
);
3113 printf("low swap: unthrottling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3117 PROC_RESETACTION_STATE(p
);
3119 printf("low swap: resuming pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3120 task_resume(p
->task
);
3125 PROC_SETACTION_STATE(p
);
3127 printf("low swap: attempt to unkill pid %d (%s) ignored\n", p
->p_pid
, p
->p_comm
);
3143 struct no_paging_space
3145 uint64_t pcs_max_size
;
3146 uint64_t pcs_uniqueid
;
3149 uint64_t pcs_total_size
;
3151 uint64_t npcs_max_size
;
3152 uint64_t npcs_uniqueid
;
3154 int npcs_proc_count
;
3155 uint64_t npcs_total_size
;
3157 int apcs_proc_count
;
3158 uint64_t apcs_total_size
;
3163 proc_pcontrol_filter(proc_t p
, void *arg
)
3165 struct no_paging_space
*nps
;
3166 uint64_t compressed
;
3168 nps
= (struct no_paging_space
*)arg
;
3170 compressed
= get_task_compressed(p
->task
);
3172 if (PROC_CONTROL_STATE(p
)) {
3173 if (PROC_ACTION_STATE(p
) == 0) {
3174 if (compressed
> nps
->pcs_max_size
) {
3175 nps
->pcs_pid
= p
->p_pid
;
3176 nps
->pcs_uniqueid
= p
->p_uniqueid
;
3177 nps
->pcs_max_size
= compressed
;
3179 nps
->pcs_total_size
+= compressed
;
3180 nps
->pcs_proc_count
++;
3182 nps
->apcs_total_size
+= compressed
;
3183 nps
->apcs_proc_count
++;
3186 if (compressed
> nps
->npcs_max_size
) {
3187 nps
->npcs_pid
= p
->p_pid
;
3188 nps
->npcs_uniqueid
= p
->p_uniqueid
;
3189 nps
->npcs_max_size
= compressed
;
3191 nps
->npcs_total_size
+= compressed
;
3192 nps
->npcs_proc_count
++;
3200 proc_pcontrol_null(__unused proc_t p
, __unused
void *arg
)
3202 return(PROC_RETURNED
);
3207 * Deal with the low on compressor pool space condition... this function
3208 * gets called when we are approaching the limits of the compressor pool or
3209 * we are unable to create a new swap file.
3210 * Since this eventually creates a memory deadlock situtation, we need to take action to free up
3211 * memory resources (both compressed and uncompressed) in order to prevent the system from hanging completely.
3212 * There are 2 categories of processes to deal with. Those that have an action
3213 * associated with them by the task itself and those that do not. Actionable
3214 * tasks can have one of three categories specified: ones that
3215 * can be killed immediately, ones that should be suspended, and ones that should
3216 * be throttled. Processes that do not have an action associated with them are normally
3217 * ignored unless they are utilizing such a large percentage of the compressor pool (currently 50%)
3218 * that only by killing them can we hope to put the system back into a usable state.
3221 #define NO_PAGING_SPACE_DEBUG 0
3223 extern uint64_t vm_compressor_pages_compressed(void);
3225 struct timeval last_no_space_action
= {0, 0};
3227 #if DEVELOPMENT || DEBUG
3228 extern boolean_t kill_on_no_paging_space
;
3229 #endif /* DEVELOPMENT || DEBUG */
3231 #define MB_SIZE (1024 * 1024ULL)
3232 boolean_t
memorystatus_kill_on_VM_thrashing(boolean_t
);
3234 extern int32_t max_kill_priority
;
3235 extern int memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index
);
3238 no_paging_space_action()
3241 struct no_paging_space nps
;
3245 * Throttle how often we come through here. Once every 5 seconds should be plenty.
3249 if (now
.tv_sec
<= last_no_space_action
.tv_sec
+ 5)
3253 * Examine all processes and find the biggest (biggest is based on the number of pages this
3254 * task has in the compressor pool) that has been marked to have some action
3255 * taken when swap space runs out... we also find the biggest that hasn't been marked for
3258 * If the biggest non-actionable task is over the "dangerously big" threashold (currently 50% of
3259 * the total number of pages held by the compressor, we go ahead and kill it since no other task
3260 * can have any real effect on the situation. Otherwise, we go after the actionable process.
3262 bzero(&nps
, sizeof(nps
));
3264 proc_iterate(PROC_ALLPROCLIST
, proc_pcontrol_null
, (void *)NULL
, proc_pcontrol_filter
, (void *)&nps
);
3266 #if NO_PAGING_SPACE_DEBUG
3267 printf("low swap: npcs_proc_count = %d, npcs_total_size = %qd, npcs_max_size = %qd\n",
3268 nps
.npcs_proc_count
, nps
.npcs_total_size
, nps
.npcs_max_size
);
3269 printf("low swap: pcs_proc_count = %d, pcs_total_size = %qd, pcs_max_size = %qd\n",
3270 nps
.pcs_proc_count
, nps
.pcs_total_size
, nps
.pcs_max_size
);
3271 printf("low swap: apcs_proc_count = %d, apcs_total_size = %qd\n",
3272 nps
.apcs_proc_count
, nps
.apcs_total_size
);
3274 if (nps
.npcs_max_size
> (vm_compressor_pages_compressed() * 50) / 100) {
3276 * for now we'll knock out any task that has more then 50% of the pages
3277 * held by the compressor
3279 if ((p
= proc_find(nps
.npcs_pid
)) != PROC_NULL
) {
3281 if (nps
.npcs_uniqueid
== p
->p_uniqueid
) {
3283 * verify this is still the same process
3284 * in case the proc exited and the pid got reused while
3285 * we were finishing the proc_iterate and getting to this point
3287 last_no_space_action
= now
;
3289 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
));
3290 psignal(p
, SIGKILL
);
3302 * We have some processes within our jetsam bands of consideration and hence can be killed.
3303 * So we will invoke the memorystatus thread to go ahead and kill something.
3305 if (memorystatus_get_proccnt_upto_priority(max_kill_priority
) > 0) {
3307 last_no_space_action
= now
;
3308 memorystatus_kill_on_VM_thrashing(TRUE
/* async */);
3313 * No eligible processes to kill. So let's suspend/kill the largest
3314 * process depending on its policy control specifications.
3317 if (nps
.pcs_max_size
> 0) {
3318 if ((p
= proc_find(nps
.pcs_pid
)) != PROC_NULL
) {
3320 if (nps
.pcs_uniqueid
== p
->p_uniqueid
) {
3322 * verify this is still the same process
3323 * in case the proc exited and the pid got reused while
3324 * we were finishing the proc_iterate and getting to this point
3326 last_no_space_action
= now
;
3338 last_no_space_action
= now
;
3340 printf("low swap: unable to find any eligible processes to take action on\n");
3346 proc_trace_log(__unused proc_t p
, struct proc_trace_log_args
*uap
, __unused
int *retval
)
3349 proc_t target_proc
= PROC_NULL
;
3350 pid_t target_pid
= uap
->pid
;
3351 uint64_t target_uniqueid
= uap
->uniqueid
;
3352 task_t target_task
= NULL
;
3354 if (priv_check_cred(kauth_cred_get(), PRIV_PROC_TRACE_INSPECT
, 0)) {
3358 target_proc
= proc_find(target_pid
);
3359 if (target_proc
!= PROC_NULL
) {
3360 if (target_uniqueid
!= proc_uniqueid(target_proc
)) {
3365 target_task
= proc_task(target_proc
);
3366 if (task_send_trace_memory(target_task
, target_pid
, target_uniqueid
)) {
3374 if (target_proc
!= PROC_NULL
)
3375 proc_rele(target_proc
);
3379 #if VM_SCAN_FOR_SHADOW_CHAIN
3380 extern int vm_map_shadow_max(vm_map_t map
);
3381 int proc_shadow_max(void);
3382 int proc_shadow_max(void)
3391 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
3392 if (p
->p_stat
== SIDL
)
3398 map
= get_task_map(task
);
3402 retval
= vm_map_shadow_max(map
);
3410 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */
3412 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
);
3413 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
)
3415 if (target_proc
!= NULL
) {
3416 target_proc
->p_responsible_pid
= responsible_pid
;
3422 proc_chrooted(proc_t p
)
3428 retval
= (p
->p_fd
->fd_rdir
!= NULL
) ? 1 : 0;
3436 proc_get_uthread_uu_threadlist(void * uthread_v
)
3438 uthread_t uth
= (uthread_t
)uthread_v
;
3439 return (uth
!= NULL
) ? uth
->uu_threadlist
: NULL
;
3442 #ifdef CONFIG_32BIT_TELEMETRY
3444 proc_log_32bit_telemetry(proc_t p
)
3447 char signature_buf
[MAX_32BIT_EXEC_SIG_SIZE
] = { 0 };
3448 char * signature_cur_end
= &signature_buf
[0];
3449 char * signature_buf_end
= &signature_buf
[MAX_32BIT_EXEC_SIG_SIZE
- 1];
3450 int bytes_printed
= 0;
3452 const char * teamid
= NULL
;
3453 const char * identity
= NULL
;
3454 struct cs_blob
* csblob
= NULL
;
3459 * Get proc name and parent proc name; if the parent execs, we'll get a
3462 bytes_printed
= snprintf(signature_cur_end
,
3463 signature_buf_end
- signature_cur_end
,
3464 "%s,%s,", p
->p_name
,
3465 (p
->p_pptr
? p
->p_pptr
->p_name
: ""));
3467 if (bytes_printed
> 0) {
3468 signature_cur_end
+= bytes_printed
;
3473 /* Get developer info. */
3474 vnode_t v
= proc_getexecutablevnode(p
);
3477 csblob
= csvnode_get_blob(v
, 0);
3480 teamid
= csblob_get_teamid(csblob
);
3481 identity
= csblob_get_identity(csblob
);
3485 if (teamid
== NULL
) {
3489 if (identity
== NULL
) {
3493 bytes_printed
= snprintf(signature_cur_end
,
3494 signature_buf_end
- signature_cur_end
,
3495 "%s,%s", teamid
, identity
);
3497 if (bytes_printed
> 0) {
3498 signature_cur_end
+= bytes_printed
;
3506 * We may want to rate limit here, although the SUMMARIZE key should
3507 * help us aggregate events in userspace.
3511 kern_asl_msg(LOG_DEBUG
, "messagetracer", 3,
3512 /* 0 */ "com.apple.message.domain", "com.apple.kernel.32bit_exec",
3513 /* 1 */ "com.apple.message.signature", signature_buf
,
3514 /* 2 */ "com.apple.message.summarize", "YES",
3517 #endif /* CONFIG_32BIT_TELEMETRY */