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() */
88 #include <sys/event.h>
89 #include <sys/codesign.h>
91 /* Needed by proc_listcoalitions() */
92 #ifdef CONFIG_COALITIONS
93 #include <sys/coalition.h>
102 uint64_t get_dispatchqueue_offset_from_proc(void *);
103 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *);
104 int proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
107 * TODO: Replace the noinline attribute below. Currently, it serves
108 * to avoid stack bloat caused by inlining multiple functions that
109 * have large stack footprints; when the functions are independent
110 * of each other (will not both be called in any given call to the
111 * caller), this only serves to bloat the stack, as we allocate
112 * space for both functions, despite the fact that we only need a
113 * fraction of that space.
115 * Long term, these functions should not be allocating everything on
116 * the stack, and should move large allocations (the huge structs
117 * that proc info deals in) to the heap, or eliminate them if
120 * The functions that most desperately need to improve stack usage
121 * (starting with the worst offenders):
122 * proc_pidvnodepathinfo
124 * proc_pidregionpathinfo
130 * proc_pidoriginatorinfo
133 /* protos for proc_info calls */
134 int __attribute__ ((noinline
)) proc_listpids(uint32_t type
, uint32_t tyoneinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
135 int __attribute__ ((noinline
)) proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
136 int __attribute__ ((noinline
)) proc_pidfdinfo(int pid
, int flavor
,int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
137 int __attribute__ ((noinline
)) proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
138 int __attribute__ ((noinline
)) proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
139 int __attribute__ ((noinline
)) proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
140 int __attribute__ ((noinline
)) proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t * retval
);
141 int __attribute__ ((noinline
)) proc_terminate(int pid
, int32_t * retval
);
142 int __attribute__ ((noinline
)) proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, int32_t * retval
);
143 int __attribute__ ((noinline
)) proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
144 int __attribute__ ((noinline
)) proc_listcoalitions(int flavor
, int coaltype
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
145 int __attribute__ ((noinline
)) proc_can_use_foreground_hw(int pid
, user_addr_t reason
, uint32_t resonsize
, int32_t *retval
);
147 /* protos for procpidinfo calls */
148 int __attribute__ ((noinline
)) proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
149 int __attribute__ ((noinline
)) proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
*pbsd
, int zombie
);
150 int __attribute__ ((noinline
)) proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
*pbsd_shortp
, int zombie
);
151 int __attribute__ ((noinline
)) proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
*ptinfo
);
152 int __attribute__ ((noinline
)) proc_pidthreadinfo(proc_t p
, uint64_t arg
, int thuniqueid
, struct proc_threadinfo
*pthinfo
);
153 int __attribute__ ((noinline
)) proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
);
154 int __attribute__ ((noinline
)) proc_pidlistthreads(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
155 int __attribute__ ((noinline
)) proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
156 int __attribute__ ((noinline
)) proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
157 int __attribute__ ((noinline
)) proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
158 int __attribute__ ((noinline
)) proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
159 int __attribute__ ((noinline
)) proc_pidvnodepathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
160 int __attribute__ ((noinline
)) proc_pidpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
161 int __attribute__ ((noinline
)) proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
);
162 int __attribute__ ((noinline
)) proc_pidfileportlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
163 void __attribute__ ((noinline
)) proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
164 void __attribute__ ((noinline
)) proc_archinfo(proc_t p
, struct proc_archinfo
*pai
);
165 void __attribute__ ((noinline
)) proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*pci
);
166 int __attribute__ ((noinline
)) proc_pidnoteexit(proc_t p
, uint64_t arg
, uint32_t *data
);
167 int __attribute__ ((noinline
)) proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
);
168 int __attribute__ ((noinline
)) proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
);
171 /* protos for proc_pidfdinfo calls */
172 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
);
173 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
);
174 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
);
175 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
);
176 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
);
177 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
);
178 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
);
179 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
);
182 /* protos for misc */
184 int fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
);
185 void fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* finfo
);
186 int proc_security_policy(proc_t targetp
, int callnum
, int flavor
, boolean_t check_same_user
);
187 static void munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
);
188 static int proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
);
189 int proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
);
190 int proc_listfd_kqueue(proc_t p
, int32_t *fdlist
, int len
);
191 int proc_kqueue_udata_info(proc_t p
, int32_t fd
, uint64_t *buffer
, int bufsize
);
192 int proc_list_uptrs(proc_t p
, uint64_t *udata_buffer
, int size
);
194 extern int cansignal(struct proc
*, kauth_cred_t
, struct proc
*, int, int);
195 extern int proc_get_rusage(proc_t proc
, int flavor
, user_addr_t buffer
, int is_zombie
);
197 #define CHECK_SAME_USER TRUE
198 #define NO_CHECK_SAME_USER FALSE
200 uint64_t get_dispatchqueue_offset_from_proc(void *p
)
203 proc_t pself
= (proc_t
)p
;
204 return (pself
->p_dispatchqueue_offset
);
210 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p
)
213 proc_t pself
= (proc_t
)p
;
214 return (pself
->p_dispatchqueue_serialno_offset
);
220 /***************************** proc_info ********************/
223 proc_info(__unused
struct proc
*p
, struct proc_info_args
* uap
, int32_t *retval
)
225 return(proc_info_internal(uap
->callnum
, uap
->pid
, uap
->flavor
, uap
->arg
, uap
->buffer
, uap
->buffersize
, retval
));
230 proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
234 case PROC_INFO_CALL_LISTPIDS
:
235 /* pid contains type and flavor contains typeinfo */
236 return(proc_listpids(pid
, flavor
, buffer
, buffersize
, retval
));
237 case PROC_INFO_CALL_PIDINFO
:
238 return(proc_pidinfo(pid
, flavor
, arg
, buffer
, buffersize
, retval
));
239 case PROC_INFO_CALL_PIDFDINFO
:
240 return(proc_pidfdinfo(pid
, flavor
, (int)arg
, buffer
, buffersize
, retval
));
241 case PROC_INFO_CALL_KERNMSGBUF
:
242 return(proc_kernmsgbuf(buffer
, buffersize
, retval
));
243 case PROC_INFO_CALL_SETCONTROL
:
244 return(proc_setcontrol(pid
, flavor
, arg
, buffer
, buffersize
, retval
));
245 case PROC_INFO_CALL_PIDFILEPORTINFO
:
246 return(proc_pidfileportinfo(pid
, flavor
, (mach_port_name_t
)arg
, buffer
, buffersize
, retval
));
247 case PROC_INFO_CALL_TERMINATE
:
248 return(proc_terminate(pid
, retval
));
249 case PROC_INFO_CALL_DIRTYCONTROL
:
250 return(proc_dirtycontrol(pid
, flavor
, arg
, retval
));
251 case PROC_INFO_CALL_PIDRUSAGE
:
252 return (proc_pid_rusage(pid
, flavor
, buffer
, retval
));
253 case PROC_INFO_CALL_PIDORIGINATORINFO
:
254 return (proc_pidoriginatorinfo(pid
, flavor
, buffer
, buffersize
, retval
));
255 case PROC_INFO_CALL_LISTCOALITIONS
:
256 return proc_listcoalitions(pid
/* flavor */, flavor
/* coaltype */, buffer
,
258 case PROC_INFO_CALL_CANUSEFGHW
:
259 return proc_can_use_foreground_hw(pid
, buffer
, buffersize
, retval
);
267 /******************* proc_listpids routine ****************/
269 proc_listpids(uint32_t type
, uint32_t typeinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
271 int numprocs
, wantpids
;
278 struct proclist
*current_list
;
280 /* Do we have permission to look into this? */
281 if ((error
= proc_security_policy(PROC_NULL
, PROC_INFO_CALL_LISTPIDS
, type
, NO_CHECK_SAME_USER
)))
284 /* if the buffer is null, return num of procs */
285 if (buffer
== (user_addr_t
)0) {
286 *retval
= ((nprocs
+20) * sizeof(int));
290 if (buffersize
< sizeof(int)) {
293 wantpids
= buffersize
/sizeof(int);
294 numprocs
= nprocs
+20;
295 if (numprocs
> wantpids
)
298 kbuf
= (char *)kalloc((vm_size_t
)(numprocs
* sizeof(int)));
301 bzero(kbuf
, sizeof(int));
308 current_list
= &allproc
;
310 LIST_FOREACH(p
, current_list
, p_list
) {
314 if (p
->p_pgrpid
!= (pid_t
)typeinfo
)
318 if ((p
->p_ppid
!= (pid_t
)typeinfo
) && (((p
->p_lflag
& P_LTRACED
) == 0) || (p
->p_oppid
!= (pid_t
)typeinfo
)))
326 /* racy but list lock is held */
327 if ((p
->p_flag
& P_CONTROLT
) == 0 ||
328 (p
->p_pgrp
== NULL
) || (p
->p_pgrp
->pg_session
== NULL
) ||
329 (tp
= SESSION_TP(p
->p_pgrp
->pg_session
)) == TTY_NULL
||
330 tp
->t_dev
!= (dev_t
)typeinfo
)
334 if (p
->p_ucred
== NULL
)
337 kauth_cred_t my_cred
;
340 my_cred
= kauth_cred_proc_ref(p
);
341 uid
= kauth_cred_getuid(my_cred
);
342 kauth_cred_unref(&my_cred
);
343 if (uid
!= (uid_t
)typeinfo
)
348 if (p
->p_ucred
== NULL
)
351 kauth_cred_t my_cred
;
354 my_cred
= kauth_cred_proc_ref(p
);
355 uid
= kauth_cred_getruid(my_cred
);
356 kauth_cred_unref(&my_cred
);
357 if (uid
!= (uid_t
)typeinfo
)
374 if ((n
< numprocs
) && (current_list
== &allproc
)) {
375 current_list
= &zombproc
;
382 error
= copyout((caddr_t
)ptr
, buffer
, n
* sizeof(int));
384 *retval
= (n
* sizeof(int));
385 kfree((void *)kbuf
, (vm_size_t
)(numprocs
* sizeof(int)));
391 /********************************** proc_pidfdlist routines ********************************/
394 proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
398 struct proc_fdinfo
* pfd
;
399 struct fileproc
* fp
;
404 numfds
= p
->p_fd
->fd_nfiles
;
406 if (buffer
== (user_addr_t
) 0) {
408 *retval
= (numfds
* sizeof(struct proc_fdinfo
));
412 /* buffersize is big enough atleast for one struct */
413 needfds
= buffersize
/sizeof(struct proc_fdinfo
);
415 if (numfds
> needfds
)
418 kbuf
= (char *)kalloc((vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
421 bzero(kbuf
, numfds
* sizeof(struct proc_fdinfo
));
425 pfd
= (struct proc_fdinfo
*)kbuf
;
427 for (n
= 0; ((n
< numfds
) && (n
< p
->p_fd
->fd_nfiles
)); n
++) {
428 if (((fp
= p
->p_fd
->fd_ofiles
[n
]) != 0)
429 && ((p
->p_fd
->fd_ofileflags
[n
] & UF_RESERVED
) == 0)) {
430 file_type_t fdtype
= FILEGLOB_DTYPE(fp
->f_fglob
);
432 pfd
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
433 fdtype
: PROX_FDTYPE_ATALK
;
440 error
= copyout(kbuf
, buffer
, count
* sizeof(struct proc_fdinfo
));
441 kfree((void *)kbuf
, (vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
443 *retval
= (count
* sizeof(struct proc_fdinfo
));
448 * Helper functions for proc_pidfileportlist.
451 proc_fileport_count(__unused mach_port_name_t name
,
452 __unused
struct fileglob
*fg
, void *arg
)
454 uint32_t *counter
= arg
;
460 struct fileport_fdtype_args
{
461 struct proc_fileportinfo
*ffa_pfi
;
462 struct proc_fileportinfo
*ffa_pfi_end
;
466 proc_fileport_fdtype(mach_port_name_t name
, struct fileglob
*fg
, void *arg
)
468 struct fileport_fdtype_args
*ffa
= arg
;
470 if (ffa
->ffa_pfi
!= ffa
->ffa_pfi_end
) {
471 file_type_t fdtype
= FILEGLOB_DTYPE(fg
);
473 ffa
->ffa_pfi
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
474 fdtype
: PROX_FDTYPE_ATALK
;
475 ffa
->ffa_pfi
->proc_fileport
= name
;
477 return (0); /* keep walking */
479 return (-1); /* stop the walk! */
483 proc_pidfileportlist(proc_t p
,
484 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
488 struct proc_fileportinfo
*pfi
;
489 uint32_t needfileports
, numfileports
;
490 struct fileport_fdtype_args ffa
;
493 needfileports
= buffersize
/ sizeof (*pfi
);
494 if ((user_addr_t
)0 == buffer
|| needfileports
> (uint32_t)maxfiles
) {
496 * Either (i) the user is asking for a fileport count,
497 * or (ii) the number of fileports they're asking for is
498 * larger than the maximum number of open files (!); count
499 * them to bound subsequent heap allocations.
502 switch (fileport_walk(p
->task
,
503 proc_fileport_count
, &numfileports
)) {
506 case KERN_RESOURCE_SHORTAGE
:
508 case KERN_INVALID_TASK
:
514 if (numfileports
== 0) {
515 *retval
= 0; /* none at all, bail */
518 if ((user_addr_t
)0 == buffer
) {
519 numfileports
+= 20; /* accelerate convergence */
520 *retval
= numfileports
* sizeof (*pfi
);
523 if (needfileports
> numfileports
)
524 needfileports
= numfileports
;
527 assert(buffersize
>= PROC_PIDLISTFILEPORTS_SIZE
);
529 kbufsize
= (vm_size_t
)needfileports
* sizeof (*pfi
);
530 pfi
= kbuf
= kalloc(kbufsize
);
533 bzero(kbuf
, kbufsize
);
536 ffa
.ffa_pfi_end
= pfi
+ needfileports
;
538 switch (fileport_walk(p
->task
, proc_fileport_fdtype
, &ffa
)) {
542 if ((numfileports
= pfi
- (typeof(pfi
))kbuf
) == 0)
544 if (numfileports
> needfileports
)
545 panic("more fileports returned than requested");
546 error
= copyout(kbuf
, buffer
, numfileports
* sizeof (*pfi
));
548 case KERN_RESOURCE_SHORTAGE
:
551 case KERN_INVALID_TASK
:
558 kfree(kbuf
, kbufsize
);
560 *retval
= numfileports
* sizeof (*pfi
);
565 proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
* pbsd
, int zombie
)
568 struct session
*sessionp
= NULL
;
570 kauth_cred_t my_cred
;
573 sessionp
= proc_session(p
);
575 my_cred
= kauth_cred_proc_ref(p
);
576 bzero(pbsd
, sizeof(struct proc_bsdinfo
));
577 pbsd
->pbi_status
= p
->p_stat
;
578 pbsd
->pbi_xstatus
= p
->p_xstat
;
579 pbsd
->pbi_pid
= p
->p_pid
;
580 pbsd
->pbi_ppid
= p
->p_ppid
;
581 pbsd
->pbi_uid
= kauth_cred_getuid(my_cred
);
582 pbsd
->pbi_gid
= kauth_cred_getgid(my_cred
);
583 pbsd
->pbi_ruid
= kauth_cred_getruid(my_cred
);
584 pbsd
->pbi_rgid
= kauth_cred_getrgid(my_cred
);
585 pbsd
->pbi_svuid
= kauth_cred_getsvuid(my_cred
);
586 pbsd
->pbi_svgid
= kauth_cred_getsvgid(my_cred
);
587 kauth_cred_unref(&my_cred
);
589 pbsd
->pbi_nice
= p
->p_nice
;
590 pbsd
->pbi_start_tvsec
= p
->p_start
.tv_sec
;
591 pbsd
->pbi_start_tvusec
= p
->p_start
.tv_usec
;
592 bcopy(&p
->p_comm
, &pbsd
->pbi_comm
[0], MAXCOMLEN
);
593 pbsd
->pbi_comm
[MAXCOMLEN
- 1] = '\0';
594 bcopy(&p
->p_name
, &pbsd
->pbi_name
[0], 2*MAXCOMLEN
);
595 pbsd
->pbi_name
[(2*MAXCOMLEN
) - 1] = '\0';
598 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
)
599 pbsd
->pbi_flags
|= PROC_FLAG_SYSTEM
;
600 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
)
601 pbsd
->pbi_flags
|= PROC_FLAG_TRACED
;
602 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
)
603 pbsd
->pbi_flags
|= PROC_FLAG_INEXIT
;
604 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
)
605 pbsd
->pbi_flags
|= PROC_FLAG_PPWAIT
;
606 if ((p
->p_flag
& P_LP64
) == P_LP64
)
607 pbsd
->pbi_flags
|= PROC_FLAG_LP64
;
608 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
)
609 pbsd
->pbi_flags
|= PROC_FLAG_CONTROLT
;
610 if ((p
->p_flag
& P_THCWD
) == P_THCWD
)
611 pbsd
->pbi_flags
|= PROC_FLAG_THCWD
;
612 if ((p
->p_flag
& P_SUGID
) == P_SUGID
)
613 pbsd
->pbi_flags
|= PROC_FLAG_PSUGID
;
614 if ((p
->p_flag
& P_EXEC
) == P_EXEC
)
615 pbsd
->pbi_flags
|= PROC_FLAG_EXEC
;
617 if (sessionp
!= SESSION_NULL
) {
618 if (SESS_LEADER(p
, sessionp
))
619 pbsd
->pbi_flags
|= PROC_FLAG_SLEADER
;
620 if (sessionp
->s_ttyvp
)
621 pbsd
->pbi_flags
|= PROC_FLAG_CTTY
;
624 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
625 pbsd
->pbi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
627 switch(PROC_CONTROL_STATE(p
)) {
629 pbsd
->pbi_flags
|= PROC_FLAG_PC_THROTTLE
;
632 pbsd
->pbi_flags
|= PROC_FLAG_PC_SUSP
;
635 pbsd
->pbi_flags
|= PROC_FLAG_PC_KILL
;
639 switch(PROC_ACTION_STATE(p
)) {
641 pbsd
->pbi_flags
|= PROC_FLAG_PA_THROTTLE
;
644 pbsd
->pbi_flags
|= PROC_FLAG_PA_SUSP
;
648 /* if process is a zombie skip bg state */
649 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
))
650 proc_get_darwinbgstate(p
->task
, &pbsd
->pbi_flags
);
653 pbsd
->pbi_nfiles
= p
->p_fd
->fd_nfiles
;
655 pbsd
->e_tdev
= NODEV
;
656 if (pg
!= PGRP_NULL
) {
657 pbsd
->pbi_pgid
= p
->p_pgrpid
;
658 pbsd
->pbi_pjobc
= pg
->pg_jobc
;
659 if ((p
->p_flag
& P_CONTROLT
) && (sessionp
!= SESSION_NULL
) && (tp
= SESSION_TP(sessionp
))) {
660 pbsd
->e_tdev
= tp
->t_dev
;
661 pbsd
->e_tpgid
= sessionp
->s_ttypgrpid
;
664 if (sessionp
!= SESSION_NULL
)
665 session_rele(sessionp
);
674 proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
* pbsd_shortp
, int zombie
)
676 bzero(pbsd_shortp
, sizeof(struct proc_bsdshortinfo
));
677 pbsd_shortp
->pbsi_pid
= p
->p_pid
;
678 pbsd_shortp
->pbsi_ppid
= p
->p_ppid
;
679 pbsd_shortp
->pbsi_pgid
= p
->p_pgrpid
;
680 pbsd_shortp
->pbsi_status
= p
->p_stat
;
681 bcopy(&p
->p_comm
, &pbsd_shortp
->pbsi_comm
[0], MAXCOMLEN
);
682 pbsd_shortp
->pbsi_comm
[MAXCOMLEN
- 1] = '\0';
684 pbsd_shortp
->pbsi_flags
= 0;
685 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
)
686 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_SYSTEM
;
687 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
)
688 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_TRACED
;
689 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
)
690 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_INEXIT
;
691 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
)
692 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PPWAIT
;
693 if ((p
->p_flag
& P_LP64
) == P_LP64
)
694 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_LP64
;
695 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
)
696 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_CONTROLT
;
697 if ((p
->p_flag
& P_THCWD
) == P_THCWD
)
698 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_THCWD
;
699 if ((p
->p_flag
& P_SUGID
) == P_SUGID
)
700 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PSUGID
;
701 if ((p
->p_flag
& P_EXEC
) == P_EXEC
)
702 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_EXEC
;
703 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
704 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
706 switch(PROC_CONTROL_STATE(p
)) {
708 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_THROTTLE
;
711 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_SUSP
;
714 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_KILL
;
718 switch(PROC_ACTION_STATE(p
)) {
720 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_THROTTLE
;
723 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_SUSP
;
727 /* if process is a zombie skip bg state */
728 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
))
729 proc_get_darwinbgstate(p
->task
, &pbsd_shortp
->pbsi_flags
);
731 pbsd_shortp
->pbsi_uid
= p
->p_uid
;
732 pbsd_shortp
->pbsi_gid
= p
->p_gid
;
733 pbsd_shortp
->pbsi_ruid
= p
->p_ruid
;
734 pbsd_shortp
->pbsi_rgid
= p
->p_rgid
;
735 pbsd_shortp
->pbsi_svuid
= p
->p_svuid
;
736 pbsd_shortp
->pbsi_svgid
= p
->p_svgid
;
742 proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
* ptinfo
)
748 bzero(ptinfo
, sizeof(struct proc_taskinfo
));
749 fill_taskprocinfo(task
, (struct proc_taskinfo_internal
*)ptinfo
);
757 proc_pidthreadinfo(proc_t p
, uint64_t arg
, int thuniqueid
, struct proc_threadinfo
*pthinfo
)
760 uint64_t threadaddr
= (uint64_t)arg
;
762 bzero(pthinfo
, sizeof(struct proc_threadinfo
));
764 error
= fill_taskthreadinfo(p
->task
, threadaddr
, thuniqueid
, (struct proc_threadinfo_internal
*)pthinfo
, NULL
, NULL
);
773 bsd_hasthreadname(void *uth
)
775 struct uthread
*ut
= (struct uthread
*)uth
;
777 /* This doesn't check for the empty string; do we care? */
786 bsd_getthreadname(void *uth
, char *buffer
)
788 struct uthread
*ut
= (struct uthread
*)uth
;
790 bcopy(ut
->pth_name
,buffer
,MAXTHREADNAMESIZE
);
794 * This is known to race with regards to the contents of the thread name; concurrent
795 * callers may result in a garbled name.
798 bsd_setthreadname(void *uth
, const char *name
) {
799 struct uthread
*ut
= (struct uthread
*)uth
;
800 char * name_buf
= NULL
;
803 /* If there is no existing thread name, allocate a buffer for one. */
804 name_buf
= kalloc(MAXTHREADNAMESIZE
);
806 bzero(name_buf
, MAXTHREADNAMESIZE
);
808 /* Someone could conceivably have named the thread at the same time we did. */
809 if (!OSCompareAndSwapPtr(NULL
, name_buf
, &ut
->pth_name
)) {
810 kfree(name_buf
, MAXTHREADNAMESIZE
);
813 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV
, ut
->pth_name
);
816 strncpy(ut
->pth_name
, name
, MAXTHREADNAMESIZE
- 1);
817 kernel_debug_string_simple(TRACE_STRING_THREADNAME
, ut
->pth_name
);
821 bsd_copythreadname(void *dst_uth
, void *src_uth
)
823 struct uthread
*dst_ut
= (struct uthread
*)dst_uth
;
824 struct uthread
*src_ut
= (struct uthread
*)src_uth
;
826 if (src_ut
->pth_name
== NULL
)
829 if (dst_ut
->pth_name
== NULL
) {
830 dst_ut
->pth_name
= (char *)kalloc(MAXTHREADNAMESIZE
);
831 if (dst_ut
->pth_name
== NULL
)
835 bcopy(src_ut
->pth_name
, dst_ut
->pth_name
, MAXTHREADNAMESIZE
);
840 bsd_threadcdir(void * uth
, void *vptr
, int *vidp
)
842 struct uthread
* ut
= (struct uthread
*)uth
;
844 vnode_t
*vpp
= (vnode_t
*)vptr
;
858 proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
)
863 uint64_t threadaddr
= (uint64_t)arg
;
866 bzero(pinfo
, sizeof(struct proc_threadwithpathinfo
));
868 error
= fill_taskthreadinfo(p
->task
, threadaddr
, 0, (struct proc_threadinfo_internal
*)&pinfo
->pt
, (void *)&vp
, &vid
);
872 if ((vp
!= NULLVP
) && ((vnode_getwithvid(vp
, vid
)) == 0)) {
873 error
= fill_vnodeinfo(vp
, &pinfo
->pvip
.vip_vi
) ;
876 vn_getpath(vp
, &pinfo
->pvip
.vip_path
[0], &count
);
877 pinfo
->pvip
.vip_path
[MAXPATHLEN
-1] = 0;
887 proc_pidlistthreads(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
896 count
= buffersize
/(sizeof(uint64_t));
897 numthreads
= get_numthreads(p
->task
);
901 if (numthreads
> count
)
904 kbuf
= (void *)kalloc(numthreads
* sizeof(uint64_t));
907 bzero(kbuf
, numthreads
* sizeof(uint64_t));
909 ret
= fill_taskthreadlist(p
->task
, kbuf
, numthreads
);
911 error
= copyout(kbuf
, buffer
, ret
);
912 kfree(kbuf
, numthreads
* sizeof(uint64_t));
921 proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
923 struct proc_regioninfo preginfo
;
926 bzero(&preginfo
, sizeof(struct proc_regioninfo
));
927 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
, (uintptr_t *)0, (uint32_t *)0);
930 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regioninfo
));
932 *retval
= sizeof(struct proc_regioninfo
);
938 proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
940 struct proc_regionwithpathinfo preginfo
;
942 uintptr_t vnodeaddr
= 0;
947 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
949 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
953 vp
= (vnode_t
)vnodeaddr
;
954 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
955 /* FILL THE VNODEINFO */
956 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
958 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
959 /* Always make sure it is null terminated */
960 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
964 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
966 *retval
= sizeof(struct proc_regionwithpathinfo
);
971 proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
973 struct proc_regionwithpathinfo preginfo
;
975 uintptr_t vnodeaddr
= 0;
980 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
982 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
988 vp
= (vnode_t
)vnodeaddr
;
989 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
990 /* FILL THE VNODEINFO */
991 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
993 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
994 /* Always make sure it is null terminated */
995 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
1001 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1003 *retval
= sizeof(struct proc_regionwithpathinfo
);
1008 proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1010 struct proc_regionwithpathinfo preginfo
;
1012 uintptr_t vnodeaddr
;
1018 /* Loop while looking for vnodes that match dev_t filter */
1020 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
1024 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, addr
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
1030 vp
= (vnode_t
)vnodeaddr
;
1031 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
1032 /* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
1033 struct vnode_attr va
;
1035 memset(&va
, 0, sizeof(va
));
1037 VATTR_WANTED(&va
, va_fsid
);
1039 ret
= vnode_getattr(vp
, &va
, vfs_context_current());
1045 if (va
.va_fsid
== arg
) {
1046 /* FILL THE VNODEINFO */
1047 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
1049 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
1050 /* Always make sure it is null terminated */
1051 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
1060 addr
= preginfo
.prp_prinfo
.pri_address
+ preginfo
.prp_prinfo
.pri_size
;
1063 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
1065 *retval
= sizeof(struct proc_regionwithpathinfo
);
1070 * Path is relative to current process directory; may different from current
1074 proc_pidvnodepathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1076 struct proc_vnodepathinfo pvninfo
;
1078 vnode_t vncdirvp
= NULLVP
;
1079 uint32_t vncdirid
=0;
1080 vnode_t vnrdirvp
= NULLVP
;
1081 uint32_t vnrdirid
=0;
1084 bzero(&pvninfo
, sizeof(struct proc_vnodepathinfo
));
1087 if (p
->p_fd
->fd_cdir
) {
1088 vncdirvp
= p
->p_fd
->fd_cdir
;
1089 vncdirid
= p
->p_fd
->fd_cdir
->v_id
;
1091 if (p
->p_fd
->fd_rdir
) {
1092 vnrdirvp
= p
->p_fd
->fd_rdir
;
1093 vnrdirid
= p
->p_fd
->fd_rdir
->v_id
;
1097 if (vncdirvp
!= NULLVP
) {
1098 if ((error
= vnode_getwithvid(vncdirvp
, vncdirid
)) == 0) {
1099 /* FILL THE VNODEINFO */
1100 error
= fill_vnodeinfo(vncdirvp
, &pvninfo
.pvi_cdir
.vip_vi
);
1103 vn_getpath(vncdirvp
, &pvninfo
.pvi_cdir
.vip_path
[0], &count
);
1104 pvninfo
.pvi_cdir
.vip_path
[MAXPATHLEN
-1] = 0;
1106 vnode_put(vncdirvp
);
1112 if ((error
== 0) && (vnrdirvp
!= NULLVP
)) {
1113 if ((error
= vnode_getwithvid(vnrdirvp
, vnrdirid
)) == 0) {
1114 /* FILL THE VNODEINFO */
1115 error
= fill_vnodeinfo(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_vi
);
1118 vn_getpath(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_path
[0], &count
);
1119 pvninfo
.pvi_rdir
.vip_path
[MAXPATHLEN
-1] = 0;
1121 vnode_put(vnrdirvp
);
1127 error
= copyout(&pvninfo
, buffer
, sizeof(struct proc_vnodepathinfo
));
1129 *retval
= sizeof(struct proc_vnodepathinfo
);
1136 proc_pidpathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t *retval
)
1140 int len
= buffersize
;
1148 buf
= (char *)kalloc(buffersize
);
1152 error
= proc_pidpathinfo_internal(p
, arg
, buf
, buffersize
, retval
);
1154 error
= copyout(buf
, buffer
, len
);
1156 kfree(buf
, buffersize
);
1161 proc_pidpathinfo_internal(proc_t p
, __unused
uint64_t arg
, char *buf
, uint32_t buffersize
, __unused
int32_t *retval
)
1165 vnode_t nvp
= NULLVP
;
1166 int len
= buffersize
;
1173 vid
= vnode_vid(tvp
);
1174 error
= vnode_getwithvid(tvp
, vid
);
1176 error
= vn_getpath_fsenter(tvp
, buf
, &len
);
1179 error
= vnode_lookup(buf
, 0, &nvp
, vfs_context_current());
1180 if ((error
== 0) && ( nvp
!= NULLVP
))
1189 proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
)
1193 bzero(pwqinfo
, sizeof(struct proc_workqueueinfo
));
1195 error
= fill_procworkqueue(p
, pwqinfo
);
1205 proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
)
1207 p_uniqidinfo
->p_uniqueid
= proc_uniqueid(p
);
1208 proc_getexecutableuuid(p
, (unsigned char *)&p_uniqidinfo
->p_uuid
, sizeof(p_uniqidinfo
->p_uuid
));
1209 p_uniqidinfo
->p_puniqueid
= proc_puniqueid(p
);
1210 p_uniqidinfo
->p_reserve2
= 0;
1211 p_uniqidinfo
->p_reserve3
= 0;
1212 p_uniqidinfo
->p_reserve4
= 0;
1217 proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
)
1219 struct proc
* p
= PROC_NULL
;
1222 if (buffersize
< sizeof(uuid_t
))
1225 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1226 p
= proc_find_zombref(pid
);
1229 if (p
== PROC_NULL
) {
1233 proc_getexecutableuuid(p
, (unsigned char *)uuid_buf
, buffersize
);
1236 proc_drop_zombref(p
);
1244 * Function to get the uuid and pid of the originator of the voucher.
1247 proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
)
1249 pid_t originator_pid
;
1254 * Get the current voucher origin pid. The pid returned here
1255 * might not be valid or may have been recycled.
1257 kr
= thread_get_current_voucher_origin_pid(&originator_pid
);
1258 /* If errors, convert errors to appropriate format */
1260 if (kr
== KERN_INVALID_TASK
)
1262 else if (kr
== KERN_INVALID_VALUE
)
1269 *pid
= originator_pid
;
1270 error
= proc_piduuidinfo(originator_pid
, uuid
, buffersize
);
1275 * Function to get the uuid of the originator of the voucher.
1278 proc_pidoriginatoruuid(uuid_t uuid
, uint32_t buffersize
)
1280 pid_t originator_pid
;
1281 return (proc_pidoriginatorpid_uuid(uuid
, buffersize
, &originator_pid
));
1284 /***************************** proc_pidoriginatorinfo ***************************/
1287 proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1289 int error
= ENOTSUP
;
1293 case PROC_PIDORIGINATOR_UUID
:
1294 size
= PROC_PIDORIGINATOR_UUID_SIZE
;
1296 case PROC_PIDORIGINATOR_BGSTATE
:
1297 size
= PROC_PIDORIGINATOR_BGSTATE_SIZE
;
1299 case PROC_PIDORIGINATOR_PID_UUID
:
1300 size
= PROC_PIDORIGINATOR_PID_UUID_SIZE
;
1306 if (buffersize
< size
)
1309 if (pid
!= 0 && pid
!= proc_selfpid())
1313 case PROC_PIDORIGINATOR_UUID
: {
1316 error
= proc_pidoriginatoruuid(uuid
, sizeof(uuid
));
1320 error
= copyout(uuid
, buffer
, size
);
1326 case PROC_PIDORIGINATOR_PID_UUID
: {
1327 struct proc_originatorinfo originator_info
;
1328 bzero(&originator_info
, sizeof(originator_info
));
1330 error
= proc_pidoriginatorpid_uuid(originator_info
.originator_uuid
,
1331 sizeof(uuid_t
), &originator_info
.originator_pid
);
1335 error
= copyout(&originator_info
, buffer
, size
);
1341 case PROC_PIDORIGINATOR_BGSTATE
: {
1342 uint32_t is_backgrounded
;
1343 error
= proc_get_originatorbgstate(&is_backgrounded
);
1347 error
= copyout(&is_backgrounded
, buffer
, size
);
1360 /***************************** proc_listcoalitions ***************************/
1361 int proc_listcoalitions(int flavor
, int type
, user_addr_t buffer
,
1362 uint32_t buffersize
, int32_t *retval
)
1364 #if CONFIG_COALITIONS
1365 int error
= ENOTSUP
;
1368 void *coalinfo
= NULL
;
1369 uint32_t k_buffersize
= 0, copyout_sz
= 0;
1370 int ncoals
= 0, ncoals_
= 0;
1372 /* struct procinfo_coalinfo; */
1375 case LISTCOALITIONS_ALL_COALS
:
1376 elem_size
= LISTCOALITIONS_ALL_COALS_SIZE
;
1379 case LISTCOALITIONS_SINGLE_TYPE
:
1380 elem_size
= LISTCOALITIONS_SINGLE_TYPE_SIZE
;
1387 /* find the total number of coalitions */
1388 ncoals
= coalitions_get_list(coal_type
, NULL
, 0);
1390 if (ncoals
== 0 || buffer
== 0 || buffersize
== 0) {
1392 * user just wants buffer size
1393 * or there are no coalitions
1396 *retval
= (int)(ncoals
* elem_size
);
1400 k_buffersize
= ncoals
* elem_size
;
1401 coalinfo
= kalloc((vm_size_t
)k_buffersize
);
1406 bzero(coalinfo
, k_buffersize
);
1409 case LISTCOALITIONS_ALL_COALS
:
1410 case LISTCOALITIONS_SINGLE_TYPE
:
1411 ncoals_
= coalitions_get_list(coal_type
, coalinfo
, ncoals
);
1414 panic("memory corruption?!");
1418 /* all the coalitions disappeared... weird but valid */
1425 * Some coalitions may have disappeared between our initial check,
1426 * and the the actual list acquisition.
1427 * Only copy out what we really need.
1429 copyout_sz
= k_buffersize
;
1430 if (ncoals_
< ncoals
)
1431 copyout_sz
= ncoals_
* elem_size
;
1434 * copy the list up to user space
1435 * (we're guaranteed to have a non-null pointer/size here)
1437 error
= copyout(coalinfo
, buffer
,
1438 copyout_sz
< buffersize
? copyout_sz
: buffersize
);
1441 *retval
= (int)copyout_sz
;
1445 kfree(coalinfo
, k_buffersize
);
1449 /* no coalition support */
1460 /*************************** proc_can_use_forgeound_hw **************************/
1461 int proc_can_use_foreground_hw(int pid
, user_addr_t u_reason
, uint32_t reasonsize
, int32_t *retval
)
1463 proc_t p
= PROC_NULL
;
1465 uint32_t reason
= PROC_FGHW_ERROR
;
1467 task_t task
= TASK_NULL
;
1468 #if CONFIG_COALITIONS
1469 coalition_t coal
= COALITION_NULL
;
1476 reason
= PROC_FGHW_ERROR
;
1481 if (p
== PROC_NULL
) {
1483 reason
= PROC_FGHW_ERROR
;
1487 #if CONFIG_COALITIONS
1488 if (p
!= current_proc() &&
1489 !kauth_cred_issuser(kauth_cred_get())) {
1491 reason
= PROC_FGHW_ERROR
;
1496 task_reference(task
);
1497 if (coalition_is_leader(task
, COALITION_TYPE_JETSAM
, &coal
) == FALSE
) {
1498 /* current task is not a coalition leader: find the leader */
1499 task_deallocate(task
);
1500 task
= coalition_get_leader(coal
);
1503 if (task
!= TASK_NULL
) {
1505 * If task is non-null, then it is the coalition leader of the
1506 * current process' coalition. This could be the same task as
1507 * the current_task, and that's OK.
1512 proc_get_darwinbgstate(task
, &flags
);
1513 if ((flags
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
1515 * Coalition leader is not an application, continue
1516 * searching for other ways this task could gain
1519 reason
= PROC_FGHW_DAEMON_LEADER
;
1523 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
)) {
1525 * If the leader of the current process' coalition has
1526 * been marked as DARWIN_BG, then it definitely should
1527 * not be using foreground hardware resources.
1529 reason
= PROC_FGHW_LEADER_BACKGROUND
;
1533 role
= proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
);
1535 case TASK_FOREGROUND_APPLICATION
: /* DARWIN_ROLE_UI_FOCAL */
1536 case TASK_BACKGROUND_APPLICATION
: /* DARWIN_ROLE_UI */
1538 * The leader of this coalition is a focal, UI app:
1540 * TODO: should extensions/plugins be allowed to use
1544 reason
= PROC_FGHW_OK
;
1546 case TASK_DEFAULT_APPLICATION
: /* DARWIN_ROLE_UI_NON_FOCAL */
1547 case TASK_NONUI_APPLICATION
: /* DARWIN_ROLE_NON_UI */
1548 case TASK_THROTTLE_APPLICATION
:
1549 case TASK_UNSPECIFIED
:
1551 /* non-focal, non-ui apps don't get access */
1552 reason
= PROC_FGHW_LEADER_NONUI
;
1558 if (task
!= TASK_NULL
) {
1559 task_deallocate(task
);
1562 #endif /* CONFIG_COALITIONS */
1565 * There is no reasonable semantic to investigate the currently
1566 * adopted voucher of an arbitrary thread in a non-current process.
1569 if (p
!= current_proc()) {
1575 * In the absence of coalitions, fall back to a voucher-based lookup
1576 * where a daemon can used foreground HW if it's operating on behalf
1577 * of a foreground application.
1578 * NOTE: this is equivalent to a call to
1579 * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
1582 error
= proc_get_originatorbgstate(&isBG
);
1587 reason
= PROC_FGHW_NO_ORIGINATOR
;
1591 reason
= PROC_FGHW_NO_VOUCHER_ATTR
;
1595 reason
= PROC_FGHW_DAEMON_NO_VOUCHER
;
1599 /* some other error occurred: report that to the caller */
1600 reason
= PROC_FGHW_VOUCHER_ERROR
;
1605 reason
= PROC_FGHW_ORIGINATOR_BACKGROUND
;
1609 * The process itself is either a foreground app, or has
1610 * adopted a voucher originating from an app that's still in
1613 reason
= PROC_FGHW_DAEMON_OK
;
1618 if (task
!= TASK_NULL
)
1619 task_deallocate(task
);
1622 if (reasonsize
>= sizeof(reason
) && u_reason
!= (user_addr_t
)0)
1623 (void)copyout(&reason
, u_reason
, sizeof(reason
));
1628 /********************************** proc_pidinfo ********************************/
1632 proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1634 struct proc
* p
= PROC_NULL
;
1635 int error
= ENOTSUP
;
1638 int shortversion
= 0;
1642 int uniqidversion
= 0;
1643 boolean_t check_same_user
;
1646 case PROC_PIDLISTFDS
:
1647 size
= PROC_PIDLISTFD_SIZE
;
1648 if (buffer
== (user_addr_t
)0)
1651 case PROC_PIDTBSDINFO
:
1652 size
= PROC_PIDTBSDINFO_SIZE
;
1654 case PROC_PIDTASKINFO
:
1655 size
= PROC_PIDTASKINFO_SIZE
;
1657 case PROC_PIDTASKALLINFO
:
1658 size
= PROC_PIDTASKALLINFO_SIZE
;
1660 case PROC_PIDTHREADINFO
:
1661 size
= PROC_PIDTHREADINFO_SIZE
;
1663 case PROC_PIDLISTTHREADS
:
1664 size
= PROC_PIDLISTTHREADS_SIZE
;
1666 case PROC_PIDREGIONINFO
:
1667 size
= PROC_PIDREGIONINFO_SIZE
;
1669 case PROC_PIDREGIONPATHINFO
:
1670 size
= PROC_PIDREGIONPATHINFO_SIZE
;
1672 case PROC_PIDVNODEPATHINFO
:
1673 size
= PROC_PIDVNODEPATHINFO_SIZE
;
1675 case PROC_PIDTHREADPATHINFO
:
1676 size
= PROC_PIDTHREADPATHINFO_SIZE
;
1678 case PROC_PIDPATHINFO
:
1681 case PROC_PIDWORKQUEUEINFO
:
1682 /* kernel does not have workq info */
1686 size
= PROC_PIDWORKQUEUEINFO_SIZE
;
1688 case PROC_PIDT_SHORTBSDINFO
:
1689 size
= PROC_PIDT_SHORTBSDINFO_SIZE
;
1691 case PROC_PIDLISTFILEPORTS
:
1692 size
= PROC_PIDLISTFILEPORTS_SIZE
;
1693 if (buffer
== (user_addr_t
)0)
1696 case PROC_PIDTHREADID64INFO
:
1697 size
= PROC_PIDTHREADID64INFO_SIZE
;
1699 case PROC_PIDUNIQIDENTIFIERINFO
:
1700 size
= PROC_PIDUNIQIDENTIFIERINFO_SIZE
;
1702 case PROC_PIDT_BSDINFOWITHUNIQID
:
1703 size
= PROC_PIDT_BSDINFOWITHUNIQID_SIZE
;
1705 case PROC_PIDARCHINFO
:
1706 size
= PROC_PIDARCHINFO_SIZE
;
1708 case PROC_PIDCOALITIONINFO
:
1709 size
= PROC_PIDCOALITIONINFO_SIZE
;
1711 case PROC_PIDNOTEEXIT
:
1713 * Set findzomb explicitly because arg passed
1714 * in is used as note exit status bits.
1716 size
= PROC_PIDNOTEEXIT_SIZE
;
1719 case PROC_PIDEXITREASONINFO
:
1720 size
= PROC_PIDEXITREASONINFO_SIZE
;
1723 case PROC_PIDEXITREASONBASICINFO
:
1724 size
= PROC_PIDEXITREASONBASICINFOSIZE
;
1727 case PROC_PIDREGIONPATHINFO2
:
1728 size
= PROC_PIDREGIONPATHINFO2_SIZE
;
1730 case PROC_PIDREGIONPATHINFO3
:
1731 size
= PROC_PIDREGIONPATHINFO3_SIZE
;
1737 if (buffersize
< size
)
1740 if ((flavor
== PROC_PIDPATHINFO
) && (buffersize
> PROC_PIDPATHINFO_MAXSIZE
)) {
1744 /* Check if we need to look for zombies */
1745 if ((flavor
== PROC_PIDTBSDINFO
) || (flavor
== PROC_PIDT_SHORTBSDINFO
) || (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1746 || (flavor
== PROC_PIDUNIQIDENTIFIERINFO
)) {
1751 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1753 p
= proc_find_zombref(pid
);
1754 if (p
== PROC_NULL
) {
1763 /* Certain operations don't require privileges */
1765 case PROC_PIDT_SHORTBSDINFO
:
1766 case PROC_PIDUNIQIDENTIFIERINFO
:
1767 case PROC_PIDPATHINFO
:
1768 case PROC_PIDCOALITIONINFO
:
1769 check_same_user
= NO_CHECK_SAME_USER
;
1772 check_same_user
= CHECK_SAME_USER
;
1776 /* Do we have permission to look into this? */
1777 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDINFO
, flavor
, check_same_user
)))
1781 case PROC_PIDLISTFDS
: {
1782 error
= proc_pidfdlist(p
, buffer
, buffersize
, retval
);
1786 case PROC_PIDUNIQIDENTIFIERINFO
: {
1787 struct proc_uniqidentifierinfo p_uniqidinfo
;
1788 bzero(&p_uniqidinfo
, sizeof(p_uniqidinfo
));
1789 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
1790 error
= copyout(&p_uniqidinfo
, buffer
, sizeof(struct proc_uniqidentifierinfo
));
1792 *retval
= sizeof(struct proc_uniqidentifierinfo
);
1796 case PROC_PIDT_SHORTBSDINFO
:
1798 case PROC_PIDT_BSDINFOWITHUNIQID
:
1799 case PROC_PIDTBSDINFO
: {
1800 struct proc_bsdinfo pbsd
;
1801 struct proc_bsdshortinfo pbsd_short
;
1802 struct proc_bsdinfowithuniqid pbsd_uniqid
;
1804 if (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1807 if (shortversion
!= 0) {
1808 error
= proc_pidshortbsdinfo(p
, &pbsd_short
, zombie
);
1810 error
= proc_pidbsdinfo(p
, &pbsd
, zombie
);
1811 if (uniqidversion
!= 0) {
1812 bzero(&pbsd_uniqid
, sizeof(pbsd_uniqid
));
1813 proc_piduniqidentifierinfo(p
, &pbsd_uniqid
.p_uniqidentifier
);
1814 pbsd_uniqid
.pbsd
= pbsd
;
1819 if (shortversion
!= 0) {
1820 error
= copyout(&pbsd_short
, buffer
, sizeof(struct proc_bsdshortinfo
));
1822 *retval
= sizeof(struct proc_bsdshortinfo
);
1823 } else if (uniqidversion
!= 0) {
1824 error
= copyout(&pbsd_uniqid
, buffer
, sizeof(struct proc_bsdinfowithuniqid
));
1826 *retval
= sizeof(struct proc_bsdinfowithuniqid
);
1828 error
= copyout(&pbsd
, buffer
, sizeof(struct proc_bsdinfo
));
1830 *retval
= sizeof(struct proc_bsdinfo
);
1836 case PROC_PIDTASKINFO
: {
1837 struct proc_taskinfo ptinfo
;
1839 error
= proc_pidtaskinfo(p
, &ptinfo
);
1841 error
= copyout(&ptinfo
, buffer
, sizeof(struct proc_taskinfo
));
1843 *retval
= sizeof(struct proc_taskinfo
);
1848 case PROC_PIDTASKALLINFO
: {
1849 struct proc_taskallinfo pall
;
1850 bzero(&pall
, sizeof(pall
));
1851 error
= proc_pidbsdinfo(p
, &pall
.pbsd
, 0);
1852 error
= proc_pidtaskinfo(p
, &pall
.ptinfo
);
1854 error
= copyout(&pall
, buffer
, sizeof(struct proc_taskallinfo
));
1856 *retval
= sizeof(struct proc_taskallinfo
);
1861 case PROC_PIDTHREADID64INFO
:
1863 case PROC_PIDTHREADINFO
:{
1864 struct proc_threadinfo pthinfo
;
1866 error
= proc_pidthreadinfo(p
, arg
, thuniqueid
, &pthinfo
);
1868 error
= copyout(&pthinfo
, buffer
, sizeof(struct proc_threadinfo
));
1870 *retval
= sizeof(struct proc_threadinfo
);
1875 case PROC_PIDLISTTHREADS
:{
1876 error
= proc_pidlistthreads(p
, buffer
, buffersize
, retval
);
1880 case PROC_PIDREGIONINFO
:{
1881 error
= proc_pidregioninfo(p
, arg
, buffer
, buffersize
, retval
);
1886 case PROC_PIDREGIONPATHINFO
:{
1887 error
= proc_pidregionpathinfo(p
, arg
, buffer
, buffersize
, retval
);
1891 case PROC_PIDREGIONPATHINFO2
:{
1892 error
= proc_pidregionpathinfo2(p
, arg
, buffer
, buffersize
, retval
);
1896 case PROC_PIDREGIONPATHINFO3
:{
1897 error
= proc_pidregionpathinfo3(p
, arg
, buffer
, buffersize
, retval
);
1901 case PROC_PIDVNODEPATHINFO
:{
1902 error
= proc_pidvnodepathinfo(p
, arg
, buffer
, buffersize
, retval
);
1907 case PROC_PIDTHREADPATHINFO
:{
1908 struct proc_threadwithpathinfo pinfo
;
1910 error
= proc_pidthreadpathinfo(p
, arg
, &pinfo
);
1912 error
= copyout((caddr_t
)&pinfo
, buffer
, sizeof(struct proc_threadwithpathinfo
));
1914 *retval
= sizeof(struct proc_threadwithpathinfo
);
1919 case PROC_PIDPATHINFO
: {
1920 error
= proc_pidpathinfo(p
, arg
, buffer
, buffersize
, retval
);
1925 case PROC_PIDWORKQUEUEINFO
:{
1926 struct proc_workqueueinfo pwqinfo
;
1928 error
= proc_pidworkqueueinfo(p
, &pwqinfo
);
1930 error
= copyout(&pwqinfo
, buffer
, sizeof(struct proc_workqueueinfo
));
1932 *retval
= sizeof(struct proc_workqueueinfo
);
1937 case PROC_PIDLISTFILEPORTS
: {
1938 error
= proc_pidfileportlist(p
, buffer
, buffersize
, retval
);
1942 case PROC_PIDARCHINFO
: {
1943 struct proc_archinfo pai
;
1944 bzero(&pai
, sizeof(pai
));
1945 proc_archinfo(p
, &pai
);
1946 error
= copyout(&pai
, buffer
, sizeof(struct proc_archinfo
));
1948 *retval
= sizeof(struct proc_archinfo
);
1953 case PROC_PIDCOALITIONINFO
: {
1954 struct proc_pidcoalitioninfo pci
;
1955 proc_pidcoalitioninfo(p
, &pci
);
1956 error
= copyout(&pci
, buffer
, sizeof(struct proc_pidcoalitioninfo
));
1958 *retval
= sizeof(struct proc_pidcoalitioninfo
);
1963 case PROC_PIDNOTEEXIT
: {
1965 error
= proc_pidnoteexit(p
, arg
, &data
);
1967 error
= copyout(&data
, buffer
, sizeof(data
));
1969 *retval
= sizeof(data
);
1975 case PROC_PIDEXITREASONINFO
: {
1976 struct proc_exitreasoninfo eri
;
1978 error
= copyin(buffer
, &eri
, sizeof(eri
));
1983 error
= proc_pidexitreasoninfo(p
, &eri
, NULL
);
1985 error
= copyout(&eri
, buffer
, sizeof(eri
));
1987 *retval
= sizeof(eri
);
1993 case PROC_PIDEXITREASONBASICINFO
: {
1994 struct proc_exitreasonbasicinfo beri
;
1996 bzero(&beri
, sizeof(struct proc_exitreasonbasicinfo
));
1998 error
= proc_pidexitreasoninfo(p
, NULL
, &beri
);
2000 error
= copyout(&beri
, buffer
, sizeof(beri
));
2002 *retval
= sizeof(beri
);
2016 proc_drop_zombref(p
);
2022 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
)
2024 struct vnode_fdinfo vfi
;
2027 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2030 bzero(&vfi
, sizeof(struct vnode_fdinfo
));
2031 fill_fileinfo(fp
, proc
, fd
, &vfi
.pfi
);
2032 error
= fill_vnodeinfo(vp
, &vfi
.pvi
);
2035 error
= copyout((caddr_t
)&vfi
, buffer
, sizeof(struct vnode_fdinfo
));
2037 *retval
= sizeof(struct vnode_fdinfo
);
2043 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
)
2045 struct vnode_fdinfowithpath vfip
;
2046 int count
, error
= 0;
2048 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
2051 bzero(&vfip
, sizeof(struct vnode_fdinfowithpath
));
2052 fill_fileinfo(fp
, proc
, fd
, &vfip
.pfi
);
2053 error
= fill_vnodeinfo(vp
, &vfip
.pvip
.vip_vi
) ;
2056 vn_getpath(vp
, &vfip
.pvip
.vip_path
[0], &count
);
2057 vfip
.pvip
.vip_path
[MAXPATHLEN
-1] = 0;
2059 error
= copyout((caddr_t
)&vfip
, buffer
, sizeof(struct vnode_fdinfowithpath
));
2061 *retval
= sizeof(struct vnode_fdinfowithpath
);
2068 fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* fproc
)
2070 fproc
->fi_openflags
= fp
->f_fglob
->fg_flag
;
2071 fproc
->fi_status
= 0;
2072 fproc
->fi_offset
= fp
->f_fglob
->fg_offset
;
2073 fproc
->fi_type
= FILEGLOB_DTYPE(fp
->f_fglob
);
2074 if (fp
->f_fglob
->fg_count
> 1)
2075 fproc
->fi_status
|= PROC_FP_SHARED
;
2076 if (proc
!= PROC_NULL
) {
2077 if ((FDFLAGS_GET(proc
, fd
) & UF_EXCLOSE
) != 0)
2078 fproc
->fi_status
|= PROC_FP_CLEXEC
;
2079 if ((FDFLAGS_GET(proc
, fd
) & UF_FORKCLOSE
) != 0)
2080 fproc
->fi_status
|= PROC_FP_CLFORK
;
2082 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
2083 fproc
->fi_status
|= PROC_FP_GUARDED
;
2084 fproc
->fi_guardflags
= 0;
2085 if (fp_isguarded(fp
, GUARD_CLOSE
))
2086 fproc
->fi_guardflags
|= PROC_FI_GUARD_CLOSE
;
2087 if (fp_isguarded(fp
, GUARD_DUP
))
2088 fproc
->fi_guardflags
|= PROC_FI_GUARD_DUP
;
2089 if (fp_isguarded(fp
, GUARD_SOCKET_IPC
))
2090 fproc
->fi_guardflags
|= PROC_FI_GUARD_SOCKET_IPC
;
2091 if (fp_isguarded(fp
, GUARD_FILEPORT
))
2092 fproc
->fi_guardflags
|= PROC_FI_GUARD_FILEPORT
;
2099 fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
)
2101 vfs_context_t context
;
2105 bzero(&sb
, sizeof(struct stat64
));
2106 context
= vfs_context_create((vfs_context_t
)0);
2107 error
= vn_stat(vp
, &sb
, NULL
, 1, context
);
2108 (void)vfs_context_rele(context
);
2110 munge_vinfo_stat(&sb
, &vinfo
->vi_stat
);
2115 if (vp
->v_mount
!= dead_mountp
) {
2116 vinfo
->vi_fsid
= vp
->v_mount
->mnt_vfsstat
.f_fsid
;
2118 vinfo
->vi_fsid
.val
[0] = 0;
2119 vinfo
->vi_fsid
.val
[1] = 0;
2121 vinfo
->vi_type
= vp
->v_type
;
2127 pid_socketinfo(socket_t so
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2130 struct socket_fdinfo s
;
2133 bzero(&s
, sizeof(struct socket_fdinfo
));
2134 fill_fileinfo(fp
, proc
, fd
, &s
.pfi
);
2135 if ((error
= fill_socketinfo(so
, &s
.psi
)) == 0) {
2136 if ((error
= copyout(&s
, buffer
, sizeof(struct socket_fdinfo
))) == 0)
2137 *retval
= sizeof(struct socket_fdinfo
);
2141 #pragma unused(so, fp, proc, fd, buffer)
2148 pid_pseminfo(struct psemnode
*psem
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2150 struct psem_fdinfo pseminfo
;
2153 bzero(&pseminfo
, sizeof(struct psem_fdinfo
));
2154 fill_fileinfo(fp
, proc
, fd
, &pseminfo
.pfi
);
2156 if ((error
= fill_pseminfo(psem
, &pseminfo
.pseminfo
)) == 0) {
2157 if ((error
= copyout(&pseminfo
, buffer
, sizeof(struct psem_fdinfo
))) == 0)
2158 *retval
= sizeof(struct psem_fdinfo
);
2165 pid_pshminfo(struct pshmnode
*pshm
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2167 struct pshm_fdinfo pshminfo
;
2170 bzero(&pshminfo
, sizeof(struct pshm_fdinfo
));
2171 fill_fileinfo(fp
, proc
, fd
, &pshminfo
.pfi
);
2173 if ((error
= fill_pshminfo(pshm
, &pshminfo
.pshminfo
)) == 0) {
2174 if ((error
= copyout(&pshminfo
, buffer
, sizeof(struct pshm_fdinfo
))) == 0)
2175 *retval
= sizeof(struct pshm_fdinfo
);
2182 pid_pipeinfo(struct pipe
* p
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2184 struct pipe_fdinfo pipeinfo
;
2187 bzero(&pipeinfo
, sizeof(struct pipe_fdinfo
));
2188 fill_fileinfo(fp
, proc
, fd
, &pipeinfo
.pfi
);
2189 if ((error
= fill_pipeinfo(p
, &pipeinfo
.pipeinfo
)) == 0) {
2190 if ((error
= copyout(&pipeinfo
, buffer
, sizeof(struct pipe_fdinfo
))) == 0)
2191 *retval
= sizeof(struct pipe_fdinfo
);
2198 pid_kqueueinfo(struct kqueue
* kq
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2200 struct kqueue_fdinfo kqinfo
;
2203 bzero(&kqinfo
, sizeof(struct kqueue_fdinfo
));
2205 /* not all kq's are associated with a file (e.g. workqkq) */
2208 fill_fileinfo(fp
, proc
, fd
, &kqinfo
.pfi
);
2211 if ((error
= fill_kqueueinfo(kq
, &kqinfo
.kqueueinfo
)) == 0) {
2212 if ((error
= copyout(&kqinfo
, buffer
, sizeof(struct kqueue_fdinfo
))) == 0)
2213 *retval
= sizeof(struct kqueue_fdinfo
);
2220 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
)
2226 /************************** proc_pidfdinfo routine ***************************/
2228 proc_pidfdinfo(int pid
, int flavor
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2231 int error
= ENOTSUP
;
2232 struct fileproc
* fp
= NULL
;
2236 case PROC_PIDFDVNODEINFO
:
2237 size
= PROC_PIDFDVNODEINFO_SIZE
;
2239 case PROC_PIDFDVNODEPATHINFO
:
2240 size
= PROC_PIDFDVNODEPATHINFO_SIZE
;
2242 case PROC_PIDFDSOCKETINFO
:
2243 size
= PROC_PIDFDSOCKETINFO_SIZE
;
2245 case PROC_PIDFDPSEMINFO
:
2246 size
= PROC_PIDFDPSEMINFO_SIZE
;
2248 case PROC_PIDFDPSHMINFO
:
2249 size
= PROC_PIDFDPSHMINFO_SIZE
;
2251 case PROC_PIDFDPIPEINFO
:
2252 size
= PROC_PIDFDPIPEINFO_SIZE
;
2254 case PROC_PIDFDKQUEUEINFO
:
2255 size
= PROC_PIDFDKQUEUEINFO_SIZE
;
2257 case PROC_PIDFDKQUEUE_EXTINFO
:
2258 size
= PROC_PIDFDKQUEUE_EXTINFO_SIZE
;
2259 if (buffer
== (user_addr_t
)0)
2262 case PROC_PIDFDATALKINFO
:
2263 size
= PROC_PIDFDATALKINFO_SIZE
;
2271 if (buffersize
< size
)
2274 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2279 /* Do we have permission to look into this? */
2280 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFDINFO
, flavor
, CHECK_SAME_USER
)))
2284 case PROC_PIDFDVNODEINFO
: {
2288 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) !=0) {
2291 /* no need to be under the fdlock */
2292 error
= pid_vnodeinfo(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2296 case PROC_PIDFDVNODEPATHINFO
: {
2300 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) !=0) {
2304 /* no need to be under the fdlock */
2305 error
= pid_vnodeinfopath(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2309 case PROC_PIDFDSOCKETINFO
: {
2312 if ((error
= fp_getfsock(p
, fd
, &fp
, &so
)) !=0) {
2315 /* no need to be under the fdlock */
2316 error
= pid_socketinfo(so
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2320 case PROC_PIDFDPSEMINFO
: {
2321 struct psemnode
* psem
;
2323 if ((error
= fp_getfpsem(p
, fd
, &fp
, &psem
)) !=0) {
2326 /* no need to be under the fdlock */
2327 error
= pid_pseminfo(psem
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2331 case PROC_PIDFDPSHMINFO
: {
2332 struct pshmnode
* pshm
;
2334 if ((error
= fp_getfpshm(p
, fd
, &fp
, &pshm
)) !=0) {
2337 /* no need to be under the fdlock */
2338 error
= pid_pshminfo(pshm
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2342 case PROC_PIDFDPIPEINFO
: {
2343 struct pipe
* cpipe
;
2345 if ((error
= fp_getfpipe(p
, fd
, &fp
, &cpipe
)) !=0) {
2348 /* no need to be under the fdlock */
2349 error
= pid_pipeinfo(cpipe
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2353 case PROC_PIDFDKQUEUEINFO
: {
2357 if ((kq
= p
->p_wqkqueue
) == NULL
) {
2358 /* wqkqueue is initialized on-demand */
2362 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2366 /* no need to be under the fdlock */
2367 error
= pid_kqueueinfo(kq
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2371 case PROC_PIDFDKQUEUE_EXTINFO
: {
2375 if ((kq
= p
->p_wqkqueue
) == NULL
) {
2376 /* wqkqueue is initialized on-demand */
2380 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2383 error
= pid_kqueue_extinfo(p
, kq
, buffer
, buffersize
, retval
);
2394 fp_drop(p
, fd
, fp
, 0);
2403 proc_listfd_kqueue(proc_t p
, int32_t *fdlist
, int len
)
2406 struct fileproc
* fp
;
2410 numfds
= p
->p_fd
->fd_nfiles
;
2416 for (n
= 0; ((n
< numfds
) && (n
< p
->p_fd
->fd_nfiles
)); n
++) {
2417 if (((fp
= p
->p_fd
->fd_ofiles
[n
]) != 0)
2418 && ((p
->p_fd
->fd_ofileflags
[n
] & UF_RESERVED
) == 0)
2419 && (FILEGLOB_DTYPE(fp
->f_fglob
) == PROX_FDTYPE_KQUEUE
)) {
2420 fdlist
[count
++] = n
;
2428 proc_kqueue_udata_info(proc_t p
, int32_t fd
, uint64_t *buffer
, int bufsize
)
2431 struct fileproc
* fp
= NULL
;
2435 /* wqkqueue is initialized on-demand */
2436 if ((kq
= p
->p_wqkqueue
) == NULL
) {
2440 int error
= fp_getfkq(p
, fd
, &fp
, &kq
);
2446 retval
= pid_kqueue_udatainfo(p
, kq
, buffer
, bufsize
);
2448 fp_drop(p
, fd
, fp
, 0);
2455 proc_list_uptrs(proc_t p
, uint64_t *udata_buffer
, int size
)
2457 int32_t *fdlist
= NULL
;
2462 int knote_max
= 4096;
2464 int bufsize
= knote_max
* sizeof(uint64_t);
2466 fdlist
= (int32_t *)kalloc((OPEN_MAX
+ 1) * sizeof(int32_t));
2471 nfds
= proc_listfd_kqueue(p
, &fdlist
[1], OPEN_MAX
);
2472 if (nfds
< 0 || nfds
> OPEN_MAX
) {
2473 kfree(fdlist
, (OPEN_MAX
+ 1) * sizeof(int32_t));
2477 /* Add FD -1, the implicit workq kqueue */
2485 bufsize
= knote_max
* sizeof(uint64_t);
2486 buffer
= (uint64_t *)kalloc(bufsize
);
2489 for (i
= 0; i
< nfds
; i
++) {
2491 ret
= proc_kqueue_udata_info(p
, fdlist
[i
], buffer
, bufsize
);
2492 if (bufsize
!= 0 && ret
> knote_max
) {
2493 kfree(buffer
, bufsize
);
2494 knote_max
= ret
+ 32;
2495 bufsize
= knote_max
* sizeof(uint64_t);
2496 buffer
= kalloc(bufsize
);
2503 /* Copy the udata ptrs */
2504 if (size
>= (int)((count
+ ret
) * sizeof(uint64_t))) {
2505 memcpy(&udata_buffer
[count
], buffer
, ret
* sizeof(uint64_t));
2507 count
= count
+ ret
;
2510 kfree(fdlist
, (OPEN_MAX
+ 1) * sizeof(int32_t));
2512 kfree(buffer
, bufsize
);
2518 * Helper function for proc_pidfileportinfo
2521 struct fileport_info_args
{
2523 user_addr_t fia_buffer
;
2524 uint32_t fia_buffersize
;
2525 int32_t *fia_retval
;
2528 static kern_return_t
2529 proc_fileport_info(__unused mach_port_name_t name
,
2530 struct fileglob
*fg
, void *arg
)
2532 struct fileport_info_args
*fia
= arg
;
2533 struct fileproc __fileproc
, *fp
= &__fileproc
;
2536 bzero(fp
, sizeof (*fp
));
2539 switch (fia
->fia_flavor
) {
2540 case PROC_PIDFILEPORTVNODEPATHINFO
: {
2543 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
2547 vp
= (struct vnode
*)fg
->fg_data
;
2548 error
= pid_vnodeinfopath(vp
, vnode_vid(vp
), fp
, PROC_NULL
, 0,
2549 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2552 case PROC_PIDFILEPORTSOCKETINFO
: {
2555 if (FILEGLOB_DTYPE(fg
) != DTYPE_SOCKET
) {
2559 so
= (socket_t
)fg
->fg_data
;
2560 error
= pid_socketinfo(so
, fp
, PROC_NULL
, 0,
2561 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2564 case PROC_PIDFILEPORTPSHMINFO
: {
2565 struct pshmnode
*pshm
;
2567 if (FILEGLOB_DTYPE(fg
) != DTYPE_PSXSHM
) {
2568 error
= EBADF
; /* ick - mirror fp_getfpshm */
2571 pshm
= (struct pshmnode
*)fg
->fg_data
;
2572 error
= pid_pshminfo(pshm
, fp
, PROC_NULL
, 0,
2573 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2576 case PROC_PIDFILEPORTPIPEINFO
: {
2579 if (FILEGLOB_DTYPE(fg
) != DTYPE_PIPE
) {
2580 error
= EBADF
; /* ick - mirror fp_getfpipe */
2583 cpipe
= (struct pipe
*)fg
->fg_data
;
2584 error
= pid_pipeinfo(cpipe
, fp
, PROC_NULL
, 0,
2585 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2596 /************************* proc_pidfileportinfo routine *********************/
2598 proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
,
2599 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2602 int error
= ENOTSUP
;
2604 struct fileport_info_args fia
;
2606 /* fileport types are restricted by file_issendable() */
2609 case PROC_PIDFILEPORTVNODEPATHINFO
:
2610 size
= PROC_PIDFILEPORTVNODEPATHINFO_SIZE
;
2612 case PROC_PIDFILEPORTSOCKETINFO
:
2613 size
= PROC_PIDFILEPORTSOCKETINFO_SIZE
;
2615 case PROC_PIDFILEPORTPSHMINFO
:
2616 size
= PROC_PIDFILEPORTPSHMINFO_SIZE
;
2618 case PROC_PIDFILEPORTPIPEINFO
:
2619 size
= PROC_PIDFILEPORTPIPEINFO_SIZE
;
2625 if (buffersize
< size
)
2627 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2632 /* Do we have permission to look into this? */
2633 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFILEPORTINFO
, flavor
, CHECK_SAME_USER
)))
2636 fia
.fia_flavor
= flavor
;
2637 fia
.fia_buffer
= buffer
;
2638 fia
.fia_buffersize
= buffersize
;
2639 fia
.fia_retval
= retval
;
2641 if (fileport_invoke(p
->task
, name
,
2642 proc_fileport_info
, &fia
, &error
) != KERN_SUCCESS
)
2651 proc_security_policy(proc_t targetp
, __unused
int callnum
, __unused
int flavor
, boolean_t check_same_user
)
2656 if ((error
= mac_proc_check_proc_info(current_proc(), targetp
, callnum
, flavor
)))
2660 /* The 'listpids' call doesn't have a target proc */
2661 if (targetp
== PROC_NULL
) {
2662 assert(callnum
== PROC_INFO_CALL_LISTPIDS
&& check_same_user
== NO_CHECK_SAME_USER
);
2667 * Check for 'get information for processes owned by other users' privilege
2668 * root has this privilege by default
2670 if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO
, 0) == 0)
2671 check_same_user
= FALSE
;
2673 if (check_same_user
) {
2674 kauth_cred_t target_cred
;
2677 target_cred
= kauth_cred_proc_ref(targetp
);
2678 target_uid
= kauth_cred_getuid(target_cred
);
2679 kauth_cred_unref(&target_cred
);
2681 if (kauth_getuid() != target_uid
)
2689 proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2691 if (suser(kauth_cred_get(), (u_short
*)0) == 0) {
2692 return(log_dmesg(buffer
, buffersize
, retval
));
2697 /* ********* process control sets on self only */
2699 proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t * retval
)
2701 struct proc
* pself
= PROC_NULL
;
2703 uint32_t pcontrol
= (uint32_t)arg
;
2704 struct uthread
*ut
= NULL
;
2705 char name_buf
[MAXTHREADNAMESIZE
];
2707 pself
= current_proc();
2708 if (pid
!= pself
->p_pid
)
2711 /* Do we have permission to look into this? */
2712 if ((error
= proc_security_policy(pself
, PROC_INFO_CALL_SETCONTROL
, flavor
, NO_CHECK_SAME_USER
)))
2716 case PROC_SELFSET_PCONTROL
: {
2717 if (pcontrol
> P_PCMAX
)
2720 /* reset existing control setting while retaining action state */
2721 pself
->p_pcaction
&= PROC_ACTION_MASK
;
2722 /* set new control state */
2723 pself
->p_pcaction
|= pcontrol
;
2728 case PROC_SELFSET_THREADNAME
: {
2730 * This is a bit ugly, as it copies the name into the kernel, and then
2731 * invokes bsd_setthreadname again to copy it into the uthread name
2732 * buffer. Hopefully this isn't such a hot codepath that an additional
2733 * MAXTHREADNAMESIZE copy is a big issue.
2735 if (buffersize
> (MAXTHREADNAMESIZE
- 1)) {
2736 return ENAMETOOLONG
;
2739 ut
= current_uthread();
2741 bzero(name_buf
, MAXTHREADNAMESIZE
);
2742 error
= copyin(buffer
, name_buf
, buffersize
);
2745 bsd_setthreadname(ut
, name_buf
);
2750 case PROC_SELFSET_VMRSRCOWNER
: {
2751 /* need to to be superuser */
2752 if (suser(kauth_cred_get(), (u_short
*)0) != 0) {
2758 /* reset existing control setting while retaining action state */
2759 pself
->p_lflag
|= P_LVMRSRCOWNER
;
2764 case PROC_SELFSET_DELAYIDLESLEEP
: {
2765 /* mark or clear the process property to delay idle sleep disk IO */
2767 OSBitOrAtomic(P_DELAYIDLESLEEP
, &pself
->p_flag
);
2769 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP
), &pself
->p_flag
);
2781 #if CONFIG_MEMORYSTATUS
2784 proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t *retval
) {
2785 struct proc
*target_p
;
2787 uint32_t pcontrol
= (uint32_t)arg
;
2788 kauth_cred_t my_cred
, target_cred
;
2789 boolean_t self
= FALSE
;
2790 boolean_t child
= FALSE
;
2791 boolean_t zombref
= FALSE
;
2794 target_p
= proc_find(pid
);
2796 if (target_p
== PROC_NULL
) {
2797 if (flavor
== PROC_DIRTYCONTROL_GET
) {
2798 target_p
= proc_find_zombref(pid
);
2802 if (target_p
== PROC_NULL
)
2807 my_cred
= kauth_cred_get();
2808 target_cred
= kauth_cred_proc_ref(target_p
);
2810 /* Do we have permission to look into this? */
2811 if ((error
= proc_security_policy(target_p
, PROC_INFO_CALL_DIRTYCONTROL
, flavor
, NO_CHECK_SAME_USER
)))
2814 selfpid
= proc_selfpid();
2815 if (pid
== selfpid
) {
2817 } else if (target_p
->p_ppid
== selfpid
) {
2822 case PROC_DIRTYCONTROL_TRACK
: {
2823 /* Only allow the process itself, its parent, or root */
2824 if ((self
== FALSE
) && (child
== FALSE
) && kauth_cred_issuser(kauth_cred_get()) != TRUE
) {
2829 error
= memorystatus_dirty_track(target_p
, pcontrol
);
2833 case PROC_DIRTYCONTROL_SET
: {
2834 /* Check privileges; use cansignal() here since the process could be terminated */
2835 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
, 0)) {
2840 error
= memorystatus_dirty_set(target_p
, self
, pcontrol
);
2844 case PROC_DIRTYCONTROL_GET
: {
2845 /* No permissions check - dirty state is freely available */
2847 *retval
= memorystatus_dirty_get(target_p
);
2854 case PROC_DIRTYCONTROL_CLEAR
: {
2855 /* Check privileges; use cansignal() here since the process could be terminated */
2856 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
, 0)) {
2861 error
= memorystatus_dirty_clear(target_p
, pcontrol
);
2868 proc_drop_zombref(target_p
);
2870 proc_rele(target_p
);
2872 kauth_cred_unref(&target_cred
);
2879 proc_dirtycontrol(__unused
int pid
, __unused
int flavor
, __unused
uint64_t arg
, __unused
int32_t *retval
) {
2883 #endif /* CONFIG_MEMORYSTATUS */
2886 * proc_terminate() provides support for sudden termination.
2887 * SIGKILL is issued to tracked, clean processes; otherwise,
2892 proc_terminate(int pid
, int32_t *retval
)
2896 kauth_cred_t uc
= kauth_cred_get();
2900 /* XXX: Check if these are necessary */
2901 AUDIT_ARG(pid
, pid
);
2902 AUDIT_ARG(signum
, sig
);
2905 if (pid
<= 0 || retval
== NULL
) {
2909 if ((p
= proc_find(pid
)) == NULL
) {
2914 /* XXX: Check if these are necessary */
2915 AUDIT_ARG(process
, p
);
2918 /* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
2919 if (!cansignal(current_proc(), uc
, p
, SIGKILL
, 0)) {
2924 /* Not allowed to sudden terminate yourself */
2925 if (p
== current_proc()) {
2930 #if CONFIG_MEMORYSTATUS
2931 /* Determine requisite signal to issue */
2932 sig
= memorystatus_on_terminate(p
);
2937 proc_set_task_policy(p
->task
, TASK_POLICY_ATTRIBUTE
,
2938 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2950 * copy stat64 structure into vinfo_stat structure.
2953 munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
)
2955 bzero(vsbp
, sizeof(struct vinfo_stat
));
2957 vsbp
->vst_dev
= sbp
->st_dev
;
2958 vsbp
->vst_mode
= sbp
->st_mode
;
2959 vsbp
->vst_nlink
= sbp
->st_nlink
;
2960 vsbp
->vst_ino
= sbp
->st_ino
;
2961 vsbp
->vst_uid
= sbp
->st_uid
;
2962 vsbp
->vst_gid
= sbp
->st_gid
;
2963 vsbp
->vst_atime
= sbp
->st_atimespec
.tv_sec
;
2964 vsbp
->vst_atimensec
= sbp
->st_atimespec
.tv_nsec
;
2965 vsbp
->vst_mtime
= sbp
->st_mtimespec
.tv_sec
;
2966 vsbp
->vst_mtimensec
= sbp
->st_mtimespec
.tv_nsec
;
2967 vsbp
->vst_ctime
= sbp
->st_ctimespec
.tv_sec
;
2968 vsbp
->vst_ctimensec
= sbp
->st_ctimespec
.tv_nsec
;
2969 vsbp
->vst_birthtime
= sbp
->st_birthtimespec
.tv_sec
;
2970 vsbp
->vst_birthtimensec
= sbp
->st_birthtimespec
.tv_nsec
;
2971 vsbp
->vst_size
= sbp
->st_size
;
2972 vsbp
->vst_blocks
= sbp
->st_blocks
;
2973 vsbp
->vst_blksize
= sbp
->st_blksize
;
2974 vsbp
->vst_flags
= sbp
->st_flags
;
2975 vsbp
->vst_gen
= sbp
->st_gen
;
2976 vsbp
->vst_rdev
= sbp
->st_rdev
;
2977 vsbp
->vst_qspare
[0] = sbp
->st_qspare
[0];
2978 vsbp
->vst_qspare
[1] = sbp
->st_qspare
[1];
2982 proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, __unused
int32_t *retval
)
2988 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2989 if ((p
= proc_find_zombref(pid
)) == PROC_NULL
) {
2995 /* Do we have permission to look into this? */
2996 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDRUSAGE
, flavor
, CHECK_SAME_USER
)))
2999 error
= proc_get_rusage(p
, flavor
, buffer
, zombie
);
3003 proc_drop_zombref(p
);
3011 proc_archinfo(proc_t p
, struct proc_archinfo
*pai
)
3014 pai
->p_cputype
= p
->p_cputype
;
3015 pai
->p_cpusubtype
= p
->p_cpusubtype
;
3020 proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*ppci
)
3022 bzero(ppci
, sizeof(*ppci
));
3023 proc_coalitionids(p
, ppci
->coalition_id
);
3027 proc_pidexitreasoninfo(proc_t p
, struct proc_exitreasoninfo
*peri
, struct proc_exitreasonbasicinfo
*pberi
)
3029 uint32_t reason_data_size
= 0;
3031 pid_t selfpid
= proc_selfpid();
3036 * One (and only one) of peri and pberi must be non-NULL.
3038 assert((peri
!= NULL
) || (pberi
!= NULL
));
3039 assert((peri
== NULL
) || (pberi
== NULL
));
3042 * Allow access to the parent of the exiting
3043 * child or the parent debugger only.
3046 if (p
->p_ppid
== selfpid
)
3047 break; /* parent => ok */
3049 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3050 (p
->p_oppid
== selfpid
))
3051 break; /* parent-in-waiting => ok */
3057 if (p
->p_exit_reason
== OS_REASON_NULL
) {
3062 if (p
->p_exit_reason
->osr_kcd_buf
!= NULL
) {
3063 reason_data_size
= kcdata_memory_get_used_bytes(&p
->p_exit_reason
->osr_kcd_descriptor
);
3067 peri
->eri_namespace
= p
->p_exit_reason
->osr_namespace
;
3068 peri
->eri_code
= p
->p_exit_reason
->osr_code
;
3069 peri
->eri_flags
= p
->p_exit_reason
->osr_flags
;
3071 if ((peri
->eri_kcd_buf
== 0) || (peri
->eri_reason_buf_size
< reason_data_size
)) {
3076 peri
->eri_reason_buf_size
= reason_data_size
;
3077 if (reason_data_size
!= 0) {
3078 error
= copyout(p
->p_exit_reason
->osr_kcd_buf
, peri
->eri_kcd_buf
, reason_data_size
);
3081 pberi
->beri_namespace
= p
->p_exit_reason
->osr_namespace
;
3082 pberi
->beri_code
= p
->p_exit_reason
->osr_code
;
3083 pberi
->beri_flags
= p
->p_exit_reason
->osr_flags
;
3084 pberi
->beri_reason_buf_size
= reason_data_size
;
3093 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
3094 * It mimics the data that is typically captured by the
3095 * EVFILT_PROC, NOTE_EXIT event mechanism.
3096 * See filt_proc() in kern_event.c.
3099 proc_pidnoteexit(proc_t p
, uint64_t flags
, uint32_t *data
)
3101 uint32_t exit_data
= 0;
3102 uint32_t exit_flags
= (uint32_t)flags
;
3107 * Allow access to the parent of the exiting
3108 * child or the parent debugger only.
3111 pid_t selfpid
= proc_selfpid();
3113 if (p
->p_ppid
== selfpid
)
3114 break; /* parent => ok */
3116 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
3117 (p
->p_oppid
== selfpid
))
3118 break; /* parent-in-waiting => ok */
3124 if ((exit_flags
& NOTE_EXITSTATUS
) != 0) {
3125 /* The signal and exit status */
3126 exit_data
|= (p
->p_xstat
& NOTE_PDATAMASK
);
3129 if ((exit_flags
& NOTE_EXIT_DETAIL
) != 0) {
3130 /* The exit detail */
3131 if ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) != 0) {
3132 exit_data
|= NOTE_EXIT_DECRYPTFAIL
;
3135 if ((p
->p_lflag
& P_LTERM_JETSAM
) != 0) {
3136 exit_data
|= NOTE_EXIT_MEMORY
;
3138 switch (p
->p_lflag
& P_JETSAM_MASK
) {
3139 case P_JETSAM_VMPAGESHORTAGE
:
3140 exit_data
|= NOTE_EXIT_MEMORY_VMPAGESHORTAGE
;
3142 case P_JETSAM_VMTHRASHING
:
3143 exit_data
|= NOTE_EXIT_MEMORY_VMTHRASHING
;
3145 case P_JETSAM_FCTHRASHING
:
3146 exit_data
|= NOTE_EXIT_MEMORY_FCTHRASHING
;
3148 case P_JETSAM_VNODE
:
3149 exit_data
|= NOTE_EXIT_MEMORY_VNODE
;
3151 case P_JETSAM_HIWAT
:
3152 exit_data
|= NOTE_EXIT_MEMORY_HIWAT
;
3155 exit_data
|= NOTE_EXIT_MEMORY_PID
;
3157 case P_JETSAM_IDLEEXIT
:
3158 exit_data
|= NOTE_EXIT_MEMORY_IDLE
;
3163 if ((p
->p_csflags
& CS_KILLED
) != 0) {
3164 exit_data
|= NOTE_EXIT_CSERROR
;