2 * Copyright (c) 2005-2016 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@
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/proc_internal.h>
38 #include <sys/kauth.h>
39 #include <sys/file_internal.h>
40 #include <sys/vnode_internal.h>
41 #include <sys/unistd.h>
43 #include <sys/ioctl.h>
44 #include <sys/namei.h>
46 #include <sys/disklabel.h>
48 #include <sys/reason.h>
49 #include <sys/sysctl.h>
51 #include <sys/aio_kern.h>
52 #include <sys/kern_memorystatus.h>
54 #include <security/audit/audit.h>
56 #include <mach/machine.h>
57 #include <mach/mach_types.h>
58 #include <mach/vm_param.h>
59 #include <kern/task.h>
60 #include <kern/kalloc.h>
61 #include <kern/assert.h>
62 #include <kern/policy_internal.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
66 #include <mach/host_info.h>
67 #include <mach/task_info.h>
68 #include <mach/thread_info.h>
69 #include <mach/vm_region.h>
71 #include <sys/mount_internal.h>
72 #include <sys/proc_info.h>
73 #include <sys/bsdtask_info.h>
74 #include <sys/kdebug.h>
75 #include <sys/sysproto.h>
76 #include <sys/msgbuf.h>
79 #include <sys/guarded.h>
81 #include <machine/machine_routines.h>
83 #include <kern/ipc_misc.h>
85 #include <vm/vm_protos.h>
87 /* Needed by proc_pidnoteexit(), proc_pidlistuptrs() */
88 #include <sys/event.h>
89 #include <sys/codesign.h>
91 /* Needed by proc_listcoalitions() */
92 #ifdef CONFIG_COALITIONS
93 #include <sys/coalition.h>
97 #include <security/mac_framework.h>
106 uint64_t get_dispatchqueue_offset_from_proc(void *);
107 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *);
108 uint64_t get_return_to_kernel_offset_from_proc(void *p
);
109 int proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
112 * TODO: Replace the noinline attribute below. Currently, it serves
113 * to avoid stack bloat caused by inlining multiple functions that
114 * have large stack footprints; when the functions are independent
115 * of each other (will not both be called in any given call to the
116 * caller), this only serves to bloat the stack, as we allocate
117 * space for both functions, despite the fact that we only need a
118 * fraction of that space.
120 * Long term, these functions should not be allocating everything on
121 * the stack, and should move large allocations (the huge structs
122 * that proc info deals in) to the heap, or eliminate them if
125 * The functions that most desperately need to improve stack usage
126 * (starting with the worst offenders):
127 * proc_pidvnodepathinfo
129 * proc_pidregionpathinfo
135 * proc_pidoriginatorinfo
138 /* protos for proc_info calls */
139 int __attribute__ ((noinline
)) proc_listpids(uint32_t type
, uint32_t tyoneinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
140 int __attribute__ ((noinline
)) proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
141 int __attribute__ ((noinline
)) proc_pidfdinfo(int pid
, int flavor
,int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
142 int __attribute__ ((noinline
)) proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
143 int __attribute__ ((noinline
)) proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
144 int __attribute__ ((noinline
)) proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
145 int __attribute__ ((noinline
)) proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t * retval
);
146 int __attribute__ ((noinline
)) proc_terminate(int pid
, int32_t * retval
);
147 int __attribute__ ((noinline
)) proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, int32_t * retval
);
148 int __attribute__ ((noinline
)) proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
149 int __attribute__ ((noinline
)) proc_listcoalitions(int flavor
, int coaltype
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
150 int __attribute__ ((noinline
)) proc_can_use_foreground_hw(int pid
, user_addr_t reason
, uint32_t resonsize
, int32_t *retval
);
152 /* protos for procpidinfo calls */
153 int __attribute__ ((noinline
)) proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
154 int __attribute__ ((noinline
)) proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
*pbsd
, int zombie
);
155 int __attribute__ ((noinline
)) proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
*pbsd_shortp
, int zombie
);
156 int __attribute__ ((noinline
)) proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
*ptinfo
);
157 int __attribute__ ((noinline
)) proc_pidthreadinfo(proc_t p
, uint64_t arg
, int thuniqueid
, struct proc_threadinfo
*pthinfo
);
158 int __attribute__ ((noinline
)) proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
);
159 int __attribute__ ((noinline
)) proc_pidlistthreads(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
160 int __attribute__ ((noinline
)) proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
161 int __attribute__ ((noinline
)) proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
162 int __attribute__ ((noinline
)) proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
163 int __attribute__ ((noinline
)) proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
164 int __attribute__ ((noinline
)) proc_pidvnodepathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
165 int __attribute__ ((noinline
)) proc_pidpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
166 int __attribute__ ((noinline
)) proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
);
167 int __attribute__ ((noinline
)) proc_pidfileportlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
168 void __attribute__ ((noinline
)) proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
169 void __attribute__ ((noinline
)) proc_archinfo(proc_t p
, struct proc_archinfo
*pai
);
170 void __attribute__ ((noinline
)) proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*pci
);
171 int __attribute__ ((noinline
)) proc_pidnoteexit(proc_t p
, uint64_t arg
, uint32_t *data
);
172 int __attribute__ ((noinline
)) proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
);
173 int __attribute__ ((noinline
)) proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
);
174 int __attribute__ ((noinline
)) proc_pidlistuptrs(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
175 int __attribute__ ((noinline
)) proc_piddynkqueueinfo(pid_t pid
, int flavor
, kqueue_id_t id
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
177 /* protos for proc_pidfdinfo calls */
178 int __attribute__ ((noinline
)) pid_vnodeinfo(vnode_t vp
, uint32_t vid
, struct fileproc
* fp
,proc_t proc
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
179 int __attribute__ ((noinline
)) pid_vnodeinfopath(vnode_t vp
, uint32_t vid
, struct fileproc
* fp
,proc_t proc
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
180 int __attribute__ ((noinline
)) pid_socketinfo(socket_t so
, struct fileproc
*fp
,proc_t proc
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
181 int __attribute__ ((noinline
)) pid_pseminfo(struct psemnode
* psem
, struct fileproc
* fp
, proc_t proc
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
182 int __attribute__ ((noinline
)) pid_pshminfo(struct pshmnode
* pshm
, struct fileproc
* fp
, proc_t proc
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
183 int __attribute__ ((noinline
)) pid_pipeinfo(struct pipe
* p
, struct fileproc
* fp
, proc_t proc
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
184 int __attribute__ ((noinline
)) pid_kqueueinfo(struct kqueue
* kq
, struct fileproc
* fp
, proc_t proc
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
185 int __attribute__ ((noinline
)) pid_atalkinfo(struct atalk
* at
, struct fileproc
* fp
, proc_t proc
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
188 /* protos for misc */
190 int fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
);
191 void fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* finfo
);
192 int proc_security_policy(proc_t targetp
, int callnum
, int flavor
, boolean_t check_same_user
);
193 static void munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
);
194 static int proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
);
195 int proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
);
197 extern int cansignal(struct proc
*, kauth_cred_t
, struct proc
*, int, int);
198 extern int proc_get_rusage(proc_t proc
, int flavor
, user_addr_t buffer
, int is_zombie
);
200 #define CHECK_SAME_USER TRUE
201 #define NO_CHECK_SAME_USER FALSE
203 uint64_t get_dispatchqueue_offset_from_proc(void *p
)
206 proc_t pself
= (proc_t
)p
;
207 return (pself
->p_dispatchqueue_offset
);
213 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p
)
216 proc_t pself
= (proc_t
)p
;
217 return (pself
->p_dispatchqueue_serialno_offset
);
223 uint64_t get_return_to_kernel_offset_from_proc(void *p
)
226 proc_t pself
= (proc_t
)p
;
227 return (pself
->p_return_to_kernel_offset
);
233 /***************************** proc_info ********************/
236 proc_info(__unused
struct proc
*p
, struct proc_info_args
* uap
, int32_t *retval
)
238 return(proc_info_internal(uap
->callnum
, uap
->pid
, uap
->flavor
, uap
->arg
, uap
->buffer
, uap
->buffersize
, retval
));
243 proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
247 case PROC_INFO_CALL_LISTPIDS
:
248 /* pid contains type and flavor contains typeinfo */
249 return(proc_listpids(pid
, flavor
, buffer
, buffersize
, retval
));
250 case PROC_INFO_CALL_PIDINFO
:
251 return(proc_pidinfo(pid
, flavor
, arg
, buffer
, buffersize
, retval
));
252 case PROC_INFO_CALL_PIDFDINFO
:
253 return(proc_pidfdinfo(pid
, flavor
, (int)arg
, buffer
, buffersize
, retval
));
254 case PROC_INFO_CALL_KERNMSGBUF
:
255 return(proc_kernmsgbuf(buffer
, buffersize
, retval
));
256 case PROC_INFO_CALL_SETCONTROL
:
257 return(proc_setcontrol(pid
, flavor
, arg
, buffer
, buffersize
, retval
));
258 case PROC_INFO_CALL_PIDFILEPORTINFO
:
259 return(proc_pidfileportinfo(pid
, flavor
, (mach_port_name_t
)arg
, buffer
, buffersize
, retval
));
260 case PROC_INFO_CALL_TERMINATE
:
261 return(proc_terminate(pid
, retval
));
262 case PROC_INFO_CALL_DIRTYCONTROL
:
263 return(proc_dirtycontrol(pid
, flavor
, arg
, retval
));
264 case PROC_INFO_CALL_PIDRUSAGE
:
265 return (proc_pid_rusage(pid
, flavor
, buffer
, retval
));
266 case PROC_INFO_CALL_PIDORIGINATORINFO
:
267 return (proc_pidoriginatorinfo(pid
, flavor
, buffer
, buffersize
, retval
));
268 case PROC_INFO_CALL_LISTCOALITIONS
:
269 return proc_listcoalitions(pid
/* flavor */, flavor
/* coaltype */, buffer
,
271 case PROC_INFO_CALL_CANUSEFGHW
:
272 return proc_can_use_foreground_hw(pid
, buffer
, buffersize
, retval
);
273 case PROC_INFO_CALL_PIDDYNKQUEUEINFO
:
274 return proc_piddynkqueueinfo(pid
, flavor
, (kqueue_id_t
)arg
, buffer
, buffersize
, retval
);
282 /******************* proc_listpids routine ****************/
284 proc_listpids(uint32_t type
, uint32_t typeinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
286 uint32_t numprocs
= 0;
295 struct proclist
*current_list
;
297 /* Do we have permission to look into this? */
298 if ((error
= proc_security_policy(PROC_NULL
, PROC_INFO_CALL_LISTPIDS
, type
, NO_CHECK_SAME_USER
)))
301 /* if the buffer is null, return num of procs */
302 if (buffer
== (user_addr_t
)0) {
303 *retval
= ((nprocs
+ 20) * sizeof(int));
307 if (buffersize
< sizeof(int)) {
310 wantpids
= buffersize
/sizeof(int);
311 if ((nprocs
+ 20) > 0) {
312 numprocs
= (uint32_t)(nprocs
+ 20);
314 if (numprocs
> wantpids
) {
318 kbuf
= (char *)kalloc((vm_size_t
)(numprocs
* sizeof(int)));
322 bzero(kbuf
, sizeof(int));
329 current_list
= &allproc
;
331 LIST_FOREACH(p
, current_list
, p_list
) {
335 if (p
->p_pgrpid
!= (pid_t
)typeinfo
)
339 if ((p
->p_ppid
!= (pid_t
)typeinfo
) && (((p
->p_lflag
& P_LTRACED
) == 0) || (p
->p_oppid
!= (pid_t
)typeinfo
)))
347 /* racy but list lock is held */
348 if ((p
->p_flag
& P_CONTROLT
) == 0 ||
349 (p
->p_pgrp
== NULL
) || (p
->p_pgrp
->pg_session
== NULL
) ||
350 (tp
= SESSION_TP(p
->p_pgrp
->pg_session
)) == TTY_NULL
||
351 tp
->t_dev
!= (dev_t
)typeinfo
)
355 if (p
->p_ucred
== NULL
)
358 kauth_cred_t my_cred
;
361 my_cred
= kauth_cred_proc_ref(p
);
362 uid
= kauth_cred_getuid(my_cred
);
363 kauth_cred_unref(&my_cred
);
364 if (uid
!= (uid_t
)typeinfo
)
369 if (p
->p_ucred
== NULL
)
372 kauth_cred_t my_cred
;
375 my_cred
= kauth_cred_proc_ref(p
);
376 uid
= kauth_cred_getruid(my_cred
);
377 kauth_cred_unref(&my_cred
);
378 if (uid
!= (uid_t
)typeinfo
)
383 if (p
->p_kdebug
== 0) {
400 if ((n
< numprocs
) && (current_list
== &allproc
)) {
401 current_list
= &zombproc
;
408 error
= copyout((caddr_t
)ptr
, buffer
, n
* sizeof(int));
410 *retval
= (n
* sizeof(int));
411 kfree((void *)kbuf
, (vm_size_t
)(numprocs
* sizeof(int)));
417 /********************************** proc_pidfdlist routines ********************************/
420 proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
425 struct proc_fdinfo
* pfd
;
426 struct fileproc
* fp
;
431 if (p
->p_fd
->fd_nfiles
> 0) {
432 numfds
= (uint32_t)p
->p_fd
->fd_nfiles
;
435 if (buffer
== (user_addr_t
) 0) {
437 *retval
= (numfds
* sizeof(struct proc_fdinfo
));
441 /* buffersize is big enough atleast for one struct */
442 needfds
= buffersize
/sizeof(struct proc_fdinfo
);
444 if (numfds
> needfds
) {
448 kbuf
= (char *)kalloc((vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
451 bzero(kbuf
, numfds
* sizeof(struct proc_fdinfo
));
455 pfd
= (struct proc_fdinfo
*)kbuf
;
457 for (n
= 0; ((n
< (int)numfds
) && (n
< p
->p_fd
->fd_nfiles
)); n
++) {
458 if (((fp
= p
->p_fd
->fd_ofiles
[n
]) != 0)
459 && ((p
->p_fd
->fd_ofileflags
[n
] & UF_RESERVED
) == 0)) {
460 file_type_t fdtype
= FILEGLOB_DTYPE(fp
->f_fglob
);
462 pfd
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
463 fdtype
: PROX_FDTYPE_ATALK
;
470 error
= copyout(kbuf
, buffer
, count
* sizeof(struct proc_fdinfo
));
471 kfree((void *)kbuf
, (vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
473 *retval
= (count
* sizeof(struct proc_fdinfo
));
478 * Helper functions for proc_pidfileportlist.
481 proc_fileport_count(__unused mach_port_name_t name
,
482 __unused
struct fileglob
*fg
, void *arg
)
484 uint32_t *counter
= arg
;
490 struct fileport_fdtype_args
{
491 struct proc_fileportinfo
*ffa_pfi
;
492 struct proc_fileportinfo
*ffa_pfi_end
;
496 proc_fileport_fdtype(mach_port_name_t name
, struct fileglob
*fg
, void *arg
)
498 struct fileport_fdtype_args
*ffa
= arg
;
500 if (ffa
->ffa_pfi
!= ffa
->ffa_pfi_end
) {
501 file_type_t fdtype
= FILEGLOB_DTYPE(fg
);
503 ffa
->ffa_pfi
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
504 fdtype
: PROX_FDTYPE_ATALK
;
505 ffa
->ffa_pfi
->proc_fileport
= name
;
507 return (0); /* keep walking */
509 return (-1); /* stop the walk! */
513 proc_pidfileportlist(proc_t p
,
514 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
518 struct proc_fileportinfo
*pfi
;
519 uint32_t needfileports
, numfileports
;
520 struct fileport_fdtype_args ffa
;
523 needfileports
= buffersize
/ sizeof (*pfi
);
524 if ((user_addr_t
)0 == buffer
|| needfileports
> (uint32_t)maxfiles
) {
526 * Either (i) the user is asking for a fileport count,
527 * or (ii) the number of fileports they're asking for is
528 * larger than the maximum number of open files (!); count
529 * them to bound subsequent heap allocations.
532 switch (fileport_walk(p
->task
,
533 proc_fileport_count
, &numfileports
)) {
536 case KERN_RESOURCE_SHORTAGE
:
538 case KERN_INVALID_TASK
:
544 if (numfileports
== 0) {
545 *retval
= 0; /* none at all, bail */
548 if ((user_addr_t
)0 == buffer
) {
549 numfileports
+= 20; /* accelerate convergence */
550 *retval
= numfileports
* sizeof (*pfi
);
553 if (needfileports
> numfileports
)
554 needfileports
= numfileports
;
557 assert(buffersize
>= PROC_PIDLISTFILEPORTS_SIZE
);
559 kbufsize
= (vm_size_t
)needfileports
* sizeof (*pfi
);
560 pfi
= kbuf
= kalloc(kbufsize
);
563 bzero(kbuf
, kbufsize
);
566 ffa
.ffa_pfi_end
= pfi
+ needfileports
;
568 switch (fileport_walk(p
->task
, proc_fileport_fdtype
, &ffa
)) {
572 if ((numfileports
= pfi
- (typeof(pfi
))kbuf
) == 0)
574 if (numfileports
> needfileports
)
575 panic("more fileports returned than requested");
576 error
= copyout(kbuf
, buffer
, numfileports
* sizeof (*pfi
));
578 case KERN_RESOURCE_SHORTAGE
:
581 case KERN_INVALID_TASK
:
588 kfree(kbuf
, kbufsize
);
590 *retval
= numfileports
* sizeof (*pfi
);
595 proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
* pbsd
, int zombie
)
598 struct session
*sessionp
= NULL
;
600 kauth_cred_t my_cred
;
603 sessionp
= proc_session(p
);
605 my_cred
= kauth_cred_proc_ref(p
);
606 bzero(pbsd
, sizeof(struct proc_bsdinfo
));
607 pbsd
->pbi_status
= p
->p_stat
;
608 pbsd
->pbi_xstatus
= p
->p_xstat
;
609 pbsd
->pbi_pid
= p
->p_pid
;
610 pbsd
->pbi_ppid
= p
->p_ppid
;
611 pbsd
->pbi_uid
= kauth_cred_getuid(my_cred
);
612 pbsd
->pbi_gid
= kauth_cred_getgid(my_cred
);
613 pbsd
->pbi_ruid
= kauth_cred_getruid(my_cred
);
614 pbsd
->pbi_rgid
= kauth_cred_getrgid(my_cred
);
615 pbsd
->pbi_svuid
= kauth_cred_getsvuid(my_cred
);
616 pbsd
->pbi_svgid
= kauth_cred_getsvgid(my_cred
);
617 kauth_cred_unref(&my_cred
);
619 pbsd
->pbi_nice
= p
->p_nice
;
620 pbsd
->pbi_start_tvsec
= p
->p_start
.tv_sec
;
621 pbsd
->pbi_start_tvusec
= p
->p_start
.tv_usec
;
622 bcopy(&p
->p_comm
, &pbsd
->pbi_comm
[0], MAXCOMLEN
);
623 pbsd
->pbi_comm
[MAXCOMLEN
- 1] = '\0';
624 bcopy(&p
->p_name
, &pbsd
->pbi_name
[0], 2*MAXCOMLEN
);
625 pbsd
->pbi_name
[(2*MAXCOMLEN
) - 1] = '\0';
628 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
)
629 pbsd
->pbi_flags
|= PROC_FLAG_SYSTEM
;
630 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
)
631 pbsd
->pbi_flags
|= PROC_FLAG_TRACED
;
632 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
)
633 pbsd
->pbi_flags
|= PROC_FLAG_INEXIT
;
634 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
)
635 pbsd
->pbi_flags
|= PROC_FLAG_PPWAIT
;
636 if ((p
->p_flag
& P_LP64
) == P_LP64
)
637 pbsd
->pbi_flags
|= PROC_FLAG_LP64
;
638 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
)
639 pbsd
->pbi_flags
|= PROC_FLAG_CONTROLT
;
640 if ((p
->p_flag
& P_THCWD
) == P_THCWD
)
641 pbsd
->pbi_flags
|= PROC_FLAG_THCWD
;
642 if ((p
->p_flag
& P_SUGID
) == P_SUGID
)
643 pbsd
->pbi_flags
|= PROC_FLAG_PSUGID
;
644 if ((p
->p_flag
& P_EXEC
) == P_EXEC
)
645 pbsd
->pbi_flags
|= PROC_FLAG_EXEC
;
647 if (sessionp
!= SESSION_NULL
) {
648 if (SESS_LEADER(p
, sessionp
))
649 pbsd
->pbi_flags
|= PROC_FLAG_SLEADER
;
650 if (sessionp
->s_ttyvp
)
651 pbsd
->pbi_flags
|= PROC_FLAG_CTTY
;
655 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
656 pbsd
->pbi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
657 #endif /* !CONFIG_EMBEDDED */
659 switch(PROC_CONTROL_STATE(p
)) {
661 pbsd
->pbi_flags
|= PROC_FLAG_PC_THROTTLE
;
664 pbsd
->pbi_flags
|= PROC_FLAG_PC_SUSP
;
667 pbsd
->pbi_flags
|= PROC_FLAG_PC_KILL
;
671 switch(PROC_ACTION_STATE(p
)) {
673 pbsd
->pbi_flags
|= PROC_FLAG_PA_THROTTLE
;
676 pbsd
->pbi_flags
|= PROC_FLAG_PA_SUSP
;
680 /* if process is a zombie skip bg state */
681 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
))
682 proc_get_darwinbgstate(p
->task
, &pbsd
->pbi_flags
);
685 pbsd
->pbi_nfiles
= p
->p_fd
->fd_nfiles
;
687 pbsd
->e_tdev
= NODEV
;
688 if (pg
!= PGRP_NULL
) {
689 pbsd
->pbi_pgid
= p
->p_pgrpid
;
690 pbsd
->pbi_pjobc
= pg
->pg_jobc
;
691 if ((p
->p_flag
& P_CONTROLT
) && (sessionp
!= SESSION_NULL
) && (tp
= SESSION_TP(sessionp
))) {
692 pbsd
->e_tdev
= tp
->t_dev
;
693 pbsd
->e_tpgid
= sessionp
->s_ttypgrpid
;
696 if (sessionp
!= SESSION_NULL
)
697 session_rele(sessionp
);
706 proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
* pbsd_shortp
, int zombie
)
708 bzero(pbsd_shortp
, sizeof(struct proc_bsdshortinfo
));
709 pbsd_shortp
->pbsi_pid
= p
->p_pid
;
710 pbsd_shortp
->pbsi_ppid
= p
->p_ppid
;
711 pbsd_shortp
->pbsi_pgid
= p
->p_pgrpid
;
712 pbsd_shortp
->pbsi_status
= p
->p_stat
;
713 bcopy(&p
->p_comm
, &pbsd_shortp
->pbsi_comm
[0], MAXCOMLEN
);
714 pbsd_shortp
->pbsi_comm
[MAXCOMLEN
- 1] = '\0';
716 pbsd_shortp
->pbsi_flags
= 0;
717 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
)
718 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_SYSTEM
;
719 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
)
720 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_TRACED
;
721 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
)
722 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_INEXIT
;
723 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
)
724 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PPWAIT
;
725 if ((p
->p_flag
& P_LP64
) == P_LP64
)
726 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_LP64
;
727 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
)
728 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_CONTROLT
;
729 if ((p
->p_flag
& P_THCWD
) == P_THCWD
)
730 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_THCWD
;
731 if ((p
->p_flag
& P_SUGID
) == P_SUGID
)
732 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PSUGID
;
733 if ((p
->p_flag
& P_EXEC
) == P_EXEC
)
734 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_EXEC
;
736 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
737 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
738 #endif /* !CONFIG_EMBEDDED */
740 switch(PROC_CONTROL_STATE(p
)) {
742 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_THROTTLE
;
745 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_SUSP
;
748 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_KILL
;
752 switch(PROC_ACTION_STATE(p
)) {
754 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_THROTTLE
;
757 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_SUSP
;
761 /* if process is a zombie skip bg state */
762 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
))
763 proc_get_darwinbgstate(p
->task
, &pbsd_shortp
->pbsi_flags
);
765 pbsd_shortp
->pbsi_uid
= p
->p_uid
;
766 pbsd_shortp
->pbsi_gid
= p
->p_gid
;
767 pbsd_shortp
->pbsi_ruid
= p
->p_ruid
;
768 pbsd_shortp
->pbsi_rgid
= p
->p_rgid
;
769 pbsd_shortp
->pbsi_svuid
= p
->p_svuid
;
770 pbsd_shortp
->pbsi_svgid
= p
->p_svgid
;
776 proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
* ptinfo
)
782 bzero(ptinfo
, sizeof(struct proc_taskinfo
));
783 fill_taskprocinfo(task
, (struct proc_taskinfo_internal
*)ptinfo
);
791 proc_pidthreadinfo(proc_t p
, uint64_t arg
, int thuniqueid
, struct proc_threadinfo
*pthinfo
)
794 uint64_t threadaddr
= (uint64_t)arg
;
796 bzero(pthinfo
, sizeof(struct proc_threadinfo
));
798 error
= fill_taskthreadinfo(p
->task
, threadaddr
, thuniqueid
, (struct proc_threadinfo_internal
*)pthinfo
, NULL
, NULL
);
807 bsd_hasthreadname(void *uth
)
809 struct uthread
*ut
= (struct uthread
*)uth
;
811 /* This doesn't check for the empty string; do we care? */
820 bsd_getthreadname(void *uth
, char *buffer
)
822 struct uthread
*ut
= (struct uthread
*)uth
;
824 bcopy(ut
->pth_name
,buffer
,MAXTHREADNAMESIZE
);
828 * This is known to race with regards to the contents of the thread name; concurrent
829 * callers may result in a garbled name.
832 bsd_setthreadname(void *uth
, const char *name
) {
833 struct uthread
*ut
= (struct uthread
*)uth
;
834 char * name_buf
= NULL
;
837 /* If there is no existing thread name, allocate a buffer for one. */
838 name_buf
= kalloc(MAXTHREADNAMESIZE
);
840 bzero(name_buf
, MAXTHREADNAMESIZE
);
842 /* Someone could conceivably have named the thread at the same time we did. */
843 if (!OSCompareAndSwapPtr(NULL
, name_buf
, &ut
->pth_name
)) {
844 kfree(name_buf
, MAXTHREADNAMESIZE
);
847 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV
, ut
->pth_name
);
850 strncpy(ut
->pth_name
, name
, MAXTHREADNAMESIZE
- 1);
851 kernel_debug_string_simple(TRACE_STRING_THREADNAME
, ut
->pth_name
);
855 bsd_copythreadname(void *dst_uth
, void *src_uth
)
857 struct uthread
*dst_ut
= (struct uthread
*)dst_uth
;
858 struct uthread
*src_ut
= (struct uthread
*)src_uth
;
860 if (src_ut
->pth_name
== NULL
)
863 if (dst_ut
->pth_name
== NULL
) {
864 dst_ut
->pth_name
= (char *)kalloc(MAXTHREADNAMESIZE
);
865 if (dst_ut
->pth_name
== NULL
)
869 bcopy(src_ut
->pth_name
, dst_ut
->pth_name
, MAXTHREADNAMESIZE
);
874 bsd_threadcdir(void * uth
, void *vptr
, int *vidp
)
876 struct uthread
* ut
= (struct uthread
*)uth
;
878 vnode_t
*vpp
= (vnode_t
*)vptr
;
892 proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
)
897 uint64_t threadaddr
= (uint64_t)arg
;
900 bzero(pinfo
, sizeof(struct proc_threadwithpathinfo
));
902 error
= fill_taskthreadinfo(p
->task
, threadaddr
, 0, (struct proc_threadinfo_internal
*)&pinfo
->pt
, (void *)&vp
, &vid
);
906 if ((vp
!= NULLVP
) && ((vnode_getwithvid(vp
, vid
)) == 0)) {
907 error
= fill_vnodeinfo(vp
, &pinfo
->pvip
.vip_vi
) ;
910 vn_getpath(vp
, &pinfo
->pvip
.vip_path
[0], &count
);
911 pinfo
->pvip
.vip_path
[MAXPATHLEN
-1] = 0;
921 proc_pidlistthreads(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
927 uint32_t numthreads
= 0;
929 int num
= get_numthreads(p
->task
) + 10;
931 numthreads
= (uint32_t)num
;
934 count
= buffersize
/(sizeof(uint64_t));
936 if (numthreads
> count
) {
940 kbuf
= (void *)kalloc(numthreads
* sizeof(uint64_t));
943 bzero(kbuf
, numthreads
* sizeof(uint64_t));
945 ret
= fill_taskthreadlist(p
->task
, kbuf
, numthreads
);
947 error
= copyout(kbuf
, buffer
, ret
);
948 kfree(kbuf
, numthreads
* sizeof(uint64_t));
957 proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
959 struct proc_regioninfo preginfo
;
962 bzero(&preginfo
, sizeof(struct proc_regioninfo
));
963 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
, (uintptr_t *)0, (uint32_t *)0);
966 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regioninfo
));
968 *retval
= sizeof(struct proc_regioninfo
);
974 proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
976 struct proc_regionwithpathinfo preginfo
;
978 uintptr_t vnodeaddr
= 0;
983 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
985 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
989 vp
= (vnode_t
)vnodeaddr
;
990 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
991 /* FILL THE VNODEINFO */
992 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
994 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
995 /* Always make sure it is null terminated */
996 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
1000 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1002 *retval
= sizeof(struct proc_regionwithpathinfo
);
1007 proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1009 struct proc_regionwithpathinfo preginfo
;
1011 uintptr_t vnodeaddr
= 0;
1012 uint32_t vnodeid
= 0;
1016 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1018 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1024 vp
= (vnode_t
)vnodeaddr
;
1025 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1026 /* FILL THE VNODEINFO */
1027 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1029 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1030 /* Always make sure it is null terminated */
1031 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
1037 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1039 *retval
= sizeof(struct proc_regionwithpathinfo
);
1044 proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1046 struct proc_regionwithpathinfo preginfo
;
1048 uintptr_t vnodeaddr
;
1054 /* Loop while looking for vnodes that match dev_t filter */
1056 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1060 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, addr
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1066 vp
= (vnode_t
)vnodeaddr
;
1067 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1068 /* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
1069 struct vnode_attr va
;
1071 memset(&va
, 0, sizeof(va
));
1073 VATTR_WANTED(&va
, va_fsid
);
1075 ret
= vnode_getattr(vp
, &va
, vfs_context_current());
1081 if (va
.va_fsid
== arg
) {
1082 /* FILL THE VNODEINFO */
1083 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1085 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1086 /* Always make sure it is null terminated */
1087 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
1096 addr
= preginfo
.prp_prinfo
.pri_address
+ preginfo
.prp_prinfo
.pri_size
;
1099 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1101 *retval
= sizeof(struct proc_regionwithpathinfo
);
1106 * Path is relative to current process directory; may different from current
1110 proc_pidvnodepathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1112 struct proc_vnodepathinfo pvninfo
;
1114 vnode_t vncdirvp
= NULLVP
;
1115 uint32_t vncdirid
=0;
1116 vnode_t vnrdirvp
= NULLVP
;
1117 uint32_t vnrdirid
=0;
1120 bzero(&pvninfo
, sizeof(struct proc_vnodepathinfo
));
1123 if (p
->p_fd
->fd_cdir
) {
1124 vncdirvp
= p
->p_fd
->fd_cdir
;
1125 vncdirid
= p
->p_fd
->fd_cdir
->v_id
;
1127 if (p
->p_fd
->fd_rdir
) {
1128 vnrdirvp
= p
->p_fd
->fd_rdir
;
1129 vnrdirid
= p
->p_fd
->fd_rdir
->v_id
;
1133 if (vncdirvp
!= NULLVP
) {
1134 if ((error
= vnode_getwithvid(vncdirvp
, vncdirid
)) == 0) {
1135 /* FILL THE VNODEINFO */
1136 error
= fill_vnodeinfo(vncdirvp
, &pvninfo
.pvi_cdir
.vip_vi
);
1139 vn_getpath(vncdirvp
, &pvninfo
.pvi_cdir
.vip_path
[0], &count
);
1140 pvninfo
.pvi_cdir
.vip_path
[MAXPATHLEN
-1] = 0;
1142 vnode_put(vncdirvp
);
1148 if ((error
== 0) && (vnrdirvp
!= NULLVP
)) {
1149 if ((error
= vnode_getwithvid(vnrdirvp
, vnrdirid
)) == 0) {
1150 /* FILL THE VNODEINFO */
1151 error
= fill_vnodeinfo(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_vi
);
1154 vn_getpath(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_path
[0], &count
);
1155 pvninfo
.pvi_rdir
.vip_path
[MAXPATHLEN
-1] = 0;
1157 vnode_put(vnrdirvp
);
1163 error
= copyout(&pvninfo
, buffer
, sizeof(struct proc_vnodepathinfo
));
1165 *retval
= sizeof(struct proc_vnodepathinfo
);
1172 proc_pidpathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t *retval
)
1176 int len
= buffersize
;
1184 buf
= (char *)kalloc(buffersize
);
1188 bzero(buf
, buffersize
);
1190 error
= proc_pidpathinfo_internal(p
, arg
, buf
, buffersize
, retval
);
1192 error
= copyout(buf
, buffer
, len
);
1194 kfree(buf
, buffersize
);
1199 proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
)
1203 vnode_t nvp
= NULLVP
;
1204 int len
= buffersize
;
1211 vid
= vnode_vid(tvp
);
1212 error
= vnode_getwithvid(tvp
, vid
);
1214 error
= vn_getpath_fsenter(tvp
, buf
, &len
);
1217 error
= vnode_lookup(buf
, 0, &nvp
, vfs_context_current());
1218 if ((error
== 0) && ( nvp
!= NULLVP
))
1227 proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
)
1231 bzero(pwqinfo
, sizeof(struct proc_workqueueinfo
));
1233 error
= fill_procworkqueue(p
, pwqinfo
);
1243 proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
)
1245 p_uniqidinfo
->p_uniqueid
= proc_uniqueid(p
);
1246 proc_getexecutableuuid(p
, (unsigned char *)&p_uniqidinfo
->p_uuid
, sizeof(p_uniqidinfo
->p_uuid
));
1247 p_uniqidinfo
->p_puniqueid
= proc_puniqueid(p
);
1248 p_uniqidinfo
->p_reserve2
= 0;
1249 p_uniqidinfo
->p_reserve3
= 0;
1250 p_uniqidinfo
->p_reserve4
= 0;
1255 proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
)
1257 struct proc
* p
= PROC_NULL
;
1260 if (buffersize
< sizeof(uuid_t
))
1263 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1264 p
= proc_find_zombref(pid
);
1267 if (p
== PROC_NULL
) {
1271 proc_getexecutableuuid(p
, (unsigned char *)uuid_buf
, buffersize
);
1274 proc_drop_zombref(p
);
1282 * Function to get the uuid and pid of the originator of the voucher.
1285 proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
)
1287 pid_t originator_pid
;
1292 * Get the current voucher origin pid. The pid returned here
1293 * might not be valid or may have been recycled.
1295 kr
= thread_get_current_voucher_origin_pid(&originator_pid
);
1296 /* If errors, convert errors to appropriate format */
1298 if (kr
== KERN_INVALID_TASK
)
1300 else if (kr
== KERN_INVALID_VALUE
)
1307 *pid
= originator_pid
;
1308 error
= proc_piduuidinfo(originator_pid
, uuid
, buffersize
);
1313 * Function to get the uuid of the originator of the voucher.
1316 proc_pidoriginatoruuid(uuid_t uuid
, uint32_t buffersize
)
1318 pid_t originator_pid
;
1319 return (proc_pidoriginatorpid_uuid(uuid
, buffersize
, &originator_pid
));
1322 /***************************** proc_pidoriginatorinfo ***************************/
1325 proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1327 int error
= ENOTSUP
;
1331 case PROC_PIDORIGINATOR_UUID
:
1332 size
= PROC_PIDORIGINATOR_UUID_SIZE
;
1334 case PROC_PIDORIGINATOR_BGSTATE
:
1335 size
= PROC_PIDORIGINATOR_BGSTATE_SIZE
;
1337 case PROC_PIDORIGINATOR_PID_UUID
:
1338 size
= PROC_PIDORIGINATOR_PID_UUID_SIZE
;
1344 if (buffersize
< size
)
1347 if (pid
!= 0 && pid
!= proc_selfpid())
1351 case PROC_PIDORIGINATOR_UUID
: {
1354 error
= proc_pidoriginatoruuid(uuid
, sizeof(uuid
));
1358 error
= copyout(uuid
, buffer
, size
);
1364 case PROC_PIDORIGINATOR_PID_UUID
: {
1365 struct proc_originatorinfo originator_info
;
1366 bzero(&originator_info
, sizeof(originator_info
));
1368 error
= proc_pidoriginatorpid_uuid(originator_info
.originator_uuid
,
1369 sizeof(uuid_t
), &originator_info
.originator_pid
);
1373 error
= copyout(&originator_info
, buffer
, size
);
1379 case PROC_PIDORIGINATOR_BGSTATE
: {
1380 uint32_t is_backgrounded
;
1381 error
= proc_get_originatorbgstate(&is_backgrounded
);
1385 error
= copyout(&is_backgrounded
, buffer
, size
);
1398 /***************************** proc_listcoalitions ***************************/
1399 int proc_listcoalitions(int flavor
, int type
, user_addr_t buffer
,
1400 uint32_t buffersize
, int32_t *retval
)
1402 #if CONFIG_COALITIONS
1403 int error
= ENOTSUP
;
1406 void *coalinfo
= NULL
;
1407 uint32_t k_buffersize
= 0, copyout_sz
= 0;
1408 int ncoals
= 0, ncoals_
= 0;
1410 /* struct procinfo_coalinfo; */
1413 case LISTCOALITIONS_ALL_COALS
:
1414 elem_size
= LISTCOALITIONS_ALL_COALS_SIZE
;
1417 case LISTCOALITIONS_SINGLE_TYPE
:
1418 elem_size
= LISTCOALITIONS_SINGLE_TYPE_SIZE
;
1425 /* find the total number of coalitions */
1426 ncoals
= coalitions_get_list(coal_type
, NULL
, 0);
1428 if (ncoals
== 0 || buffer
== 0 || buffersize
== 0) {
1430 * user just wants buffer size
1431 * or there are no coalitions
1434 *retval
= (int)(ncoals
* elem_size
);
1438 k_buffersize
= ncoals
* elem_size
;
1439 coalinfo
= kalloc((vm_size_t
)k_buffersize
);
1444 bzero(coalinfo
, k_buffersize
);
1447 case LISTCOALITIONS_ALL_COALS
:
1448 case LISTCOALITIONS_SINGLE_TYPE
:
1449 ncoals_
= coalitions_get_list(coal_type
, coalinfo
, ncoals
);
1452 panic("memory corruption?!");
1456 /* all the coalitions disappeared... weird but valid */
1463 * Some coalitions may have disappeared between our initial check,
1464 * and the the actual list acquisition.
1465 * Only copy out what we really need.
1467 copyout_sz
= k_buffersize
;
1468 if (ncoals_
< ncoals
)
1469 copyout_sz
= ncoals_
* elem_size
;
1472 * copy the list up to user space
1473 * (we're guaranteed to have a non-null pointer/size here)
1475 error
= copyout(coalinfo
, buffer
,
1476 copyout_sz
< buffersize
? copyout_sz
: buffersize
);
1479 *retval
= (int)copyout_sz
;
1483 kfree(coalinfo
, k_buffersize
);
1487 /* no coalition support */
1498 /*************************** proc_can_use_forgeound_hw **************************/
1499 int proc_can_use_foreground_hw(int pid
, user_addr_t u_reason
, uint32_t reasonsize
, int32_t *retval
)
1501 proc_t p
= PROC_NULL
;
1503 uint32_t reason
= PROC_FGHW_ERROR
;
1505 task_t task
= TASK_NULL
;
1506 #if CONFIG_COALITIONS
1507 coalition_t coal
= COALITION_NULL
;
1514 reason
= PROC_FGHW_ERROR
;
1519 if (p
== PROC_NULL
) {
1521 reason
= PROC_FGHW_ERROR
;
1525 #if CONFIG_COALITIONS
1526 if (p
!= current_proc() &&
1527 !kauth_cred_issuser(kauth_cred_get())) {
1529 reason
= PROC_FGHW_ERROR
;
1534 task_reference(task
);
1535 if (coalition_is_leader(task
, COALITION_TYPE_JETSAM
, &coal
) == FALSE
) {
1536 /* current task is not a coalition leader: find the leader */
1537 task_deallocate(task
);
1538 task
= coalition_get_leader(coal
);
1541 if (task
!= TASK_NULL
) {
1543 * If task is non-null, then it is the coalition leader of the
1544 * current process' coalition. This could be the same task as
1545 * the current_task, and that's OK.
1550 proc_get_darwinbgstate(task
, &flags
);
1551 if ((flags
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
1553 * Coalition leader is not an application, continue
1554 * searching for other ways this task could gain
1557 reason
= PROC_FGHW_DAEMON_LEADER
;
1561 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
)) {
1563 * If the leader of the current process' coalition has
1564 * been marked as DARWIN_BG, then it definitely should
1565 * not be using foreground hardware resources.
1567 reason
= PROC_FGHW_LEADER_BACKGROUND
;
1571 role
= proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
);
1573 case TASK_FOREGROUND_APPLICATION
: /* DARWIN_ROLE_UI_FOCAL */
1574 case TASK_BACKGROUND_APPLICATION
: /* DARWIN_ROLE_UI */
1576 * The leader of this coalition is a focal, UI app:
1578 * TODO: should extensions/plugins be allowed to use
1582 reason
= PROC_FGHW_OK
;
1584 case TASK_DEFAULT_APPLICATION
: /* DARWIN_ROLE_UI_NON_FOCAL */
1585 case TASK_NONUI_APPLICATION
: /* DARWIN_ROLE_NON_UI */
1586 case TASK_THROTTLE_APPLICATION
:
1587 case TASK_UNSPECIFIED
:
1589 /* non-focal, non-ui apps don't get access */
1590 reason
= PROC_FGHW_LEADER_NONUI
;
1596 if (task
!= TASK_NULL
) {
1597 task_deallocate(task
);
1600 #endif /* CONFIG_COALITIONS */
1603 * There is no reasonable semantic to investigate the currently
1604 * adopted voucher of an arbitrary thread in a non-current process.
1607 if (p
!= current_proc()) {
1613 * In the absence of coalitions, fall back to a voucher-based lookup
1614 * where a daemon can used foreground HW if it's operating on behalf
1615 * of a foreground application.
1616 * NOTE: this is equivalent to a call to
1617 * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
1620 error
= proc_get_originatorbgstate(&isBG
);
1625 reason
= PROC_FGHW_NO_ORIGINATOR
;
1629 reason
= PROC_FGHW_NO_VOUCHER_ATTR
;
1633 reason
= PROC_FGHW_DAEMON_NO_VOUCHER
;
1637 /* some other error occurred: report that to the caller */
1638 reason
= PROC_FGHW_VOUCHER_ERROR
;
1643 reason
= PROC_FGHW_ORIGINATOR_BACKGROUND
;
1647 * The process itself is either a foreground app, or has
1648 * adopted a voucher originating from an app that's still in
1651 reason
= PROC_FGHW_DAEMON_OK
;
1656 if (task
!= TASK_NULL
)
1657 task_deallocate(task
);
1660 if (reasonsize
>= sizeof(reason
) && u_reason
!= (user_addr_t
)0)
1661 (void)copyout(&reason
, u_reason
, sizeof(reason
));
1666 /********************************** proc_pidinfo ********************************/
1670 proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1672 struct proc
* p
= PROC_NULL
;
1673 int error
= ENOTSUP
;
1676 int shortversion
= 0;
1680 int uniqidversion
= 0;
1681 boolean_t check_same_user
;
1684 case PROC_PIDLISTFDS
:
1685 size
= PROC_PIDLISTFD_SIZE
;
1686 if (buffer
== USER_ADDR_NULL
)
1689 case PROC_PIDTBSDINFO
:
1690 size
= PROC_PIDTBSDINFO_SIZE
;
1692 case PROC_PIDTASKINFO
:
1693 size
= PROC_PIDTASKINFO_SIZE
;
1695 case PROC_PIDTASKALLINFO
:
1696 size
= PROC_PIDTASKALLINFO_SIZE
;
1698 case PROC_PIDTHREADINFO
:
1699 size
= PROC_PIDTHREADINFO_SIZE
;
1701 case PROC_PIDLISTTHREADS
:
1702 size
= PROC_PIDLISTTHREADS_SIZE
;
1704 case PROC_PIDREGIONINFO
:
1705 size
= PROC_PIDREGIONINFO_SIZE
;
1707 case PROC_PIDREGIONPATHINFO
:
1708 size
= PROC_PIDREGIONPATHINFO_SIZE
;
1710 case PROC_PIDVNODEPATHINFO
:
1711 size
= PROC_PIDVNODEPATHINFO_SIZE
;
1713 case PROC_PIDTHREADPATHINFO
:
1714 size
= PROC_PIDTHREADPATHINFO_SIZE
;
1716 case PROC_PIDPATHINFO
:
1719 case PROC_PIDWORKQUEUEINFO
:
1720 /* kernel does not have workq info */
1724 size
= PROC_PIDWORKQUEUEINFO_SIZE
;
1726 case PROC_PIDT_SHORTBSDINFO
:
1727 size
= PROC_PIDT_SHORTBSDINFO_SIZE
;
1729 case PROC_PIDLISTFILEPORTS
:
1730 size
= PROC_PIDLISTFILEPORTS_SIZE
;
1731 if (buffer
== (user_addr_t
)0)
1734 case PROC_PIDTHREADID64INFO
:
1735 size
= PROC_PIDTHREADID64INFO_SIZE
;
1737 case PROC_PIDUNIQIDENTIFIERINFO
:
1738 size
= PROC_PIDUNIQIDENTIFIERINFO_SIZE
;
1740 case PROC_PIDT_BSDINFOWITHUNIQID
:
1741 size
= PROC_PIDT_BSDINFOWITHUNIQID_SIZE
;
1743 case PROC_PIDARCHINFO
:
1744 size
= PROC_PIDARCHINFO_SIZE
;
1746 case PROC_PIDCOALITIONINFO
:
1747 size
= PROC_PIDCOALITIONINFO_SIZE
;
1749 case PROC_PIDNOTEEXIT
:
1751 * Set findzomb explicitly because arg passed
1752 * in is used as note exit status bits.
1754 size
= PROC_PIDNOTEEXIT_SIZE
;
1757 case PROC_PIDEXITREASONINFO
:
1758 size
= PROC_PIDEXITREASONINFO_SIZE
;
1761 case PROC_PIDEXITREASONBASICINFO
:
1762 size
= PROC_PIDEXITREASONBASICINFOSIZE
;
1765 case PROC_PIDREGIONPATHINFO2
:
1766 size
= PROC_PIDREGIONPATHINFO2_SIZE
;
1768 case PROC_PIDREGIONPATHINFO3
:
1769 size
= PROC_PIDREGIONPATHINFO3_SIZE
;
1771 case PROC_PIDLISTUPTRS
:
1772 size
= PROC_PIDLISTUPTRS_SIZE
;
1773 if (buffer
== USER_ADDR_NULL
) {
1777 case PROC_PIDLISTDYNKQUEUES
:
1778 size
= PROC_PIDLISTDYNKQUEUES_SIZE
;
1779 if (buffer
== USER_ADDR_NULL
) {
1787 if (buffersize
< size
)
1790 if ((flavor
== PROC_PIDPATHINFO
) && (buffersize
> PROC_PIDPATHINFO_MAXSIZE
)) {
1794 /* Check if we need to look for zombies */
1795 if ((flavor
== PROC_PIDTBSDINFO
) || (flavor
== PROC_PIDT_SHORTBSDINFO
) || (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1796 || (flavor
== PROC_PIDUNIQIDENTIFIERINFO
)) {
1801 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1803 p
= proc_find_zombref(pid
);
1804 if (p
== PROC_NULL
) {
1813 /* Certain operations don't require privileges */
1815 case PROC_PIDT_SHORTBSDINFO
:
1816 case PROC_PIDUNIQIDENTIFIERINFO
:
1817 case PROC_PIDPATHINFO
:
1818 case PROC_PIDCOALITIONINFO
:
1819 check_same_user
= NO_CHECK_SAME_USER
;
1822 check_same_user
= CHECK_SAME_USER
;
1826 /* Do we have permission to look into this? */
1827 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDINFO
, flavor
, check_same_user
)))
1831 case PROC_PIDLISTFDS
: {
1832 error
= proc_pidfdlist(p
, buffer
, buffersize
, retval
);
1836 case PROC_PIDUNIQIDENTIFIERINFO
: {
1837 struct proc_uniqidentifierinfo p_uniqidinfo
;
1838 bzero(&p_uniqidinfo
, sizeof(p_uniqidinfo
));
1839 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
1840 error
= copyout(&p_uniqidinfo
, buffer
, sizeof(struct proc_uniqidentifierinfo
));
1842 *retval
= sizeof(struct proc_uniqidentifierinfo
);
1846 case PROC_PIDT_SHORTBSDINFO
:
1848 case PROC_PIDT_BSDINFOWITHUNIQID
:
1849 case PROC_PIDTBSDINFO
: {
1850 struct proc_bsdinfo pbsd
;
1851 struct proc_bsdshortinfo pbsd_short
;
1852 struct proc_bsdinfowithuniqid pbsd_uniqid
;
1854 if (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1857 if (shortversion
!= 0) {
1858 error
= proc_pidshortbsdinfo(p
, &pbsd_short
, zombie
);
1860 error
= proc_pidbsdinfo(p
, &pbsd
, zombie
);
1861 if (uniqidversion
!= 0) {
1862 bzero(&pbsd_uniqid
, sizeof(pbsd_uniqid
));
1863 proc_piduniqidentifierinfo(p
, &pbsd_uniqid
.p_uniqidentifier
);
1864 pbsd_uniqid
.pbsd
= pbsd
;
1869 if (shortversion
!= 0) {
1870 error
= copyout(&pbsd_short
, buffer
, sizeof(struct proc_bsdshortinfo
));
1872 *retval
= sizeof(struct proc_bsdshortinfo
);
1873 } else if (uniqidversion
!= 0) {
1874 error
= copyout(&pbsd_uniqid
, buffer
, sizeof(struct proc_bsdinfowithuniqid
));
1876 *retval
= sizeof(struct proc_bsdinfowithuniqid
);
1878 error
= copyout(&pbsd
, buffer
, sizeof(struct proc_bsdinfo
));
1880 *retval
= sizeof(struct proc_bsdinfo
);
1886 case PROC_PIDTASKINFO
: {
1887 struct proc_taskinfo ptinfo
;
1889 error
= proc_pidtaskinfo(p
, &ptinfo
);
1891 error
= copyout(&ptinfo
, buffer
, sizeof(struct proc_taskinfo
));
1893 *retval
= sizeof(struct proc_taskinfo
);
1898 case PROC_PIDTASKALLINFO
: {
1899 struct proc_taskallinfo pall
;
1900 bzero(&pall
, sizeof(pall
));
1901 error
= proc_pidbsdinfo(p
, &pall
.pbsd
, 0);
1902 error
= proc_pidtaskinfo(p
, &pall
.ptinfo
);
1904 error
= copyout(&pall
, buffer
, sizeof(struct proc_taskallinfo
));
1906 *retval
= sizeof(struct proc_taskallinfo
);
1911 case PROC_PIDTHREADID64INFO
:
1913 case PROC_PIDTHREADINFO
:{
1914 struct proc_threadinfo pthinfo
;
1916 error
= proc_pidthreadinfo(p
, arg
, thuniqueid
, &pthinfo
);
1918 error
= copyout(&pthinfo
, buffer
, sizeof(struct proc_threadinfo
));
1920 *retval
= sizeof(struct proc_threadinfo
);
1925 case PROC_PIDLISTTHREADS
:{
1926 error
= proc_pidlistthreads(p
, buffer
, buffersize
, retval
);
1930 case PROC_PIDREGIONINFO
:{
1931 error
= proc_pidregioninfo(p
, arg
, buffer
, buffersize
, retval
);
1936 case PROC_PIDREGIONPATHINFO
:{
1937 error
= proc_pidregionpathinfo(p
, arg
, buffer
, buffersize
, retval
);
1941 case PROC_PIDREGIONPATHINFO2
:{
1942 error
= proc_pidregionpathinfo2(p
, arg
, buffer
, buffersize
, retval
);
1946 case PROC_PIDREGIONPATHINFO3
:{
1947 error
= proc_pidregionpathinfo3(p
, arg
, buffer
, buffersize
, retval
);
1951 case PROC_PIDVNODEPATHINFO
:{
1952 error
= proc_pidvnodepathinfo(p
, arg
, buffer
, buffersize
, retval
);
1957 case PROC_PIDTHREADPATHINFO
:{
1958 struct proc_threadwithpathinfo pinfo
;
1960 error
= proc_pidthreadpathinfo(p
, arg
, &pinfo
);
1962 error
= copyout((caddr_t
)&pinfo
, buffer
, sizeof(struct proc_threadwithpathinfo
));
1964 *retval
= sizeof(struct proc_threadwithpathinfo
);
1969 case PROC_PIDPATHINFO
: {
1970 error
= proc_pidpathinfo(p
, arg
, buffer
, buffersize
, retval
);
1975 case PROC_PIDWORKQUEUEINFO
:{
1976 struct proc_workqueueinfo pwqinfo
;
1978 error
= proc_pidworkqueueinfo(p
, &pwqinfo
);
1980 error
= copyout(&pwqinfo
, buffer
, sizeof(struct proc_workqueueinfo
));
1982 *retval
= sizeof(struct proc_workqueueinfo
);
1987 case PROC_PIDLISTFILEPORTS
: {
1988 error
= proc_pidfileportlist(p
, buffer
, buffersize
, retval
);
1992 case PROC_PIDARCHINFO
: {
1993 struct proc_archinfo pai
;
1994 bzero(&pai
, sizeof(pai
));
1995 proc_archinfo(p
, &pai
);
1996 error
= copyout(&pai
, buffer
, sizeof(struct proc_archinfo
));
1998 *retval
= sizeof(struct proc_archinfo
);
2003 case PROC_PIDCOALITIONINFO
: {
2004 struct proc_pidcoalitioninfo pci
;
2005 proc_pidcoalitioninfo(p
, &pci
);
2006 error
= copyout(&pci
, buffer
, sizeof(struct proc_pidcoalitioninfo
));
2008 *retval
= sizeof(struct proc_pidcoalitioninfo
);
2013 case PROC_PIDNOTEEXIT
: {
2015 error
= proc_pidnoteexit(p
, arg
, &data
);
2017 error
= copyout(&data
, buffer
, sizeof(data
));
2019 *retval
= sizeof(data
);
2025 case PROC_PIDEXITREASONINFO
: {
2026 struct proc_exitreasoninfo eri
;
2028 error
= copyin(buffer
, &eri
, sizeof(eri
));
2033 error
= proc_pidexitreasoninfo(p
, &eri
, NULL
);
2035 error
= copyout(&eri
, buffer
, sizeof(eri
));
2037 *retval
= sizeof(eri
);
2043 case PROC_PIDEXITREASONBASICINFO
: {
2044 struct proc_exitreasonbasicinfo beri
;
2046 bzero(&beri
, sizeof(struct proc_exitreasonbasicinfo
));
2048 error
= proc_pidexitreasoninfo(p
, NULL
, &beri
);
2050 error
= copyout(&beri
, buffer
, sizeof(beri
));
2052 *retval
= sizeof(beri
);
2058 case PROC_PIDLISTUPTRS
:
2059 error
= proc_pidlistuptrs(p
, buffer
, buffersize
, retval
);
2062 case PROC_PIDLISTDYNKQUEUES
:
2063 error
= kevent_copyout_proc_dynkqids(p
, buffer
, buffersize
, retval
);
2075 proc_drop_zombref(p
);
2081 pid_vnodeinfo(vnode_t vp
, uint32_t vid
, struct fileproc
* fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2083 struct vnode_fdinfo vfi
;
2086 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2089 bzero(&vfi
, sizeof(struct vnode_fdinfo
));
2090 fill_fileinfo(fp
, proc
, fd
, &vfi
.pfi
);
2091 error
= fill_vnodeinfo(vp
, &vfi
.pvi
);
2094 error
= copyout((caddr_t
)&vfi
, buffer
, sizeof(struct vnode_fdinfo
));
2096 *retval
= sizeof(struct vnode_fdinfo
);
2102 pid_vnodeinfopath(vnode_t vp
, uint32_t vid
, struct fileproc
* fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2104 struct vnode_fdinfowithpath vfip
;
2105 int count
, error
= 0;
2107 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2110 bzero(&vfip
, sizeof(struct vnode_fdinfowithpath
));
2111 fill_fileinfo(fp
, proc
, fd
, &vfip
.pfi
);
2112 error
= fill_vnodeinfo(vp
, &vfip
.pvip
.vip_vi
) ;
2115 vn_getpath(vp
, &vfip
.pvip
.vip_path
[0], &count
);
2116 vfip
.pvip
.vip_path
[MAXPATHLEN
-1] = 0;
2118 error
= copyout((caddr_t
)&vfip
, buffer
, sizeof(struct vnode_fdinfowithpath
));
2120 *retval
= sizeof(struct vnode_fdinfowithpath
);
2127 fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* fproc
)
2129 fproc
->fi_openflags
= fp
->f_fglob
->fg_flag
;
2130 fproc
->fi_status
= 0;
2131 fproc
->fi_offset
= fp
->f_fglob
->fg_offset
;
2132 fproc
->fi_type
= FILEGLOB_DTYPE(fp
->f_fglob
);
2133 if (fp
->f_fglob
->fg_count
> 1)
2134 fproc
->fi_status
|= PROC_FP_SHARED
;
2135 if (proc
!= PROC_NULL
) {
2136 if ((FDFLAGS_GET(proc
, fd
) & UF_EXCLOSE
) != 0)
2137 fproc
->fi_status
|= PROC_FP_CLEXEC
;
2138 if ((FDFLAGS_GET(proc
, fd
) & UF_FORKCLOSE
) != 0)
2139 fproc
->fi_status
|= PROC_FP_CLFORK
;
2141 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
2142 fproc
->fi_status
|= PROC_FP_GUARDED
;
2143 fproc
->fi_guardflags
= 0;
2144 if (fp_isguarded(fp
, GUARD_CLOSE
))
2145 fproc
->fi_guardflags
|= PROC_FI_GUARD_CLOSE
;
2146 if (fp_isguarded(fp
, GUARD_DUP
))
2147 fproc
->fi_guardflags
|= PROC_FI_GUARD_DUP
;
2148 if (fp_isguarded(fp
, GUARD_SOCKET_IPC
))
2149 fproc
->fi_guardflags
|= PROC_FI_GUARD_SOCKET_IPC
;
2150 if (fp_isguarded(fp
, GUARD_FILEPORT
))
2151 fproc
->fi_guardflags
|= PROC_FI_GUARD_FILEPORT
;
2158 fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
)
2160 vfs_context_t context
;
2164 bzero(&sb
, sizeof(struct stat64
));
2165 context
= vfs_context_create((vfs_context_t
)0);
2166 error
= vn_stat(vp
, &sb
, NULL
, 1, context
);
2167 (void)vfs_context_rele(context
);
2169 munge_vinfo_stat(&sb
, &vinfo
->vi_stat
);
2174 if (vp
->v_mount
!= dead_mountp
) {
2175 vinfo
->vi_fsid
= vp
->v_mount
->mnt_vfsstat
.f_fsid
;
2177 vinfo
->vi_fsid
.val
[0] = 0;
2178 vinfo
->vi_fsid
.val
[1] = 0;
2180 vinfo
->vi_type
= vp
->v_type
;
2186 pid_socketinfo(socket_t so
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2189 struct socket_fdinfo s
;
2192 bzero(&s
, sizeof(struct socket_fdinfo
));
2193 fill_fileinfo(fp
, proc
, fd
, &s
.pfi
);
2194 if ((error
= fill_socketinfo(so
, &s
.psi
)) == 0) {
2195 if ((error
= copyout(&s
, buffer
, sizeof(struct socket_fdinfo
))) == 0)
2196 *retval
= sizeof(struct socket_fdinfo
);
2200 #pragma unused(so, fp, proc, fd, buffer)
2207 pid_pseminfo(struct psemnode
*psem
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2209 struct psem_fdinfo pseminfo
;
2212 bzero(&pseminfo
, sizeof(struct psem_fdinfo
));
2213 fill_fileinfo(fp
, proc
, fd
, &pseminfo
.pfi
);
2215 if ((error
= fill_pseminfo(psem
, &pseminfo
.pseminfo
)) == 0) {
2216 if ((error
= copyout(&pseminfo
, buffer
, sizeof(struct psem_fdinfo
))) == 0)
2217 *retval
= sizeof(struct psem_fdinfo
);
2224 pid_pshminfo(struct pshmnode
*pshm
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2226 struct pshm_fdinfo pshminfo
;
2229 bzero(&pshminfo
, sizeof(struct pshm_fdinfo
));
2230 fill_fileinfo(fp
, proc
, fd
, &pshminfo
.pfi
);
2232 if ((error
= fill_pshminfo(pshm
, &pshminfo
.pshminfo
)) == 0) {
2233 if ((error
= copyout(&pshminfo
, buffer
, sizeof(struct pshm_fdinfo
))) == 0)
2234 *retval
= sizeof(struct pshm_fdinfo
);
2241 pid_pipeinfo(struct pipe
* p
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2243 struct pipe_fdinfo pipeinfo
;
2246 bzero(&pipeinfo
, sizeof(struct pipe_fdinfo
));
2247 fill_fileinfo(fp
, proc
, fd
, &pipeinfo
.pfi
);
2248 if ((error
= fill_pipeinfo(p
, &pipeinfo
.pipeinfo
)) == 0) {
2249 if ((error
= copyout(&pipeinfo
, buffer
, sizeof(struct pipe_fdinfo
))) == 0)
2250 *retval
= sizeof(struct pipe_fdinfo
);
2257 pid_kqueueinfo(struct kqueue
* kq
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2259 struct kqueue_fdinfo kqinfo
;
2262 bzero(&kqinfo
, sizeof(struct kqueue_fdinfo
));
2264 /* not all kq's are associated with a file (e.g. workqkq) */
2267 fill_fileinfo(fp
, proc
, fd
, &kqinfo
.pfi
);
2270 if ((error
= fill_kqueueinfo(kq
, &kqinfo
.kqueueinfo
)) == 0) {
2271 if ((error
= copyout(&kqinfo
, buffer
, sizeof(struct kqueue_fdinfo
))) == 0)
2272 *retval
= sizeof(struct kqueue_fdinfo
);
2279 pid_atalkinfo(__unused
struct atalk
* at
, __unused
struct fileproc
*fp
, __unused proc_t proc
, __unused
int fd
, __unused user_addr_t buffer
, __unused
uint32_t buffersize
, __unused
int32_t * retval
)
2285 /************************** proc_pidfdinfo routine ***************************/
2287 proc_pidfdinfo(int pid
, int flavor
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2290 int error
= ENOTSUP
;
2291 struct fileproc
* fp
= NULL
;
2295 case PROC_PIDFDVNODEINFO
:
2296 size
= PROC_PIDFDVNODEINFO_SIZE
;
2298 case PROC_PIDFDVNODEPATHINFO
:
2299 size
= PROC_PIDFDVNODEPATHINFO_SIZE
;
2301 case PROC_PIDFDSOCKETINFO
:
2302 size
= PROC_PIDFDSOCKETINFO_SIZE
;
2304 case PROC_PIDFDPSEMINFO
:
2305 size
= PROC_PIDFDPSEMINFO_SIZE
;
2307 case PROC_PIDFDPSHMINFO
:
2308 size
= PROC_PIDFDPSHMINFO_SIZE
;
2310 case PROC_PIDFDPIPEINFO
:
2311 size
= PROC_PIDFDPIPEINFO_SIZE
;
2313 case PROC_PIDFDKQUEUEINFO
:
2314 size
= PROC_PIDFDKQUEUEINFO_SIZE
;
2316 case PROC_PIDFDKQUEUE_EXTINFO
:
2317 size
= PROC_PIDFDKQUEUE_EXTINFO_SIZE
;
2318 if (buffer
== (user_addr_t
)0)
2321 case PROC_PIDFDATALKINFO
:
2322 size
= PROC_PIDFDATALKINFO_SIZE
;
2330 if (buffersize
< size
)
2333 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2338 /* Do we have permission to look into this? */
2339 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFDINFO
, flavor
, CHECK_SAME_USER
)))
2343 case PROC_PIDFDVNODEINFO
: {
2347 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) !=0) {
2350 /* no need to be under the fdlock */
2351 error
= pid_vnodeinfo(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2355 case PROC_PIDFDVNODEPATHINFO
: {
2359 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) !=0) {
2363 /* no need to be under the fdlock */
2364 error
= pid_vnodeinfopath(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2368 case PROC_PIDFDSOCKETINFO
: {
2371 if ((error
= fp_getfsock(p
, fd
, &fp
, &so
)) !=0) {
2374 /* no need to be under the fdlock */
2375 error
= pid_socketinfo(so
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2379 case PROC_PIDFDPSEMINFO
: {
2380 struct psemnode
* psem
;
2382 if ((error
= fp_getfpsem(p
, fd
, &fp
, &psem
)) !=0) {
2385 /* no need to be under the fdlock */
2386 error
= pid_pseminfo(psem
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2390 case PROC_PIDFDPSHMINFO
: {
2391 struct pshmnode
* pshm
;
2393 if ((error
= fp_getfpshm(p
, fd
, &fp
, &pshm
)) !=0) {
2396 /* no need to be under the fdlock */
2397 error
= pid_pshminfo(pshm
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2401 case PROC_PIDFDPIPEINFO
: {
2402 struct pipe
* cpipe
;
2404 if ((error
= fp_getfpipe(p
, fd
, &fp
, &cpipe
)) !=0) {
2407 /* no need to be under the fdlock */
2408 error
= pid_pipeinfo(cpipe
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2412 case PROC_PIDFDKQUEUEINFO
: {
2416 if ((kq
= p
->p_fd
->fd_wqkqueue
) == NULL
) {
2417 /* wqkqueue is initialized on-demand */
2421 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2425 /* no need to be under the fdlock */
2426 error
= pid_kqueueinfo(kq
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2430 case PROC_PIDFDKQUEUE_EXTINFO
: {
2434 if ((kq
= p
->p_fd
->fd_wqkqueue
) == NULL
) {
2435 /* wqkqueue is initialized on-demand */
2439 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2442 error
= pid_kqueue_extinfo(p
, kq
, buffer
, buffersize
, retval
);
2453 fp_drop(p
, fd
, fp
, 0);
2461 #define MAX_UPTRS 16392
2464 proc_pidlistuptrs(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2471 if (buffer
!= USER_ADDR_NULL
) {
2472 count
= buffersize
/ sizeof(uint64_t);
2473 if (count
> MAX_UPTRS
) {
2477 buffersize
= count
* sizeof(uint64_t);
2478 kbuf
= kalloc(buffersize
);
2479 bzero(kbuf
, buffersize
);
2480 assert(kbuf
!= NULL
);
2488 nuptrs
= kevent_proc_copy_uptrs(p
, kbuf
, buffersize
);
2492 if (os_mul_overflow(nuptrs
, sizeof(uint64_t), ©size
)) {
2496 if (copysize
> buffersize
) {
2497 copysize
= buffersize
;
2499 error
= copyout(kbuf
, buffer
, copysize
);
2506 kfree(kbuf
, buffersize
);
2514 * Helper function for proc_pidfileportinfo
2517 struct fileport_info_args
{
2519 user_addr_t fia_buffer
;
2520 uint32_t fia_buffersize
;
2521 int32_t *fia_retval
;
2524 static kern_return_t
2525 proc_fileport_info(__unused mach_port_name_t name
,
2526 struct fileglob
*fg
, void *arg
)
2528 struct fileport_info_args
*fia
= arg
;
2529 struct fileproc __fileproc
, *fp
= &__fileproc
;
2532 bzero(fp
, sizeof (*fp
));
2535 switch (fia
->fia_flavor
) {
2536 case PROC_PIDFILEPORTVNODEPATHINFO
: {
2539 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
2543 vp
= (struct vnode
*)fg
->fg_data
;
2544 error
= pid_vnodeinfopath(vp
, vnode_vid(vp
), fp
, PROC_NULL
, 0,
2545 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2548 case PROC_PIDFILEPORTSOCKETINFO
: {
2551 if (FILEGLOB_DTYPE(fg
) != DTYPE_SOCKET
) {
2555 so
= (socket_t
)fg
->fg_data
;
2556 error
= pid_socketinfo(so
, fp
, PROC_NULL
, 0,
2557 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2560 case PROC_PIDFILEPORTPSHMINFO
: {
2561 struct pshmnode
*pshm
;
2563 if (FILEGLOB_DTYPE(fg
) != DTYPE_PSXSHM
) {
2564 error
= EBADF
; /* ick - mirror fp_getfpshm */
2567 pshm
= (struct pshmnode
*)fg
->fg_data
;
2568 error
= pid_pshminfo(pshm
, fp
, PROC_NULL
, 0,
2569 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2572 case PROC_PIDFILEPORTPIPEINFO
: {
2575 if (FILEGLOB_DTYPE(fg
) != DTYPE_PIPE
) {
2576 error
= EBADF
; /* ick - mirror fp_getfpipe */
2579 cpipe
= (struct pipe
*)fg
->fg_data
;
2580 error
= pid_pipeinfo(cpipe
, fp
, PROC_NULL
, 0,
2581 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2592 /************************* proc_pidfileportinfo routine *********************/
2594 proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
,
2595 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2598 int error
= ENOTSUP
;
2600 struct fileport_info_args fia
;
2602 /* fileport types are restricted by file_issendable() */
2605 case PROC_PIDFILEPORTVNODEPATHINFO
:
2606 size
= PROC_PIDFILEPORTVNODEPATHINFO_SIZE
;
2608 case PROC_PIDFILEPORTSOCKETINFO
:
2609 size
= PROC_PIDFILEPORTSOCKETINFO_SIZE
;
2611 case PROC_PIDFILEPORTPSHMINFO
:
2612 size
= PROC_PIDFILEPORTPSHMINFO_SIZE
;
2614 case PROC_PIDFILEPORTPIPEINFO
:
2615 size
= PROC_PIDFILEPORTPIPEINFO_SIZE
;
2621 if (buffersize
< size
)
2623 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2628 /* Do we have permission to look into this? */
2629 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFILEPORTINFO
, flavor
, CHECK_SAME_USER
)))
2632 fia
.fia_flavor
= flavor
;
2633 fia
.fia_buffer
= buffer
;
2634 fia
.fia_buffersize
= buffersize
;
2635 fia
.fia_retval
= retval
;
2637 if (fileport_invoke(p
->task
, name
,
2638 proc_fileport_info
, &fia
, &error
) != KERN_SUCCESS
)
2647 proc_security_policy(proc_t targetp
, __unused
int callnum
, __unused
int flavor
, boolean_t check_same_user
)
2652 if ((error
= mac_proc_check_proc_info(current_proc(), targetp
, callnum
, flavor
)))
2656 /* The 'listpids' call doesn't have a target proc */
2657 if (targetp
== PROC_NULL
) {
2658 assert(callnum
== PROC_INFO_CALL_LISTPIDS
&& check_same_user
== NO_CHECK_SAME_USER
);
2663 * Check for 'get information for processes owned by other users' privilege
2664 * root has this privilege by default
2666 if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO
, 0) == 0)
2667 check_same_user
= FALSE
;
2669 if (check_same_user
) {
2670 kauth_cred_t target_cred
;
2673 target_cred
= kauth_cred_proc_ref(targetp
);
2674 target_uid
= kauth_cred_getuid(target_cred
);
2675 kauth_cred_unref(&target_cred
);
2677 if (kauth_getuid() != target_uid
)
2685 proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2687 if (suser(kauth_cred_get(), (u_short
*)0) == 0) {
2688 return(log_dmesg(buffer
, buffersize
, retval
));
2693 /* ********* process control sets on self only */
2695 proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t * retval
)
2697 struct proc
* pself
= PROC_NULL
;
2699 uint32_t pcontrol
= (uint32_t)arg
;
2700 struct uthread
*ut
= NULL
;
2701 char name_buf
[MAXTHREADNAMESIZE
];
2703 pself
= current_proc();
2704 if (pid
!= pself
->p_pid
)
2707 /* Do we have permission to look into this? */
2708 if ((error
= proc_security_policy(pself
, PROC_INFO_CALL_SETCONTROL
, flavor
, NO_CHECK_SAME_USER
)))
2712 case PROC_SELFSET_PCONTROL
: {
2713 if (pcontrol
> P_PCMAX
)
2716 /* reset existing control setting while retaining action state */
2717 pself
->p_pcaction
&= PROC_ACTION_MASK
;
2718 /* set new control state */
2719 pself
->p_pcaction
|= pcontrol
;
2724 case PROC_SELFSET_THREADNAME
: {
2726 * This is a bit ugly, as it copies the name into the kernel, and then
2727 * invokes bsd_setthreadname again to copy it into the uthread name
2728 * buffer. Hopefully this isn't such a hot codepath that an additional
2729 * MAXTHREADNAMESIZE copy is a big issue.
2731 if (buffersize
> (MAXTHREADNAMESIZE
- 1)) {
2732 return ENAMETOOLONG
;
2735 ut
= current_uthread();
2737 bzero(name_buf
, MAXTHREADNAMESIZE
);
2738 error
= copyin(buffer
, name_buf
, buffersize
);
2741 bsd_setthreadname(ut
, name_buf
);
2746 case PROC_SELFSET_VMRSRCOWNER
: {
2747 /* need to to be superuser */
2748 if (suser(kauth_cred_get(), (u_short
*)0) != 0) {
2754 /* reset existing control setting while retaining action state */
2755 pself
->p_lflag
|= P_LVMRSRCOWNER
;
2760 case PROC_SELFSET_DELAYIDLESLEEP
: {
2761 /* mark or clear the process property to delay idle sleep disk IO */
2763 OSBitOrAtomic(P_DELAYIDLESLEEP
, &pself
->p_flag
);
2765 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP
), &pself
->p_flag
);
2777 #if CONFIG_MEMORYSTATUS
2780 proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t *retval
) {
2781 struct proc
*target_p
;
2783 uint32_t pcontrol
= (uint32_t)arg
;
2784 kauth_cred_t my_cred
, target_cred
;
2785 boolean_t self
= FALSE
;
2786 boolean_t child
= FALSE
;
2787 boolean_t zombref
= FALSE
;
2790 target_p
= proc_find(pid
);
2792 if (target_p
== PROC_NULL
) {
2793 if (flavor
== PROC_DIRTYCONTROL_GET
) {
2794 target_p
= proc_find_zombref(pid
);
2798 if (target_p
== PROC_NULL
)
2803 my_cred
= kauth_cred_get();
2804 target_cred
= kauth_cred_proc_ref(target_p
);
2806 /* Do we have permission to look into this? */
2807 if ((error
= proc_security_policy(target_p
, PROC_INFO_CALL_DIRTYCONTROL
, flavor
, NO_CHECK_SAME_USER
)))
2810 selfpid
= proc_selfpid();
2811 if (pid
== selfpid
) {
2813 } else if (target_p
->p_ppid
== selfpid
) {
2818 case PROC_DIRTYCONTROL_TRACK
: {
2819 /* Only allow the process itself, its parent, or root */
2820 if ((self
== FALSE
) && (child
== FALSE
) && kauth_cred_issuser(kauth_cred_get()) != TRUE
) {
2825 error
= memorystatus_dirty_track(target_p
, pcontrol
);
2829 case PROC_DIRTYCONTROL_SET
: {
2830 /* Check privileges; use cansignal() here since the process could be terminated */
2831 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
, 0)) {
2836 error
= memorystatus_dirty_set(target_p
, self
, pcontrol
);
2840 case PROC_DIRTYCONTROL_GET
: {
2841 /* No permissions check - dirty state is freely available */
2843 *retval
= memorystatus_dirty_get(target_p
);
2850 case PROC_DIRTYCONTROL_CLEAR
: {
2851 /* Check privileges; use cansignal() here since the process could be terminated */
2852 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
, 0)) {
2857 error
= memorystatus_dirty_clear(target_p
, pcontrol
);
2864 proc_drop_zombref(target_p
);
2866 proc_rele(target_p
);
2868 kauth_cred_unref(&target_cred
);
2875 proc_dirtycontrol(__unused
int pid
, __unused
int flavor
, __unused
uint64_t arg
, __unused
int32_t *retval
) {
2879 #endif /* CONFIG_MEMORYSTATUS */
2882 * proc_terminate() provides support for sudden termination.
2883 * SIGKILL is issued to tracked, clean processes; otherwise,
2888 proc_terminate(int pid
, int32_t *retval
)
2892 kauth_cred_t uc
= kauth_cred_get();
2896 /* XXX: Check if these are necessary */
2897 AUDIT_ARG(pid
, pid
);
2898 AUDIT_ARG(signum
, sig
);
2901 if (pid
<= 0 || retval
== NULL
) {
2905 if ((p
= proc_find(pid
)) == NULL
) {
2910 /* XXX: Check if these are necessary */
2911 AUDIT_ARG(process
, p
);
2914 /* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
2915 if (!cansignal(current_proc(), uc
, p
, SIGKILL
, 0)) {
2920 /* Not allowed to sudden terminate yourself */
2921 if (p
== current_proc()) {
2926 #if CONFIG_MEMORYSTATUS
2927 /* Determine requisite signal to issue */
2928 sig
= memorystatus_on_terminate(p
);
2933 proc_set_task_policy(p
->task
, TASK_POLICY_ATTRIBUTE
,
2934 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2946 * copy stat64 structure into vinfo_stat structure.
2949 munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
)
2951 bzero(vsbp
, sizeof(struct vinfo_stat
));
2953 vsbp
->vst_dev
= sbp
->st_dev
;
2954 vsbp
->vst_mode
= sbp
->st_mode
;
2955 vsbp
->vst_nlink
= sbp
->st_nlink
;
2956 vsbp
->vst_ino
= sbp
->st_ino
;
2957 vsbp
->vst_uid
= sbp
->st_uid
;
2958 vsbp
->vst_gid
= sbp
->st_gid
;
2959 vsbp
->vst_atime
= sbp
->st_atimespec
.tv_sec
;
2960 vsbp
->vst_atimensec
= sbp
->st_atimespec
.tv_nsec
;
2961 vsbp
->vst_mtime
= sbp
->st_mtimespec
.tv_sec
;
2962 vsbp
->vst_mtimensec
= sbp
->st_mtimespec
.tv_nsec
;
2963 vsbp
->vst_ctime
= sbp
->st_ctimespec
.tv_sec
;
2964 vsbp
->vst_ctimensec
= sbp
->st_ctimespec
.tv_nsec
;
2965 vsbp
->vst_birthtime
= sbp
->st_birthtimespec
.tv_sec
;
2966 vsbp
->vst_birthtimensec
= sbp
->st_birthtimespec
.tv_nsec
;
2967 vsbp
->vst_size
= sbp
->st_size
;
2968 vsbp
->vst_blocks
= sbp
->st_blocks
;
2969 vsbp
->vst_blksize
= sbp
->st_blksize
;
2970 vsbp
->vst_flags
= sbp
->st_flags
;
2971 vsbp
->vst_gen
= sbp
->st_gen
;
2972 vsbp
->vst_rdev
= sbp
->st_rdev
;
2973 vsbp
->vst_qspare
[0] = sbp
->st_qspare
[0];
2974 vsbp
->vst_qspare
[1] = sbp
->st_qspare
[1];
2978 proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, __unused
int32_t *retval
)
2984 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2985 if ((p
= proc_find_zombref(pid
)) == PROC_NULL
) {
2991 /* Do we have permission to look into this? */
2992 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDRUSAGE
, flavor
, CHECK_SAME_USER
)))
2995 error
= proc_get_rusage(p
, flavor
, buffer
, zombie
);
2999 proc_drop_zombref(p
);
3007 proc_archinfo(proc_t p
, struct proc_archinfo
*pai
)
3010 pai
->p_cputype
= p
->p_cputype
;
3011 pai
->p_cpusubtype
= p
->p_cpusubtype
;
3016 proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*ppci
)
3018 bzero(ppci
, sizeof(*ppci
));
3019 proc_coalitionids(p
, ppci
->coalition_id
);
3023 proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
)
3025 uint32_t reason_data_size
= 0;
3027 pid_t selfpid
= proc_selfpid();
3032 * One (and only one) of peri and pberi must be non-NULL.
3034 assert((peri
!= NULL
) || (pberi
!= NULL
));
3035 assert((peri
== NULL
) || (pberi
== NULL
));
3038 * Allow access to the parent of the exiting
3039 * child or the parent debugger only.
3042 if (p
->p_ppid
== selfpid
)
3043 break; /* parent => ok */
3045 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3046 (p
->p_oppid
== selfpid
))
3047 break; /* parent-in-waiting => ok */
3053 if (p
->p_exit_reason
== OS_REASON_NULL
) {
3058 if (p
->p_exit_reason
->osr_kcd_buf
!= NULL
) {
3059 reason_data_size
= kcdata_memory_get_used_bytes(&p
->p_exit_reason
->osr_kcd_descriptor
);
3063 peri
->eri_namespace
= p
->p_exit_reason
->osr_namespace
;
3064 peri
->eri_code
= p
->p_exit_reason
->osr_code
;
3065 peri
->eri_flags
= p
->p_exit_reason
->osr_flags
;
3067 if ((peri
->eri_kcd_buf
== 0) || (peri
->eri_reason_buf_size
< reason_data_size
)) {
3072 peri
->eri_reason_buf_size
= reason_data_size
;
3073 if (reason_data_size
!= 0) {
3074 error
= copyout(p
->p_exit_reason
->osr_kcd_buf
, peri
->eri_kcd_buf
, reason_data_size
);
3077 pberi
->beri_namespace
= p
->p_exit_reason
->osr_namespace
;
3078 pberi
->beri_code
= p
->p_exit_reason
->osr_code
;
3079 pberi
->beri_flags
= p
->p_exit_reason
->osr_flags
;
3080 pberi
->beri_reason_buf_size
= reason_data_size
;
3089 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
3090 * It mimics the data that is typically captured by the
3091 * EVFILT_PROC, NOTE_EXIT event mechanism.
3092 * See filt_proc() in kern_event.c.
3095 proc_pidnoteexit(proc_t p
, uint64_t flags
, uint32_t *data
)
3097 uint32_t exit_data
= 0;
3098 uint32_t exit_flags
= (uint32_t)flags
;
3103 * Allow access to the parent of the exiting
3104 * child or the parent debugger only.
3107 pid_t selfpid
= proc_selfpid();
3109 if (p
->p_ppid
== selfpid
)
3110 break; /* parent => ok */
3112 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3113 (p
->p_oppid
== selfpid
))
3114 break; /* parent-in-waiting => ok */
3120 if ((exit_flags
& NOTE_EXITSTATUS
) != 0) {
3121 /* The signal and exit status */
3122 exit_data
|= (p
->p_xstat
& NOTE_PDATAMASK
);
3125 if ((exit_flags
& NOTE_EXIT_DETAIL
) != 0) {
3126 /* The exit detail */
3127 if ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) != 0) {
3128 exit_data
|= NOTE_EXIT_DECRYPTFAIL
;
3131 if ((p
->p_lflag
& P_LTERM_JETSAM
) != 0) {
3132 exit_data
|= NOTE_EXIT_MEMORY
;
3134 switch (p
->p_lflag
& P_JETSAM_MASK
) {
3135 case P_JETSAM_VMPAGESHORTAGE
:
3136 exit_data
|= NOTE_EXIT_MEMORY_VMPAGESHORTAGE
;
3138 case P_JETSAM_VMTHRASHING
:
3139 exit_data
|= NOTE_EXIT_MEMORY_VMTHRASHING
;
3141 case P_JETSAM_FCTHRASHING
:
3142 exit_data
|= NOTE_EXIT_MEMORY_FCTHRASHING
;
3144 case P_JETSAM_VNODE
:
3145 exit_data
|= NOTE_EXIT_MEMORY_VNODE
;
3147 case P_JETSAM_HIWAT
:
3148 exit_data
|= NOTE_EXIT_MEMORY_HIWAT
;
3151 exit_data
|= NOTE_EXIT_MEMORY_PID
;
3153 case P_JETSAM_IDLEEXIT
:
3154 exit_data
|= NOTE_EXIT_MEMORY_IDLE
;
3159 if ((p
->p_csflags
& CS_KILLED
) != 0) {
3160 exit_data
|= NOTE_EXIT_CSERROR
;
3172 proc_piddynkqueueinfo(int pid
, int flavor
, kqueue_id_t kq_id
,
3173 user_addr_t ubuf
, uint32_t bufsize
, int32_t *retval
)
3178 if (ubuf
== USER_ADDR_NULL
) {
3183 if (p
== PROC_NULL
) {
3187 err
= proc_security_policy(p
, PROC_INFO_CALL_PIDDYNKQUEUEINFO
, 0, CHECK_SAME_USER
);
3193 case PROC_PIDDYNKQUEUE_INFO
:
3194 err
= kevent_copyout_dynkqinfo(p
, kq_id
, ubuf
, bufsize
, retval
);
3196 case PROC_PIDDYNKQUEUE_EXTINFO
:
3197 err
= kevent_copyout_dynkqextinfo(p
, kq_id
, ubuf
, bufsize
, retval
);