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 #if CONFIG_MEMORYSTATUS
116 #include <sys/kern_memorystatus.h>
120 #include <security/mac_framework.h>
123 #include <libkern/crypto/sha1.h>
126 * Structure associated with user cacheing.
129 LIST_ENTRY(uidinfo
) ui_hash
;
133 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
134 LIST_HEAD(uihashhead
, uidinfo
) *uihashtbl
;
135 u_long uihash
; /* size of hash table - 1 */
138 * Other process lists
140 struct pidhashhead
*pidhashtbl
;
142 struct pgrphashhead
*pgrphashtbl
;
144 struct sesshashhead
*sesshashtbl
;
147 struct proclist allproc
;
148 struct proclist zombproc
;
149 extern struct tty cons
;
154 #define __PROC_INTERNAL_DEBUG 1
157 /* Name to give to core files */
158 __XNU_PRIVATE_EXTERN
char corefilename
[MAXPATHLEN
+1] = {"/cores/core.%P"};
162 #include <kern/backtrace.h>
165 static void orphanpg(struct pgrp
* pg
);
166 void proc_name_kdp(task_t t
, char * buf
, int size
);
167 void * proc_get_uthread_uu_threadlist(void * uthread_v
);
168 int proc_threadname_kdp(void * uth
, char * buf
, size_t size
);
169 void proc_starttime_kdp(void * p
, uint64_t * tv_sec
, uint64_t * tv_usec
, uint64_t * abstime
);
170 char * proc_name_address(void * p
);
172 /* TODO: make a header that's exported and usable in osfmk */
173 char* proc_best_name(proc_t p
);
175 static void pgrp_add(struct pgrp
* pgrp
, proc_t parent
, proc_t child
);
176 static void pgrp_remove(proc_t p
);
177 static void pgrp_replace(proc_t p
, struct pgrp
*pgrp
);
178 static void pgdelete_dropref(struct pgrp
*pgrp
);
179 extern void pg_rele_dropref(struct pgrp
* pgrp
);
180 static int csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaddittoken
);
181 static boolean_t
proc_parent_is_currentproc(proc_t p
);
183 struct fixjob_iterargs
{
185 struct session
* mysession
;
189 int fixjob_callback(proc_t
, void *);
191 uint64_t get_current_unique_pid(void);
195 get_current_unique_pid(void)
197 proc_t p
= current_proc();
200 return p
->p_uniqueid
;
206 * Initialize global process hashing structures.
212 LIST_INIT(&zombproc
);
213 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
214 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
215 sesshashtbl
= hashinit(maxproc
/ 4, M_PROC
, &sesshash
);
216 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
218 personas_bootstrap();
223 * Change the count associated with number of processes
224 * a given user is using. This routine protects the uihash
228 chgproccnt(uid_t uid
, int diff
)
231 struct uidinfo
*newuip
= NULL
;
232 struct uihashhead
*uipp
;
238 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
)
239 if (uip
->ui_uid
== uid
)
242 uip
->ui_proccnt
+= diff
;
243 if (uip
->ui_proccnt
> 0) {
244 retval
= uip
->ui_proccnt
;
248 if (uip
->ui_proccnt
< 0)
249 panic("chgproccnt: procs < 0");
250 LIST_REMOVE(uip
, ui_hash
);
253 FREE_ZONE(uip
, sizeof(*uip
), M_PROC
);
262 panic("chgproccnt: lost user");
264 if (newuip
!= NULL
) {
267 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
269 uip
->ui_proccnt
= diff
;
275 MALLOC_ZONE(newuip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
277 panic("chgproccnt: M_PROC zone depleted");
281 FREE_ZONE(newuip
, sizeof(*uip
), M_PROC
);
286 * Is p an inferior of the current process?
294 for (; p
!= current_proc(); p
= p
->p_pptr
)
304 * Is p an inferior of t ?
307 isinferior(proc_t p
, proc_t t
)
313 /* if p==t they are not inferior */
318 for (; p
!= t
; p
= p
->p_pptr
) {
321 /* Detect here if we're in a cycle */
322 if ((p
->p_pid
== 0) || (p
->p_pptr
== start
) || (nchecked
>= nprocs
))
332 proc_isinferior(int pid1
, int pid2
)
334 proc_t p
= PROC_NULL
;
335 proc_t t
= PROC_NULL
;
338 if (((p
= proc_find(pid1
)) != (proc_t
)0 ) && ((t
= proc_find(pid2
)) != (proc_t
)0))
339 retval
= isinferior(p
, t
);
352 return(proc_findinternal(pid
, 0));
356 proc_findinternal(int pid
, int locked
)
358 proc_t p
= PROC_NULL
;
364 p
= pfind_locked(pid
);
365 if ((p
== PROC_NULL
) || (p
!= proc_ref_locked(p
)))
376 proc_findthread(thread_t thread
)
378 proc_t p
= PROC_NULL
;
382 uth
= get_bsdthread_info(thread
);
383 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
386 p
= (proc_t
)(get_bsdthreadtask_info(thread
));
387 p
= proc_ref_locked(p
);
394 uthread_reset_proc_refcount(void *uthread
) {
397 if (proc_ref_tracking_disabled
) {
401 uth
= (uthread_t
) uthread
;
403 uth
->uu_proc_refcount
= 0;
408 uthread_get_proc_refcount(void *uthread
) {
411 if (proc_ref_tracking_disabled
) {
415 uth
= (uthread_t
) uthread
;
417 return uth
->uu_proc_refcount
;
421 record_procref(proc_t p
, int count
) {
424 if (proc_ref_tracking_disabled
) {
428 uth
= current_uthread();
429 uth
->uu_proc_refcount
+= count
;
432 if (uth
->uu_pindex
< NUM_PROC_REFS_TO_TRACK
) {
433 backtrace((uintptr_t *) &uth
->uu_proc_pcs
[uth
->uu_pindex
], PROC_REF_STACK_DEPTH
);
435 uth
->uu_proc_ps
[uth
->uu_pindex
] = p
;
460 if (p
!= proc_ref_locked(p
))
468 proc_ref_locked(proc_t p
)
472 /* if process still in creation return failure */
473 if ((p
== PROC_NULL
) || ((p
->p_listflag
& P_LIST_INCREATE
) != 0))
475 /* do not return process marked for termination */
476 if ((p
->p_stat
!= SZOMB
) && ((p
->p_listflag
& P_LIST_EXITED
) == 0) && ((p
->p_listflag
& (P_LIST_DRAINWAIT
| P_LIST_DRAIN
| P_LIST_DEAD
)) == 0)) {
479 record_procref(p
, 1);
489 proc_rele_locked(proc_t p
)
492 if (p
->p_refcount
> 0) {
495 record_procref(p
, -1);
497 if ((p
->p_refcount
== 0) && ((p
->p_listflag
& P_LIST_DRAINWAIT
) == P_LIST_DRAINWAIT
)) {
498 p
->p_listflag
&= ~P_LIST_DRAINWAIT
;
499 wakeup(&p
->p_refcount
);
502 panic("proc_rele_locked -ve ref\n");
507 proc_find_zombref(int pid
)
514 p
= pfind_locked(pid
);
516 /* should we bail? */
517 if ((p
== PROC_NULL
) /* not found */
518 || ((p
->p_listflag
& P_LIST_INCREATE
) != 0) /* not created yet */
519 || ((p
->p_listflag
& P_LIST_EXITED
) == 0)) { /* not started exit */
525 /* If someone else is controlling the (unreaped) zombie - wait */
526 if ((p
->p_listflag
& P_LIST_WAITING
) != 0) {
527 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
530 p
->p_listflag
|= P_LIST_WAITING
;
538 proc_drop_zombref(proc_t p
)
541 if ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
542 p
->p_listflag
&= ~P_LIST_WAITING
;
550 proc_refdrain(proc_t p
)
555 p
->p_listflag
|= P_LIST_DRAIN
;
556 while (p
->p_refcount
) {
557 p
->p_listflag
|= P_LIST_DRAINWAIT
;
558 msleep(&p
->p_refcount
, proc_list_mlock
, 0, "proc_refdrain", 0) ;
560 p
->p_listflag
&= ~P_LIST_DRAIN
;
561 p
->p_listflag
|= P_LIST_DEAD
;
569 proc_parentholdref(proc_t p
)
571 proc_t parent
= PROC_NULL
;
579 if ((pp
== PROC_NULL
) || (pp
->p_stat
== SZOMB
) || ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
))) {
584 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == P_LIST_CHILDDRSTART
) {
585 pp
->p_listflag
|= P_LIST_CHILDDRWAIT
;
586 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
595 if ((pp
->p_listflag
& (P_LIST_CHILDDRSTART
| P_LIST_CHILDDRAINED
)) == 0) {
606 proc_parentdropref(proc_t p
, int listlocked
)
611 if (p
->p_parentref
> 0) {
613 if ((p
->p_parentref
== 0) && ((p
->p_listflag
& P_LIST_PARENTREFWAIT
) == P_LIST_PARENTREFWAIT
)) {
614 p
->p_listflag
&= ~P_LIST_PARENTREFWAIT
;
615 wakeup(&p
->p_parentref
);
618 panic("proc_parentdropref -ve ref\n");
626 proc_childdrainstart(proc_t p
)
628 #if __PROC_INTERNAL_DEBUG
629 if ((p
->p_listflag
& P_LIST_CHILDDRSTART
) == P_LIST_CHILDDRSTART
)
630 panic("proc_childdrainstart: childdrain already started\n");
632 p
->p_listflag
|= P_LIST_CHILDDRSTART
;
633 /* wait for all that hold parentrefs to drop */
634 while (p
->p_parentref
> 0) {
635 p
->p_listflag
|= P_LIST_PARENTREFWAIT
;
636 msleep(&p
->p_parentref
, proc_list_mlock
, 0, "proc_childdrainstart", 0) ;
642 proc_childdrainend(proc_t p
)
644 #if __PROC_INTERNAL_DEBUG
645 if (p
->p_childrencnt
> 0)
646 panic("exiting: children stil hanging around\n");
648 p
->p_listflag
|= P_LIST_CHILDDRAINED
;
649 if ((p
->p_listflag
& (P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
)) != 0) {
650 p
->p_listflag
&= ~(P_LIST_CHILDLKWAIT
|P_LIST_CHILDDRWAIT
);
651 wakeup(&p
->p_childrencnt
);
656 proc_checkdeadrefs(__unused proc_t p
)
658 #if __PROC_INTERNAL_DEBUG
659 if ((p
->p_listflag
& P_LIST_INHASH
) != 0)
660 panic("proc being freed and still in hash %p: %u\n", p
, p
->p_listflag
);
661 if (p
->p_childrencnt
!= 0)
662 panic("proc being freed and pending children cnt %p:%d\n", p
, p
->p_childrencnt
);
663 if (p
->p_refcount
!= 0)
664 panic("proc being freed and pending refcount %p:%d\n", p
, p
->p_refcount
);
665 if (p
->p_parentref
!= 0)
666 panic("proc being freed and pending parentrefs %p:%d\n", p
, p
->p_parentref
);
689 return (current_proc()->p_pid
);
695 return (current_proc()->p_ppid
);
699 proc_selfcsflags(void)
701 return (current_proc()->p_csflags
);
706 dtrace_current_proc_vforking(void)
708 thread_t th
= current_thread();
709 struct uthread
*ut
= get_bsdthread_info(th
);
712 ((ut
->uu_flag
& (UT_VFORK
|UT_VFORKING
)) == (UT_VFORK
|UT_VFORKING
))) {
714 * Handle the narrow window where we're in the vfork syscall,
715 * but we're not quite ready to claim (in particular, to DTrace)
716 * that we're running as the child.
718 return (get_bsdtask_info(get_threadtask(th
)));
720 return (current_proc());
724 dtrace_proc_selfpid(void)
726 return (dtrace_current_proc_vforking()->p_pid
);
730 dtrace_proc_selfppid(void)
732 return (dtrace_current_proc_vforking()->p_ppid
);
736 dtrace_proc_selfruid(void)
738 return (dtrace_current_proc_vforking()->p_ruid
);
740 #endif /* CONFIG_DTRACE */
743 proc_parent(proc_t p
)
751 parent
= proc_ref_locked(pp
);
752 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)){
753 pp
->p_listflag
|= P_LIST_CHILDLKWAIT
;
754 msleep(&pp
->p_childrencnt
, proc_list_mlock
, 0, "proc_parent", 0);
762 proc_parent_is_currentproc(proc_t p
)
764 boolean_t ret
= FALSE
;
767 if (p
->p_pptr
== current_proc())
775 proc_name(int pid
, char * buf
, int size
)
779 if ((p
= proc_find(pid
)) != PROC_NULL
) {
780 strlcpy(buf
, &p
->p_comm
[0], size
);
786 proc_name_kdp(task_t t
, char * buf
, int size
)
788 proc_t p
= get_bsdtask_info(t
);
792 if ((size_t)size
> sizeof(p
->p_comm
))
793 strlcpy(buf
, &p
->p_name
[0], MIN((int)sizeof(p
->p_name
), size
));
795 strlcpy(buf
, &p
->p_comm
[0], MIN((int)sizeof(p
->p_comm
), size
));
799 proc_threadname_kdp(void * uth
, char * buf
, size_t size
)
801 if (size
< MAXTHREADNAMESIZE
) {
802 /* this is really just a protective measure for the future in
803 * case the thread name size in stackshot gets out of sync with
804 * the BSD max thread name size. Note that bsd_getthreadname
805 * doesn't take input buffer size into account. */
810 bsd_getthreadname(uth
, buf
);
815 /* note that this function is generally going to be called from stackshot,
816 * and the arguments will be coming from a struct which is declared packed
817 * thus the input arguments will in general be unaligned. We have to handle
820 proc_starttime_kdp(void *p
, uint64_t *tv_sec
, uint64_t *tv_usec
, uint64_t *abstime
)
822 proc_t pp
= (proc_t
)p
;
825 } __attribute__((packed
));
827 if (pp
!= PROC_NULL
) {
829 ((struct uint64p
*)tv_sec
)->val
= pp
->p_start
.tv_sec
;
831 ((struct uint64p
*)tv_usec
)->val
= pp
->p_start
.tv_usec
;
832 if (abstime
!= NULL
) {
833 if (pp
->p_stats
!= NULL
)
834 *abstime
= pp
->p_stats
->ps_start
;
842 proc_name_address(void *p
)
844 return &((proc_t
)p
)->p_comm
[0];
848 proc_best_name(proc_t p
)
850 if (p
->p_name
[0] != 0)
851 return (&p
->p_name
[0]);
852 return (&p
->p_comm
[0]);
856 proc_selfname(char * buf
, int size
)
860 if ((p
= current_proc())!= (proc_t
)0) {
861 strlcpy(buf
, &p
->p_comm
[0], size
);
866 proc_signal(int pid
, int signum
)
870 if ((p
= proc_find(pid
)) != PROC_NULL
) {
877 proc_issignal(int pid
, sigset_t mask
)
882 if ((p
= proc_find(pid
)) != PROC_NULL
) {
883 error
= proc_pendingsignals(p
, mask
);
891 proc_noremotehang(proc_t p
)
896 retval
= p
->p_flag
& P_NOREMOTEHANG
;
897 return(retval
? 1: 0);
902 proc_exiting(proc_t p
)
907 retval
= p
->p_lflag
& P_LEXIT
;
908 return(retval
? 1: 0);
912 proc_forcequota(proc_t p
)
917 retval
= p
->p_flag
& P_FORCEQUOTA
;
918 return(retval
? 1: 0);
925 kauth_cred_t my_cred
;
928 my_cred
= kauth_cred_proc_ref(p
);
929 error
= suser(my_cred
, &p
->p_acflag
);
930 kauth_cred_unref(&my_cred
);
935 proc_task(proc_t proc
)
937 return (task_t
)proc
->task
;
941 * Obtain the first thread in a process
943 * XXX This is a bad thing to do; it exists predominantly to support the
944 * XXX use of proc_t's in places that should really be using
945 * XXX thread_t's instead. This maintains historical behaviour, but really
946 * XXX needs an audit of the context (proxy vs. not) to clean up.
949 proc_thread(proc_t proc
)
951 uthread_t uth
= TAILQ_FIRST(&proc
->p_uthlist
);
954 return(uth
->uu_context
.vc_thread
);
968 thread_t th
= current_thread();
970 return((struct uthread
*)get_bsdthread_info(th
));
975 proc_is64bit(proc_t p
)
977 return(IS_64BIT_PROCESS(p
));
981 proc_pidversion(proc_t p
)
983 return(p
->p_idversion
);
987 proc_persona_id(proc_t p
)
989 return (uint32_t)persona_id_from_proc(p
);
993 proc_getuid(proc_t p
)
999 proc_getgid(proc_t p
)
1005 proc_uniqueid(proc_t p
)
1007 return(p
->p_uniqueid
);
1011 proc_puniqueid(proc_t p
)
1013 return(p
->p_puniqueid
);
1017 proc_coalitionids(__unused proc_t p
, __unused
uint64_t ids
[COALITION_NUM_TYPES
])
1019 #if CONFIG_COALITIONS
1020 task_coalition_ids(p
->task
, ids
);
1022 memset(ids
, 0, sizeof(uint64_t [COALITION_NUM_TYPES
]));
1028 proc_was_throttled(proc_t p
)
1030 return (p
->was_throttled
);
1034 proc_did_throttle(proc_t p
)
1036 return (p
->did_throttle
);
1040 proc_getcdhash(proc_t p
, unsigned char *cdhash
)
1042 return vn_getcdhash(p
->p_textvp
, p
->p_textoff
, cdhash
);
1046 proc_getexecutableuuid(proc_t p
, unsigned char *uuidbuf
, unsigned long size
)
1048 if (size
>= sizeof(p
->p_uuid
)) {
1049 memcpy(uuidbuf
, p
->p_uuid
, sizeof(p
->p_uuid
));
1053 /* Return vnode for executable with an iocount. Must be released with vnode_put() */
1055 proc_getexecutablevnode(proc_t p
)
1057 vnode_t tvp
= p
->p_textvp
;
1059 if ( tvp
!= NULLVP
) {
1060 if (vnode_getwithref(tvp
) == 0) {
1070 bsd_set_dependency_capable(task_t task
)
1072 proc_t p
= get_bsdtask_info(task
);
1075 OSBitOrAtomic(P_DEPENDENCY_CAPABLE
, &p
->p_flag
);
1081 IS_64BIT_PROCESS(proc_t p
)
1083 if (p
&& (p
->p_flag
& P_LP64
))
1090 * Locate a process by number
1093 pfind_locked(pid_t pid
)
1103 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
) {
1104 if (p
->p_pid
== pid
) {
1106 for (q
= p
->p_hash
.le_next
; q
!= 0; q
= q
->p_hash
.le_next
) {
1107 if ((p
!=q
) && (q
->p_pid
== pid
))
1108 panic("two procs with same pid %p:%p:%d:%d\n", p
, q
, p
->p_pid
, q
->p_pid
);
1118 * Locate a zombie by PID
1120 __private_extern__ proc_t
1128 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
1129 if (p
->p_pid
== pid
)
1138 * Locate a process group by number
1147 pgrp
= pgfind_internal(pgid
);
1148 if ((pgrp
== NULL
) || ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) != 0))
1151 pgrp
->pg_refcount
++;
1159 pgfind_internal(pid_t pgid
)
1163 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
)
1164 if (pgrp
->pg_id
== pgid
)
1170 pg_rele(struct pgrp
* pgrp
)
1172 if(pgrp
== PGRP_NULL
)
1174 pg_rele_dropref(pgrp
);
1178 pg_rele_dropref(struct pgrp
* pgrp
)
1181 if ((pgrp
->pg_refcount
== 1) && ((pgrp
->pg_listflags
& PGRP_FLAG_TERMINATE
) == PGRP_FLAG_TERMINATE
)) {
1183 pgdelete_dropref(pgrp
);
1187 pgrp
->pg_refcount
--;
1192 session_find_internal(pid_t sessid
)
1194 struct session
*sess
;
1196 for (sess
= SESSHASH(sessid
)->lh_first
; sess
!= 0; sess
= sess
->s_hash
.le_next
)
1197 if (sess
->s_sid
== sessid
)
1204 * Make a new process ready to become a useful member of society by making it
1205 * visible in all the right places and initialize its own lists to empty.
1207 * Parameters: parent The parent of the process to insert
1208 * child The child process to insert
1212 * Notes: Insert a child process into the parents process group, assign
1213 * the child the parent process pointer and PPID of the parent,
1214 * place it on the parents p_children list as a sibling,
1215 * initialize its own child list, place it in the allproc list,
1216 * insert it in the proper hash bucket, and initialize its
1220 pinsertchild(proc_t parent
, proc_t child
)
1224 LIST_INIT(&child
->p_children
);
1225 TAILQ_INIT(&child
->p_evlist
);
1226 child
->p_pptr
= parent
;
1227 child
->p_ppid
= parent
->p_pid
;
1228 child
->p_puniqueid
= parent
->p_uniqueid
;
1230 pg
= proc_pgrp(parent
);
1231 pgrp_add(pg
, parent
, child
);
1236 #if CONFIG_MEMORYSTATUS
1237 memorystatus_add(child
, TRUE
);
1240 parent
->p_childrencnt
++;
1241 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1243 LIST_INSERT_HEAD(&allproc
, child
, p_list
);
1244 /* mark the completion of proc creation */
1245 child
->p_listflag
&= ~P_LIST_INCREATE
;
1251 * Move p to a new or existing process group (and session)
1253 * Returns: 0 Success
1254 * ESRCH No such process
1257 enterpgrp(proc_t p
, pid_t pgid
, int mksess
)
1260 struct pgrp
*mypgrp
;
1261 struct session
* procsp
;
1263 pgrp
= pgfind(pgid
);
1264 mypgrp
= proc_pgrp(p
);
1265 procsp
= proc_session(p
);
1268 if (pgrp
!= NULL
&& mksess
) /* firewalls */
1269 panic("enterpgrp: setsid into non-empty pgrp");
1270 if (SESS_LEADER(p
, procsp
))
1271 panic("enterpgrp: session leader attempted setpgrp");
1273 if (pgrp
== PGRP_NULL
) {
1274 pid_t savepid
= p
->p_pid
;
1275 proc_t np
= PROC_NULL
;
1280 if (p
->p_pid
!= pgid
)
1281 panic("enterpgrp: new pgrp and pid != pgid");
1283 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
1286 panic("enterpgrp: M_PGRP zone depleted");
1287 if ((np
= proc_find(savepid
)) == NULL
|| np
!= p
) {
1288 if (np
!= PROC_NULL
)
1290 if (mypgrp
!= PGRP_NULL
)
1292 if (procsp
!= SESSION_NULL
)
1293 session_rele(procsp
);
1294 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
1299 struct session
*sess
;
1304 MALLOC_ZONE(sess
, struct session
*,
1305 sizeof(struct session
), M_SESSION
, M_WAITOK
);
1307 panic("enterpgrp: M_SESSION zone depleted");
1309 sess
->s_sid
= p
->p_pid
;
1311 sess
->s_ttyvp
= NULL
;
1312 sess
->s_ttyp
= TTY_NULL
;
1314 sess
->s_listflags
= 0;
1315 sess
->s_ttypgrpid
= NO_PID
;
1316 #if CONFIG_FINE_LOCK_GROUPS
1317 lck_mtx_init(&sess
->s_mlock
, proc_mlock_grp
, proc_lck_attr
);
1319 lck_mtx_init(&sess
->s_mlock
, proc_lck_grp
, proc_lck_attr
);
1321 bcopy(procsp
->s_login
, sess
->s_login
,
1322 sizeof(sess
->s_login
));
1323 OSBitAndAtomic(~((uint32_t)P_CONTROLT
), &p
->p_flag
);
1325 LIST_INSERT_HEAD(SESSHASH(sess
->s_sid
), sess
, s_hash
);
1327 pgrp
->pg_session
= sess
;
1329 if (p
!= current_proc())
1330 panic("enterpgrp: mksession and p != curproc");
1334 pgrp
->pg_session
= procsp
;
1336 if ((pgrp
->pg_session
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1337 panic("enterpgrp: providing ref to terminating session ");
1338 pgrp
->pg_session
->s_count
++;
1342 #if CONFIG_FINE_LOCK_GROUPS
1343 lck_mtx_init(&pgrp
->pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
1345 lck_mtx_init(&pgrp
->pg_mlock
, proc_lck_grp
, proc_lck_attr
);
1347 LIST_INIT(&pgrp
->pg_members
);
1348 pgrp
->pg_membercnt
= 0;
1351 pgrp
->pg_refcount
= 1;
1352 pgrp
->pg_listflags
= 0;
1353 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
1355 } else if (pgrp
== mypgrp
) {
1359 if (procsp
!= SESSION_NULL
)
1360 session_rele(procsp
);
1364 if (procsp
!= SESSION_NULL
)
1365 session_rele(procsp
);
1367 * Adjust eligibility of affected pgrps to participate in job control.
1368 * Increment eligibility counts before decrementing, otherwise we
1369 * could reach 0 spuriously during the first call.
1371 fixjobc(p
, pgrp
, 1);
1372 fixjobc(p
, mypgrp
, 0);
1374 if(mypgrp
!= PGRP_NULL
)
1376 pgrp_replace(p
, pgrp
);
1383 * remove process from process group
1394 * delete a process group
1397 pgdelete_dropref(struct pgrp
*pgrp
)
1401 struct session
*sessp
;
1405 if (pgrp
->pg_membercnt
!= 0) {
1411 pgrp
->pg_refcount
--;
1412 if ((emptypgrp
== 0) || (pgrp
->pg_membercnt
!= 0)) {
1417 pgrp
->pg_listflags
|= PGRP_FLAG_TERMINATE
;
1419 if (pgrp
->pg_refcount
> 0) {
1424 pgrp
->pg_listflags
|= PGRP_FLAG_DEAD
;
1425 LIST_REMOVE(pgrp
, pg_hash
);
1429 ttyp
= SESSION_TP(pgrp
->pg_session
);
1430 if (ttyp
!= TTY_NULL
) {
1431 if (ttyp
->t_pgrp
== pgrp
) {
1433 /* Re-check after acquiring the lock */
1434 if (ttyp
->t_pgrp
== pgrp
) {
1435 ttyp
->t_pgrp
= NULL
;
1436 pgrp
->pg_session
->s_ttypgrpid
= NO_PID
;
1444 sessp
= pgrp
->pg_session
;
1445 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1446 panic("pg_deleteref: manipulating refs of already terminating session");
1447 if (--sessp
->s_count
== 0) {
1448 if ((sessp
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
1449 panic("pg_deleteref: terminating already terminated session");
1450 sessp
->s_listflags
|= S_LIST_TERM
;
1451 ttyp
= SESSION_TP(sessp
);
1452 LIST_REMOVE(sessp
, s_hash
);
1454 if (ttyp
!= TTY_NULL
) {
1456 if (ttyp
->t_session
== sessp
)
1457 ttyp
->t_session
= NULL
;
1461 sessp
->s_listflags
|= S_LIST_DEAD
;
1462 if (sessp
->s_count
!= 0)
1463 panic("pg_deleteref: freeing session in use");
1465 #if CONFIG_FINE_LOCK_GROUPS
1466 lck_mtx_destroy(&sessp
->s_mlock
, proc_mlock_grp
);
1468 lck_mtx_destroy(&sessp
->s_mlock
, proc_lck_grp
);
1470 FREE_ZONE(sessp
, sizeof(struct session
), M_SESSION
);
1473 #if CONFIG_FINE_LOCK_GROUPS
1474 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_mlock_grp
);
1476 lck_mtx_destroy(&pgrp
->pg_mlock
, proc_lck_grp
);
1478 FREE_ZONE(pgrp
, sizeof(*pgrp
), M_PGRP
);
1483 * Adjust pgrp jobc counters when specified process changes process group.
1484 * We count the number of processes in each process group that "qualify"
1485 * the group for terminal job control (those with a parent in a different
1486 * process group of the same session). If that count reaches zero, the
1487 * process group becomes orphaned. Check both the specified process'
1488 * process group and that of its children.
1489 * entering == 0 => p is leaving specified group.
1490 * entering == 1 => p is entering specified group.
1493 fixjob_callback(proc_t p
, void * arg
)
1495 struct fixjob_iterargs
*fp
;
1496 struct pgrp
* pg
, *hispg
;
1497 struct session
* mysession
, *hissess
;
1500 fp
= (struct fixjob_iterargs
*)arg
;
1502 mysession
= fp
->mysession
;
1503 entering
= fp
->entering
;
1505 hispg
= proc_pgrp(p
);
1506 hissess
= proc_session(p
);
1508 if ((hispg
!= pg
) &&
1509 (hissess
== mysession
)) {
1514 } else if (--hispg
->pg_jobc
== 0) {
1520 if (hissess
!= SESSION_NULL
)
1521 session_rele(hissess
);
1522 if (hispg
!= PGRP_NULL
)
1525 return(PROC_RETURNED
);
1529 fixjobc(proc_t p
, struct pgrp
*pgrp
, int entering
)
1531 struct pgrp
*hispgrp
= PGRP_NULL
;
1532 struct session
*hissess
= SESSION_NULL
;
1533 struct session
*mysession
= pgrp
->pg_session
;
1535 struct fixjob_iterargs fjarg
;
1536 boolean_t proc_parent_self
;
1539 * Check if p's parent is current proc, if yes then no need to take
1540 * a ref; calling proc_parent with current proc as parent may
1541 * deadlock if current proc is exiting.
1543 proc_parent_self
= proc_parent_is_currentproc(p
);
1544 if (proc_parent_self
)
1545 parent
= current_proc();
1547 parent
= proc_parent(p
);
1549 if (parent
!= PROC_NULL
) {
1550 hispgrp
= proc_pgrp(parent
);
1551 hissess
= proc_session(parent
);
1552 if (!proc_parent_self
)
1558 * Check p's parent to see whether p qualifies its own process
1559 * group; if so, adjust count for p's process group.
1561 if ((hispgrp
!= pgrp
) &&
1562 (hissess
== mysession
)) {
1567 }else if (--pgrp
->pg_jobc
== 0) {
1574 if (hissess
!= SESSION_NULL
)
1575 session_rele(hissess
);
1576 if (hispgrp
!= PGRP_NULL
)
1580 * Check this process' children to see whether they qualify
1581 * their process groups; if so, adjust counts for children's
1585 fjarg
.mysession
= mysession
;
1586 fjarg
.entering
= entering
;
1587 proc_childrenwalk(p
, fixjob_callback
, &fjarg
);
1591 * A process group has become orphaned; if there are any stopped processes in
1592 * the group, hang-up all process in that group.
1595 orphanpg(struct pgrp
*pgrp
)
1599 vm_size_t pid_list_size
= 0;
1600 vm_size_t pid_list_size_needed
= 0;
1602 int pid_count_available
= 0;
1604 assert(pgrp
!= NULL
);
1606 /* allocate outside of the pgrp_lock */
1610 boolean_t should_iterate
= FALSE
;
1611 pid_count_available
= 0;
1613 PGMEMBERS_FOREACH(pgrp
, p
) {
1614 pid_count_available
++;
1616 if (p
->p_stat
== SSTOP
) {
1617 should_iterate
= TRUE
;
1621 if (pid_count_available
== 0 || !should_iterate
) {
1626 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
1627 if (pid_list_size
>= pid_list_size_needed
) {
1632 if (pid_list_size
!= 0) {
1633 kfree(pid_list
, pid_list_size
);
1635 pid_list
= kalloc(pid_list_size_needed
);
1639 pid_list_size
= pid_list_size_needed
;
1642 /* no orphaned processes */
1643 if (pid_list_size
== 0) {
1648 PGMEMBERS_FOREACH(pgrp
, p
) {
1649 pid_list
[pid_count
++] = proc_pid(p
);
1650 if (pid_count
>= pid_count_available
) {
1656 if (pid_count
== 0) {
1660 for (int i
= 0; i
< pid_count
; i
++) {
1661 /* do not handle kernproc */
1662 if (pid_list
[i
] == 0) {
1665 p
= proc_find(pid_list
[i
]);
1670 proc_transwait(p
, 0);
1673 psignal(p
, SIGCONT
);
1678 kfree(pid_list
, pid_list_size
);
1683 proc_is_classic(proc_t p __unused
)
1688 /* XXX Why does this function exist? Need to kill it off... */
1690 current_proc_EXTERNAL(void)
1692 return (current_proc());
1696 proc_is_forcing_hfs_case_sensitivity(proc_t p
)
1698 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
) ? 1 : 0;
1703 * proc_core_name(name, uid, pid)
1704 * Expand the name described in corefilename, using name, uid, and pid.
1705 * corefilename is a printf-like string, with three format specifiers:
1706 * %N name of process ("name")
1707 * %P process id (pid)
1709 * For example, "%N.core" is the default; they can be disabled completely
1710 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1711 * This is controlled by the sysctl variable kern.corefile (see above).
1713 __private_extern__
int
1714 proc_core_name(const char *name
, uid_t uid
, pid_t pid
, char *cf_name
,
1717 const char *format
, *appendstr
;
1718 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
1721 if (cf_name
== NULL
)
1724 format
= corefilename
;
1725 for (i
= 0, n
= 0; n
< cf_name_len
&& format
[i
]; i
++) {
1726 switch (format
[i
]) {
1727 case '%': /* Format character */
1729 switch (format
[i
]) {
1733 case 'N': /* process name */
1736 case 'P': /* process id */
1737 snprintf(id_buf
, sizeof(id_buf
), "%u", pid
);
1740 case 'U': /* user id */
1741 snprintf(id_buf
, sizeof(id_buf
), "%u", uid
);
1744 case '\0': /* format string ended in % symbol */
1749 "Unknown format character %c in `%s'\n",
1752 l
= strlen(appendstr
);
1753 if ((n
+ l
) >= cf_name_len
)
1755 bcopy(appendstr
, cf_name
+ n
, l
);
1759 cf_name
[n
++] = format
[i
];
1762 if (format
[i
] != '\0')
1766 log(LOG_ERR
, "pid %ld (%s), uid (%u): corename is too long\n",
1767 (long)pid
, name
, (uint32_t)uid
);
1770 log(LOG_ERR
, "pid %ld (%s), uid (%u): unexpected end of string after %% token\n",
1771 (long)pid
, name
, (uint32_t)uid
);
1774 #endif /* CONFIG_COREDUMP */
1776 /* Code Signing related routines */
1779 csops(__unused proc_t p
, struct csops_args
*uap
, __unused
int32_t *retval
)
1781 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1782 uap
->usersize
, USER_ADDR_NULL
));
1786 csops_audittoken(__unused proc_t p
, struct csops_audittoken_args
*uap
, __unused
int32_t *retval
)
1788 if (uap
->uaudittoken
== USER_ADDR_NULL
)
1790 return(csops_internal(uap
->pid
, uap
->ops
, uap
->useraddr
,
1791 uap
->usersize
, uap
->uaudittoken
));
1795 csops_copy_token(void *start
, size_t length
, user_size_t usize
, user_addr_t uaddr
)
1797 char fakeheader
[8] = { 0 };
1800 if (usize
< sizeof(fakeheader
))
1803 /* if no blob, fill in zero header */
1804 if (NULL
== start
) {
1806 length
= sizeof(fakeheader
);
1807 } else if (usize
< length
) {
1808 /* ... if input too short, copy out length of entitlement */
1809 uint32_t length32
= htonl((uint32_t)length
);
1810 memcpy(&fakeheader
[4], &length32
, sizeof(length32
));
1812 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
1814 return ERANGE
; /* input buffer to short, ERANGE signals that */
1817 return copyout(start
, uaddr
, length
);
1821 csops_internal(pid_t pid
, int ops
, user_addr_t uaddr
, user_size_t usersize
, user_addr_t uaudittoken
)
1823 size_t usize
= (size_t)CAST_DOWN(size_t, usersize
);
1829 unsigned char cdhash
[SHA1_RESULTLEN
];
1830 audit_token_t token
;
1831 unsigned int upid
=0, uidversion
= 0;
1833 forself
= error
= 0;
1836 pid
= proc_selfpid();
1837 if (pid
== proc_selfpid())
1844 case CS_OPS_PIDOFFSET
:
1845 case CS_OPS_ENTITLEMENTS_BLOB
:
1846 case CS_OPS_IDENTITY
:
1848 break; /* not restricted to root */
1850 if (forself
== 0 && kauth_cred_issuser(kauth_cred_get()) != TRUE
)
1855 pt
= proc_find(pid
);
1856 if (pt
== PROC_NULL
)
1860 uidversion
= pt
->p_idversion
;
1861 if (uaudittoken
!= USER_ADDR_NULL
) {
1863 error
= copyin(uaudittoken
, &token
, sizeof(audit_token_t
));
1866 /* verify the audit token pid/idversion matches with proc */
1867 if ((token
.val
[5] != upid
) || (token
.val
[7] != uidversion
)) {
1875 case CS_OPS_MARKINVALID
:
1876 case CS_OPS_MARKHARD
:
1877 case CS_OPS_MARKKILL
:
1878 case CS_OPS_MARKRESTRICT
:
1879 case CS_OPS_SET_STATUS
:
1880 case CS_OPS_CLEARINSTALLER
:
1881 if ((error
= mac_proc_check_set_cs_info(current_proc(), pt
, ops
)))
1885 if ((error
= mac_proc_check_get_cs_info(current_proc(), pt
, ops
)))
1892 case CS_OPS_STATUS
: {
1896 retflags
= pt
->p_csflags
;
1897 if (cs_enforcement(pt
))
1898 retflags
|= CS_ENFORCEMENT
;
1899 if (csproc_get_platform_binary(pt
))
1900 retflags
|= CS_PLATFORM_BINARY
;
1901 if (csproc_get_platform_path(pt
))
1902 retflags
|= CS_PLATFORM_PATH
;
1905 if (uaddr
!= USER_ADDR_NULL
)
1906 error
= copyout(&retflags
, uaddr
, sizeof(uint32_t));
1909 case CS_OPS_MARKINVALID
:
1911 if ((pt
->p_csflags
& CS_VALID
) == CS_VALID
) { /* is currently valid */
1912 pt
->p_csflags
&= ~CS_VALID
; /* set invalid */
1913 if ((pt
->p_csflags
& CS_KILL
) == CS_KILL
) {
1914 pt
->p_csflags
|= CS_KILLED
;
1917 printf("CODE SIGNING: marked invalid by pid %d: "
1918 "p=%d[%s] honoring CS_KILL, final status 0x%x\n",
1919 proc_selfpid(), pt
->p_pid
, pt
->p_comm
, pt
->p_csflags
);
1921 psignal(pt
, SIGKILL
);
1929 case CS_OPS_MARKHARD
:
1931 pt
->p_csflags
|= CS_HARD
;
1932 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1933 /* @@@ allow? reject? kill? @@@ */
1941 case CS_OPS_MARKKILL
:
1943 pt
->p_csflags
|= CS_KILL
;
1944 if ((pt
->p_csflags
& CS_VALID
) == 0) {
1946 psignal(pt
, SIGKILL
);
1951 case CS_OPS_PIDOFFSET
:
1952 toff
= pt
->p_textoff
;
1954 error
= copyout(&toff
, uaddr
, sizeof(toff
));
1959 /* pt already holds a reference on its p_textvp */
1961 toff
= pt
->p_textoff
;
1963 if (tvp
== NULLVP
|| usize
!= SHA1_RESULTLEN
) {
1968 error
= vn_getcdhash(tvp
, toff
, cdhash
);
1972 error
= copyout(cdhash
, uaddr
, sizeof (cdhash
));
1977 case CS_OPS_ENTITLEMENTS_BLOB
: {
1983 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
1989 error
= cs_entitlements_blob_get(pt
, &start
, &length
);
1994 error
= csops_copy_token(start
, length
, usize
, uaddr
);
1997 case CS_OPS_MARKRESTRICT
:
1999 pt
->p_csflags
|= CS_RESTRICT
;
2003 case CS_OPS_SET_STATUS
: {
2006 if (usize
< sizeof(flags
)) {
2011 error
= copyin(uaddr
, &flags
, sizeof(flags
));
2015 /* only allow setting a subset of all code sign flags */
2017 CS_HARD
| CS_EXEC_SET_HARD
|
2018 CS_KILL
| CS_EXEC_SET_KILL
|
2021 CS_ENFORCEMENT
| CS_EXEC_SET_ENFORCEMENT
;
2024 if (pt
->p_csflags
& CS_VALID
)
2025 pt
->p_csflags
|= flags
;
2037 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2043 error
= cs_blob_get(pt
, &start
, &length
);
2048 error
= csops_copy_token(start
, length
, usize
, uaddr
);
2051 case CS_OPS_IDENTITY
: {
2052 const char *identity
;
2053 uint8_t fakeheader
[8];
2058 * Make identity have a blob header to make it
2059 * easier on userland to guess the identity
2062 if (usize
< sizeof(fakeheader
)) {
2066 memset(fakeheader
, 0, sizeof(fakeheader
));
2069 if ((pt
->p_csflags
& (CS_VALID
| CS_DEBUGGED
)) == 0) {
2075 identity
= cs_identity_get(pt
);
2077 if (identity
== NULL
) {
2082 length
= strlen(identity
) + 1; /* include NUL */
2083 idlen
= htonl(length
+ sizeof(fakeheader
));
2084 memcpy(&fakeheader
[4], &idlen
, sizeof(idlen
));
2086 error
= copyout(fakeheader
, uaddr
, sizeof(fakeheader
));
2090 if (usize
< sizeof(fakeheader
) + length
)
2092 else if (usize
> sizeof(fakeheader
))
2093 error
= copyout(identity
, uaddr
+ sizeof(fakeheader
), length
);
2098 case CS_OPS_CLEARINSTALLER
:
2100 pt
->p_csflags
&= ~(CS_INSTALLER
| CS_EXEC_SET_INSTALLER
);
2116 proc_iterate_fn_t callout
,
2118 proc_iterate_fn_t filterfn
,
2122 vm_size_t pid_list_size
= 0;
2123 vm_size_t pid_list_size_needed
= 0;
2125 int pid_count_available
= 0;
2127 assert(callout
!= NULL
);
2129 /* allocate outside of the proc_list_lock */
2133 pid_count_available
= nprocs
;
2134 assert(pid_count_available
> 0);
2136 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2137 if (pid_list_size
>= pid_list_size_needed
) {
2142 if (pid_list_size
!= 0) {
2143 kfree(pid_list
, pid_list_size
);
2145 pid_list
= kalloc(pid_list_size_needed
);
2149 pid_list_size
= pid_list_size_needed
;
2152 /* filter pids into pid_list */
2154 if (flags
& PROC_ALLPROCLIST
) {
2156 ALLPROC_FOREACH(p
) {
2157 /* ignore processes that are being forked */
2158 if (p
->p_stat
== SIDL
) {
2161 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2165 pid_list
[pid_count
++] = proc_pid(p
);
2166 if (pid_count
>= pid_count_available
) {
2172 if ((pid_count
< pid_count_available
) &&
2173 (flags
& PROC_ZOMBPROCLIST
))
2176 ZOMBPROC_FOREACH(p
) {
2177 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2181 pid_list
[pid_count
++] = proc_pid(p
);
2182 if (pid_count
>= pid_count_available
) {
2190 /* call callout on processes in the pid_list */
2192 for (int i
= 0; i
< pid_count
; i
++) {
2193 proc_t p
= proc_find(pid_list
[i
]);
2195 if ((flags
& PROC_NOWAITTRANS
) == 0) {
2196 proc_transwait(p
, 0);
2198 int callout_ret
= callout(p
, arg
);
2200 switch (callout_ret
) {
2201 case PROC_RETURNED_DONE
:
2204 case PROC_CLAIMED_DONE
:
2214 panic("proc_iterate: callout returned %d for pid %d",
2215 callout_ret
, pid_list
[i
]);
2218 } else if (flags
& PROC_ZOMBPROCLIST
) {
2219 p
= proc_find_zombref(pid_list
[i
]);
2223 int callout_ret
= callout(p
, arg
);
2225 switch (callout_ret
) {
2226 case PROC_RETURNED_DONE
:
2227 proc_drop_zombref(p
);
2229 case PROC_CLAIMED_DONE
:
2233 proc_drop_zombref(p
);
2239 panic("proc_iterate: callout returned %d for zombie pid %d",
2240 callout_ret
, pid_list
[i
]);
2247 kfree(pid_list
, pid_list_size
);
2254 proc_iterate_fn_t callout
,
2256 proc_iterate_fn_t filterfn
,
2261 assert(callout
!= NULL
);
2263 proc_shutdown_exitcount
= 0;
2269 ALLPROC_FOREACH(p
) {
2270 if ((filterfn
!= NULL
) && filterfn(p
, filterarg
) == 0) {
2273 p
= proc_ref_locked(p
);
2280 proc_transwait(p
, 0);
2281 (void)callout(p
, arg
);
2284 goto restart_foreach
;
2293 proc_iterate_fn_t callout
,
2297 vm_size_t pid_list_size
= 0;
2298 vm_size_t pid_list_size_needed
= 0;
2300 int pid_count_available
= 0;
2302 assert(parent
!= NULL
);
2303 assert(callout
!= NULL
);
2308 pid_count_available
= parent
->p_childrencnt
;
2309 if (pid_count_available
== 0) {
2314 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2315 if (pid_list_size
>= pid_list_size_needed
) {
2320 if (pid_list_size
!= 0) {
2321 kfree(pid_list
, pid_list_size
);
2323 pid_list
= kalloc(pid_list_size_needed
);
2327 pid_list_size
= pid_list_size_needed
;
2331 PCHILDREN_FOREACH(parent
, p
) {
2332 if (p
->p_stat
== SIDL
) {
2336 pid_list
[pid_count
++] = proc_pid(p
);
2337 if (pid_count
>= pid_count_available
) {
2344 for (int i
= 0; i
< pid_count
; i
++) {
2345 p
= proc_find(pid_list
[i
]);
2350 int callout_ret
= callout(p
, arg
);
2352 switch (callout_ret
) {
2353 case PROC_RETURNED_DONE
:
2356 case PROC_CLAIMED_DONE
:
2365 panic("proc_childrenwalk: callout returned %d for pid %d",
2366 callout_ret
, pid_list
[i
]);
2372 kfree(pid_list
, pid_list_size
);
2380 proc_iterate_fn_t callout
,
2382 proc_iterate_fn_t filterfn
,
2387 vm_size_t pid_list_size
= 0;
2388 vm_size_t pid_list_size_needed
= 0;
2390 int pid_count_available
= 0;
2394 assert(pgrp
!= NULL
);
2395 assert(callout
!= NULL
);
2400 pid_count_available
= pgrp
->pg_membercnt
;
2401 if (pid_count_available
== 0) {
2406 pid_list_size_needed
= pid_count_available
* sizeof(pid_t
);
2407 if (pid_list_size
>= pid_list_size_needed
) {
2412 if (pid_list_size
!= 0) {
2413 kfree(pid_list
, pid_list_size
);
2415 pid_list
= kalloc(pid_list_size_needed
);
2419 pid_list_size
= pid_list_size_needed
;
2424 PGMEMBERS_FOREACH(pgrp
, p
) {
2425 if ((filterfn
!= NULL
) && (filterfn(p
, filterarg
) == 0)) {
2428 pid_list
[pid_count
++] = proc_pid(p
);
2429 if (pid_count
>= pid_count_available
) {
2436 if (flags
& PGRP_DROPREF
) {
2440 for (int i
= 0; i
< pid_count
; i
++) {
2441 /* do not handle kernproc */
2442 if (pid_list
[i
] == 0) {
2445 p
= proc_find(pid_list
[i
]);
2449 if (p
->p_pgrpid
!= pgid
) {
2454 int callout_ret
= callout(p
, arg
);
2456 switch (callout_ret
) {
2463 case PROC_RETURNED_DONE
:
2466 case PROC_CLAIMED_DONE
:
2470 panic("pgrp_iterate: callout returned %d for pid %d",
2471 callout_ret
, pid_list
[i
]);
2476 kfree(pid_list
, pid_list_size
);
2481 pgrp_add(struct pgrp
* pgrp
, struct proc
* parent
, struct proc
* child
)
2484 child
->p_pgrp
= pgrp
;
2485 child
->p_pgrpid
= pgrp
->pg_id
;
2486 child
->p_listflag
|= P_LIST_INPGRP
;
2488 * When pgrp is being freed , a process can still
2489 * request addition using setpgid from bash when
2490 * login is terminated (login cycler) return ESRCH
2491 * Safe to hold lock due to refcount on pgrp
2493 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2494 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2497 if ((pgrp
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2498 panic("pgrp_add : pgrp is dead adding process");
2502 pgrp
->pg_membercnt
++;
2503 if ( parent
!= PROC_NULL
) {
2504 LIST_INSERT_AFTER(parent
, child
, p_pglist
);
2506 LIST_INSERT_HEAD(&pgrp
->pg_members
, child
, p_pglist
);
2511 if (((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (pgrp
->pg_membercnt
!= 0)) {
2512 pgrp
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2518 pgrp_remove(struct proc
* p
)
2525 #if __PROC_INTERNAL_DEBUG
2526 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2527 panic("removing from pglist but no named ref\n");
2529 p
->p_pgrpid
= PGRPID_DEAD
;
2530 p
->p_listflag
&= ~P_LIST_INPGRP
;
2534 if (pg
== PGRP_NULL
)
2535 panic("pgrp_remove: pg is NULL");
2539 if (pg
->pg_membercnt
< 0)
2540 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",pg
, p
);
2542 LIST_REMOVE(p
, p_pglist
);
2543 if (pg
->pg_members
.lh_first
== 0) {
2545 pgdelete_dropref(pg
);
2553 /* cannot use proc_pgrp as it maybe stalled */
2555 pgrp_replace(struct proc
* p
, struct pgrp
* newpg
)
2557 struct pgrp
* oldpg
;
2563 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2564 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2565 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2568 p
->p_listflag
|= P_LIST_PGRPTRANS
;
2571 if (oldpg
== PGRP_NULL
)
2572 panic("pgrp_replace: oldpg NULL");
2573 oldpg
->pg_refcount
++;
2574 #if __PROC_INTERNAL_DEBUG
2575 if ((p
->p_listflag
& P_LIST_INPGRP
) == 0)
2576 panic("removing from pglist but no named ref\n");
2578 p
->p_pgrpid
= PGRPID_DEAD
;
2579 p
->p_listflag
&= ~P_LIST_INPGRP
;
2585 oldpg
->pg_membercnt
--;
2586 if (oldpg
->pg_membercnt
< 0)
2587 panic("pgprp: -ve membercnt pgprp:%p p:%p\n",oldpg
, p
);
2588 LIST_REMOVE(p
, p_pglist
);
2589 if (oldpg
->pg_members
.lh_first
== 0) {
2591 pgdelete_dropref(oldpg
);
2599 p
->p_pgrpid
= newpg
->pg_id
;
2600 p
->p_listflag
|= P_LIST_INPGRP
;
2602 * When pgrp is being freed , a process can still
2603 * request addition using setpgid from bash when
2604 * login is terminated (login cycler) return ESRCH
2605 * Safe to hold lock due to refcount on pgrp
2607 if ((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) {
2608 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2611 if ((newpg
->pg_listflags
& PGRP_FLAG_DEAD
) == PGRP_FLAG_DEAD
)
2612 panic("pgrp_add : pgrp is dead adding process");
2616 newpg
->pg_membercnt
++;
2617 LIST_INSERT_HEAD(&newpg
->pg_members
, p
, p_pglist
);
2621 if (((newpg
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) == PGRP_FLAG_TERMINATE
) && (newpg
->pg_membercnt
!= 0)) {
2622 newpg
->pg_listflags
&= ~PGRP_FLAG_TERMINATE
;
2625 p
->p_listflag
&= ~P_LIST_PGRPTRANS
;
2626 if ((p
->p_listflag
& P_LIST_PGRPTRWAIT
) == P_LIST_PGRPTRWAIT
) {
2627 p
->p_listflag
&= ~P_LIST_PGRPTRWAIT
;
2628 wakeup(&p
->p_pgrpid
);
2635 pgrp_lock(struct pgrp
* pgrp
)
2637 lck_mtx_lock(&pgrp
->pg_mlock
);
2641 pgrp_unlock(struct pgrp
* pgrp
)
2643 lck_mtx_unlock(&pgrp
->pg_mlock
);
2647 session_lock(struct session
* sess
)
2649 lck_mtx_lock(&sess
->s_mlock
);
2654 session_unlock(struct session
* sess
)
2656 lck_mtx_unlock(&sess
->s_mlock
);
2668 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2669 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2670 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2675 assert(pgrp
!= NULL
);
2677 if (pgrp
!= PGRP_NULL
) {
2678 pgrp
->pg_refcount
++;
2679 if ((pgrp
->pg_listflags
& (PGRP_FLAG_TERMINATE
| PGRP_FLAG_DEAD
)) != 0)
2680 panic("proc_pgrp: ref being povided for dead pgrp");
2689 tty_pgrp(struct tty
* tp
)
2691 struct pgrp
* pg
= PGRP_NULL
;
2696 if (pg
!= PGRP_NULL
) {
2697 if ((pg
->pg_listflags
& PGRP_FLAG_DEAD
) != 0)
2698 panic("tty_pgrp: ref being povided for dead pgrp");
2707 proc_session(proc_t p
)
2709 struct session
* sess
= SESSION_NULL
;
2712 return(SESSION_NULL
);
2716 /* wait during transitions */
2717 while ((p
->p_listflag
& P_LIST_PGRPTRANS
) == P_LIST_PGRPTRANS
) {
2718 p
->p_listflag
|= P_LIST_PGRPTRWAIT
;
2719 (void)msleep(&p
->p_pgrpid
, proc_list_mlock
, 0, "proc_pgrp", 0);
2722 if ((p
->p_pgrp
!= PGRP_NULL
) && ((sess
= p
->p_pgrp
->pg_session
) != SESSION_NULL
)) {
2723 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2724 panic("proc_session:returning sesssion ref on terminating session");
2732 session_rele(struct session
*sess
)
2735 if (--sess
->s_count
== 0) {
2736 if ((sess
->s_listflags
& (S_LIST_TERM
| S_LIST_DEAD
)) != 0)
2737 panic("session_rele: terminating already terminated session");
2738 sess
->s_listflags
|= S_LIST_TERM
;
2739 LIST_REMOVE(sess
, s_hash
);
2740 sess
->s_listflags
|= S_LIST_DEAD
;
2741 if (sess
->s_count
!= 0)
2742 panic("session_rele: freeing session in use");
2744 #if CONFIG_FINE_LOCK_GROUPS
2745 lck_mtx_destroy(&sess
->s_mlock
, proc_mlock_grp
);
2747 lck_mtx_destroy(&sess
->s_mlock
, proc_lck_grp
);
2749 FREE_ZONE(sess
, sizeof(struct session
), M_SESSION
);
2755 proc_transstart(proc_t p
, int locked
, int non_blocking
)
2759 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2760 if (((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
) || non_blocking
) {
2765 p
->p_lflag
|= P_LTRANSWAIT
;
2766 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2768 p
->p_lflag
|= P_LINTRANSIT
;
2769 p
->p_transholder
= current_thread();
2776 proc_transcommit(proc_t p
, int locked
)
2781 assert ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
);
2782 assert (p
->p_transholder
== current_thread());
2783 p
->p_lflag
|= P_LTRANSCOMMIT
;
2785 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2786 p
->p_lflag
&= ~P_LTRANSWAIT
;
2787 wakeup(&p
->p_lflag
);
2794 proc_transend(proc_t p
, int locked
)
2799 p
->p_lflag
&= ~( P_LINTRANSIT
| P_LTRANSCOMMIT
);
2800 p
->p_transholder
= NULL
;
2802 if ((p
->p_lflag
& P_LTRANSWAIT
) == P_LTRANSWAIT
) {
2803 p
->p_lflag
&= ~P_LTRANSWAIT
;
2804 wakeup(&p
->p_lflag
);
2811 proc_transwait(proc_t p
, int locked
)
2815 while ((p
->p_lflag
& P_LINTRANSIT
) == P_LINTRANSIT
) {
2816 if ((p
->p_lflag
& P_LTRANSCOMMIT
) == P_LTRANSCOMMIT
&& current_proc() == p
) {
2821 p
->p_lflag
|= P_LTRANSWAIT
;
2822 msleep(&p
->p_lflag
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2830 proc_klist_lock(void)
2832 lck_mtx_lock(proc_klist_mlock
);
2836 proc_klist_unlock(void)
2838 lck_mtx_unlock(proc_klist_mlock
);
2842 proc_knote(struct proc
* p
, long hint
)
2845 KNOTE(&p
->p_klist
, hint
);
2846 proc_klist_unlock();
2850 proc_knote_drain(struct proc
*p
)
2852 struct knote
*kn
= NULL
;
2855 * Clear the proc's klist to avoid references after the proc is reaped.
2858 while ((kn
= SLIST_FIRST(&p
->p_klist
))) {
2859 kn
->kn_ptr
.p_proc
= PROC_NULL
;
2860 KNOTE_DETACH(&p
->p_klist
, kn
);
2862 proc_klist_unlock();
2866 proc_setregister(proc_t p
)
2869 p
->p_lflag
|= P_LREGISTER
;
2874 proc_resetregister(proc_t p
)
2877 p
->p_lflag
&= ~P_LREGISTER
;
2882 proc_pgrpid(proc_t p
)
2890 return current_proc()->p_pgrpid
;
2894 /* return control and action states */
2896 proc_getpcontrol(int pid
, int * pcontrolp
)
2903 if (pcontrolp
!= NULL
)
2904 *pcontrolp
= p
->p_pcaction
;
2911 proc_dopcontrol(proc_t p
)
2917 pcontrol
= PROC_CONTROL_STATE(p
);
2919 if (PROC_ACTION_STATE(p
) == 0) {
2922 PROC_SETACTION_STATE(p
);
2924 printf("low swap: throttling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2928 PROC_SETACTION_STATE(p
);
2930 printf("low swap: suspending pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2931 task_suspend(p
->task
);
2935 PROC_SETACTION_STATE(p
);
2937 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2938 psignal(p
, SIGKILL
);
2948 return(PROC_RETURNED
);
2953 * Resume a throttled or suspended process. This is an internal interface that's only
2954 * used by the user level code that presents the GUI when we run out of swap space and
2955 * hence is restricted to processes with superuser privileges.
2959 proc_resetpcontrol(int pid
)
2964 proc_t self
= current_proc();
2966 /* if the process has been validated to handle resource control or root is valid one */
2967 if (((self
->p_lflag
& P_LVMRSRCOWNER
) == 0) && (error
= suser(kauth_cred_get(), 0)))
2976 pcontrol
= PROC_CONTROL_STATE(p
);
2978 if(PROC_ACTION_STATE(p
) !=0) {
2981 PROC_RESETACTION_STATE(p
);
2983 printf("low swap: unthrottling pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2987 PROC_RESETACTION_STATE(p
);
2989 printf("low swap: resuming pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
2990 task_resume(p
->task
);
2995 PROC_SETACTION_STATE(p
);
2997 printf("low swap: attempt to unkill pid %d (%s) ignored\n", p
->p_pid
, p
->p_comm
);
3013 struct no_paging_space
3015 uint64_t pcs_max_size
;
3016 uint64_t pcs_uniqueid
;
3019 uint64_t pcs_total_size
;
3021 uint64_t npcs_max_size
;
3022 uint64_t npcs_uniqueid
;
3024 int npcs_proc_count
;
3025 uint64_t npcs_total_size
;
3027 int apcs_proc_count
;
3028 uint64_t apcs_total_size
;
3033 proc_pcontrol_filter(proc_t p
, void *arg
)
3035 struct no_paging_space
*nps
;
3036 uint64_t compressed
;
3038 nps
= (struct no_paging_space
*)arg
;
3040 compressed
= get_task_compressed(p
->task
);
3042 if (PROC_CONTROL_STATE(p
)) {
3043 if (PROC_ACTION_STATE(p
) == 0) {
3044 if (compressed
> nps
->pcs_max_size
) {
3045 nps
->pcs_pid
= p
->p_pid
;
3046 nps
->pcs_uniqueid
= p
->p_uniqueid
;
3047 nps
->pcs_max_size
= compressed
;
3049 nps
->pcs_total_size
+= compressed
;
3050 nps
->pcs_proc_count
++;
3052 nps
->apcs_total_size
+= compressed
;
3053 nps
->apcs_proc_count
++;
3056 if (compressed
> nps
->npcs_max_size
) {
3057 nps
->npcs_pid
= p
->p_pid
;
3058 nps
->npcs_uniqueid
= p
->p_uniqueid
;
3059 nps
->npcs_max_size
= compressed
;
3061 nps
->npcs_total_size
+= compressed
;
3062 nps
->npcs_proc_count
++;
3070 proc_pcontrol_null(__unused proc_t p
, __unused
void *arg
)
3072 return(PROC_RETURNED
);
3077 * Deal with the low on compressor pool space condition... this function
3078 * gets called when we are approaching the limits of the compressor pool or
3079 * we are unable to create a new swap file.
3080 * Since this eventually creates a memory deadlock situtation, we need to take action to free up
3081 * memory resources (both compressed and uncompressed) in order to prevent the system from hanging completely.
3082 * There are 2 categories of processes to deal with. Those that have an action
3083 * associated with them by the task itself and those that do not. Actionable
3084 * tasks can have one of three categories specified: ones that
3085 * can be killed immediately, ones that should be suspended, and ones that should
3086 * be throttled. Processes that do not have an action associated with them are normally
3087 * ignored unless they are utilizing such a large percentage of the compressor pool (currently 50%)
3088 * that only by killing them can we hope to put the system back into a usable state.
3091 #define NO_PAGING_SPACE_DEBUG 0
3093 extern uint64_t vm_compressor_pages_compressed(void);
3095 struct timeval last_no_space_action
= {0, 0};
3098 no_paging_space_action()
3101 struct no_paging_space nps
;
3105 * Throttle how often we come through here. Once every 5 seconds should be plenty.
3109 if (now
.tv_sec
<= last_no_space_action
.tv_sec
+ 5)
3113 * Examine all processes and find the biggest (biggest is based on the number of pages this
3114 * task has in the compressor pool) that has been marked to have some action
3115 * taken when swap space runs out... we also find the biggest that hasn't been marked for
3118 * If the biggest non-actionable task is over the "dangerously big" threashold (currently 50% of
3119 * the total number of pages held by the compressor, we go ahead and kill it since no other task
3120 * can have any real effect on the situation. Otherwise, we go after the actionable process.
3122 bzero(&nps
, sizeof(nps
));
3124 proc_iterate(PROC_ALLPROCLIST
, proc_pcontrol_null
, (void *)NULL
, proc_pcontrol_filter
, (void *)&nps
);
3126 #if NO_PAGING_SPACE_DEBUG
3127 printf("low swap: npcs_proc_count = %d, npcs_total_size = %qd, npcs_max_size = %qd\n",
3128 nps
.npcs_proc_count
, nps
.npcs_total_size
, nps
.npcs_max_size
);
3129 printf("low swap: pcs_proc_count = %d, pcs_total_size = %qd, pcs_max_size = %qd\n",
3130 nps
.pcs_proc_count
, nps
.pcs_total_size
, nps
.pcs_max_size
);
3131 printf("low swap: apcs_proc_count = %d, apcs_total_size = %qd\n",
3132 nps
.apcs_proc_count
, nps
.apcs_total_size
);
3134 if (nps
.npcs_max_size
> (vm_compressor_pages_compressed() * 50) / 100) {
3136 * for now we'll knock out any task that has more then 50% of the pages
3137 * held by the compressor
3139 if ((p
= proc_find(nps
.npcs_pid
)) != PROC_NULL
) {
3141 if (nps
.npcs_uniqueid
== p
->p_uniqueid
) {
3143 * verify this is still the same process
3144 * in case the proc exited and the pid got reused while
3145 * we were finishing the proc_iterate and getting to this point
3147 last_no_space_action
= now
;
3149 printf("low swap: killing pid %d (%s)\n", p
->p_pid
, p
->p_comm
);
3150 psignal(p
, SIGKILL
);
3161 if (nps
.pcs_max_size
> 0) {
3162 if ((p
= proc_find(nps
.pcs_pid
)) != PROC_NULL
) {
3164 if (nps
.pcs_uniqueid
== p
->p_uniqueid
) {
3166 * verify this is still the same process
3167 * in case the proc exited and the pid got reused while
3168 * we were finishing the proc_iterate and getting to this point
3170 last_no_space_action
= now
;
3182 last_no_space_action
= now
;
3184 printf("low swap: unable to find any eligible processes to take action on\n");
3190 proc_trace_log(__unused proc_t p
, struct proc_trace_log_args
*uap
, __unused
int *retval
)
3193 proc_t target_proc
= PROC_NULL
;
3194 pid_t target_pid
= uap
->pid
;
3195 uint64_t target_uniqueid
= uap
->uniqueid
;
3196 task_t target_task
= NULL
;
3198 if (priv_check_cred(kauth_cred_get(), PRIV_PROC_TRACE_INSPECT
, 0)) {
3202 target_proc
= proc_find(target_pid
);
3203 if (target_proc
!= PROC_NULL
) {
3204 if (target_uniqueid
!= proc_uniqueid(target_proc
)) {
3209 target_task
= proc_task(target_proc
);
3210 if (task_send_trace_memory(target_task
, target_pid
, target_uniqueid
)) {
3218 if (target_proc
!= PROC_NULL
)
3219 proc_rele(target_proc
);
3223 #if VM_SCAN_FOR_SHADOW_CHAIN
3224 extern int vm_map_shadow_max(vm_map_t map
);
3225 int proc_shadow_max(void);
3226 int proc_shadow_max(void)
3235 for (p
= allproc
.lh_first
; (p
!= 0); p
= p
->p_list
.le_next
) {
3236 if (p
->p_stat
== SIDL
)
3242 map
= get_task_map(task
);
3246 retval
= vm_map_shadow_max(map
);
3254 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */
3256 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
);
3257 void proc_set_responsible_pid(proc_t target_proc
, pid_t responsible_pid
)
3259 if (target_proc
!= NULL
) {
3260 target_proc
->p_responsible_pid
= responsible_pid
;
3266 proc_chrooted(proc_t p
)
3272 retval
= (p
->p_fd
->fd_rdir
!= NULL
) ? 1 : 0;
3280 proc_get_uthread_uu_threadlist(void * uthread_v
)
3282 uthread_t uth
= (uthread_t
)uthread_v
;
3283 return (uth
!= NULL
) ? uth
->uu_threadlist
: NULL
;