2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 * 04-Aug-97 Umesh Vaishampayan (umeshv@apple.com)
71 * Added current_proc_EXTERNAL() function for the use of kernel
74 * 05-Jun-95 Mac Gillon (mgillon) at NeXT
75 * New version based on 3.3NS and 4.4
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/proc_internal.h>
85 #include <sys/file_internal.h>
87 #include <sys/malloc.h>
90 #include <sys/ioctl.h>
92 #include <sys/signalvar.h>
93 #include <sys/syslog.h>
94 #include <sys/sysctl.h>
95 #include <sys/sysproto.h>
96 #include <sys/kauth.h>
97 #include <sys/codesign.h>
98 #include <sys/kernel_types.h>
100 #include <kern/kalloc.h>
101 #include <kern/task.h>
102 #include <kern/coalition.h>
103 #include <sys/coalition.h>
104 #include <kern/assert.h>
105 #include <vm/vm_protos.h>
106 #include <vm/vm_map.h> /* vm_map_switch_protect() */
107 #include <vm/vm_pageout.h>
108 #include <mach/task.h>
109 #include <mach/message.h>
110 #include <sys/priv.h>
111 #include <sys/proc_info.h>
112 #include <sys/bsdtask_info.h>
113 #include <sys/persona.h>
119 #if CONFIG_MEMORYSTATUS
120 #include <sys/kern_memorystatus.h>
124 #include <security/mac_framework.h>
127 #include <libkern/crypto/sha1.h>
130 * Structure associated with user cacheing.
133 LIST_ENTRY(uidinfo
) ui_hash
;
137 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
138 LIST_HEAD(uihashhead
, uidinfo
) *uihashtbl
;
139 u_long uihash
; /* size of hash table - 1 */
142 * Other process lists
144 struct pidhashhead
*pidhashtbl
;
146 struct pgrphashhead
*pgrphashtbl
;
148 struct sesshashhead
*sesshashtbl
;
151 struct proclist allproc
;
152 struct proclist zombproc
;
153 extern struct tty cons
;
157 #if DEVELOPMENT || DEBUG
158 extern int cs_enforcement_enable
;
162 #define __PROC_INTERNAL_DEBUG 1
165 /* Name to give to core files */
167 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/private/var/cores/%N.core"};
169 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/cores/core.%P"};
174 #include <kern/backtrace.h>
177 static void orphanpg(struct pgrp
* pg
);
178 void proc_name_kdp(task_t t
, char * buf
, int size
);
179 void * proc_get_uthread_uu_threadlist(void * uthread_v
);
180 int proc_threadname_kdp(void * uth
, char * buf
, size_t size
);
181 void proc_starttime_kdp(void * p
, uint64_t * tv_sec
, uint64_t * tv_usec
, uint64_t * abstime
);
182 char * proc_name_address(void * p
);
184 /* TODO: make a header that's exported and usable in osfmk */
185 char* proc_best_name(proc_t p
);
187 static void pgrp_add(struct pgrp
* pgrp
, proc_t parent
, proc_t child
);
188 static void pgrp_remove(proc_t p
);
189 static void pgrp_replace(proc_t p
, struct pgrp
*pgrp
);
190 static void pgdelete_dropref(struct pgrp
*pgrp
);
191 extern void pg_rele_dropref(struct pgrp
* pgrp
);
192 static int csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaddittoken
);
193 static boolean_t
proc_parent_is_currentproc(proc_t p
);
195 struct fixjob_iterargs
{
197 struct session
* mysession
;
201 int fixjob_callback(proc_t
, void *);
203 uint64_t get_current_unique_pid(void);
207 get_current_unique_pid(void)
209 proc_t p
= current_proc();
212 return p
->p_uniqueid
;
218 * Initialize global process hashing structures.
224 LIST_INIT(&zombproc
);
225 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
226 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
227 sesshashtbl
= hashinit(maxproc
/ 4, M_PROC
, &sesshash
);
228 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
230 personas_bootstrap();
235 * Change the count associated with number of processes
236 * a given user is using. This routine protects the uihash
240 chgproccnt(uid_t uid
, int diff
)
243 struct uidinfo
*newuip
= NULL
;
244 struct uihashhead
*uipp
;
250 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
)
251 if (uip
->ui_uid
== uid
)
254 uip
->ui_proccnt
+= diff
;
255 if (uip
->ui_proccnt
> 0) {
256 retval
= uip
->ui_proccnt
;
260 if (uip
->ui_proccnt
< 0)
261 panic("chgproccnt: procs < 0");
262 LIST_REMOVE(uip
, ui_hash
);
265 FREE_ZONE(uip
, sizeof(*uip
), M_PROC
);
274 panic("chgproccnt: lost user");
276 if (newuip
!= NULL
) {
279 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
281 uip
->ui_proccnt
= diff
;
287 MALLOC_ZONE(newuip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
289 panic("chgproccnt: M_PROC zone depleted");
293 FREE_ZONE(newuip
, sizeof(*uip
), M_PROC
);
298 * Is p an inferior of the current process?
306 for (; p
!= current_proc(); p
= p
->p_pptr
)
316 * Is p an inferior of t ?
319 isinferior(proc_t p
, proc_t t
)
325 /* if p==t they are not inferior */
330 for (; p
!= t
; p
= p
->p_pptr
) {
333 /* Detect here if we're in a cycle */
334 if ((p
->p_pid
== 0) || (p
->p_pptr
== start
) || (nchecked
>= nprocs
))
344 proc_isinferior(int pid1
, int pid2
)
346 proc_t p
= PROC_NULL
;
347 proc_t t
= PROC_NULL
;
350 if (((p
= proc_find(pid1
)) != (proc_t
)0 ) && ((t
= proc_find(pid2
)) != (proc_t
)0))
351 retval
= isinferior(p
, t
);
364 return(proc_findinternal(pid
, 0));
368 proc_findinternal(int pid
, int locked
)
370 proc_t p
= PROC_NULL
;
376 p
= pfind_locked(pid
);
377 if ((p
== PROC_NULL
) || (p
!= proc_ref_locked(p
)))
388 proc_findthread(thread_t thread
)
390 proc_t p
= PROC_NULL
;
394 uth
= get_bsdthread_info(thread
);
395 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
398 p
= (proc_t
)(get_bsdthreadtask_info(thread
));
399 p
= proc_ref_locked(p
);
405 uthread_reset_proc_refcount(void *uthread
) {
408 uth
= (uthread_t
) uthread
;
409 uth
->uu_proc_refcount
= 0;
412 if (proc_ref_tracking_disabled
) {
422 uthread_get_proc_refcount(void *uthread
) {
425 if (proc_ref_tracking_disabled
) {
429 uth
= (uthread_t
) uthread
;
431 return uth
->uu_proc_refcount
;
436 record_procref(proc_t p __unused
, int count
) {
439 uth
= current_uthread();
440 uth
->uu_proc_refcount
+= count
;
443 if (proc_ref_tracking_disabled
) {
448 if (uth
->uu_pindex
< NUM_PROC_REFS_TO_TRACK
) {
449 backtrace((uintptr_t *) &uth
->uu_proc_pcs
[uth
->uu_pindex
], PROC_REF_STACK_DEPTH
);
451 uth
->uu_proc_ps
[uth
->uu_pindex
] = p
;
459 uthread_needs_to_wait_in_proc_refwait(void) {
460 uthread_t uth
= current_uthread();
463 * Allow threads holding no proc refs to wait
464 * in proc_refwait, allowing threads holding
465 * proc refs to wait in proc_refwait causes
466 * deadlocks and makes proc_find non-reentrant.
468 if (uth
->uu_proc_refcount
== 0)
492 if (p
!= proc_ref_locked(p
))
500 proc_ref_locked(proc_t p
)
503 int pid
= proc_pid(p
);
507 * if process still in creation or proc got recycled
508 * during msleep then return failure.
510 if ((p
== PROC_NULL
) || (p1
!= p
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0))
514 * Do not return process marked for termination
515 * or proc_refdrain called without ref wait.
516 * Wait for proc_refdrain_with_refwait to complete if
517 * process in refdrain and refwait flag is set, unless
518 * the current thread is holding to a proc_ref
521 if ((p
->p_stat
!= SZOMB
) &&
522 ((p
->p_listflag
& P_LIST_EXITED
) == 0) &&
523 ((p
->p_listflag
& P_LIST_DEAD
) == 0) &&
524 (((p
->p_listflag
& (P_LIST_DRAIN
| P_LIST_DRAINWAIT
)) == 0) ||
525 ((p
->p_listflag
& P_LIST_REFWAIT
) != 0))) {
526 if ((p
->p_listflag
& P_LIST_REFWAIT
) != 0 && uthread_needs_to_wait_in_proc_refwait()) {
527 msleep(&p
->p_listflag
, proc_list_mlock
, 0, "proc_refwait", 0) ;
529 * the proc might have been recycled since we dropped
530 * the proc list lock, get the proc again.
532 p
= pfind_locked(pid
);
536 record_procref(p
, 1);
545 proc_rele_locked(proc_t p
)
548 if (p
->p_refcount
> 0) {
550 record_procref(p
, -1);
551 if ((p
->p_refcount
== 0) && ((p
->p_listflag
& P_LIST_DRAINWAIT
) == P_LIST_DRAINWAIT
)) {
552 p
->p_listflag
&= ~P_LIST_DRAINWAIT
;
553 wakeup(&p
->p_refcount
);
556 panic("proc_rele_locked -ve ref\n");
561 proc_find_zombref(int pid
)
568 p
= pfind_locked(pid
);
570 /* should we bail? */
571 if ((p
== PROC_NULL
) /* not found */
572 || ((p
->p_listflag
& P_LIST_INCREATE
) != 0) /* not created yet */
573 || ((p
->p_listflag
& P_LIST_EXITED
) == 0)) { /* not started exit */
579 /* If someone else is controlling the (unreaped) zombie - wait */
580 if ((p
->p_listflag
& P_LIST_WAITING
) != 0) {
581 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
584 p
->p_listflag
|= P_LIST_WAITING
;
592 proc_drop_zombref(proc_t p
)
595 if ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
596 p
->p_listflag
&= ~P_LIST_WAITING
;
604 proc_refdrain(proc_t p
)
606 proc_refdrain_with_refwait(p
, FALSE
);
610 proc_refdrain_with_refwait(proc_t p
, boolean_t get_ref_and_allow_wait
)
612 boolean_t initexec
= FALSE
;
615 p
->p_listflag
|= P_LIST_DRAIN
;
616 if (get_ref_and_allow_wait
) {
618 * All the calls to proc_ref_locked will wait
619 * for the flag to get cleared before returning a ref,
620 * unless the current thread is holding to a proc ref
623 p
->p_listflag
|= P_LIST_REFWAIT
;
629 /* Do not wait in ref drain for launchd exec */
630 while (p
->p_refcount
&& !initexec
) {
631 p
->p_listflag
|= P_LIST_DRAINWAIT
;
632 msleep(&p
->p_refcount
, proc_list_mlock
, 0, "proc_refdrain", 0) ;
635 p
->p_listflag
&= ~P_LIST_DRAIN
;
636 if (!get_ref_and_allow_wait
) {
637 p
->p_listflag
|= P_LIST_DEAD
;
639 /* Return a ref to the caller */
641 record_procref(p
, 1);
646 if (get_ref_and_allow_wait
) {
653 proc_refwake(proc_t p
)
656 p
->p_listflag
&= ~P_LIST_REFWAIT
;
657 wakeup(&p
->p_listflag
);
662 proc_parentholdref(proc_t p
)
664 proc_t parent
= PROC_NULL
;
672 if ((pp
== PROC_NULL
) || (pp
->p_stat
== SZOMB
) || ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
))) {
677 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == P_LIST_CHILDDRSTART
) {
678 pp
->p_listflag
|= P_LIST_CHILDDRWAIT
;
679 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
688 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == 0) {
699 proc_parentdropref(proc_t p
, int listlocked
)
704 if (p
->p_parentref
> 0) {
706 if ((p
->p_parentref
== 0) && ((p
->p_listflag
& P_LIST_PARENTREFWAIT
) == P_LIST_PARENTREFWAIT
)) {
707 p
->p_listflag
&= ~P_LIST_PARENTREFWAIT
;
708 wakeup(&p
->p_parentref
);
711 panic("proc_parentdropref -ve ref\n");
719 proc_childdrainstart(proc_t p
)
721 #if __PROC_INTERNAL_DEBUG
722 if ((p
->p_listflag
& P_LIST_CHILDDRSTART
) == P_LIST_CHILDDRSTART
)
723 panic("proc_childdrainstart: childdrain already started\n");
725 p
->p_listflag
|= P_LIST_CHILDDRSTART
;
726 /* wait for all that hold parentrefs to drop */
727 while (p
->p_parentref
> 0) {
728 p
->p_listflag
|= P_LIST_PARENTREFWAIT
;
729 msleep(&p
->p_parentref
, proc_list_mlock
, 0, "proc_childdrainstart", 0) ;
735 proc_childdrainend(proc_t p
)
737 #if __PROC_INTERNAL_DEBUG
738 if (p
->p_childrencnt
> 0)
739 panic("exiting: children stil hanging around\n");
741 p
->p_listflag
|= P_LIST_CHILDDRAINED
;
742 if ((p
->p_listflag
& (P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
)) != 0) {
743 p
->p_listflag
&= ~(P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
);
744 wakeup(&p
->p_childrencnt
);
749 proc_checkdeadrefs(__unused proc_t p
)
751 #if __PROC_INTERNAL_DEBUG
752 if ((p
->p_listflag
& P_LIST_INHASH
) != 0)
753 panic("proc being freed and still in hash %p: %u\n", p
, p
->p_listflag
);
754 if (p
->p_childrencnt
!= 0)
755 panic("proc being freed and pending children cnt %p:%d\n", p
, p
->p_childrencnt
);
756 if (p
->p_refcount
!= 0)
757 panic("proc being freed and pending refcount %p:%d\n", p
, p
->p_refcount
);
758 if (p
->p_parentref
!= 0)
759 panic("proc being freed and pending parentrefs %p:%d\n", p
, p
->p_parentref
);
782 return (current_proc()->p_pid
);
788 return (current_proc()->p_ppid
);
792 proc_selfcsflags(void)
794 return (current_proc()->p_csflags
);
799 dtrace_current_proc_vforking(void)
801 thread_t th
= current_thread();
802 struct uthread
*ut
= get_bsdthread_info(th
);
805 ((ut
->uu_flag
& (UT_VFORK
|UT_VFORKING
)) == (UT_VFORK
|UT_VFORKING
))) {
807 * Handle the narrow window where we're in the vfork syscall,
808 * but we're not quite ready to claim (in particular, to DTrace)
809 * that we're running as the child.
811 return (get_bsdtask_info(get_threadtask(th
)));
813 return (current_proc());
817 dtrace_proc_selfpid(void)
819 return (dtrace_current_proc_vforking()->p_pid
);
823 dtrace_proc_selfppid(void)
825 return (dtrace_current_proc_vforking()->p_ppid
);
829 dtrace_proc_selfruid(void)
831 return (dtrace_current_proc_vforking()->p_ruid
);
833 #endif /* CONFIG_DTRACE */
836 proc_parent(proc_t p
)
844 parent
= proc_ref_locked(pp
);
845 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)){
846 pp
->p_listflag
|= P_LIST_CHILDLKWAIT
;
847 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
855 proc_parent_is_currentproc(proc_t p
)
857 boolean_t ret
= FALSE
;
860 if (p
->p_pptr
== current_proc())
868 proc_name(int pid
, char * buf
, int size
)
872 if ((p
= proc_find(pid
)) != PROC_NULL
) {
873 strlcpy(buf
, &p
->p_comm
[0], size
);
879 proc_name_kdp(task_t t
, char * buf
, int size
)
881 proc_t p
= get_bsdtask_info(t
);
885 if ((size_t)size
> sizeof(p
->p_comm
))
886 strlcpy(buf
, &p
->p_name
[0], MIN((int)sizeof(p
->p_name
), size
));
888 strlcpy(buf
, &p
->p_comm
[0], MIN((int)sizeof(p
->p_comm
), size
));
892 proc_threadname_kdp(void * uth
, char * buf
, size_t size
)
894 if (size
< MAXTHREADNAMESIZE
) {
895 /* this is really just a protective measure for the future in
896 * case the thread name size in stackshot gets out of sync with
897 * the BSD max thread name size. Note that bsd_getthreadname
898 * doesn't take input buffer size into account. */
903 bsd_getthreadname(uth
, buf
);
908 /* note that this function is generally going to be called from stackshot,
909 * and the arguments will be coming from a struct which is declared packed
910 * thus the input arguments will in general be unaligned. We have to handle
913 proc_starttime_kdp(void *p
, uint64_t *tv_sec
, uint64_t *tv_usec
, uint64_t *abstime
)
915 proc_t pp
= (proc_t
)p
;
918 } __attribute__((packed
));
920 if (pp
!= PROC_NULL
) {
922 ((struct uint64p
*)tv_sec
)->val
= pp
->p_start
.tv_sec
;
924 ((struct uint64p
*)tv_usec
)->val
= pp
->p_start
.tv_usec
;
925 if (abstime
!= NULL
) {
926 if (pp
->p_stats
!= NULL
)
927 *abstime
= pp
->p_stats
->ps_start
;
935 proc_name_address(void *p
)
937 return &((proc_t
)p
)->p_comm
[0];
941 proc_best_name(proc_t p
)
943 if (p
->p_name
[0] != 0)
944 return (&p
->p_name
[0]);
945 return (&p
->p_comm
[0]);
949 proc_selfname(char * buf
, int size
)
953 if ((p
= current_proc())!= (proc_t
)0) {
954 strlcpy(buf
, &p
->p_comm
[0], size
);
959 proc_signal(int pid
, int signum
)
963 if ((p
= proc_find(pid
)) != PROC_NULL
) {
970 proc_issignal(int pid
, sigset_t mask
)
975 if ((p
= proc_find(pid
)) != PROC_NULL
) {
976 error
= proc_pendingsignals(p
, mask
);
984 proc_noremotehang(proc_t p
)
989 retval
= p
->p_flag
& P_NOREMOTEHANG
;
990 return(retval
? 1: 0);
995 proc_exiting(proc_t p
)
1000 retval
= p
->p_lflag
& P_LEXIT
;
1001 return(retval
? 1: 0);
1005 proc_forcequota(proc_t p
)
1010 retval
= p
->p_flag
& P_FORCEQUOTA
;
1011 return(retval
? 1: 0);
1016 proc_suser(proc_t p
)
1018 kauth_cred_t my_cred
;
1021 my_cred
= kauth_cred_proc_ref(p
);
1022 error
= suser(my_cred
, &p
->p_acflag
);
1023 kauth_cred_unref(&my_cred
);
1028 proc_task(proc_t proc
)
1030 return (task_t
)proc
->task
;
1034 * Obtain the first thread in a process
1036 * XXX This is a bad thing to do; it exists predominantly to support the
1037 * XXX use of proc_t's in places that should really be using
1038 * XXX thread_t's instead. This maintains historical behaviour, but really
1039 * XXX needs an audit of the context (proxy vs. not) to clean up.
1042 proc_thread(proc_t proc
)
1044 uthread_t uth
= TAILQ_FIRST(&proc
->p_uthlist
);
1047 return(uth
->uu_context
.vc_thread
);
1053 proc_ucred(proc_t p
)
1061 thread_t th
= current_thread();
1063 return((struct uthread
*)get_bsdthread_info(th
));
1068 proc_is64bit(proc_t p
)
1070 return(IS_64BIT_PROCESS(p
));
1074 proc_pidversion(proc_t p
)
1076 return(p
->p_idversion
);
1080 proc_persona_id(proc_t p
)
1082 return (uint32_t)persona_id_from_proc(p
);
1086 proc_getuid(proc_t p
)
1092 proc_getgid(proc_t p
)
1098 proc_uniqueid(proc_t p
)
1100 return(p
->p_uniqueid
);
1104 proc_puniqueid(proc_t p
)
1106 return(p
->p_puniqueid
);
1110 proc_coalitionids(__unused proc_t p
, __unused
uint64_t ids
[COALITION_NUM_TYPES
])
1112 #if CONFIG_COALITIONS
1113 task_coalition_ids(p
->task
, ids
);
1115 memset(ids
, 0, sizeof(uint64_t [COALITION_NUM_TYPES
]));
1121 proc_was_throttled(proc_t p
)
1123 return (p
->was_throttled
);
1127 proc_did_throttle(proc_t p
)
1129 return (p
->did_throttle
);
1133 proc_getcdhash(proc_t p
, unsigned char *cdhash
)
1135 return vn_getcdhash(p
->p_textvp
, p
->p_textoff
, cdhash
);
1139 proc_getexecutableuuid(proc_t p
, unsigned char *uuidbuf
, unsigned long size
)
1141 if (size
>= sizeof(p
->p_uuid
)) {
1142 memcpy(uuidbuf
, p
->p_uuid
, sizeof(p
->p_uuid
));
1146 /* Return vnode for executable with an iocount. Must be released with vnode_put() */
1148 proc_getexecutablevnode(proc_t p
)
1150 vnode_t tvp
= p
->p_textvp
;
1152 if ( tvp
!= NULLVP
) {
1153 if (vnode_getwithref(tvp
) == 0) {
1163 bsd_set_dependency_capable(task_t task
)
1165 proc_t p
= get_bsdtask_info(task
);
1168 OSBitOrAtomic(P_DEPENDENCY_CAPABLE
, &p
->p_flag
);
1175 IS_64BIT_PROCESS(proc_t p
)
1177 if (p
&& (p
->p_flag
& P_LP64
))
1185 * Locate a process by number
1188 pfind_locked(pid_t pid
)
1198 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
) {
1199 if (p
->p_pid
== pid
) {
1201 for (q
= p
->p_hash
.le_next
; q
!= 0; q
= q
->p_hash
.le_next
) {
1202 if ((p
!=q
) && (q
->p_pid
== pid
))
1203 panic("two procs with same pid %p:%p:%d:%d\n", p
, q
, p
->p_pid
, q
->p_pid
);
1213 * Locate a zombie by PID
1215 __private_extern__ proc_t
1223 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
1224 if (p
->p_pid
== pid
)
1233 * Locate a process group by number
1242 pgrp
= pgfind_internal(pgid
);
1243 if ((pgrp
== NULL
) || ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) != 0))
1246 pgrp
->pg_refcount
++;
1254 pgfind_internal(pid_t pgid
)
1258 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
)
1259 if (pgrp
->pg_id
== pgid
)
1265 pg_rele(struct pgrp
* pgrp
)
1267 if(pgrp
== PGRP_NULL
)
1269 pg_rele_dropref(pgrp
);
1273 pg_rele_dropref(struct pgrp
* pgrp
)
1276 if ((pgrp
->pg_refcount
== 1) && ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) == PGRP_FLAG_TERMINATE
)) {
1278 pgdelete_dropref(pgrp
);
1282 pgrp
->pg_refcount
--;
1287 session_find_internal(pid_t sessid
)
1289 struct session
*sess
;
1291 for (sess
= SESSHASH(sessid
)->lh_first
; sess
!= 0; sess
= sess
->s_hash
.le_next
)
1292 if (sess
->s_sid
== sessid
)
1299 * Make a new process ready to become a useful member of society by making it
1300 * visible in all the right places and initialize its own lists to empty.
1302 * Parameters: parent The parent of the process to insert
1303 * child The child process to insert
1307 * Notes: Insert a child process into the parents process group, assign
1308 * the child the parent process pointer and PPID of the parent,
1309 * place it on the parents p_children list as a sibling,
1310 * initialize its own child list, place it in the allproc list,
1311 * insert it in the proper hash bucket, and initialize its
1315 pinsertchild(proc_t parent
, proc_t child
)
1319 LIST_INIT(&child
->p_children
);
1320 TAILQ_INIT(&child
->p_evlist
);
1321 child
->p_pptr
= parent
;
1322 child
->p_ppid
= parent
->p_pid
;
1323 child
->p_puniqueid
= parent
->p_uniqueid
;
1324 child
->p_xhighbits
= 0;
1326 pg
= proc_pgrp(parent
);
1327 pgrp_add(pg
, parent
, child
);
1332 #if CONFIG_MEMORYSTATUS
1333 memorystatus_add(child
, TRUE
);
1336 parent
->p_childrencnt
++;
1337 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1339 LIST_INSERT_HEAD(&allproc
, child
, p_list
);
1340 /* mark the completion of proc creation */
1341 child
->p_listflag
&= ~P_LIST_INCREATE
;
1347 * Move p to a new or existing process group (and session)
1349 * Returns: 0 Success
1350 * ESRCH No such process
1353 enterpgrp(proc_t p
, pid_t pgid
, int mksess
)
1356 struct pgrp
*mypgrp
;
1357 struct session
* procsp
;
1359 pgrp
= pgfind(pgid
);
1360 mypgrp
= proc_pgrp(p
);
1361 procsp
= proc_session(p
);
1364 if (pgrp
!= NULL
&& mksess
) /* firewalls */
1365 panic("enterpgrp: setsid into non-empty pgrp");
1366 if (SESS_LEADER(p
, procsp
))
1367 panic("enterpgrp: session leader attempted setpgrp");
1369 if (pgrp
== PGRP_NULL
) {
1370 pid_t savepid
= p
->p_pid
;
1371 proc_t np
= PROC_NULL
;
1376 if (p
->p_pid
!= pgid
)
1377 panic("enterpgrp: new pgrp and pid != pgid");
1379 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
1382 panic("enterpgrp: M_PGRP zone depleted");
1383 if ((np
= proc_find(savepid
)) == NULL
|| np
!= p
) {
1384 if (np
!= PROC_NULL
)
1386 if (mypgrp
!= PGRP_NULL
)
1388 if (procsp
!= SESSION_NULL
)
1389 session_rele(procsp
);
1390 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
1395 struct session
*sess
;
1400 MALLOC_ZONE(sess
, struct session
*,
1401 sizeof(struct session
), M_SESSION
, M_WAITOK
);
1403 panic("enterpgrp: M_SESSION zone depleted");
1405 sess
->s_sid
= p
->p_pid
;
1407 sess
->s_ttyvp
= NULL
;
1408 sess
->s_ttyp
= TTY_NULL
;
1410 sess
->s_listflags
= 0;
1411 sess
->s_ttypgrpid
= NO_PID
;
1412 #if CONFIG_FINE_LOCK_GROUPS
1413 lck_mtx_init(&sess
->s_mlock
, proc_mlock_grp
, proc_lck_attr
);
1415 lck_mtx_init(&sess
->s_mlock
, proc_lck_grp
, proc_lck_attr
);
1417 bcopy(procsp
->s_login
, sess
->s_login
,
1418 sizeof(sess
->s_login
));
1419 OSBitAndAtomic(~((uint32_t)P_CONTROLT
), &p
->p_flag
);
1421 LIST_INSERT_HEAD(SESSHASH(sess
->s_sid
), sess
, s_hash
);
1423 pgrp
->pg_session
= sess
;
1425 if (p
!= current_proc())
1426 panic("enterpgrp: mksession and p != curproc");
1430 pgrp
->pg_session
= procsp
;
1432 if ((pgrp
->pg_session
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1433 panic("enterpgrp: providing ref to terminating session ");
1434 pgrp
->pg_session
->s_count
++;
1438 #if CONFIG_FINE_LOCK_GROUPS
1439 lck_mtx_init(&pgrp
->pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
1441 lck_mtx_init(&pgrp
->pg_mlock
, proc_lck_grp
, proc_lck_attr
);
1443 LIST_INIT(&pgrp
->pg_members
);
1444 pgrp
->pg_membercnt
= 0;
1447 pgrp
->pg_refcount
= 1;
1448 pgrp
->pg_listflags
= 0;
1449 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
1451 } else if (pgrp
== mypgrp
) {
1455 if (procsp
!= SESSION_NULL
)
1456 session_rele(procsp
);
1460 if (procsp
!= SESSION_NULL
)
1461 session_rele(procsp
);
1463 * Adjust eligibility of affected pgrps to participate in job control.
1464 * Increment eligibility counts before decrementing, otherwise we
1465 * could reach 0 spuriously during the first call.
1467 fixjobc(p
, pgrp
, 1);
1468 fixjobc(p
, mypgrp
, 0);
1470 if(mypgrp
!= PGRP_NULL
)
1472 pgrp_replace(p
, pgrp
);
1479 * remove process from process group
1490 * delete a process group
1493 pgdelete_dropref(struct pgrp
*pgrp
)
1497 struct session
*sessp
;
1501 if (pgrp
->pg_membercnt
!= 0) {
1507 pgrp
->pg_refcount
--;
1508 if ((emptypgrp
== 0) || (pgrp
->pg_membercnt
!= 0)) {
1513 pgrp
->pg_listflags
|= PGRP_FLAG_TERMINATE
;
1515 if (pgrp
->pg_refcount
> 0) {
1520 pgrp
->pg_listflags
|= PGRP_FLAG_DEAD
;
1521 LIST_REMOVE(pgrp
, pg_hash
);
1525 ttyp
= SESSION_TP(pgrp
->pg_session
);
1526 if (ttyp
!= TTY_NULL
) {
1527 if (ttyp
->t_pgrp
== pgrp
) {
1529 /* Re-check after acquiring the lock */
1530 if (ttyp
->t_pgrp
== pgrp
) {
1531 ttyp
->t_pgrp
= NULL
;
1532 pgrp
->pg_session
->s_ttypgrpid
= NO_PID
;
1540 sessp
= pgrp
->pg_session
;
1541 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1542 panic("pg_deleteref: manipulating refs of already terminating session");
1543 if (--sessp
->s_count
== 0) {
1544 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1545 panic("pg_deleteref: terminating already terminated session");
1546 sessp
->s_listflags
|= S_LIST_TERM
;
1547 ttyp
= SESSION_TP(sessp
);
1548 LIST_REMOVE(sessp
, s_hash
);
1550 if (ttyp
!= TTY_NULL
) {
1552 if (ttyp
->t_session
== sessp
)
1553 ttyp
->t_session
= NULL
;
1557 sessp
->s_listflags
|= S_LIST_DEAD
;
1558 if (sessp
->s_count
!= 0)
1559 panic("pg_deleteref: freeing session in use");
1561 #if CONFIG_FINE_LOCK_GROUPS
1562 lck_mtx_destroy(&sessp
->s_mlock
, proc_mlock_grp
);
1564 lck_mtx_destroy(&sessp
->s_mlock
, proc_lck_grp
);
1566 FREE_ZONE(sessp
, sizeof(struct session
), M_SESSION
);
1569 #if CONFIG_FINE_LOCK_GROUPS
1570 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_mlock_grp
);
1572 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_lck_grp
);
1574 FREE_ZONE(pgrp
, sizeof(*pgrp
), M_PGRP
);
1579 * Adjust pgrp jobc counters when specified process changes process group.
1580 * We count the number of processes in each process group that "qualify"
1581 * the group for terminal job control (those with a parent in a different
1582 * process group of the same session). If that count reaches zero, the
1583 * process group becomes orphaned. Check both the specified process'
1584 * process group and that of its children.
1585 * entering == 0 => p is leaving specified group.
1586 * entering == 1 => p is entering specified group.
1589 fixjob_callback(proc_t p
, void * arg
)
1591 struct fixjob_iterargs
*fp
;
1592 struct pgrp
* pg
, *hispg
;
1593 struct session
* mysession
, *hissess
;
1596 fp
= (struct fixjob_iterargs
*)arg
;
1598 mysession
= fp
->mysession
;
1599 entering
= fp
->entering
;
1601 hispg
= proc_pgrp(p
);
1602 hissess
= proc_session(p
);
1604 if ((hispg
!= pg
) &&
1605 (hissess
== mysession
)) {
1610 } else if (--hispg
->pg_jobc
== 0) {
1616 if (hissess
!= SESSION_NULL
)
1617 session_rele(hissess
);
1618 if (hispg
!= PGRP_NULL
)
1621 return(PROC_RETURNED
);
1625 fixjobc(proc_t p
, struct pgrp
*pgrp
, int entering
)
1627 struct pgrp
*hispgrp
= PGRP_NULL
;
1628 struct session
*hissess
= SESSION_NULL
;
1629 struct session
*mysession
= pgrp
->pg_session
;
1631 struct fixjob_iterargs fjarg
;
1632 boolean_t proc_parent_self
;
1635 * Check if p's parent is current proc, if yes then no need to take
1636 * a ref; calling proc_parent with current proc as parent may
1637 * deadlock if current proc is exiting.
1639 proc_parent_self
= proc_parent_is_currentproc(p
);
1640 if (proc_parent_self
)
1641 parent
= current_proc();
1643 parent
= proc_parent(p
);
1645 if (parent
!= PROC_NULL
) {
1646 hispgrp
= proc_pgrp(parent
);
1647 hissess
= proc_session(parent
);
1648 if (!proc_parent_self
)
1654 * Check p's parent to see whether p qualifies its own process
1655 * group; if so, adjust count for p's process group.
1657 if ((hispgrp
!= pgrp
) &&
1658 (hissess
== mysession
)) {
1663 }else if (--pgrp
->pg_jobc
== 0) {
1670 if (hissess
!= SESSION_NULL
)
1671 session_rele(hissess
);
1672 if (hispgrp
!= PGRP_NULL
)
1676 * Check this process' children to see whether they qualify
1677 * their process groups; if so, adjust counts for children's
1681 fjarg
.mysession
= mysession
;
1682 fjarg
.entering
= entering
;
1683 proc_childrenwalk(p
, fixjob_callback
, &fjarg
);
1687 * A process group has become orphaned; if there are any stopped processes in
1688 * the group, hang-up all process in that group.
1691 orphanpg(struct pgrp
*pgrp
)
1695 vm_size_t pid_list_size
= 0;
1696 vm_size_t pid_list_size_needed
= 0;
1698 int pid_count_available
= 0;
1700 assert(pgrp
!= NULL
);
1702 /* allocate outside of the pgrp_lock */
1706 boolean_t should_iterate
= FALSE
;
1707 pid_count_available
= 0;
1709 PGMEMBERS_FOREACH(pgrp
, p
) {
1710 pid_count_available
++;
1712 if (p
->p_stat
== SSTOP
) {
1713 should_iterate
= TRUE
;
1717 if (pid_count_available
== 0 || !should_iterate
) {
1722 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
1723 if (pid_list_size
>= pid_list_size_needed
) {
1728 if (pid_list_size
!= 0) {
1729 kfree(pid_list
, pid_list_size
);
1731 pid_list
= kalloc(pid_list_size_needed
);
1735 pid_list_size
= pid_list_size_needed
;
1738 /* no orphaned processes */
1739 if (pid_list_size
== 0) {
1744 PGMEMBERS_FOREACH(pgrp
, p
) {
1745 pid_list
[pid_count
++] = proc_pid(p
);
1746 if (pid_count
>= pid_count_available
) {
1752 if (pid_count
== 0) {
1756 for (int i
= 0; i
< pid_count
; i
++) {
1757 /* do not handle kernproc */
1758 if (pid_list
[i
] == 0) {
1761 p
= proc_find(pid_list
[i
]);
1766 proc_transwait(p
, 0);
1769 psignal(p
, SIGCONT
);
1774 kfree(pid_list
, pid_list_size
);
1779 proc_is_classic(proc_t p __unused
)
1784 /* XXX Why does this function exist? Need to kill it off... */
1786 current_proc_EXTERNAL(void)
1788 return (current_proc());
1792 proc_is_forcing_hfs_case_sensitivity(proc_t p
)
1794 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
) ? 1 : 0;
1799 * proc_core_name(name, uid, pid)
1800 * Expand the name described in corefilename, using name, uid, and pid.
1801 * corefilename is a printf-like string, with three format specifiers:
1802 * %N name of process ("name")
1803 * %P process id (pid)
1805 * For example, "%N.core" is the default; they can be disabled completely
1806 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1807 * This is controlled by the sysctl variable kern.corefile (see above).
1809 __private_extern__
int
1810 proc_core_name(const char *name
, uid_t uid
, pid_t pid
, char *cf_name
,
1813 const char *format
, *appendstr
;
1814 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
1817 if (cf_name
== NULL
)
1820 format
= corefilename
;
1821 for (i
= 0, n
= 0; n
< cf_name_len
&& format
[i
]; i
++) {
1822 switch (format
[i
]) {
1823 case '%': /* Format character */
1825 switch (format
[i
]) {
1829 case 'N': /* process name */
1832 case 'P': /* process id */
1833 snprintf(id_buf
, sizeof(id_buf
), "%u", pid
);
1836 case 'U': /* user id */
1837 snprintf(id_buf
, sizeof(id_buf
), "%u", uid
);
1840 case '\0': /* format string ended in % symbol */
1845 "Unknown format character %c in `%s'\n",
1848 l
= strlen(appendstr
);
1849 if ((n
+ l
) >= cf_name_len
)
1851 bcopy(appendstr
, cf_name
+ n
, l
);
1855 cf_name
[n
++] = format
[i
];
1858 if (format
[i
] != '\0')
1862 log(LOG_ERR
, "pid %ld (%s), uid (%u): corename is too long\n",
1863 (long)pid
, name
, (uint32_t)uid
);
1866 log(LOG_ERR
, "pid %ld (%s), uid (%u): unexpected end of string after %% token\n",
1867 (long)pid
, name
, (uint32_t)uid
);
1870 #endif /* CONFIG_COREDUMP */
1872 /* Code Signing related routines */
1875 csops(__unused proc_t p
, struct csops_args
*uap
, __unused
int32_t *retval
)
1877 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1878 uap
->usersize
, USER_ADDR_NULL
));
1882 csops_audittoken(__unused proc_t p
, struct csops_audittoken_args
*uap
, __unused
int32_t *retval
)
1884 if (uap
->uaudittoken
== USER_ADDR_NULL
)
1886 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1887 uap
->usersize
, uap
->uaudittoken
));
1891 csops_copy_token(void *start
, size_t length
, user_size_t usize
, user_addr_t uaddr
)
1893 char fakeheader
[8] = { 0 };
1896 if (usize
< sizeof(fakeheader
))
1899 /* if no blob, fill in zero header */
1900 if (NULL
== start
) {
1902 length
= sizeof(fakeheader
);
1903 } else if (usize
< length
) {
1904 /* ... if input too short, copy out length of entitlement */
1905 uint32_t length32
= htonl((uint32_t)length
);
1906 memcpy(&fakeheader
[4], &length32
, sizeof(length32
));
1908 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
1910 return ERANGE
; /* input buffer to short, ERANGE signals that */
1913 return copyout(start
, uaddr
, length
);
1917 csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaudittoken
)
1919 size_t usize
= (size_t)CAST_DOWN(size_t, usersize
);
1925 unsigned char cdhash
[SHA1_RESULTLEN
];
1926 audit_token_t token
;
1927 unsigned int upid
=0, uidversion
= 0;
1929 forself
= error
= 0;
1932 pid
= proc_selfpid();
1933 if (pid
== proc_selfpid())
1940 case CS_OPS_PIDOFFSET
:
1941 case CS_OPS_ENTITLEMENTS_BLOB
:
1942 case CS_OPS_IDENTITY
:
1944 break; /* not restricted to root */
1946 if (forself
== 0 && kauth_cred_issuser(kauth_cred_get()) != TRUE
)
1951 pt
= proc_find(pid
);
1952 if (pt
== PROC_NULL
)
1956 uidversion
= pt
->p_idversion
;
1957 if (uaudittoken
!= USER_ADDR_NULL
) {
1959 error
= copyin(uaudittoken
, &token
, sizeof(audit_token_t
));
1962 /* verify the audit token pid/idversion matches with proc */
1963 if ((token
.val
[5] != upid
) || (token
.val
[7] != uidversion
)) {
1971 case CS_OPS_MARKINVALID
:
1972 case CS_OPS_MARKHARD
:
1973 case CS_OPS_MARKKILL
:
1974 case CS_OPS_MARKRESTRICT
:
1975 case CS_OPS_SET_STATUS
:
1976 case CS_OPS_CLEARINSTALLER
:
1977 case CS_OPS_CLEARPLATFORM
:
1978 if ((error
= mac_proc_check_set_cs_info(current_proc(), pt
, ops
)))
1982 if ((error
= mac_proc_check_get_cs_info(current_proc(), pt
, ops
)))
1989 case CS_OPS_STATUS
: {
1993 retflags
= pt
->p_csflags
;
1994 if (cs_enforcement(pt
))
1995 retflags
|= CS_ENFORCEMENT
;
1996 if (csproc_get_platform_binary(pt
))
1997 retflags
|= CS_PLATFORM_BINARY
;
1998 if (csproc_get_platform_path(pt
))
1999 retflags
|= CS_PLATFORM_PATH
;
2002 if (uaddr
!= USER_ADDR_NULL
)
2003 error
= copyout(&retflags
, uaddr
, sizeof(uint32_t));
2006 case CS_OPS_MARKINVALID
:
2008 if ((pt
->p_csflags
& CS_VALID
) == CS_VALID
) { /* is currently valid */
2009 pt
->p_csflags
&= ~CS_VALID
; /* set invalid */
2010 if ((pt
->p_csflags
& CS_KILL
) == CS_KILL
) {
2011 pt
->p_csflags
|= CS_KILLED
;
2014 printf("CODE SIGNING: marked invalid by pid %d: "
2015 "p=%d[%s] honoring CS_KILL, final status 0x%x\n",
2016 proc_selfpid(), pt
->p_pid
, pt
->p_comm
, pt
->p_csflags
);
2018 psignal(pt
, SIGKILL
);
2026 case CS_OPS_MARKHARD
:
2028 pt
->p_csflags
|= CS_HARD
;
2029 if ((pt
->p_csflags
& CS_VALID
) == 0) {
2030 /* @@@ allow? reject? kill? @@@ */
2038 case CS_OPS_MARKKILL
:
2040 pt
->p_csflags
|= CS_KILL
;
2041 if ((pt
->p_csflags
& CS_VALID
) == 0) {
2043 psignal(pt
, SIGKILL
);
2048 case CS_OPS_PIDOFFSET
:
2049 toff
= pt
->p_textoff
;
2051 error
= copyout(&toff
, uaddr
, sizeof(toff
));
2056 /* pt already holds a reference on its p_textvp */
2058 toff
= pt
->p_textoff
;
2060 if (tvp
== NULLVP
|| usize
!= SHA1_RESULTLEN
) {
2065 error
= vn_getcdhash(tvp
, toff
, cdhash
);
2069 error
= copyout(cdhash
, uaddr
, sizeof (cdhash
));
2074 case CS_OPS_ENTITLEMENTS_BLOB
: {
2080 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2086 error
= cs_entitlements_blob_get(pt
, &start
, &length
);
2091 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2094 case CS_OPS_MARKRESTRICT
:
2096 pt
->p_csflags
|= CS_RESTRICT
;
2100 case CS_OPS_SET_STATUS
: {
2103 if (usize
< sizeof(flags
)) {
2108 error
= copyin(uaddr
, &flags
, sizeof(flags
));
2112 /* only allow setting a subset of all code sign flags */
2114 CS_HARD
| CS_EXEC_SET_HARD
|
2115 CS_KILL
| CS_EXEC_SET_KILL
|
2118 CS_ENFORCEMENT
| CS_EXEC_SET_ENFORCEMENT
;
2121 if (pt
->p_csflags
& CS_VALID
)
2122 pt
->p_csflags
|= flags
;
2134 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2140 error
= cs_blob_get(pt
, &start
, &length
);
2145 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2148 case CS_OPS_IDENTITY
: {
2149 const char *identity
;
2150 uint8_t fakeheader
[8];
2155 * Make identity have a blob header to make it
2156 * easier on userland to guess the identity
2159 if (usize
< sizeof(fakeheader
)) {
2163 memset(fakeheader
, 0, sizeof(fakeheader
));
2166 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2172 identity
= cs_identity_get(pt
);
2174 if (identity
== NULL
) {
2179 length
= strlen(identity
) + 1; /* include NUL */
2180 idlen
= htonl(length
+ sizeof(fakeheader
));
2181 memcpy(&fakeheader
[4], &idlen
, sizeof(idlen
));
2183 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
2187 if (usize
< sizeof(fakeheader
) + length
)
2189 else if (usize
> sizeof(fakeheader
))
2190 error
= copyout(identity
, uaddr
+ sizeof(fakeheader
), length
);
2195 case CS_OPS_CLEARINSTALLER
:
2197 pt
->p_csflags
&= ~(CS_INSTALLER
| CS_DATAVAULT_CONTROLLER
| CS_EXEC_INHERIT_SIP
);
2201 case CS_OPS_CLEARPLATFORM
:
2202 #if DEVELOPMENT || DEBUG
2203 if (cs_enforcement_enable
) {
2209 if (csr_check(CSR_ALLOW_APPLE_INTERNAL
) != 0) {
2216 pt
->p_csflags
&= ~(CS_PLATFORM_BINARY
|CS_PLATFORM_PATH
);
2217 csproc_clear_platform_binary(pt
);
2223 #endif /* !DEVELOPMENT || DEBUG */
2237 proc_iterate_fn_t callout
,
2239 proc_iterate_fn_t filterfn
,
2243 vm_size_t pid_list_size
= 0;
2244 vm_size_t pid_list_size_needed
= 0;
2246 int pid_count_available
= 0;
2248 assert(callout
!= NULL
);
2250 /* allocate outside of the proc_list_lock */
2254 pid_count_available
= nprocs
+ 1; //kernel_task is not counted in nprocs
2255 assert(pid_count_available
> 0);
2257 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2258 if (pid_list_size
>= pid_list_size_needed
) {
2263 if (pid_list_size
!= 0) {
2264 kfree(pid_list
, pid_list_size
);
2266 pid_list
= kalloc(pid_list_size_needed
);
2270 pid_list_size
= pid_list_size_needed
;
2273 /* filter pids into pid_list */
2275 if (flags
& PROC_ALLPROCLIST
) {
2277 ALLPROC_FOREACH(p
) {
2278 /* ignore processes that are being forked */
2279 if (p
->p_stat
== SIDL
) {
2282 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2286 pid_list
[pid_count
++] = proc_pid(p
);
2287 if (pid_count
>= pid_count_available
) {
2293 if ((pid_count
< pid_count_available
) &&
2294 (flags
& PROC_ZOMBPROCLIST
))
2297 ZOMBPROC_FOREACH(p
) {
2298 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2302 pid_list
[pid_count
++] = proc_pid(p
);
2303 if (pid_count
>= pid_count_available
) {
2311 /* call callout on processes in the pid_list */
2313 for (int i
= 0; i
< pid_count
; i
++) {
2314 proc_t p
= proc_find(pid_list
[i
]);
2316 if ((flags
& PROC_NOWAITTRANS
) == 0) {
2317 proc_transwait(p
, 0);
2319 int callout_ret
= callout(p
, arg
);
2321 switch (callout_ret
) {
2322 case PROC_RETURNED_DONE
:
2325 case PROC_CLAIMED_DONE
:
2335 panic("proc_iterate: callout returned %d for pid %d",
2336 callout_ret
, pid_list
[i
]);
2339 } else if (flags
& PROC_ZOMBPROCLIST
) {
2340 p
= proc_find_zombref(pid_list
[i
]);
2344 int callout_ret
= callout(p
, arg
);
2346 switch (callout_ret
) {
2347 case PROC_RETURNED_DONE
:
2348 proc_drop_zombref(p
);
2350 case PROC_CLAIMED_DONE
:
2354 proc_drop_zombref(p
);
2360 panic("proc_iterate: callout returned %d for zombie pid %d",
2361 callout_ret
, pid_list
[i
]);
2368 kfree(pid_list
, pid_list_size
);
2375 proc_iterate_fn_t callout
,
2377 proc_iterate_fn_t filterfn
,
2382 assert(callout
!= NULL
);
2384 proc_shutdown_exitcount
= 0;
2390 ALLPROC_FOREACH(p
) {
2391 if ((filterfn
!= NULL
) && filterfn(p
, filterarg
) == 0) {
2394 p
= proc_ref_locked(p
);
2401 proc_transwait(p
, 0);
2402 (void)callout(p
, arg
);
2405 goto restart_foreach
;
2414 proc_iterate_fn_t callout
,
2418 vm_size_t pid_list_size
= 0;
2419 vm_size_t pid_list_size_needed
= 0;
2421 int pid_count_available
= 0;
2423 assert(parent
!= NULL
);
2424 assert(callout
!= NULL
);
2429 pid_count_available
= parent
->p_childrencnt
;
2430 if (pid_count_available
== 0) {
2435 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2436 if (pid_list_size
>= pid_list_size_needed
) {
2441 if (pid_list_size
!= 0) {
2442 kfree(pid_list
, pid_list_size
);
2444 pid_list
= kalloc(pid_list_size_needed
);
2448 pid_list_size
= pid_list_size_needed
;
2452 PCHILDREN_FOREACH(parent
, p
) {
2453 if (p
->p_stat
== SIDL
) {
2457 pid_list
[pid_count
++] = proc_pid(p
);
2458 if (pid_count
>= pid_count_available
) {
2465 for (int i
= 0; i
< pid_count
; i
++) {
2466 p
= proc_find(pid_list
[i
]);
2471 int callout_ret
= callout(p
, arg
);
2473 switch (callout_ret
) {
2474 case PROC_RETURNED_DONE
:
2477 case PROC_CLAIMED_DONE
:
2486 panic("proc_childrenwalk: callout returned %d for pid %d",
2487 callout_ret
, pid_list
[i
]);
2493 kfree(pid_list
, pid_list_size
);
2501 proc_iterate_fn_t callout
,
2503 proc_iterate_fn_t filterfn
,
2508 vm_size_t pid_list_size
= 0;
2509 vm_size_t pid_list_size_needed
= 0;
2511 int pid_count_available
= 0;
2515 assert(pgrp
!= NULL
);
2516 assert(callout
!= NULL
);
2521 pid_count_available
= pgrp
->pg_membercnt
;
2522 if (pid_count_available
== 0) {
2527 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2528 if (pid_list_size
>= pid_list_size_needed
) {
2533 if (pid_list_size
!= 0) {
2534 kfree(pid_list
, pid_list_size
);
2536 pid_list
= kalloc(pid_list_size_needed
);
2540 pid_list_size
= pid_list_size_needed
;
2545 PGMEMBERS_FOREACH(pgrp
, p
) {
2546 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2549 pid_list
[pid_count
++] = proc_pid(p
);
2550 if (pid_count
>= pid_count_available
) {
2557 if (flags
& PGRP_DROPREF
) {
2561 for (int i
= 0; i
< pid_count
; i
++) {
2562 /* do not handle kernproc */
2563 if (pid_list
[i
] == 0) {
2566 p
= proc_find(pid_list
[i
]);
2570 if (p
->p_pgrpid
!= pgid
) {
2575 int callout_ret
= callout(p
, arg
);
2577 switch (callout_ret
) {
2584 case PROC_RETURNED_DONE
:
2587 case PROC_CLAIMED_DONE
:
2591 panic("pgrp_iterate: callout returned %d for pid %d",
2592 callout_ret
, pid_list
[i
]);
2597 kfree(pid_list
, pid_list_size
);
2602 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2605 child
->p_pgrp
= pgrp
;
2606 child
->p_pgrpid
= pgrp
->pg_id
;
2607 child
->p_listflag
|= P_LIST_INPGRP
;
2609 * When pgrp is being freed , a process can still
2610 * request addition using setpgid from bash when
2611 * login is terminated (login cycler) return ESRCH
2612 * Safe to hold lock due to refcount on pgrp
2614 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2615 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2618 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2619 panic("pgrp_add : pgrp is dead adding process");
2623 pgrp
->pg_membercnt
++;
2624 if ( parent
!= PROC_NULL
) {
2625 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2627 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2632 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2633 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2639 pgrp_remove(struct proc
* p
)
2646 #if __PROC_INTERNAL_DEBUG
2647 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2648 panic("removing from pglist but no named ref\n");
2650 p
->p_pgrpid
= PGRPID_DEAD
;
2651 p
->p_listflag
&= ~P_LIST_INPGRP
;
2655 if (pg
== PGRP_NULL
)
2656 panic("pgrp_remove: pg is NULL");
2660 if (pg
->pg_membercnt
< 0)
2661 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",pg
, p
);
2663 LIST_REMOVE(p
, p_pglist
);
2664 if (pg
->pg_members
.lh_first
== 0) {
2666 pgdelete_dropref(pg
);
2674 /* cannot use proc_pgrp as it maybe stalled */
2676 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2678 struct pgrp
* oldpg
;
2684 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2685 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2686 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2689 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2692 if (oldpg
== PGRP_NULL
)
2693 panic("pgrp_replace: oldpg NULL");
2694 oldpg
->pg_refcount
++;
2695 #if __PROC_INTERNAL_DEBUG
2696 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2697 panic("removing from pglist but no named ref\n");
2699 p
->p_pgrpid
= PGRPID_DEAD
;
2700 p
->p_listflag
&= ~P_LIST_INPGRP
;
2706 oldpg
->pg_membercnt
--;
2707 if (oldpg
->pg_membercnt
< 0)
2708 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",oldpg
, p
);
2709 LIST_REMOVE(p
, p_pglist
);
2710 if (oldpg
->pg_members
.lh_first
== 0) {
2712 pgdelete_dropref(oldpg
);
2720 p
->p_pgrpid
= newpg
->pg_id
;
2721 p
->p_listflag
|= P_LIST_INPGRP
;
2723 * When pgrp is being freed , a process can still
2724 * request addition using setpgid from bash when
2725 * login is terminated (login cycler) return ESRCH
2726 * Safe to hold lock due to refcount on pgrp
2728 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2729 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2732 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2733 panic("pgrp_add : pgrp is dead adding process");
2737 newpg
->pg_membercnt
++;
2738 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
2742 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
2743 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2746 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
2747 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
2748 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
2749 wakeup(&p
->p_pgrpid
);
2756 pgrp_lock(struct pgrp
* pgrp
)
2758 lck_mtx_lock(&pgrp
->pg_mlock
);
2762 pgrp_unlock(struct pgrp
* pgrp
)
2764 lck_mtx_unlock(&pgrp
->pg_mlock
);
2768 session_lock(struct session
* sess
)
2770 lck_mtx_lock(&sess
->s_mlock
);
2775 session_unlock(struct session
* sess
)
2777 lck_mtx_unlock(&sess
->s_mlock
);
2789 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2790 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2791 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2796 assert(pgrp
!= NULL
);
2798 if (pgrp
!= PGRP_NULL
) {
2799 pgrp
->pg_refcount
++;
2800 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0)
2801 panic("proc_pgrp: ref being povided for dead pgrp");
2810 tty_pgrp(struct tty
* tp
)
2812 struct pgrp
* pg
= PGRP_NULL
;
2817 if (pg
!= PGRP_NULL
) {
2818 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0)
2819 panic("tty_pgrp: ref being povided for dead pgrp");
2828 proc_session(proc_t p
)
2830 struct session
* sess
= SESSION_NULL
;
2833 return(SESSION_NULL
);
2837 /* wait during transitions */
2838 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2839 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2840 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2843 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
2844 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2845 panic("proc_session:returning sesssion ref on terminating session");
2853 session_rele(struct session
*sess
)
2856 if (--sess
->s_count
== 0) {
2857 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2858 panic("session_rele: terminating already terminated session");
2859 sess
->s_listflags
|= S_LIST_TERM
;
2860 LIST_REMOVE(sess
, s_hash
);
2861 sess
->s_listflags
|= S_LIST_DEAD
;
2862 if (sess
->s_count
!= 0)
2863 panic("session_rele: freeing session in use");
2865 #if CONFIG_FINE_LOCK_GROUPS
2866 lck_mtx_destroy(&sess
->s_mlock
, proc_mlock_grp
);
2868 lck_mtx_destroy(&sess
->s_mlock
, proc_lck_grp
);
2870 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
2876 proc_transstart(proc_t p
, int locked
, int non_blocking
)
2880 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2881 if (((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
) || non_blocking
) {
2886 p
->p_lflag
|= P_LTRANSWAIT
;
2887 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2889 p
->p_lflag
|= P_LINTRANSIT
;
2890 p
->p_transholder
= current_thread();
2897 proc_transcommit(proc_t p
, int locked
)
2902 assert ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
);
2903 assert (p
->p_transholder
== current_thread());
2904 p
->p_lflag
|= P_LTRANSCOMMIT
;
2906 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2907 p
->p_lflag
&= ~P_LTRANSWAIT
;
2908 wakeup(&p
->p_lflag
);
2915 proc_transend(proc_t p
, int locked
)
2920 p
->p_lflag
&= ~( P_LINTRANSIT
| P_LTRANSCOMMIT
);
2921 p
->p_transholder
= NULL
;
2923 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2924 p
->p_lflag
&= ~P_LTRANSWAIT
;
2925 wakeup(&p
->p_lflag
);
2932 proc_transwait(proc_t p
, int locked
)
2936 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2937 if ((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
&& current_proc() == p
) {
2942 p
->p_lflag
|= P_LTRANSWAIT
;
2943 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2951 proc_klist_lock(void)
2953 lck_mtx_lock(proc_klist_mlock
);
2957 proc_klist_unlock(void)
2959 lck_mtx_unlock(proc_klist_mlock
);
2963 proc_knote(struct proc
* p
, long hint
)
2966 KNOTE(&p
->p_klist
, hint
);
2967 proc_klist_unlock();
2971 proc_knote_drain(struct proc
*p
)
2973 struct knote
*kn
= NULL
;
2976 * Clear the proc's klist to avoid references after the proc is reaped.
2979 while ((kn
= SLIST_FIRST(&p
->p_klist
))) {
2980 kn
->kn_ptr
.p_proc
= PROC_NULL
;
2981 KNOTE_DETACH(&p
->p_klist
, kn
);
2983 proc_klist_unlock();
2987 proc_setregister(proc_t p
)
2990 p
->p_lflag
|= P_LREGISTER
;
2995 proc_resetregister(proc_t p
)
2998 p
->p_lflag
&= ~P_LREGISTER
;
3003 proc_pgrpid(proc_t p
)
3011 return current_proc()->p_pgrpid
;
3015 /* return control and action states */
3017 proc_getpcontrol(int pid
, int * pcontrolp
)
3024 if (pcontrolp
!= NULL
)
3025 *pcontrolp
= p
->p_pcaction
;
3032 proc_dopcontrol(proc_t p
)
3038 pcontrol
= PROC_CONTROL_STATE(p
);
3040 if (PROC_ACTION_STATE(p
) == 0) {
3043 PROC_SETACTION_STATE(p
);
3045 printf("low swap: throttling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3049 PROC_SETACTION_STATE(p
);
3051 printf("low swap: suspending pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3052 task_suspend(p
->task
);
3056 PROC_SETACTION_STATE(p
);
3058 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3059 psignal(p
, SIGKILL
);
3069 return(PROC_RETURNED
);
3074 * Resume a throttled or suspended process. This is an internal interface that's only
3075 * used by the user level code that presents the GUI when we run out of swap space and
3076 * hence is restricted to processes with superuser privileges.
3080 proc_resetpcontrol(int pid
)
3085 proc_t self
= current_proc();
3087 /* if the process has been validated to handle resource control or root is valid one */
3088 if (((self
->p_lflag
& P_LVMRSRCOWNER
) == 0) && (error
= suser(kauth_cred_get(), 0)))
3097 pcontrol
= PROC_CONTROL_STATE(p
);
3099 if(PROC_ACTION_STATE(p
) !=0) {
3102 PROC_RESETACTION_STATE(p
);
3104 printf("low swap: unthrottling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3108 PROC_RESETACTION_STATE(p
);
3110 printf("low swap: resuming pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3111 task_resume(p
->task
);
3116 PROC_SETACTION_STATE(p
);
3118 printf("low swap: attempt to unkill pid %d (%s) ignored\n", p
->p_pid
, p
->p_comm
);
3134 struct no_paging_space
3136 uint64_t pcs_max_size
;
3137 uint64_t pcs_uniqueid
;
3140 uint64_t pcs_total_size
;
3142 uint64_t npcs_max_size
;
3143 uint64_t npcs_uniqueid
;
3145 int npcs_proc_count
;
3146 uint64_t npcs_total_size
;
3148 int apcs_proc_count
;
3149 uint64_t apcs_total_size
;
3154 proc_pcontrol_filter(proc_t p
, void *arg
)
3156 struct no_paging_space
*nps
;
3157 uint64_t compressed
;
3159 nps
= (struct no_paging_space
*)arg
;
3161 compressed
= get_task_compressed(p
->task
);
3163 if (PROC_CONTROL_STATE(p
)) {
3164 if (PROC_ACTION_STATE(p
) == 0) {
3165 if (compressed
> nps
->pcs_max_size
) {
3166 nps
->pcs_pid
= p
->p_pid
;
3167 nps
->pcs_uniqueid
= p
->p_uniqueid
;
3168 nps
->pcs_max_size
= compressed
;
3170 nps
->pcs_total_size
+= compressed
;
3171 nps
->pcs_proc_count
++;
3173 nps
->apcs_total_size
+= compressed
;
3174 nps
->apcs_proc_count
++;
3177 if (compressed
> nps
->npcs_max_size
) {
3178 nps
->npcs_pid
= p
->p_pid
;
3179 nps
->npcs_uniqueid
= p
->p_uniqueid
;
3180 nps
->npcs_max_size
= compressed
;
3182 nps
->npcs_total_size
+= compressed
;
3183 nps
->npcs_proc_count
++;
3191 proc_pcontrol_null(__unused proc_t p
, __unused
void *arg
)
3193 return(PROC_RETURNED
);
3198 * Deal with the low on compressor pool space condition... this function
3199 * gets called when we are approaching the limits of the compressor pool or
3200 * we are unable to create a new swap file.
3201 * Since this eventually creates a memory deadlock situtation, we need to take action to free up
3202 * memory resources (both compressed and uncompressed) in order to prevent the system from hanging completely.
3203 * There are 2 categories of processes to deal with. Those that have an action
3204 * associated with them by the task itself and those that do not. Actionable
3205 * tasks can have one of three categories specified: ones that
3206 * can be killed immediately, ones that should be suspended, and ones that should
3207 * be throttled. Processes that do not have an action associated with them are normally
3208 * ignored unless they are utilizing such a large percentage of the compressor pool (currently 50%)
3209 * that only by killing them can we hope to put the system back into a usable state.
3212 #define NO_PAGING_SPACE_DEBUG 0
3214 extern uint64_t vm_compressor_pages_compressed(void);
3216 struct timeval last_no_space_action
= {0, 0};
3218 #if DEVELOPMENT || DEBUG
3219 extern boolean_t kill_on_no_paging_space
;
3220 #endif /* DEVELOPMENT || DEBUG */
3222 #define MB_SIZE (1024 * 1024ULL)
3223 boolean_t
memorystatus_kill_on_VM_thrashing(boolean_t
);
3225 extern int32_t max_kill_priority
;
3226 extern int memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index
);
3229 no_paging_space_action()
3232 struct no_paging_space nps
;
3236 * Throttle how often we come through here. Once every 5 seconds should be plenty.
3240 if (now
.tv_sec
<= last_no_space_action
.tv_sec
+ 5)
3244 * Examine all processes and find the biggest (biggest is based on the number of pages this
3245 * task has in the compressor pool) that has been marked to have some action
3246 * taken when swap space runs out... we also find the biggest that hasn't been marked for
3249 * If the biggest non-actionable task is over the "dangerously big" threashold (currently 50% of
3250 * the total number of pages held by the compressor, we go ahead and kill it since no other task
3251 * can have any real effect on the situation. Otherwise, we go after the actionable process.
3253 bzero(&nps
, sizeof(nps
));
3255 proc_iterate(PROC_ALLPROCLIST
, proc_pcontrol_null
, (void *)NULL
, proc_pcontrol_filter
, (void *)&nps
);
3257 #if NO_PAGING_SPACE_DEBUG
3258 printf("low swap: npcs_proc_count = %d, npcs_total_size = %qd, npcs_max_size = %qd\n",
3259 nps
.npcs_proc_count
, nps
.npcs_total_size
, nps
.npcs_max_size
);
3260 printf("low swap: pcs_proc_count = %d, pcs_total_size = %qd, pcs_max_size = %qd\n",
3261 nps
.pcs_proc_count
, nps
.pcs_total_size
, nps
.pcs_max_size
);
3262 printf("low swap: apcs_proc_count = %d, apcs_total_size = %qd\n",
3263 nps
.apcs_proc_count
, nps
.apcs_total_size
);
3265 if (nps
.npcs_max_size
> (vm_compressor_pages_compressed() * 50) / 100) {
3267 * for now we'll knock out any task that has more then 50% of the pages
3268 * held by the compressor
3270 if ((p
= proc_find(nps
.npcs_pid
)) != PROC_NULL
) {
3272 if (nps
.npcs_uniqueid
== p
->p_uniqueid
) {
3274 * verify this is still the same process
3275 * in case the proc exited and the pid got reused while
3276 * we were finishing the proc_iterate and getting to this point
3278 last_no_space_action
= now
;
3280 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
));
3281 psignal(p
, SIGKILL
);
3293 * We have some processes within our jetsam bands of consideration and hence can be killed.
3294 * So we will invoke the memorystatus thread to go ahead and kill something.
3296 if (memorystatus_get_proccnt_upto_priority(max_kill_priority
) > 0) {
3298 last_no_space_action
= now
;
3299 memorystatus_kill_on_VM_thrashing(TRUE
/* async */);
3304 * No eligible processes to kill. So let's suspend/kill the largest
3305 * process depending on its policy control specifications.
3308 if (nps
.pcs_max_size
> 0) {
3309 if ((p
= proc_find(nps
.pcs_pid
)) != PROC_NULL
) {
3311 if (nps
.pcs_uniqueid
== p
->p_uniqueid
) {
3313 * verify this is still the same process
3314 * in case the proc exited and the pid got reused while
3315 * we were finishing the proc_iterate and getting to this point
3317 last_no_space_action
= now
;
3329 last_no_space_action
= now
;
3331 printf("low swap: unable to find any eligible processes to take action on\n");
3337 proc_trace_log(__unused proc_t p
, struct proc_trace_log_args
*uap
, __unused
int *retval
)
3340 proc_t target_proc
= PROC_NULL
;
3341 pid_t target_pid
= uap
->pid
;
3342 uint64_t target_uniqueid
= uap
->uniqueid
;
3343 task_t target_task
= NULL
;
3345 if (priv_check_cred(kauth_cred_get(), PRIV_PROC_TRACE_INSPECT
, 0)) {
3349 target_proc
= proc_find(target_pid
);
3350 if (target_proc
!= PROC_NULL
) {
3351 if (target_uniqueid
!= proc_uniqueid(target_proc
)) {
3356 target_task
= proc_task(target_proc
);
3357 if (task_send_trace_memory(target_task
, target_pid
, target_uniqueid
)) {
3365 if (target_proc
!= PROC_NULL
)
3366 proc_rele(target_proc
);
3370 #if VM_SCAN_FOR_SHADOW_CHAIN
3371 extern int vm_map_shadow_max(vm_map_t map
);
3372 int proc_shadow_max(void);
3373 int proc_shadow_max(void)
3382 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
3383 if (p
->p_stat
== SIDL
)
3389 map
= get_task_map(task
);
3393 retval
= vm_map_shadow_max(map
);
3401 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */
3403 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
);
3404 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
)
3406 if (target_proc
!= NULL
) {
3407 target_proc
->p_responsible_pid
= responsible_pid
;
3413 proc_chrooted(proc_t p
)
3419 retval
= (p
->p_fd
->fd_rdir
!= NULL
) ? 1 : 0;
3427 proc_get_uthread_uu_threadlist(void * uthread_v
)
3429 uthread_t uth
= (uthread_t
)uthread_v
;
3430 return (uth
!= NULL
) ? uth
->uu_threadlist
: NULL
;