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>
70 #include <mach/vm_types.h>
72 #include <sys/mount_internal.h>
73 #include <sys/proc_info.h>
74 #include <sys/bsdtask_info.h>
75 #include <sys/kdebug.h>
76 #include <sys/sysproto.h>
77 #include <sys/msgbuf.h>
80 #include <sys/guarded.h>
82 #include <machine/machine_routines.h>
84 #include <kern/ipc_misc.h>
86 #include <vm/vm_protos.h>
88 /* Needed by proc_pidnoteexit(), proc_pidlistuptrs() */
89 #include <sys/event.h>
90 #include <sys/codesign.h>
92 /* Needed by proc_listcoalitions() */
93 #ifdef CONFIG_COALITIONS
94 #include <sys/coalition.h>
98 #include <security/mac_framework.h>
107 uint64_t get_dispatchqueue_offset_from_proc(void *);
108 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *);
109 uint64_t get_return_to_kernel_offset_from_proc(void *p
);
110 int proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
113 * TODO: Replace the noinline attribute below. Currently, it serves
114 * to avoid stack bloat caused by inlining multiple functions that
115 * have large stack footprints; when the functions are independent
116 * of each other (will not both be called in any given call to the
117 * caller), this only serves to bloat the stack, as we allocate
118 * space for both functions, despite the fact that we only need a
119 * fraction of that space.
121 * Long term, these functions should not be allocating everything on
122 * the stack, and should move large allocations (the huge structs
123 * that proc info deals in) to the heap, or eliminate them if
126 * The functions that most desperately need to improve stack usage
127 * (starting with the worst offenders):
128 * proc_pidvnodepathinfo
130 * proc_pidregionpathinfo
136 * proc_pidoriginatorinfo
139 /* protos for proc_info calls */
140 int __attribute__ ((noinline
)) proc_listpids(uint32_t type
, uint32_t tyoneinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
141 int __attribute__ ((noinline
)) proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
142 int __attribute__ ((noinline
)) proc_pidfdinfo(int pid
, int flavor
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
143 int __attribute__ ((noinline
)) proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
144 int __attribute__ ((noinline
)) proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
145 int __attribute__ ((noinline
)) proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
146 int __attribute__ ((noinline
)) proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t * retval
);
147 int __attribute__ ((noinline
)) proc_terminate(int pid
, int32_t * retval
);
148 int __attribute__ ((noinline
)) proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, int32_t * retval
);
149 int __attribute__ ((noinline
)) proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
150 int __attribute__ ((noinline
)) proc_listcoalitions(int flavor
, int coaltype
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
151 int __attribute__ ((noinline
)) proc_can_use_foreground_hw(int pid
, user_addr_t reason
, uint32_t resonsize
, int32_t *retval
);
153 /* protos for procpidinfo calls */
154 int __attribute__ ((noinline
)) proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
155 int __attribute__ ((noinline
)) proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
*pbsd
, int zombie
);
156 int __attribute__ ((noinline
)) proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
*pbsd_shortp
, int zombie
);
157 int __attribute__ ((noinline
)) proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
*ptinfo
);
158 int __attribute__ ((noinline
)) proc_pidthreadinfo(proc_t p
, uint64_t arg
, bool thuniqueid
, struct proc_threadinfo
*pthinfo
);
159 int __attribute__ ((noinline
)) proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
);
160 int __attribute__ ((noinline
)) proc_pidlistthreads(proc_t p
, bool thuniqueid
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
161 int __attribute__ ((noinline
)) proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
162 int __attribute__ ((noinline
)) proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
163 int __attribute__ ((noinline
)) proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
164 int __attribute__ ((noinline
)) proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
165 int __attribute__ ((noinline
)) proc_pidvnodepathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
166 int __attribute__ ((noinline
)) proc_pidpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
167 int __attribute__ ((noinline
)) proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
);
168 int __attribute__ ((noinline
)) proc_pidfileportlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
169 void __attribute__ ((noinline
)) proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
170 void __attribute__ ((noinline
)) proc_archinfo(proc_t p
, struct proc_archinfo
*pai
);
171 void __attribute__ ((noinline
)) proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*pci
);
172 int __attribute__ ((noinline
)) proc_pidnoteexit(proc_t p
, uint64_t arg
, uint32_t *data
);
173 int __attribute__ ((noinline
)) proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
);
174 int __attribute__ ((noinline
)) proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
);
175 int __attribute__ ((noinline
)) proc_pidlistuptrs(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
176 int __attribute__ ((noinline
)) proc_piddynkqueueinfo(pid_t pid
, int flavor
, kqueue_id_t id
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
179 int __attribute__ ((noinline
)) proc_udata_info(pid_t pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
182 /* protos for proc_pidfdinfo calls */
183 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
);
184 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
);
185 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
);
186 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
);
187 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
);
188 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
);
189 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
);
190 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
);
193 /* protos for misc */
195 int fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
);
196 void fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* finfo
);
197 int proc_security_policy(proc_t targetp
, int callnum
, int flavor
, boolean_t check_same_user
);
198 static void munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
);
199 static int proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
);
200 int proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
);
202 extern int cansignal(struct proc
*, kauth_cred_t
, struct proc
*, int);
203 extern int proc_get_rusage(proc_t proc
, int flavor
, user_addr_t buffer
, int is_zombie
);
205 #define CHECK_SAME_USER TRUE
206 #define NO_CHECK_SAME_USER FALSE
209 get_dispatchqueue_offset_from_proc(void *p
)
212 proc_t pself
= (proc_t
)p
;
213 return pself
->p_dispatchqueue_offset
;
220 get_dispatchqueue_serialno_offset_from_proc(void *p
)
223 proc_t pself
= (proc_t
)p
;
224 return pself
->p_dispatchqueue_serialno_offset
;
231 get_return_to_kernel_offset_from_proc(void *p
)
234 proc_t pself
= (proc_t
)p
;
235 return pself
->p_return_to_kernel_offset
;
241 /***************************** proc_info ********************/
244 proc_info(__unused
struct proc
*p
, struct proc_info_args
* uap
, int32_t *retval
)
246 return proc_info_internal(uap
->callnum
, uap
->pid
, uap
->flavor
, uap
->arg
, uap
->buffer
, uap
->buffersize
, retval
);
251 proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
254 case PROC_INFO_CALL_LISTPIDS
:
255 /* pid contains type and flavor contains typeinfo */
256 return proc_listpids(pid
, flavor
, buffer
, buffersize
, retval
);
257 case PROC_INFO_CALL_PIDINFO
:
258 return proc_pidinfo(pid
, flavor
, arg
, buffer
, buffersize
, retval
);
259 case PROC_INFO_CALL_PIDFDINFO
:
260 return proc_pidfdinfo(pid
, flavor
, (int)arg
, buffer
, buffersize
, retval
);
261 case PROC_INFO_CALL_KERNMSGBUF
:
262 return proc_kernmsgbuf(buffer
, buffersize
, retval
);
263 case PROC_INFO_CALL_SETCONTROL
:
264 return proc_setcontrol(pid
, flavor
, arg
, buffer
, buffersize
, retval
);
265 case PROC_INFO_CALL_PIDFILEPORTINFO
:
266 return proc_pidfileportinfo(pid
, flavor
, (mach_port_name_t
)arg
, buffer
, buffersize
, retval
);
267 case PROC_INFO_CALL_TERMINATE
:
268 return proc_terminate(pid
, retval
);
269 case PROC_INFO_CALL_DIRTYCONTROL
:
270 return proc_dirtycontrol(pid
, flavor
, arg
, retval
);
271 case PROC_INFO_CALL_PIDRUSAGE
:
272 return proc_pid_rusage(pid
, flavor
, buffer
, retval
);
273 case PROC_INFO_CALL_PIDORIGINATORINFO
:
274 return proc_pidoriginatorinfo(pid
, flavor
, buffer
, buffersize
, retval
);
275 case PROC_INFO_CALL_LISTCOALITIONS
:
276 return proc_listcoalitions(pid
/* flavor */, flavor
/* coaltype */, buffer
,
278 case PROC_INFO_CALL_CANUSEFGHW
:
279 return proc_can_use_foreground_hw(pid
, buffer
, buffersize
, retval
);
280 case PROC_INFO_CALL_PIDDYNKQUEUEINFO
:
281 return proc_piddynkqueueinfo(pid
, flavor
, (kqueue_id_t
)arg
, buffer
, buffersize
, retval
);
283 case PROC_INFO_CALL_UDATA_INFO
:
284 return proc_udata_info(pid
, flavor
, buffer
, buffersize
, retval
);
285 #endif /* !CONFIG_EMBEDDED */
293 /******************* proc_listpids routine ****************/
295 proc_listpids(uint32_t type
, uint32_t typeinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
297 uint32_t numprocs
= 0;
306 struct proclist
*current_list
;
308 /* Do we have permission to look into this? */
309 if ((error
= proc_security_policy(PROC_NULL
, PROC_INFO_CALL_LISTPIDS
, type
, NO_CHECK_SAME_USER
))) {
313 /* if the buffer is null, return num of procs */
314 if (buffer
== (user_addr_t
)0) {
315 *retval
= ((nprocs
+ 20) * sizeof(int));
319 if (buffersize
< sizeof(int)) {
322 wantpids
= buffersize
/ sizeof(int);
323 if ((nprocs
+ 20) > 0) {
324 numprocs
= (uint32_t)(nprocs
+ 20);
326 if (numprocs
> wantpids
) {
330 kbuf
= (char *)kalloc((vm_size_t
)(numprocs
* sizeof(int)));
334 bzero(kbuf
, numprocs
* sizeof(int));
340 current_list
= &allproc
;
342 LIST_FOREACH(p
, current_list
, p_list
) {
346 if (p
->p_pgrpid
!= (pid_t
)typeinfo
) {
351 if ((p
->p_ppid
!= (pid_t
)typeinfo
) && (((p
->p_lflag
& P_LTRACED
) == 0) || (p
->p_oppid
!= (pid_t
)typeinfo
))) {
360 /* racy but list lock is held */
361 if ((p
->p_flag
& P_CONTROLT
) == 0 ||
362 (p
->p_pgrp
== NULL
) || (p
->p_pgrp
->pg_session
== NULL
) ||
363 (tp
= SESSION_TP(p
->p_pgrp
->pg_session
)) == TTY_NULL
||
364 tp
->t_dev
!= (dev_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_getuid(my_cred
);
377 kauth_cred_unref(&my_cred
);
378 if (uid
!= (uid_t
)typeinfo
) {
384 if (p
->p_ucred
== NULL
) {
387 kauth_cred_t my_cred
;
390 my_cred
= kauth_cred_proc_ref(p
);
391 uid
= kauth_cred_getruid(my_cred
);
392 kauth_cred_unref(&my_cred
);
393 if (uid
!= (uid_t
)typeinfo
) {
399 if (p
->p_kdebug
== 0) {
418 if ((n
< numprocs
) && (current_list
== &allproc
)) {
419 current_list
= &zombproc
;
426 error
= copyout((caddr_t
)ptr
, buffer
, n
* sizeof(int));
428 *retval
= (n
* sizeof(int));
430 kfree(kbuf
, (vm_size_t
)(numprocs
* sizeof(int)));
436 /********************************** proc_pidfdlist routines ********************************/
439 proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
444 struct proc_fdinfo
* pfd
;
445 struct fileproc
* fp
;
450 if (p
->p_fd
->fd_nfiles
> 0) {
451 numfds
= (uint32_t)p
->p_fd
->fd_nfiles
;
454 if (buffer
== (user_addr_t
) 0) {
456 *retval
= (numfds
* sizeof(struct proc_fdinfo
));
460 /* buffersize is big enough atleast for one struct */
461 needfds
= buffersize
/ sizeof(struct proc_fdinfo
);
463 if (numfds
> needfds
) {
467 kbuf
= (char *)kalloc((vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
471 bzero(kbuf
, numfds
* sizeof(struct proc_fdinfo
));
475 pfd
= (struct proc_fdinfo
*)kbuf
;
477 for (n
= 0; ((n
< (int)numfds
) && (n
< p
->p_fd
->fd_nfiles
)); n
++) {
478 if (((fp
= p
->p_fd
->fd_ofiles
[n
]) != 0)
479 && ((p
->p_fd
->fd_ofileflags
[n
] & UF_RESERVED
) == 0)) {
480 file_type_t fdtype
= FILEGLOB_DTYPE(fp
->f_fglob
);
482 pfd
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
483 fdtype
: PROX_FDTYPE_ATALK
;
490 error
= copyout(kbuf
, buffer
, count
* sizeof(struct proc_fdinfo
));
491 kfree(kbuf
, (vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
493 *retval
= (count
* sizeof(struct proc_fdinfo
));
499 * Helper functions for proc_pidfileportlist.
502 proc_fileport_count(__unused mach_port_name_t name
,
503 __unused
struct fileglob
*fg
, void *arg
)
505 uint32_t *counter
= arg
;
511 struct fileport_fdtype_args
{
512 struct proc_fileportinfo
*ffa_pfi
;
513 struct proc_fileportinfo
*ffa_pfi_end
;
517 proc_fileport_fdtype(mach_port_name_t name
, struct fileglob
*fg
, void *arg
)
519 struct fileport_fdtype_args
*ffa
= arg
;
521 if (ffa
->ffa_pfi
!= ffa
->ffa_pfi_end
) {
522 file_type_t fdtype
= FILEGLOB_DTYPE(fg
);
524 ffa
->ffa_pfi
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
525 fdtype
: PROX_FDTYPE_ATALK
;
526 ffa
->ffa_pfi
->proc_fileport
= name
;
528 return 0; /* keep walking */
530 return -1; /* stop the walk! */
535 proc_pidfileportlist(proc_t p
,
536 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
540 struct proc_fileportinfo
*pfi
;
541 uint32_t needfileports
, numfileports
;
542 struct fileport_fdtype_args ffa
;
545 needfileports
= buffersize
/ sizeof(*pfi
);
546 if ((user_addr_t
)0 == buffer
|| needfileports
> (uint32_t)maxfiles
) {
548 * Either (i) the user is asking for a fileport count,
549 * or (ii) the number of fileports they're asking for is
550 * larger than the maximum number of open files (!); count
551 * them to bound subsequent heap allocations.
554 switch (fileport_walk(p
->task
,
555 proc_fileport_count
, &numfileports
)) {
558 case KERN_RESOURCE_SHORTAGE
:
560 case KERN_INVALID_TASK
:
566 if (numfileports
== 0) {
567 *retval
= 0; /* none at all, bail */
570 if ((user_addr_t
)0 == buffer
) {
571 numfileports
+= 20; /* accelerate convergence */
572 *retval
= numfileports
* sizeof(*pfi
);
575 if (needfileports
> numfileports
) {
576 needfileports
= numfileports
;
580 assert(buffersize
>= PROC_PIDLISTFILEPORTS_SIZE
);
582 kbufsize
= (vm_size_t
)needfileports
* sizeof(*pfi
);
583 pfi
= kbuf
= kalloc(kbufsize
);
587 bzero(kbuf
, kbufsize
);
590 ffa
.ffa_pfi_end
= pfi
+ needfileports
;
592 switch (fileport_walk(p
->task
, proc_fileport_fdtype
, &ffa
)) {
596 if ((numfileports
= pfi
- (typeof(pfi
))kbuf
) == 0) {
599 if (numfileports
> needfileports
) {
600 panic("more fileports returned than requested");
602 error
= copyout(kbuf
, buffer
, numfileports
* sizeof(*pfi
));
604 case KERN_RESOURCE_SHORTAGE
:
607 case KERN_INVALID_TASK
:
614 kfree(kbuf
, kbufsize
);
616 *retval
= numfileports
* sizeof(*pfi
);
622 proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
* pbsd
, int zombie
)
625 struct session
*sessionp
= NULL
;
627 kauth_cred_t my_cred
;
630 sessionp
= proc_session(p
);
632 my_cred
= kauth_cred_proc_ref(p
);
633 bzero(pbsd
, sizeof(struct proc_bsdinfo
));
634 pbsd
->pbi_status
= p
->p_stat
;
635 pbsd
->pbi_xstatus
= p
->p_xstat
;
636 pbsd
->pbi_pid
= p
->p_pid
;
637 pbsd
->pbi_ppid
= p
->p_ppid
;
638 pbsd
->pbi_uid
= kauth_cred_getuid(my_cred
);
639 pbsd
->pbi_gid
= kauth_cred_getgid(my_cred
);
640 pbsd
->pbi_ruid
= kauth_cred_getruid(my_cred
);
641 pbsd
->pbi_rgid
= kauth_cred_getrgid(my_cred
);
642 pbsd
->pbi_svuid
= kauth_cred_getsvuid(my_cred
);
643 pbsd
->pbi_svgid
= kauth_cred_getsvgid(my_cred
);
644 kauth_cred_unref(&my_cred
);
646 pbsd
->pbi_nice
= p
->p_nice
;
647 pbsd
->pbi_start_tvsec
= p
->p_start
.tv_sec
;
648 pbsd
->pbi_start_tvusec
= p
->p_start
.tv_usec
;
649 bcopy(&p
->p_comm
, &pbsd
->pbi_comm
[0], MAXCOMLEN
);
650 pbsd
->pbi_comm
[MAXCOMLEN
- 1] = '\0';
651 bcopy(&p
->p_name
, &pbsd
->pbi_name
[0], 2 * MAXCOMLEN
);
652 pbsd
->pbi_name
[(2 * MAXCOMLEN
) - 1] = '\0';
655 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
) {
656 pbsd
->pbi_flags
|= PROC_FLAG_SYSTEM
;
658 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
) {
659 pbsd
->pbi_flags
|= PROC_FLAG_TRACED
;
661 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
) {
662 pbsd
->pbi_flags
|= PROC_FLAG_INEXIT
;
664 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
) {
665 pbsd
->pbi_flags
|= PROC_FLAG_PPWAIT
;
667 if ((p
->p_flag
& P_LP64
) == P_LP64
) {
668 pbsd
->pbi_flags
|= PROC_FLAG_LP64
;
670 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
) {
671 pbsd
->pbi_flags
|= PROC_FLAG_CONTROLT
;
673 if ((p
->p_flag
& P_THCWD
) == P_THCWD
) {
674 pbsd
->pbi_flags
|= PROC_FLAG_THCWD
;
676 if ((p
->p_flag
& P_SUGID
) == P_SUGID
) {
677 pbsd
->pbi_flags
|= PROC_FLAG_PSUGID
;
679 if ((p
->p_flag
& P_EXEC
) == P_EXEC
) {
680 pbsd
->pbi_flags
|= PROC_FLAG_EXEC
;
683 if (sessionp
!= SESSION_NULL
) {
684 if (SESS_LEADER(p
, sessionp
)) {
685 pbsd
->pbi_flags
|= PROC_FLAG_SLEADER
;
687 if (sessionp
->s_ttyvp
) {
688 pbsd
->pbi_flags
|= PROC_FLAG_CTTY
;
693 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
) {
694 pbsd
->pbi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
696 #endif /* !CONFIG_EMBEDDED */
698 switch (PROC_CONTROL_STATE(p
)) {
700 pbsd
->pbi_flags
|= PROC_FLAG_PC_THROTTLE
;
703 pbsd
->pbi_flags
|= PROC_FLAG_PC_SUSP
;
706 pbsd
->pbi_flags
|= PROC_FLAG_PC_KILL
;
711 switch (PROC_ACTION_STATE(p
)) {
713 pbsd
->pbi_flags
|= PROC_FLAG_PA_THROTTLE
;
716 pbsd
->pbi_flags
|= PROC_FLAG_PA_SUSP
;
721 /* if process is a zombie skip bg state */
722 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
)) {
723 proc_get_darwinbgstate(p
->task
, &pbsd
->pbi_flags
);
727 pbsd
->pbi_nfiles
= p
->p_fd
->fd_nfiles
;
730 pbsd
->e_tdev
= NODEV
;
731 if (pg
!= PGRP_NULL
) {
732 pbsd
->pbi_pgid
= p
->p_pgrpid
;
733 pbsd
->pbi_pjobc
= pg
->pg_jobc
;
734 if ((p
->p_flag
& P_CONTROLT
) && (sessionp
!= SESSION_NULL
) && (tp
= SESSION_TP(sessionp
))) {
735 pbsd
->e_tdev
= tp
->t_dev
;
736 pbsd
->e_tpgid
= sessionp
->s_ttypgrpid
;
739 if (sessionp
!= SESSION_NULL
) {
740 session_rele(sessionp
);
742 if (pg
!= PGRP_NULL
) {
751 proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
* pbsd_shortp
, int zombie
)
753 bzero(pbsd_shortp
, sizeof(struct proc_bsdshortinfo
));
754 pbsd_shortp
->pbsi_pid
= p
->p_pid
;
755 pbsd_shortp
->pbsi_ppid
= p
->p_ppid
;
756 pbsd_shortp
->pbsi_pgid
= p
->p_pgrpid
;
757 pbsd_shortp
->pbsi_status
= p
->p_stat
;
758 bcopy(&p
->p_comm
, &pbsd_shortp
->pbsi_comm
[0], MAXCOMLEN
);
759 pbsd_shortp
->pbsi_comm
[MAXCOMLEN
- 1] = '\0';
761 pbsd_shortp
->pbsi_flags
= 0;
762 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
) {
763 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_SYSTEM
;
765 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
) {
766 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_TRACED
;
768 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
) {
769 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_INEXIT
;
771 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
) {
772 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PPWAIT
;
774 if ((p
->p_flag
& P_LP64
) == P_LP64
) {
775 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_LP64
;
777 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
) {
778 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_CONTROLT
;
780 if ((p
->p_flag
& P_THCWD
) == P_THCWD
) {
781 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_THCWD
;
783 if ((p
->p_flag
& P_SUGID
) == P_SUGID
) {
784 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PSUGID
;
786 if ((p
->p_flag
& P_EXEC
) == P_EXEC
) {
787 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_EXEC
;
790 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
) {
791 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
793 #endif /* !CONFIG_EMBEDDED */
795 switch (PROC_CONTROL_STATE(p
)) {
797 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_THROTTLE
;
800 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_SUSP
;
803 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_KILL
;
808 switch (PROC_ACTION_STATE(p
)) {
810 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_THROTTLE
;
813 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_SUSP
;
818 /* if process is a zombie skip bg state */
819 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
)) {
820 proc_get_darwinbgstate(p
->task
, &pbsd_shortp
->pbsi_flags
);
823 pbsd_shortp
->pbsi_uid
= p
->p_uid
;
824 pbsd_shortp
->pbsi_gid
= p
->p_gid
;
825 pbsd_shortp
->pbsi_ruid
= p
->p_ruid
;
826 pbsd_shortp
->pbsi_rgid
= p
->p_rgid
;
827 pbsd_shortp
->pbsi_svuid
= p
->p_svuid
;
828 pbsd_shortp
->pbsi_svgid
= p
->p_svgid
;
834 proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
* ptinfo
)
840 bzero(ptinfo
, sizeof(struct proc_taskinfo
));
841 fill_taskprocinfo(task
, (struct proc_taskinfo_internal
*)ptinfo
);
849 proc_pidthreadinfo(proc_t p
, uint64_t arg
, bool thuniqueid
, struct proc_threadinfo
*pthinfo
)
852 uint64_t threadaddr
= (uint64_t)arg
;
854 bzero(pthinfo
, sizeof(struct proc_threadinfo
));
856 error
= fill_taskthreadinfo(p
->task
, threadaddr
, thuniqueid
, (struct proc_threadinfo_internal
*)pthinfo
, NULL
, NULL
);
865 bsd_hasthreadname(void *uth
)
867 struct uthread
*ut
= (struct uthread
*)uth
;
869 /* This doesn't check for the empty string; do we care? */
878 bsd_getthreadname(void *uth
, char *buffer
)
880 struct uthread
*ut
= (struct uthread
*)uth
;
882 bcopy(ut
->pth_name
, buffer
, MAXTHREADNAMESIZE
);
887 * This is known to race with regards to the contents of the thread name; concurrent
888 * callers may result in a garbled name.
891 bsd_setthreadname(void *uth
, const char *name
)
893 struct uthread
*ut
= (struct uthread
*)uth
;
894 char * name_buf
= NULL
;
897 /* If there is no existing thread name, allocate a buffer for one. */
898 name_buf
= kalloc(MAXTHREADNAMESIZE
);
900 bzero(name_buf
, MAXTHREADNAMESIZE
);
902 /* Someone could conceivably have named the thread at the same time we did. */
903 if (!OSCompareAndSwapPtr(NULL
, name_buf
, &ut
->pth_name
)) {
904 kfree(name_buf
, MAXTHREADNAMESIZE
);
907 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV
, ut
->pth_name
);
910 strncpy(ut
->pth_name
, name
, MAXTHREADNAMESIZE
- 1);
911 kernel_debug_string_simple(TRACE_STRING_THREADNAME
, ut
->pth_name
);
915 bsd_copythreadname(void *dst_uth
, void *src_uth
)
917 struct uthread
*dst_ut
= (struct uthread
*)dst_uth
;
918 struct uthread
*src_ut
= (struct uthread
*)src_uth
;
920 if (src_ut
->pth_name
== NULL
) {
924 if (dst_ut
->pth_name
== NULL
) {
925 dst_ut
->pth_name
= (char *)kalloc(MAXTHREADNAMESIZE
);
926 if (dst_ut
->pth_name
== NULL
) {
931 bcopy(src_ut
->pth_name
, dst_ut
->pth_name
, MAXTHREADNAMESIZE
);
936 bsd_threadcdir(void * uth
, void *vptr
, int *vidp
)
938 struct uthread
* ut
= (struct uthread
*)uth
;
940 vnode_t
*vpp
= (vnode_t
*)vptr
;
955 proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
)
960 uint64_t threadaddr
= (uint64_t)arg
;
963 bzero(pinfo
, sizeof(struct proc_threadwithpathinfo
));
965 error
= fill_taskthreadinfo(p
->task
, threadaddr
, 0, (struct proc_threadinfo_internal
*)&pinfo
->pt
, (void *)&vp
, &vid
);
970 if ((vp
!= NULLVP
) && ((vnode_getwithvid(vp
, vid
)) == 0)) {
971 error
= fill_vnodeinfo(vp
, &pinfo
->pvip
.vip_vi
);
974 vn_getpath(vp
, &pinfo
->pvip
.vip_path
[0], &count
);
975 pinfo
->pvip
.vip_path
[MAXPATHLEN
- 1] = 0;
985 proc_pidlistthreads(proc_t p
, bool thuniqueid
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
991 uint32_t numthreads
= 0;
993 int num
= get_numthreads(p
->task
) + 10;
995 numthreads
= (uint32_t)num
;
998 count
= buffersize
/ (sizeof(uint64_t));
1000 if (numthreads
> count
) {
1004 kbuf
= (void *)kalloc(numthreads
* sizeof(uint64_t));
1008 bzero(kbuf
, numthreads
* sizeof(uint64_t));
1010 ret
= fill_taskthreadlist(p
->task
, kbuf
, numthreads
, thuniqueid
);
1012 error
= copyout(kbuf
, buffer
, ret
);
1013 kfree(kbuf
, numthreads
* sizeof(uint64_t));
1022 proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1024 struct proc_regioninfo preginfo
;
1027 bzero(&preginfo
, sizeof(struct proc_regioninfo
));
1028 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
, (uintptr_t *)0, (uint32_t *)0);
1032 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regioninfo
));
1034 *retval
= sizeof(struct proc_regioninfo
);
1041 proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1043 struct proc_regionwithpathinfo preginfo
;
1045 uintptr_t vnodeaddr
= 0;
1046 uint32_t vnodeid
= 0;
1050 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1052 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1057 vp
= (vnode_t
)vnodeaddr
;
1058 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1059 /* FILL THE VNODEINFO */
1060 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1062 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1063 /* Always make sure it is null terminated */
1064 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
- 1] = 0;
1068 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1070 *retval
= sizeof(struct proc_regionwithpathinfo
);
1076 proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1078 struct proc_regionwithpathinfo preginfo
;
1080 uintptr_t vnodeaddr
= 0;
1081 uint32_t vnodeid
= 0;
1085 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1087 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1095 vp
= (vnode_t
)vnodeaddr
;
1096 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1097 /* FILL THE VNODEINFO */
1098 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1100 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1101 /* Always make sure it is null terminated */
1102 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
- 1] = 0;
1108 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1110 *retval
= sizeof(struct proc_regionwithpathinfo
);
1116 proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1118 struct proc_regionwithpathinfo preginfo
;
1120 uintptr_t vnodeaddr
;
1126 /* Loop while looking for vnodes that match dev_t filter */
1128 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1132 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, addr
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1140 vp
= (vnode_t
)vnodeaddr
;
1141 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1142 /* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
1143 struct vnode_attr va
;
1145 memset(&va
, 0, sizeof(va
));
1147 VATTR_WANTED(&va
, va_fsid
);
1148 VATTR_WANTED(&va
, va_fsid64
);
1150 ret
= vnode_getattr(vp
, &va
, vfs_context_current());
1156 if (vnode_get_va_fsid(&va
) == arg
) {
1157 /* FILL THE VNODEINFO */
1158 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1160 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1161 /* Always make sure it is null terminated */
1162 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
- 1] = 0;
1171 addr
= preginfo
.prp_prinfo
.pri_address
+ preginfo
.prp_prinfo
.pri_size
;
1174 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1176 *retval
= sizeof(struct proc_regionwithpathinfo
);
1182 * Path is relative to current process directory; may different from current
1186 proc_pidvnodepathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1188 struct proc_vnodepathinfo pvninfo
;
1190 vnode_t vncdirvp
= NULLVP
;
1191 uint32_t vncdirid
= 0;
1192 vnode_t vnrdirvp
= NULLVP
;
1193 uint32_t vnrdirid
= 0;
1196 bzero(&pvninfo
, sizeof(struct proc_vnodepathinfo
));
1199 if (p
->p_fd
->fd_cdir
) {
1200 vncdirvp
= p
->p_fd
->fd_cdir
;
1201 vncdirid
= p
->p_fd
->fd_cdir
->v_id
;
1203 if (p
->p_fd
->fd_rdir
) {
1204 vnrdirvp
= p
->p_fd
->fd_rdir
;
1205 vnrdirid
= p
->p_fd
->fd_rdir
->v_id
;
1209 if (vncdirvp
!= NULLVP
) {
1210 if ((error
= vnode_getwithvid(vncdirvp
, vncdirid
)) == 0) {
1211 /* FILL THE VNODEINFO */
1212 error
= fill_vnodeinfo(vncdirvp
, &pvninfo
.pvi_cdir
.vip_vi
);
1215 vn_getpath(vncdirvp
, &pvninfo
.pvi_cdir
.vip_path
[0], &count
);
1216 pvninfo
.pvi_cdir
.vip_path
[MAXPATHLEN
- 1] = 0;
1218 vnode_put(vncdirvp
);
1224 if ((error
== 0) && (vnrdirvp
!= NULLVP
)) {
1225 if ((error
= vnode_getwithvid(vnrdirvp
, vnrdirid
)) == 0) {
1226 /* FILL THE VNODEINFO */
1227 error
= fill_vnodeinfo(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_vi
);
1230 vn_getpath(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_path
[0], &count
);
1231 pvninfo
.pvi_rdir
.vip_path
[MAXPATHLEN
- 1] = 0;
1233 vnode_put(vnrdirvp
);
1239 error
= copyout(&pvninfo
, buffer
, sizeof(struct proc_vnodepathinfo
));
1241 *retval
= sizeof(struct proc_vnodepathinfo
);
1249 proc_pidpathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t *retval
)
1253 int len
= buffersize
;
1258 if (tvp
== NULLVP
) {
1262 buf
= (char *)kalloc(buffersize
);
1267 bzero(buf
, buffersize
);
1269 error
= proc_pidpathinfo_internal(p
, arg
, buf
, buffersize
, retval
);
1271 error
= copyout(buf
, buffer
, len
);
1273 kfree(buf
, buffersize
);
1278 proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
)
1282 vnode_t nvp
= NULLVP
;
1283 int len
= buffersize
;
1287 if (tvp
== NULLVP
) {
1291 vid
= vnode_vid(tvp
);
1292 error
= vnode_getwithvid(tvp
, vid
);
1294 error
= vn_getpath_fsenter(tvp
, buf
, &len
);
1297 error
= vnode_lookup(buf
, 0, &nvp
, vfs_context_current());
1298 if ((error
== 0) && (nvp
!= NULLVP
)) {
1308 proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
)
1312 bzero(pwqinfo
, sizeof(struct proc_workqueueinfo
));
1314 error
= fill_procworkqueue(p
, pwqinfo
);
1324 proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
)
1326 p_uniqidinfo
->p_uniqueid
= proc_uniqueid(p
);
1327 proc_getexecutableuuid(p
, (unsigned char *)&p_uniqidinfo
->p_uuid
, sizeof(p_uniqidinfo
->p_uuid
));
1328 p_uniqidinfo
->p_puniqueid
= proc_puniqueid(p
);
1329 p_uniqidinfo
->p_reserve2
= 0;
1330 p_uniqidinfo
->p_reserve3
= 0;
1331 p_uniqidinfo
->p_reserve4
= 0;
1336 proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
)
1338 struct proc
* p
= PROC_NULL
;
1341 if (buffersize
< sizeof(uuid_t
)) {
1345 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1346 p
= proc_find_zombref(pid
);
1349 if (p
== PROC_NULL
) {
1353 proc_getexecutableuuid(p
, (unsigned char *)uuid_buf
, buffersize
);
1356 proc_drop_zombref(p
);
1365 * Function to get the uuid and pid of the originator of the voucher.
1368 proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
)
1370 pid_t originator_pid
;
1375 * Get the current voucher origin pid. The pid returned here
1376 * might not be valid or may have been recycled.
1378 kr
= thread_get_current_voucher_origin_pid(&originator_pid
);
1379 /* If errors, convert errors to appropriate format */
1381 if (kr
== KERN_INVALID_TASK
) {
1383 } else if (kr
== KERN_INVALID_VALUE
) {
1391 *pid
= originator_pid
;
1392 error
= proc_piduuidinfo(originator_pid
, uuid
, buffersize
);
1397 * Function to get the uuid of the originator of the voucher.
1400 proc_pidoriginatoruuid(uuid_t uuid
, uint32_t buffersize
)
1402 pid_t originator_pid
;
1403 return proc_pidoriginatorpid_uuid(uuid
, buffersize
, &originator_pid
);
1406 /***************************** proc_pidoriginatorinfo ***************************/
1409 proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1411 int error
= ENOTSUP
;
1415 case PROC_PIDORIGINATOR_UUID
:
1416 size
= PROC_PIDORIGINATOR_UUID_SIZE
;
1418 case PROC_PIDORIGINATOR_BGSTATE
:
1419 size
= PROC_PIDORIGINATOR_BGSTATE_SIZE
;
1421 case PROC_PIDORIGINATOR_PID_UUID
:
1422 size
= PROC_PIDORIGINATOR_PID_UUID_SIZE
;
1428 if (buffersize
< size
) {
1432 if (pid
!= 0 && pid
!= proc_selfpid()) {
1437 case PROC_PIDORIGINATOR_UUID
: {
1440 error
= proc_pidoriginatoruuid(uuid
, sizeof(uuid
));
1445 error
= copyout(uuid
, buffer
, size
);
1452 case PROC_PIDORIGINATOR_PID_UUID
: {
1453 struct proc_originatorinfo originator_info
;
1454 bzero(&originator_info
, sizeof(originator_info
));
1456 error
= proc_pidoriginatorpid_uuid(originator_info
.originator_uuid
,
1457 sizeof(uuid_t
), &originator_info
.originator_pid
);
1462 error
= copyout(&originator_info
, buffer
, size
);
1469 case PROC_PIDORIGINATOR_BGSTATE
: {
1470 uint32_t is_backgrounded
= 0;
1471 error
= proc_get_originatorbgstate(&is_backgrounded
);
1476 error
= copyout(&is_backgrounded
, buffer
, size
);
1490 /***************************** proc_listcoalitions ***************************/
1492 proc_listcoalitions(int flavor
, int type
, user_addr_t buffer
,
1493 uint32_t buffersize
, int32_t *retval
)
1495 #if CONFIG_COALITIONS
1496 int error
= ENOTSUP
;
1499 void *coalinfo
= NULL
;
1500 uint32_t k_buffersize
= 0, copyout_sz
= 0;
1501 int ncoals
= 0, ncoals_
= 0;
1503 /* struct procinfo_coalinfo; */
1506 case LISTCOALITIONS_ALL_COALS
:
1507 elem_size
= LISTCOALITIONS_ALL_COALS_SIZE
;
1510 case LISTCOALITIONS_SINGLE_TYPE
:
1511 elem_size
= LISTCOALITIONS_SINGLE_TYPE_SIZE
;
1518 /* find the total number of coalitions */
1519 ncoals
= coalitions_get_list(coal_type
, NULL
, 0);
1521 if (ncoals
== 0 || buffer
== 0 || buffersize
== 0) {
1523 * user just wants buffer size
1524 * or there are no coalitions
1527 *retval
= (int)(ncoals
* elem_size
);
1531 k_buffersize
= ncoals
* elem_size
;
1532 coalinfo
= kalloc((vm_size_t
)k_buffersize
);
1537 bzero(coalinfo
, k_buffersize
);
1540 case LISTCOALITIONS_ALL_COALS
:
1541 case LISTCOALITIONS_SINGLE_TYPE
:
1542 ncoals_
= coalitions_get_list(coal_type
, coalinfo
, ncoals
);
1545 panic("memory corruption?!");
1549 /* all the coalitions disappeared... weird but valid */
1556 * Some coalitions may have disappeared between our initial check,
1557 * and the the actual list acquisition.
1558 * Only copy out what we really need.
1560 copyout_sz
= k_buffersize
;
1561 if (ncoals_
< ncoals
) {
1562 copyout_sz
= ncoals_
* elem_size
;
1566 * copy the list up to user space
1567 * (we're guaranteed to have a non-null pointer/size here)
1569 error
= copyout(coalinfo
, buffer
,
1570 copyout_sz
< buffersize
? copyout_sz
: buffersize
);
1573 *retval
= (int)copyout_sz
;
1578 kfree(coalinfo
, k_buffersize
);
1583 /* no coalition support */
1594 /*************************** proc_can_use_forgeound_hw **************************/
1596 proc_can_use_foreground_hw(int pid
, user_addr_t u_reason
, uint32_t reasonsize
, int32_t *retval
)
1598 proc_t p
= PROC_NULL
;
1600 uint32_t reason
= PROC_FGHW_ERROR
;
1602 task_t task
= TASK_NULL
;
1603 #if CONFIG_COALITIONS
1604 coalition_t coal
= COALITION_NULL
;
1611 reason
= PROC_FGHW_ERROR
;
1616 if (p
== PROC_NULL
) {
1618 reason
= PROC_FGHW_ERROR
;
1622 #if CONFIG_COALITIONS
1623 if (p
!= current_proc() &&
1624 !kauth_cred_issuser(kauth_cred_get())) {
1626 reason
= PROC_FGHW_ERROR
;
1631 task_reference(task
);
1632 if (coalition_is_leader(task
, COALITION_TYPE_JETSAM
, &coal
) == FALSE
) {
1633 /* current task is not a coalition leader: find the leader */
1634 task_deallocate(task
);
1635 task
= coalition_get_leader(coal
);
1638 if (task
!= TASK_NULL
) {
1640 * If task is non-null, then it is the coalition leader of the
1641 * current process' coalition. This could be the same task as
1642 * the current_task, and that's OK.
1647 proc_get_darwinbgstate(task
, &flags
);
1648 if ((flags
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
1650 * Coalition leader is not an application, continue
1651 * searching for other ways this task could gain
1654 reason
= PROC_FGHW_DAEMON_LEADER
;
1658 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
)) {
1660 * If the leader of the current process' coalition has
1661 * been marked as DARWIN_BG, then it definitely should
1662 * not be using foreground hardware resources.
1664 reason
= PROC_FGHW_LEADER_BACKGROUND
;
1668 role
= proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
);
1670 case TASK_FOREGROUND_APPLICATION
: /* DARWIN_ROLE_UI_FOCAL */
1671 case TASK_BACKGROUND_APPLICATION
: /* DARWIN_ROLE_UI */
1673 * The leader of this coalition is a focal, UI app:
1675 * TODO: should extensions/plugins be allowed to use
1679 reason
= PROC_FGHW_OK
;
1681 case TASK_DEFAULT_APPLICATION
: /* DARWIN_ROLE_UI_NON_FOCAL */
1682 case TASK_NONUI_APPLICATION
: /* DARWIN_ROLE_NON_UI */
1683 case TASK_THROTTLE_APPLICATION
:
1684 case TASK_UNSPECIFIED
:
1686 /* non-focal, non-ui apps don't get access */
1687 reason
= PROC_FGHW_LEADER_NONUI
;
1693 if (task
!= TASK_NULL
) {
1694 task_deallocate(task
);
1697 #endif /* CONFIG_COALITIONS */
1700 * There is no reasonable semantic to investigate the currently
1701 * adopted voucher of an arbitrary thread in a non-current process.
1704 if (p
!= current_proc()) {
1710 * In the absence of coalitions, fall back to a voucher-based lookup
1711 * where a daemon can used foreground HW if it's operating on behalf
1712 * of a foreground application.
1713 * NOTE: this is equivalent to a call to
1714 * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
1717 error
= proc_get_originatorbgstate(&isBG
);
1722 reason
= PROC_FGHW_NO_ORIGINATOR
;
1726 reason
= PROC_FGHW_NO_VOUCHER_ATTR
;
1730 reason
= PROC_FGHW_DAEMON_NO_VOUCHER
;
1734 /* some other error occurred: report that to the caller */
1735 reason
= PROC_FGHW_VOUCHER_ERROR
;
1740 reason
= PROC_FGHW_ORIGINATOR_BACKGROUND
;
1744 * The process itself is either a foreground app, or has
1745 * adopted a voucher originating from an app that's still in
1748 reason
= PROC_FGHW_DAEMON_OK
;
1753 if (task
!= TASK_NULL
) {
1754 task_deallocate(task
);
1756 if (p
!= PROC_NULL
) {
1759 if (reasonsize
>= sizeof(reason
) && u_reason
!= (user_addr_t
)0) {
1760 (void)copyout(&reason
, u_reason
, sizeof(reason
));
1766 /********************************** proc_pidinfo ********************************/
1770 proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1772 struct proc
* p
= PROC_NULL
;
1773 int error
= ENOTSUP
;
1776 int shortversion
= 0;
1779 bool thuniqueid
= false;
1780 int uniqidversion
= 0;
1781 boolean_t check_same_user
;
1784 case PROC_PIDLISTFDS
:
1785 size
= PROC_PIDLISTFD_SIZE
;
1786 if (buffer
== USER_ADDR_NULL
) {
1790 case PROC_PIDTBSDINFO
:
1791 size
= PROC_PIDTBSDINFO_SIZE
;
1793 case PROC_PIDTASKINFO
:
1794 size
= PROC_PIDTASKINFO_SIZE
;
1796 case PROC_PIDTASKALLINFO
:
1797 size
= PROC_PIDTASKALLINFO_SIZE
;
1799 case PROC_PIDTHREADINFO
:
1800 size
= PROC_PIDTHREADINFO_SIZE
;
1802 case PROC_PIDLISTTHREADIDS
:
1803 size
= PROC_PIDLISTTHREADIDS_SIZE
;
1805 case PROC_PIDLISTTHREADS
:
1806 size
= PROC_PIDLISTTHREADS_SIZE
;
1808 case PROC_PIDREGIONINFO
:
1809 size
= PROC_PIDREGIONINFO_SIZE
;
1811 case PROC_PIDREGIONPATHINFO
:
1812 size
= PROC_PIDREGIONPATHINFO_SIZE
;
1814 case PROC_PIDVNODEPATHINFO
:
1815 size
= PROC_PIDVNODEPATHINFO_SIZE
;
1817 case PROC_PIDTHREADPATHINFO
:
1818 size
= PROC_PIDTHREADPATHINFO_SIZE
;
1820 case PROC_PIDPATHINFO
:
1823 case PROC_PIDWORKQUEUEINFO
:
1824 /* kernel does not have workq info */
1828 size
= PROC_PIDWORKQUEUEINFO_SIZE
;
1831 case PROC_PIDT_SHORTBSDINFO
:
1832 size
= PROC_PIDT_SHORTBSDINFO_SIZE
;
1834 case PROC_PIDLISTFILEPORTS
:
1835 size
= PROC_PIDLISTFILEPORTS_SIZE
;
1836 if (buffer
== (user_addr_t
)0) {
1840 case PROC_PIDTHREADID64INFO
:
1841 size
= PROC_PIDTHREADID64INFO_SIZE
;
1843 case PROC_PIDUNIQIDENTIFIERINFO
:
1844 size
= PROC_PIDUNIQIDENTIFIERINFO_SIZE
;
1846 case PROC_PIDT_BSDINFOWITHUNIQID
:
1847 size
= PROC_PIDT_BSDINFOWITHUNIQID_SIZE
;
1849 case PROC_PIDARCHINFO
:
1850 size
= PROC_PIDARCHINFO_SIZE
;
1852 case PROC_PIDCOALITIONINFO
:
1853 size
= PROC_PIDCOALITIONINFO_SIZE
;
1855 case PROC_PIDNOTEEXIT
:
1857 * Set findzomb explicitly because arg passed
1858 * in is used as note exit status bits.
1860 size
= PROC_PIDNOTEEXIT_SIZE
;
1863 case PROC_PIDEXITREASONINFO
:
1864 size
= PROC_PIDEXITREASONINFO_SIZE
;
1867 case PROC_PIDEXITREASONBASICINFO
:
1868 size
= PROC_PIDEXITREASONBASICINFOSIZE
;
1871 case PROC_PIDREGIONPATHINFO2
:
1872 size
= PROC_PIDREGIONPATHINFO2_SIZE
;
1874 case PROC_PIDREGIONPATHINFO3
:
1875 size
= PROC_PIDREGIONPATHINFO3_SIZE
;
1877 case PROC_PIDLISTUPTRS
:
1878 size
= PROC_PIDLISTUPTRS_SIZE
;
1879 if (buffer
== USER_ADDR_NULL
) {
1883 case PROC_PIDLISTDYNKQUEUES
:
1884 size
= PROC_PIDLISTDYNKQUEUES_SIZE
;
1885 if (buffer
== USER_ADDR_NULL
) {
1889 case PROC_PIDVMRTFAULTINFO
:
1890 size
= sizeof(vm_rtfault_record_t
);
1891 if (buffer
== USER_ADDR_NULL
) {
1899 if (buffersize
< size
) {
1903 if ((flavor
== PROC_PIDPATHINFO
) && (buffersize
> PROC_PIDPATHINFO_MAXSIZE
)) {
1907 /* Check if we need to look for zombies */
1908 if ((flavor
== PROC_PIDTBSDINFO
) || (flavor
== PROC_PIDT_SHORTBSDINFO
) || (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1909 || (flavor
== PROC_PIDUNIQIDENTIFIERINFO
)) {
1915 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1917 p
= proc_find_zombref(pid
);
1919 if (p
== PROC_NULL
) {
1928 /* Certain operations don't require privileges */
1930 case PROC_PIDT_SHORTBSDINFO
:
1931 case PROC_PIDUNIQIDENTIFIERINFO
:
1932 case PROC_PIDPATHINFO
:
1933 case PROC_PIDCOALITIONINFO
:
1934 check_same_user
= NO_CHECK_SAME_USER
;
1937 check_same_user
= CHECK_SAME_USER
;
1941 /* Do we have permission to look into this? */
1942 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDINFO
, flavor
, check_same_user
))) {
1947 case PROC_PIDLISTFDS
: {
1948 error
= proc_pidfdlist(p
, buffer
, buffersize
, retval
);
1952 case PROC_PIDUNIQIDENTIFIERINFO
: {
1953 struct proc_uniqidentifierinfo p_uniqidinfo
;
1954 bzero(&p_uniqidinfo
, sizeof(p_uniqidinfo
));
1955 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
1956 error
= copyout(&p_uniqidinfo
, buffer
, sizeof(struct proc_uniqidentifierinfo
));
1958 *retval
= sizeof(struct proc_uniqidentifierinfo
);
1963 case PROC_PIDT_SHORTBSDINFO
:
1965 case PROC_PIDT_BSDINFOWITHUNIQID
:
1966 case PROC_PIDTBSDINFO
: {
1967 struct proc_bsdinfo pbsd
;
1968 struct proc_bsdshortinfo pbsd_short
;
1969 struct proc_bsdinfowithuniqid pbsd_uniqid
;
1971 if (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
) {
1975 if (shortversion
!= 0) {
1976 error
= proc_pidshortbsdinfo(p
, &pbsd_short
, zombie
);
1978 error
= proc_pidbsdinfo(p
, &pbsd
, zombie
);
1979 if (uniqidversion
!= 0) {
1980 bzero(&pbsd_uniqid
, sizeof(pbsd_uniqid
));
1981 proc_piduniqidentifierinfo(p
, &pbsd_uniqid
.p_uniqidentifier
);
1982 pbsd_uniqid
.pbsd
= pbsd
;
1987 if (shortversion
!= 0) {
1988 error
= copyout(&pbsd_short
, buffer
, sizeof(struct proc_bsdshortinfo
));
1990 *retval
= sizeof(struct proc_bsdshortinfo
);
1992 } else if (uniqidversion
!= 0) {
1993 error
= copyout(&pbsd_uniqid
, buffer
, sizeof(struct proc_bsdinfowithuniqid
));
1995 *retval
= sizeof(struct proc_bsdinfowithuniqid
);
1998 error
= copyout(&pbsd
, buffer
, sizeof(struct proc_bsdinfo
));
2000 *retval
= sizeof(struct proc_bsdinfo
);
2007 case PROC_PIDTASKINFO
: {
2008 struct proc_taskinfo ptinfo
;
2010 error
= proc_pidtaskinfo(p
, &ptinfo
);
2012 error
= copyout(&ptinfo
, buffer
, sizeof(struct proc_taskinfo
));
2014 *retval
= sizeof(struct proc_taskinfo
);
2020 case PROC_PIDTASKALLINFO
: {
2021 struct proc_taskallinfo pall
;
2022 bzero(&pall
, sizeof(pall
));
2023 error
= proc_pidbsdinfo(p
, &pall
.pbsd
, 0);
2024 error
= proc_pidtaskinfo(p
, &pall
.ptinfo
);
2026 error
= copyout(&pall
, buffer
, sizeof(struct proc_taskallinfo
));
2028 *retval
= sizeof(struct proc_taskallinfo
);
2034 case PROC_PIDTHREADID64INFO
:
2036 case PROC_PIDTHREADINFO
:{
2037 struct proc_threadinfo pthinfo
;
2039 error
= proc_pidthreadinfo(p
, arg
, thuniqueid
, &pthinfo
);
2041 error
= copyout(&pthinfo
, buffer
, sizeof(struct proc_threadinfo
));
2043 *retval
= sizeof(struct proc_threadinfo
);
2049 case PROC_PIDLISTTHREADIDS
:
2051 case PROC_PIDLISTTHREADS
:{
2052 error
= proc_pidlistthreads(p
, thuniqueid
, buffer
, buffersize
, retval
);
2056 case PROC_PIDREGIONINFO
:{
2057 error
= proc_pidregioninfo(p
, arg
, buffer
, buffersize
, retval
);
2062 case PROC_PIDREGIONPATHINFO
:{
2063 error
= proc_pidregionpathinfo(p
, arg
, buffer
, buffersize
, retval
);
2067 case PROC_PIDREGIONPATHINFO2
:{
2068 error
= proc_pidregionpathinfo2(p
, arg
, buffer
, buffersize
, retval
);
2072 case PROC_PIDREGIONPATHINFO3
:{
2073 error
= proc_pidregionpathinfo3(p
, arg
, buffer
, buffersize
, retval
);
2077 case PROC_PIDVNODEPATHINFO
:{
2078 error
= proc_pidvnodepathinfo(p
, arg
, buffer
, buffersize
, retval
);
2083 case PROC_PIDTHREADPATHINFO
:{
2084 struct proc_threadwithpathinfo pinfo
;
2086 error
= proc_pidthreadpathinfo(p
, arg
, &pinfo
);
2088 error
= copyout((caddr_t
)&pinfo
, buffer
, sizeof(struct proc_threadwithpathinfo
));
2090 *retval
= sizeof(struct proc_threadwithpathinfo
);
2096 case PROC_PIDPATHINFO
: {
2097 error
= proc_pidpathinfo(p
, arg
, buffer
, buffersize
, retval
);
2102 case PROC_PIDWORKQUEUEINFO
:{
2103 struct proc_workqueueinfo pwqinfo
;
2105 error
= proc_pidworkqueueinfo(p
, &pwqinfo
);
2107 error
= copyout(&pwqinfo
, buffer
, sizeof(struct proc_workqueueinfo
));
2109 *retval
= sizeof(struct proc_workqueueinfo
);
2115 case PROC_PIDLISTFILEPORTS
: {
2116 error
= proc_pidfileportlist(p
, buffer
, buffersize
, retval
);
2120 case PROC_PIDARCHINFO
: {
2121 struct proc_archinfo pai
;
2122 bzero(&pai
, sizeof(pai
));
2123 proc_archinfo(p
, &pai
);
2124 error
= copyout(&pai
, buffer
, sizeof(struct proc_archinfo
));
2126 *retval
= sizeof(struct proc_archinfo
);
2131 case PROC_PIDCOALITIONINFO
: {
2132 struct proc_pidcoalitioninfo pci
;
2133 proc_pidcoalitioninfo(p
, &pci
);
2134 error
= copyout(&pci
, buffer
, sizeof(struct proc_pidcoalitioninfo
));
2136 *retval
= sizeof(struct proc_pidcoalitioninfo
);
2141 case PROC_PIDNOTEEXIT
: {
2143 error
= proc_pidnoteexit(p
, arg
, &data
);
2145 error
= copyout(&data
, buffer
, sizeof(data
));
2147 *retval
= sizeof(data
);
2153 case PROC_PIDEXITREASONINFO
: {
2154 struct proc_exitreasoninfo eri
;
2156 error
= copyin(buffer
, &eri
, sizeof(eri
));
2161 error
= proc_pidexitreasoninfo(p
, &eri
, NULL
);
2163 error
= copyout(&eri
, buffer
, sizeof(eri
));
2165 *retval
= sizeof(eri
);
2171 case PROC_PIDEXITREASONBASICINFO
: {
2172 struct proc_exitreasonbasicinfo beri
;
2174 bzero(&beri
, sizeof(struct proc_exitreasonbasicinfo
));
2176 error
= proc_pidexitreasoninfo(p
, NULL
, &beri
);
2178 error
= copyout(&beri
, buffer
, sizeof(beri
));
2180 *retval
= sizeof(beri
);
2186 case PROC_PIDLISTUPTRS
:
2187 error
= proc_pidlistuptrs(p
, buffer
, buffersize
, retval
);
2190 case PROC_PIDLISTDYNKQUEUES
:
2191 error
= kevent_copyout_proc_dynkqids(p
, buffer
, buffersize
, retval
);
2193 case PROC_PIDVMRTFAULTINFO
: {
2194 /* This interface can only be employed on the current
2195 * process. We will eventually enforce an entitlement.
2199 if (p
!= current_proc()) {
2204 size_t kbufsz
= MIN(buffersize
, vmrtfaultinfo_bufsz());
2205 void *vmrtfbuf
= kalloc(kbufsz
);
2207 if (vmrtfbuf
== NULL
) {
2212 bzero(vmrtfbuf
, kbufsz
);
2214 uint64_t effpid
= get_current_unique_pid();
2215 /* The VM may choose to provide more comprehensive records
2216 * for root-privileged users on internal configurations.
2218 boolean_t isroot
= (suser(kauth_cred_get(), (u_short
*)0) == 0);
2219 int vmf_residue
= vmrtf_extract(effpid
, isroot
, kbufsz
, vmrtfbuf
, retval
);
2220 int vmfsz
= *retval
* sizeof(vm_rtfault_record_t
);
2224 error
= copyout(vmrtfbuf
, buffer
, vmfsz
);
2232 kfree(vmrtfbuf
, kbufsz
);
2243 } else if (zombie
) {
2244 proc_drop_zombref(p
);
2251 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
)
2253 struct vnode_fdinfo vfi
;
2256 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2259 bzero(&vfi
, sizeof(struct vnode_fdinfo
));
2260 fill_fileinfo(fp
, proc
, fd
, &vfi
.pfi
);
2261 error
= fill_vnodeinfo(vp
, &vfi
.pvi
);
2264 error
= copyout((caddr_t
)&vfi
, buffer
, sizeof(struct vnode_fdinfo
));
2266 *retval
= sizeof(struct vnode_fdinfo
);
2273 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
)
2275 struct vnode_fdinfowithpath vfip
;
2276 int count
, error
= 0;
2278 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2281 bzero(&vfip
, sizeof(struct vnode_fdinfowithpath
));
2282 fill_fileinfo(fp
, proc
, fd
, &vfip
.pfi
);
2283 error
= fill_vnodeinfo(vp
, &vfip
.pvip
.vip_vi
);
2286 vn_getpath(vp
, &vfip
.pvip
.vip_path
[0], &count
);
2287 vfip
.pvip
.vip_path
[MAXPATHLEN
- 1] = 0;
2289 error
= copyout((caddr_t
)&vfip
, buffer
, sizeof(struct vnode_fdinfowithpath
));
2291 *retval
= sizeof(struct vnode_fdinfowithpath
);
2300 fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* fproc
)
2302 fproc
->fi_openflags
= fp
->f_fglob
->fg_flag
;
2303 fproc
->fi_status
= 0;
2304 fproc
->fi_offset
= fp
->f_fglob
->fg_offset
;
2305 fproc
->fi_type
= FILEGLOB_DTYPE(fp
->f_fglob
);
2306 if (fp
->f_fglob
->fg_count
> 1) {
2307 fproc
->fi_status
|= PROC_FP_SHARED
;
2309 if (proc
!= PROC_NULL
) {
2310 if ((FDFLAGS_GET(proc
, fd
) & UF_EXCLOSE
) != 0) {
2311 fproc
->fi_status
|= PROC_FP_CLEXEC
;
2313 if ((FDFLAGS_GET(proc
, fd
) & UF_FORKCLOSE
) != 0) {
2314 fproc
->fi_status
|= PROC_FP_CLFORK
;
2317 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
2318 fproc
->fi_status
|= PROC_FP_GUARDED
;
2319 fproc
->fi_guardflags
= 0;
2320 if (fp_isguarded(fp
, GUARD_CLOSE
)) {
2321 fproc
->fi_guardflags
|= PROC_FI_GUARD_CLOSE
;
2323 if (fp_isguarded(fp
, GUARD_DUP
)) {
2324 fproc
->fi_guardflags
|= PROC_FI_GUARD_DUP
;
2326 if (fp_isguarded(fp
, GUARD_SOCKET_IPC
)) {
2327 fproc
->fi_guardflags
|= PROC_FI_GUARD_SOCKET_IPC
;
2329 if (fp_isguarded(fp
, GUARD_FILEPORT
)) {
2330 fproc
->fi_guardflags
|= PROC_FI_GUARD_FILEPORT
;
2338 fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
)
2340 vfs_context_t context
;
2344 bzero(&sb
, sizeof(struct stat64
));
2345 context
= vfs_context_create((vfs_context_t
)0);
2346 error
= vn_stat(vp
, &sb
, NULL
, 1, context
);
2347 (void)vfs_context_rele(context
);
2349 munge_vinfo_stat(&sb
, &vinfo
->vi_stat
);
2355 if (vp
->v_mount
!= dead_mountp
) {
2356 vinfo
->vi_fsid
= vp
->v_mount
->mnt_vfsstat
.f_fsid
;
2358 vinfo
->vi_fsid
.val
[0] = 0;
2359 vinfo
->vi_fsid
.val
[1] = 0;
2361 vinfo
->vi_type
= vp
->v_type
;
2367 pid_socketinfo(socket_t so
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2370 struct socket_fdinfo s
;
2373 bzero(&s
, sizeof(struct socket_fdinfo
));
2374 fill_fileinfo(fp
, proc
, fd
, &s
.pfi
);
2375 if ((error
= fill_socketinfo(so
, &s
.psi
)) == 0) {
2376 if ((error
= copyout(&s
, buffer
, sizeof(struct socket_fdinfo
))) == 0) {
2377 *retval
= sizeof(struct socket_fdinfo
);
2382 #pragma unused(so, fp, proc, fd, buffer)
2389 pid_pseminfo(struct psemnode
*psem
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2391 struct psem_fdinfo pseminfo
;
2394 bzero(&pseminfo
, sizeof(struct psem_fdinfo
));
2395 fill_fileinfo(fp
, proc
, fd
, &pseminfo
.pfi
);
2397 if ((error
= fill_pseminfo(psem
, &pseminfo
.pseminfo
)) == 0) {
2398 if ((error
= copyout(&pseminfo
, buffer
, sizeof(struct psem_fdinfo
))) == 0) {
2399 *retval
= sizeof(struct psem_fdinfo
);
2407 pid_pshminfo(struct pshmnode
*pshm
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2409 struct pshm_fdinfo pshminfo
;
2412 bzero(&pshminfo
, sizeof(struct pshm_fdinfo
));
2413 fill_fileinfo(fp
, proc
, fd
, &pshminfo
.pfi
);
2415 if ((error
= fill_pshminfo(pshm
, &pshminfo
.pshminfo
)) == 0) {
2416 if ((error
= copyout(&pshminfo
, buffer
, sizeof(struct pshm_fdinfo
))) == 0) {
2417 *retval
= sizeof(struct pshm_fdinfo
);
2425 pid_pipeinfo(struct pipe
* p
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2427 struct pipe_fdinfo pipeinfo
;
2430 bzero(&pipeinfo
, sizeof(struct pipe_fdinfo
));
2431 fill_fileinfo(fp
, proc
, fd
, &pipeinfo
.pfi
);
2432 if ((error
= fill_pipeinfo(p
, &pipeinfo
.pipeinfo
)) == 0) {
2433 if ((error
= copyout(&pipeinfo
, buffer
, sizeof(struct pipe_fdinfo
))) == 0) {
2434 *retval
= sizeof(struct pipe_fdinfo
);
2442 pid_kqueueinfo(struct kqueue
* kq
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2444 struct kqueue_fdinfo kqinfo
;
2447 bzero(&kqinfo
, sizeof(struct kqueue_fdinfo
));
2449 /* not all kq's are associated with a file (e.g. workqkq) */
2452 fill_fileinfo(fp
, proc
, fd
, &kqinfo
.pfi
);
2455 if ((error
= fill_kqueueinfo(kq
, &kqinfo
.kqueueinfo
)) == 0) {
2456 if ((error
= copyout(&kqinfo
, buffer
, sizeof(struct kqueue_fdinfo
))) == 0) {
2457 *retval
= sizeof(struct kqueue_fdinfo
);
2465 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
)
2471 /************************** proc_pidfdinfo routine ***************************/
2473 proc_pidfdinfo(int pid
, int flavor
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2476 int error
= ENOTSUP
;
2477 struct fileproc
* fp
= NULL
;
2481 case PROC_PIDFDVNODEINFO
:
2482 size
= PROC_PIDFDVNODEINFO_SIZE
;
2484 case PROC_PIDFDVNODEPATHINFO
:
2485 size
= PROC_PIDFDVNODEPATHINFO_SIZE
;
2487 case PROC_PIDFDSOCKETINFO
:
2488 size
= PROC_PIDFDSOCKETINFO_SIZE
;
2490 case PROC_PIDFDPSEMINFO
:
2491 size
= PROC_PIDFDPSEMINFO_SIZE
;
2493 case PROC_PIDFDPSHMINFO
:
2494 size
= PROC_PIDFDPSHMINFO_SIZE
;
2496 case PROC_PIDFDPIPEINFO
:
2497 size
= PROC_PIDFDPIPEINFO_SIZE
;
2499 case PROC_PIDFDKQUEUEINFO
:
2500 size
= PROC_PIDFDKQUEUEINFO_SIZE
;
2502 case PROC_PIDFDKQUEUE_EXTINFO
:
2503 size
= PROC_PIDFDKQUEUE_EXTINFO_SIZE
;
2504 if (buffer
== (user_addr_t
)0) {
2508 case PROC_PIDFDATALKINFO
:
2509 size
= PROC_PIDFDATALKINFO_SIZE
;
2516 if (buffersize
< size
) {
2520 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2525 /* Do we have permission to look into this? */
2526 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFDINFO
, flavor
, CHECK_SAME_USER
))) {
2531 case PROC_PIDFDVNODEINFO
: {
2535 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) != 0) {
2538 /* no need to be under the fdlock */
2539 error
= pid_vnodeinfo(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2543 case PROC_PIDFDVNODEPATHINFO
: {
2547 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) != 0) {
2551 /* no need to be under the fdlock */
2552 error
= pid_vnodeinfopath(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2556 case PROC_PIDFDSOCKETINFO
: {
2559 if ((error
= fp_getfsock(p
, fd
, &fp
, &so
)) != 0) {
2562 /* no need to be under the fdlock */
2563 error
= pid_socketinfo(so
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2567 case PROC_PIDFDPSEMINFO
: {
2568 struct psemnode
* psem
;
2570 if ((error
= fp_getfpsem(p
, fd
, &fp
, &psem
)) != 0) {
2573 /* no need to be under the fdlock */
2574 error
= pid_pseminfo(psem
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2578 case PROC_PIDFDPSHMINFO
: {
2579 struct pshmnode
* pshm
;
2581 if ((error
= fp_getfpshm(p
, fd
, &fp
, &pshm
)) != 0) {
2584 /* no need to be under the fdlock */
2585 error
= pid_pshminfo(pshm
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2589 case PROC_PIDFDPIPEINFO
: {
2590 struct pipe
* cpipe
;
2592 if ((error
= fp_getfpipe(p
, fd
, &fp
, &cpipe
)) != 0) {
2595 /* no need to be under the fdlock */
2596 error
= pid_pipeinfo(cpipe
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2600 case PROC_PIDFDKQUEUEINFO
: {
2604 if ((kq
= p
->p_fd
->fd_wqkqueue
) == NULL
) {
2605 /* wqkqueue is initialized on-demand */
2609 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2613 /* no need to be under the fdlock */
2614 error
= pid_kqueueinfo(kq
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2618 case PROC_PIDFDKQUEUE_EXTINFO
: {
2622 if ((kq
= p
->p_fd
->fd_wqkqueue
) == NULL
) {
2623 /* wqkqueue is initialized on-demand */
2627 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2630 error
= pid_kqueue_extinfo(p
, kq
, buffer
, buffersize
, retval
);
2641 fp_drop(p
, fd
, fp
, 0);
2649 #define MAX_UPTRS 16392
2652 proc_pidlistuptrs(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2659 if (buffer
!= USER_ADDR_NULL
) {
2660 count
= buffersize
/ sizeof(uint64_t);
2661 if (count
> MAX_UPTRS
) {
2665 buffersize
= count
* sizeof(uint64_t);
2666 kbuf
= kalloc(buffersize
);
2667 bzero(kbuf
, buffersize
);
2668 assert(kbuf
!= NULL
);
2676 nuptrs
= kevent_proc_copy_uptrs(p
, kbuf
, buffersize
);
2680 if (os_mul_overflow(nuptrs
, sizeof(uint64_t), ©size
)) {
2684 if (copysize
> buffersize
) {
2685 copysize
= buffersize
;
2687 error
= copyout(kbuf
, buffer
, copysize
);
2694 kfree(kbuf
, buffersize
);
2702 * Helper function for proc_pidfileportinfo
2705 struct fileport_info_args
{
2707 user_addr_t fia_buffer
;
2708 uint32_t fia_buffersize
;
2709 int32_t *fia_retval
;
2712 static kern_return_t
2713 proc_fileport_info(__unused mach_port_name_t name
,
2714 struct fileglob
*fg
, void *arg
)
2716 struct fileport_info_args
*fia
= arg
;
2717 struct fileproc __fileproc
, *fp
= &__fileproc
;
2720 bzero(fp
, sizeof(*fp
));
2723 switch (fia
->fia_flavor
) {
2724 case PROC_PIDFILEPORTVNODEPATHINFO
: {
2727 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
2731 vp
= (struct vnode
*)fg
->fg_data
;
2732 error
= pid_vnodeinfopath(vp
, vnode_vid(vp
), fp
, PROC_NULL
, 0,
2733 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2736 case PROC_PIDFILEPORTSOCKETINFO
: {
2739 if (FILEGLOB_DTYPE(fg
) != DTYPE_SOCKET
) {
2743 so
= (socket_t
)fg
->fg_data
;
2744 error
= pid_socketinfo(so
, fp
, PROC_NULL
, 0,
2745 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2748 case PROC_PIDFILEPORTPSHMINFO
: {
2749 struct pshmnode
*pshm
;
2751 if (FILEGLOB_DTYPE(fg
) != DTYPE_PSXSHM
) {
2752 error
= EBADF
; /* ick - mirror fp_getfpshm */
2755 pshm
= (struct pshmnode
*)fg
->fg_data
;
2756 error
= pid_pshminfo(pshm
, fp
, PROC_NULL
, 0,
2757 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2760 case PROC_PIDFILEPORTPIPEINFO
: {
2763 if (FILEGLOB_DTYPE(fg
) != DTYPE_PIPE
) {
2764 error
= EBADF
; /* ick - mirror fp_getfpipe */
2767 cpipe
= (struct pipe
*)fg
->fg_data
;
2768 error
= pid_pipeinfo(cpipe
, fp
, PROC_NULL
, 0,
2769 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2780 /************************* proc_pidfileportinfo routine *********************/
2782 proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
,
2783 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2786 int error
= ENOTSUP
;
2788 struct fileport_info_args fia
;
2790 /* fileport types are restricted by file_issendable() */
2793 case PROC_PIDFILEPORTVNODEPATHINFO
:
2794 size
= PROC_PIDFILEPORTVNODEPATHINFO_SIZE
;
2796 case PROC_PIDFILEPORTSOCKETINFO
:
2797 size
= PROC_PIDFILEPORTSOCKETINFO_SIZE
;
2799 case PROC_PIDFILEPORTPSHMINFO
:
2800 size
= PROC_PIDFILEPORTPSHMINFO_SIZE
;
2802 case PROC_PIDFILEPORTPIPEINFO
:
2803 size
= PROC_PIDFILEPORTPIPEINFO_SIZE
;
2809 if (buffersize
< size
) {
2812 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2817 /* Do we have permission to look into this? */
2818 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFILEPORTINFO
, flavor
, CHECK_SAME_USER
))) {
2822 fia
.fia_flavor
= flavor
;
2823 fia
.fia_buffer
= buffer
;
2824 fia
.fia_buffersize
= buffersize
;
2825 fia
.fia_retval
= retval
;
2827 if (fileport_invoke(p
->task
, name
,
2828 proc_fileport_info
, &fia
, &error
) != KERN_SUCCESS
) {
2838 proc_security_policy(proc_t targetp
, __unused
int callnum
, __unused
int flavor
, boolean_t check_same_user
)
2843 if ((error
= mac_proc_check_proc_info(current_proc(), targetp
, callnum
, flavor
))) {
2848 /* The 'listpids' call doesn't have a target proc */
2849 if (targetp
== PROC_NULL
) {
2850 assert(callnum
== PROC_INFO_CALL_LISTPIDS
&& check_same_user
== NO_CHECK_SAME_USER
);
2855 * Check for 'get information for processes owned by other users' privilege
2856 * root has this privilege by default
2858 if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO
, 0) == 0) {
2859 check_same_user
= FALSE
;
2862 if (check_same_user
) {
2863 kauth_cred_t target_cred
;
2866 target_cred
= kauth_cred_proc_ref(targetp
);
2867 target_uid
= kauth_cred_getuid(target_cred
);
2868 kauth_cred_unref(&target_cred
);
2870 if (kauth_getuid() != target_uid
) {
2879 proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2881 if (suser(kauth_cred_get(), (u_short
*)0) == 0) {
2882 return log_dmesg(buffer
, buffersize
, retval
);
2888 /* ********* process control sets on self only */
2890 proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t * retval
)
2892 struct proc
* pself
= PROC_NULL
;
2894 uint32_t pcontrol
= (uint32_t)arg
;
2895 struct uthread
*ut
= NULL
;
2896 char name_buf
[MAXTHREADNAMESIZE
];
2898 pself
= current_proc();
2899 if (pid
!= pself
->p_pid
) {
2903 /* Do we have permission to look into this? */
2904 if ((error
= proc_security_policy(pself
, PROC_INFO_CALL_SETCONTROL
, flavor
, NO_CHECK_SAME_USER
))) {
2909 case PROC_SELFSET_PCONTROL
: {
2910 if (pcontrol
> P_PCMAX
) {
2914 /* reset existing control setting while retaining action state */
2915 pself
->p_pcaction
&= PROC_ACTION_MASK
;
2916 /* set new control state */
2917 pself
->p_pcaction
|= pcontrol
;
2922 case PROC_SELFSET_THREADNAME
: {
2924 * This is a bit ugly, as it copies the name into the kernel, and then
2925 * invokes bsd_setthreadname again to copy it into the uthread name
2926 * buffer. Hopefully this isn't such a hot codepath that an additional
2927 * MAXTHREADNAMESIZE copy is a big issue.
2929 if (buffersize
> (MAXTHREADNAMESIZE
- 1)) {
2930 return ENAMETOOLONG
;
2933 ut
= current_uthread();
2935 bzero(name_buf
, MAXTHREADNAMESIZE
);
2936 error
= copyin(buffer
, name_buf
, buffersize
);
2939 bsd_setthreadname(ut
, name_buf
);
2944 case PROC_SELFSET_VMRSRCOWNER
: {
2945 /* need to to be superuser */
2946 if (suser(kauth_cred_get(), (u_short
*)0) != 0) {
2952 /* reset existing control setting while retaining action state */
2953 pself
->p_lflag
|= P_LVMRSRCOWNER
;
2958 case PROC_SELFSET_DELAYIDLESLEEP
: {
2959 /* mark or clear the process property to delay idle sleep disk IO */
2960 if (pcontrol
!= 0) {
2961 OSBitOrAtomic(P_DELAYIDLESLEEP
, &pself
->p_flag
);
2963 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP
), &pself
->p_flag
);
2976 #if CONFIG_MEMORYSTATUS
2979 proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t *retval
)
2981 struct proc
*target_p
;
2983 uint32_t pcontrol
= (uint32_t)arg
;
2984 kauth_cred_t my_cred
, target_cred
;
2985 boolean_t self
= FALSE
;
2986 boolean_t child
= FALSE
;
2987 boolean_t zombref
= FALSE
;
2990 target_p
= proc_find(pid
);
2992 if (target_p
== PROC_NULL
) {
2993 if (flavor
== PROC_DIRTYCONTROL_GET
) {
2994 target_p
= proc_find_zombref(pid
);
2998 if (target_p
== PROC_NULL
) {
3003 my_cred
= kauth_cred_get();
3004 target_cred
= kauth_cred_proc_ref(target_p
);
3006 /* Do we have permission to look into this? */
3007 if ((error
= proc_security_policy(target_p
, PROC_INFO_CALL_DIRTYCONTROL
, flavor
, NO_CHECK_SAME_USER
))) {
3011 selfpid
= proc_selfpid();
3012 if (pid
== selfpid
) {
3014 } else if (target_p
->p_ppid
== selfpid
) {
3019 case PROC_DIRTYCONTROL_TRACK
: {
3020 /* Only allow the process itself, its parent, or root */
3021 if ((self
== FALSE
) && (child
== FALSE
) && kauth_cred_issuser(kauth_cred_get()) != TRUE
) {
3026 error
= memorystatus_dirty_track(target_p
, pcontrol
);
3030 case PROC_DIRTYCONTROL_SET
: {
3031 /* Check privileges; use cansignal() here since the process could be terminated */
3032 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
)) {
3037 error
= memorystatus_dirty_set(target_p
, self
, pcontrol
);
3041 case PROC_DIRTYCONTROL_GET
: {
3042 /* No permissions check - dirty state is freely available */
3044 *retval
= memorystatus_dirty_get(target_p
);
3051 case PROC_DIRTYCONTROL_CLEAR
: {
3052 /* Check privileges; use cansignal() here since the process could be terminated */
3053 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
)) {
3058 error
= memorystatus_dirty_clear(target_p
, pcontrol
);
3065 proc_drop_zombref(target_p
);
3067 proc_rele(target_p
);
3070 kauth_cred_unref(&target_cred
);
3077 proc_dirtycontrol(__unused
int pid
, __unused
int flavor
, __unused
uint64_t arg
, __unused
int32_t *retval
)
3082 #endif /* CONFIG_MEMORYSTATUS */
3085 * proc_terminate() provides support for sudden termination.
3086 * SIGKILL is issued to tracked, clean processes; otherwise,
3091 proc_terminate(int pid
, int32_t *retval
)
3095 kauth_cred_t uc
= kauth_cred_get();
3099 /* XXX: Check if these are necessary */
3100 AUDIT_ARG(pid
, pid
);
3101 AUDIT_ARG(signum
, sig
);
3104 if (pid
<= 0 || retval
== NULL
) {
3108 if ((p
= proc_find(pid
)) == NULL
) {
3113 /* XXX: Check if these are necessary */
3114 AUDIT_ARG(process
, p
);
3117 /* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
3118 if (!cansignal(current_proc(), uc
, p
, SIGKILL
)) {
3123 /* Not allowed to sudden terminate yourself */
3124 if (p
== current_proc()) {
3129 #if CONFIG_MEMORYSTATUS
3130 /* Determine requisite signal to issue */
3131 sig
= memorystatus_on_terminate(p
);
3136 proc_set_task_policy(p
->task
, TASK_POLICY_ATTRIBUTE
,
3137 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
3149 * copy stat64 structure into vinfo_stat structure.
3152 munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
)
3154 bzero(vsbp
, sizeof(struct vinfo_stat
));
3156 vsbp
->vst_dev
= sbp
->st_dev
;
3157 vsbp
->vst_mode
= sbp
->st_mode
;
3158 vsbp
->vst_nlink
= sbp
->st_nlink
;
3159 vsbp
->vst_ino
= sbp
->st_ino
;
3160 vsbp
->vst_uid
= sbp
->st_uid
;
3161 vsbp
->vst_gid
= sbp
->st_gid
;
3162 vsbp
->vst_atime
= sbp
->st_atimespec
.tv_sec
;
3163 vsbp
->vst_atimensec
= sbp
->st_atimespec
.tv_nsec
;
3164 vsbp
->vst_mtime
= sbp
->st_mtimespec
.tv_sec
;
3165 vsbp
->vst_mtimensec
= sbp
->st_mtimespec
.tv_nsec
;
3166 vsbp
->vst_ctime
= sbp
->st_ctimespec
.tv_sec
;
3167 vsbp
->vst_ctimensec
= sbp
->st_ctimespec
.tv_nsec
;
3168 vsbp
->vst_birthtime
= sbp
->st_birthtimespec
.tv_sec
;
3169 vsbp
->vst_birthtimensec
= sbp
->st_birthtimespec
.tv_nsec
;
3170 vsbp
->vst_size
= sbp
->st_size
;
3171 vsbp
->vst_blocks
= sbp
->st_blocks
;
3172 vsbp
->vst_blksize
= sbp
->st_blksize
;
3173 vsbp
->vst_flags
= sbp
->st_flags
;
3174 vsbp
->vst_gen
= sbp
->st_gen
;
3175 vsbp
->vst_rdev
= sbp
->st_rdev
;
3176 vsbp
->vst_qspare
[0] = sbp
->st_qspare
[0];
3177 vsbp
->vst_qspare
[1] = sbp
->st_qspare
[1];
3181 proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, __unused
int32_t *retval
)
3187 if ((p
= proc_find(pid
)) == PROC_NULL
) {
3188 if ((p
= proc_find_zombref(pid
)) == PROC_NULL
) {
3194 /* Do we have permission to look into this? */
3195 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDRUSAGE
, flavor
, CHECK_SAME_USER
))) {
3199 error
= proc_get_rusage(p
, flavor
, buffer
, zombie
);
3203 proc_drop_zombref(p
);
3212 proc_archinfo(proc_t p
, struct proc_archinfo
*pai
)
3215 pai
->p_cputype
= p
->p_cputype
;
3216 pai
->p_cpusubtype
= p
->p_cpusubtype
;
3221 proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*ppci
)
3223 bzero(ppci
, sizeof(*ppci
));
3224 proc_coalitionids(p
, ppci
->coalition_id
);
3228 proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
)
3230 uint32_t reason_data_size
= 0;
3232 pid_t selfpid
= proc_selfpid();
3237 * One (and only one) of peri and pberi must be non-NULL.
3239 assert((peri
!= NULL
) || (pberi
!= NULL
));
3240 assert((peri
== NULL
) || (pberi
== NULL
));
3243 * Allow access to the parent of the exiting
3244 * child or the parent debugger only.
3247 if (p
->p_ppid
== selfpid
) {
3248 break; /* parent => ok */
3250 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3251 (p
->p_oppid
== selfpid
)) {
3252 break; /* parent-in-waiting => ok */
3258 if (p
->p_exit_reason
== OS_REASON_NULL
) {
3263 if (p
->p_exit_reason
->osr_kcd_buf
!= NULL
) {
3264 reason_data_size
= kcdata_memory_get_used_bytes(&p
->p_exit_reason
->osr_kcd_descriptor
);
3268 peri
->eri_namespace
= p
->p_exit_reason
->osr_namespace
;
3269 peri
->eri_code
= p
->p_exit_reason
->osr_code
;
3270 peri
->eri_flags
= p
->p_exit_reason
->osr_flags
;
3272 if ((peri
->eri_kcd_buf
== 0) || (peri
->eri_reason_buf_size
< reason_data_size
)) {
3277 peri
->eri_reason_buf_size
= reason_data_size
;
3278 if (reason_data_size
!= 0) {
3279 error
= copyout(p
->p_exit_reason
->osr_kcd_buf
, peri
->eri_kcd_buf
, reason_data_size
);
3282 pberi
->beri_namespace
= p
->p_exit_reason
->osr_namespace
;
3283 pberi
->beri_code
= p
->p_exit_reason
->osr_code
;
3284 pberi
->beri_flags
= p
->p_exit_reason
->osr_flags
;
3285 pberi
->beri_reason_buf_size
= reason_data_size
;
3294 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
3295 * It mimics the data that is typically captured by the
3296 * EVFILT_PROC, NOTE_EXIT event mechanism.
3297 * See filt_proc() in kern_event.c.
3300 proc_pidnoteexit(proc_t p
, uint64_t flags
, uint32_t *data
)
3302 uint32_t exit_data
= 0;
3303 uint32_t exit_flags
= (uint32_t)flags
;
3308 * Allow access to the parent of the exiting
3309 * child or the parent debugger only.
3312 pid_t selfpid
= proc_selfpid();
3314 if (p
->p_ppid
== selfpid
) {
3315 break; /* parent => ok */
3317 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3318 (p
->p_oppid
== selfpid
)) {
3319 break; /* parent-in-waiting => ok */
3325 if ((exit_flags
& NOTE_EXITSTATUS
) != 0) {
3326 /* The signal and exit status */
3327 exit_data
|= (p
->p_xstat
& NOTE_PDATAMASK
);
3330 if ((exit_flags
& NOTE_EXIT_DETAIL
) != 0) {
3331 /* The exit detail */
3332 if ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) != 0) {
3333 exit_data
|= NOTE_EXIT_DECRYPTFAIL
;
3336 if ((p
->p_lflag
& P_LTERM_JETSAM
) != 0) {
3337 exit_data
|= NOTE_EXIT_MEMORY
;
3339 switch (p
->p_lflag
& P_JETSAM_MASK
) {
3340 case P_JETSAM_VMPAGESHORTAGE
:
3341 exit_data
|= NOTE_EXIT_MEMORY_VMPAGESHORTAGE
;
3343 case P_JETSAM_VMTHRASHING
:
3344 exit_data
|= NOTE_EXIT_MEMORY_VMTHRASHING
;
3346 case P_JETSAM_FCTHRASHING
:
3347 exit_data
|= NOTE_EXIT_MEMORY_FCTHRASHING
;
3349 case P_JETSAM_VNODE
:
3350 exit_data
|= NOTE_EXIT_MEMORY_VNODE
;
3352 case P_JETSAM_HIWAT
:
3353 exit_data
|= NOTE_EXIT_MEMORY_HIWAT
;
3356 exit_data
|= NOTE_EXIT_MEMORY_PID
;
3358 case P_JETSAM_IDLEEXIT
:
3359 exit_data
|= NOTE_EXIT_MEMORY_IDLE
;
3364 if ((p
->p_csflags
& CS_KILLED
) != 0) {
3365 exit_data
|= NOTE_EXIT_CSERROR
;
3377 proc_piddynkqueueinfo(int pid
, int flavor
, kqueue_id_t kq_id
,
3378 user_addr_t ubuf
, uint32_t bufsize
, int32_t *retval
)
3383 if (ubuf
== USER_ADDR_NULL
) {
3388 if (p
== PROC_NULL
) {
3392 err
= proc_security_policy(p
, PROC_INFO_CALL_PIDDYNKQUEUEINFO
, 0, CHECK_SAME_USER
);
3398 case PROC_PIDDYNKQUEUE_INFO
:
3399 err
= kevent_copyout_dynkqinfo(p
, kq_id
, ubuf
, bufsize
, retval
);
3401 case PROC_PIDDYNKQUEUE_EXTINFO
:
3402 err
= kevent_copyout_dynkqextinfo(p
, kq_id
, ubuf
, bufsize
, retval
);
3415 #if !CONFIG_EMBEDDED
3417 proc_udata_info(int pid
, int flavor
, user_addr_t buffer
, uint32_t bufsize
, int32_t *retval
)
3423 if (p
== PROC_NULL
) {
3428 * Only support calls against oneself for the moment.
3430 if (p
->p_pid
!= proc_selfpid()) {
3435 if (bufsize
!= sizeof(p
->p_user_data
)) {
3441 case PROC_UDATA_INFO_SET
:
3442 err
= copyin(buffer
, &p
->p_user_data
, sizeof(p
->p_user_data
));
3444 case PROC_UDATA_INFO_GET
:
3445 err
= copyout(&p
->p_user_data
, buffer
, sizeof(p
->p_user_data
));
3461 #endif /* !CONFIG_EMBEDDED */