2 * Copyright (c) 2005-2013 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/sysctl.h>
50 #include <sys/aio_kern.h>
51 #include <sys/kern_memorystatus.h>
53 #include <security/audit/audit.h>
55 #include <mach/machine.h>
56 #include <mach/mach_types.h>
57 #include <mach/vm_param.h>
58 #include <kern/task.h>
59 #include <kern/kalloc.h>
60 #include <kern/assert.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_map.h>
63 #include <mach/host_info.h>
64 #include <mach/task_info.h>
65 #include <mach/thread_info.h>
66 #include <mach/vm_region.h>
68 #include <sys/mount_internal.h>
69 #include <sys/proc_info.h>
70 #include <sys/bsdtask_info.h>
71 #include <sys/kdebug.h>
72 #include <sys/sysproto.h>
73 #include <sys/msgbuf.h>
76 #include <sys/guarded.h>
78 #include <machine/machine_routines.h>
80 #include <kern/ipc_misc.h>
82 #include <vm/vm_protos.h>
84 /* Needed by proc_pidnoteexit() */
85 #include <sys/event.h>
86 #include <sys/codesign.h>
88 /* Needed by proc_listcoalitions() */
89 #ifdef CONFIG_COALITIONS
90 #include <sys/coalition.h>
99 uint64_t get_dispatchqueue_offset_from_proc(void *);
100 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *);
101 int proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
104 * TODO: Replace the noinline attribute below. Currently, it serves
105 * to avoid stack bloat caused by inlining multiple functions that
106 * have large stack footprints; when the functions are independent
107 * of each other (will not both be called in any given call to the
108 * caller), this only serves to bloat the stack, as we allocate
109 * space for both functions, despite the fact that we only need a
110 * fraction of that space.
112 * Long term, these functions should not be allocating everything on
113 * the stack, and should move large allocations (the huge structs
114 * that proc info deals in) to the heap, or eliminate them if
117 * The functions that most desperately need to improve stack usage
118 * (starting with the worst offenders):
119 * proc_pidvnodepathinfo
121 * proc_pidregionpathinfo
127 * proc_pidoriginatorinfo
130 /* protos for proc_info calls */
131 int __attribute__ ((noinline
)) proc_listpids(uint32_t type
, uint32_t tyoneinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
132 int __attribute__ ((noinline
)) proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
133 int __attribute__ ((noinline
)) proc_pidfdinfo(int pid
, int flavor
,int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
134 int __attribute__ ((noinline
)) proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
135 int __attribute__ ((noinline
)) proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
136 int __attribute__ ((noinline
)) proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
137 int __attribute__ ((noinline
)) proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t * retval
);
138 int __attribute__ ((noinline
)) proc_terminate(int pid
, int32_t * retval
);
139 int __attribute__ ((noinline
)) proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, int32_t * retval
);
140 int __attribute__ ((noinline
)) proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
);
141 int __attribute__ ((noinline
)) proc_listcoalitions(int flavor
, int coaltype
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
142 int __attribute__ ((noinline
)) proc_can_use_foreground_hw(int pid
, user_addr_t reason
, uint32_t resonsize
, int32_t *retval
);
144 /* protos for procpidinfo calls */
145 int __attribute__ ((noinline
)) proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
146 int __attribute__ ((noinline
)) proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
*pbsd
, int zombie
);
147 int __attribute__ ((noinline
)) proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
*pbsd_shortp
, int zombie
);
148 int __attribute__ ((noinline
)) proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
*ptinfo
);
149 int __attribute__ ((noinline
)) proc_pidthreadinfo(proc_t p
, uint64_t arg
, int thuniqueid
, struct proc_threadinfo
*pthinfo
);
150 int __attribute__ ((noinline
)) proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
);
151 int __attribute__ ((noinline
)) proc_pidlistthreads(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
152 int __attribute__ ((noinline
)) proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
153 int __attribute__ ((noinline
)) proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
154 int __attribute__ ((noinline
)) proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
155 int __attribute__ ((noinline
)) proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
156 int __attribute__ ((noinline
)) proc_pidvnodepathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
157 int __attribute__ ((noinline
)) proc_pidpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
158 int __attribute__ ((noinline
)) proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
);
159 int __attribute__ ((noinline
)) proc_pidfileportlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
160 void __attribute__ ((noinline
)) proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
161 void __attribute__ ((noinline
)) proc_archinfo(proc_t p
, struct proc_archinfo
*pai
);
162 void __attribute__ ((noinline
)) proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*pci
);
163 int __attribute__ ((noinline
)) proc_pidnoteexit(proc_t p
, uint64_t arg
, uint32_t *data
);
164 int __attribute__ ((noinline
)) proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
);
167 /* protos for proc_pidfdinfo calls */
168 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
);
169 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
);
170 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
);
171 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
);
172 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
);
173 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
);
174 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
);
175 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
);
178 /* protos for misc */
180 int fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
);
181 void fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* finfo
);
182 int proc_security_policy(proc_t targetp
, int callnum
, int flavor
, boolean_t check_same_user
);
183 static void munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
);
184 static int proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
);
186 extern int cansignal(struct proc
*, kauth_cred_t
, struct proc
*, int, int);
187 extern int proc_get_rusage(proc_t proc
, int flavor
, user_addr_t buffer
, int is_zombie
);
189 #define CHECK_SAME_USER TRUE
190 #define NO_CHECK_SAME_USER FALSE
192 uint64_t get_dispatchqueue_offset_from_proc(void *p
)
195 proc_t pself
= (proc_t
)p
;
196 return (pself
->p_dispatchqueue_offset
);
202 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p
)
205 proc_t pself
= (proc_t
)p
;
206 return (pself
->p_dispatchqueue_serialno_offset
);
212 /***************************** proc_info ********************/
215 proc_info(__unused
struct proc
*p
, struct proc_info_args
* uap
, int32_t *retval
)
217 return(proc_info_internal(uap
->callnum
, uap
->pid
, uap
->flavor
, uap
->arg
, uap
->buffer
, uap
->buffersize
, retval
));
222 proc_info_internal(int callnum
, int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
226 case PROC_INFO_CALL_LISTPIDS
:
227 /* pid contains type and flavor contains typeinfo */
228 return(proc_listpids(pid
, flavor
, buffer
, buffersize
, retval
));
229 case PROC_INFO_CALL_PIDINFO
:
230 return(proc_pidinfo(pid
, flavor
, arg
, buffer
, buffersize
, retval
));
231 case PROC_INFO_CALL_PIDFDINFO
:
232 return(proc_pidfdinfo(pid
, flavor
, (int)arg
, buffer
, buffersize
, retval
));
233 case PROC_INFO_CALL_KERNMSGBUF
:
234 return(proc_kernmsgbuf(buffer
, buffersize
, retval
));
235 case PROC_INFO_CALL_SETCONTROL
:
236 return(proc_setcontrol(pid
, flavor
, arg
, buffer
, buffersize
, retval
));
237 case PROC_INFO_CALL_PIDFILEPORTINFO
:
238 return(proc_pidfileportinfo(pid
, flavor
, (mach_port_name_t
)arg
, buffer
, buffersize
, retval
));
239 case PROC_INFO_CALL_TERMINATE
:
240 return(proc_terminate(pid
, retval
));
241 case PROC_INFO_CALL_DIRTYCONTROL
:
242 return(proc_dirtycontrol(pid
, flavor
, arg
, retval
));
243 case PROC_INFO_CALL_PIDRUSAGE
:
244 return (proc_pid_rusage(pid
, flavor
, buffer
, retval
));
245 case PROC_INFO_CALL_PIDORIGINATORINFO
:
246 return (proc_pidoriginatorinfo(pid
, flavor
, buffer
, buffersize
, retval
));
247 case PROC_INFO_CALL_LISTCOALITIONS
:
248 return proc_listcoalitions(pid
/* flavor */, flavor
/* coaltype */, buffer
,
250 case PROC_INFO_CALL_CANUSEFGHW
:
251 return proc_can_use_foreground_hw(pid
, buffer
, buffersize
, retval
);
259 /******************* proc_listpids routine ****************/
261 proc_listpids(uint32_t type
, uint32_t typeinfo
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
263 int numprocs
, wantpids
;
270 struct proclist
*current_list
;
272 /* Do we have permission to look into this? */
273 if ((error
= proc_security_policy(PROC_NULL
, PROC_INFO_CALL_LISTPIDS
, type
, NO_CHECK_SAME_USER
)))
276 /* if the buffer is null, return num of procs */
277 if (buffer
== (user_addr_t
)0) {
278 *retval
= ((nprocs
+20) * sizeof(int));
282 if (buffersize
< sizeof(int)) {
285 wantpids
= buffersize
/sizeof(int);
286 numprocs
= nprocs
+20;
287 if (numprocs
> wantpids
)
290 kbuf
= (char *)kalloc((vm_size_t
)(numprocs
* sizeof(int)));
293 bzero(kbuf
, sizeof(int));
300 current_list
= &allproc
;
302 LIST_FOREACH(p
, current_list
, p_list
) {
306 if (p
->p_pgrpid
!= (pid_t
)typeinfo
)
310 if ((p
->p_ppid
!= (pid_t
)typeinfo
) && (((p
->p_lflag
& P_LTRACED
) == 0) || (p
->p_oppid
!= (pid_t
)typeinfo
)))
318 /* racy but list lock is held */
319 if ((p
->p_flag
& P_CONTROLT
) == 0 ||
320 (p
->p_pgrp
== NULL
) || (p
->p_pgrp
->pg_session
== NULL
) ||
321 (tp
= SESSION_TP(p
->p_pgrp
->pg_session
)) == TTY_NULL
||
322 tp
->t_dev
!= (dev_t
)typeinfo
)
326 if (p
->p_ucred
== NULL
)
329 kauth_cred_t my_cred
;
332 my_cred
= kauth_cred_proc_ref(p
);
333 uid
= kauth_cred_getuid(my_cred
);
334 kauth_cred_unref(&my_cred
);
335 if (uid
!= (uid_t
)typeinfo
)
340 if (p
->p_ucred
== NULL
)
343 kauth_cred_t my_cred
;
346 my_cred
= kauth_cred_proc_ref(p
);
347 uid
= kauth_cred_getruid(my_cred
);
348 kauth_cred_unref(&my_cred
);
349 if (uid
!= (uid_t
)typeinfo
)
366 if ((n
< numprocs
) && (current_list
== &allproc
)) {
367 current_list
= &zombproc
;
374 error
= copyout((caddr_t
)ptr
, buffer
, n
* sizeof(int));
376 *retval
= (n
* sizeof(int));
377 kfree((void *)kbuf
, (vm_size_t
)(numprocs
* sizeof(int)));
383 /********************************** proc_pidfdlist routines ********************************/
386 proc_pidfdlist(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
390 struct proc_fdinfo
* pfd
;
391 struct fileproc
* fp
;
396 numfds
= p
->p_fd
->fd_nfiles
;
398 if (buffer
== (user_addr_t
) 0) {
400 *retval
= (numfds
* sizeof(struct proc_fdinfo
));
404 /* buffersize is big enough atleast for one struct */
405 needfds
= buffersize
/sizeof(struct proc_fdinfo
);
407 if (numfds
> needfds
)
410 kbuf
= (char *)kalloc((vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
413 bzero(kbuf
, numfds
* sizeof(struct proc_fdinfo
));
417 pfd
= (struct proc_fdinfo
*)kbuf
;
419 for (n
= 0; ((n
< numfds
) && (n
< p
->p_fd
->fd_nfiles
)); n
++) {
420 if (((fp
= p
->p_fd
->fd_ofiles
[n
]) != 0)
421 && ((p
->p_fd
->fd_ofileflags
[n
] & UF_RESERVED
) == 0)) {
422 file_type_t fdtype
= FILEGLOB_DTYPE(fp
->f_fglob
);
424 pfd
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
425 fdtype
: PROX_FDTYPE_ATALK
;
432 error
= copyout(kbuf
, buffer
, count
* sizeof(struct proc_fdinfo
));
433 kfree((void *)kbuf
, (vm_size_t
)(numfds
* sizeof(struct proc_fdinfo
)));
435 *retval
= (count
* sizeof(struct proc_fdinfo
));
440 * Helper functions for proc_pidfileportlist.
443 proc_fileport_count(__unused mach_port_name_t name
,
444 __unused
struct fileglob
*fg
, void *arg
)
446 uint32_t *counter
= arg
;
452 struct fileport_fdtype_args
{
453 struct proc_fileportinfo
*ffa_pfi
;
454 struct proc_fileportinfo
*ffa_pfi_end
;
458 proc_fileport_fdtype(mach_port_name_t name
, struct fileglob
*fg
, void *arg
)
460 struct fileport_fdtype_args
*ffa
= arg
;
462 if (ffa
->ffa_pfi
!= ffa
->ffa_pfi_end
) {
463 file_type_t fdtype
= FILEGLOB_DTYPE(fg
);
465 ffa
->ffa_pfi
->proc_fdtype
= (fdtype
!= DTYPE_ATALK
) ?
466 fdtype
: PROX_FDTYPE_ATALK
;
467 ffa
->ffa_pfi
->proc_fileport
= name
;
469 return (0); /* keep walking */
471 return (-1); /* stop the walk! */
475 proc_pidfileportlist(proc_t p
,
476 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
480 struct proc_fileportinfo
*pfi
;
481 uint32_t needfileports
, numfileports
;
482 struct fileport_fdtype_args ffa
;
485 needfileports
= buffersize
/ sizeof (*pfi
);
486 if ((user_addr_t
)0 == buffer
|| needfileports
> (uint32_t)maxfiles
) {
488 * Either (i) the user is asking for a fileport count,
489 * or (ii) the number of fileports they're asking for is
490 * larger than the maximum number of open files (!); count
491 * them to bound subsequent heap allocations.
494 switch (fileport_walk(p
->task
,
495 proc_fileport_count
, &numfileports
)) {
498 case KERN_RESOURCE_SHORTAGE
:
500 case KERN_INVALID_TASK
:
506 if (numfileports
== 0) {
507 *retval
= 0; /* none at all, bail */
510 if ((user_addr_t
)0 == buffer
) {
511 numfileports
+= 20; /* accelerate convergence */
512 *retval
= numfileports
* sizeof (*pfi
);
515 if (needfileports
> numfileports
)
516 needfileports
= numfileports
;
519 assert(buffersize
>= PROC_PIDLISTFILEPORTS_SIZE
);
521 kbufsize
= (vm_size_t
)needfileports
* sizeof (*pfi
);
522 pfi
= kbuf
= kalloc(kbufsize
);
525 bzero(kbuf
, kbufsize
);
528 ffa
.ffa_pfi_end
= pfi
+ needfileports
;
530 switch (fileport_walk(p
->task
, proc_fileport_fdtype
, &ffa
)) {
534 if ((numfileports
= pfi
- (typeof(pfi
))kbuf
) == 0)
536 if (numfileports
> needfileports
)
537 panic("more fileports returned than requested");
538 error
= copyout(kbuf
, buffer
, numfileports
* sizeof (*pfi
));
540 case KERN_RESOURCE_SHORTAGE
:
543 case KERN_INVALID_TASK
:
550 kfree(kbuf
, kbufsize
);
552 *retval
= numfileports
* sizeof (*pfi
);
557 proc_pidbsdinfo(proc_t p
, struct proc_bsdinfo
* pbsd
, int zombie
)
559 register struct tty
*tp
;
560 struct session
*sessionp
= NULL
;
562 kauth_cred_t my_cred
;
565 sessionp
= proc_session(p
);
567 my_cred
= kauth_cred_proc_ref(p
);
568 bzero(pbsd
, sizeof(struct proc_bsdinfo
));
569 pbsd
->pbi_status
= p
->p_stat
;
570 pbsd
->pbi_xstatus
= p
->p_xstat
;
571 pbsd
->pbi_pid
= p
->p_pid
;
572 pbsd
->pbi_ppid
= p
->p_ppid
;
573 pbsd
->pbi_uid
= kauth_cred_getuid(my_cred
);
574 pbsd
->pbi_gid
= kauth_cred_getgid(my_cred
);
575 pbsd
->pbi_ruid
= kauth_cred_getruid(my_cred
);
576 pbsd
->pbi_rgid
= kauth_cred_getrgid(my_cred
);
577 pbsd
->pbi_svuid
= kauth_cred_getsvuid(my_cred
);
578 pbsd
->pbi_svgid
= kauth_cred_getsvgid(my_cred
);
579 kauth_cred_unref(&my_cred
);
581 pbsd
->pbi_nice
= p
->p_nice
;
582 pbsd
->pbi_start_tvsec
= p
->p_start
.tv_sec
;
583 pbsd
->pbi_start_tvusec
= p
->p_start
.tv_usec
;
584 bcopy(&p
->p_comm
, &pbsd
->pbi_comm
[0], MAXCOMLEN
);
585 pbsd
->pbi_comm
[MAXCOMLEN
- 1] = '\0';
586 bcopy(&p
->p_name
, &pbsd
->pbi_name
[0], 2*MAXCOMLEN
);
587 pbsd
->pbi_name
[(2*MAXCOMLEN
) - 1] = '\0';
590 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
)
591 pbsd
->pbi_flags
|= PROC_FLAG_SYSTEM
;
592 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
)
593 pbsd
->pbi_flags
|= PROC_FLAG_TRACED
;
594 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
)
595 pbsd
->pbi_flags
|= PROC_FLAG_INEXIT
;
596 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
)
597 pbsd
->pbi_flags
|= PROC_FLAG_PPWAIT
;
598 if ((p
->p_flag
& P_LP64
) == P_LP64
)
599 pbsd
->pbi_flags
|= PROC_FLAG_LP64
;
600 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
)
601 pbsd
->pbi_flags
|= PROC_FLAG_CONTROLT
;
602 if ((p
->p_flag
& P_THCWD
) == P_THCWD
)
603 pbsd
->pbi_flags
|= PROC_FLAG_THCWD
;
604 if ((p
->p_flag
& P_SUGID
) == P_SUGID
)
605 pbsd
->pbi_flags
|= PROC_FLAG_PSUGID
;
606 if ((p
->p_flag
& P_EXEC
) == P_EXEC
)
607 pbsd
->pbi_flags
|= PROC_FLAG_EXEC
;
609 if (sessionp
!= SESSION_NULL
) {
610 if (SESS_LEADER(p
, sessionp
))
611 pbsd
->pbi_flags
|= PROC_FLAG_SLEADER
;
612 if (sessionp
->s_ttyvp
)
613 pbsd
->pbi_flags
|= PROC_FLAG_CTTY
;
616 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
617 pbsd
->pbi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
619 switch(PROC_CONTROL_STATE(p
)) {
621 pbsd
->pbi_flags
|= PROC_FLAG_PC_THROTTLE
;
624 pbsd
->pbi_flags
|= PROC_FLAG_PC_SUSP
;
627 pbsd
->pbi_flags
|= PROC_FLAG_PC_KILL
;
631 switch(PROC_ACTION_STATE(p
)) {
633 pbsd
->pbi_flags
|= PROC_FLAG_PA_THROTTLE
;
636 pbsd
->pbi_flags
|= PROC_FLAG_PA_SUSP
;
640 /* if process is a zombie skip bg state */
641 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
))
642 proc_get_darwinbgstate(p
->task
, &pbsd
->pbi_flags
);
645 pbsd
->pbi_nfiles
= p
->p_fd
->fd_nfiles
;
647 pbsd
->e_tdev
= NODEV
;
648 if (pg
!= PGRP_NULL
) {
649 pbsd
->pbi_pgid
= p
->p_pgrpid
;
650 pbsd
->pbi_pjobc
= pg
->pg_jobc
;
651 if ((p
->p_flag
& P_CONTROLT
) && (sessionp
!= SESSION_NULL
) && (tp
= SESSION_TP(sessionp
))) {
652 pbsd
->e_tdev
= tp
->t_dev
;
653 pbsd
->e_tpgid
= sessionp
->s_ttypgrpid
;
656 if (sessionp
!= SESSION_NULL
)
657 session_rele(sessionp
);
666 proc_pidshortbsdinfo(proc_t p
, struct proc_bsdshortinfo
* pbsd_shortp
, int zombie
)
668 bzero(pbsd_shortp
, sizeof(struct proc_bsdshortinfo
));
669 pbsd_shortp
->pbsi_pid
= p
->p_pid
;
670 pbsd_shortp
->pbsi_ppid
= p
->p_ppid
;
671 pbsd_shortp
->pbsi_pgid
= p
->p_pgrpid
;
672 pbsd_shortp
->pbsi_status
= p
->p_stat
;
673 bcopy(&p
->p_comm
, &pbsd_shortp
->pbsi_comm
[0], MAXCOMLEN
);
674 pbsd_shortp
->pbsi_comm
[MAXCOMLEN
- 1] = '\0';
676 pbsd_shortp
->pbsi_flags
= 0;
677 if ((p
->p_flag
& P_SYSTEM
) == P_SYSTEM
)
678 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_SYSTEM
;
679 if ((p
->p_lflag
& P_LTRACED
) == P_LTRACED
)
680 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_TRACED
;
681 if ((p
->p_lflag
& P_LEXIT
) == P_LEXIT
)
682 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_INEXIT
;
683 if ((p
->p_lflag
& P_LPPWAIT
) == P_LPPWAIT
)
684 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PPWAIT
;
685 if ((p
->p_flag
& P_LP64
) == P_LP64
)
686 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_LP64
;
687 if ((p
->p_flag
& P_CONTROLT
) == P_CONTROLT
)
688 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_CONTROLT
;
689 if ((p
->p_flag
& P_THCWD
) == P_THCWD
)
690 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_THCWD
;
691 if ((p
->p_flag
& P_SUGID
) == P_SUGID
)
692 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PSUGID
;
693 if ((p
->p_flag
& P_EXEC
) == P_EXEC
)
694 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_EXEC
;
695 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
696 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_DELAYIDLESLEEP
;
698 switch(PROC_CONTROL_STATE(p
)) {
700 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_THROTTLE
;
703 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_SUSP
;
706 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PC_KILL
;
710 switch(PROC_ACTION_STATE(p
)) {
712 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_THROTTLE
;
715 pbsd_shortp
->pbsi_flags
|= PROC_FLAG_PA_SUSP
;
719 /* if process is a zombie skip bg state */
720 if ((zombie
== 0) && (p
->p_stat
!= SZOMB
) && (p
->task
!= TASK_NULL
))
721 proc_get_darwinbgstate(p
->task
, &pbsd_shortp
->pbsi_flags
);
723 pbsd_shortp
->pbsi_uid
= p
->p_uid
;
724 pbsd_shortp
->pbsi_gid
= p
->p_gid
;
725 pbsd_shortp
->pbsi_ruid
= p
->p_ruid
;
726 pbsd_shortp
->pbsi_rgid
= p
->p_rgid
;
727 pbsd_shortp
->pbsi_svuid
= p
->p_svuid
;
728 pbsd_shortp
->pbsi_svgid
= p
->p_svgid
;
734 proc_pidtaskinfo(proc_t p
, struct proc_taskinfo
* ptinfo
)
740 bzero(ptinfo
, sizeof(struct proc_taskinfo
));
741 fill_taskprocinfo(task
, (struct proc_taskinfo_internal
*)ptinfo
);
749 proc_pidthreadinfo(proc_t p
, uint64_t arg
, int thuniqueid
, struct proc_threadinfo
*pthinfo
)
752 uint64_t threadaddr
= (uint64_t)arg
;
754 bzero(pthinfo
, sizeof(struct proc_threadinfo
));
756 error
= fill_taskthreadinfo(p
->task
, threadaddr
, thuniqueid
, (struct proc_threadinfo_internal
*)pthinfo
, NULL
, NULL
);
765 bsd_getthreadname(void *uth
, char *buffer
)
767 struct uthread
*ut
= (struct uthread
*)uth
;
769 bcopy(ut
->pth_name
,buffer
,MAXTHREADNAMESIZE
);
773 bsd_threadcdir(void * uth
, void *vptr
, int *vidp
)
775 struct uthread
* ut
= (struct uthread
*)uth
;
777 vnode_t
*vpp
= (vnode_t
*)vptr
;
791 proc_pidthreadpathinfo(proc_t p
, uint64_t arg
, struct proc_threadwithpathinfo
*pinfo
)
796 uint64_t threadaddr
= (uint64_t)arg
;
799 bzero(pinfo
, sizeof(struct proc_threadwithpathinfo
));
801 error
= fill_taskthreadinfo(p
->task
, threadaddr
, 0, (struct proc_threadinfo_internal
*)&pinfo
->pt
, (void *)&vp
, &vid
);
805 if ((vp
!= NULLVP
) && ((vnode_getwithvid(vp
, vid
)) == 0)) {
806 error
= fill_vnodeinfo(vp
, &pinfo
->pvip
.vip_vi
) ;
809 vn_getpath(vp
, &pinfo
->pvip
.vip_path
[0], &count
);
810 pinfo
->pvip
.vip_path
[MAXPATHLEN
-1] = 0;
820 proc_pidlistthreads(proc_t p
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
829 count
= buffersize
/(sizeof(uint64_t));
830 numthreads
= get_numthreads(p
->task
);
834 if (numthreads
> count
)
837 kbuf
= (void *)kalloc(numthreads
* sizeof(uint64_t));
840 bzero(kbuf
, numthreads
* sizeof(uint64_t));
842 ret
= fill_taskthreadlist(p
->task
, kbuf
, numthreads
);
844 error
= copyout(kbuf
, buffer
, ret
);
845 kfree(kbuf
, numthreads
* sizeof(uint64_t));
854 proc_pidregioninfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
856 struct proc_regioninfo preginfo
;
859 bzero(&preginfo
, sizeof(struct proc_regioninfo
));
860 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
, (uintptr_t *)0, (uint32_t *)0);
863 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regioninfo
));
865 *retval
= sizeof(struct proc_regioninfo
);
871 proc_pidregionpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
873 struct proc_regionwithpathinfo preginfo
;
875 uintptr_t vnodeaddr
= 0;
880 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
882 ret
= fill_procregioninfo( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
886 vp
= (vnode_t
)vnodeaddr
;
887 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
888 /* FILL THE VNODEINFO */
889 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
891 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
892 /* Always make sure it is null terminated */
893 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
897 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
899 *retval
= sizeof(struct proc_regionwithpathinfo
);
904 proc_pidregionpathinfo2(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
906 struct proc_regionwithpathinfo preginfo
;
908 uintptr_t vnodeaddr
= 0;
913 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
915 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, arg
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
921 vp
= (vnode_t
)vnodeaddr
;
922 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
923 /* FILL THE VNODEINFO */
924 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
926 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
927 /* Always make sure it is null terminated */
928 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
934 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
936 *retval
= sizeof(struct proc_regionwithpathinfo
);
941 proc_pidregionpathinfo3(proc_t p
, uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
943 struct proc_regionwithpathinfo preginfo
;
951 /* Loop while looking for vnodes that match dev_t filter */
953 bzero(&preginfo
, sizeof(struct proc_regionwithpathinfo
));
957 ret
= fill_procregioninfo_onlymappedvnodes( p
->task
, addr
, (struct proc_regioninfo_internal
*)&preginfo
.prp_prinfo
, (uintptr_t *)&vnodeaddr
, (uint32_t *)&vnodeid
);
963 vp
= (vnode_t
)vnodeaddr
;
964 if ((vnode_getwithvid(vp
, vnodeid
)) == 0) {
965 /* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
966 struct vnode_attr va
;
968 memset(&va
, 0, sizeof(va
));
970 VATTR_WANTED(&va
, va_fsid
);
972 ret
= vnode_getattr(vp
, &va
, vfs_context_current());
978 if (va
.va_fsid
== arg
) {
979 /* FILL THE VNODEINFO */
980 error
= fill_vnodeinfo(vp
, &preginfo
.prp_vip
.vip_vi
);
982 vn_getpath(vp
, &preginfo
.prp_vip
.vip_path
[0], &count
);
983 /* Always make sure it is null terminated */
984 preginfo
.prp_vip
.vip_path
[MAXPATHLEN
-1] = 0;
993 addr
= preginfo
.prp_prinfo
.pri_address
+ preginfo
.prp_prinfo
.pri_size
;
996 error
= copyout(&preginfo
, buffer
, sizeof(struct proc_regionwithpathinfo
));
998 *retval
= sizeof(struct proc_regionwithpathinfo
);
1003 * Path is relative to current process directory; may different from current
1007 proc_pidvnodepathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t *retval
)
1009 struct proc_vnodepathinfo pvninfo
;
1011 vnode_t vncdirvp
= NULLVP
;
1012 uint32_t vncdirid
=0;
1013 vnode_t vnrdirvp
= NULLVP
;
1014 uint32_t vnrdirid
=0;
1017 bzero(&pvninfo
, sizeof(struct proc_vnodepathinfo
));
1020 if (p
->p_fd
->fd_cdir
) {
1021 vncdirvp
= p
->p_fd
->fd_cdir
;
1022 vncdirid
= p
->p_fd
->fd_cdir
->v_id
;
1024 if (p
->p_fd
->fd_rdir
) {
1025 vnrdirvp
= p
->p_fd
->fd_rdir
;
1026 vnrdirid
= p
->p_fd
->fd_rdir
->v_id
;
1030 if (vncdirvp
!= NULLVP
) {
1031 if ((error
= vnode_getwithvid(vncdirvp
, vncdirid
)) == 0) {
1032 /* FILL THE VNODEINFO */
1033 error
= fill_vnodeinfo(vncdirvp
, &pvninfo
.pvi_cdir
.vip_vi
);
1036 vn_getpath(vncdirvp
, &pvninfo
.pvi_cdir
.vip_path
[0], &count
);
1037 pvninfo
.pvi_cdir
.vip_path
[MAXPATHLEN
-1] = 0;
1039 vnode_put(vncdirvp
);
1045 if ((error
== 0) && (vnrdirvp
!= NULLVP
)) {
1046 if ((error
= vnode_getwithvid(vnrdirvp
, vnrdirid
)) == 0) {
1047 /* FILL THE VNODEINFO */
1048 error
= fill_vnodeinfo(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_vi
);
1051 vn_getpath(vnrdirvp
, &pvninfo
.pvi_rdir
.vip_path
[0], &count
);
1052 pvninfo
.pvi_rdir
.vip_path
[MAXPATHLEN
-1] = 0;
1054 vnode_put(vnrdirvp
);
1060 error
= copyout(&pvninfo
, buffer
, sizeof(struct proc_vnodepathinfo
));
1062 *retval
= sizeof(struct proc_vnodepathinfo
);
1069 proc_pidpathinfo(proc_t p
, __unused
uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t *retval
)
1073 vnode_t nvp
= NULLVP
;
1074 int len
= buffersize
;
1082 buf
= (char *)kalloc(buffersize
);
1087 vid
= vnode_vid(tvp
);
1088 error
= vnode_getwithvid(tvp
, vid
);
1090 error
= vn_getpath_fsenter(tvp
, buf
, &len
);
1093 error
= vnode_lookup(buf
, 0, &nvp
, vfs_context_current());
1094 if ((error
== 0) && ( nvp
!= NULLVP
))
1097 error
= copyout(buf
, buffer
, len
);
1101 kfree(buf
, buffersize
);
1107 proc_pidworkqueueinfo(proc_t p
, struct proc_workqueueinfo
*pwqinfo
)
1111 bzero(pwqinfo
, sizeof(struct proc_workqueueinfo
));
1113 error
= fill_procworkqueue(p
, pwqinfo
);
1123 proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
)
1125 p_uniqidinfo
->p_uniqueid
= proc_uniqueid(p
);
1126 proc_getexecutableuuid(p
, (unsigned char *)&p_uniqidinfo
->p_uuid
, sizeof(p_uniqidinfo
->p_uuid
));
1127 p_uniqidinfo
->p_puniqueid
= proc_puniqueid(p
);
1128 p_uniqidinfo
->p_reserve2
= 0;
1129 p_uniqidinfo
->p_reserve3
= 0;
1130 p_uniqidinfo
->p_reserve4
= 0;
1135 proc_piduuidinfo(pid_t pid
, uuid_t uuid_buf
, uint32_t buffersize
)
1137 struct proc
* p
= PROC_NULL
;
1140 if (buffersize
< sizeof(uuid_t
))
1143 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1144 p
= proc_find_zombref(pid
);
1147 if (p
== PROC_NULL
) {
1151 proc_getexecutableuuid(p
, (unsigned char *)uuid_buf
, buffersize
);
1154 proc_drop_zombref(p
);
1162 * Function to get the uuid and pid of the originator of the voucher.
1165 proc_pidoriginatorpid_uuid(uuid_t uuid
, uint32_t buffersize
, pid_t
*pid
)
1167 pid_t originator_pid
;
1172 * Get the current voucher origin pid. The pid returned here
1173 * might not be valid or may have been recycled.
1175 kr
= thread_get_current_voucher_origin_pid(&originator_pid
);
1176 /* If errors, convert errors to appropriate format */
1178 if (kr
== KERN_INVALID_TASK
)
1180 else if (kr
== KERN_INVALID_VALUE
)
1187 *pid
= originator_pid
;
1188 error
= proc_piduuidinfo(originator_pid
, uuid
, buffersize
);
1193 * Function to get the uuid of the originator of the voucher.
1196 proc_pidoriginatoruuid(uuid_t uuid
, uint32_t buffersize
)
1198 pid_t originator_pid
;
1199 return (proc_pidoriginatorpid_uuid(uuid
, buffersize
, &originator_pid
));
1202 /***************************** proc_pidoriginatorinfo ***************************/
1205 proc_pidoriginatorinfo(int pid
, int flavor
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1207 int error
= ENOTSUP
;
1211 case PROC_PIDORIGINATOR_UUID
:
1212 size
= PROC_PIDORIGINATOR_UUID_SIZE
;
1214 case PROC_PIDORIGINATOR_BGSTATE
:
1215 size
= PROC_PIDORIGINATOR_BGSTATE_SIZE
;
1217 case PROC_PIDORIGINATOR_PID_UUID
:
1218 size
= PROC_PIDORIGINATOR_PID_UUID_SIZE
;
1224 if (buffersize
< size
)
1227 if (pid
!= 0 && pid
!= proc_selfpid())
1231 case PROC_PIDORIGINATOR_UUID
: {
1234 error
= proc_pidoriginatoruuid(uuid
, sizeof(uuid
));
1238 error
= copyout(uuid
, buffer
, size
);
1244 case PROC_PIDORIGINATOR_PID_UUID
: {
1245 struct proc_originatorinfo originator_info
;
1247 error
= proc_pidoriginatorpid_uuid(originator_info
.originator_uuid
,
1248 sizeof(uuid_t
), &originator_info
.originator_pid
);
1252 originator_info
.p_reserve2
= 0;
1253 originator_info
.p_reserve3
= 0;
1254 originator_info
.p_reserve4
= 0;
1256 error
= copyout(&originator_info
, buffer
, size
);
1262 case PROC_PIDORIGINATOR_BGSTATE
: {
1263 uint32_t is_backgrounded
;
1264 error
= proc_get_originatorbgstate(&is_backgrounded
);
1268 error
= copyout(&is_backgrounded
, buffer
, size
);
1281 /***************************** proc_listcoalitions ***************************/
1282 int proc_listcoalitions(int flavor
, int type
, user_addr_t buffer
,
1283 uint32_t buffersize
, int32_t *retval
)
1285 #if CONFIG_COALITIONS
1286 int error
= ENOTSUP
;
1289 void *coalinfo
= NULL
;
1290 uint32_t k_buffersize
= 0, copyout_sz
= 0;
1291 int ncoals
= 0, ncoals_
= 0;
1293 /* struct procinfo_coalinfo; */
1296 case LISTCOALITIONS_ALL_COALS
:
1297 elem_size
= LISTCOALITIONS_ALL_COALS_SIZE
;
1300 case LISTCOALITIONS_SINGLE_TYPE
:
1301 elem_size
= LISTCOALITIONS_SINGLE_TYPE_SIZE
;
1308 /* find the total number of coalitions */
1309 ncoals
= coalitions_get_list(coal_type
, NULL
, 0);
1311 if (ncoals
== 0 || buffer
== 0 || buffersize
== 0) {
1313 * user just wants buffer size
1314 * or there are no coalitions
1317 *retval
= (int)(ncoals
* elem_size
);
1321 k_buffersize
= ncoals
* elem_size
;
1322 coalinfo
= kalloc((vm_size_t
)k_buffersize
);
1327 bzero(coalinfo
, k_buffersize
);
1330 case LISTCOALITIONS_ALL_COALS
:
1331 case LISTCOALITIONS_SINGLE_TYPE
:
1332 ncoals_
= coalitions_get_list(coal_type
, coalinfo
, ncoals
);
1335 panic("memory corruption?!");
1339 /* all the coalitions disappeared... weird but valid */
1346 * Some coalitions may have disappeared between our initial check,
1347 * and the the actual list acquisition.
1348 * Only copy out what we really need.
1350 copyout_sz
= k_buffersize
;
1351 if (ncoals_
< ncoals
)
1352 copyout_sz
= ncoals_
* elem_size
;
1355 * copy the list up to user space
1356 * (we're guaranteed to have a non-null pointer/size here)
1358 error
= copyout(coalinfo
, buffer
,
1359 copyout_sz
< buffersize
? copyout_sz
: buffersize
);
1362 *retval
= (int)copyout_sz
;
1366 kfree(coalinfo
, k_buffersize
);
1370 /* no coalition support */
1381 /*************************** proc_can_use_forgeound_hw **************************/
1382 int proc_can_use_foreground_hw(int pid
, user_addr_t u_reason
, uint32_t reasonsize
, int32_t *retval
)
1384 proc_t p
= PROC_NULL
;
1386 uint32_t reason
= PROC_FGHW_ERROR
;
1388 task_t task
= TASK_NULL
;
1389 #if CONFIG_COALITIONS
1390 coalition_t coal
= COALITION_NULL
;
1397 reason
= PROC_FGHW_ERROR
;
1402 if (p
== PROC_NULL
) {
1404 reason
= PROC_FGHW_ERROR
;
1408 #if CONFIG_COALITIONS
1409 if (p
!= current_proc() &&
1410 !kauth_cred_issuser(kauth_cred_get())) {
1412 reason
= PROC_FGHW_ERROR
;
1417 task_reference(task
);
1418 if (coalition_is_leader(task
, COALITION_TYPE_JETSAM
, &coal
) == FALSE
) {
1419 /* current task is not a coalition leader: find the leader */
1420 task_deallocate(task
);
1421 task
= coalition_get_leader(coal
);
1424 if (task
!= TASK_NULL
) {
1426 * If task is non-null, then it is the coalition leader of the
1427 * current process' coalition. This could be the same task as
1428 * the current_task, and that's OK.
1433 proc_get_darwinbgstate(task
, &flags
);
1434 if ((flags
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
1436 * Coalition leader is not an application, continue
1437 * searching for other ways this task could gain
1440 reason
= PROC_FGHW_DAEMON_LEADER
;
1444 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
)) {
1446 * If the leader of the current process' coalition has
1447 * been marked as DARWIN_BG, then it definitely should
1448 * not be using foreground hardware resources.
1450 reason
= PROC_FGHW_LEADER_BACKGROUND
;
1454 role
= proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
);
1456 case TASK_FOREGROUND_APPLICATION
: /* DARWIN_ROLE_UI_FOCAL */
1457 case TASK_BACKGROUND_APPLICATION
: /* DARWIN_ROLE_UI */
1459 * The leader of this coalition is a focal, UI app:
1461 * TODO: should extensions/plugins be allowed to use
1465 reason
= PROC_FGHW_OK
;
1467 case TASK_DEFAULT_APPLICATION
: /* DARWIN_ROLE_UI_NON_FOCAL */
1468 case TASK_NONUI_APPLICATION
: /* DARWIN_ROLE_NON_UI */
1469 case TASK_THROTTLE_APPLICATION
:
1470 case TASK_UNSPECIFIED
:
1472 /* non-focal, non-ui apps don't get access */
1473 reason
= PROC_FGHW_LEADER_NONUI
;
1479 if (task
!= TASK_NULL
) {
1480 task_deallocate(task
);
1483 #endif /* CONFIG_COALITIONS */
1486 * There is no reasonable semantic to investigate the currently
1487 * adopted voucher of an arbitrary thread in a non-current process.
1490 if (p
!= current_proc()) {
1496 * In the absence of coalitions, fall back to a voucher-based lookup
1497 * where a daemon can used foreground HW if it's operating on behalf
1498 * of a foreground application.
1499 * NOTE: this is equivalent to a call to
1500 * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
1503 error
= proc_get_originatorbgstate(&isBG
);
1508 reason
= PROC_FGHW_NO_ORIGINATOR
;
1512 reason
= PROC_FGHW_NO_VOUCHER_ATTR
;
1516 reason
= PROC_FGHW_DAEMON_NO_VOUCHER
;
1520 /* some other error occurred: report that to the caller */
1521 reason
= PROC_FGHW_VOUCHER_ERROR
;
1526 reason
= PROC_FGHW_ORIGINATOR_BACKGROUND
;
1530 * The process itself is either a foreground app, or has
1531 * adopted a voucher originating from an app that's still in
1534 reason
= PROC_FGHW_DAEMON_OK
;
1539 if (task
!= TASK_NULL
)
1540 task_deallocate(task
);
1543 if (reasonsize
>= sizeof(reason
) && u_reason
!= (user_addr_t
)0)
1544 (void)copyout(&reason
, u_reason
, sizeof(reason
));
1549 /********************************** proc_pidinfo ********************************/
1553 proc_pidinfo(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1555 struct proc
* p
= PROC_NULL
;
1556 int error
= ENOTSUP
;
1559 int shortversion
= 0;
1563 int uniqidversion
= 0;
1564 boolean_t check_same_user
;
1567 case PROC_PIDLISTFDS
:
1568 size
= PROC_PIDLISTFD_SIZE
;
1569 if (buffer
== (user_addr_t
)0)
1572 case PROC_PIDTBSDINFO
:
1573 size
= PROC_PIDTBSDINFO_SIZE
;
1575 case PROC_PIDTASKINFO
:
1576 size
= PROC_PIDTASKINFO_SIZE
;
1578 case PROC_PIDTASKALLINFO
:
1579 size
= PROC_PIDTASKALLINFO_SIZE
;
1581 case PROC_PIDTHREADINFO
:
1582 size
= PROC_PIDTHREADINFO_SIZE
;
1584 case PROC_PIDLISTTHREADS
:
1585 size
= PROC_PIDLISTTHREADS_SIZE
;
1587 case PROC_PIDREGIONINFO
:
1588 size
= PROC_PIDREGIONINFO_SIZE
;
1590 case PROC_PIDREGIONPATHINFO
:
1591 size
= PROC_PIDREGIONPATHINFO_SIZE
;
1593 case PROC_PIDVNODEPATHINFO
:
1594 size
= PROC_PIDVNODEPATHINFO_SIZE
;
1596 case PROC_PIDTHREADPATHINFO
:
1597 size
= PROC_PIDTHREADPATHINFO_SIZE
;
1599 case PROC_PIDPATHINFO
:
1602 case PROC_PIDWORKQUEUEINFO
:
1603 /* kernel does not have workq info */
1607 size
= PROC_PIDWORKQUEUEINFO_SIZE
;
1609 case PROC_PIDT_SHORTBSDINFO
:
1610 size
= PROC_PIDT_SHORTBSDINFO_SIZE
;
1612 case PROC_PIDLISTFILEPORTS
:
1613 size
= PROC_PIDLISTFILEPORTS_SIZE
;
1614 if (buffer
== (user_addr_t
)0)
1617 case PROC_PIDTHREADID64INFO
:
1618 size
= PROC_PIDTHREADID64INFO_SIZE
;
1620 case PROC_PIDUNIQIDENTIFIERINFO
:
1621 size
= PROC_PIDUNIQIDENTIFIERINFO_SIZE
;
1623 case PROC_PIDT_BSDINFOWITHUNIQID
:
1624 size
= PROC_PIDT_BSDINFOWITHUNIQID_SIZE
;
1626 case PROC_PIDARCHINFO
:
1627 size
= PROC_PIDARCHINFO_SIZE
;
1629 case PROC_PIDCOALITIONINFO
:
1630 size
= PROC_PIDCOALITIONINFO_SIZE
;
1632 case PROC_PIDNOTEEXIT
:
1634 * Set findzomb explicitly because arg passed
1635 * in is used as note exit status bits.
1637 size
= PROC_PIDNOTEEXIT_SIZE
;
1640 case PROC_PIDREGIONPATHINFO2
:
1641 size
= PROC_PIDREGIONPATHINFO2_SIZE
;
1643 case PROC_PIDREGIONPATHINFO3
:
1644 size
= PROC_PIDREGIONPATHINFO3_SIZE
;
1650 if (buffersize
< size
)
1653 if ((flavor
== PROC_PIDPATHINFO
) && (buffersize
> PROC_PIDPATHINFO_MAXSIZE
)) {
1657 /* Check if we need to look for zombies */
1658 if ((flavor
== PROC_PIDTBSDINFO
) || (flavor
== PROC_PIDT_SHORTBSDINFO
) || (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1659 || (flavor
== PROC_PIDUNIQIDENTIFIERINFO
)) {
1664 if ((p
= proc_find(pid
)) == PROC_NULL
) {
1666 p
= proc_find_zombref(pid
);
1667 if (p
== PROC_NULL
) {
1676 /* Certain operations don't require privileges */
1678 case PROC_PIDT_SHORTBSDINFO
:
1679 case PROC_PIDUNIQIDENTIFIERINFO
:
1680 case PROC_PIDPATHINFO
:
1681 case PROC_PIDCOALITIONINFO
:
1682 check_same_user
= NO_CHECK_SAME_USER
;
1685 check_same_user
= CHECK_SAME_USER
;
1689 /* Do we have permission to look into this? */
1690 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDINFO
, flavor
, check_same_user
)))
1694 case PROC_PIDLISTFDS
: {
1695 error
= proc_pidfdlist(p
, buffer
, buffersize
, retval
);
1699 case PROC_PIDUNIQIDENTIFIERINFO
: {
1700 struct proc_uniqidentifierinfo p_uniqidinfo
;
1702 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
1703 error
= copyout(&p_uniqidinfo
, buffer
, sizeof(struct proc_uniqidentifierinfo
));
1705 *retval
= sizeof(struct proc_uniqidentifierinfo
);
1709 case PROC_PIDT_SHORTBSDINFO
:
1711 case PROC_PIDT_BSDINFOWITHUNIQID
:
1712 case PROC_PIDTBSDINFO
: {
1713 struct proc_bsdinfo pbsd
;
1714 struct proc_bsdshortinfo pbsd_short
;
1715 struct proc_bsdinfowithuniqid pbsd_uniqid
;
1717 if (flavor
== PROC_PIDT_BSDINFOWITHUNIQID
)
1720 if (shortversion
!= 0) {
1721 error
= proc_pidshortbsdinfo(p
, &pbsd_short
, zombie
);
1723 error
= proc_pidbsdinfo(p
, &pbsd
, zombie
);
1724 if (uniqidversion
!= 0) {
1725 proc_piduniqidentifierinfo(p
, &pbsd_uniqid
.p_uniqidentifier
);
1726 pbsd_uniqid
.pbsd
= pbsd
;
1731 if (shortversion
!= 0) {
1732 error
= copyout(&pbsd_short
, buffer
, sizeof(struct proc_bsdshortinfo
));
1734 *retval
= sizeof(struct proc_bsdshortinfo
);
1735 } else if (uniqidversion
!= 0) {
1736 error
= copyout(&pbsd_uniqid
, buffer
, sizeof(struct proc_bsdinfowithuniqid
));
1738 *retval
= sizeof(struct proc_bsdinfowithuniqid
);
1740 error
= copyout(&pbsd
, buffer
, sizeof(struct proc_bsdinfo
));
1742 *retval
= sizeof(struct proc_bsdinfo
);
1748 case PROC_PIDTASKINFO
: {
1749 struct proc_taskinfo ptinfo
;
1751 error
= proc_pidtaskinfo(p
, &ptinfo
);
1753 error
= copyout(&ptinfo
, buffer
, sizeof(struct proc_taskinfo
));
1755 *retval
= sizeof(struct proc_taskinfo
);
1760 case PROC_PIDTASKALLINFO
: {
1761 struct proc_taskallinfo pall
;
1763 error
= proc_pidbsdinfo(p
, &pall
.pbsd
, 0);
1764 error
= proc_pidtaskinfo(p
, &pall
.ptinfo
);
1766 error
= copyout(&pall
, buffer
, sizeof(struct proc_taskallinfo
));
1768 *retval
= sizeof(struct proc_taskallinfo
);
1773 case PROC_PIDTHREADID64INFO
:
1775 case PROC_PIDTHREADINFO
:{
1776 struct proc_threadinfo pthinfo
;
1778 error
= proc_pidthreadinfo(p
, arg
, thuniqueid
, &pthinfo
);
1780 error
= copyout(&pthinfo
, buffer
, sizeof(struct proc_threadinfo
));
1782 *retval
= sizeof(struct proc_threadinfo
);
1787 case PROC_PIDLISTTHREADS
:{
1788 error
= proc_pidlistthreads(p
, buffer
, buffersize
, retval
);
1792 case PROC_PIDREGIONINFO
:{
1793 error
= proc_pidregioninfo(p
, arg
, buffer
, buffersize
, retval
);
1798 case PROC_PIDREGIONPATHINFO
:{
1799 error
= proc_pidregionpathinfo(p
, arg
, buffer
, buffersize
, retval
);
1803 case PROC_PIDREGIONPATHINFO2
:{
1804 error
= proc_pidregionpathinfo2(p
, arg
, buffer
, buffersize
, retval
);
1808 case PROC_PIDREGIONPATHINFO3
:{
1809 error
= proc_pidregionpathinfo3(p
, arg
, buffer
, buffersize
, retval
);
1813 case PROC_PIDVNODEPATHINFO
:{
1814 error
= proc_pidvnodepathinfo(p
, arg
, buffer
, buffersize
, retval
);
1819 case PROC_PIDTHREADPATHINFO
:{
1820 struct proc_threadwithpathinfo pinfo
;
1822 error
= proc_pidthreadpathinfo(p
, arg
, &pinfo
);
1824 error
= copyout((caddr_t
)&pinfo
, buffer
, sizeof(struct proc_threadwithpathinfo
));
1826 *retval
= sizeof(struct proc_threadwithpathinfo
);
1831 case PROC_PIDPATHINFO
: {
1832 error
= proc_pidpathinfo(p
, arg
, buffer
, buffersize
, retval
);
1837 case PROC_PIDWORKQUEUEINFO
:{
1838 struct proc_workqueueinfo pwqinfo
;
1840 error
= proc_pidworkqueueinfo(p
, &pwqinfo
);
1842 error
= copyout(&pwqinfo
, buffer
, sizeof(struct proc_workqueueinfo
));
1844 *retval
= sizeof(struct proc_workqueueinfo
);
1849 case PROC_PIDLISTFILEPORTS
: {
1850 error
= proc_pidfileportlist(p
, buffer
, buffersize
,
1855 case PROC_PIDARCHINFO
: {
1856 struct proc_archinfo pai
;
1857 proc_archinfo(p
, &pai
);
1858 error
= copyout(&pai
, buffer
, sizeof(struct proc_archinfo
));
1860 *retval
= sizeof(struct proc_archinfo
);
1865 case PROC_PIDCOALITIONINFO
: {
1866 struct proc_pidcoalitioninfo pci
;
1867 proc_pidcoalitioninfo(p
, &pci
);
1868 error
= copyout(&pci
, buffer
, sizeof(struct proc_pidcoalitioninfo
));
1870 *retval
= sizeof(struct proc_pidcoalitioninfo
);
1875 case PROC_PIDNOTEEXIT
: {
1877 error
= proc_pidnoteexit(p
, arg
, &data
);
1879 error
= copyout(&data
, buffer
, sizeof(data
));
1881 *retval
= sizeof(data
);
1895 proc_drop_zombref(p
);
1901 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
)
1903 struct vnode_fdinfo vfi
;
1906 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
1909 bzero(&vfi
, sizeof(struct vnode_fdinfo
));
1910 fill_fileinfo(fp
, proc
, fd
, &vfi
.pfi
);
1911 error
= fill_vnodeinfo(vp
, &vfi
.pvi
);
1914 error
= copyout((caddr_t
)&vfi
, buffer
, sizeof(struct vnode_fdinfo
));
1916 *retval
= sizeof(struct vnode_fdinfo
);
1922 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
)
1924 struct vnode_fdinfowithpath vfip
;
1925 int count
, error
= 0;
1927 if ((error
= vnode_getwithvid(vp
, vid
)) != 0) {
1930 bzero(&vfip
, sizeof(struct vnode_fdinfowithpath
));
1931 fill_fileinfo(fp
, proc
, fd
, &vfip
.pfi
);
1932 error
= fill_vnodeinfo(vp
, &vfip
.pvip
.vip_vi
) ;
1935 vn_getpath(vp
, &vfip
.pvip
.vip_path
[0], &count
);
1936 vfip
.pvip
.vip_path
[MAXPATHLEN
-1] = 0;
1938 error
= copyout((caddr_t
)&vfip
, buffer
, sizeof(struct vnode_fdinfowithpath
));
1940 *retval
= sizeof(struct vnode_fdinfowithpath
);
1947 fill_fileinfo(struct fileproc
* fp
, proc_t proc
, int fd
, struct proc_fileinfo
* fproc
)
1949 fproc
->fi_openflags
= fp
->f_fglob
->fg_flag
;
1950 fproc
->fi_status
= 0;
1951 fproc
->fi_offset
= fp
->f_fglob
->fg_offset
;
1952 fproc
->fi_type
= FILEGLOB_DTYPE(fp
->f_fglob
);
1953 if (fp
->f_fglob
->fg_count
> 1)
1954 fproc
->fi_status
|= PROC_FP_SHARED
;
1955 if (proc
!= PROC_NULL
) {
1956 if ((FDFLAGS_GET(proc
, fd
) & UF_EXCLOSE
) != 0)
1957 fproc
->fi_status
|= PROC_FP_CLEXEC
;
1958 if ((FDFLAGS_GET(proc
, fd
) & UF_FORKCLOSE
) != 0)
1959 fproc
->fi_status
|= PROC_FP_CLFORK
;
1961 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
1962 fproc
->fi_status
|= PROC_FP_GUARDED
;
1963 fproc
->fi_guardflags
= 0;
1964 if (fp_isguarded(fp
, GUARD_CLOSE
))
1965 fproc
->fi_guardflags
|= PROC_FI_GUARD_CLOSE
;
1966 if (fp_isguarded(fp
, GUARD_DUP
))
1967 fproc
->fi_guardflags
|= PROC_FI_GUARD_DUP
;
1968 if (fp_isguarded(fp
, GUARD_SOCKET_IPC
))
1969 fproc
->fi_guardflags
|= PROC_FI_GUARD_SOCKET_IPC
;
1970 if (fp_isguarded(fp
, GUARD_FILEPORT
))
1971 fproc
->fi_guardflags
|= PROC_FI_GUARD_FILEPORT
;
1978 fill_vnodeinfo(vnode_t vp
, struct vnode_info
*vinfo
)
1980 vfs_context_t context
;
1984 bzero(&sb
, sizeof(struct stat64
));
1985 context
= vfs_context_create((vfs_context_t
)0);
1986 error
= vn_stat(vp
, &sb
, NULL
, 1, context
);
1987 (void)vfs_context_rele(context
);
1989 munge_vinfo_stat(&sb
, &vinfo
->vi_stat
);
1994 if (vp
->v_mount
!= dead_mountp
) {
1995 vinfo
->vi_fsid
= vp
->v_mount
->mnt_vfsstat
.f_fsid
;
1997 vinfo
->vi_fsid
.val
[0] = 0;
1998 vinfo
->vi_fsid
.val
[1] = 0;
2000 vinfo
->vi_type
= vp
->v_type
;
2006 pid_socketinfo(socket_t so
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2009 struct socket_fdinfo s
;
2012 bzero(&s
, sizeof(struct socket_fdinfo
));
2013 fill_fileinfo(fp
, proc
, fd
, &s
.pfi
);
2014 if ((error
= fill_socketinfo(so
, &s
.psi
)) == 0) {
2015 if ((error
= copyout(&s
, buffer
, sizeof(struct socket_fdinfo
))) == 0)
2016 *retval
= sizeof(struct socket_fdinfo
);
2020 #pragma unused(so, fp, proc, fd, buffer)
2027 pid_pseminfo(struct psemnode
*psem
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2029 struct psem_fdinfo pseminfo
;
2032 bzero(&pseminfo
, sizeof(struct psem_fdinfo
));
2033 fill_fileinfo(fp
, proc
, fd
, &pseminfo
.pfi
);
2035 if ((error
= fill_pseminfo(psem
, &pseminfo
.pseminfo
)) == 0) {
2036 if ((error
= copyout(&pseminfo
, buffer
, sizeof(struct psem_fdinfo
))) == 0)
2037 *retval
= sizeof(struct psem_fdinfo
);
2044 pid_pshminfo(struct pshmnode
*pshm
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2046 struct pshm_fdinfo pshminfo
;
2049 bzero(&pshminfo
, sizeof(struct pshm_fdinfo
));
2050 fill_fileinfo(fp
, proc
, fd
, &pshminfo
.pfi
);
2052 if ((error
= fill_pshminfo(pshm
, &pshminfo
.pshminfo
)) == 0) {
2053 if ((error
= copyout(&pshminfo
, buffer
, sizeof(struct pshm_fdinfo
))) == 0)
2054 *retval
= sizeof(struct pshm_fdinfo
);
2061 pid_pipeinfo(struct pipe
* p
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2063 struct pipe_fdinfo pipeinfo
;
2066 bzero(&pipeinfo
, sizeof(struct pipe_fdinfo
));
2067 fill_fileinfo(fp
, proc
, fd
, &pipeinfo
.pfi
);
2068 if ((error
= fill_pipeinfo(p
, &pipeinfo
.pipeinfo
)) == 0) {
2069 if ((error
= copyout(&pipeinfo
, buffer
, sizeof(struct pipe_fdinfo
))) == 0)
2070 *retval
= sizeof(struct pipe_fdinfo
);
2077 pid_kqueueinfo(struct kqueue
* kq
, struct fileproc
*fp
, proc_t proc
, int fd
, user_addr_t buffer
, __unused
uint32_t buffersize
, int32_t * retval
)
2079 struct kqueue_fdinfo kqinfo
;
2082 bzero(&kqinfo
, sizeof(struct kqueue_fdinfo
));
2084 /* not all kq's are associated with a file (e.g. workqkq) */
2087 fill_fileinfo(fp
, proc
, fd
, &kqinfo
.pfi
);
2090 if ((error
= fill_kqueueinfo(kq
, &kqinfo
.kqueueinfo
)) == 0) {
2091 if ((error
= copyout(&kqinfo
, buffer
, sizeof(struct kqueue_fdinfo
))) == 0)
2092 *retval
= sizeof(struct kqueue_fdinfo
);
2099 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
)
2106 /************************** proc_pidfdinfo routine ***************************/
2108 proc_pidfdinfo(int pid
, int flavor
, int fd
, user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2111 int error
= ENOTSUP
;
2112 struct fileproc
* fp
= NULL
;
2116 case PROC_PIDFDVNODEINFO
:
2117 size
= PROC_PIDFDVNODEINFO_SIZE
;
2119 case PROC_PIDFDVNODEPATHINFO
:
2120 size
= PROC_PIDFDVNODEPATHINFO_SIZE
;
2122 case PROC_PIDFDSOCKETINFO
:
2123 size
= PROC_PIDFDSOCKETINFO_SIZE
;
2125 case PROC_PIDFDPSEMINFO
:
2126 size
= PROC_PIDFDPSEMINFO_SIZE
;
2128 case PROC_PIDFDPSHMINFO
:
2129 size
= PROC_PIDFDPSHMINFO_SIZE
;
2131 case PROC_PIDFDPIPEINFO
:
2132 size
= PROC_PIDFDPIPEINFO_SIZE
;
2134 case PROC_PIDFDKQUEUEINFO
:
2135 size
= PROC_PIDFDKQUEUEINFO_SIZE
;
2137 case PROC_PIDFDKQUEUE_EXTINFO
:
2138 size
= PROC_PIDFDKQUEUE_EXTINFO_SIZE
;
2139 if (buffer
== (user_addr_t
)0)
2142 case PROC_PIDFDATALKINFO
:
2143 size
= PROC_PIDFDATALKINFO_SIZE
;
2151 if (buffersize
< size
)
2154 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2159 /* Do we have permission to look into this? */
2160 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFDINFO
, flavor
, CHECK_SAME_USER
)))
2164 case PROC_PIDFDVNODEINFO
: {
2168 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) !=0) {
2171 /* no need to be under the fdlock */
2172 error
= pid_vnodeinfo(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2176 case PROC_PIDFDVNODEPATHINFO
: {
2180 if ((error
= fp_getfvpandvid(p
, fd
, &fp
, &vp
, &vid
)) !=0) {
2184 /* no need to be under the fdlock */
2185 error
= pid_vnodeinfopath(vp
, vid
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2189 case PROC_PIDFDSOCKETINFO
: {
2192 if ((error
= fp_getfsock(p
, fd
, &fp
, &so
)) !=0) {
2195 /* no need to be under the fdlock */
2196 error
= pid_socketinfo(so
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2200 case PROC_PIDFDPSEMINFO
: {
2201 struct psemnode
* psem
;
2203 if ((error
= fp_getfpsem(p
, fd
, &fp
, &psem
)) !=0) {
2206 /* no need to be under the fdlock */
2207 error
= pid_pseminfo(psem
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2211 case PROC_PIDFDPSHMINFO
: {
2212 struct pshmnode
* pshm
;
2214 if ((error
= fp_getfpshm(p
, fd
, &fp
, &pshm
)) !=0) {
2217 /* no need to be under the fdlock */
2218 error
= pid_pshminfo(pshm
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2222 case PROC_PIDFDPIPEINFO
: {
2223 struct pipe
* cpipe
;
2225 if ((error
= fp_getfpipe(p
, fd
, &fp
, &cpipe
)) !=0) {
2228 /* no need to be under the fdlock */
2229 error
= pid_pipeinfo(cpipe
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2233 case PROC_PIDFDKQUEUEINFO
: {
2237 if ((kq
= p
->p_wqkqueue
) == NULL
) {
2238 /* wqkqueue is initialized on-demand */
2242 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2246 /* no need to be under the fdlock */
2247 error
= pid_kqueueinfo(kq
, fp
, p
, fd
, buffer
, buffersize
, retval
);
2251 case PROC_PIDFDKQUEUE_EXTINFO
: {
2255 if ((kq
= p
->p_wqkqueue
) == NULL
) {
2256 /* wqkqueue is initialized on-demand */
2260 } else if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) {
2263 error
= pid_kqueue_extinfo(p
, kq
, buffer
, buffersize
, retval
);
2274 fp_drop(p
, fd
, fp
, 0);
2283 * Helper function for proc_pidfileportinfo
2286 struct fileport_info_args
{
2288 user_addr_t fia_buffer
;
2289 uint32_t fia_buffersize
;
2290 int32_t *fia_retval
;
2293 static kern_return_t
2294 proc_fileport_info(__unused mach_port_name_t name
,
2295 struct fileglob
*fg
, void *arg
)
2297 struct fileport_info_args
*fia
= arg
;
2298 struct fileproc __fileproc
, *fp
= &__fileproc
;
2301 bzero(fp
, sizeof (*fp
));
2304 switch (fia
->fia_flavor
) {
2305 case PROC_PIDFILEPORTVNODEPATHINFO
: {
2308 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
2312 vp
= (struct vnode
*)fg
->fg_data
;
2313 error
= pid_vnodeinfopath(vp
, vnode_vid(vp
), fp
, PROC_NULL
, 0,
2314 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2317 case PROC_PIDFILEPORTSOCKETINFO
: {
2320 if (FILEGLOB_DTYPE(fg
) != DTYPE_SOCKET
) {
2324 so
= (socket_t
)fg
->fg_data
;
2325 error
= pid_socketinfo(so
, fp
, PROC_NULL
, 0,
2326 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2329 case PROC_PIDFILEPORTPSHMINFO
: {
2330 struct pshmnode
*pshm
;
2332 if (FILEGLOB_DTYPE(fg
) != DTYPE_PSXSHM
) {
2333 error
= EBADF
; /* ick - mirror fp_getfpshm */
2336 pshm
= (struct pshmnode
*)fg
->fg_data
;
2337 error
= pid_pshminfo(pshm
, fp
, PROC_NULL
, 0,
2338 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2341 case PROC_PIDFILEPORTPIPEINFO
: {
2344 if (FILEGLOB_DTYPE(fg
) != DTYPE_PIPE
) {
2345 error
= EBADF
; /* ick - mirror fp_getfpipe */
2348 cpipe
= (struct pipe
*)fg
->fg_data
;
2349 error
= pid_pipeinfo(cpipe
, fp
, PROC_NULL
, 0,
2350 fia
->fia_buffer
, fia
->fia_buffersize
, fia
->fia_retval
);
2361 /************************* proc_pidfileportinfo routine *********************/
2363 proc_pidfileportinfo(int pid
, int flavor
, mach_port_name_t name
,
2364 user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
)
2367 int error
= ENOTSUP
;
2369 struct fileport_info_args fia
;
2371 /* fileport types are restricted by file_issendable() */
2374 case PROC_PIDFILEPORTVNODEPATHINFO
:
2375 size
= PROC_PIDFILEPORTVNODEPATHINFO_SIZE
;
2377 case PROC_PIDFILEPORTSOCKETINFO
:
2378 size
= PROC_PIDFILEPORTSOCKETINFO_SIZE
;
2380 case PROC_PIDFILEPORTPSHMINFO
:
2381 size
= PROC_PIDFILEPORTPSHMINFO_SIZE
;
2383 case PROC_PIDFILEPORTPIPEINFO
:
2384 size
= PROC_PIDFILEPORTPIPEINFO_SIZE
;
2390 if (buffersize
< size
)
2392 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2397 /* Do we have permission to look into this? */
2398 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDFILEPORTINFO
, flavor
, CHECK_SAME_USER
)))
2401 fia
.fia_flavor
= flavor
;
2402 fia
.fia_buffer
= buffer
;
2403 fia
.fia_buffersize
= buffersize
;
2404 fia
.fia_retval
= retval
;
2406 if (fileport_invoke(p
->task
, name
,
2407 proc_fileport_info
, &fia
, &error
) != KERN_SUCCESS
)
2416 proc_security_policy(proc_t targetp
, __unused
int callnum
, __unused
int flavor
, boolean_t check_same_user
)
2421 if ((error
= mac_proc_check_proc_info(current_proc(), targetp
, callnum
, flavor
)))
2425 /* The 'listpids' call doesn't have a target proc */
2426 if (targetp
== PROC_NULL
) {
2427 assert(callnum
== PROC_INFO_CALL_LISTPIDS
&& check_same_user
== NO_CHECK_SAME_USER
);
2432 * Check for 'get information for processes owned by other users' privilege
2433 * root has this privilege by default
2435 if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO
, 0) == 0)
2436 check_same_user
= FALSE
;
2438 if (check_same_user
) {
2439 kauth_cred_t target_cred
;
2442 target_cred
= kauth_cred_proc_ref(targetp
);
2443 target_uid
= kauth_cred_getuid(target_cred
);
2444 kauth_cred_unref(&target_cred
);
2446 if (kauth_getuid() != target_uid
)
2454 proc_kernmsgbuf(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
2456 if (suser(kauth_cred_get(), (u_short
*)0) == 0) {
2457 return(log_dmesg(buffer
, buffersize
, retval
));
2462 /* ********* process control sets on self only */
2464 proc_setcontrol(int pid
, int flavor
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, __unused
int32_t * retval
)
2466 struct proc
* pself
= PROC_NULL
;
2468 uint32_t pcontrol
= (uint32_t)arg
;
2469 struct uthread
*ut
= NULL
;
2472 pself
= current_proc();
2473 if (pid
!= pself
->p_pid
)
2476 /* Do we have permission to look into this? */
2477 if ((error
= proc_security_policy(pself
, PROC_INFO_CALL_SETCONTROL
, flavor
, NO_CHECK_SAME_USER
)))
2481 case PROC_SELFSET_PCONTROL
: {
2482 if (pcontrol
> P_PCMAX
)
2485 /* reset existing control setting while retaining action state */
2486 pself
->p_pcaction
&= PROC_ACTION_MASK
;
2487 /* set new control state */
2488 pself
->p_pcaction
|= pcontrol
;
2493 case PROC_SELFSET_THREADNAME
: {
2494 /* PROC_SELFSET_THREADNAME_SIZE = (MAXTHREADNAMESIZE -1) */
2495 if(buffersize
> PROC_SELFSET_THREADNAME_SIZE
)
2496 return ENAMETOOLONG
;
2497 ut
= current_uthread();
2501 ut
->pth_name
= (char*)kalloc(MAXTHREADNAMESIZE
);
2505 bzero(ut
->pth_name
, MAXTHREADNAMESIZE
);
2506 error
= copyin(buffer
, ut
->pth_name
, buffersize
);
2510 case PROC_SELFSET_VMRSRCOWNER
: {
2511 /* need to to be superuser */
2512 if (suser(kauth_cred_get(), (u_short
*)0) != 0) {
2518 /* reset existing control setting while retaining action state */
2519 pself
->p_lflag
|= P_LVMRSRCOWNER
;
2524 case PROC_SELFSET_DELAYIDLESLEEP
: {
2525 /* mark or clear the process property to delay idle sleep disk IO */
2527 OSBitOrAtomic(P_DELAYIDLESLEEP
, &pself
->p_flag
);
2529 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP
), &pself
->p_flag
);
2541 #if CONFIG_MEMORYSTATUS
2544 proc_dirtycontrol(int pid
, int flavor
, uint64_t arg
, int32_t *retval
) {
2545 struct proc
*target_p
;
2547 uint32_t pcontrol
= (uint32_t)arg
;
2548 kauth_cred_t my_cred
, target_cred
;
2549 boolean_t self
= FALSE
;
2550 boolean_t child
= FALSE
;
2551 boolean_t zombref
= FALSE
;
2554 target_p
= proc_find(pid
);
2556 if (target_p
== PROC_NULL
) {
2557 if (flavor
== PROC_DIRTYCONTROL_GET
) {
2558 target_p
= proc_find_zombref(pid
);
2562 if (target_p
== PROC_NULL
)
2567 my_cred
= kauth_cred_get();
2568 target_cred
= kauth_cred_proc_ref(target_p
);
2570 /* Do we have permission to look into this? */
2571 if ((error
= proc_security_policy(target_p
, PROC_INFO_CALL_DIRTYCONTROL
, flavor
, NO_CHECK_SAME_USER
)))
2574 selfpid
= proc_selfpid();
2575 if (pid
== selfpid
) {
2577 } else if (target_p
->p_ppid
== selfpid
) {
2582 case PROC_DIRTYCONTROL_TRACK
: {
2583 /* Only allow the process itself, its parent, or root */
2584 if ((self
== FALSE
) && (child
== FALSE
) && kauth_cred_issuser(kauth_cred_get()) != TRUE
) {
2589 error
= memorystatus_dirty_track(target_p
, pcontrol
);
2593 case PROC_DIRTYCONTROL_SET
: {
2594 /* Check privileges; use cansignal() here since the process could be terminated */
2595 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
, 0)) {
2600 error
= memorystatus_dirty_set(target_p
, self
, pcontrol
);
2604 case PROC_DIRTYCONTROL_GET
: {
2605 /* No permissions check - dirty state is freely available */
2607 *retval
= memorystatus_dirty_get(target_p
);
2614 case PROC_DIRTYCONTROL_CLEAR
: {
2615 /* Check privileges; use cansignal() here since the process could be terminated */
2616 if (!cansignal(current_proc(), my_cred
, target_p
, SIGKILL
, 0)) {
2621 error
= memorystatus_dirty_clear(target_p
, pcontrol
);
2628 proc_drop_zombref(target_p
);
2630 proc_rele(target_p
);
2632 kauth_cred_unref(&target_cred
);
2639 proc_dirtycontrol(__unused
int pid
, __unused
int flavor
, __unused
uint64_t arg
, __unused
int32_t *retval
) {
2643 #endif /* CONFIG_MEMORYSTATUS */
2646 * proc_terminate() provides support for sudden termination.
2647 * SIGKILL is issued to tracked, clean processes; otherwise,
2652 proc_terminate(int pid
, int32_t *retval
)
2656 kauth_cred_t uc
= kauth_cred_get();
2660 /* XXX: Check if these are necessary */
2661 AUDIT_ARG(pid
, pid
);
2662 AUDIT_ARG(signum
, sig
);
2665 if (pid
<= 0 || retval
== NULL
) {
2669 if ((p
= proc_find(pid
)) == NULL
) {
2674 /* XXX: Check if these are necessary */
2675 AUDIT_ARG(process
, p
);
2678 /* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
2679 if (!cansignal(current_proc(), uc
, p
, SIGKILL
, 0)) {
2684 /* Not allowed to sudden terminate yourself */
2685 if (p
== current_proc()) {
2690 #if CONFIG_MEMORYSTATUS
2691 /* Determine requisite signal to issue */
2692 sig
= memorystatus_on_terminate(p
);
2697 proc_set_task_policy(p
->task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
,
2698 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2710 * copy stat64 structure into vinfo_stat structure.
2713 munge_vinfo_stat(struct stat64
*sbp
, struct vinfo_stat
*vsbp
)
2715 bzero(vsbp
, sizeof(struct vinfo_stat
));
2717 vsbp
->vst_dev
= sbp
->st_dev
;
2718 vsbp
->vst_mode
= sbp
->st_mode
;
2719 vsbp
->vst_nlink
= sbp
->st_nlink
;
2720 vsbp
->vst_ino
= sbp
->st_ino
;
2721 vsbp
->vst_uid
= sbp
->st_uid
;
2722 vsbp
->vst_gid
= sbp
->st_gid
;
2723 vsbp
->vst_atime
= sbp
->st_atimespec
.tv_sec
;
2724 vsbp
->vst_atimensec
= sbp
->st_atimespec
.tv_nsec
;
2725 vsbp
->vst_mtime
= sbp
->st_mtimespec
.tv_sec
;
2726 vsbp
->vst_mtimensec
= sbp
->st_mtimespec
.tv_nsec
;
2727 vsbp
->vst_ctime
= sbp
->st_ctimespec
.tv_sec
;
2728 vsbp
->vst_ctimensec
= sbp
->st_ctimespec
.tv_nsec
;
2729 vsbp
->vst_birthtime
= sbp
->st_birthtimespec
.tv_sec
;
2730 vsbp
->vst_birthtimensec
= sbp
->st_birthtimespec
.tv_nsec
;
2731 vsbp
->vst_size
= sbp
->st_size
;
2732 vsbp
->vst_blocks
= sbp
->st_blocks
;
2733 vsbp
->vst_blksize
= sbp
->st_blksize
;
2734 vsbp
->vst_flags
= sbp
->st_flags
;
2735 vsbp
->vst_gen
= sbp
->st_gen
;
2736 vsbp
->vst_rdev
= sbp
->st_rdev
;
2737 vsbp
->vst_qspare
[0] = sbp
->st_qspare
[0];
2738 vsbp
->vst_qspare
[1] = sbp
->st_qspare
[1];
2742 proc_pid_rusage(int pid
, int flavor
, user_addr_t buffer
, __unused
int32_t *retval
)
2748 if ((p
= proc_find(pid
)) == PROC_NULL
) {
2749 if ((p
= proc_find_zombref(pid
)) == PROC_NULL
) {
2755 /* Do we have permission to look into this? */
2756 if ((error
= proc_security_policy(p
, PROC_INFO_CALL_PIDRUSAGE
, flavor
, CHECK_SAME_USER
)))
2759 error
= proc_get_rusage(p
, flavor
, buffer
, zombie
);
2763 proc_drop_zombref(p
);
2771 proc_archinfo(proc_t p
, struct proc_archinfo
*pai
)
2774 pai
->p_cputype
= p
->p_cputype
;
2775 pai
->p_cpusubtype
= p
->p_cpusubtype
;
2780 proc_pidcoalitioninfo(proc_t p
, struct proc_pidcoalitioninfo
*ppci
)
2782 bzero(ppci
, sizeof(*ppci
));
2783 proc_coalitionids(p
, ppci
->coalition_id
);
2789 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
2790 * It mimics the data that is typically captured by the
2791 * EVFILT_PROC, NOTE_EXIT event mechanism.
2792 * See filt_proc() in kern_event.c.
2795 proc_pidnoteexit(proc_t p
, uint64_t flags
, uint32_t *data
)
2797 uint32_t exit_data
= 0;
2798 uint32_t exit_flags
= (uint32_t)flags
;
2803 * Allow access to the parent of the exiting
2804 * child or the parent debugger only.
2807 pid_t selfpid
= proc_selfpid();
2809 if (p
->p_ppid
== selfpid
)
2810 break; /* parent => ok */
2812 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
2813 (p
->p_oppid
== selfpid
))
2814 break; /* parent-in-waiting => ok */
2820 if ((exit_flags
& NOTE_EXITSTATUS
) != 0) {
2821 /* The signal and exit status */
2822 exit_data
|= (p
->p_xstat
& NOTE_PDATAMASK
);
2825 if ((exit_flags
& NOTE_EXIT_DETAIL
) != 0) {
2826 /* The exit detail */
2827 if ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) != 0) {
2828 exit_data
|= NOTE_EXIT_DECRYPTFAIL
;
2831 if ((p
->p_lflag
& P_LTERM_JETSAM
) != 0) {
2832 exit_data
|= NOTE_EXIT_MEMORY
;
2834 switch (p
->p_lflag
& P_JETSAM_MASK
) {
2835 case P_JETSAM_VMPAGESHORTAGE
:
2836 exit_data
|= NOTE_EXIT_MEMORY_VMPAGESHORTAGE
;
2838 case P_JETSAM_VMTHRASHING
:
2839 exit_data
|= NOTE_EXIT_MEMORY_VMTHRASHING
;
2841 case P_JETSAM_FCTHRASHING
:
2842 exit_data
|= NOTE_EXIT_MEMORY_FCTHRASHING
;
2844 case P_JETSAM_VNODE
:
2845 exit_data
|= NOTE_EXIT_MEMORY_VNODE
;
2847 case P_JETSAM_HIWAT
:
2848 exit_data
|= NOTE_EXIT_MEMORY_HIWAT
;
2851 exit_data
|= NOTE_EXIT_MEMORY_PID
;
2853 case P_JETSAM_IDLEEXIT
:
2854 exit_data
|= NOTE_EXIT_MEMORY_IDLE
;
2859 if ((p
->p_csflags
& CS_KILLED
) != 0) {
2860 exit_data
|= NOTE_EXIT_CSERROR
;