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_dispatchqueue_label_offset_from_proc(void *p
);
110 uint64_t get_return_to_kernel_offset_from_proc(void *p
);
111 int proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
114 * TODO: Replace the noinline attribute below. Currently, it serves
115 * to avoid stack bloat caused by inlining multiple functions that
116 * have large stack footprints; when the functions are independent
117 * of each other (will not both be called in any given call to the
118 * caller), this only serves to bloat the stack, as we allocate
119 * space for both functions, despite the fact that we only need a
120 * fraction of that space.
122 * Long term, these functions should not be allocating everything on
123 * the stack, and should move large allocations (the huge structs
124 * that proc info deals in) to the heap, or eliminate them if
127 * The functions that most desperately need to improve stack usage
128 * (starting with the worst offenders):
129 * proc_pidvnodepathinfo
131 * proc_pidregionpathinfo
137 * proc_pidoriginatorinfo
140 /* protos for proc_info calls */
141 int __attribute__ ((noinline
)) proc_listpids(uint32_t type
, uint32_t tyoneinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
142 int __attribute__ ((noinline
)) proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
143 int __attribute__ ((noinline
)) proc_pidfdinfo(int pid
, int flavor
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
144 int __attribute__ ((noinline
)) proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
145 int __attribute__ ((noinline
)) proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
146 int __attribute__ ((noinline
)) proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
147 int __attribute__ ((noinline
)) proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t * retval
);
148 int __attribute__ ((noinline
)) proc_terminate(int pid
, int32_t * retval
);
149 int __attribute__ ((noinline
)) proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, int32_t * retval
);
150 int __attribute__ ((noinline
)) proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
151 int __attribute__ ((noinline
)) proc_listcoalitions(int flavor
, int coaltype
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
152 int __attribute__ ((noinline
)) proc_can_use_foreground_hw(int pid
, user_addr_t reason
, uint32_t resonsize
, int32_t *retval
);
154 /* protos for procpidinfo calls */
155 int __attribute__ ((noinline
)) proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
156 int __attribute__ ((noinline
)) proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
*pbsd
, int zombie
);
157 int __attribute__ ((noinline
)) proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
*pbsd_shortp
, int zombie
);
158 int __attribute__ ((noinline
)) proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
*ptinfo
);
159 int __attribute__ ((noinline
)) proc_pidthreadinfo(proc_t p
, uint64_t arg
, bool thuniqueid
, struct proc_threadinfo
*pthinfo
);
160 int __attribute__ ((noinline
)) proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
);
161 int __attribute__ ((noinline
)) proc_pidlistthreads(proc_t p
, bool thuniqueid
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
162 int __attribute__ ((noinline
)) proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
163 int __attribute__ ((noinline
)) proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
164 int __attribute__ ((noinline
)) proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
165 int __attribute__ ((noinline
)) proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
166 int __attribute__ ((noinline
)) proc_pidvnodepathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
167 int __attribute__ ((noinline
)) proc_pidpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
168 int __attribute__ ((noinline
)) proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
);
169 int __attribute__ ((noinline
)) proc_pidfileportlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
170 void __attribute__ ((noinline
)) proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
171 void __attribute__ ((noinline
)) proc_archinfo(proc_t p
, struct proc_archinfo
*pai
);
172 void __attribute__ ((noinline
)) proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*pci
);
173 int __attribute__ ((noinline
)) proc_pidnoteexit(proc_t p
, uint64_t arg
, uint32_t *data
);
174 int __attribute__ ((noinline
)) proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
);
175 int __attribute__ ((noinline
)) proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
);
176 int __attribute__ ((noinline
)) proc_pidlistuptrs(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
177 int __attribute__ ((noinline
)) proc_piddynkqueueinfo(pid_t pid
, int flavor
, kqueue_id_t id
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
178 int __attribute__ ((noinline
)) proc_pidregionpath(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
);
179 int __attribute__ ((noinline
)) proc_pidipctableinfo(proc_t p
, struct proc_ipctableinfo
*table_info
);
182 int __attribute__ ((noinline
)) proc_udata_info(pid_t pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
185 /* protos for proc_pidfdinfo calls */
186 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
);
187 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
);
188 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
);
189 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
);
190 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
);
191 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
);
192 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
);
193 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
);
196 /* protos for misc */
198 int fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
, boolean_t check_fsgetpath
);
199 void fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* finfo
);
200 int proc_security_policy(proc_t targetp
, int callnum
, int flavor
, boolean_t check_same_user
);
201 static void munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
);
202 static int proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
);
203 int proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
);
205 extern int cansignal(struct proc
*, kauth_cred_t
, struct proc
*, int);
206 extern int proc_get_rusage(proc_t proc
, int flavor
, user_addr_t buffer
, int is_zombie
);
208 #define CHECK_SAME_USER TRUE
209 #define NO_CHECK_SAME_USER FALSE
212 get_dispatchqueue_offset_from_proc(void *p
)
215 proc_t pself
= (proc_t
)p
;
216 return pself
->p_dispatchqueue_offset
;
223 get_dispatchqueue_serialno_offset_from_proc(void *p
)
226 proc_t pself
= (proc_t
)p
;
227 return pself
->p_dispatchqueue_serialno_offset
;
234 get_dispatchqueue_label_offset_from_proc(void *p
)
237 proc_t pself
= (proc_t
)p
;
238 return pself
->p_dispatchqueue_label_offset
;
245 get_return_to_kernel_offset_from_proc(void *p
)
248 proc_t pself
= (proc_t
)p
;
249 return pself
->p_return_to_kernel_offset
;
255 /***************************** proc_info ********************/
258 proc_info(__unused
struct proc
*p
, struct proc_info_args
* uap
, int32_t *retval
)
260 return proc_info_internal(uap
->callnum
, uap
->pid
, uap
->flavor
, uap
->arg
, uap
->buffer
, uap
->buffersize
, retval
);
265 proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
268 case PROC_INFO_CALL_LISTPIDS
:
269 /* pid contains type and flavor contains typeinfo */
270 return proc_listpids(pid
, flavor
, buffer
, buffersize
, retval
);
271 case PROC_INFO_CALL_PIDINFO
:
272 return proc_pidinfo(pid
, flavor
, arg
, buffer
, buffersize
, retval
);
273 case PROC_INFO_CALL_PIDFDINFO
:
274 return proc_pidfdinfo(pid
, flavor
, (int)arg
, buffer
, buffersize
, retval
);
275 case PROC_INFO_CALL_KERNMSGBUF
:
276 return proc_kernmsgbuf(buffer
, buffersize
, retval
);
277 case PROC_INFO_CALL_SETCONTROL
:
278 return proc_setcontrol(pid
, flavor
, arg
, buffer
, buffersize
, retval
);
279 case PROC_INFO_CALL_PIDFILEPORTINFO
:
280 return proc_pidfileportinfo(pid
, flavor
, (mach_port_name_t
)arg
, buffer
, buffersize
, retval
);
281 case PROC_INFO_CALL_TERMINATE
:
282 return proc_terminate(pid
, retval
);
283 case PROC_INFO_CALL_DIRTYCONTROL
:
284 return proc_dirtycontrol(pid
, flavor
, arg
, retval
);
285 case PROC_INFO_CALL_PIDRUSAGE
:
286 return proc_pid_rusage(pid
, flavor
, buffer
, retval
);
287 case PROC_INFO_CALL_PIDORIGINATORINFO
:
288 return proc_pidoriginatorinfo(pid
, flavor
, buffer
, buffersize
, retval
);
289 case PROC_INFO_CALL_LISTCOALITIONS
:
290 return proc_listcoalitions(pid
/* flavor */, flavor
/* coaltype */, buffer
,
292 case PROC_INFO_CALL_CANUSEFGHW
:
293 return proc_can_use_foreground_hw(pid
, buffer
, buffersize
, retval
);
294 case PROC_INFO_CALL_PIDDYNKQUEUEINFO
:
295 return proc_piddynkqueueinfo(pid
, flavor
, (kqueue_id_t
)arg
, buffer
, buffersize
, retval
);
297 case PROC_INFO_CALL_UDATA_INFO
:
298 return proc_udata_info(pid
, flavor
, buffer
, buffersize
, retval
);
299 #endif /* !CONFIG_EMBEDDED */
307 /******************* proc_listpids routine ****************/
309 proc_listpids(uint32_t type
, uint32_t typeinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
311 uint32_t numprocs
= 0;
320 struct proclist
*current_list
;
322 /* Do we have permission to look into this? */
323 if ((error
= proc_security_policy(PROC_NULL
, PROC_INFO_CALL_LISTPIDS
, type
, NO_CHECK_SAME_USER
))) {
327 /* if the buffer is null, return num of procs */
328 if (buffer
== (user_addr_t
)0) {
329 *retval
= ((nprocs
+ 20) * sizeof(int));
333 if (buffersize
< sizeof(int)) {
336 wantpids
= buffersize
/ sizeof(int);
337 if ((nprocs
+ 20) > 0) {
338 numprocs
= (uint32_t)(nprocs
+ 20);
340 if (numprocs
> wantpids
) {
344 kbuf
= (char *)kalloc((vm_size_t
)(numprocs
* sizeof(int)));
348 bzero(kbuf
, numprocs
* sizeof(int));
354 current_list
= &allproc
;
356 LIST_FOREACH(p
, current_list
, p_list
) {
360 if (p
->p_pgrpid
!= (pid_t
)typeinfo
) {
365 if ((p
->p_ppid
!= (pid_t
)typeinfo
) && (((p
->p_lflag
& P_LTRACED
) == 0) || (p
->p_oppid
!= (pid_t
)typeinfo
))) {
374 /* racy but list lock is held */
375 if ((p
->p_flag
& P_CONTROLT
) == 0 ||
376 (p
->p_pgrp
== NULL
) || (p
->p_pgrp
->pg_session
== NULL
) ||
377 (tp
= SESSION_TP(p
->p_pgrp
->pg_session
)) == TTY_NULL
||
378 tp
->t_dev
!= (dev_t
)typeinfo
) {
383 if (p
->p_ucred
== NULL
) {
386 kauth_cred_t my_cred
;
389 my_cred
= kauth_cred_proc_ref(p
);
390 uid
= kauth_cred_getuid(my_cred
);
391 kauth_cred_unref(&my_cred
);
392 if (uid
!= (uid_t
)typeinfo
) {
398 if (p
->p_ucred
== NULL
) {
401 kauth_cred_t my_cred
;
404 my_cred
= kauth_cred_proc_ref(p
);
405 uid
= kauth_cred_getruid(my_cred
);
406 kauth_cred_unref(&my_cred
);
407 if (uid
!= (uid_t
)typeinfo
) {
413 if (p
->p_kdebug
== 0) {
432 if ((n
< numprocs
) && (current_list
== &allproc
)) {
433 current_list
= &zombproc
;
440 error
= copyout((caddr_t
)ptr
, buffer
, n
* sizeof(int));
442 *retval
= (n
* sizeof(int));
444 kfree(kbuf
, (vm_size_t
)(numprocs
* sizeof(int)));
450 /********************************** proc_pidfdlist routines ********************************/
453 proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
458 struct proc_fdinfo
* pfd
;
459 struct fileproc
* fp
;
464 if (p
->p_fd
->fd_nfiles
> 0) {
465 numfds
= (uint32_t)p
->p_fd
->fd_nfiles
;
468 if (buffer
== (user_addr_t
) 0) {
470 *retval
= (numfds
* sizeof(struct proc_fdinfo
));
474 /* buffersize is big enough atleast for one struct */
475 needfds
= buffersize
/ sizeof(struct proc_fdinfo
);
477 if (numfds
> needfds
) {
481 kbuf
= (char *)kalloc((vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
485 bzero(kbuf
, numfds
* sizeof(struct proc_fdinfo
));
489 pfd
= (struct proc_fdinfo
*)kbuf
;
491 for (n
= 0; ((n
< (int)numfds
) && (n
< p
->p_fd
->fd_nfiles
)); n
++) {
492 if (((fp
= p
->p_fd
->fd_ofiles
[n
]) != 0)
493 && ((p
->p_fd
->fd_ofileflags
[n
] & UF_RESERVED
) == 0)) {
494 file_type_t fdtype
= FILEGLOB_DTYPE(fp
->f_fglob
);
496 pfd
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
497 fdtype
: PROX_FDTYPE_ATALK
;
504 error
= copyout(kbuf
, buffer
, count
* sizeof(struct proc_fdinfo
));
505 kfree(kbuf
, (vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
507 *retval
= (count
* sizeof(struct proc_fdinfo
));
513 * Helper functions for proc_pidfileportlist.
516 proc_fileport_count(__unused mach_port_name_t name
,
517 __unused
struct fileglob
*fg
, void *arg
)
519 uint32_t *counter
= arg
;
525 struct fileport_fdtype_args
{
526 struct proc_fileportinfo
*ffa_pfi
;
527 struct proc_fileportinfo
*ffa_pfi_end
;
531 proc_fileport_fdtype(mach_port_name_t name
, struct fileglob
*fg
, void *arg
)
533 struct fileport_fdtype_args
*ffa
= arg
;
535 if (ffa
->ffa_pfi
!= ffa
->ffa_pfi_end
) {
536 file_type_t fdtype
= FILEGLOB_DTYPE(fg
);
538 ffa
->ffa_pfi
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
539 fdtype
: PROX_FDTYPE_ATALK
;
540 ffa
->ffa_pfi
->proc_fileport
= name
;
542 return 0; /* keep walking */
544 return -1; /* stop the walk! */
549 proc_pidfileportlist(proc_t p
,
550 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
554 struct proc_fileportinfo
*pfi
;
555 uint32_t needfileports
, numfileports
;
556 struct fileport_fdtype_args ffa
;
559 needfileports
= buffersize
/ sizeof(*pfi
);
560 if ((user_addr_t
)0 == buffer
|| needfileports
> (uint32_t)maxfiles
) {
562 * Either (i) the user is asking for a fileport count,
563 * or (ii) the number of fileports they're asking for is
564 * larger than the maximum number of open files (!); count
565 * them to bound subsequent heap allocations.
568 switch (fileport_walk(p
->task
,
569 proc_fileport_count
, &numfileports
)) {
572 case KERN_RESOURCE_SHORTAGE
:
574 case KERN_INVALID_TASK
:
580 if (numfileports
== 0) {
581 *retval
= 0; /* none at all, bail */
584 if ((user_addr_t
)0 == buffer
) {
585 numfileports
+= 20; /* accelerate convergence */
586 *retval
= numfileports
* sizeof(*pfi
);
589 if (needfileports
> numfileports
) {
590 needfileports
= numfileports
;
594 assert(buffersize
>= PROC_PIDLISTFILEPORTS_SIZE
);
596 kbufsize
= (vm_size_t
)needfileports
* sizeof(*pfi
);
597 pfi
= kbuf
= kalloc(kbufsize
);
601 bzero(kbuf
, kbufsize
);
604 ffa
.ffa_pfi_end
= pfi
+ needfileports
;
606 switch (fileport_walk(p
->task
, proc_fileport_fdtype
, &ffa
)) {
610 if ((numfileports
= pfi
- (typeof(pfi
))kbuf
) == 0) {
613 if (numfileports
> needfileports
) {
614 panic("more fileports returned than requested");
616 error
= copyout(kbuf
, buffer
, numfileports
* sizeof(*pfi
));
618 case KERN_RESOURCE_SHORTAGE
:
621 case KERN_INVALID_TASK
:
628 kfree(kbuf
, kbufsize
);
630 *retval
= numfileports
* sizeof(*pfi
);
636 proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
* pbsd
, int zombie
)
639 struct session
*sessionp
= NULL
;
641 kauth_cred_t my_cred
;
644 sessionp
= proc_session(p
);
646 my_cred
= kauth_cred_proc_ref(p
);
647 bzero(pbsd
, sizeof(struct proc_bsdinfo
));
648 pbsd
->pbi_status
= p
->p_stat
;
649 pbsd
->pbi_xstatus
= p
->p_xstat
;
650 pbsd
->pbi_pid
= p
->p_pid
;
651 pbsd
->pbi_ppid
= p
->p_ppid
;
652 pbsd
->pbi_uid
= kauth_cred_getuid(my_cred
);
653 pbsd
->pbi_gid
= kauth_cred_getgid(my_cred
);
654 pbsd
->pbi_ruid
= kauth_cred_getruid(my_cred
);
655 pbsd
->pbi_rgid
= kauth_cred_getrgid(my_cred
);
656 pbsd
->pbi_svuid
= kauth_cred_getsvuid(my_cred
);
657 pbsd
->pbi_svgid
= kauth_cred_getsvgid(my_cred
);
658 kauth_cred_unref(&my_cred
);
660 pbsd
->pbi_nice
= p
->p_nice
;
661 pbsd
->pbi_start_tvsec
= p
->p_start
.tv_sec
;
662 pbsd
->pbi_start_tvusec
= p
->p_start
.tv_usec
;
663 bcopy(&p
->p_comm
, &pbsd
->pbi_comm
[0], MAXCOMLEN
);
664 pbsd
->pbi_comm
[MAXCOMLEN
- 1] = '\0';
665 bcopy(&p
->p_name
, &pbsd
->pbi_name
[0], 2 * MAXCOMLEN
);
666 pbsd
->pbi_name
[(2 * MAXCOMLEN
) - 1] = '\0';
669 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
) {
670 pbsd
->pbi_flags
|= PROC_FLAG_SYSTEM
;
672 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
) {
673 pbsd
->pbi_flags
|= PROC_FLAG_TRACED
;
675 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
) {
676 pbsd
->pbi_flags
|= PROC_FLAG_INEXIT
;
678 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
) {
679 pbsd
->pbi_flags
|= PROC_FLAG_PPWAIT
;
681 if ((p
->p_flag
& P_LP64
) == P_LP64
) {
682 pbsd
->pbi_flags
|= PROC_FLAG_LP64
;
684 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
) {
685 pbsd
->pbi_flags
|= PROC_FLAG_CONTROLT
;
687 if ((p
->p_flag
& P_THCWD
) == P_THCWD
) {
688 pbsd
->pbi_flags
|= PROC_FLAG_THCWD
;
690 if ((p
->p_flag
& P_SUGID
) == P_SUGID
) {
691 pbsd
->pbi_flags
|= PROC_FLAG_PSUGID
;
693 if ((p
->p_flag
& P_EXEC
) == P_EXEC
) {
694 pbsd
->pbi_flags
|= PROC_FLAG_EXEC
;
697 if (sessionp
!= SESSION_NULL
) {
698 if (SESS_LEADER(p
, sessionp
)) {
699 pbsd
->pbi_flags
|= PROC_FLAG_SLEADER
;
701 if (sessionp
->s_ttyvp
) {
702 pbsd
->pbi_flags
|= PROC_FLAG_CTTY
;
707 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
) {
708 pbsd
->pbi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
710 #endif /* !CONFIG_EMBEDDED */
712 switch (PROC_CONTROL_STATE(p
)) {
714 pbsd
->pbi_flags
|= PROC_FLAG_PC_THROTTLE
;
717 pbsd
->pbi_flags
|= PROC_FLAG_PC_SUSP
;
720 pbsd
->pbi_flags
|= PROC_FLAG_PC_KILL
;
725 switch (PROC_ACTION_STATE(p
)) {
727 pbsd
->pbi_flags
|= PROC_FLAG_PA_THROTTLE
;
730 pbsd
->pbi_flags
|= PROC_FLAG_PA_SUSP
;
735 /* if process is a zombie skip bg state */
736 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
)) {
737 proc_get_darwinbgstate(p
->task
, &pbsd
->pbi_flags
);
741 pbsd
->pbi_nfiles
= p
->p_fd
->fd_nfiles
;
744 pbsd
->e_tdev
= NODEV
;
745 if (pg
!= PGRP_NULL
) {
746 pbsd
->pbi_pgid
= p
->p_pgrpid
;
747 pbsd
->pbi_pjobc
= pg
->pg_jobc
;
748 if ((p
->p_flag
& P_CONTROLT
) && (sessionp
!= SESSION_NULL
) && (tp
= SESSION_TP(sessionp
))) {
749 pbsd
->e_tdev
= tp
->t_dev
;
750 pbsd
->e_tpgid
= sessionp
->s_ttypgrpid
;
753 if (sessionp
!= SESSION_NULL
) {
754 session_rele(sessionp
);
756 if (pg
!= PGRP_NULL
) {
765 proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
* pbsd_shortp
, int zombie
)
767 bzero(pbsd_shortp
, sizeof(struct proc_bsdshortinfo
));
768 pbsd_shortp
->pbsi_pid
= p
->p_pid
;
769 pbsd_shortp
->pbsi_ppid
= p
->p_ppid
;
770 pbsd_shortp
->pbsi_pgid
= p
->p_pgrpid
;
771 pbsd_shortp
->pbsi_status
= p
->p_stat
;
772 bcopy(&p
->p_comm
, &pbsd_shortp
->pbsi_comm
[0], MAXCOMLEN
);
773 pbsd_shortp
->pbsi_comm
[MAXCOMLEN
- 1] = '\0';
775 pbsd_shortp
->pbsi_flags
= 0;
776 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
) {
777 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_SYSTEM
;
779 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
) {
780 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_TRACED
;
782 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
) {
783 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_INEXIT
;
785 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
) {
786 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PPWAIT
;
788 if ((p
->p_flag
& P_LP64
) == P_LP64
) {
789 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_LP64
;
791 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
) {
792 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_CONTROLT
;
794 if ((p
->p_flag
& P_THCWD
) == P_THCWD
) {
795 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_THCWD
;
797 if ((p
->p_flag
& P_SUGID
) == P_SUGID
) {
798 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PSUGID
;
800 if ((p
->p_flag
& P_EXEC
) == P_EXEC
) {
801 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_EXEC
;
804 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
) {
805 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
807 #endif /* !CONFIG_EMBEDDED */
809 switch (PROC_CONTROL_STATE(p
)) {
811 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_THROTTLE
;
814 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_SUSP
;
817 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_KILL
;
822 switch (PROC_ACTION_STATE(p
)) {
824 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_THROTTLE
;
827 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_SUSP
;
832 /* if process is a zombie skip bg state */
833 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
)) {
834 proc_get_darwinbgstate(p
->task
, &pbsd_shortp
->pbsi_flags
);
837 pbsd_shortp
->pbsi_uid
= p
->p_uid
;
838 pbsd_shortp
->pbsi_gid
= p
->p_gid
;
839 pbsd_shortp
->pbsi_ruid
= p
->p_ruid
;
840 pbsd_shortp
->pbsi_rgid
= p
->p_rgid
;
841 pbsd_shortp
->pbsi_svuid
= p
->p_svuid
;
842 pbsd_shortp
->pbsi_svgid
= p
->p_svgid
;
848 proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
* ptinfo
)
854 bzero(ptinfo
, sizeof(struct proc_taskinfo
));
855 fill_taskprocinfo(task
, (struct proc_taskinfo_internal
*)ptinfo
);
863 proc_pidthreadinfo(proc_t p
, uint64_t arg
, bool thuniqueid
, struct proc_threadinfo
*pthinfo
)
866 uint64_t threadaddr
= (uint64_t)arg
;
868 bzero(pthinfo
, sizeof(struct proc_threadinfo
));
870 error
= fill_taskthreadinfo(p
->task
, threadaddr
, thuniqueid
, (struct proc_threadinfo_internal
*)pthinfo
, NULL
, NULL
);
879 bsd_hasthreadname(void *uth
)
881 struct uthread
*ut
= (struct uthread
*)uth
;
883 /* This doesn't check for the empty string; do we care? */
892 bsd_getthreadname(void *uth
, char *buffer
)
894 struct uthread
*ut
= (struct uthread
*)uth
;
896 bcopy(ut
->pth_name
, buffer
, MAXTHREADNAMESIZE
);
901 * This is known to race with regards to the contents of the thread name; concurrent
902 * callers may result in a garbled name.
905 bsd_setthreadname(void *uth
, const char *name
)
907 struct uthread
*ut
= (struct uthread
*)uth
;
908 char * name_buf
= NULL
;
911 /* If there is no existing thread name, allocate a buffer for one. */
912 name_buf
= kalloc(MAXTHREADNAMESIZE
);
914 bzero(name_buf
, MAXTHREADNAMESIZE
);
916 /* Someone could conceivably have named the thread at the same time we did. */
917 if (!OSCompareAndSwapPtr(NULL
, name_buf
, &ut
->pth_name
)) {
918 kfree(name_buf
, MAXTHREADNAMESIZE
);
921 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV
, ut
->pth_name
);
924 strncpy(ut
->pth_name
, name
, MAXTHREADNAMESIZE
- 1);
925 kernel_debug_string_simple(TRACE_STRING_THREADNAME
, ut
->pth_name
);
929 bsd_copythreadname(void *dst_uth
, void *src_uth
)
931 struct uthread
*dst_ut
= (struct uthread
*)dst_uth
;
932 struct uthread
*src_ut
= (struct uthread
*)src_uth
;
934 if (src_ut
->pth_name
== NULL
) {
938 if (dst_ut
->pth_name
== NULL
) {
939 dst_ut
->pth_name
= (char *)kalloc(MAXTHREADNAMESIZE
);
940 if (dst_ut
->pth_name
== NULL
) {
945 bcopy(src_ut
->pth_name
, dst_ut
->pth_name
, MAXTHREADNAMESIZE
);
950 bsd_threadcdir(void * uth
, void *vptr
, int *vidp
)
952 struct uthread
* ut
= (struct uthread
*)uth
;
954 vnode_t
*vpp
= (vnode_t
*)vptr
;
969 proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
)
974 uint64_t threadaddr
= (uint64_t)arg
;
977 bzero(pinfo
, sizeof(struct proc_threadwithpathinfo
));
979 error
= fill_taskthreadinfo(p
->task
, threadaddr
, 0, (struct proc_threadinfo_internal
*)&pinfo
->pt
, (void *)&vp
, &vid
);
984 if ((vp
!= NULLVP
) && ((vnode_getwithvid(vp
, vid
)) == 0)) {
985 error
= fill_vnodeinfo(vp
, &pinfo
->pvip
.vip_vi
, FALSE
);
988 vn_getpath(vp
, &pinfo
->pvip
.vip_path
[0], &count
);
989 pinfo
->pvip
.vip_path
[MAXPATHLEN
- 1] = 0;
999 proc_pidlistthreads(proc_t p
, bool thuniqueid
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
1005 uint32_t numthreads
= 0;
1007 int num
= get_numthreads(p
->task
) + 10;
1009 numthreads
= (uint32_t)num
;
1012 count
= buffersize
/ (sizeof(uint64_t));
1014 if (numthreads
> count
) {
1018 kbuf
= (void *)kalloc(numthreads
* sizeof(uint64_t));
1022 bzero(kbuf
, numthreads
* sizeof(uint64_t));
1024 ret
= fill_taskthreadlist(p
->task
, kbuf
, numthreads
, thuniqueid
);
1026 error
= copyout(kbuf
, buffer
, ret
);
1027 kfree(kbuf
, numthreads
* sizeof(uint64_t));
1036 proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1038 struct proc_regioninfo preginfo
;
1041 bzero(&preginfo
, sizeof(struct proc_regioninfo
));
1042 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
, (uintptr_t *)0, (uint32_t *)0);
1046 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regioninfo
));
1048 *retval
= sizeof(struct proc_regioninfo
);
1055 proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1057 struct proc_regionwithpathinfo preginfo
;
1059 uintptr_t vnodeaddr
= 0;
1060 uint32_t vnodeid
= 0;
1064 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1066 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1071 vp
= (vnode_t
)vnodeaddr
;
1072 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1073 /* FILL THE VNODEINFO */
1074 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
, FALSE
);
1076 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1077 /* Always make sure it is null terminated */
1078 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
- 1] = 0;
1082 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1084 *retval
= sizeof(struct proc_regionwithpathinfo
);
1090 proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1092 struct proc_regionwithpathinfo preginfo
;
1094 uintptr_t vnodeaddr
= 0;
1095 uint32_t vnodeid
= 0;
1099 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1101 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1109 vp
= (vnode_t
)vnodeaddr
;
1110 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1111 /* FILL THE VNODEINFO */
1112 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
, FALSE
);
1114 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1115 /* Always make sure it is null terminated */
1116 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
- 1] = 0;
1122 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1124 *retval
= sizeof(struct proc_regionwithpathinfo
);
1130 proc_pidregionpath(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1132 struct proc_regionpath path
;
1134 uintptr_t vnodeaddr
= 0;
1135 uint32_t vnodeid
= 0;
1138 bzero(&path
, sizeof(struct proc_regionpath
));
1140 ret
= find_region_details(p
->task
, (vm_map_offset_t
) arg
,
1141 (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
,
1142 &path
.prpo_addr
, &path
.prpo_regionlength
);
1150 vp
= (vnode_t
)vnodeaddr
;
1151 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1152 int count
= MAXPATHLEN
;
1153 vn_getpath(vp
, &path
.prpo_path
[0], &count
);
1154 /* Always make sure it is null terminated */
1155 path
.prpo_path
[MAXPATHLEN
- 1] = 0;
1161 error
= copyout(&path
, buffer
, sizeof(struct proc_regionpath
));
1163 *retval
= sizeof(struct proc_regionpath
);
1169 proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1171 struct proc_regionwithpathinfo preginfo
;
1173 uintptr_t vnodeaddr
;
1179 /* Loop while looking for vnodes that match dev_t filter */
1181 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1185 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, addr
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1193 vp
= (vnode_t
)vnodeaddr
;
1194 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1195 /* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
1196 struct vnode_attr va
;
1198 memset(&va
, 0, sizeof(va
));
1200 VATTR_WANTED(&va
, va_fsid
);
1201 VATTR_WANTED(&va
, va_fsid64
);
1203 ret
= vnode_getattr(vp
, &va
, vfs_context_current());
1209 if (vnode_get_va_fsid(&va
) == arg
) {
1210 /* FILL THE VNODEINFO */
1211 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
, FALSE
);
1213 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1214 /* Always make sure it is null terminated */
1215 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
- 1] = 0;
1224 addr
= preginfo
.prp_prinfo
.pri_address
+ preginfo
.prp_prinfo
.pri_size
;
1227 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1229 *retval
= sizeof(struct proc_regionwithpathinfo
);
1235 * Path is relative to current process directory; may different from current
1239 proc_pidvnodepathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1241 struct proc_vnodepathinfo pvninfo
;
1243 vnode_t vncdirvp
= NULLVP
;
1244 uint32_t vncdirid
= 0;
1245 vnode_t vnrdirvp
= NULLVP
;
1246 uint32_t vnrdirid
= 0;
1249 bzero(&pvninfo
, sizeof(struct proc_vnodepathinfo
));
1252 if (p
->p_fd
->fd_cdir
) {
1253 vncdirvp
= p
->p_fd
->fd_cdir
;
1254 vncdirid
= p
->p_fd
->fd_cdir
->v_id
;
1256 if (p
->p_fd
->fd_rdir
) {
1257 vnrdirvp
= p
->p_fd
->fd_rdir
;
1258 vnrdirid
= p
->p_fd
->fd_rdir
->v_id
;
1262 if (vncdirvp
!= NULLVP
) {
1263 if ((error
= vnode_getwithvid(vncdirvp
, vncdirid
)) == 0) {
1264 /* FILL THE VNODEINFO */
1265 error
= fill_vnodeinfo(vncdirvp
, &pvninfo
.pvi_cdir
.vip_vi
, TRUE
);
1268 vn_getpath(vncdirvp
, &pvninfo
.pvi_cdir
.vip_path
[0], &count
);
1269 pvninfo
.pvi_cdir
.vip_path
[MAXPATHLEN
- 1] = 0;
1271 vnode_put(vncdirvp
);
1277 if ((error
== 0) && (vnrdirvp
!= NULLVP
)) {
1278 if ((error
= vnode_getwithvid(vnrdirvp
, vnrdirid
)) == 0) {
1279 /* FILL THE VNODEINFO */
1280 error
= fill_vnodeinfo(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_vi
, TRUE
);
1283 vn_getpath(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_path
[0], &count
);
1284 pvninfo
.pvi_rdir
.vip_path
[MAXPATHLEN
- 1] = 0;
1286 vnode_put(vnrdirvp
);
1292 error
= copyout(&pvninfo
, buffer
, sizeof(struct proc_vnodepathinfo
));
1294 *retval
= sizeof(struct proc_vnodepathinfo
);
1302 proc_pidpathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t *retval
)
1306 int len
= buffersize
;
1311 if (tvp
== NULLVP
) {
1315 buf
= (char *)kalloc(buffersize
);
1320 bzero(buf
, buffersize
);
1322 error
= proc_pidpathinfo_internal(p
, arg
, buf
, buffersize
, retval
);
1324 error
= copyout(buf
, buffer
, len
);
1326 kfree(buf
, buffersize
);
1331 proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
)
1335 vnode_t nvp
= NULLVP
;
1336 int len
= buffersize
;
1340 if (tvp
== NULLVP
) {
1344 vid
= vnode_vid(tvp
);
1345 error
= vnode_getwithvid(tvp
, vid
);
1347 error
= vn_getpath_fsenter(tvp
, buf
, &len
);
1350 error
= vnode_lookup(buf
, 0, &nvp
, vfs_context_current());
1351 if ((error
== 0) && (nvp
!= NULLVP
)) {
1361 proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
)
1365 bzero(pwqinfo
, sizeof(struct proc_workqueueinfo
));
1367 error
= fill_procworkqueue(p
, pwqinfo
);
1377 proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
)
1379 p_uniqidinfo
->p_uniqueid
= proc_uniqueid(p
);
1380 proc_getexecutableuuid(p
, (unsigned char *)&p_uniqidinfo
->p_uuid
, sizeof(p_uniqidinfo
->p_uuid
));
1381 p_uniqidinfo
->p_puniqueid
= proc_puniqueid(p
);
1382 p_uniqidinfo
->p_reserve2
= 0;
1383 p_uniqidinfo
->p_reserve3
= 0;
1384 p_uniqidinfo
->p_reserve4
= 0;
1389 proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
)
1391 struct proc
* p
= PROC_NULL
;
1394 if (buffersize
< sizeof(uuid_t
)) {
1398 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1399 p
= proc_find_zombref(pid
);
1402 if (p
== PROC_NULL
) {
1406 proc_getexecutableuuid(p
, (unsigned char *)uuid_buf
, buffersize
);
1409 proc_drop_zombref(p
);
1418 * Function to get the uuid and pid of the originator of the voucher.
1421 proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
)
1423 pid_t originator_pid
;
1428 * Get the current voucher origin pid. The pid returned here
1429 * might not be valid or may have been recycled.
1431 kr
= thread_get_current_voucher_origin_pid(&originator_pid
);
1432 /* If errors, convert errors to appropriate format */
1434 if (kr
== KERN_INVALID_TASK
) {
1436 } else if (kr
== KERN_INVALID_VALUE
) {
1444 *pid
= originator_pid
;
1445 error
= proc_piduuidinfo(originator_pid
, uuid
, buffersize
);
1450 * Function to get the uuid of the originator of the voucher.
1453 proc_pidoriginatoruuid(uuid_t uuid
, uint32_t buffersize
)
1455 pid_t originator_pid
;
1456 return proc_pidoriginatorpid_uuid(uuid
, buffersize
, &originator_pid
);
1460 * Function to get the task ipc table size.
1463 proc_pidipctableinfo(proc_t p
, struct proc_ipctableinfo
*table_info
)
1470 bzero(table_info
, sizeof(struct proc_ipctableinfo
));
1471 error
= fill_taskipctableinfo(task
, &(table_info
->table_size
), &(table_info
->table_free
));
1480 /***************************** proc_pidoriginatorinfo ***************************/
1483 proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1485 int error
= ENOTSUP
;
1489 case PROC_PIDORIGINATOR_UUID
:
1490 size
= PROC_PIDORIGINATOR_UUID_SIZE
;
1492 case PROC_PIDORIGINATOR_BGSTATE
:
1493 size
= PROC_PIDORIGINATOR_BGSTATE_SIZE
;
1495 case PROC_PIDORIGINATOR_PID_UUID
:
1496 size
= PROC_PIDORIGINATOR_PID_UUID_SIZE
;
1502 if (buffersize
< size
) {
1506 if (pid
!= 0 && pid
!= proc_selfpid()) {
1511 case PROC_PIDORIGINATOR_UUID
: {
1514 error
= proc_pidoriginatoruuid(uuid
, sizeof(uuid
));
1519 error
= copyout(uuid
, buffer
, size
);
1526 case PROC_PIDORIGINATOR_PID_UUID
: {
1527 struct proc_originatorinfo originator_info
;
1528 bzero(&originator_info
, sizeof(originator_info
));
1530 error
= proc_pidoriginatorpid_uuid(originator_info
.originator_uuid
,
1531 sizeof(uuid_t
), &originator_info
.originator_pid
);
1536 error
= copyout(&originator_info
, buffer
, size
);
1543 case PROC_PIDORIGINATOR_BGSTATE
: {
1544 uint32_t is_backgrounded
= 0;
1545 error
= proc_get_originatorbgstate(&is_backgrounded
);
1550 error
= copyout(&is_backgrounded
, buffer
, size
);
1564 /***************************** proc_listcoalitions ***************************/
1566 proc_listcoalitions(int flavor
, int type
, user_addr_t buffer
,
1567 uint32_t buffersize
, int32_t *retval
)
1569 #if CONFIG_COALITIONS
1570 int error
= ENOTSUP
;
1573 void *coalinfo
= NULL
;
1574 uint32_t k_buffersize
= 0, copyout_sz
= 0;
1575 int ncoals
= 0, ncoals_
= 0;
1577 /* struct procinfo_coalinfo; */
1580 case LISTCOALITIONS_ALL_COALS
:
1581 elem_size
= LISTCOALITIONS_ALL_COALS_SIZE
;
1584 case LISTCOALITIONS_SINGLE_TYPE
:
1585 elem_size
= LISTCOALITIONS_SINGLE_TYPE_SIZE
;
1592 /* find the total number of coalitions */
1593 ncoals
= coalitions_get_list(coal_type
, NULL
, 0);
1595 if (ncoals
== 0 || buffer
== 0 || buffersize
== 0) {
1597 * user just wants buffer size
1598 * or there are no coalitions
1601 *retval
= (int)(ncoals
* elem_size
);
1605 k_buffersize
= ncoals
* elem_size
;
1606 coalinfo
= kalloc((vm_size_t
)k_buffersize
);
1611 bzero(coalinfo
, k_buffersize
);
1614 case LISTCOALITIONS_ALL_COALS
:
1615 case LISTCOALITIONS_SINGLE_TYPE
:
1616 ncoals_
= coalitions_get_list(coal_type
, coalinfo
, ncoals
);
1619 panic("memory corruption?!");
1623 /* all the coalitions disappeared... weird but valid */
1630 * Some coalitions may have disappeared between our initial check,
1631 * and the the actual list acquisition.
1632 * Only copy out what we really need.
1634 copyout_sz
= k_buffersize
;
1635 if (ncoals_
< ncoals
) {
1636 copyout_sz
= ncoals_
* elem_size
;
1640 * copy the list up to user space
1641 * (we're guaranteed to have a non-null pointer/size here)
1643 error
= copyout(coalinfo
, buffer
,
1644 copyout_sz
< buffersize
? copyout_sz
: buffersize
);
1647 *retval
= (int)copyout_sz
;
1652 kfree(coalinfo
, k_buffersize
);
1657 /* no coalition support */
1668 /*************************** proc_can_use_forgeound_hw **************************/
1670 proc_can_use_foreground_hw(int pid
, user_addr_t u_reason
, uint32_t reasonsize
, int32_t *retval
)
1672 proc_t p
= PROC_NULL
;
1674 uint32_t reason
= PROC_FGHW_ERROR
;
1676 task_t task
= TASK_NULL
;
1677 #if CONFIG_COALITIONS
1678 coalition_t coal
= COALITION_NULL
;
1685 reason
= PROC_FGHW_ERROR
;
1690 if (p
== PROC_NULL
) {
1692 reason
= PROC_FGHW_ERROR
;
1696 #if CONFIG_COALITIONS
1697 if (p
!= current_proc() &&
1698 !kauth_cred_issuser(kauth_cred_get())) {
1700 reason
= PROC_FGHW_ERROR
;
1705 if (coalition_is_leader(task
, task_get_coalition(task
, COALITION_TYPE_JETSAM
))) {
1706 task_reference(task
);
1708 /* current task is not a coalition leader: find the leader */
1709 task
= coalition_get_leader(coal
);
1712 if (task
!= TASK_NULL
) {
1714 * If task is non-null, then it is the coalition leader of the
1715 * current process' coalition. This could be the same task as
1716 * the current_task, and that's OK.
1721 proc_get_darwinbgstate(task
, &flags
);
1722 if ((flags
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
1724 * Coalition leader is not an application, continue
1725 * searching for other ways this task could gain
1728 reason
= PROC_FGHW_DAEMON_LEADER
;
1732 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
)) {
1734 * If the leader of the current process' coalition has
1735 * been marked as DARWIN_BG, then it definitely should
1736 * not be using foreground hardware resources.
1738 reason
= PROC_FGHW_LEADER_BACKGROUND
;
1742 role
= proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
);
1744 case TASK_FOREGROUND_APPLICATION
: /* DARWIN_ROLE_UI_FOCAL */
1745 case TASK_BACKGROUND_APPLICATION
: /* DARWIN_ROLE_UI */
1747 * The leader of this coalition is a focal, UI app:
1749 * TODO: should extensions/plugins be allowed to use
1753 reason
= PROC_FGHW_OK
;
1755 case TASK_DEFAULT_APPLICATION
: /* DARWIN_ROLE_UI_NON_FOCAL */
1756 case TASK_NONUI_APPLICATION
: /* DARWIN_ROLE_NON_UI */
1757 case TASK_THROTTLE_APPLICATION
:
1758 case TASK_UNSPECIFIED
:
1760 /* non-focal, non-ui apps don't get access */
1761 reason
= PROC_FGHW_LEADER_NONUI
;
1767 if (task
!= TASK_NULL
) {
1768 task_deallocate(task
);
1771 #endif /* CONFIG_COALITIONS */
1774 * There is no reasonable semantic to investigate the currently
1775 * adopted voucher of an arbitrary thread in a non-current process.
1778 if (p
!= current_proc()) {
1784 * In the absence of coalitions, fall back to a voucher-based lookup
1785 * where a daemon can used foreground HW if it's operating on behalf
1786 * of a foreground application.
1787 * NOTE: this is equivalent to a call to
1788 * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
1791 error
= proc_get_originatorbgstate(&isBG
);
1796 reason
= PROC_FGHW_NO_ORIGINATOR
;
1800 reason
= PROC_FGHW_NO_VOUCHER_ATTR
;
1804 reason
= PROC_FGHW_DAEMON_NO_VOUCHER
;
1808 /* some other error occurred: report that to the caller */
1809 reason
= PROC_FGHW_VOUCHER_ERROR
;
1814 reason
= PROC_FGHW_ORIGINATOR_BACKGROUND
;
1818 * The process itself is either a foreground app, or has
1819 * adopted a voucher originating from an app that's still in
1822 reason
= PROC_FGHW_DAEMON_OK
;
1827 if (task
!= TASK_NULL
) {
1828 task_deallocate(task
);
1830 if (p
!= PROC_NULL
) {
1833 if (reasonsize
>= sizeof(reason
) && u_reason
!= (user_addr_t
)0) {
1834 (void)copyout(&reason
, u_reason
, sizeof(reason
));
1840 /********************************** proc_pidinfo ********************************/
1844 proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1846 struct proc
* p
= PROC_NULL
;
1847 int error
= ENOTSUP
;
1850 int shortversion
= 0;
1853 bool thuniqueid
= false;
1854 int uniqidversion
= 0;
1855 boolean_t check_same_user
;
1858 case PROC_PIDLISTFDS
:
1859 size
= PROC_PIDLISTFD_SIZE
;
1860 if (buffer
== USER_ADDR_NULL
) {
1864 case PROC_PIDTBSDINFO
:
1865 size
= PROC_PIDTBSDINFO_SIZE
;
1867 case PROC_PIDTASKINFO
:
1868 size
= PROC_PIDTASKINFO_SIZE
;
1870 case PROC_PIDTASKALLINFO
:
1871 size
= PROC_PIDTASKALLINFO_SIZE
;
1873 case PROC_PIDTHREADINFO
:
1874 size
= PROC_PIDTHREADINFO_SIZE
;
1876 case PROC_PIDLISTTHREADIDS
:
1877 size
= PROC_PIDLISTTHREADIDS_SIZE
;
1879 case PROC_PIDLISTTHREADS
:
1880 size
= PROC_PIDLISTTHREADS_SIZE
;
1882 case PROC_PIDREGIONINFO
:
1883 size
= PROC_PIDREGIONINFO_SIZE
;
1885 case PROC_PIDREGIONPATHINFO
:
1886 size
= PROC_PIDREGIONPATHINFO_SIZE
;
1888 case PROC_PIDVNODEPATHINFO
:
1889 size
= PROC_PIDVNODEPATHINFO_SIZE
;
1891 case PROC_PIDTHREADPATHINFO
:
1892 size
= PROC_PIDTHREADPATHINFO_SIZE
;
1894 case PROC_PIDPATHINFO
:
1897 case PROC_PIDWORKQUEUEINFO
:
1898 /* kernel does not have workq info */
1902 size
= PROC_PIDWORKQUEUEINFO_SIZE
;
1905 case PROC_PIDT_SHORTBSDINFO
:
1906 size
= PROC_PIDT_SHORTBSDINFO_SIZE
;
1908 case PROC_PIDLISTFILEPORTS
:
1909 size
= PROC_PIDLISTFILEPORTS_SIZE
;
1910 if (buffer
== (user_addr_t
)0) {
1914 case PROC_PIDTHREADID64INFO
:
1915 size
= PROC_PIDTHREADID64INFO_SIZE
;
1917 case PROC_PIDUNIQIDENTIFIERINFO
:
1918 size
= PROC_PIDUNIQIDENTIFIERINFO_SIZE
;
1920 case PROC_PIDT_BSDINFOWITHUNIQID
:
1921 size
= PROC_PIDT_BSDINFOWITHUNIQID_SIZE
;
1923 case PROC_PIDARCHINFO
:
1924 size
= PROC_PIDARCHINFO_SIZE
;
1926 case PROC_PIDCOALITIONINFO
:
1927 size
= PROC_PIDCOALITIONINFO_SIZE
;
1929 case PROC_PIDNOTEEXIT
:
1931 * Set findzomb explicitly because arg passed
1932 * in is used as note exit status bits.
1934 size
= PROC_PIDNOTEEXIT_SIZE
;
1937 case PROC_PIDEXITREASONINFO
:
1938 size
= PROC_PIDEXITREASONINFO_SIZE
;
1941 case PROC_PIDEXITREASONBASICINFO
:
1942 size
= PROC_PIDEXITREASONBASICINFOSIZE
;
1945 case PROC_PIDREGIONPATHINFO2
:
1946 size
= PROC_PIDREGIONPATHINFO2_SIZE
;
1948 case PROC_PIDREGIONPATHINFO3
:
1949 size
= PROC_PIDREGIONPATHINFO3_SIZE
;
1951 case PROC_PIDLISTUPTRS
:
1952 size
= PROC_PIDLISTUPTRS_SIZE
;
1953 if (buffer
== USER_ADDR_NULL
) {
1957 case PROC_PIDLISTDYNKQUEUES
:
1958 size
= PROC_PIDLISTDYNKQUEUES_SIZE
;
1959 if (buffer
== USER_ADDR_NULL
) {
1963 case PROC_PIDVMRTFAULTINFO
:
1964 size
= sizeof(vm_rtfault_record_t
);
1965 if (buffer
== USER_ADDR_NULL
) {
1969 case PROC_PIDPLATFORMINFO
:
1970 size
= PROC_PIDPLATFORMINFO_SIZE
;
1973 case PROC_PIDREGIONPATH
:
1974 size
= PROC_PIDREGIONPATH_SIZE
;
1976 case PROC_PIDIPCTABLEINFO
:
1977 size
= PROC_PIDIPCTABLEINFO_SIZE
;
1983 if (buffersize
< size
) {
1987 if ((flavor
== PROC_PIDPATHINFO
) && (buffersize
> PROC_PIDPATHINFO_MAXSIZE
)) {
1991 /* Check if we need to look for zombies */
1992 if ((flavor
== PROC_PIDTBSDINFO
) || (flavor
== PROC_PIDT_SHORTBSDINFO
) || (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1993 || (flavor
== PROC_PIDUNIQIDENTIFIERINFO
)) {
1999 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2001 p
= proc_find_zombref(pid
);
2003 if (p
== PROC_NULL
) {
2012 /* Certain operations don't require privileges */
2014 case PROC_PIDT_SHORTBSDINFO
:
2015 case PROC_PIDUNIQIDENTIFIERINFO
:
2016 case PROC_PIDPATHINFO
:
2017 case PROC_PIDCOALITIONINFO
:
2018 case PROC_PIDPLATFORMINFO
:
2019 check_same_user
= NO_CHECK_SAME_USER
;
2022 check_same_user
= CHECK_SAME_USER
;
2026 /* Do we have permission to look into this? */
2027 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDINFO
, flavor
, check_same_user
))) {
2032 case PROC_PIDLISTFDS
: {
2033 error
= proc_pidfdlist(p
, buffer
, buffersize
, retval
);
2037 case PROC_PIDUNIQIDENTIFIERINFO
: {
2038 struct proc_uniqidentifierinfo p_uniqidinfo
;
2039 bzero(&p_uniqidinfo
, sizeof(p_uniqidinfo
));
2040 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
2041 error
= copyout(&p_uniqidinfo
, buffer
, sizeof(struct proc_uniqidentifierinfo
));
2043 *retval
= sizeof(struct proc_uniqidentifierinfo
);
2048 case PROC_PIDT_SHORTBSDINFO
:
2050 case PROC_PIDT_BSDINFOWITHUNIQID
:
2051 case PROC_PIDTBSDINFO
: {
2052 struct proc_bsdinfo pbsd
;
2053 struct proc_bsdshortinfo pbsd_short
;
2054 struct proc_bsdinfowithuniqid pbsd_uniqid
;
2056 if (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
) {
2060 if (shortversion
!= 0) {
2061 error
= proc_pidshortbsdinfo(p
, &pbsd_short
, zombie
);
2063 error
= proc_pidbsdinfo(p
, &pbsd
, zombie
);
2064 if (uniqidversion
!= 0) {
2065 bzero(&pbsd_uniqid
, sizeof(pbsd_uniqid
));
2066 proc_piduniqidentifierinfo(p
, &pbsd_uniqid
.p_uniqidentifier
);
2067 pbsd_uniqid
.pbsd
= pbsd
;
2072 if (shortversion
!= 0) {
2073 error
= copyout(&pbsd_short
, buffer
, sizeof(struct proc_bsdshortinfo
));
2075 *retval
= sizeof(struct proc_bsdshortinfo
);
2077 } else if (uniqidversion
!= 0) {
2078 error
= copyout(&pbsd_uniqid
, buffer
, sizeof(struct proc_bsdinfowithuniqid
));
2080 *retval
= sizeof(struct proc_bsdinfowithuniqid
);
2083 error
= copyout(&pbsd
, buffer
, sizeof(struct proc_bsdinfo
));
2085 *retval
= sizeof(struct proc_bsdinfo
);
2092 case PROC_PIDTASKINFO
: {
2093 struct proc_taskinfo ptinfo
;
2095 error
= proc_pidtaskinfo(p
, &ptinfo
);
2097 error
= copyout(&ptinfo
, buffer
, sizeof(struct proc_taskinfo
));
2099 *retval
= sizeof(struct proc_taskinfo
);
2105 case PROC_PIDTASKALLINFO
: {
2106 struct proc_taskallinfo pall
;
2107 bzero(&pall
, sizeof(pall
));
2108 error
= proc_pidbsdinfo(p
, &pall
.pbsd
, 0);
2109 error
= proc_pidtaskinfo(p
, &pall
.ptinfo
);
2111 error
= copyout(&pall
, buffer
, sizeof(struct proc_taskallinfo
));
2113 *retval
= sizeof(struct proc_taskallinfo
);
2119 case PROC_PIDTHREADID64INFO
:
2121 case PROC_PIDTHREADINFO
:{
2122 struct proc_threadinfo pthinfo
;
2124 error
= proc_pidthreadinfo(p
, arg
, thuniqueid
, &pthinfo
);
2126 error
= copyout(&pthinfo
, buffer
, sizeof(struct proc_threadinfo
));
2128 *retval
= sizeof(struct proc_threadinfo
);
2134 case PROC_PIDLISTTHREADIDS
:
2136 case PROC_PIDLISTTHREADS
:{
2137 error
= proc_pidlistthreads(p
, thuniqueid
, buffer
, buffersize
, retval
);
2141 case PROC_PIDREGIONINFO
:{
2142 error
= proc_pidregioninfo(p
, arg
, buffer
, buffersize
, retval
);
2147 case PROC_PIDREGIONPATHINFO
:{
2148 error
= proc_pidregionpathinfo(p
, arg
, buffer
, buffersize
, retval
);
2152 case PROC_PIDREGIONPATHINFO2
:{
2153 error
= proc_pidregionpathinfo2(p
, arg
, buffer
, buffersize
, retval
);
2157 case PROC_PIDREGIONPATHINFO3
:{
2158 error
= proc_pidregionpathinfo3(p
, arg
, buffer
, buffersize
, retval
);
2162 case PROC_PIDVNODEPATHINFO
:{
2163 error
= proc_pidvnodepathinfo(p
, arg
, buffer
, buffersize
, retval
);
2168 case PROC_PIDTHREADPATHINFO
:{
2169 struct proc_threadwithpathinfo pinfo
;
2171 error
= proc_pidthreadpathinfo(p
, arg
, &pinfo
);
2173 error
= copyout((caddr_t
)&pinfo
, buffer
, sizeof(struct proc_threadwithpathinfo
));
2175 *retval
= sizeof(struct proc_threadwithpathinfo
);
2181 case PROC_PIDPATHINFO
: {
2182 error
= proc_pidpathinfo(p
, arg
, buffer
, buffersize
, retval
);
2187 case PROC_PIDWORKQUEUEINFO
:{
2188 struct proc_workqueueinfo pwqinfo
;
2190 error
= proc_pidworkqueueinfo(p
, &pwqinfo
);
2192 error
= copyout(&pwqinfo
, buffer
, sizeof(struct proc_workqueueinfo
));
2194 *retval
= sizeof(struct proc_workqueueinfo
);
2200 case PROC_PIDLISTFILEPORTS
: {
2201 error
= proc_pidfileportlist(p
, buffer
, buffersize
, retval
);
2205 case PROC_PIDARCHINFO
: {
2206 struct proc_archinfo pai
;
2207 bzero(&pai
, sizeof(pai
));
2208 proc_archinfo(p
, &pai
);
2209 error
= copyout(&pai
, buffer
, sizeof(struct proc_archinfo
));
2211 *retval
= sizeof(struct proc_archinfo
);
2216 case PROC_PIDCOALITIONINFO
: {
2217 struct proc_pidcoalitioninfo pci
;
2218 proc_pidcoalitioninfo(p
, &pci
);
2219 error
= copyout(&pci
, buffer
, sizeof(struct proc_pidcoalitioninfo
));
2221 *retval
= sizeof(struct proc_pidcoalitioninfo
);
2226 case PROC_PIDNOTEEXIT
: {
2228 error
= proc_pidnoteexit(p
, arg
, &data
);
2230 error
= copyout(&data
, buffer
, sizeof(data
));
2232 *retval
= sizeof(data
);
2238 case PROC_PIDEXITREASONINFO
: {
2239 struct proc_exitreasoninfo eri
;
2241 error
= copyin(buffer
, &eri
, sizeof(eri
));
2246 error
= proc_pidexitreasoninfo(p
, &eri
, NULL
);
2248 error
= copyout(&eri
, buffer
, sizeof(eri
));
2250 *retval
= sizeof(eri
);
2256 case PROC_PIDEXITREASONBASICINFO
: {
2257 struct proc_exitreasonbasicinfo beri
;
2259 bzero(&beri
, sizeof(struct proc_exitreasonbasicinfo
));
2261 error
= proc_pidexitreasoninfo(p
, NULL
, &beri
);
2263 error
= copyout(&beri
, buffer
, sizeof(beri
));
2265 *retval
= sizeof(beri
);
2271 case PROC_PIDLISTUPTRS
:
2272 error
= proc_pidlistuptrs(p
, buffer
, buffersize
, retval
);
2275 case PROC_PIDLISTDYNKQUEUES
:
2276 error
= kevent_copyout_proc_dynkqids(p
, buffer
, buffersize
, retval
);
2278 case PROC_PIDVMRTFAULTINFO
: {
2279 /* This interface can only be employed on the current
2280 * process. We will eventually enforce an entitlement.
2284 if (p
!= current_proc()) {
2289 size_t kbufsz
= MIN(buffersize
, vmrtfaultinfo_bufsz());
2290 void *vmrtfbuf
= kalloc(kbufsz
);
2292 if (vmrtfbuf
== NULL
) {
2297 bzero(vmrtfbuf
, kbufsz
);
2299 uint64_t effpid
= get_current_unique_pid();
2300 /* The VM may choose to provide more comprehensive records
2301 * for root-privileged users on internal configurations.
2303 boolean_t isroot
= (suser(kauth_cred_get(), (u_short
*)0) == 0);
2304 int vmf_residue
= vmrtf_extract(effpid
, isroot
, kbufsz
, vmrtfbuf
, retval
);
2305 int vmfsz
= *retval
* sizeof(vm_rtfault_record_t
);
2309 error
= copyout(vmrtfbuf
, buffer
, vmfsz
);
2317 kfree(vmrtfbuf
, kbufsz
);
2320 case PROC_PIDPLATFORMINFO
: {
2322 uint32_t platform
= p
->p_platform
;
2324 error
= copyout(&platform
, buffer
, sizeof(uint32_t));
2326 *retval
= sizeof(uint32_t);
2329 case PROC_PIDREGIONPATH
: {
2330 error
= proc_pidregionpath(p
, arg
, buffer
, buffersize
, retval
);
2333 case PROC_PIDIPCTABLEINFO
: {
2334 struct proc_ipctableinfo table_info
;
2336 error
= proc_pidipctableinfo(p
, &table_info
);
2338 error
= copyout(&table_info
, buffer
, sizeof(struct proc_ipctableinfo
));
2340 *retval
= sizeof(struct proc_ipctableinfo
);
2353 } else if (zombie
) {
2354 proc_drop_zombref(p
);
2361 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
)
2363 struct vnode_fdinfo vfi
;
2366 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2369 bzero(&vfi
, sizeof(struct vnode_fdinfo
));
2370 fill_fileinfo(fp
, proc
, fd
, &vfi
.pfi
);
2371 error
= fill_vnodeinfo(vp
, &vfi
.pvi
, FALSE
);
2374 error
= copyout((caddr_t
)&vfi
, buffer
, sizeof(struct vnode_fdinfo
));
2376 *retval
= sizeof(struct vnode_fdinfo
);
2383 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
)
2385 struct vnode_fdinfowithpath vfip
;
2386 int count
, error
= 0;
2388 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2391 bzero(&vfip
, sizeof(struct vnode_fdinfowithpath
));
2392 fill_fileinfo(fp
, proc
, fd
, &vfip
.pfi
);
2393 error
= fill_vnodeinfo(vp
, &vfip
.pvip
.vip_vi
, TRUE
);
2396 vn_getpath(vp
, &vfip
.pvip
.vip_path
[0], &count
);
2397 vfip
.pvip
.vip_path
[MAXPATHLEN
- 1] = 0;
2399 error
= copyout((caddr_t
)&vfip
, buffer
, sizeof(struct vnode_fdinfowithpath
));
2401 *retval
= sizeof(struct vnode_fdinfowithpath
);
2410 fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* fproc
)
2412 fproc
->fi_openflags
= fp
->f_fglob
->fg_flag
;
2413 fproc
->fi_status
= 0;
2414 fproc
->fi_offset
= fp
->f_fglob
->fg_offset
;
2415 fproc
->fi_type
= FILEGLOB_DTYPE(fp
->f_fglob
);
2416 if (fp
->f_fglob
->fg_count
> 1) {
2417 fproc
->fi_status
|= PROC_FP_SHARED
;
2419 if (proc
!= PROC_NULL
) {
2420 if ((FDFLAGS_GET(proc
, fd
) & UF_EXCLOSE
) != 0) {
2421 fproc
->fi_status
|= PROC_FP_CLEXEC
;
2423 if ((FDFLAGS_GET(proc
, fd
) & UF_FORKCLOSE
) != 0) {
2424 fproc
->fi_status
|= PROC_FP_CLFORK
;
2427 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
2428 fproc
->fi_status
|= PROC_FP_GUARDED
;
2429 fproc
->fi_guardflags
= 0;
2430 if (fp_isguarded(fp
, GUARD_CLOSE
)) {
2431 fproc
->fi_guardflags
|= PROC_FI_GUARD_CLOSE
;
2433 if (fp_isguarded(fp
, GUARD_DUP
)) {
2434 fproc
->fi_guardflags
|= PROC_FI_GUARD_DUP
;
2436 if (fp_isguarded(fp
, GUARD_SOCKET_IPC
)) {
2437 fproc
->fi_guardflags
|= PROC_FI_GUARD_SOCKET_IPC
;
2439 if (fp_isguarded(fp
, GUARD_FILEPORT
)) {
2440 fproc
->fi_guardflags
|= PROC_FI_GUARD_FILEPORT
;
2448 fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
, __unused boolean_t check_fsgetpath
)
2450 vfs_context_t context
;
2454 bzero(&sb
, sizeof(struct stat64
));
2455 context
= vfs_context_create((vfs_context_t
)0);
2457 /* Called when vnode info is used by the caller to get vnode's path */
2458 if (check_fsgetpath
) {
2459 error
= mac_vnode_check_fsgetpath(context
, vp
);
2463 error
= vn_stat(vp
, &sb
, NULL
, 1, 0, context
);
2464 munge_vinfo_stat(&sb
, &vinfo
->vi_stat
);
2466 (void)vfs_context_rele(context
);
2471 if (vp
->v_mount
!= dead_mountp
) {
2472 vinfo
->vi_fsid
= vp
->v_mount
->mnt_vfsstat
.f_fsid
;
2474 vinfo
->vi_fsid
.val
[0] = 0;
2475 vinfo
->vi_fsid
.val
[1] = 0;
2477 vinfo
->vi_type
= vp
->v_type
;
2483 pid_socketinfo(socket_t so
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2486 struct socket_fdinfo s
;
2489 bzero(&s
, sizeof(struct socket_fdinfo
));
2490 fill_fileinfo(fp
, proc
, fd
, &s
.pfi
);
2491 if ((error
= fill_socketinfo(so
, &s
.psi
)) == 0) {
2492 if ((error
= copyout(&s
, buffer
, sizeof(struct socket_fdinfo
))) == 0) {
2493 *retval
= sizeof(struct socket_fdinfo
);
2498 #pragma unused(so, fp, proc, fd, buffer)
2505 pid_pseminfo(struct psemnode
*psem
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2507 struct psem_fdinfo pseminfo
;
2510 bzero(&pseminfo
, sizeof(struct psem_fdinfo
));
2511 fill_fileinfo(fp
, proc
, fd
, &pseminfo
.pfi
);
2513 if ((error
= fill_pseminfo(psem
, &pseminfo
.pseminfo
)) == 0) {
2514 if ((error
= copyout(&pseminfo
, buffer
, sizeof(struct psem_fdinfo
))) == 0) {
2515 *retval
= sizeof(struct psem_fdinfo
);
2523 pid_pshminfo(struct pshmnode
*pshm
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2525 struct pshm_fdinfo pshminfo
;
2528 bzero(&pshminfo
, sizeof(struct pshm_fdinfo
));
2529 fill_fileinfo(fp
, proc
, fd
, &pshminfo
.pfi
);
2531 if ((error
= fill_pshminfo(pshm
, &pshminfo
.pshminfo
)) == 0) {
2532 if ((error
= copyout(&pshminfo
, buffer
, sizeof(struct pshm_fdinfo
))) == 0) {
2533 *retval
= sizeof(struct pshm_fdinfo
);
2541 pid_pipeinfo(struct pipe
* p
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2543 struct pipe_fdinfo pipeinfo
;
2546 bzero(&pipeinfo
, sizeof(struct pipe_fdinfo
));
2547 fill_fileinfo(fp
, proc
, fd
, &pipeinfo
.pfi
);
2548 if ((error
= fill_pipeinfo(p
, &pipeinfo
.pipeinfo
)) == 0) {
2549 if ((error
= copyout(&pipeinfo
, buffer
, sizeof(struct pipe_fdinfo
))) == 0) {
2550 *retval
= sizeof(struct pipe_fdinfo
);
2558 pid_kqueueinfo(struct kqueue
* kq
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2560 struct kqueue_fdinfo kqinfo
;
2563 bzero(&kqinfo
, sizeof(struct kqueue_fdinfo
));
2565 /* not all kq's are associated with a file (e.g. workqkq) */
2568 fill_fileinfo(fp
, proc
, fd
, &kqinfo
.pfi
);
2571 if ((error
= fill_kqueueinfo(kq
, &kqinfo
.kqueueinfo
)) == 0) {
2572 if ((error
= copyout(&kqinfo
, buffer
, sizeof(struct kqueue_fdinfo
))) == 0) {
2573 *retval
= sizeof(struct kqueue_fdinfo
);
2581 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
)
2587 /************************** proc_pidfdinfo routine ***************************/
2589 proc_pidfdinfo(int pid
, int flavor
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2592 int error
= ENOTSUP
;
2593 struct fileproc
* fp
= NULL
;
2597 case PROC_PIDFDVNODEINFO
:
2598 size
= PROC_PIDFDVNODEINFO_SIZE
;
2600 case PROC_PIDFDVNODEPATHINFO
:
2601 size
= PROC_PIDFDVNODEPATHINFO_SIZE
;
2603 case PROC_PIDFDSOCKETINFO
:
2604 size
= PROC_PIDFDSOCKETINFO_SIZE
;
2606 case PROC_PIDFDPSEMINFO
:
2607 size
= PROC_PIDFDPSEMINFO_SIZE
;
2609 case PROC_PIDFDPSHMINFO
:
2610 size
= PROC_PIDFDPSHMINFO_SIZE
;
2612 case PROC_PIDFDPIPEINFO
:
2613 size
= PROC_PIDFDPIPEINFO_SIZE
;
2615 case PROC_PIDFDKQUEUEINFO
:
2616 size
= PROC_PIDFDKQUEUEINFO_SIZE
;
2618 case PROC_PIDFDKQUEUE_EXTINFO
:
2619 size
= PROC_PIDFDKQUEUE_EXTINFO_SIZE
;
2620 if (buffer
== (user_addr_t
)0) {
2624 case PROC_PIDFDATALKINFO
:
2625 size
= PROC_PIDFDATALKINFO_SIZE
;
2632 if (buffersize
< size
) {
2636 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2641 /* Do we have permission to look into this? */
2642 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFDINFO
, flavor
, CHECK_SAME_USER
))) {
2647 case PROC_PIDFDVNODEINFO
: {
2651 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) != 0) {
2654 /* no need to be under the fdlock */
2655 error
= pid_vnodeinfo(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2659 case PROC_PIDFDVNODEPATHINFO
: {
2663 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) != 0) {
2667 /* no need to be under the fdlock */
2668 error
= pid_vnodeinfopath(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2672 case PROC_PIDFDSOCKETINFO
: {
2675 if ((error
= fp_getfsock(p
, fd
, &fp
, &so
)) != 0) {
2678 /* no need to be under the fdlock */
2679 error
= pid_socketinfo(so
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2683 case PROC_PIDFDPSEMINFO
: {
2684 struct psemnode
* psem
;
2686 if ((error
= fp_getfpsem(p
, fd
, &fp
, &psem
)) != 0) {
2689 /* no need to be under the fdlock */
2690 error
= pid_pseminfo(psem
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2694 case PROC_PIDFDPSHMINFO
: {
2695 struct pshmnode
* pshm
;
2697 if ((error
= fp_getfpshm(p
, fd
, &fp
, &pshm
)) != 0) {
2700 /* no need to be under the fdlock */
2701 error
= pid_pshminfo(pshm
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2705 case PROC_PIDFDPIPEINFO
: {
2706 struct pipe
* cpipe
;
2708 if ((error
= fp_getfpipe(p
, fd
, &fp
, &cpipe
)) != 0) {
2711 /* no need to be under the fdlock */
2712 error
= pid_pipeinfo(cpipe
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2716 case PROC_PIDFDKQUEUEINFO
: {
2720 if ((kqu
.kqwq
= p
->p_fd
->fd_wqkqueue
) == NULL
) {
2721 /* wqkqueue is initialized on-demand */
2725 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kqu
.kq
)) != 0) {
2729 /* no need to be under the fdlock */
2730 error
= pid_kqueueinfo(kqu
.kq
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2734 case PROC_PIDFDKQUEUE_EXTINFO
: {
2738 if ((kqu
.kqwq
= p
->p_fd
->fd_wqkqueue
) == NULL
) {
2739 /* wqkqueue is initialized on-demand */
2743 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kqu
.kq
)) != 0) {
2746 error
= pid_kqueue_extinfo(p
, kqu
.kq
, buffer
, buffersize
, retval
);
2757 fp_drop(p
, fd
, fp
, 0);
2765 #define MAX_UPTRS 16392
2768 proc_pidlistuptrs(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2775 if (buffer
!= USER_ADDR_NULL
) {
2776 count
= buffersize
/ sizeof(uint64_t);
2777 if (count
> MAX_UPTRS
) {
2781 buffersize
= count
* sizeof(uint64_t);
2782 kbuf
= kalloc(buffersize
);
2783 bzero(kbuf
, buffersize
);
2784 assert(kbuf
!= NULL
);
2792 nuptrs
= kevent_proc_copy_uptrs(p
, kbuf
, buffersize
);
2796 if (os_mul_overflow(nuptrs
, sizeof(uint64_t), ©size
)) {
2800 if (copysize
> buffersize
) {
2801 copysize
= buffersize
;
2803 error
= copyout(kbuf
, buffer
, copysize
);
2810 kfree(kbuf
, buffersize
);
2818 * Helper function for proc_pidfileportinfo
2821 struct fileport_info_args
{
2823 user_addr_t fia_buffer
;
2824 uint32_t fia_buffersize
;
2825 int32_t *fia_retval
;
2828 static kern_return_t
2829 proc_fileport_info(__unused mach_port_name_t name
,
2830 struct fileglob
*fg
, void *arg
)
2832 struct fileport_info_args
*fia
= arg
;
2833 struct fileproc __fileproc
, *fp
= &__fileproc
;
2836 bzero(fp
, sizeof(*fp
));
2839 switch (fia
->fia_flavor
) {
2840 case PROC_PIDFILEPORTVNODEPATHINFO
: {
2843 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
2847 vp
= (struct vnode
*)fg
->fg_data
;
2848 error
= pid_vnodeinfopath(vp
, vnode_vid(vp
), fp
, PROC_NULL
, 0,
2849 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2852 case PROC_PIDFILEPORTSOCKETINFO
: {
2855 if (FILEGLOB_DTYPE(fg
) != DTYPE_SOCKET
) {
2859 so
= (socket_t
)fg
->fg_data
;
2860 error
= pid_socketinfo(so
, fp
, PROC_NULL
, 0,
2861 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2864 case PROC_PIDFILEPORTPSHMINFO
: {
2865 struct pshmnode
*pshm
;
2867 if (FILEGLOB_DTYPE(fg
) != DTYPE_PSXSHM
) {
2868 error
= EBADF
; /* ick - mirror fp_getfpshm */
2871 pshm
= (struct pshmnode
*)fg
->fg_data
;
2872 error
= pid_pshminfo(pshm
, fp
, PROC_NULL
, 0,
2873 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2876 case PROC_PIDFILEPORTPIPEINFO
: {
2879 if (FILEGLOB_DTYPE(fg
) != DTYPE_PIPE
) {
2880 error
= EBADF
; /* ick - mirror fp_getfpipe */
2883 cpipe
= (struct pipe
*)fg
->fg_data
;
2884 error
= pid_pipeinfo(cpipe
, fp
, PROC_NULL
, 0,
2885 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2896 /************************* proc_pidfileportinfo routine *********************/
2898 proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
,
2899 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2902 int error
= ENOTSUP
;
2904 struct fileport_info_args fia
;
2906 /* fileport types are restricted by file_issendable() */
2909 case PROC_PIDFILEPORTVNODEPATHINFO
:
2910 size
= PROC_PIDFILEPORTVNODEPATHINFO_SIZE
;
2912 case PROC_PIDFILEPORTSOCKETINFO
:
2913 size
= PROC_PIDFILEPORTSOCKETINFO_SIZE
;
2915 case PROC_PIDFILEPORTPSHMINFO
:
2916 size
= PROC_PIDFILEPORTPSHMINFO_SIZE
;
2918 case PROC_PIDFILEPORTPIPEINFO
:
2919 size
= PROC_PIDFILEPORTPIPEINFO_SIZE
;
2925 if (buffersize
< size
) {
2928 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2933 /* Do we have permission to look into this? */
2934 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFILEPORTINFO
, flavor
, CHECK_SAME_USER
))) {
2938 fia
.fia_flavor
= flavor
;
2939 fia
.fia_buffer
= buffer
;
2940 fia
.fia_buffersize
= buffersize
;
2941 fia
.fia_retval
= retval
;
2943 if (fileport_invoke(p
->task
, name
,
2944 proc_fileport_info
, &fia
, &error
) != KERN_SUCCESS
) {
2954 proc_security_policy(proc_t targetp
, __unused
int callnum
, __unused
int flavor
, boolean_t check_same_user
)
2959 if ((error
= mac_proc_check_proc_info(current_proc(), targetp
, callnum
, flavor
))) {
2964 /* The 'listpids' call doesn't have a target proc */
2965 if (targetp
== PROC_NULL
) {
2966 assert(callnum
== PROC_INFO_CALL_LISTPIDS
&& check_same_user
== NO_CHECK_SAME_USER
);
2971 * Check for 'get information for processes owned by other users' privilege
2972 * root has this privilege by default
2974 if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO
, 0) == 0) {
2975 check_same_user
= FALSE
;
2978 if (check_same_user
) {
2979 kauth_cred_t target_cred
;
2982 target_cred
= kauth_cred_proc_ref(targetp
);
2983 target_uid
= kauth_cred_getuid(target_cred
);
2984 kauth_cred_unref(&target_cred
);
2986 if (kauth_getuid() != target_uid
) {
2995 proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2997 if (suser(kauth_cred_get(), (u_short
*)0) == 0) {
2998 return log_dmesg(buffer
, buffersize
, retval
);
3004 /* ********* process control sets on self only */
3006 proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t * retval
)
3008 struct proc
* pself
= PROC_NULL
;
3010 uint32_t pcontrol
= (uint32_t)arg
;
3011 struct uthread
*ut
= NULL
;
3012 char name_buf
[MAXTHREADNAMESIZE
];
3014 pself
= current_proc();
3015 if (pid
!= pself
->p_pid
) {
3019 /* Do we have permission to look into this? */
3020 if ((error
= proc_security_policy(pself
, PROC_INFO_CALL_SETCONTROL
, flavor
, NO_CHECK_SAME_USER
))) {
3025 case PROC_SELFSET_PCONTROL
: {
3026 if (pcontrol
> P_PCMAX
) {
3030 /* reset existing control setting while retaining action state */
3031 pself
->p_pcaction
&= PROC_ACTION_MASK
;
3032 /* set new control state */
3033 pself
->p_pcaction
|= pcontrol
;
3038 case PROC_SELFSET_THREADNAME
: {
3040 * This is a bit ugly, as it copies the name into the kernel, and then
3041 * invokes bsd_setthreadname again to copy it into the uthread name
3042 * buffer. Hopefully this isn't such a hot codepath that an additional
3043 * MAXTHREADNAMESIZE copy is a big issue.
3045 if (buffersize
> (MAXTHREADNAMESIZE
- 1)) {
3046 return ENAMETOOLONG
;
3049 ut
= current_uthread();
3051 bzero(name_buf
, MAXTHREADNAMESIZE
);
3052 error
= copyin(buffer
, name_buf
, buffersize
);
3055 bsd_setthreadname(ut
, name_buf
);
3060 case PROC_SELFSET_VMRSRCOWNER
: {
3061 /* need to to be superuser */
3062 if (suser(kauth_cred_get(), (u_short
*)0) != 0) {
3068 /* reset existing control setting while retaining action state */
3069 pself
->p_lflag
|= P_LVMRSRCOWNER
;
3074 case PROC_SELFSET_DELAYIDLESLEEP
: {
3075 /* mark or clear the process property to delay idle sleep disk IO */
3076 if (pcontrol
!= 0) {
3077 OSBitOrAtomic(P_DELAYIDLESLEEP
, &pself
->p_flag
);
3079 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP
), &pself
->p_flag
);
3092 #if CONFIG_MEMORYSTATUS
3095 proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t *retval
)
3097 struct proc
*target_p
;
3099 uint32_t pcontrol
= (uint32_t)arg
;
3100 kauth_cred_t my_cred
, target_cred
;
3101 boolean_t self
= FALSE
;
3102 boolean_t child
= FALSE
;
3103 boolean_t zombref
= FALSE
;
3106 target_p
= proc_find(pid
);
3108 if (target_p
== PROC_NULL
) {
3109 if (flavor
== PROC_DIRTYCONTROL_GET
) {
3110 target_p
= proc_find_zombref(pid
);
3114 if (target_p
== PROC_NULL
) {
3119 my_cred
= kauth_cred_get();
3120 target_cred
= kauth_cred_proc_ref(target_p
);
3122 /* Do we have permission to look into this? */
3123 if ((error
= proc_security_policy(target_p
, PROC_INFO_CALL_DIRTYCONTROL
, flavor
, NO_CHECK_SAME_USER
))) {
3127 selfpid
= proc_selfpid();
3128 if (pid
== selfpid
) {
3130 } else if (target_p
->p_ppid
== selfpid
) {
3135 case PROC_DIRTYCONTROL_TRACK
: {
3136 /* Only allow the process itself, its parent, or root */
3137 if ((self
== FALSE
) && (child
== FALSE
) && kauth_cred_issuser(kauth_cred_get()) != TRUE
) {
3142 error
= memorystatus_dirty_track(target_p
, pcontrol
);
3146 case PROC_DIRTYCONTROL_SET
: {
3147 /* Check privileges; use cansignal() here since the process could be terminated */
3148 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
)) {
3153 error
= memorystatus_dirty_set(target_p
, self
, pcontrol
);
3157 case PROC_DIRTYCONTROL_GET
: {
3158 /* No permissions check - dirty state is freely available */
3160 *retval
= memorystatus_dirty_get(target_p
, FALSE
);
3167 case PROC_DIRTYCONTROL_CLEAR
: {
3168 /* Check privileges; use cansignal() here since the process could be terminated */
3169 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
)) {
3174 error
= memorystatus_dirty_clear(target_p
, pcontrol
);
3181 proc_drop_zombref(target_p
);
3183 proc_rele(target_p
);
3186 kauth_cred_unref(&target_cred
);
3193 proc_dirtycontrol(__unused
int pid
, __unused
int flavor
, __unused
uint64_t arg
, __unused
int32_t *retval
)
3198 #endif /* CONFIG_MEMORYSTATUS */
3201 * proc_terminate() provides support for sudden termination.
3202 * SIGKILL is issued to tracked, clean processes; otherwise,
3207 proc_terminate(int pid
, int32_t *retval
)
3211 kauth_cred_t uc
= kauth_cred_get();
3215 /* XXX: Check if these are necessary */
3216 AUDIT_ARG(pid
, pid
);
3217 AUDIT_ARG(signum
, sig
);
3220 if (pid
<= 0 || retval
== NULL
) {
3224 if ((p
= proc_find(pid
)) == NULL
) {
3229 /* XXX: Check if these are necessary */
3230 AUDIT_ARG(process
, p
);
3233 /* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
3234 if (!cansignal(current_proc(), uc
, p
, SIGKILL
)) {
3239 /* Not allowed to sudden terminate yourself */
3240 if (p
== current_proc()) {
3245 #if CONFIG_MEMORYSTATUS
3246 /* Determine requisite signal to issue */
3247 sig
= memorystatus_on_terminate(p
);
3252 proc_set_task_policy(p
->task
, TASK_POLICY_ATTRIBUTE
,
3253 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
3265 * copy stat64 structure into vinfo_stat structure.
3268 munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
)
3270 bzero(vsbp
, sizeof(struct vinfo_stat
));
3272 vsbp
->vst_dev
= sbp
->st_dev
;
3273 vsbp
->vst_mode
= sbp
->st_mode
;
3274 vsbp
->vst_nlink
= sbp
->st_nlink
;
3275 vsbp
->vst_ino
= sbp
->st_ino
;
3276 vsbp
->vst_uid
= sbp
->st_uid
;
3277 vsbp
->vst_gid
= sbp
->st_gid
;
3278 vsbp
->vst_atime
= sbp
->st_atimespec
.tv_sec
;
3279 vsbp
->vst_atimensec
= sbp
->st_atimespec
.tv_nsec
;
3280 vsbp
->vst_mtime
= sbp
->st_mtimespec
.tv_sec
;
3281 vsbp
->vst_mtimensec
= sbp
->st_mtimespec
.tv_nsec
;
3282 vsbp
->vst_ctime
= sbp
->st_ctimespec
.tv_sec
;
3283 vsbp
->vst_ctimensec
= sbp
->st_ctimespec
.tv_nsec
;
3284 vsbp
->vst_birthtime
= sbp
->st_birthtimespec
.tv_sec
;
3285 vsbp
->vst_birthtimensec
= sbp
->st_birthtimespec
.tv_nsec
;
3286 vsbp
->vst_size
= sbp
->st_size
;
3287 vsbp
->vst_blocks
= sbp
->st_blocks
;
3288 vsbp
->vst_blksize
= sbp
->st_blksize
;
3289 vsbp
->vst_flags
= sbp
->st_flags
;
3290 vsbp
->vst_gen
= sbp
->st_gen
;
3291 vsbp
->vst_rdev
= sbp
->st_rdev
;
3292 vsbp
->vst_qspare
[0] = sbp
->st_qspare
[0];
3293 vsbp
->vst_qspare
[1] = sbp
->st_qspare
[1];
3297 proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, __unused
int32_t *retval
)
3303 if ((p
= proc_find(pid
)) == PROC_NULL
) {
3304 if ((p
= proc_find_zombref(pid
)) == PROC_NULL
) {
3310 /* Do we have permission to look into this? */
3311 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDRUSAGE
, flavor
, CHECK_SAME_USER
))) {
3315 error
= proc_get_rusage(p
, flavor
, buffer
, zombie
);
3319 proc_drop_zombref(p
);
3328 proc_archinfo(proc_t p
, struct proc_archinfo
*pai
)
3331 pai
->p_cputype
= p
->p_cputype
;
3332 pai
->p_cpusubtype
= p
->p_cpusubtype
;
3337 proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*ppci
)
3339 bzero(ppci
, sizeof(*ppci
));
3340 proc_coalitionids(p
, ppci
->coalition_id
);
3344 proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
)
3346 uint32_t reason_data_size
= 0;
3348 pid_t selfpid
= proc_selfpid();
3353 * One (and only one) of peri and pberi must be non-NULL.
3355 assert((peri
!= NULL
) || (pberi
!= NULL
));
3356 assert((peri
== NULL
) || (pberi
== NULL
));
3359 * Allow access to the parent of the exiting
3360 * child or the parent debugger only.
3363 if (p
->p_ppid
== selfpid
) {
3364 break; /* parent => ok */
3366 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3367 (p
->p_oppid
== selfpid
)) {
3368 break; /* parent-in-waiting => ok */
3374 if (p
->p_exit_reason
== OS_REASON_NULL
) {
3379 if (p
->p_exit_reason
->osr_kcd_buf
!= NULL
) {
3380 reason_data_size
= kcdata_memory_get_used_bytes(&p
->p_exit_reason
->osr_kcd_descriptor
);
3384 peri
->eri_namespace
= p
->p_exit_reason
->osr_namespace
;
3385 peri
->eri_code
= p
->p_exit_reason
->osr_code
;
3386 peri
->eri_flags
= p
->p_exit_reason
->osr_flags
;
3388 if ((peri
->eri_kcd_buf
== 0) || (peri
->eri_reason_buf_size
< reason_data_size
)) {
3393 peri
->eri_reason_buf_size
= reason_data_size
;
3394 if (reason_data_size
!= 0) {
3395 error
= copyout(p
->p_exit_reason
->osr_kcd_buf
, peri
->eri_kcd_buf
, reason_data_size
);
3398 pberi
->beri_namespace
= p
->p_exit_reason
->osr_namespace
;
3399 pberi
->beri_code
= p
->p_exit_reason
->osr_code
;
3400 pberi
->beri_flags
= p
->p_exit_reason
->osr_flags
;
3401 pberi
->beri_reason_buf_size
= reason_data_size
;
3410 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
3411 * It mimics the data that is typically captured by the
3412 * EVFILT_PROC, NOTE_EXIT event mechanism.
3413 * See filt_proc() in kern_event.c.
3416 proc_pidnoteexit(proc_t p
, uint64_t flags
, uint32_t *data
)
3418 uint32_t exit_data
= 0;
3419 uint32_t exit_flags
= (uint32_t)flags
;
3424 * Allow access to the parent of the exiting
3425 * child or the parent debugger only.
3428 pid_t selfpid
= proc_selfpid();
3430 if (p
->p_ppid
== selfpid
) {
3431 break; /* parent => ok */
3433 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3434 (p
->p_oppid
== selfpid
)) {
3435 break; /* parent-in-waiting => ok */
3441 if ((exit_flags
& NOTE_EXITSTATUS
) != 0) {
3442 /* The signal and exit status */
3443 exit_data
|= (p
->p_xstat
& NOTE_PDATAMASK
);
3446 if ((exit_flags
& NOTE_EXIT_DETAIL
) != 0) {
3447 /* The exit detail */
3448 if ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) != 0) {
3449 exit_data
|= NOTE_EXIT_DECRYPTFAIL
;
3452 if ((p
->p_lflag
& P_LTERM_JETSAM
) != 0) {
3453 exit_data
|= NOTE_EXIT_MEMORY
;
3455 switch (p
->p_lflag
& P_JETSAM_MASK
) {
3456 case P_JETSAM_VMPAGESHORTAGE
:
3457 exit_data
|= NOTE_EXIT_MEMORY_VMPAGESHORTAGE
;
3459 case P_JETSAM_VMTHRASHING
:
3460 exit_data
|= NOTE_EXIT_MEMORY_VMTHRASHING
;
3462 case P_JETSAM_FCTHRASHING
:
3463 exit_data
|= NOTE_EXIT_MEMORY_FCTHRASHING
;
3465 case P_JETSAM_VNODE
:
3466 exit_data
|= NOTE_EXIT_MEMORY_VNODE
;
3468 case P_JETSAM_HIWAT
:
3469 exit_data
|= NOTE_EXIT_MEMORY_HIWAT
;
3472 exit_data
|= NOTE_EXIT_MEMORY_PID
;
3474 case P_JETSAM_IDLEEXIT
:
3475 exit_data
|= NOTE_EXIT_MEMORY_IDLE
;
3480 if ((p
->p_csflags
& CS_KILLED
) != 0) {
3481 exit_data
|= NOTE_EXIT_CSERROR
;
3493 proc_piddynkqueueinfo(int pid
, int flavor
, kqueue_id_t kq_id
,
3494 user_addr_t ubuf
, uint32_t bufsize
, int32_t *retval
)
3499 if (ubuf
== USER_ADDR_NULL
) {
3504 if (p
== PROC_NULL
) {
3508 err
= proc_security_policy(p
, PROC_INFO_CALL_PIDDYNKQUEUEINFO
, 0, CHECK_SAME_USER
);
3514 case PROC_PIDDYNKQUEUE_INFO
:
3515 err
= kevent_copyout_dynkqinfo(p
, kq_id
, ubuf
, bufsize
, retval
);
3517 case PROC_PIDDYNKQUEUE_EXTINFO
:
3518 err
= kevent_copyout_dynkqextinfo(p
, kq_id
, ubuf
, bufsize
, retval
);
3531 #if !CONFIG_EMBEDDED
3533 proc_udata_info(int pid
, int flavor
, user_addr_t buffer
, uint32_t bufsize
, int32_t *retval
)
3539 if (p
== PROC_NULL
) {
3544 * Only support calls against oneself for the moment.
3546 if (p
->p_pid
!= proc_selfpid()) {
3551 if (bufsize
!= sizeof(p
->p_user_data
)) {
3557 case PROC_UDATA_INFO_SET
:
3558 err
= copyin(buffer
, &p
->p_user_data
, sizeof(p
->p_user_data
));
3560 case PROC_UDATA_INFO_GET
:
3561 err
= copyout(&p
->p_user_data
, buffer
, sizeof(p
->p_user_data
));
3577 #endif /* !CONFIG_EMBEDDED */