2 * Copyright (c) 2005-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/proc_internal.h>
38 #include <sys/kauth.h>
39 #include <sys/file_internal.h>
40 #include <sys/vnode_internal.h>
41 #include <sys/unistd.h>
43 #include <sys/ioctl.h>
44 #include <sys/namei.h>
46 #include <sys/disklabel.h>
48 #include <sys/reason.h>
49 #include <sys/sysctl.h>
51 #include <sys/aio_kern.h>
52 #include <sys/kern_memorystatus.h>
54 #include <security/audit/audit.h>
56 #include <mach/machine.h>
57 #include <mach/mach_types.h>
58 #include <mach/vm_param.h>
59 #include <kern/task.h>
60 #include <kern/kalloc.h>
61 #include <kern/assert.h>
62 #include <kern/policy_internal.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
66 #include <mach/host_info.h>
67 #include <mach/task_info.h>
68 #include <mach/thread_info.h>
69 #include <mach/vm_region.h>
71 #include <sys/mount_internal.h>
72 #include <sys/proc_info.h>
73 #include <sys/bsdtask_info.h>
74 #include <sys/kdebug.h>
75 #include <sys/sysproto.h>
76 #include <sys/msgbuf.h>
79 #include <sys/guarded.h>
81 #include <machine/machine_routines.h>
83 #include <kern/ipc_misc.h>
85 #include <vm/vm_protos.h>
87 /* Needed by proc_pidnoteexit(), proc_pidlistuptrs() */
88 #include <sys/event.h>
89 #include <sys/codesign.h>
91 /* Needed by proc_listcoalitions() */
92 #ifdef CONFIG_COALITIONS
93 #include <sys/coalition.h>
97 #include <security/mac_framework.h>
106 uint64_t get_dispatchqueue_offset_from_proc(void *);
107 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *);
108 uint64_t get_return_to_kernel_offset_from_proc(void *p
);
109 int proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
112 * TODO: Replace the noinline attribute below. Currently, it serves
113 * to avoid stack bloat caused by inlining multiple functions that
114 * have large stack footprints; when the functions are independent
115 * of each other (will not both be called in any given call to the
116 * caller), this only serves to bloat the stack, as we allocate
117 * space for both functions, despite the fact that we only need a
118 * fraction of that space.
120 * Long term, these functions should not be allocating everything on
121 * the stack, and should move large allocations (the huge structs
122 * that proc info deals in) to the heap, or eliminate them if
125 * The functions that most desperately need to improve stack usage
126 * (starting with the worst offenders):
127 * proc_pidvnodepathinfo
129 * proc_pidregionpathinfo
135 * proc_pidoriginatorinfo
138 /* protos for proc_info calls */
139 int __attribute__ ((noinline
)) proc_listpids(uint32_t type
, uint32_t tyoneinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
140 int __attribute__ ((noinline
)) proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
141 int __attribute__ ((noinline
)) proc_pidfdinfo(int pid
, int flavor
,int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
142 int __attribute__ ((noinline
)) proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
143 int __attribute__ ((noinline
)) proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
144 int __attribute__ ((noinline
)) proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
145 int __attribute__ ((noinline
)) proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t * retval
);
146 int __attribute__ ((noinline
)) proc_terminate(int pid
, int32_t * retval
);
147 int __attribute__ ((noinline
)) proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, int32_t * retval
);
148 int __attribute__ ((noinline
)) proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
149 int __attribute__ ((noinline
)) proc_listcoalitions(int flavor
, int coaltype
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
150 int __attribute__ ((noinline
)) proc_can_use_foreground_hw(int pid
, user_addr_t reason
, uint32_t resonsize
, int32_t *retval
);
152 /* protos for procpidinfo calls */
153 int __attribute__ ((noinline
)) proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
154 int __attribute__ ((noinline
)) proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
*pbsd
, int zombie
);
155 int __attribute__ ((noinline
)) proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
*pbsd_shortp
, int zombie
);
156 int __attribute__ ((noinline
)) proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
*ptinfo
);
157 int __attribute__ ((noinline
)) proc_pidthreadinfo(proc_t p
, uint64_t arg
, int thuniqueid
, struct proc_threadinfo
*pthinfo
);
158 int __attribute__ ((noinline
)) proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
);
159 int __attribute__ ((noinline
)) proc_pidlistthreads(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
160 int __attribute__ ((noinline
)) proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
161 int __attribute__ ((noinline
)) proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
162 int __attribute__ ((noinline
)) proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
163 int __attribute__ ((noinline
)) proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
164 int __attribute__ ((noinline
)) proc_pidvnodepathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
165 int __attribute__ ((noinline
)) proc_pidpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
166 int __attribute__ ((noinline
)) proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
);
167 int __attribute__ ((noinline
)) proc_pidfileportlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
168 void __attribute__ ((noinline
)) proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
169 void __attribute__ ((noinline
)) proc_archinfo(proc_t p
, struct proc_archinfo
*pai
);
170 void __attribute__ ((noinline
)) proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*pci
);
171 int __attribute__ ((noinline
)) proc_pidnoteexit(proc_t p
, uint64_t arg
, uint32_t *data
);
172 int __attribute__ ((noinline
)) proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
);
173 int __attribute__ ((noinline
)) proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
);
174 int __attribute__ ((noinline
)) proc_pidlistuptrs(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
175 int __attribute__ ((noinline
)) proc_piddynkqueueinfo(pid_t pid
, int flavor
, kqueue_id_t id
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
178 int __attribute__ ((noinline
)) proc_udata_info(pid_t pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
181 /* protos for proc_pidfdinfo calls */
182 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
);
183 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
);
184 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
);
185 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
);
186 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
);
187 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
);
188 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
);
189 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
);
192 /* protos for misc */
194 int fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
);
195 void fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* finfo
);
196 int proc_security_policy(proc_t targetp
, int callnum
, int flavor
, boolean_t check_same_user
);
197 static void munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
);
198 static int proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
);
199 int proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
);
201 extern int cansignal(struct proc
*, kauth_cred_t
, struct proc
*, int);
202 extern int proc_get_rusage(proc_t proc
, int flavor
, user_addr_t buffer
, int is_zombie
);
204 #define CHECK_SAME_USER TRUE
205 #define NO_CHECK_SAME_USER FALSE
207 uint64_t get_dispatchqueue_offset_from_proc(void *p
)
210 proc_t pself
= (proc_t
)p
;
211 return (pself
->p_dispatchqueue_offset
);
217 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p
)
220 proc_t pself
= (proc_t
)p
;
221 return (pself
->p_dispatchqueue_serialno_offset
);
227 uint64_t get_return_to_kernel_offset_from_proc(void *p
)
230 proc_t pself
= (proc_t
)p
;
231 return (pself
->p_return_to_kernel_offset
);
237 /***************************** proc_info ********************/
240 proc_info(__unused
struct proc
*p
, struct proc_info_args
* uap
, int32_t *retval
)
242 return(proc_info_internal(uap
->callnum
, uap
->pid
, uap
->flavor
, uap
->arg
, uap
->buffer
, uap
->buffersize
, retval
));
247 proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
251 case PROC_INFO_CALL_LISTPIDS
:
252 /* pid contains type and flavor contains typeinfo */
253 return(proc_listpids(pid
, flavor
, buffer
, buffersize
, retval
));
254 case PROC_INFO_CALL_PIDINFO
:
255 return(proc_pidinfo(pid
, flavor
, arg
, buffer
, buffersize
, retval
));
256 case PROC_INFO_CALL_PIDFDINFO
:
257 return(proc_pidfdinfo(pid
, flavor
, (int)arg
, buffer
, buffersize
, retval
));
258 case PROC_INFO_CALL_KERNMSGBUF
:
259 return(proc_kernmsgbuf(buffer
, buffersize
, retval
));
260 case PROC_INFO_CALL_SETCONTROL
:
261 return(proc_setcontrol(pid
, flavor
, arg
, buffer
, buffersize
, retval
));
262 case PROC_INFO_CALL_PIDFILEPORTINFO
:
263 return(proc_pidfileportinfo(pid
, flavor
, (mach_port_name_t
)arg
, buffer
, buffersize
, retval
));
264 case PROC_INFO_CALL_TERMINATE
:
265 return(proc_terminate(pid
, retval
));
266 case PROC_INFO_CALL_DIRTYCONTROL
:
267 return(proc_dirtycontrol(pid
, flavor
, arg
, retval
));
268 case PROC_INFO_CALL_PIDRUSAGE
:
269 return (proc_pid_rusage(pid
, flavor
, buffer
, retval
));
270 case PROC_INFO_CALL_PIDORIGINATORINFO
:
271 return (proc_pidoriginatorinfo(pid
, flavor
, buffer
, buffersize
, retval
));
272 case PROC_INFO_CALL_LISTCOALITIONS
:
273 return proc_listcoalitions(pid
/* flavor */, flavor
/* coaltype */, buffer
,
275 case PROC_INFO_CALL_CANUSEFGHW
:
276 return proc_can_use_foreground_hw(pid
, buffer
, buffersize
, retval
);
277 case PROC_INFO_CALL_PIDDYNKQUEUEINFO
:
278 return proc_piddynkqueueinfo(pid
, flavor
, (kqueue_id_t
)arg
, buffer
, buffersize
, retval
);
280 case PROC_INFO_CALL_UDATA_INFO
:
281 return proc_udata_info(pid
, flavor
, buffer
, buffersize
, retval
);
282 #endif /* !CONFIG_EMBEDDED */
290 /******************* proc_listpids routine ****************/
292 proc_listpids(uint32_t type
, uint32_t typeinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
294 uint32_t numprocs
= 0;
303 struct proclist
*current_list
;
305 /* Do we have permission to look into this? */
306 if ((error
= proc_security_policy(PROC_NULL
, PROC_INFO_CALL_LISTPIDS
, type
, NO_CHECK_SAME_USER
)))
309 /* if the buffer is null, return num of procs */
310 if (buffer
== (user_addr_t
)0) {
311 *retval
= ((nprocs
+ 20) * sizeof(int));
315 if (buffersize
< sizeof(int)) {
318 wantpids
= buffersize
/sizeof(int);
319 if ((nprocs
+ 20) > 0) {
320 numprocs
= (uint32_t)(nprocs
+ 20);
322 if (numprocs
> wantpids
) {
326 kbuf
= (char *)kalloc((vm_size_t
)(numprocs
* sizeof(int)));
330 bzero(kbuf
, sizeof(int));
337 current_list
= &allproc
;
339 LIST_FOREACH(p
, current_list
, p_list
) {
343 if (p
->p_pgrpid
!= (pid_t
)typeinfo
)
347 if ((p
->p_ppid
!= (pid_t
)typeinfo
) && (((p
->p_lflag
& P_LTRACED
) == 0) || (p
->p_oppid
!= (pid_t
)typeinfo
)))
355 /* racy but list lock is held */
356 if ((p
->p_flag
& P_CONTROLT
) == 0 ||
357 (p
->p_pgrp
== NULL
) || (p
->p_pgrp
->pg_session
== NULL
) ||
358 (tp
= SESSION_TP(p
->p_pgrp
->pg_session
)) == TTY_NULL
||
359 tp
->t_dev
!= (dev_t
)typeinfo
)
363 if (p
->p_ucred
== NULL
)
366 kauth_cred_t my_cred
;
369 my_cred
= kauth_cred_proc_ref(p
);
370 uid
= kauth_cred_getuid(my_cred
);
371 kauth_cred_unref(&my_cred
);
372 if (uid
!= (uid_t
)typeinfo
)
377 if (p
->p_ucred
== NULL
)
380 kauth_cred_t my_cred
;
383 my_cred
= kauth_cred_proc_ref(p
);
384 uid
= kauth_cred_getruid(my_cred
);
385 kauth_cred_unref(&my_cred
);
386 if (uid
!= (uid_t
)typeinfo
)
391 if (p
->p_kdebug
== 0) {
408 if ((n
< numprocs
) && (current_list
== &allproc
)) {
409 current_list
= &zombproc
;
416 error
= copyout((caddr_t
)ptr
, buffer
, n
* sizeof(int));
418 *retval
= (n
* sizeof(int));
419 kfree((void *)kbuf
, (vm_size_t
)(numprocs
* sizeof(int)));
425 /********************************** proc_pidfdlist routines ********************************/
428 proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
433 struct proc_fdinfo
* pfd
;
434 struct fileproc
* fp
;
439 if (p
->p_fd
->fd_nfiles
> 0) {
440 numfds
= (uint32_t)p
->p_fd
->fd_nfiles
;
443 if (buffer
== (user_addr_t
) 0) {
445 *retval
= (numfds
* sizeof(struct proc_fdinfo
));
449 /* buffersize is big enough atleast for one struct */
450 needfds
= buffersize
/sizeof(struct proc_fdinfo
);
452 if (numfds
> needfds
) {
456 kbuf
= (char *)kalloc((vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
459 bzero(kbuf
, numfds
* sizeof(struct proc_fdinfo
));
463 pfd
= (struct proc_fdinfo
*)kbuf
;
465 for (n
= 0; ((n
< (int)numfds
) && (n
< p
->p_fd
->fd_nfiles
)); n
++) {
466 if (((fp
= p
->p_fd
->fd_ofiles
[n
]) != 0)
467 && ((p
->p_fd
->fd_ofileflags
[n
] & UF_RESERVED
) == 0)) {
468 file_type_t fdtype
= FILEGLOB_DTYPE(fp
->f_fglob
);
470 pfd
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
471 fdtype
: PROX_FDTYPE_ATALK
;
478 error
= copyout(kbuf
, buffer
, count
* sizeof(struct proc_fdinfo
));
479 kfree((void *)kbuf
, (vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
481 *retval
= (count
* sizeof(struct proc_fdinfo
));
486 * Helper functions for proc_pidfileportlist.
489 proc_fileport_count(__unused mach_port_name_t name
,
490 __unused
struct fileglob
*fg
, void *arg
)
492 uint32_t *counter
= arg
;
498 struct fileport_fdtype_args
{
499 struct proc_fileportinfo
*ffa_pfi
;
500 struct proc_fileportinfo
*ffa_pfi_end
;
504 proc_fileport_fdtype(mach_port_name_t name
, struct fileglob
*fg
, void *arg
)
506 struct fileport_fdtype_args
*ffa
= arg
;
508 if (ffa
->ffa_pfi
!= ffa
->ffa_pfi_end
) {
509 file_type_t fdtype
= FILEGLOB_DTYPE(fg
);
511 ffa
->ffa_pfi
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
512 fdtype
: PROX_FDTYPE_ATALK
;
513 ffa
->ffa_pfi
->proc_fileport
= name
;
515 return (0); /* keep walking */
517 return (-1); /* stop the walk! */
521 proc_pidfileportlist(proc_t p
,
522 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
526 struct proc_fileportinfo
*pfi
;
527 uint32_t needfileports
, numfileports
;
528 struct fileport_fdtype_args ffa
;
531 needfileports
= buffersize
/ sizeof (*pfi
);
532 if ((user_addr_t
)0 == buffer
|| needfileports
> (uint32_t)maxfiles
) {
534 * Either (i) the user is asking for a fileport count,
535 * or (ii) the number of fileports they're asking for is
536 * larger than the maximum number of open files (!); count
537 * them to bound subsequent heap allocations.
540 switch (fileport_walk(p
->task
,
541 proc_fileport_count
, &numfileports
)) {
544 case KERN_RESOURCE_SHORTAGE
:
546 case KERN_INVALID_TASK
:
552 if (numfileports
== 0) {
553 *retval
= 0; /* none at all, bail */
556 if ((user_addr_t
)0 == buffer
) {
557 numfileports
+= 20; /* accelerate convergence */
558 *retval
= numfileports
* sizeof (*pfi
);
561 if (needfileports
> numfileports
)
562 needfileports
= numfileports
;
565 assert(buffersize
>= PROC_PIDLISTFILEPORTS_SIZE
);
567 kbufsize
= (vm_size_t
)needfileports
* sizeof (*pfi
);
568 pfi
= kbuf
= kalloc(kbufsize
);
571 bzero(kbuf
, kbufsize
);
574 ffa
.ffa_pfi_end
= pfi
+ needfileports
;
576 switch (fileport_walk(p
->task
, proc_fileport_fdtype
, &ffa
)) {
580 if ((numfileports
= pfi
- (typeof(pfi
))kbuf
) == 0)
582 if (numfileports
> needfileports
)
583 panic("more fileports returned than requested");
584 error
= copyout(kbuf
, buffer
, numfileports
* sizeof (*pfi
));
586 case KERN_RESOURCE_SHORTAGE
:
589 case KERN_INVALID_TASK
:
596 kfree(kbuf
, kbufsize
);
598 *retval
= numfileports
* sizeof (*pfi
);
603 proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
* pbsd
, int zombie
)
606 struct session
*sessionp
= NULL
;
608 kauth_cred_t my_cred
;
611 sessionp
= proc_session(p
);
613 my_cred
= kauth_cred_proc_ref(p
);
614 bzero(pbsd
, sizeof(struct proc_bsdinfo
));
615 pbsd
->pbi_status
= p
->p_stat
;
616 pbsd
->pbi_xstatus
= p
->p_xstat
;
617 pbsd
->pbi_pid
= p
->p_pid
;
618 pbsd
->pbi_ppid
= p
->p_ppid
;
619 pbsd
->pbi_uid
= kauth_cred_getuid(my_cred
);
620 pbsd
->pbi_gid
= kauth_cred_getgid(my_cred
);
621 pbsd
->pbi_ruid
= kauth_cred_getruid(my_cred
);
622 pbsd
->pbi_rgid
= kauth_cred_getrgid(my_cred
);
623 pbsd
->pbi_svuid
= kauth_cred_getsvuid(my_cred
);
624 pbsd
->pbi_svgid
= kauth_cred_getsvgid(my_cred
);
625 kauth_cred_unref(&my_cred
);
627 pbsd
->pbi_nice
= p
->p_nice
;
628 pbsd
->pbi_start_tvsec
= p
->p_start
.tv_sec
;
629 pbsd
->pbi_start_tvusec
= p
->p_start
.tv_usec
;
630 bcopy(&p
->p_comm
, &pbsd
->pbi_comm
[0], MAXCOMLEN
);
631 pbsd
->pbi_comm
[MAXCOMLEN
- 1] = '\0';
632 bcopy(&p
->p_name
, &pbsd
->pbi_name
[0], 2*MAXCOMLEN
);
633 pbsd
->pbi_name
[(2*MAXCOMLEN
) - 1] = '\0';
636 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
)
637 pbsd
->pbi_flags
|= PROC_FLAG_SYSTEM
;
638 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
)
639 pbsd
->pbi_flags
|= PROC_FLAG_TRACED
;
640 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
)
641 pbsd
->pbi_flags
|= PROC_FLAG_INEXIT
;
642 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
)
643 pbsd
->pbi_flags
|= PROC_FLAG_PPWAIT
;
644 if ((p
->p_flag
& P_LP64
) == P_LP64
)
645 pbsd
->pbi_flags
|= PROC_FLAG_LP64
;
646 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
)
647 pbsd
->pbi_flags
|= PROC_FLAG_CONTROLT
;
648 if ((p
->p_flag
& P_THCWD
) == P_THCWD
)
649 pbsd
->pbi_flags
|= PROC_FLAG_THCWD
;
650 if ((p
->p_flag
& P_SUGID
) == P_SUGID
)
651 pbsd
->pbi_flags
|= PROC_FLAG_PSUGID
;
652 if ((p
->p_flag
& P_EXEC
) == P_EXEC
)
653 pbsd
->pbi_flags
|= PROC_FLAG_EXEC
;
655 if (sessionp
!= SESSION_NULL
) {
656 if (SESS_LEADER(p
, sessionp
))
657 pbsd
->pbi_flags
|= PROC_FLAG_SLEADER
;
658 if (sessionp
->s_ttyvp
)
659 pbsd
->pbi_flags
|= PROC_FLAG_CTTY
;
663 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
664 pbsd
->pbi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
665 #endif /* !CONFIG_EMBEDDED */
667 switch(PROC_CONTROL_STATE(p
)) {
669 pbsd
->pbi_flags
|= PROC_FLAG_PC_THROTTLE
;
672 pbsd
->pbi_flags
|= PROC_FLAG_PC_SUSP
;
675 pbsd
->pbi_flags
|= PROC_FLAG_PC_KILL
;
679 switch(PROC_ACTION_STATE(p
)) {
681 pbsd
->pbi_flags
|= PROC_FLAG_PA_THROTTLE
;
684 pbsd
->pbi_flags
|= PROC_FLAG_PA_SUSP
;
688 /* if process is a zombie skip bg state */
689 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
))
690 proc_get_darwinbgstate(p
->task
, &pbsd
->pbi_flags
);
693 pbsd
->pbi_nfiles
= p
->p_fd
->fd_nfiles
;
695 pbsd
->e_tdev
= NODEV
;
696 if (pg
!= PGRP_NULL
) {
697 pbsd
->pbi_pgid
= p
->p_pgrpid
;
698 pbsd
->pbi_pjobc
= pg
->pg_jobc
;
699 if ((p
->p_flag
& P_CONTROLT
) && (sessionp
!= SESSION_NULL
) && (tp
= SESSION_TP(sessionp
))) {
700 pbsd
->e_tdev
= tp
->t_dev
;
701 pbsd
->e_tpgid
= sessionp
->s_ttypgrpid
;
704 if (sessionp
!= SESSION_NULL
)
705 session_rele(sessionp
);
714 proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
* pbsd_shortp
, int zombie
)
716 bzero(pbsd_shortp
, sizeof(struct proc_bsdshortinfo
));
717 pbsd_shortp
->pbsi_pid
= p
->p_pid
;
718 pbsd_shortp
->pbsi_ppid
= p
->p_ppid
;
719 pbsd_shortp
->pbsi_pgid
= p
->p_pgrpid
;
720 pbsd_shortp
->pbsi_status
= p
->p_stat
;
721 bcopy(&p
->p_comm
, &pbsd_shortp
->pbsi_comm
[0], MAXCOMLEN
);
722 pbsd_shortp
->pbsi_comm
[MAXCOMLEN
- 1] = '\0';
724 pbsd_shortp
->pbsi_flags
= 0;
725 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
)
726 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_SYSTEM
;
727 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
)
728 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_TRACED
;
729 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
)
730 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_INEXIT
;
731 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
)
732 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PPWAIT
;
733 if ((p
->p_flag
& P_LP64
) == P_LP64
)
734 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_LP64
;
735 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
)
736 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_CONTROLT
;
737 if ((p
->p_flag
& P_THCWD
) == P_THCWD
)
738 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_THCWD
;
739 if ((p
->p_flag
& P_SUGID
) == P_SUGID
)
740 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PSUGID
;
741 if ((p
->p_flag
& P_EXEC
) == P_EXEC
)
742 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_EXEC
;
744 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
745 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
746 #endif /* !CONFIG_EMBEDDED */
748 switch(PROC_CONTROL_STATE(p
)) {
750 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_THROTTLE
;
753 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_SUSP
;
756 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_KILL
;
760 switch(PROC_ACTION_STATE(p
)) {
762 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_THROTTLE
;
765 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_SUSP
;
769 /* if process is a zombie skip bg state */
770 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
))
771 proc_get_darwinbgstate(p
->task
, &pbsd_shortp
->pbsi_flags
);
773 pbsd_shortp
->pbsi_uid
= p
->p_uid
;
774 pbsd_shortp
->pbsi_gid
= p
->p_gid
;
775 pbsd_shortp
->pbsi_ruid
= p
->p_ruid
;
776 pbsd_shortp
->pbsi_rgid
= p
->p_rgid
;
777 pbsd_shortp
->pbsi_svuid
= p
->p_svuid
;
778 pbsd_shortp
->pbsi_svgid
= p
->p_svgid
;
784 proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
* ptinfo
)
790 bzero(ptinfo
, sizeof(struct proc_taskinfo
));
791 fill_taskprocinfo(task
, (struct proc_taskinfo_internal
*)ptinfo
);
799 proc_pidthreadinfo(proc_t p
, uint64_t arg
, int thuniqueid
, struct proc_threadinfo
*pthinfo
)
802 uint64_t threadaddr
= (uint64_t)arg
;
804 bzero(pthinfo
, sizeof(struct proc_threadinfo
));
806 error
= fill_taskthreadinfo(p
->task
, threadaddr
, thuniqueid
, (struct proc_threadinfo_internal
*)pthinfo
, NULL
, NULL
);
815 bsd_hasthreadname(void *uth
)
817 struct uthread
*ut
= (struct uthread
*)uth
;
819 /* This doesn't check for the empty string; do we care? */
828 bsd_getthreadname(void *uth
, char *buffer
)
830 struct uthread
*ut
= (struct uthread
*)uth
;
832 bcopy(ut
->pth_name
,buffer
,MAXTHREADNAMESIZE
);
836 * This is known to race with regards to the contents of the thread name; concurrent
837 * callers may result in a garbled name.
840 bsd_setthreadname(void *uth
, const char *name
) {
841 struct uthread
*ut
= (struct uthread
*)uth
;
842 char * name_buf
= NULL
;
845 /* If there is no existing thread name, allocate a buffer for one. */
846 name_buf
= kalloc(MAXTHREADNAMESIZE
);
848 bzero(name_buf
, MAXTHREADNAMESIZE
);
850 /* Someone could conceivably have named the thread at the same time we did. */
851 if (!OSCompareAndSwapPtr(NULL
, name_buf
, &ut
->pth_name
)) {
852 kfree(name_buf
, MAXTHREADNAMESIZE
);
855 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV
, ut
->pth_name
);
858 strncpy(ut
->pth_name
, name
, MAXTHREADNAMESIZE
- 1);
859 kernel_debug_string_simple(TRACE_STRING_THREADNAME
, ut
->pth_name
);
863 bsd_copythreadname(void *dst_uth
, void *src_uth
)
865 struct uthread
*dst_ut
= (struct uthread
*)dst_uth
;
866 struct uthread
*src_ut
= (struct uthread
*)src_uth
;
868 if (src_ut
->pth_name
== NULL
)
871 if (dst_ut
->pth_name
== NULL
) {
872 dst_ut
->pth_name
= (char *)kalloc(MAXTHREADNAMESIZE
);
873 if (dst_ut
->pth_name
== NULL
)
877 bcopy(src_ut
->pth_name
, dst_ut
->pth_name
, MAXTHREADNAMESIZE
);
882 bsd_threadcdir(void * uth
, void *vptr
, int *vidp
)
884 struct uthread
* ut
= (struct uthread
*)uth
;
886 vnode_t
*vpp
= (vnode_t
*)vptr
;
900 proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
)
905 uint64_t threadaddr
= (uint64_t)arg
;
908 bzero(pinfo
, sizeof(struct proc_threadwithpathinfo
));
910 error
= fill_taskthreadinfo(p
->task
, threadaddr
, 0, (struct proc_threadinfo_internal
*)&pinfo
->pt
, (void *)&vp
, &vid
);
914 if ((vp
!= NULLVP
) && ((vnode_getwithvid(vp
, vid
)) == 0)) {
915 error
= fill_vnodeinfo(vp
, &pinfo
->pvip
.vip_vi
) ;
918 vn_getpath(vp
, &pinfo
->pvip
.vip_path
[0], &count
);
919 pinfo
->pvip
.vip_path
[MAXPATHLEN
-1] = 0;
929 proc_pidlistthreads(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
935 uint32_t numthreads
= 0;
937 int num
= get_numthreads(p
->task
) + 10;
939 numthreads
= (uint32_t)num
;
942 count
= buffersize
/(sizeof(uint64_t));
944 if (numthreads
> count
) {
948 kbuf
= (void *)kalloc(numthreads
* sizeof(uint64_t));
951 bzero(kbuf
, numthreads
* sizeof(uint64_t));
953 ret
= fill_taskthreadlist(p
->task
, kbuf
, numthreads
);
955 error
= copyout(kbuf
, buffer
, ret
);
956 kfree(kbuf
, numthreads
* sizeof(uint64_t));
965 proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
967 struct proc_regioninfo preginfo
;
970 bzero(&preginfo
, sizeof(struct proc_regioninfo
));
971 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
, (uintptr_t *)0, (uint32_t *)0);
974 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regioninfo
));
976 *retval
= sizeof(struct proc_regioninfo
);
982 proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
984 struct proc_regionwithpathinfo preginfo
;
986 uintptr_t vnodeaddr
= 0;
991 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
993 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
997 vp
= (vnode_t
)vnodeaddr
;
998 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
999 /* FILL THE VNODEINFO */
1000 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1002 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1003 /* Always make sure it is null terminated */
1004 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
1008 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1010 *retval
= sizeof(struct proc_regionwithpathinfo
);
1015 proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1017 struct proc_regionwithpathinfo preginfo
;
1019 uintptr_t vnodeaddr
= 0;
1020 uint32_t vnodeid
= 0;
1024 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1026 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1032 vp
= (vnode_t
)vnodeaddr
;
1033 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1034 /* FILL THE VNODEINFO */
1035 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1037 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1038 /* Always make sure it is null terminated */
1039 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
1045 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1047 *retval
= sizeof(struct proc_regionwithpathinfo
);
1052 proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1054 struct proc_regionwithpathinfo preginfo
;
1056 uintptr_t vnodeaddr
;
1062 /* Loop while looking for vnodes that match dev_t filter */
1064 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1068 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, addr
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1074 vp
= (vnode_t
)vnodeaddr
;
1075 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1076 /* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
1077 struct vnode_attr va
;
1079 memset(&va
, 0, sizeof(va
));
1081 VATTR_WANTED(&va
, va_fsid
);
1083 ret
= vnode_getattr(vp
, &va
, vfs_context_current());
1089 if (va
.va_fsid
== arg
) {
1090 /* FILL THE VNODEINFO */
1091 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1093 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1094 /* Always make sure it is null terminated */
1095 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
1104 addr
= preginfo
.prp_prinfo
.pri_address
+ preginfo
.prp_prinfo
.pri_size
;
1107 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1109 *retval
= sizeof(struct proc_regionwithpathinfo
);
1114 * Path is relative to current process directory; may different from current
1118 proc_pidvnodepathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1120 struct proc_vnodepathinfo pvninfo
;
1122 vnode_t vncdirvp
= NULLVP
;
1123 uint32_t vncdirid
=0;
1124 vnode_t vnrdirvp
= NULLVP
;
1125 uint32_t vnrdirid
=0;
1128 bzero(&pvninfo
, sizeof(struct proc_vnodepathinfo
));
1131 if (p
->p_fd
->fd_cdir
) {
1132 vncdirvp
= p
->p_fd
->fd_cdir
;
1133 vncdirid
= p
->p_fd
->fd_cdir
->v_id
;
1135 if (p
->p_fd
->fd_rdir
) {
1136 vnrdirvp
= p
->p_fd
->fd_rdir
;
1137 vnrdirid
= p
->p_fd
->fd_rdir
->v_id
;
1141 if (vncdirvp
!= NULLVP
) {
1142 if ((error
= vnode_getwithvid(vncdirvp
, vncdirid
)) == 0) {
1143 /* FILL THE VNODEINFO */
1144 error
= fill_vnodeinfo(vncdirvp
, &pvninfo
.pvi_cdir
.vip_vi
);
1147 vn_getpath(vncdirvp
, &pvninfo
.pvi_cdir
.vip_path
[0], &count
);
1148 pvninfo
.pvi_cdir
.vip_path
[MAXPATHLEN
-1] = 0;
1150 vnode_put(vncdirvp
);
1156 if ((error
== 0) && (vnrdirvp
!= NULLVP
)) {
1157 if ((error
= vnode_getwithvid(vnrdirvp
, vnrdirid
)) == 0) {
1158 /* FILL THE VNODEINFO */
1159 error
= fill_vnodeinfo(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_vi
);
1162 vn_getpath(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_path
[0], &count
);
1163 pvninfo
.pvi_rdir
.vip_path
[MAXPATHLEN
-1] = 0;
1165 vnode_put(vnrdirvp
);
1171 error
= copyout(&pvninfo
, buffer
, sizeof(struct proc_vnodepathinfo
));
1173 *retval
= sizeof(struct proc_vnodepathinfo
);
1180 proc_pidpathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t *retval
)
1184 int len
= buffersize
;
1192 buf
= (char *)kalloc(buffersize
);
1196 bzero(buf
, buffersize
);
1198 error
= proc_pidpathinfo_internal(p
, arg
, buf
, buffersize
, retval
);
1200 error
= copyout(buf
, buffer
, len
);
1202 kfree(buf
, buffersize
);
1207 proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
)
1211 vnode_t nvp
= NULLVP
;
1212 int len
= buffersize
;
1219 vid
= vnode_vid(tvp
);
1220 error
= vnode_getwithvid(tvp
, vid
);
1222 error
= vn_getpath_fsenter(tvp
, buf
, &len
);
1225 error
= vnode_lookup(buf
, 0, &nvp
, vfs_context_current());
1226 if ((error
== 0) && ( nvp
!= NULLVP
))
1235 proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
)
1239 bzero(pwqinfo
, sizeof(struct proc_workqueueinfo
));
1241 error
= fill_procworkqueue(p
, pwqinfo
);
1251 proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
)
1253 p_uniqidinfo
->p_uniqueid
= proc_uniqueid(p
);
1254 proc_getexecutableuuid(p
, (unsigned char *)&p_uniqidinfo
->p_uuid
, sizeof(p_uniqidinfo
->p_uuid
));
1255 p_uniqidinfo
->p_puniqueid
= proc_puniqueid(p
);
1256 p_uniqidinfo
->p_reserve2
= 0;
1257 p_uniqidinfo
->p_reserve3
= 0;
1258 p_uniqidinfo
->p_reserve4
= 0;
1263 proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
)
1265 struct proc
* p
= PROC_NULL
;
1268 if (buffersize
< sizeof(uuid_t
))
1271 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1272 p
= proc_find_zombref(pid
);
1275 if (p
== PROC_NULL
) {
1279 proc_getexecutableuuid(p
, (unsigned char *)uuid_buf
, buffersize
);
1282 proc_drop_zombref(p
);
1290 * Function to get the uuid and pid of the originator of the voucher.
1293 proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
)
1295 pid_t originator_pid
;
1300 * Get the current voucher origin pid. The pid returned here
1301 * might not be valid or may have been recycled.
1303 kr
= thread_get_current_voucher_origin_pid(&originator_pid
);
1304 /* If errors, convert errors to appropriate format */
1306 if (kr
== KERN_INVALID_TASK
)
1308 else if (kr
== KERN_INVALID_VALUE
)
1315 *pid
= originator_pid
;
1316 error
= proc_piduuidinfo(originator_pid
, uuid
, buffersize
);
1321 * Function to get the uuid of the originator of the voucher.
1324 proc_pidoriginatoruuid(uuid_t uuid
, uint32_t buffersize
)
1326 pid_t originator_pid
;
1327 return (proc_pidoriginatorpid_uuid(uuid
, buffersize
, &originator_pid
));
1330 /***************************** proc_pidoriginatorinfo ***************************/
1333 proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1335 int error
= ENOTSUP
;
1339 case PROC_PIDORIGINATOR_UUID
:
1340 size
= PROC_PIDORIGINATOR_UUID_SIZE
;
1342 case PROC_PIDORIGINATOR_BGSTATE
:
1343 size
= PROC_PIDORIGINATOR_BGSTATE_SIZE
;
1345 case PROC_PIDORIGINATOR_PID_UUID
:
1346 size
= PROC_PIDORIGINATOR_PID_UUID_SIZE
;
1352 if (buffersize
< size
)
1355 if (pid
!= 0 && pid
!= proc_selfpid())
1359 case PROC_PIDORIGINATOR_UUID
: {
1362 error
= proc_pidoriginatoruuid(uuid
, sizeof(uuid
));
1366 error
= copyout(uuid
, buffer
, size
);
1372 case PROC_PIDORIGINATOR_PID_UUID
: {
1373 struct proc_originatorinfo originator_info
;
1374 bzero(&originator_info
, sizeof(originator_info
));
1376 error
= proc_pidoriginatorpid_uuid(originator_info
.originator_uuid
,
1377 sizeof(uuid_t
), &originator_info
.originator_pid
);
1381 error
= copyout(&originator_info
, buffer
, size
);
1387 case PROC_PIDORIGINATOR_BGSTATE
: {
1388 uint32_t is_backgrounded
;
1389 error
= proc_get_originatorbgstate(&is_backgrounded
);
1393 error
= copyout(&is_backgrounded
, buffer
, size
);
1406 /***************************** proc_listcoalitions ***************************/
1407 int proc_listcoalitions(int flavor
, int type
, user_addr_t buffer
,
1408 uint32_t buffersize
, int32_t *retval
)
1410 #if CONFIG_COALITIONS
1411 int error
= ENOTSUP
;
1414 void *coalinfo
= NULL
;
1415 uint32_t k_buffersize
= 0, copyout_sz
= 0;
1416 int ncoals
= 0, ncoals_
= 0;
1418 /* struct procinfo_coalinfo; */
1421 case LISTCOALITIONS_ALL_COALS
:
1422 elem_size
= LISTCOALITIONS_ALL_COALS_SIZE
;
1425 case LISTCOALITIONS_SINGLE_TYPE
:
1426 elem_size
= LISTCOALITIONS_SINGLE_TYPE_SIZE
;
1433 /* find the total number of coalitions */
1434 ncoals
= coalitions_get_list(coal_type
, NULL
, 0);
1436 if (ncoals
== 0 || buffer
== 0 || buffersize
== 0) {
1438 * user just wants buffer size
1439 * or there are no coalitions
1442 *retval
= (int)(ncoals
* elem_size
);
1446 k_buffersize
= ncoals
* elem_size
;
1447 coalinfo
= kalloc((vm_size_t
)k_buffersize
);
1452 bzero(coalinfo
, k_buffersize
);
1455 case LISTCOALITIONS_ALL_COALS
:
1456 case LISTCOALITIONS_SINGLE_TYPE
:
1457 ncoals_
= coalitions_get_list(coal_type
, coalinfo
, ncoals
);
1460 panic("memory corruption?!");
1464 /* all the coalitions disappeared... weird but valid */
1471 * Some coalitions may have disappeared between our initial check,
1472 * and the the actual list acquisition.
1473 * Only copy out what we really need.
1475 copyout_sz
= k_buffersize
;
1476 if (ncoals_
< ncoals
)
1477 copyout_sz
= ncoals_
* elem_size
;
1480 * copy the list up to user space
1481 * (we're guaranteed to have a non-null pointer/size here)
1483 error
= copyout(coalinfo
, buffer
,
1484 copyout_sz
< buffersize
? copyout_sz
: buffersize
);
1487 *retval
= (int)copyout_sz
;
1491 kfree(coalinfo
, k_buffersize
);
1495 /* no coalition support */
1506 /*************************** proc_can_use_forgeound_hw **************************/
1507 int proc_can_use_foreground_hw(int pid
, user_addr_t u_reason
, uint32_t reasonsize
, int32_t *retval
)
1509 proc_t p
= PROC_NULL
;
1511 uint32_t reason
= PROC_FGHW_ERROR
;
1513 task_t task
= TASK_NULL
;
1514 #if CONFIG_COALITIONS
1515 coalition_t coal
= COALITION_NULL
;
1522 reason
= PROC_FGHW_ERROR
;
1527 if (p
== PROC_NULL
) {
1529 reason
= PROC_FGHW_ERROR
;
1533 #if CONFIG_COALITIONS
1534 if (p
!= current_proc() &&
1535 !kauth_cred_issuser(kauth_cred_get())) {
1537 reason
= PROC_FGHW_ERROR
;
1542 task_reference(task
);
1543 if (coalition_is_leader(task
, COALITION_TYPE_JETSAM
, &coal
) == FALSE
) {
1544 /* current task is not a coalition leader: find the leader */
1545 task_deallocate(task
);
1546 task
= coalition_get_leader(coal
);
1549 if (task
!= TASK_NULL
) {
1551 * If task is non-null, then it is the coalition leader of the
1552 * current process' coalition. This could be the same task as
1553 * the current_task, and that's OK.
1558 proc_get_darwinbgstate(task
, &flags
);
1559 if ((flags
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
1561 * Coalition leader is not an application, continue
1562 * searching for other ways this task could gain
1565 reason
= PROC_FGHW_DAEMON_LEADER
;
1569 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
)) {
1571 * If the leader of the current process' coalition has
1572 * been marked as DARWIN_BG, then it definitely should
1573 * not be using foreground hardware resources.
1575 reason
= PROC_FGHW_LEADER_BACKGROUND
;
1579 role
= proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
);
1581 case TASK_FOREGROUND_APPLICATION
: /* DARWIN_ROLE_UI_FOCAL */
1582 case TASK_BACKGROUND_APPLICATION
: /* DARWIN_ROLE_UI */
1584 * The leader of this coalition is a focal, UI app:
1586 * TODO: should extensions/plugins be allowed to use
1590 reason
= PROC_FGHW_OK
;
1592 case TASK_DEFAULT_APPLICATION
: /* DARWIN_ROLE_UI_NON_FOCAL */
1593 case TASK_NONUI_APPLICATION
: /* DARWIN_ROLE_NON_UI */
1594 case TASK_THROTTLE_APPLICATION
:
1595 case TASK_UNSPECIFIED
:
1597 /* non-focal, non-ui apps don't get access */
1598 reason
= PROC_FGHW_LEADER_NONUI
;
1604 if (task
!= TASK_NULL
) {
1605 task_deallocate(task
);
1608 #endif /* CONFIG_COALITIONS */
1611 * There is no reasonable semantic to investigate the currently
1612 * adopted voucher of an arbitrary thread in a non-current process.
1615 if (p
!= current_proc()) {
1621 * In the absence of coalitions, fall back to a voucher-based lookup
1622 * where a daemon can used foreground HW if it's operating on behalf
1623 * of a foreground application.
1624 * NOTE: this is equivalent to a call to
1625 * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
1628 error
= proc_get_originatorbgstate(&isBG
);
1633 reason
= PROC_FGHW_NO_ORIGINATOR
;
1637 reason
= PROC_FGHW_NO_VOUCHER_ATTR
;
1641 reason
= PROC_FGHW_DAEMON_NO_VOUCHER
;
1645 /* some other error occurred: report that to the caller */
1646 reason
= PROC_FGHW_VOUCHER_ERROR
;
1651 reason
= PROC_FGHW_ORIGINATOR_BACKGROUND
;
1655 * The process itself is either a foreground app, or has
1656 * adopted a voucher originating from an app that's still in
1659 reason
= PROC_FGHW_DAEMON_OK
;
1664 if (task
!= TASK_NULL
)
1665 task_deallocate(task
);
1668 if (reasonsize
>= sizeof(reason
) && u_reason
!= (user_addr_t
)0)
1669 (void)copyout(&reason
, u_reason
, sizeof(reason
));
1674 /********************************** proc_pidinfo ********************************/
1678 proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1680 struct proc
* p
= PROC_NULL
;
1681 int error
= ENOTSUP
;
1684 int shortversion
= 0;
1688 int uniqidversion
= 0;
1689 boolean_t check_same_user
;
1692 case PROC_PIDLISTFDS
:
1693 size
= PROC_PIDLISTFD_SIZE
;
1694 if (buffer
== USER_ADDR_NULL
)
1697 case PROC_PIDTBSDINFO
:
1698 size
= PROC_PIDTBSDINFO_SIZE
;
1700 case PROC_PIDTASKINFO
:
1701 size
= PROC_PIDTASKINFO_SIZE
;
1703 case PROC_PIDTASKALLINFO
:
1704 size
= PROC_PIDTASKALLINFO_SIZE
;
1706 case PROC_PIDTHREADINFO
:
1707 size
= PROC_PIDTHREADINFO_SIZE
;
1709 case PROC_PIDLISTTHREADS
:
1710 size
= PROC_PIDLISTTHREADS_SIZE
;
1712 case PROC_PIDREGIONINFO
:
1713 size
= PROC_PIDREGIONINFO_SIZE
;
1715 case PROC_PIDREGIONPATHINFO
:
1716 size
= PROC_PIDREGIONPATHINFO_SIZE
;
1718 case PROC_PIDVNODEPATHINFO
:
1719 size
= PROC_PIDVNODEPATHINFO_SIZE
;
1721 case PROC_PIDTHREADPATHINFO
:
1722 size
= PROC_PIDTHREADPATHINFO_SIZE
;
1724 case PROC_PIDPATHINFO
:
1727 case PROC_PIDWORKQUEUEINFO
:
1728 /* kernel does not have workq info */
1732 size
= PROC_PIDWORKQUEUEINFO_SIZE
;
1734 case PROC_PIDT_SHORTBSDINFO
:
1735 size
= PROC_PIDT_SHORTBSDINFO_SIZE
;
1737 case PROC_PIDLISTFILEPORTS
:
1738 size
= PROC_PIDLISTFILEPORTS_SIZE
;
1739 if (buffer
== (user_addr_t
)0)
1742 case PROC_PIDTHREADID64INFO
:
1743 size
= PROC_PIDTHREADID64INFO_SIZE
;
1745 case PROC_PIDUNIQIDENTIFIERINFO
:
1746 size
= PROC_PIDUNIQIDENTIFIERINFO_SIZE
;
1748 case PROC_PIDT_BSDINFOWITHUNIQID
:
1749 size
= PROC_PIDT_BSDINFOWITHUNIQID_SIZE
;
1751 case PROC_PIDARCHINFO
:
1752 size
= PROC_PIDARCHINFO_SIZE
;
1754 case PROC_PIDCOALITIONINFO
:
1755 size
= PROC_PIDCOALITIONINFO_SIZE
;
1757 case PROC_PIDNOTEEXIT
:
1759 * Set findzomb explicitly because arg passed
1760 * in is used as note exit status bits.
1762 size
= PROC_PIDNOTEEXIT_SIZE
;
1765 case PROC_PIDEXITREASONINFO
:
1766 size
= PROC_PIDEXITREASONINFO_SIZE
;
1769 case PROC_PIDEXITREASONBASICINFO
:
1770 size
= PROC_PIDEXITREASONBASICINFOSIZE
;
1773 case PROC_PIDREGIONPATHINFO2
:
1774 size
= PROC_PIDREGIONPATHINFO2_SIZE
;
1776 case PROC_PIDREGIONPATHINFO3
:
1777 size
= PROC_PIDREGIONPATHINFO3_SIZE
;
1779 case PROC_PIDLISTUPTRS
:
1780 size
= PROC_PIDLISTUPTRS_SIZE
;
1781 if (buffer
== USER_ADDR_NULL
) {
1785 case PROC_PIDLISTDYNKQUEUES
:
1786 size
= PROC_PIDLISTDYNKQUEUES_SIZE
;
1787 if (buffer
== USER_ADDR_NULL
) {
1795 if (buffersize
< size
)
1798 if ((flavor
== PROC_PIDPATHINFO
) && (buffersize
> PROC_PIDPATHINFO_MAXSIZE
)) {
1802 /* Check if we need to look for zombies */
1803 if ((flavor
== PROC_PIDTBSDINFO
) || (flavor
== PROC_PIDT_SHORTBSDINFO
) || (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1804 || (flavor
== PROC_PIDUNIQIDENTIFIERINFO
)) {
1809 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1811 p
= proc_find_zombref(pid
);
1812 if (p
== PROC_NULL
) {
1821 /* Certain operations don't require privileges */
1823 case PROC_PIDT_SHORTBSDINFO
:
1824 case PROC_PIDUNIQIDENTIFIERINFO
:
1825 case PROC_PIDPATHINFO
:
1826 case PROC_PIDCOALITIONINFO
:
1827 check_same_user
= NO_CHECK_SAME_USER
;
1830 check_same_user
= CHECK_SAME_USER
;
1834 /* Do we have permission to look into this? */
1835 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDINFO
, flavor
, check_same_user
)))
1839 case PROC_PIDLISTFDS
: {
1840 error
= proc_pidfdlist(p
, buffer
, buffersize
, retval
);
1844 case PROC_PIDUNIQIDENTIFIERINFO
: {
1845 struct proc_uniqidentifierinfo p_uniqidinfo
;
1846 bzero(&p_uniqidinfo
, sizeof(p_uniqidinfo
));
1847 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
1848 error
= copyout(&p_uniqidinfo
, buffer
, sizeof(struct proc_uniqidentifierinfo
));
1850 *retval
= sizeof(struct proc_uniqidentifierinfo
);
1854 case PROC_PIDT_SHORTBSDINFO
:
1856 case PROC_PIDT_BSDINFOWITHUNIQID
:
1857 case PROC_PIDTBSDINFO
: {
1858 struct proc_bsdinfo pbsd
;
1859 struct proc_bsdshortinfo pbsd_short
;
1860 struct proc_bsdinfowithuniqid pbsd_uniqid
;
1862 if (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1865 if (shortversion
!= 0) {
1866 error
= proc_pidshortbsdinfo(p
, &pbsd_short
, zombie
);
1868 error
= proc_pidbsdinfo(p
, &pbsd
, zombie
);
1869 if (uniqidversion
!= 0) {
1870 bzero(&pbsd_uniqid
, sizeof(pbsd_uniqid
));
1871 proc_piduniqidentifierinfo(p
, &pbsd_uniqid
.p_uniqidentifier
);
1872 pbsd_uniqid
.pbsd
= pbsd
;
1877 if (shortversion
!= 0) {
1878 error
= copyout(&pbsd_short
, buffer
, sizeof(struct proc_bsdshortinfo
));
1880 *retval
= sizeof(struct proc_bsdshortinfo
);
1881 } else if (uniqidversion
!= 0) {
1882 error
= copyout(&pbsd_uniqid
, buffer
, sizeof(struct proc_bsdinfowithuniqid
));
1884 *retval
= sizeof(struct proc_bsdinfowithuniqid
);
1886 error
= copyout(&pbsd
, buffer
, sizeof(struct proc_bsdinfo
));
1888 *retval
= sizeof(struct proc_bsdinfo
);
1894 case PROC_PIDTASKINFO
: {
1895 struct proc_taskinfo ptinfo
;
1897 error
= proc_pidtaskinfo(p
, &ptinfo
);
1899 error
= copyout(&ptinfo
, buffer
, sizeof(struct proc_taskinfo
));
1901 *retval
= sizeof(struct proc_taskinfo
);
1906 case PROC_PIDTASKALLINFO
: {
1907 struct proc_taskallinfo pall
;
1908 bzero(&pall
, sizeof(pall
));
1909 error
= proc_pidbsdinfo(p
, &pall
.pbsd
, 0);
1910 error
= proc_pidtaskinfo(p
, &pall
.ptinfo
);
1912 error
= copyout(&pall
, buffer
, sizeof(struct proc_taskallinfo
));
1914 *retval
= sizeof(struct proc_taskallinfo
);
1919 case PROC_PIDTHREADID64INFO
:
1921 case PROC_PIDTHREADINFO
:{
1922 struct proc_threadinfo pthinfo
;
1924 error
= proc_pidthreadinfo(p
, arg
, thuniqueid
, &pthinfo
);
1926 error
= copyout(&pthinfo
, buffer
, sizeof(struct proc_threadinfo
));
1928 *retval
= sizeof(struct proc_threadinfo
);
1933 case PROC_PIDLISTTHREADS
:{
1934 error
= proc_pidlistthreads(p
, buffer
, buffersize
, retval
);
1938 case PROC_PIDREGIONINFO
:{
1939 error
= proc_pidregioninfo(p
, arg
, buffer
, buffersize
, retval
);
1944 case PROC_PIDREGIONPATHINFO
:{
1945 error
= proc_pidregionpathinfo(p
, arg
, buffer
, buffersize
, retval
);
1949 case PROC_PIDREGIONPATHINFO2
:{
1950 error
= proc_pidregionpathinfo2(p
, arg
, buffer
, buffersize
, retval
);
1954 case PROC_PIDREGIONPATHINFO3
:{
1955 error
= proc_pidregionpathinfo3(p
, arg
, buffer
, buffersize
, retval
);
1959 case PROC_PIDVNODEPATHINFO
:{
1960 error
= proc_pidvnodepathinfo(p
, arg
, buffer
, buffersize
, retval
);
1965 case PROC_PIDTHREADPATHINFO
:{
1966 struct proc_threadwithpathinfo pinfo
;
1968 error
= proc_pidthreadpathinfo(p
, arg
, &pinfo
);
1970 error
= copyout((caddr_t
)&pinfo
, buffer
, sizeof(struct proc_threadwithpathinfo
));
1972 *retval
= sizeof(struct proc_threadwithpathinfo
);
1977 case PROC_PIDPATHINFO
: {
1978 error
= proc_pidpathinfo(p
, arg
, buffer
, buffersize
, retval
);
1983 case PROC_PIDWORKQUEUEINFO
:{
1984 struct proc_workqueueinfo pwqinfo
;
1986 error
= proc_pidworkqueueinfo(p
, &pwqinfo
);
1988 error
= copyout(&pwqinfo
, buffer
, sizeof(struct proc_workqueueinfo
));
1990 *retval
= sizeof(struct proc_workqueueinfo
);
1995 case PROC_PIDLISTFILEPORTS
: {
1996 error
= proc_pidfileportlist(p
, buffer
, buffersize
, retval
);
2000 case PROC_PIDARCHINFO
: {
2001 struct proc_archinfo pai
;
2002 bzero(&pai
, sizeof(pai
));
2003 proc_archinfo(p
, &pai
);
2004 error
= copyout(&pai
, buffer
, sizeof(struct proc_archinfo
));
2006 *retval
= sizeof(struct proc_archinfo
);
2011 case PROC_PIDCOALITIONINFO
: {
2012 struct proc_pidcoalitioninfo pci
;
2013 proc_pidcoalitioninfo(p
, &pci
);
2014 error
= copyout(&pci
, buffer
, sizeof(struct proc_pidcoalitioninfo
));
2016 *retval
= sizeof(struct proc_pidcoalitioninfo
);
2021 case PROC_PIDNOTEEXIT
: {
2023 error
= proc_pidnoteexit(p
, arg
, &data
);
2025 error
= copyout(&data
, buffer
, sizeof(data
));
2027 *retval
= sizeof(data
);
2033 case PROC_PIDEXITREASONINFO
: {
2034 struct proc_exitreasoninfo eri
;
2036 error
= copyin(buffer
, &eri
, sizeof(eri
));
2041 error
= proc_pidexitreasoninfo(p
, &eri
, NULL
);
2043 error
= copyout(&eri
, buffer
, sizeof(eri
));
2045 *retval
= sizeof(eri
);
2051 case PROC_PIDEXITREASONBASICINFO
: {
2052 struct proc_exitreasonbasicinfo beri
;
2054 bzero(&beri
, sizeof(struct proc_exitreasonbasicinfo
));
2056 error
= proc_pidexitreasoninfo(p
, NULL
, &beri
);
2058 error
= copyout(&beri
, buffer
, sizeof(beri
));
2060 *retval
= sizeof(beri
);
2066 case PROC_PIDLISTUPTRS
:
2067 error
= proc_pidlistuptrs(p
, buffer
, buffersize
, retval
);
2070 case PROC_PIDLISTDYNKQUEUES
:
2071 error
= kevent_copyout_proc_dynkqids(p
, buffer
, buffersize
, retval
);
2083 proc_drop_zombref(p
);
2089 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
)
2091 struct vnode_fdinfo vfi
;
2094 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2097 bzero(&vfi
, sizeof(struct vnode_fdinfo
));
2098 fill_fileinfo(fp
, proc
, fd
, &vfi
.pfi
);
2099 error
= fill_vnodeinfo(vp
, &vfi
.pvi
);
2102 error
= copyout((caddr_t
)&vfi
, buffer
, sizeof(struct vnode_fdinfo
));
2104 *retval
= sizeof(struct vnode_fdinfo
);
2110 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
)
2112 struct vnode_fdinfowithpath vfip
;
2113 int count
, error
= 0;
2115 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2118 bzero(&vfip
, sizeof(struct vnode_fdinfowithpath
));
2119 fill_fileinfo(fp
, proc
, fd
, &vfip
.pfi
);
2120 error
= fill_vnodeinfo(vp
, &vfip
.pvip
.vip_vi
) ;
2123 vn_getpath(vp
, &vfip
.pvip
.vip_path
[0], &count
);
2124 vfip
.pvip
.vip_path
[MAXPATHLEN
-1] = 0;
2126 error
= copyout((caddr_t
)&vfip
, buffer
, sizeof(struct vnode_fdinfowithpath
));
2128 *retval
= sizeof(struct vnode_fdinfowithpath
);
2135 fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* fproc
)
2137 fproc
->fi_openflags
= fp
->f_fglob
->fg_flag
;
2138 fproc
->fi_status
= 0;
2139 fproc
->fi_offset
= fp
->f_fglob
->fg_offset
;
2140 fproc
->fi_type
= FILEGLOB_DTYPE(fp
->f_fglob
);
2141 if (fp
->f_fglob
->fg_count
> 1)
2142 fproc
->fi_status
|= PROC_FP_SHARED
;
2143 if (proc
!= PROC_NULL
) {
2144 if ((FDFLAGS_GET(proc
, fd
) & UF_EXCLOSE
) != 0)
2145 fproc
->fi_status
|= PROC_FP_CLEXEC
;
2146 if ((FDFLAGS_GET(proc
, fd
) & UF_FORKCLOSE
) != 0)
2147 fproc
->fi_status
|= PROC_FP_CLFORK
;
2149 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
2150 fproc
->fi_status
|= PROC_FP_GUARDED
;
2151 fproc
->fi_guardflags
= 0;
2152 if (fp_isguarded(fp
, GUARD_CLOSE
))
2153 fproc
->fi_guardflags
|= PROC_FI_GUARD_CLOSE
;
2154 if (fp_isguarded(fp
, GUARD_DUP
))
2155 fproc
->fi_guardflags
|= PROC_FI_GUARD_DUP
;
2156 if (fp_isguarded(fp
, GUARD_SOCKET_IPC
))
2157 fproc
->fi_guardflags
|= PROC_FI_GUARD_SOCKET_IPC
;
2158 if (fp_isguarded(fp
, GUARD_FILEPORT
))
2159 fproc
->fi_guardflags
|= PROC_FI_GUARD_FILEPORT
;
2166 fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
)
2168 vfs_context_t context
;
2172 bzero(&sb
, sizeof(struct stat64
));
2173 context
= vfs_context_create((vfs_context_t
)0);
2174 error
= vn_stat(vp
, &sb
, NULL
, 1, context
);
2175 (void)vfs_context_rele(context
);
2177 munge_vinfo_stat(&sb
, &vinfo
->vi_stat
);
2182 if (vp
->v_mount
!= dead_mountp
) {
2183 vinfo
->vi_fsid
= vp
->v_mount
->mnt_vfsstat
.f_fsid
;
2185 vinfo
->vi_fsid
.val
[0] = 0;
2186 vinfo
->vi_fsid
.val
[1] = 0;
2188 vinfo
->vi_type
= vp
->v_type
;
2194 pid_socketinfo(socket_t so
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2197 struct socket_fdinfo s
;
2200 bzero(&s
, sizeof(struct socket_fdinfo
));
2201 fill_fileinfo(fp
, proc
, fd
, &s
.pfi
);
2202 if ((error
= fill_socketinfo(so
, &s
.psi
)) == 0) {
2203 if ((error
= copyout(&s
, buffer
, sizeof(struct socket_fdinfo
))) == 0)
2204 *retval
= sizeof(struct socket_fdinfo
);
2208 #pragma unused(so, fp, proc, fd, buffer)
2215 pid_pseminfo(struct psemnode
*psem
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2217 struct psem_fdinfo pseminfo
;
2220 bzero(&pseminfo
, sizeof(struct psem_fdinfo
));
2221 fill_fileinfo(fp
, proc
, fd
, &pseminfo
.pfi
);
2223 if ((error
= fill_pseminfo(psem
, &pseminfo
.pseminfo
)) == 0) {
2224 if ((error
= copyout(&pseminfo
, buffer
, sizeof(struct psem_fdinfo
))) == 0)
2225 *retval
= sizeof(struct psem_fdinfo
);
2232 pid_pshminfo(struct pshmnode
*pshm
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2234 struct pshm_fdinfo pshminfo
;
2237 bzero(&pshminfo
, sizeof(struct pshm_fdinfo
));
2238 fill_fileinfo(fp
, proc
, fd
, &pshminfo
.pfi
);
2240 if ((error
= fill_pshminfo(pshm
, &pshminfo
.pshminfo
)) == 0) {
2241 if ((error
= copyout(&pshminfo
, buffer
, sizeof(struct pshm_fdinfo
))) == 0)
2242 *retval
= sizeof(struct pshm_fdinfo
);
2249 pid_pipeinfo(struct pipe
* p
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2251 struct pipe_fdinfo pipeinfo
;
2254 bzero(&pipeinfo
, sizeof(struct pipe_fdinfo
));
2255 fill_fileinfo(fp
, proc
, fd
, &pipeinfo
.pfi
);
2256 if ((error
= fill_pipeinfo(p
, &pipeinfo
.pipeinfo
)) == 0) {
2257 if ((error
= copyout(&pipeinfo
, buffer
, sizeof(struct pipe_fdinfo
))) == 0)
2258 *retval
= sizeof(struct pipe_fdinfo
);
2265 pid_kqueueinfo(struct kqueue
* kq
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2267 struct kqueue_fdinfo kqinfo
;
2270 bzero(&kqinfo
, sizeof(struct kqueue_fdinfo
));
2272 /* not all kq's are associated with a file (e.g. workqkq) */
2275 fill_fileinfo(fp
, proc
, fd
, &kqinfo
.pfi
);
2278 if ((error
= fill_kqueueinfo(kq
, &kqinfo
.kqueueinfo
)) == 0) {
2279 if ((error
= copyout(&kqinfo
, buffer
, sizeof(struct kqueue_fdinfo
))) == 0)
2280 *retval
= sizeof(struct kqueue_fdinfo
);
2287 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
)
2293 /************************** proc_pidfdinfo routine ***************************/
2295 proc_pidfdinfo(int pid
, int flavor
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2298 int error
= ENOTSUP
;
2299 struct fileproc
* fp
= NULL
;
2303 case PROC_PIDFDVNODEINFO
:
2304 size
= PROC_PIDFDVNODEINFO_SIZE
;
2306 case PROC_PIDFDVNODEPATHINFO
:
2307 size
= PROC_PIDFDVNODEPATHINFO_SIZE
;
2309 case PROC_PIDFDSOCKETINFO
:
2310 size
= PROC_PIDFDSOCKETINFO_SIZE
;
2312 case PROC_PIDFDPSEMINFO
:
2313 size
= PROC_PIDFDPSEMINFO_SIZE
;
2315 case PROC_PIDFDPSHMINFO
:
2316 size
= PROC_PIDFDPSHMINFO_SIZE
;
2318 case PROC_PIDFDPIPEINFO
:
2319 size
= PROC_PIDFDPIPEINFO_SIZE
;
2321 case PROC_PIDFDKQUEUEINFO
:
2322 size
= PROC_PIDFDKQUEUEINFO_SIZE
;
2324 case PROC_PIDFDKQUEUE_EXTINFO
:
2325 size
= PROC_PIDFDKQUEUE_EXTINFO_SIZE
;
2326 if (buffer
== (user_addr_t
)0)
2329 case PROC_PIDFDATALKINFO
:
2330 size
= PROC_PIDFDATALKINFO_SIZE
;
2338 if (buffersize
< size
)
2341 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2346 /* Do we have permission to look into this? */
2347 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFDINFO
, flavor
, CHECK_SAME_USER
)))
2351 case PROC_PIDFDVNODEINFO
: {
2355 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) !=0) {
2358 /* no need to be under the fdlock */
2359 error
= pid_vnodeinfo(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2363 case PROC_PIDFDVNODEPATHINFO
: {
2367 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) !=0) {
2371 /* no need to be under the fdlock */
2372 error
= pid_vnodeinfopath(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2376 case PROC_PIDFDSOCKETINFO
: {
2379 if ((error
= fp_getfsock(p
, fd
, &fp
, &so
)) !=0) {
2382 /* no need to be under the fdlock */
2383 error
= pid_socketinfo(so
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2387 case PROC_PIDFDPSEMINFO
: {
2388 struct psemnode
* psem
;
2390 if ((error
= fp_getfpsem(p
, fd
, &fp
, &psem
)) !=0) {
2393 /* no need to be under the fdlock */
2394 error
= pid_pseminfo(psem
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2398 case PROC_PIDFDPSHMINFO
: {
2399 struct pshmnode
* pshm
;
2401 if ((error
= fp_getfpshm(p
, fd
, &fp
, &pshm
)) !=0) {
2404 /* no need to be under the fdlock */
2405 error
= pid_pshminfo(pshm
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2409 case PROC_PIDFDPIPEINFO
: {
2410 struct pipe
* cpipe
;
2412 if ((error
= fp_getfpipe(p
, fd
, &fp
, &cpipe
)) !=0) {
2415 /* no need to be under the fdlock */
2416 error
= pid_pipeinfo(cpipe
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2420 case PROC_PIDFDKQUEUEINFO
: {
2424 if ((kq
= p
->p_fd
->fd_wqkqueue
) == NULL
) {
2425 /* wqkqueue is initialized on-demand */
2429 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2433 /* no need to be under the fdlock */
2434 error
= pid_kqueueinfo(kq
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2438 case PROC_PIDFDKQUEUE_EXTINFO
: {
2442 if ((kq
= p
->p_fd
->fd_wqkqueue
) == NULL
) {
2443 /* wqkqueue is initialized on-demand */
2447 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2450 error
= pid_kqueue_extinfo(p
, kq
, buffer
, buffersize
, retval
);
2461 fp_drop(p
, fd
, fp
, 0);
2469 #define MAX_UPTRS 16392
2472 proc_pidlistuptrs(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2479 if (buffer
!= USER_ADDR_NULL
) {
2480 count
= buffersize
/ sizeof(uint64_t);
2481 if (count
> MAX_UPTRS
) {
2485 buffersize
= count
* sizeof(uint64_t);
2486 kbuf
= kalloc(buffersize
);
2487 bzero(kbuf
, buffersize
);
2488 assert(kbuf
!= NULL
);
2496 nuptrs
= kevent_proc_copy_uptrs(p
, kbuf
, buffersize
);
2500 if (os_mul_overflow(nuptrs
, sizeof(uint64_t), ©size
)) {
2504 if (copysize
> buffersize
) {
2505 copysize
= buffersize
;
2507 error
= copyout(kbuf
, buffer
, copysize
);
2514 kfree(kbuf
, buffersize
);
2522 * Helper function for proc_pidfileportinfo
2525 struct fileport_info_args
{
2527 user_addr_t fia_buffer
;
2528 uint32_t fia_buffersize
;
2529 int32_t *fia_retval
;
2532 static kern_return_t
2533 proc_fileport_info(__unused mach_port_name_t name
,
2534 struct fileglob
*fg
, void *arg
)
2536 struct fileport_info_args
*fia
= arg
;
2537 struct fileproc __fileproc
, *fp
= &__fileproc
;
2540 bzero(fp
, sizeof (*fp
));
2543 switch (fia
->fia_flavor
) {
2544 case PROC_PIDFILEPORTVNODEPATHINFO
: {
2547 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
2551 vp
= (struct vnode
*)fg
->fg_data
;
2552 error
= pid_vnodeinfopath(vp
, vnode_vid(vp
), fp
, PROC_NULL
, 0,
2553 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2556 case PROC_PIDFILEPORTSOCKETINFO
: {
2559 if (FILEGLOB_DTYPE(fg
) != DTYPE_SOCKET
) {
2563 so
= (socket_t
)fg
->fg_data
;
2564 error
= pid_socketinfo(so
, fp
, PROC_NULL
, 0,
2565 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2568 case PROC_PIDFILEPORTPSHMINFO
: {
2569 struct pshmnode
*pshm
;
2571 if (FILEGLOB_DTYPE(fg
) != DTYPE_PSXSHM
) {
2572 error
= EBADF
; /* ick - mirror fp_getfpshm */
2575 pshm
= (struct pshmnode
*)fg
->fg_data
;
2576 error
= pid_pshminfo(pshm
, fp
, PROC_NULL
, 0,
2577 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2580 case PROC_PIDFILEPORTPIPEINFO
: {
2583 if (FILEGLOB_DTYPE(fg
) != DTYPE_PIPE
) {
2584 error
= EBADF
; /* ick - mirror fp_getfpipe */
2587 cpipe
= (struct pipe
*)fg
->fg_data
;
2588 error
= pid_pipeinfo(cpipe
, fp
, PROC_NULL
, 0,
2589 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2600 /************************* proc_pidfileportinfo routine *********************/
2602 proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
,
2603 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2606 int error
= ENOTSUP
;
2608 struct fileport_info_args fia
;
2610 /* fileport types are restricted by file_issendable() */
2613 case PROC_PIDFILEPORTVNODEPATHINFO
:
2614 size
= PROC_PIDFILEPORTVNODEPATHINFO_SIZE
;
2616 case PROC_PIDFILEPORTSOCKETINFO
:
2617 size
= PROC_PIDFILEPORTSOCKETINFO_SIZE
;
2619 case PROC_PIDFILEPORTPSHMINFO
:
2620 size
= PROC_PIDFILEPORTPSHMINFO_SIZE
;
2622 case PROC_PIDFILEPORTPIPEINFO
:
2623 size
= PROC_PIDFILEPORTPIPEINFO_SIZE
;
2629 if (buffersize
< size
)
2631 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2636 /* Do we have permission to look into this? */
2637 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFILEPORTINFO
, flavor
, CHECK_SAME_USER
)))
2640 fia
.fia_flavor
= flavor
;
2641 fia
.fia_buffer
= buffer
;
2642 fia
.fia_buffersize
= buffersize
;
2643 fia
.fia_retval
= retval
;
2645 if (fileport_invoke(p
->task
, name
,
2646 proc_fileport_info
, &fia
, &error
) != KERN_SUCCESS
)
2655 proc_security_policy(proc_t targetp
, __unused
int callnum
, __unused
int flavor
, boolean_t check_same_user
)
2660 if ((error
= mac_proc_check_proc_info(current_proc(), targetp
, callnum
, flavor
)))
2664 /* The 'listpids' call doesn't have a target proc */
2665 if (targetp
== PROC_NULL
) {
2666 assert(callnum
== PROC_INFO_CALL_LISTPIDS
&& check_same_user
== NO_CHECK_SAME_USER
);
2671 * Check for 'get information for processes owned by other users' privilege
2672 * root has this privilege by default
2674 if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO
, 0) == 0)
2675 check_same_user
= FALSE
;
2677 if (check_same_user
) {
2678 kauth_cred_t target_cred
;
2681 target_cred
= kauth_cred_proc_ref(targetp
);
2682 target_uid
= kauth_cred_getuid(target_cred
);
2683 kauth_cred_unref(&target_cred
);
2685 if (kauth_getuid() != target_uid
)
2693 proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2695 if (suser(kauth_cred_get(), (u_short
*)0) == 0) {
2696 return(log_dmesg(buffer
, buffersize
, retval
));
2701 /* ********* process control sets on self only */
2703 proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t * retval
)
2705 struct proc
* pself
= PROC_NULL
;
2707 uint32_t pcontrol
= (uint32_t)arg
;
2708 struct uthread
*ut
= NULL
;
2709 char name_buf
[MAXTHREADNAMESIZE
];
2711 pself
= current_proc();
2712 if (pid
!= pself
->p_pid
)
2715 /* Do we have permission to look into this? */
2716 if ((error
= proc_security_policy(pself
, PROC_INFO_CALL_SETCONTROL
, flavor
, NO_CHECK_SAME_USER
)))
2720 case PROC_SELFSET_PCONTROL
: {
2721 if (pcontrol
> P_PCMAX
)
2724 /* reset existing control setting while retaining action state */
2725 pself
->p_pcaction
&= PROC_ACTION_MASK
;
2726 /* set new control state */
2727 pself
->p_pcaction
|= pcontrol
;
2732 case PROC_SELFSET_THREADNAME
: {
2734 * This is a bit ugly, as it copies the name into the kernel, and then
2735 * invokes bsd_setthreadname again to copy it into the uthread name
2736 * buffer. Hopefully this isn't such a hot codepath that an additional
2737 * MAXTHREADNAMESIZE copy is a big issue.
2739 if (buffersize
> (MAXTHREADNAMESIZE
- 1)) {
2740 return ENAMETOOLONG
;
2743 ut
= current_uthread();
2745 bzero(name_buf
, MAXTHREADNAMESIZE
);
2746 error
= copyin(buffer
, name_buf
, buffersize
);
2749 bsd_setthreadname(ut
, name_buf
);
2754 case PROC_SELFSET_VMRSRCOWNER
: {
2755 /* need to to be superuser */
2756 if (suser(kauth_cred_get(), (u_short
*)0) != 0) {
2762 /* reset existing control setting while retaining action state */
2763 pself
->p_lflag
|= P_LVMRSRCOWNER
;
2768 case PROC_SELFSET_DELAYIDLESLEEP
: {
2769 /* mark or clear the process property to delay idle sleep disk IO */
2771 OSBitOrAtomic(P_DELAYIDLESLEEP
, &pself
->p_flag
);
2773 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP
), &pself
->p_flag
);
2785 #if CONFIG_MEMORYSTATUS
2788 proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t *retval
) {
2789 struct proc
*target_p
;
2791 uint32_t pcontrol
= (uint32_t)arg
;
2792 kauth_cred_t my_cred
, target_cred
;
2793 boolean_t self
= FALSE
;
2794 boolean_t child
= FALSE
;
2795 boolean_t zombref
= FALSE
;
2798 target_p
= proc_find(pid
);
2800 if (target_p
== PROC_NULL
) {
2801 if (flavor
== PROC_DIRTYCONTROL_GET
) {
2802 target_p
= proc_find_zombref(pid
);
2806 if (target_p
== PROC_NULL
)
2811 my_cred
= kauth_cred_get();
2812 target_cred
= kauth_cred_proc_ref(target_p
);
2814 /* Do we have permission to look into this? */
2815 if ((error
= proc_security_policy(target_p
, PROC_INFO_CALL_DIRTYCONTROL
, flavor
, NO_CHECK_SAME_USER
)))
2818 selfpid
= proc_selfpid();
2819 if (pid
== selfpid
) {
2821 } else if (target_p
->p_ppid
== selfpid
) {
2826 case PROC_DIRTYCONTROL_TRACK
: {
2827 /* Only allow the process itself, its parent, or root */
2828 if ((self
== FALSE
) && (child
== FALSE
) && kauth_cred_issuser(kauth_cred_get()) != TRUE
) {
2833 error
= memorystatus_dirty_track(target_p
, pcontrol
);
2837 case PROC_DIRTYCONTROL_SET
: {
2838 /* Check privileges; use cansignal() here since the process could be terminated */
2839 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
)) {
2844 error
= memorystatus_dirty_set(target_p
, self
, pcontrol
);
2848 case PROC_DIRTYCONTROL_GET
: {
2849 /* No permissions check - dirty state is freely available */
2851 *retval
= memorystatus_dirty_get(target_p
);
2858 case PROC_DIRTYCONTROL_CLEAR
: {
2859 /* Check privileges; use cansignal() here since the process could be terminated */
2860 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
)) {
2865 error
= memorystatus_dirty_clear(target_p
, pcontrol
);
2872 proc_drop_zombref(target_p
);
2874 proc_rele(target_p
);
2876 kauth_cred_unref(&target_cred
);
2883 proc_dirtycontrol(__unused
int pid
, __unused
int flavor
, __unused
uint64_t arg
, __unused
int32_t *retval
) {
2887 #endif /* CONFIG_MEMORYSTATUS */
2890 * proc_terminate() provides support for sudden termination.
2891 * SIGKILL is issued to tracked, clean processes; otherwise,
2896 proc_terminate(int pid
, int32_t *retval
)
2900 kauth_cred_t uc
= kauth_cred_get();
2904 /* XXX: Check if these are necessary */
2905 AUDIT_ARG(pid
, pid
);
2906 AUDIT_ARG(signum
, sig
);
2909 if (pid
<= 0 || retval
== NULL
) {
2913 if ((p
= proc_find(pid
)) == NULL
) {
2918 /* XXX: Check if these are necessary */
2919 AUDIT_ARG(process
, p
);
2922 /* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
2923 if (!cansignal(current_proc(), uc
, p
, SIGKILL
)) {
2928 /* Not allowed to sudden terminate yourself */
2929 if (p
== current_proc()) {
2934 #if CONFIG_MEMORYSTATUS
2935 /* Determine requisite signal to issue */
2936 sig
= memorystatus_on_terminate(p
);
2941 proc_set_task_policy(p
->task
, TASK_POLICY_ATTRIBUTE
,
2942 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2954 * copy stat64 structure into vinfo_stat structure.
2957 munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
)
2959 bzero(vsbp
, sizeof(struct vinfo_stat
));
2961 vsbp
->vst_dev
= sbp
->st_dev
;
2962 vsbp
->vst_mode
= sbp
->st_mode
;
2963 vsbp
->vst_nlink
= sbp
->st_nlink
;
2964 vsbp
->vst_ino
= sbp
->st_ino
;
2965 vsbp
->vst_uid
= sbp
->st_uid
;
2966 vsbp
->vst_gid
= sbp
->st_gid
;
2967 vsbp
->vst_atime
= sbp
->st_atimespec
.tv_sec
;
2968 vsbp
->vst_atimensec
= sbp
->st_atimespec
.tv_nsec
;
2969 vsbp
->vst_mtime
= sbp
->st_mtimespec
.tv_sec
;
2970 vsbp
->vst_mtimensec
= sbp
->st_mtimespec
.tv_nsec
;
2971 vsbp
->vst_ctime
= sbp
->st_ctimespec
.tv_sec
;
2972 vsbp
->vst_ctimensec
= sbp
->st_ctimespec
.tv_nsec
;
2973 vsbp
->vst_birthtime
= sbp
->st_birthtimespec
.tv_sec
;
2974 vsbp
->vst_birthtimensec
= sbp
->st_birthtimespec
.tv_nsec
;
2975 vsbp
->vst_size
= sbp
->st_size
;
2976 vsbp
->vst_blocks
= sbp
->st_blocks
;
2977 vsbp
->vst_blksize
= sbp
->st_blksize
;
2978 vsbp
->vst_flags
= sbp
->st_flags
;
2979 vsbp
->vst_gen
= sbp
->st_gen
;
2980 vsbp
->vst_rdev
= sbp
->st_rdev
;
2981 vsbp
->vst_qspare
[0] = sbp
->st_qspare
[0];
2982 vsbp
->vst_qspare
[1] = sbp
->st_qspare
[1];
2986 proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, __unused
int32_t *retval
)
2992 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2993 if ((p
= proc_find_zombref(pid
)) == PROC_NULL
) {
2999 /* Do we have permission to look into this? */
3000 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDRUSAGE
, flavor
, CHECK_SAME_USER
)))
3003 error
= proc_get_rusage(p
, flavor
, buffer
, zombie
);
3007 proc_drop_zombref(p
);
3015 proc_archinfo(proc_t p
, struct proc_archinfo
*pai
)
3018 pai
->p_cputype
= p
->p_cputype
;
3019 pai
->p_cpusubtype
= p
->p_cpusubtype
;
3024 proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*ppci
)
3026 bzero(ppci
, sizeof(*ppci
));
3027 proc_coalitionids(p
, ppci
->coalition_id
);
3031 proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
)
3033 uint32_t reason_data_size
= 0;
3035 pid_t selfpid
= proc_selfpid();
3040 * One (and only one) of peri and pberi must be non-NULL.
3042 assert((peri
!= NULL
) || (pberi
!= NULL
));
3043 assert((peri
== NULL
) || (pberi
== NULL
));
3046 * Allow access to the parent of the exiting
3047 * child or the parent debugger only.
3050 if (p
->p_ppid
== selfpid
)
3051 break; /* parent => ok */
3053 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3054 (p
->p_oppid
== selfpid
))
3055 break; /* parent-in-waiting => ok */
3061 if (p
->p_exit_reason
== OS_REASON_NULL
) {
3066 if (p
->p_exit_reason
->osr_kcd_buf
!= NULL
) {
3067 reason_data_size
= kcdata_memory_get_used_bytes(&p
->p_exit_reason
->osr_kcd_descriptor
);
3071 peri
->eri_namespace
= p
->p_exit_reason
->osr_namespace
;
3072 peri
->eri_code
= p
->p_exit_reason
->osr_code
;
3073 peri
->eri_flags
= p
->p_exit_reason
->osr_flags
;
3075 if ((peri
->eri_kcd_buf
== 0) || (peri
->eri_reason_buf_size
< reason_data_size
)) {
3080 peri
->eri_reason_buf_size
= reason_data_size
;
3081 if (reason_data_size
!= 0) {
3082 error
= copyout(p
->p_exit_reason
->osr_kcd_buf
, peri
->eri_kcd_buf
, reason_data_size
);
3085 pberi
->beri_namespace
= p
->p_exit_reason
->osr_namespace
;
3086 pberi
->beri_code
= p
->p_exit_reason
->osr_code
;
3087 pberi
->beri_flags
= p
->p_exit_reason
->osr_flags
;
3088 pberi
->beri_reason_buf_size
= reason_data_size
;
3097 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
3098 * It mimics the data that is typically captured by the
3099 * EVFILT_PROC, NOTE_EXIT event mechanism.
3100 * See filt_proc() in kern_event.c.
3103 proc_pidnoteexit(proc_t p
, uint64_t flags
, uint32_t *data
)
3105 uint32_t exit_data
= 0;
3106 uint32_t exit_flags
= (uint32_t)flags
;
3111 * Allow access to the parent of the exiting
3112 * child or the parent debugger only.
3115 pid_t selfpid
= proc_selfpid();
3117 if (p
->p_ppid
== selfpid
)
3118 break; /* parent => ok */
3120 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3121 (p
->p_oppid
== selfpid
))
3122 break; /* parent-in-waiting => ok */
3128 if ((exit_flags
& NOTE_EXITSTATUS
) != 0) {
3129 /* The signal and exit status */
3130 exit_data
|= (p
->p_xstat
& NOTE_PDATAMASK
);
3133 if ((exit_flags
& NOTE_EXIT_DETAIL
) != 0) {
3134 /* The exit detail */
3135 if ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) != 0) {
3136 exit_data
|= NOTE_EXIT_DECRYPTFAIL
;
3139 if ((p
->p_lflag
& P_LTERM_JETSAM
) != 0) {
3140 exit_data
|= NOTE_EXIT_MEMORY
;
3142 switch (p
->p_lflag
& P_JETSAM_MASK
) {
3143 case P_JETSAM_VMPAGESHORTAGE
:
3144 exit_data
|= NOTE_EXIT_MEMORY_VMPAGESHORTAGE
;
3146 case P_JETSAM_VMTHRASHING
:
3147 exit_data
|= NOTE_EXIT_MEMORY_VMTHRASHING
;
3149 case P_JETSAM_FCTHRASHING
:
3150 exit_data
|= NOTE_EXIT_MEMORY_FCTHRASHING
;
3152 case P_JETSAM_VNODE
:
3153 exit_data
|= NOTE_EXIT_MEMORY_VNODE
;
3155 case P_JETSAM_HIWAT
:
3156 exit_data
|= NOTE_EXIT_MEMORY_HIWAT
;
3159 exit_data
|= NOTE_EXIT_MEMORY_PID
;
3161 case P_JETSAM_IDLEEXIT
:
3162 exit_data
|= NOTE_EXIT_MEMORY_IDLE
;
3167 if ((p
->p_csflags
& CS_KILLED
) != 0) {
3168 exit_data
|= NOTE_EXIT_CSERROR
;
3180 proc_piddynkqueueinfo(int pid
, int flavor
, kqueue_id_t kq_id
,
3181 user_addr_t ubuf
, uint32_t bufsize
, int32_t *retval
)
3186 if (ubuf
== USER_ADDR_NULL
) {
3191 if (p
== PROC_NULL
) {
3195 err
= proc_security_policy(p
, PROC_INFO_CALL_PIDDYNKQUEUEINFO
, 0, CHECK_SAME_USER
);
3201 case PROC_PIDDYNKQUEUE_INFO
:
3202 err
= kevent_copyout_dynkqinfo(p
, kq_id
, ubuf
, bufsize
, retval
);
3204 case PROC_PIDDYNKQUEUE_EXTINFO
:
3205 err
= kevent_copyout_dynkqextinfo(p
, kq_id
, ubuf
, bufsize
, retval
);
3218 #if !CONFIG_EMBEDDED
3220 proc_udata_info(int pid
, int flavor
, user_addr_t buffer
, uint32_t bufsize
, int32_t *retval
)
3226 if (p
== PROC_NULL
) {
3231 * Only support calls against oneself for the moment.
3233 if (p
->p_pid
!= proc_selfpid()) {
3238 if (bufsize
!= sizeof (p
->p_user_data
)) {
3244 case PROC_UDATA_INFO_SET
:
3245 err
= copyin(buffer
, &p
->p_user_data
, sizeof (p
->p_user_data
));
3247 case PROC_UDATA_INFO_GET
:
3248 err
= copyout(&p
->p_user_data
, buffer
, sizeof (p
->p_user_data
));
3264 #endif /* !CONFIG_EMBEDDED */