]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/proc_info.c
xnu-4570.31.3.tar.gz
[apple/xnu.git] / bsd / kern / proc_info.c
1 /*
2 * Copyright (c) 2005-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * sysctl system call.
31 */
32
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>
42 #include <sys/buf.h>
43 #include <sys/ioctl.h>
44 #include <sys/namei.h>
45 #include <sys/tty.h>
46 #include <sys/disklabel.h>
47 #include <sys/vm.h>
48 #include <sys/reason.h>
49 #include <sys/sysctl.h>
50 #include <sys/user.h>
51 #include <sys/aio_kern.h>
52 #include <sys/kern_memorystatus.h>
53
54 #include <security/audit/audit.h>
55
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>
63
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
66 #include <mach/host_info.h>
67 #include <mach/task_info.h>
68 #include <mach/thread_info.h>
69 #include <mach/vm_region.h>
70
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>
77 #include <sys/priv.h>
78
79 #include <sys/guarded.h>
80
81 #include <machine/machine_routines.h>
82
83 #include <kern/ipc_misc.h>
84
85 #include <vm/vm_protos.h>
86
87 /* Needed by proc_pidnoteexit(), proc_pidlistuptrs() */
88 #include <sys/event.h>
89 #include <sys/codesign.h>
90
91 /* Needed by proc_listcoalitions() */
92 #ifdef CONFIG_COALITIONS
93 #include <sys/coalition.h>
94 #endif
95
96 #if CONFIG_MACF
97 #include <security/mac_framework.h>
98 #endif
99
100 struct pshmnode;
101 struct psemnode;
102 struct pipe;
103 struct kqueue;
104 struct atalk;
105
106 uint64_t get_dispatchqueue_offset_from_proc(void *);
107 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *);
108 uint64_t get_return_to_kernel_offset_from_proc(void *p);
109 int proc_info_internal(int callnum, int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
110
111 /*
112 * TODO: Replace the noinline attribute below. Currently, it serves
113 * to avoid stack bloat caused by inlining multiple functions that
114 * have large stack footprints; when the functions are independent
115 * of each other (will not both be called in any given call to the
116 * caller), this only serves to bloat the stack, as we allocate
117 * space for both functions, despite the fact that we only need a
118 * fraction of that space.
119 *
120 * Long term, these functions should not be allocating everything on
121 * the stack, and should move large allocations (the huge structs
122 * that proc info deals in) to the heap, or eliminate them if
123 * possible.
124 *
125 * The functions that most desperately need to improve stack usage
126 * (starting with the worst offenders):
127 * proc_pidvnodepathinfo
128 * proc_pidinfo
129 * proc_pidregionpathinfo
130 * pid_vnodeinfopath
131 * pid_pshminfo
132 * pid_pseminfo
133 * pid_socketinfo
134 * proc_pid_rusage
135 * proc_pidoriginatorinfo
136 */
137
138 /* protos for proc_info calls */
139 int __attribute__ ((noinline)) proc_listpids(uint32_t type, uint32_t tyoneinfo, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
140 int __attribute__ ((noinline)) proc_pidinfo(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
141 int __attribute__ ((noinline)) proc_pidfdinfo(int pid, int flavor,int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
142 int __attribute__ ((noinline)) proc_kernmsgbuf(user_addr_t buffer, uint32_t buffersize, int32_t * retval);
143 int __attribute__ ((noinline)) proc_setcontrol(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
144 int __attribute__ ((noinline)) proc_pidfileportinfo(int pid, int flavor, mach_port_name_t name, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
145 int __attribute__ ((noinline)) proc_dirtycontrol(int pid, int flavor, uint64_t arg, int32_t * retval);
146 int __attribute__ ((noinline)) proc_terminate(int pid, int32_t * retval);
147 int __attribute__ ((noinline)) proc_pid_rusage(int pid, int flavor, user_addr_t buffer, int32_t * retval);
148 int __attribute__ ((noinline)) proc_pidoriginatorinfo(int pid, int flavor, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
149 int __attribute__ ((noinline)) proc_listcoalitions(int flavor, int coaltype, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
150 int __attribute__ ((noinline)) proc_can_use_foreground_hw(int pid, user_addr_t reason, uint32_t resonsize, int32_t *retval);
151
152 /* protos for procpidinfo calls */
153 int __attribute__ ((noinline)) proc_pidfdlist(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
154 int __attribute__ ((noinline)) proc_pidbsdinfo(proc_t p, struct proc_bsdinfo *pbsd, int zombie);
155 int __attribute__ ((noinline)) proc_pidshortbsdinfo(proc_t p, struct proc_bsdshortinfo *pbsd_shortp, int zombie);
156 int __attribute__ ((noinline)) proc_pidtaskinfo(proc_t p, struct proc_taskinfo *ptinfo);
157 int __attribute__ ((noinline)) proc_pidthreadinfo(proc_t p, uint64_t arg, int thuniqueid, struct proc_threadinfo *pthinfo);
158 int __attribute__ ((noinline)) proc_pidthreadpathinfo(proc_t p, uint64_t arg, struct proc_threadwithpathinfo *pinfo);
159 int __attribute__ ((noinline)) proc_pidlistthreads(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
160 int __attribute__ ((noinline)) proc_pidregioninfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
161 int __attribute__ ((noinline)) proc_pidregionpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
162 int __attribute__ ((noinline)) proc_pidregionpathinfo2(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
163 int __attribute__ ((noinline)) proc_pidregionpathinfo3(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
164 int __attribute__ ((noinline)) proc_pidvnodepathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
165 int __attribute__ ((noinline)) proc_pidpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
166 int __attribute__ ((noinline)) proc_pidworkqueueinfo(proc_t p, struct proc_workqueueinfo *pwqinfo);
167 int __attribute__ ((noinline)) proc_pidfileportlist(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
168 void __attribute__ ((noinline)) proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo);
169 void __attribute__ ((noinline)) proc_archinfo(proc_t p, struct proc_archinfo *pai);
170 void __attribute__ ((noinline)) proc_pidcoalitioninfo(proc_t p, struct proc_pidcoalitioninfo *pci);
171 int __attribute__ ((noinline)) proc_pidnoteexit(proc_t p, uint64_t arg, uint32_t *data);
172 int __attribute__ ((noinline)) proc_pidexitreasoninfo(proc_t p, struct proc_exitreasoninfo *peri, struct proc_exitreasonbasicinfo *pberi);
173 int __attribute__ ((noinline)) proc_pidoriginatorpid_uuid(uuid_t uuid, uint32_t buffersize, pid_t *pid);
174 int __attribute__ ((noinline)) proc_pidlistuptrs(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
175 int __attribute__ ((noinline)) proc_piddynkqueueinfo(pid_t pid, int flavor, kqueue_id_t id, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
176
177 /* protos for proc_pidfdinfo calls */
178 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);
179 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);
180 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);
181 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);
182 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);
183 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);
184 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);
185 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);
186
187
188 /* protos for misc */
189
190 int fill_vnodeinfo(vnode_t vp, struct vnode_info *vinfo);
191 void fill_fileinfo(struct fileproc * fp, proc_t proc, int fd, struct proc_fileinfo * finfo);
192 int proc_security_policy(proc_t targetp, int callnum, int flavor, boolean_t check_same_user);
193 static void munge_vinfo_stat(struct stat64 *sbp, struct vinfo_stat *vsbp);
194 static int proc_piduuidinfo(pid_t pid, uuid_t uuid_buf, uint32_t buffersize);
195 int proc_pidpathinfo_internal(proc_t p, __unused uint64_t arg, char *buf, uint32_t buffersize, __unused int32_t *retval);
196
197 extern int cansignal(struct proc *, kauth_cred_t, struct proc *, int, int);
198 extern int proc_get_rusage(proc_t proc, int flavor, user_addr_t buffer, int is_zombie);
199
200 #define CHECK_SAME_USER TRUE
201 #define NO_CHECK_SAME_USER FALSE
202
203 uint64_t get_dispatchqueue_offset_from_proc(void *p)
204 {
205 if(p != NULL) {
206 proc_t pself = (proc_t)p;
207 return (pself->p_dispatchqueue_offset);
208 } else {
209 return (uint64_t)0;
210 }
211 }
212
213 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p)
214 {
215 if(p != NULL) {
216 proc_t pself = (proc_t)p;
217 return (pself->p_dispatchqueue_serialno_offset);
218 } else {
219 return (uint64_t)0;
220 }
221 }
222
223 uint64_t get_return_to_kernel_offset_from_proc(void *p)
224 {
225 if (p != NULL) {
226 proc_t pself = (proc_t)p;
227 return (pself->p_return_to_kernel_offset);
228 } else {
229 return (uint64_t)0;
230 }
231 }
232
233 /***************************** proc_info ********************/
234
235 int
236 proc_info(__unused struct proc *p, struct proc_info_args * uap, int32_t *retval)
237 {
238 return(proc_info_internal(uap->callnum, uap->pid, uap->flavor, uap->arg, uap->buffer, uap->buffersize, retval));
239 }
240
241
242 int
243 proc_info_internal(int callnum, int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
244 {
245
246 switch(callnum) {
247 case PROC_INFO_CALL_LISTPIDS:
248 /* pid contains type and flavor contains typeinfo */
249 return(proc_listpids(pid, flavor, buffer, buffersize, retval));
250 case PROC_INFO_CALL_PIDINFO:
251 return(proc_pidinfo(pid, flavor, arg, buffer, buffersize, retval));
252 case PROC_INFO_CALL_PIDFDINFO:
253 return(proc_pidfdinfo(pid, flavor, (int)arg, buffer, buffersize, retval));
254 case PROC_INFO_CALL_KERNMSGBUF:
255 return(proc_kernmsgbuf(buffer, buffersize, retval));
256 case PROC_INFO_CALL_SETCONTROL:
257 return(proc_setcontrol(pid, flavor, arg, buffer, buffersize, retval));
258 case PROC_INFO_CALL_PIDFILEPORTINFO:
259 return(proc_pidfileportinfo(pid, flavor, (mach_port_name_t)arg, buffer, buffersize, retval));
260 case PROC_INFO_CALL_TERMINATE:
261 return(proc_terminate(pid, retval));
262 case PROC_INFO_CALL_DIRTYCONTROL:
263 return(proc_dirtycontrol(pid, flavor, arg, retval));
264 case PROC_INFO_CALL_PIDRUSAGE:
265 return (proc_pid_rusage(pid, flavor, buffer, retval));
266 case PROC_INFO_CALL_PIDORIGINATORINFO:
267 return (proc_pidoriginatorinfo(pid, flavor, buffer, buffersize, retval));
268 case PROC_INFO_CALL_LISTCOALITIONS:
269 return proc_listcoalitions(pid /* flavor */, flavor /* coaltype */, buffer,
270 buffersize, retval);
271 case PROC_INFO_CALL_CANUSEFGHW:
272 return proc_can_use_foreground_hw(pid, buffer, buffersize, retval);
273 case PROC_INFO_CALL_PIDDYNKQUEUEINFO:
274 return proc_piddynkqueueinfo(pid, flavor, (kqueue_id_t)arg, buffer, buffersize, retval);
275 default:
276 return EINVAL;
277 }
278
279 return(EINVAL);
280 }
281
282 /******************* proc_listpids routine ****************/
283 int
284 proc_listpids(uint32_t type, uint32_t typeinfo, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
285 {
286 uint32_t numprocs = 0;
287 uint32_t wantpids;
288 char * kbuf;
289 int * ptr;
290 uint32_t n;
291 int skip;
292 struct proc * p;
293 struct tty * tp;
294 int error = 0;
295 struct proclist *current_list;
296
297 /* Do we have permission to look into this? */
298 if ((error = proc_security_policy(PROC_NULL, PROC_INFO_CALL_LISTPIDS, type, NO_CHECK_SAME_USER)))
299 return (error);
300
301 /* if the buffer is null, return num of procs */
302 if (buffer == (user_addr_t)0) {
303 *retval = ((nprocs + 20) * sizeof(int));
304 return(0);
305 }
306
307 if (buffersize < sizeof(int)) {
308 return(ENOMEM);
309 }
310 wantpids = buffersize/sizeof(int);
311 if ((nprocs + 20) > 0) {
312 numprocs = (uint32_t)(nprocs + 20);
313 }
314 if (numprocs > wantpids) {
315 numprocs = wantpids;
316 }
317
318 kbuf = (char *)kalloc((vm_size_t)(numprocs * sizeof(int)));
319 if (kbuf == NULL) {
320 return(ENOMEM);
321 }
322 bzero(kbuf, sizeof(int));
323
324 proc_list_lock();
325
326
327 n = 0;
328 ptr = (int *)kbuf;
329 current_list = &allproc;
330 proc_loop:
331 LIST_FOREACH(p, current_list, p_list) {
332 skip = 0;
333 switch (type) {
334 case PROC_PGRP_ONLY:
335 if (p->p_pgrpid != (pid_t)typeinfo)
336 skip = 1;
337 break;
338 case PROC_PPID_ONLY:
339 if ((p->p_ppid != (pid_t)typeinfo) && (((p->p_lflag & P_LTRACED) == 0) || (p->p_oppid != (pid_t)typeinfo)))
340 skip = 1;
341 break;
342
343 case PROC_ALL_PIDS:
344 skip = 0;
345 break;
346 case PROC_TTY_ONLY:
347 /* racy but list lock is held */
348 if ((p->p_flag & P_CONTROLT) == 0 ||
349 (p->p_pgrp == NULL) || (p->p_pgrp->pg_session == NULL) ||
350 (tp = SESSION_TP(p->p_pgrp->pg_session)) == TTY_NULL ||
351 tp->t_dev != (dev_t)typeinfo)
352 skip = 1;
353 break;
354 case PROC_UID_ONLY:
355 if (p->p_ucred == NULL)
356 skip = 1;
357 else {
358 kauth_cred_t my_cred;
359 uid_t uid;
360
361 my_cred = kauth_cred_proc_ref(p);
362 uid = kauth_cred_getuid(my_cred);
363 kauth_cred_unref(&my_cred);
364 if (uid != (uid_t)typeinfo)
365 skip = 1;
366 }
367 break;
368 case PROC_RUID_ONLY:
369 if (p->p_ucred == NULL)
370 skip = 1;
371 else {
372 kauth_cred_t my_cred;
373 uid_t uid;
374
375 my_cred = kauth_cred_proc_ref(p);
376 uid = kauth_cred_getruid(my_cred);
377 kauth_cred_unref(&my_cred);
378 if (uid != (uid_t)typeinfo)
379 skip = 1;
380 }
381 break;
382 case PROC_KDBG_ONLY:
383 if (p->p_kdebug == 0) {
384 skip = 1;
385 }
386 break;
387 default:
388 skip = 1;
389 break;
390 };
391
392 if(skip == 0) {
393 *ptr++ = p->p_pid;
394 n++;
395 }
396 if (n >= numprocs)
397 break;
398 }
399
400 if ((n < numprocs) && (current_list == &allproc)) {
401 current_list = &zombproc;
402 goto proc_loop;
403 }
404
405 proc_list_unlock();
406
407 ptr = (int *)kbuf;
408 error = copyout((caddr_t)ptr, buffer, n * sizeof(int));
409 if (error == 0)
410 *retval = (n * sizeof(int));
411 kfree((void *)kbuf, (vm_size_t)(numprocs * sizeof(int)));
412
413 return(error);
414 }
415
416
417 /********************************** proc_pidfdlist routines ********************************/
418
419 int
420 proc_pidfdlist(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval)
421 {
422 uint32_t numfds = 0;
423 uint32_t needfds;
424 char * kbuf;
425 struct proc_fdinfo * pfd;
426 struct fileproc * fp;
427 int n;
428 int count = 0;
429 int error = 0;
430
431 if (p->p_fd->fd_nfiles > 0) {
432 numfds = (uint32_t)p->p_fd->fd_nfiles;
433 }
434
435 if (buffer == (user_addr_t) 0) {
436 numfds += 20;
437 *retval = (numfds * sizeof(struct proc_fdinfo));
438 return(0);
439 }
440
441 /* buffersize is big enough atleast for one struct */
442 needfds = buffersize/sizeof(struct proc_fdinfo);
443
444 if (numfds > needfds) {
445 numfds = needfds;
446 }
447
448 kbuf = (char *)kalloc((vm_size_t)(numfds * sizeof(struct proc_fdinfo)));
449 if (kbuf == NULL)
450 return(ENOMEM);
451 bzero(kbuf, numfds * sizeof(struct proc_fdinfo));
452
453 proc_fdlock(p);
454
455 pfd = (struct proc_fdinfo *)kbuf;
456
457 for (n = 0; ((n < (int)numfds) && (n < p->p_fd->fd_nfiles)); n++) {
458 if (((fp = p->p_fd->fd_ofiles[n]) != 0)
459 && ((p->p_fd->fd_ofileflags[n] & UF_RESERVED) == 0)) {
460 file_type_t fdtype = FILEGLOB_DTYPE(fp->f_fglob);
461 pfd->proc_fd = n;
462 pfd->proc_fdtype = (fdtype != DTYPE_ATALK) ?
463 fdtype : PROX_FDTYPE_ATALK;
464 count++;
465 pfd++;
466 }
467 }
468 proc_fdunlock(p);
469
470 error = copyout(kbuf, buffer, count * sizeof(struct proc_fdinfo));
471 kfree((void *)kbuf, (vm_size_t)(numfds * sizeof(struct proc_fdinfo)));
472 if (error == 0)
473 *retval = (count * sizeof(struct proc_fdinfo));
474 return(error);
475 }
476
477 /*
478 * Helper functions for proc_pidfileportlist.
479 */
480 static int
481 proc_fileport_count(__unused mach_port_name_t name,
482 __unused struct fileglob *fg, void *arg)
483 {
484 uint32_t *counter = arg;
485
486 *counter += 1;
487 return (0);
488 }
489
490 struct fileport_fdtype_args {
491 struct proc_fileportinfo *ffa_pfi;
492 struct proc_fileportinfo *ffa_pfi_end;
493 };
494
495 static int
496 proc_fileport_fdtype(mach_port_name_t name, struct fileglob *fg, void *arg)
497 {
498 struct fileport_fdtype_args *ffa = arg;
499
500 if (ffa->ffa_pfi != ffa->ffa_pfi_end) {
501 file_type_t fdtype = FILEGLOB_DTYPE(fg);
502
503 ffa->ffa_pfi->proc_fdtype = (fdtype != DTYPE_ATALK) ?
504 fdtype : PROX_FDTYPE_ATALK;
505 ffa->ffa_pfi->proc_fileport = name;
506 ffa->ffa_pfi++;
507 return (0); /* keep walking */
508 } else
509 return (-1); /* stop the walk! */
510 }
511
512 int
513 proc_pidfileportlist(proc_t p,
514 user_addr_t buffer, uint32_t buffersize, int32_t *retval)
515 {
516 void *kbuf;
517 vm_size_t kbufsize;
518 struct proc_fileportinfo *pfi;
519 uint32_t needfileports, numfileports;
520 struct fileport_fdtype_args ffa;
521 int error;
522
523 needfileports = buffersize / sizeof (*pfi);
524 if ((user_addr_t)0 == buffer || needfileports > (uint32_t)maxfiles) {
525 /*
526 * Either (i) the user is asking for a fileport count,
527 * or (ii) the number of fileports they're asking for is
528 * larger than the maximum number of open files (!); count
529 * them to bound subsequent heap allocations.
530 */
531 numfileports = 0;
532 switch (fileport_walk(p->task,
533 proc_fileport_count, &numfileports)) {
534 case KERN_SUCCESS:
535 break;
536 case KERN_RESOURCE_SHORTAGE:
537 return (ENOMEM);
538 case KERN_INVALID_TASK:
539 return (ESRCH);
540 default:
541 return (EINVAL);
542 }
543
544 if (numfileports == 0) {
545 *retval = 0; /* none at all, bail */
546 return (0);
547 }
548 if ((user_addr_t)0 == buffer) {
549 numfileports += 20; /* accelerate convergence */
550 *retval = numfileports * sizeof (*pfi);
551 return (0);
552 }
553 if (needfileports > numfileports)
554 needfileports = numfileports;
555 }
556
557 assert(buffersize >= PROC_PIDLISTFILEPORTS_SIZE);
558
559 kbufsize = (vm_size_t)needfileports * sizeof (*pfi);
560 pfi = kbuf = kalloc(kbufsize);
561 if (kbuf == NULL)
562 return (ENOMEM);
563 bzero(kbuf, kbufsize);
564
565 ffa.ffa_pfi = pfi;
566 ffa.ffa_pfi_end = pfi + needfileports;
567
568 switch (fileport_walk(p->task, proc_fileport_fdtype, &ffa)) {
569 case KERN_SUCCESS:
570 error = 0;
571 pfi = ffa.ffa_pfi;
572 if ((numfileports = pfi - (typeof(pfi))kbuf) == 0)
573 break;
574 if (numfileports > needfileports)
575 panic("more fileports returned than requested");
576 error = copyout(kbuf, buffer, numfileports * sizeof (*pfi));
577 break;
578 case KERN_RESOURCE_SHORTAGE:
579 error = ENOMEM;
580 break;
581 case KERN_INVALID_TASK:
582 error = ESRCH;
583 break;
584 default:
585 error = EINVAL;
586 break;
587 }
588 kfree(kbuf, kbufsize);
589 if (error == 0)
590 *retval = numfileports * sizeof (*pfi);
591 return (error);
592 }
593
594 int
595 proc_pidbsdinfo(proc_t p, struct proc_bsdinfo * pbsd, int zombie)
596 {
597 struct tty *tp;
598 struct session *sessionp = NULL;
599 struct pgrp * pg;
600 kauth_cred_t my_cred;
601
602 pg = proc_pgrp(p);
603 sessionp = proc_session(p);
604
605 my_cred = kauth_cred_proc_ref(p);
606 bzero(pbsd, sizeof(struct proc_bsdinfo));
607 pbsd->pbi_status = p->p_stat;
608 pbsd->pbi_xstatus = p->p_xstat;
609 pbsd->pbi_pid = p->p_pid;
610 pbsd->pbi_ppid = p->p_ppid;
611 pbsd->pbi_uid = kauth_cred_getuid(my_cred);
612 pbsd->pbi_gid = kauth_cred_getgid(my_cred);
613 pbsd->pbi_ruid = kauth_cred_getruid(my_cred);
614 pbsd->pbi_rgid = kauth_cred_getrgid(my_cred);
615 pbsd->pbi_svuid = kauth_cred_getsvuid(my_cred);
616 pbsd->pbi_svgid = kauth_cred_getsvgid(my_cred);
617 kauth_cred_unref(&my_cred);
618
619 pbsd->pbi_nice = p->p_nice;
620 pbsd->pbi_start_tvsec = p->p_start.tv_sec;
621 pbsd->pbi_start_tvusec = p->p_start.tv_usec;
622 bcopy(&p->p_comm, &pbsd->pbi_comm[0], MAXCOMLEN);
623 pbsd->pbi_comm[MAXCOMLEN - 1] = '\0';
624 bcopy(&p->p_name, &pbsd->pbi_name[0], 2*MAXCOMLEN);
625 pbsd->pbi_name[(2*MAXCOMLEN) - 1] = '\0';
626
627 pbsd->pbi_flags = 0;
628 if ((p->p_flag & P_SYSTEM) == P_SYSTEM)
629 pbsd->pbi_flags |= PROC_FLAG_SYSTEM;
630 if ((p->p_lflag & P_LTRACED) == P_LTRACED)
631 pbsd->pbi_flags |= PROC_FLAG_TRACED;
632 if ((p->p_lflag & P_LEXIT) == P_LEXIT)
633 pbsd->pbi_flags |= PROC_FLAG_INEXIT;
634 if ((p->p_lflag & P_LPPWAIT) == P_LPPWAIT)
635 pbsd->pbi_flags |= PROC_FLAG_PPWAIT;
636 if ((p->p_flag & P_LP64) == P_LP64)
637 pbsd->pbi_flags |= PROC_FLAG_LP64;
638 if ((p->p_flag & P_CONTROLT) == P_CONTROLT)
639 pbsd->pbi_flags |= PROC_FLAG_CONTROLT;
640 if ((p->p_flag & P_THCWD) == P_THCWD)
641 pbsd->pbi_flags |= PROC_FLAG_THCWD;
642 if ((p->p_flag & P_SUGID) == P_SUGID)
643 pbsd->pbi_flags |= PROC_FLAG_PSUGID;
644 if ((p->p_flag & P_EXEC) == P_EXEC)
645 pbsd->pbi_flags |= PROC_FLAG_EXEC;
646
647 if (sessionp != SESSION_NULL) {
648 if (SESS_LEADER(p, sessionp))
649 pbsd->pbi_flags |= PROC_FLAG_SLEADER;
650 if (sessionp->s_ttyvp)
651 pbsd->pbi_flags |= PROC_FLAG_CTTY;
652 }
653
654 #if !CONFIG_EMBEDDED
655 if ((p->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP)
656 pbsd->pbi_flags |= PROC_FLAG_DELAYIDLESLEEP;
657 #endif /* !CONFIG_EMBEDDED */
658
659 switch(PROC_CONTROL_STATE(p)) {
660 case P_PCTHROTTLE:
661 pbsd->pbi_flags |= PROC_FLAG_PC_THROTTLE;
662 break;
663 case P_PCSUSP:
664 pbsd->pbi_flags |= PROC_FLAG_PC_SUSP;
665 break;
666 case P_PCKILL:
667 pbsd->pbi_flags |= PROC_FLAG_PC_KILL;
668 break;
669 };
670
671 switch(PROC_ACTION_STATE(p)) {
672 case P_PCTHROTTLE:
673 pbsd->pbi_flags |= PROC_FLAG_PA_THROTTLE;
674 break;
675 case P_PCSUSP:
676 pbsd->pbi_flags |= PROC_FLAG_PA_SUSP;
677 break;
678 };
679
680 /* if process is a zombie skip bg state */
681 if ((zombie == 0) && (p->p_stat != SZOMB) && (p->task != TASK_NULL))
682 proc_get_darwinbgstate(p->task, &pbsd->pbi_flags);
683
684 if (zombie == 0)
685 pbsd->pbi_nfiles = p->p_fd->fd_nfiles;
686
687 pbsd->e_tdev = NODEV;
688 if (pg != PGRP_NULL) {
689 pbsd->pbi_pgid = p->p_pgrpid;
690 pbsd->pbi_pjobc = pg->pg_jobc;
691 if ((p->p_flag & P_CONTROLT) && (sessionp != SESSION_NULL) && (tp = SESSION_TP(sessionp))) {
692 pbsd->e_tdev = tp->t_dev;
693 pbsd->e_tpgid = sessionp->s_ttypgrpid;
694 }
695 }
696 if (sessionp != SESSION_NULL)
697 session_rele(sessionp);
698 if (pg != PGRP_NULL)
699 pg_rele(pg);
700
701 return(0);
702 }
703
704
705 int
706 proc_pidshortbsdinfo(proc_t p, struct proc_bsdshortinfo * pbsd_shortp, int zombie)
707 {
708 bzero(pbsd_shortp, sizeof(struct proc_bsdshortinfo));
709 pbsd_shortp->pbsi_pid = p->p_pid;
710 pbsd_shortp->pbsi_ppid = p->p_ppid;
711 pbsd_shortp->pbsi_pgid = p->p_pgrpid;
712 pbsd_shortp->pbsi_status = p->p_stat;
713 bcopy(&p->p_comm, &pbsd_shortp->pbsi_comm[0], MAXCOMLEN);
714 pbsd_shortp->pbsi_comm[MAXCOMLEN - 1] = '\0';
715
716 pbsd_shortp->pbsi_flags = 0;
717 if ((p->p_flag & P_SYSTEM) == P_SYSTEM)
718 pbsd_shortp->pbsi_flags |= PROC_FLAG_SYSTEM;
719 if ((p->p_lflag & P_LTRACED) == P_LTRACED)
720 pbsd_shortp->pbsi_flags |= PROC_FLAG_TRACED;
721 if ((p->p_lflag & P_LEXIT) == P_LEXIT)
722 pbsd_shortp->pbsi_flags |= PROC_FLAG_INEXIT;
723 if ((p->p_lflag & P_LPPWAIT) == P_LPPWAIT)
724 pbsd_shortp->pbsi_flags |= PROC_FLAG_PPWAIT;
725 if ((p->p_flag & P_LP64) == P_LP64)
726 pbsd_shortp->pbsi_flags |= PROC_FLAG_LP64;
727 if ((p->p_flag & P_CONTROLT) == P_CONTROLT)
728 pbsd_shortp->pbsi_flags |= PROC_FLAG_CONTROLT;
729 if ((p->p_flag & P_THCWD) == P_THCWD)
730 pbsd_shortp->pbsi_flags |= PROC_FLAG_THCWD;
731 if ((p->p_flag & P_SUGID) == P_SUGID)
732 pbsd_shortp->pbsi_flags |= PROC_FLAG_PSUGID;
733 if ((p->p_flag & P_EXEC) == P_EXEC)
734 pbsd_shortp->pbsi_flags |= PROC_FLAG_EXEC;
735 #if !CONFIG_EMBEDDED
736 if ((p->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP)
737 pbsd_shortp->pbsi_flags |= PROC_FLAG_DELAYIDLESLEEP;
738 #endif /* !CONFIG_EMBEDDED */
739
740 switch(PROC_CONTROL_STATE(p)) {
741 case P_PCTHROTTLE:
742 pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_THROTTLE;
743 break;
744 case P_PCSUSP:
745 pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_SUSP;
746 break;
747 case P_PCKILL:
748 pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_KILL;
749 break;
750 };
751
752 switch(PROC_ACTION_STATE(p)) {
753 case P_PCTHROTTLE:
754 pbsd_shortp->pbsi_flags |= PROC_FLAG_PA_THROTTLE;
755 break;
756 case P_PCSUSP:
757 pbsd_shortp->pbsi_flags |= PROC_FLAG_PA_SUSP;
758 break;
759 };
760
761 /* if process is a zombie skip bg state */
762 if ((zombie == 0) && (p->p_stat != SZOMB) && (p->task != TASK_NULL))
763 proc_get_darwinbgstate(p->task, &pbsd_shortp->pbsi_flags);
764
765 pbsd_shortp->pbsi_uid = p->p_uid;
766 pbsd_shortp->pbsi_gid = p->p_gid;
767 pbsd_shortp->pbsi_ruid = p->p_ruid;
768 pbsd_shortp->pbsi_rgid = p->p_rgid;
769 pbsd_shortp->pbsi_svuid = p->p_svuid;
770 pbsd_shortp->pbsi_svgid = p->p_svgid;
771
772 return(0);
773 }
774
775 int
776 proc_pidtaskinfo(proc_t p, struct proc_taskinfo * ptinfo)
777 {
778 task_t task;
779
780 task = p->task;
781
782 bzero(ptinfo, sizeof(struct proc_taskinfo));
783 fill_taskprocinfo(task, (struct proc_taskinfo_internal *)ptinfo);
784
785 return(0);
786 }
787
788
789
790 int
791 proc_pidthreadinfo(proc_t p, uint64_t arg, int thuniqueid, struct proc_threadinfo *pthinfo)
792 {
793 int error = 0;
794 uint64_t threadaddr = (uint64_t)arg;
795
796 bzero(pthinfo, sizeof(struct proc_threadinfo));
797
798 error = fill_taskthreadinfo(p->task, threadaddr, thuniqueid, (struct proc_threadinfo_internal *)pthinfo, NULL, NULL);
799 if (error)
800 return(ESRCH);
801 else
802 return(0);
803
804 }
805
806 boolean_t
807 bsd_hasthreadname(void *uth)
808 {
809 struct uthread *ut = (struct uthread*)uth;
810
811 /* This doesn't check for the empty string; do we care? */
812 if (ut->pth_name) {
813 return TRUE;
814 } else {
815 return FALSE;
816 }
817 }
818
819 void
820 bsd_getthreadname(void *uth, char *buffer)
821 {
822 struct uthread *ut = (struct uthread *)uth;
823 if(ut->pth_name)
824 bcopy(ut->pth_name,buffer,MAXTHREADNAMESIZE);
825 }
826
827 /*
828 * This is known to race with regards to the contents of the thread name; concurrent
829 * callers may result in a garbled name.
830 */
831 void
832 bsd_setthreadname(void *uth, const char *name) {
833 struct uthread *ut = (struct uthread *)uth;
834 char * name_buf = NULL;
835
836 if (!ut->pth_name) {
837 /* If there is no existing thread name, allocate a buffer for one. */
838 name_buf = kalloc(MAXTHREADNAMESIZE);
839 assert(name_buf);
840 bzero(name_buf, MAXTHREADNAMESIZE);
841
842 /* Someone could conceivably have named the thread at the same time we did. */
843 if (!OSCompareAndSwapPtr(NULL, name_buf, &ut->pth_name)) {
844 kfree(name_buf, MAXTHREADNAMESIZE);
845 }
846 } else {
847 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV, ut->pth_name);
848 }
849
850 strncpy(ut->pth_name, name, MAXTHREADNAMESIZE - 1);
851 kernel_debug_string_simple(TRACE_STRING_THREADNAME, ut->pth_name);
852 }
853
854 void
855 bsd_copythreadname(void *dst_uth, void *src_uth)
856 {
857 struct uthread *dst_ut = (struct uthread *)dst_uth;
858 struct uthread *src_ut = (struct uthread *)src_uth;
859
860 if (src_ut->pth_name == NULL)
861 return;
862
863 if (dst_ut->pth_name == NULL) {
864 dst_ut->pth_name = (char *)kalloc(MAXTHREADNAMESIZE);
865 if (dst_ut->pth_name == NULL)
866 return;
867 }
868
869 bcopy(src_ut->pth_name, dst_ut->pth_name, MAXTHREADNAMESIZE);
870 return;
871 }
872
873 void
874 bsd_threadcdir(void * uth, void *vptr, int *vidp)
875 {
876 struct uthread * ut = (struct uthread *)uth;
877 vnode_t vp;
878 vnode_t *vpp = (vnode_t *)vptr;
879
880 vp = ut->uu_cdir;
881 if (vp != NULLVP) {
882 if (vpp != NULL) {
883 *vpp = vp;
884 if (vidp != NULL)
885 *vidp = vp->v_id;
886 }
887 }
888 }
889
890
891 int
892 proc_pidthreadpathinfo(proc_t p, uint64_t arg, struct proc_threadwithpathinfo *pinfo)
893 {
894 vnode_t vp = NULLVP;
895 int vid;
896 int error = 0;
897 uint64_t threadaddr = (uint64_t)arg;
898 int count;
899
900 bzero(pinfo, sizeof(struct proc_threadwithpathinfo));
901
902 error = fill_taskthreadinfo(p->task, threadaddr, 0, (struct proc_threadinfo_internal *)&pinfo->pt, (void *)&vp, &vid);
903 if (error)
904 return(ESRCH);
905
906 if ((vp != NULLVP) && ((vnode_getwithvid(vp, vid)) == 0)) {
907 error = fill_vnodeinfo(vp, &pinfo->pvip.vip_vi) ;
908 if (error == 0) {
909 count = MAXPATHLEN;
910 vn_getpath(vp, &pinfo->pvip.vip_path[0], &count);
911 pinfo->pvip.vip_path[MAXPATHLEN-1] = 0;
912 }
913 vnode_put(vp);
914 }
915 return(error);
916 }
917
918
919
920 int
921 proc_pidlistthreads(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval)
922 {
923 uint32_t count = 0;
924 int ret = 0;
925 int error = 0;
926 void * kbuf;
927 uint32_t numthreads = 0;
928
929 int num = get_numthreads(p->task) + 10;
930 if (num > 0) {
931 numthreads = (uint32_t)num;
932 }
933
934 count = buffersize/(sizeof(uint64_t));
935
936 if (numthreads > count) {
937 numthreads = count;
938 }
939
940 kbuf = (void *)kalloc(numthreads * sizeof(uint64_t));
941 if (kbuf == NULL)
942 return(ENOMEM);
943 bzero(kbuf, numthreads * sizeof(uint64_t));
944
945 ret = fill_taskthreadlist(p->task, kbuf, numthreads);
946
947 error = copyout(kbuf, buffer, ret);
948 kfree(kbuf, numthreads * sizeof(uint64_t));
949 if (error == 0)
950 *retval = ret;
951 return(error);
952
953 }
954
955
956 int
957 proc_pidregioninfo(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
958 {
959 struct proc_regioninfo preginfo;
960 int ret, error = 0;
961
962 bzero(&preginfo, sizeof(struct proc_regioninfo));
963 ret = fill_procregioninfo( p->task, arg, (struct proc_regioninfo_internal *)&preginfo, (uintptr_t *)0, (uint32_t *)0);
964 if (ret == 0)
965 return(EINVAL);
966 error = copyout(&preginfo, buffer, sizeof(struct proc_regioninfo));
967 if (error == 0)
968 *retval = sizeof(struct proc_regioninfo);
969 return(error);
970 }
971
972
973 int
974 proc_pidregionpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
975 {
976 struct proc_regionwithpathinfo preginfo;
977 int ret, error = 0;
978 uintptr_t vnodeaddr= 0;
979 uint32_t vnodeid= 0;
980 vnode_t vp;
981 int count;
982
983 bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));
984
985 ret = fill_procregioninfo( p->task, arg, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
986 if (ret == 0)
987 return(EINVAL);
988 if (vnodeaddr) {
989 vp = (vnode_t)vnodeaddr;
990 if ((vnode_getwithvid(vp, vnodeid)) == 0) {
991 /* FILL THE VNODEINFO */
992 error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi);
993 count = MAXPATHLEN;
994 vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
995 /* Always make sure it is null terminated */
996 preginfo.prp_vip.vip_path[MAXPATHLEN-1] = 0;
997 vnode_put(vp);
998 }
999 }
1000 error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
1001 if (error == 0)
1002 *retval = sizeof(struct proc_regionwithpathinfo);
1003 return(error);
1004 }
1005
1006 int
1007 proc_pidregionpathinfo2(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1008 {
1009 struct proc_regionwithpathinfo preginfo;
1010 int ret, error = 0;
1011 uintptr_t vnodeaddr= 0;
1012 uint32_t vnodeid= 0;
1013 vnode_t vp;
1014 int count;
1015
1016 bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));
1017
1018 ret = fill_procregioninfo_onlymappedvnodes( p->task, arg, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
1019 if (ret == 0)
1020 return(EINVAL);
1021 if (!vnodeaddr)
1022 return(EINVAL);
1023
1024 vp = (vnode_t)vnodeaddr;
1025 if ((vnode_getwithvid(vp, vnodeid)) == 0) {
1026 /* FILL THE VNODEINFO */
1027 error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi);
1028 count = MAXPATHLEN;
1029 vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
1030 /* Always make sure it is null terminated */
1031 preginfo.prp_vip.vip_path[MAXPATHLEN-1] = 0;
1032 vnode_put(vp);
1033 } else {
1034 return(EINVAL);
1035 }
1036
1037 error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
1038 if (error == 0)
1039 *retval = sizeof(struct proc_regionwithpathinfo);
1040 return(error);
1041 }
1042
1043 int
1044 proc_pidregionpathinfo3(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1045 {
1046 struct proc_regionwithpathinfo preginfo;
1047 int ret, error = 0;
1048 uintptr_t vnodeaddr;
1049 uint32_t vnodeid;
1050 vnode_t vp;
1051 int count;
1052 uint64_t addr = 0;
1053
1054 /* Loop while looking for vnodes that match dev_t filter */
1055 do {
1056 bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));
1057 vnodeaddr = 0;
1058 vnodeid = 0;
1059
1060 ret = fill_procregioninfo_onlymappedvnodes( p->task, addr, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
1061 if (ret == 0)
1062 return(EINVAL);
1063 if (!vnodeaddr)
1064 return(EINVAL);
1065
1066 vp = (vnode_t)vnodeaddr;
1067 if ((vnode_getwithvid(vp, vnodeid)) == 0) {
1068 /* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
1069 struct vnode_attr va;
1070
1071 memset(&va, 0, sizeof(va));
1072 VATTR_INIT(&va);
1073 VATTR_WANTED(&va, va_fsid);
1074
1075 ret = vnode_getattr(vp, &va, vfs_context_current());
1076 if (ret) {
1077 vnode_put(vp);
1078 return(EINVAL);
1079 }
1080
1081 if (va.va_fsid == arg) {
1082 /* FILL THE VNODEINFO */
1083 error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi);
1084 count = MAXPATHLEN;
1085 vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
1086 /* Always make sure it is null terminated */
1087 preginfo.prp_vip.vip_path[MAXPATHLEN-1] = 0;
1088 vnode_put(vp);
1089 break;
1090 }
1091 vnode_put(vp);
1092 } else {
1093 return(EINVAL);
1094 }
1095
1096 addr = preginfo.prp_prinfo.pri_address + preginfo.prp_prinfo.pri_size;
1097 } while (1);
1098
1099 error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
1100 if (error == 0)
1101 *retval = sizeof(struct proc_regionwithpathinfo);
1102 return(error);
1103 }
1104
1105 /*
1106 * Path is relative to current process directory; may different from current
1107 * thread directory.
1108 */
1109 int
1110 proc_pidvnodepathinfo(proc_t p, __unused uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1111 {
1112 struct proc_vnodepathinfo pvninfo;
1113 int error = 0;
1114 vnode_t vncdirvp = NULLVP;
1115 uint32_t vncdirid=0;
1116 vnode_t vnrdirvp = NULLVP;
1117 uint32_t vnrdirid=0;
1118 int count;
1119
1120 bzero(&pvninfo, sizeof(struct proc_vnodepathinfo));
1121
1122 proc_fdlock(p);
1123 if (p->p_fd->fd_cdir) {
1124 vncdirvp = p->p_fd->fd_cdir;
1125 vncdirid = p->p_fd->fd_cdir->v_id;
1126 }
1127 if (p->p_fd->fd_rdir) {
1128 vnrdirvp = p->p_fd->fd_rdir;
1129 vnrdirid = p->p_fd->fd_rdir->v_id;
1130 }
1131 proc_fdunlock(p);
1132
1133 if (vncdirvp != NULLVP) {
1134 if ((error = vnode_getwithvid(vncdirvp, vncdirid)) == 0) {
1135 /* FILL THE VNODEINFO */
1136 error = fill_vnodeinfo(vncdirvp, &pvninfo.pvi_cdir.vip_vi);
1137 if ( error == 0) {
1138 count = MAXPATHLEN;
1139 vn_getpath(vncdirvp, &pvninfo.pvi_cdir.vip_path[0], &count);
1140 pvninfo.pvi_cdir.vip_path[MAXPATHLEN-1] = 0;
1141 }
1142 vnode_put(vncdirvp);
1143 } else {
1144 goto out;
1145 }
1146 }
1147
1148 if ((error == 0) && (vnrdirvp != NULLVP)) {
1149 if ((error = vnode_getwithvid(vnrdirvp, vnrdirid)) == 0) {
1150 /* FILL THE VNODEINFO */
1151 error = fill_vnodeinfo(vnrdirvp, &pvninfo.pvi_rdir.vip_vi);
1152 if ( error == 0) {
1153 count = MAXPATHLEN;
1154 vn_getpath(vnrdirvp, &pvninfo.pvi_rdir.vip_path[0], &count);
1155 pvninfo.pvi_rdir.vip_path[MAXPATHLEN-1] = 0;
1156 }
1157 vnode_put(vnrdirvp);
1158 } else {
1159 goto out;
1160 }
1161 }
1162 if (error == 0) {
1163 error = copyout(&pvninfo, buffer, sizeof(struct proc_vnodepathinfo));
1164 if (error == 0)
1165 *retval = sizeof(struct proc_vnodepathinfo);
1166 }
1167 out:
1168 return(error);
1169 }
1170
1171 int
1172 proc_pidpathinfo(proc_t p, __unused uint64_t arg, user_addr_t buffer, uint32_t buffersize, __unused int32_t *retval)
1173 {
1174 int error;
1175 vnode_t tvp;
1176 int len = buffersize;
1177 char * buf;
1178
1179 tvp = p->p_textvp;
1180
1181 if (tvp == NULLVP)
1182 return(ESRCH);
1183
1184 buf = (char *)kalloc(buffersize);
1185 if (buf == NULL)
1186 return(ENOMEM);
1187
1188 bzero(buf, buffersize);
1189
1190 error = proc_pidpathinfo_internal(p, arg, buf, buffersize, retval);
1191 if (error == 0) {
1192 error = copyout(buf, buffer, len);
1193 }
1194 kfree(buf, buffersize);
1195 return(error);
1196 }
1197
1198 int
1199 proc_pidpathinfo_internal(proc_t p, __unused uint64_t arg, char *buf, uint32_t buffersize, __unused int32_t *retval)
1200 {
1201 int vid, error;
1202 vnode_t tvp;
1203 vnode_t nvp = NULLVP;
1204 int len = buffersize;
1205
1206 tvp = p->p_textvp;
1207
1208 if (tvp == NULLVP)
1209 return(ESRCH);
1210
1211 vid = vnode_vid(tvp);
1212 error = vnode_getwithvid(tvp, vid);
1213 if (error == 0) {
1214 error = vn_getpath_fsenter(tvp, buf, &len);
1215 vnode_put(tvp);
1216 if (error == 0) {
1217 error = vnode_lookup(buf, 0, &nvp, vfs_context_current());
1218 if ((error == 0) && ( nvp != NULLVP))
1219 vnode_put(nvp);
1220 }
1221 }
1222 return(error);
1223 }
1224
1225
1226 int
1227 proc_pidworkqueueinfo(proc_t p, struct proc_workqueueinfo *pwqinfo)
1228 {
1229 int error = 0;
1230
1231 bzero(pwqinfo, sizeof(struct proc_workqueueinfo));
1232
1233 error = fill_procworkqueue(p, pwqinfo);
1234 if (error)
1235 return(ESRCH);
1236 else
1237 return(0);
1238
1239 }
1240
1241
1242 void
1243 proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo)
1244 {
1245 p_uniqidinfo->p_uniqueid = proc_uniqueid(p);
1246 proc_getexecutableuuid(p, (unsigned char *)&p_uniqidinfo->p_uuid, sizeof(p_uniqidinfo->p_uuid));
1247 p_uniqidinfo->p_puniqueid = proc_puniqueid(p);
1248 p_uniqidinfo->p_reserve2 = 0;
1249 p_uniqidinfo->p_reserve3 = 0;
1250 p_uniqidinfo->p_reserve4 = 0;
1251 }
1252
1253
1254 static int
1255 proc_piduuidinfo(pid_t pid, uuid_t uuid_buf, uint32_t buffersize)
1256 {
1257 struct proc * p = PROC_NULL;
1258 int zombref = 0;
1259
1260 if (buffersize < sizeof(uuid_t))
1261 return EINVAL;
1262
1263 if ((p = proc_find(pid)) == PROC_NULL) {
1264 p = proc_find_zombref(pid);
1265 zombref = 1;
1266 }
1267 if (p == PROC_NULL) {
1268 return ESRCH;
1269 }
1270
1271 proc_getexecutableuuid(p, (unsigned char *)uuid_buf, buffersize);
1272
1273 if (zombref)
1274 proc_drop_zombref(p);
1275 else
1276 proc_rele(p);
1277
1278 return 0;
1279 }
1280
1281 /*
1282 * Function to get the uuid and pid of the originator of the voucher.
1283 */
1284 int
1285 proc_pidoriginatorpid_uuid(uuid_t uuid, uint32_t buffersize, pid_t *pid)
1286 {
1287 pid_t originator_pid;
1288 kern_return_t kr;
1289 int error;
1290
1291 /*
1292 * Get the current voucher origin pid. The pid returned here
1293 * might not be valid or may have been recycled.
1294 */
1295 kr = thread_get_current_voucher_origin_pid(&originator_pid);
1296 /* If errors, convert errors to appropriate format */
1297 if (kr) {
1298 if (kr == KERN_INVALID_TASK)
1299 error = ESRCH;
1300 else if (kr == KERN_INVALID_VALUE)
1301 error = ENOATTR;
1302 else
1303 error = EINVAL;
1304 return error;
1305 }
1306
1307 *pid = originator_pid;
1308 error = proc_piduuidinfo(originator_pid, uuid, buffersize);
1309 return error;
1310 }
1311
1312 /*
1313 * Function to get the uuid of the originator of the voucher.
1314 */
1315 int
1316 proc_pidoriginatoruuid(uuid_t uuid, uint32_t buffersize)
1317 {
1318 pid_t originator_pid;
1319 return (proc_pidoriginatorpid_uuid(uuid, buffersize, &originator_pid));
1320 }
1321
1322 /***************************** proc_pidoriginatorinfo ***************************/
1323
1324 int
1325 proc_pidoriginatorinfo(int pid, int flavor, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
1326 {
1327 int error = ENOTSUP;
1328 uint32_t size;
1329
1330 switch (flavor) {
1331 case PROC_PIDORIGINATOR_UUID:
1332 size = PROC_PIDORIGINATOR_UUID_SIZE;
1333 break;
1334 case PROC_PIDORIGINATOR_BGSTATE:
1335 size = PROC_PIDORIGINATOR_BGSTATE_SIZE;
1336 break;
1337 case PROC_PIDORIGINATOR_PID_UUID:
1338 size = PROC_PIDORIGINATOR_PID_UUID_SIZE;
1339 break;
1340 default:
1341 return(EINVAL);
1342 }
1343
1344 if (buffersize < size)
1345 return(ENOMEM);
1346
1347 if (pid != 0 && pid != proc_selfpid())
1348 return (EINVAL);
1349
1350 switch (flavor) {
1351 case PROC_PIDORIGINATOR_UUID: {
1352 uuid_t uuid;
1353
1354 error = proc_pidoriginatoruuid(uuid, sizeof(uuid));
1355 if (error != 0)
1356 goto out;
1357
1358 error = copyout(uuid, buffer, size);
1359 if (error == 0)
1360 *retval = size;
1361 }
1362 break;
1363
1364 case PROC_PIDORIGINATOR_PID_UUID: {
1365 struct proc_originatorinfo originator_info;
1366 bzero(&originator_info, sizeof(originator_info));
1367
1368 error = proc_pidoriginatorpid_uuid(originator_info.originator_uuid,
1369 sizeof(uuid_t), &originator_info.originator_pid);
1370 if (error != 0)
1371 goto out;
1372
1373 error = copyout(&originator_info, buffer, size);
1374 if (error == 0)
1375 *retval = size;
1376 }
1377 break;
1378
1379 case PROC_PIDORIGINATOR_BGSTATE: {
1380 uint32_t is_backgrounded;
1381 error = proc_get_originatorbgstate(&is_backgrounded);
1382 if (error)
1383 goto out;
1384
1385 error = copyout(&is_backgrounded, buffer, size);
1386 if (error == 0)
1387 *retval = size;
1388 }
1389 break;
1390
1391 default:
1392 error = ENOTSUP;
1393 }
1394 out:
1395 return error;
1396 }
1397
1398 /***************************** proc_listcoalitions ***************************/
1399 int proc_listcoalitions(int flavor, int type, user_addr_t buffer,
1400 uint32_t buffersize, int32_t *retval)
1401 {
1402 #if CONFIG_COALITIONS
1403 int error = ENOTSUP;
1404 int coal_type;
1405 uint32_t elem_size;
1406 void *coalinfo = NULL;
1407 uint32_t k_buffersize = 0, copyout_sz = 0;
1408 int ncoals = 0, ncoals_ = 0;
1409
1410 /* struct procinfo_coalinfo; */
1411
1412 switch (flavor) {
1413 case LISTCOALITIONS_ALL_COALS:
1414 elem_size = LISTCOALITIONS_ALL_COALS_SIZE;
1415 coal_type = -1;
1416 break;
1417 case LISTCOALITIONS_SINGLE_TYPE:
1418 elem_size = LISTCOALITIONS_SINGLE_TYPE_SIZE;
1419 coal_type = type;
1420 break;
1421 default:
1422 return EINVAL;
1423 }
1424
1425 /* find the total number of coalitions */
1426 ncoals = coalitions_get_list(coal_type, NULL, 0);
1427
1428 if (ncoals == 0 || buffer == 0 || buffersize == 0) {
1429 /*
1430 * user just wants buffer size
1431 * or there are no coalitions
1432 */
1433 error = 0;
1434 *retval = (int)(ncoals * elem_size);
1435 goto out;
1436 }
1437
1438 k_buffersize = ncoals * elem_size;
1439 coalinfo = kalloc((vm_size_t)k_buffersize);
1440 if (!coalinfo) {
1441 error = ENOMEM;
1442 goto out;
1443 }
1444 bzero(coalinfo, k_buffersize);
1445
1446 switch (flavor) {
1447 case LISTCOALITIONS_ALL_COALS:
1448 case LISTCOALITIONS_SINGLE_TYPE:
1449 ncoals_ = coalitions_get_list(coal_type, coalinfo, ncoals);
1450 break;
1451 default:
1452 panic("memory corruption?!");
1453 }
1454
1455 if (ncoals_ == 0) {
1456 /* all the coalitions disappeared... weird but valid */
1457 error = 0;
1458 *retval = 0;
1459 goto out;
1460 }
1461
1462 /*
1463 * Some coalitions may have disappeared between our initial check,
1464 * and the the actual list acquisition.
1465 * Only copy out what we really need.
1466 */
1467 copyout_sz = k_buffersize;
1468 if (ncoals_ < ncoals)
1469 copyout_sz = ncoals_ * elem_size;
1470
1471 /*
1472 * copy the list up to user space
1473 * (we're guaranteed to have a non-null pointer/size here)
1474 */
1475 error = copyout(coalinfo, buffer,
1476 copyout_sz < buffersize ? copyout_sz : buffersize);
1477
1478 if (error == 0)
1479 *retval = (int)copyout_sz;
1480
1481 out:
1482 if (coalinfo)
1483 kfree(coalinfo, k_buffersize);
1484
1485 return error;
1486 #else
1487 /* no coalition support */
1488 (void)flavor;
1489 (void)type;
1490 (void)buffer;
1491 (void)buffersize;
1492 (void)retval;
1493 return ENOTSUP;
1494 #endif
1495 }
1496
1497
1498 /*************************** proc_can_use_forgeound_hw **************************/
1499 int proc_can_use_foreground_hw(int pid, user_addr_t u_reason, uint32_t reasonsize, int32_t *retval)
1500 {
1501 proc_t p = PROC_NULL;
1502 int error = 0;
1503 uint32_t reason = PROC_FGHW_ERROR;
1504 uint32_t isBG = 0;
1505 task_t task = TASK_NULL;
1506 #if CONFIG_COALITIONS
1507 coalition_t coal = COALITION_NULL;
1508 #endif
1509
1510 *retval = 0;
1511
1512 if (pid <= 0) {
1513 error = EINVAL;
1514 reason = PROC_FGHW_ERROR;
1515 goto out;
1516 }
1517
1518 p = proc_find(pid);
1519 if (p == PROC_NULL) {
1520 error = ESRCH;
1521 reason = PROC_FGHW_ERROR;
1522 goto out;
1523 }
1524
1525 #if CONFIG_COALITIONS
1526 if (p != current_proc() &&
1527 !kauth_cred_issuser(kauth_cred_get())) {
1528 error = EPERM;
1529 reason = PROC_FGHW_ERROR;
1530 goto out;
1531 }
1532
1533 task = p->task;
1534 task_reference(task);
1535 if (coalition_is_leader(task, COALITION_TYPE_JETSAM, &coal) == FALSE) {
1536 /* current task is not a coalition leader: find the leader */
1537 task_deallocate(task);
1538 task = coalition_get_leader(coal);
1539 }
1540
1541 if (task != TASK_NULL) {
1542 /*
1543 * If task is non-null, then it is the coalition leader of the
1544 * current process' coalition. This could be the same task as
1545 * the current_task, and that's OK.
1546 */
1547 uint32_t flags = 0;
1548 int role;
1549
1550 proc_get_darwinbgstate(task, &flags);
1551 if ((flags & PROC_FLAG_APPLICATION) != PROC_FLAG_APPLICATION) {
1552 /*
1553 * Coalition leader is not an application, continue
1554 * searching for other ways this task could gain
1555 * access to HW
1556 */
1557 reason = PROC_FGHW_DAEMON_LEADER;
1558 goto no_leader;
1559 }
1560
1561 if (proc_get_effective_task_policy(task, TASK_POLICY_DARWIN_BG)) {
1562 /*
1563 * If the leader of the current process' coalition has
1564 * been marked as DARWIN_BG, then it definitely should
1565 * not be using foreground hardware resources.
1566 */
1567 reason = PROC_FGHW_LEADER_BACKGROUND;
1568 goto out;
1569 }
1570
1571 role = proc_get_effective_task_policy(task, TASK_POLICY_ROLE);
1572 switch (role) {
1573 case TASK_FOREGROUND_APPLICATION: /* DARWIN_ROLE_UI_FOCAL */
1574 case TASK_BACKGROUND_APPLICATION: /* DARWIN_ROLE_UI */
1575 /*
1576 * The leader of this coalition is a focal, UI app:
1577 * access granted
1578 * TODO: should extensions/plugins be allowed to use
1579 * this hardware?
1580 */
1581 *retval = 1;
1582 reason = PROC_FGHW_OK;
1583 goto out;
1584 case TASK_DEFAULT_APPLICATION: /* DARWIN_ROLE_UI_NON_FOCAL */
1585 case TASK_NONUI_APPLICATION: /* DARWIN_ROLE_NON_UI */
1586 case TASK_THROTTLE_APPLICATION:
1587 case TASK_UNSPECIFIED:
1588 default:
1589 /* non-focal, non-ui apps don't get access */
1590 reason = PROC_FGHW_LEADER_NONUI;
1591 goto out;
1592 }
1593 }
1594
1595 no_leader:
1596 if (task != TASK_NULL) {
1597 task_deallocate(task);
1598 task = TASK_NULL;
1599 }
1600 #endif /* CONFIG_COALITIONS */
1601
1602 /*
1603 * There is no reasonable semantic to investigate the currently
1604 * adopted voucher of an arbitrary thread in a non-current process.
1605 * We return '0'
1606 */
1607 if (p != current_proc()) {
1608 error = EINVAL;
1609 goto out;
1610 }
1611
1612 /*
1613 * In the absence of coalitions, fall back to a voucher-based lookup
1614 * where a daemon can used foreground HW if it's operating on behalf
1615 * of a foreground application.
1616 * NOTE: this is equivalent to a call to
1617 * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
1618 */
1619 isBG = 1;
1620 error = proc_get_originatorbgstate(&isBG);
1621 switch (error) {
1622 case 0:
1623 break;
1624 case ESRCH:
1625 reason = PROC_FGHW_NO_ORIGINATOR;
1626 error = 0;
1627 goto out;
1628 case ENOATTR:
1629 reason = PROC_FGHW_NO_VOUCHER_ATTR;
1630 error = 0;
1631 goto out;
1632 case EINVAL:
1633 reason = PROC_FGHW_DAEMON_NO_VOUCHER;
1634 error = 0;
1635 goto out;
1636 default:
1637 /* some other error occurred: report that to the caller */
1638 reason = PROC_FGHW_VOUCHER_ERROR;
1639 goto out;
1640 }
1641
1642 if (isBG) {
1643 reason = PROC_FGHW_ORIGINATOR_BACKGROUND;
1644 error = 0;
1645 } else {
1646 /*
1647 * The process itself is either a foreground app, or has
1648 * adopted a voucher originating from an app that's still in
1649 * the foreground
1650 */
1651 reason = PROC_FGHW_DAEMON_OK;
1652 *retval = 1;
1653 }
1654
1655 out:
1656 if (task != TASK_NULL)
1657 task_deallocate(task);
1658 if (p != PROC_NULL)
1659 proc_rele(p);
1660 if (reasonsize >= sizeof(reason) && u_reason != (user_addr_t)0)
1661 (void)copyout(&reason, u_reason, sizeof(reason));
1662 return error;
1663 }
1664
1665
1666 /********************************** proc_pidinfo ********************************/
1667
1668
1669 int
1670 proc_pidinfo(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
1671 {
1672 struct proc * p = PROC_NULL;
1673 int error = ENOTSUP;
1674 int gotref = 0;
1675 int findzomb = 0;
1676 int shortversion = 0;
1677 uint32_t size;
1678 int zombie = 0;
1679 int thuniqueid = 0;
1680 int uniqidversion = 0;
1681 boolean_t check_same_user;
1682
1683 switch (flavor) {
1684 case PROC_PIDLISTFDS:
1685 size = PROC_PIDLISTFD_SIZE;
1686 if (buffer == USER_ADDR_NULL)
1687 size = 0;
1688 break;
1689 case PROC_PIDTBSDINFO:
1690 size = PROC_PIDTBSDINFO_SIZE;
1691 break;
1692 case PROC_PIDTASKINFO:
1693 size = PROC_PIDTASKINFO_SIZE;
1694 break;
1695 case PROC_PIDTASKALLINFO:
1696 size = PROC_PIDTASKALLINFO_SIZE;
1697 break;
1698 case PROC_PIDTHREADINFO:
1699 size = PROC_PIDTHREADINFO_SIZE;
1700 break;
1701 case PROC_PIDLISTTHREADS:
1702 size = PROC_PIDLISTTHREADS_SIZE;
1703 break;
1704 case PROC_PIDREGIONINFO:
1705 size = PROC_PIDREGIONINFO_SIZE;
1706 break;
1707 case PROC_PIDREGIONPATHINFO:
1708 size = PROC_PIDREGIONPATHINFO_SIZE;
1709 break;
1710 case PROC_PIDVNODEPATHINFO:
1711 size = PROC_PIDVNODEPATHINFO_SIZE;
1712 break;
1713 case PROC_PIDTHREADPATHINFO:
1714 size = PROC_PIDTHREADPATHINFO_SIZE;
1715 break;
1716 case PROC_PIDPATHINFO:
1717 size = MAXPATHLEN;
1718 break;
1719 case PROC_PIDWORKQUEUEINFO:
1720 /* kernel does not have workq info */
1721 if (pid == 0)
1722 return(EINVAL);
1723 else
1724 size = PROC_PIDWORKQUEUEINFO_SIZE;
1725 break;
1726 case PROC_PIDT_SHORTBSDINFO:
1727 size = PROC_PIDT_SHORTBSDINFO_SIZE;
1728 break;
1729 case PROC_PIDLISTFILEPORTS:
1730 size = PROC_PIDLISTFILEPORTS_SIZE;
1731 if (buffer == (user_addr_t)0)
1732 size = 0;
1733 break;
1734 case PROC_PIDTHREADID64INFO:
1735 size = PROC_PIDTHREADID64INFO_SIZE;
1736 break;
1737 case PROC_PIDUNIQIDENTIFIERINFO:
1738 size = PROC_PIDUNIQIDENTIFIERINFO_SIZE;
1739 break;
1740 case PROC_PIDT_BSDINFOWITHUNIQID:
1741 size = PROC_PIDT_BSDINFOWITHUNIQID_SIZE;
1742 break;
1743 case PROC_PIDARCHINFO:
1744 size = PROC_PIDARCHINFO_SIZE;
1745 break;
1746 case PROC_PIDCOALITIONINFO:
1747 size = PROC_PIDCOALITIONINFO_SIZE;
1748 break;
1749 case PROC_PIDNOTEEXIT:
1750 /*
1751 * Set findzomb explicitly because arg passed
1752 * in is used as note exit status bits.
1753 */
1754 size = PROC_PIDNOTEEXIT_SIZE;
1755 findzomb = 1;
1756 break;
1757 case PROC_PIDEXITREASONINFO:
1758 size = PROC_PIDEXITREASONINFO_SIZE;
1759 findzomb = 1;
1760 break;
1761 case PROC_PIDEXITREASONBASICINFO:
1762 size = PROC_PIDEXITREASONBASICINFOSIZE;
1763 findzomb = 1;
1764 break;
1765 case PROC_PIDREGIONPATHINFO2:
1766 size = PROC_PIDREGIONPATHINFO2_SIZE;
1767 break;
1768 case PROC_PIDREGIONPATHINFO3:
1769 size = PROC_PIDREGIONPATHINFO3_SIZE;
1770 break;
1771 case PROC_PIDLISTUPTRS:
1772 size = PROC_PIDLISTUPTRS_SIZE;
1773 if (buffer == USER_ADDR_NULL) {
1774 size = 0;
1775 }
1776 break;
1777 case PROC_PIDLISTDYNKQUEUES:
1778 size = PROC_PIDLISTDYNKQUEUES_SIZE;
1779 if (buffer == USER_ADDR_NULL) {
1780 size = 0;
1781 }
1782 break;
1783 default:
1784 return(EINVAL);
1785 }
1786
1787 if (buffersize < size)
1788 return(ENOMEM);
1789
1790 if ((flavor == PROC_PIDPATHINFO) && (buffersize > PROC_PIDPATHINFO_MAXSIZE)) {
1791 return(EOVERFLOW);
1792 }
1793
1794 /* Check if we need to look for zombies */
1795 if ((flavor == PROC_PIDTBSDINFO) || (flavor == PROC_PIDT_SHORTBSDINFO) || (flavor == PROC_PIDT_BSDINFOWITHUNIQID)
1796 || (flavor == PROC_PIDUNIQIDENTIFIERINFO)) {
1797 if (arg)
1798 findzomb = 1;
1799 }
1800
1801 if ((p = proc_find(pid)) == PROC_NULL) {
1802 if (findzomb)
1803 p = proc_find_zombref(pid);
1804 if (p == PROC_NULL) {
1805 error = ESRCH;
1806 goto out;
1807 }
1808 zombie = 1;
1809 } else {
1810 gotref = 1;
1811 }
1812
1813 /* Certain operations don't require privileges */
1814 switch (flavor) {
1815 case PROC_PIDT_SHORTBSDINFO:
1816 case PROC_PIDUNIQIDENTIFIERINFO:
1817 case PROC_PIDPATHINFO:
1818 case PROC_PIDCOALITIONINFO:
1819 check_same_user = NO_CHECK_SAME_USER;
1820 break;
1821 default:
1822 check_same_user = CHECK_SAME_USER;
1823 break;
1824 }
1825
1826 /* Do we have permission to look into this? */
1827 if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDINFO, flavor, check_same_user)))
1828 goto out;
1829
1830 switch (flavor) {
1831 case PROC_PIDLISTFDS: {
1832 error = proc_pidfdlist(p, buffer, buffersize, retval);
1833 }
1834 break;
1835
1836 case PROC_PIDUNIQIDENTIFIERINFO: {
1837 struct proc_uniqidentifierinfo p_uniqidinfo;
1838 bzero(&p_uniqidinfo, sizeof(p_uniqidinfo));
1839 proc_piduniqidentifierinfo(p, &p_uniqidinfo);
1840 error = copyout(&p_uniqidinfo, buffer, sizeof(struct proc_uniqidentifierinfo));
1841 if (error == 0)
1842 *retval = sizeof(struct proc_uniqidentifierinfo);
1843 }
1844 break;
1845
1846 case PROC_PIDT_SHORTBSDINFO:
1847 shortversion = 1;
1848 case PROC_PIDT_BSDINFOWITHUNIQID:
1849 case PROC_PIDTBSDINFO: {
1850 struct proc_bsdinfo pbsd;
1851 struct proc_bsdshortinfo pbsd_short;
1852 struct proc_bsdinfowithuniqid pbsd_uniqid;
1853
1854 if (flavor == PROC_PIDT_BSDINFOWITHUNIQID)
1855 uniqidversion = 1;
1856
1857 if (shortversion != 0) {
1858 error = proc_pidshortbsdinfo(p, &pbsd_short, zombie);
1859 } else {
1860 error = proc_pidbsdinfo(p, &pbsd, zombie);
1861 if (uniqidversion != 0) {
1862 bzero(&pbsd_uniqid, sizeof(pbsd_uniqid));
1863 proc_piduniqidentifierinfo(p, &pbsd_uniqid.p_uniqidentifier);
1864 pbsd_uniqid.pbsd = pbsd;
1865 }
1866 }
1867
1868 if (error == 0) {
1869 if (shortversion != 0) {
1870 error = copyout(&pbsd_short, buffer, sizeof(struct proc_bsdshortinfo));
1871 if (error == 0)
1872 *retval = sizeof(struct proc_bsdshortinfo);
1873 } else if (uniqidversion != 0) {
1874 error = copyout(&pbsd_uniqid, buffer, sizeof(struct proc_bsdinfowithuniqid));
1875 if (error == 0)
1876 *retval = sizeof(struct proc_bsdinfowithuniqid);
1877 } else {
1878 error = copyout(&pbsd, buffer, sizeof(struct proc_bsdinfo));
1879 if (error == 0)
1880 *retval = sizeof(struct proc_bsdinfo);
1881 }
1882 }
1883 }
1884 break;
1885
1886 case PROC_PIDTASKINFO: {
1887 struct proc_taskinfo ptinfo;
1888
1889 error = proc_pidtaskinfo(p, &ptinfo);
1890 if (error == 0) {
1891 error = copyout(&ptinfo, buffer, sizeof(struct proc_taskinfo));
1892 if (error == 0)
1893 *retval = sizeof(struct proc_taskinfo);
1894 }
1895 }
1896 break;
1897
1898 case PROC_PIDTASKALLINFO: {
1899 struct proc_taskallinfo pall;
1900 bzero(&pall, sizeof(pall));
1901 error = proc_pidbsdinfo(p, &pall.pbsd, 0);
1902 error = proc_pidtaskinfo(p, &pall.ptinfo);
1903 if (error == 0) {
1904 error = copyout(&pall, buffer, sizeof(struct proc_taskallinfo));
1905 if (error == 0)
1906 *retval = sizeof(struct proc_taskallinfo);
1907 }
1908 }
1909 break;
1910
1911 case PROC_PIDTHREADID64INFO:
1912 thuniqueid = 1;
1913 case PROC_PIDTHREADINFO:{
1914 struct proc_threadinfo pthinfo;
1915
1916 error = proc_pidthreadinfo(p, arg, thuniqueid, &pthinfo);
1917 if (error == 0) {
1918 error = copyout(&pthinfo, buffer, sizeof(struct proc_threadinfo));
1919 if (error == 0)
1920 *retval = sizeof(struct proc_threadinfo);
1921 }
1922 }
1923 break;
1924
1925 case PROC_PIDLISTTHREADS:{
1926 error = proc_pidlistthreads(p, buffer, buffersize, retval);
1927 }
1928 break;
1929
1930 case PROC_PIDREGIONINFO:{
1931 error = proc_pidregioninfo(p, arg, buffer, buffersize, retval);
1932 }
1933 break;
1934
1935
1936 case PROC_PIDREGIONPATHINFO:{
1937 error = proc_pidregionpathinfo(p, arg, buffer, buffersize, retval);
1938 }
1939 break;
1940
1941 case PROC_PIDREGIONPATHINFO2:{
1942 error = proc_pidregionpathinfo2(p, arg, buffer, buffersize, retval);
1943 }
1944 break;
1945
1946 case PROC_PIDREGIONPATHINFO3:{
1947 error = proc_pidregionpathinfo3(p, arg, buffer, buffersize, retval);
1948 }
1949 break;
1950
1951 case PROC_PIDVNODEPATHINFO:{
1952 error = proc_pidvnodepathinfo(p, arg, buffer, buffersize, retval);
1953 }
1954 break;
1955
1956
1957 case PROC_PIDTHREADPATHINFO:{
1958 struct proc_threadwithpathinfo pinfo;
1959
1960 error = proc_pidthreadpathinfo(p, arg, &pinfo);
1961 if (error == 0) {
1962 error = copyout((caddr_t)&pinfo, buffer, sizeof(struct proc_threadwithpathinfo));
1963 if (error == 0)
1964 *retval = sizeof(struct proc_threadwithpathinfo);
1965 }
1966 }
1967 break;
1968
1969 case PROC_PIDPATHINFO: {
1970 error = proc_pidpathinfo(p, arg, buffer, buffersize, retval);
1971 }
1972 break;
1973
1974
1975 case PROC_PIDWORKQUEUEINFO:{
1976 struct proc_workqueueinfo pwqinfo;
1977
1978 error = proc_pidworkqueueinfo(p, &pwqinfo);
1979 if (error == 0) {
1980 error = copyout(&pwqinfo, buffer, sizeof(struct proc_workqueueinfo));
1981 if (error == 0)
1982 *retval = sizeof(struct proc_workqueueinfo);
1983 }
1984 }
1985 break;
1986
1987 case PROC_PIDLISTFILEPORTS: {
1988 error = proc_pidfileportlist(p, buffer, buffersize, retval);
1989 }
1990 break;
1991
1992 case PROC_PIDARCHINFO: {
1993 struct proc_archinfo pai;
1994 bzero(&pai, sizeof(pai));
1995 proc_archinfo(p, &pai);
1996 error = copyout(&pai, buffer, sizeof(struct proc_archinfo));
1997 if (error == 0) {
1998 *retval = sizeof(struct proc_archinfo);
1999 }
2000 }
2001 break;
2002
2003 case PROC_PIDCOALITIONINFO: {
2004 struct proc_pidcoalitioninfo pci;
2005 proc_pidcoalitioninfo(p, &pci);
2006 error = copyout(&pci, buffer, sizeof(struct proc_pidcoalitioninfo));
2007 if (error == 0) {
2008 *retval = sizeof(struct proc_pidcoalitioninfo);
2009 }
2010 }
2011 break;
2012
2013 case PROC_PIDNOTEEXIT: {
2014 uint32_t data;
2015 error = proc_pidnoteexit(p, arg, &data);
2016 if (error == 0) {
2017 error = copyout(&data, buffer, sizeof(data));
2018 if (error == 0) {
2019 *retval = sizeof(data);
2020 }
2021 }
2022 }
2023 break;
2024
2025 case PROC_PIDEXITREASONINFO: {
2026 struct proc_exitreasoninfo eri;
2027
2028 error = copyin(buffer, &eri, sizeof(eri));
2029 if (error != 0) {
2030 break;
2031 }
2032
2033 error = proc_pidexitreasoninfo(p, &eri, NULL);
2034 if (error == 0) {
2035 error = copyout(&eri, buffer, sizeof(eri));
2036 if (error == 0) {
2037 *retval = sizeof(eri);
2038 }
2039 }
2040 }
2041 break;
2042
2043 case PROC_PIDEXITREASONBASICINFO: {
2044 struct proc_exitreasonbasicinfo beri;
2045
2046 bzero(&beri, sizeof(struct proc_exitreasonbasicinfo));
2047
2048 error = proc_pidexitreasoninfo(p, NULL, &beri);
2049 if (error == 0) {
2050 error = copyout(&beri, buffer, sizeof(beri));
2051 if (error == 0) {
2052 *retval = sizeof(beri);
2053 }
2054 }
2055 }
2056 break;
2057
2058 case PROC_PIDLISTUPTRS:
2059 error = proc_pidlistuptrs(p, buffer, buffersize, retval);
2060 break;
2061
2062 case PROC_PIDLISTDYNKQUEUES:
2063 error = kevent_copyout_proc_dynkqids(p, buffer, buffersize, retval);
2064 break;
2065
2066 default:
2067 error = ENOTSUP;
2068 break;
2069 }
2070
2071 out:
2072 if (gotref)
2073 proc_rele(p);
2074 else if (zombie)
2075 proc_drop_zombref(p);
2076 return(error);
2077 }
2078
2079
2080 int
2081 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)
2082 {
2083 struct vnode_fdinfo vfi;
2084 int error= 0;
2085
2086 if ((error = vnode_getwithvid(vp, vid)) != 0) {
2087 return(error);
2088 }
2089 bzero(&vfi, sizeof(struct vnode_fdinfo));
2090 fill_fileinfo(fp, proc, fd, &vfi.pfi);
2091 error = fill_vnodeinfo(vp, &vfi.pvi);
2092 vnode_put(vp);
2093 if (error == 0) {
2094 error = copyout((caddr_t)&vfi, buffer, sizeof(struct vnode_fdinfo));
2095 if (error == 0)
2096 *retval = sizeof(struct vnode_fdinfo);
2097 }
2098 return(error);
2099 }
2100
2101 int
2102 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)
2103 {
2104 struct vnode_fdinfowithpath vfip;
2105 int count, error= 0;
2106
2107 if ((error = vnode_getwithvid(vp, vid)) != 0) {
2108 return(error);
2109 }
2110 bzero(&vfip, sizeof(struct vnode_fdinfowithpath));
2111 fill_fileinfo(fp, proc, fd, &vfip.pfi);
2112 error = fill_vnodeinfo(vp, &vfip.pvip.vip_vi) ;
2113 if (error == 0) {
2114 count = MAXPATHLEN;
2115 vn_getpath(vp, &vfip.pvip.vip_path[0], &count);
2116 vfip.pvip.vip_path[MAXPATHLEN-1] = 0;
2117 vnode_put(vp);
2118 error = copyout((caddr_t)&vfip, buffer, sizeof(struct vnode_fdinfowithpath));
2119 if (error == 0)
2120 *retval = sizeof(struct vnode_fdinfowithpath);
2121 } else
2122 vnode_put(vp);
2123 return(error);
2124 }
2125
2126 void
2127 fill_fileinfo(struct fileproc * fp, proc_t proc, int fd, struct proc_fileinfo * fproc)
2128 {
2129 fproc->fi_openflags = fp->f_fglob->fg_flag;
2130 fproc->fi_status = 0;
2131 fproc->fi_offset = fp->f_fglob->fg_offset;
2132 fproc->fi_type = FILEGLOB_DTYPE(fp->f_fglob);
2133 if (fp->f_fglob->fg_count > 1)
2134 fproc->fi_status |= PROC_FP_SHARED;
2135 if (proc != PROC_NULL) {
2136 if ((FDFLAGS_GET(proc, fd) & UF_EXCLOSE) != 0)
2137 fproc->fi_status |= PROC_FP_CLEXEC;
2138 if ((FDFLAGS_GET(proc, fd) & UF_FORKCLOSE) != 0)
2139 fproc->fi_status |= PROC_FP_CLFORK;
2140 }
2141 if (FILEPROC_TYPE(fp) == FTYPE_GUARDED) {
2142 fproc->fi_status |= PROC_FP_GUARDED;
2143 fproc->fi_guardflags = 0;
2144 if (fp_isguarded(fp, GUARD_CLOSE))
2145 fproc->fi_guardflags |= PROC_FI_GUARD_CLOSE;
2146 if (fp_isguarded(fp, GUARD_DUP))
2147 fproc->fi_guardflags |= PROC_FI_GUARD_DUP;
2148 if (fp_isguarded(fp, GUARD_SOCKET_IPC))
2149 fproc->fi_guardflags |= PROC_FI_GUARD_SOCKET_IPC;
2150 if (fp_isguarded(fp, GUARD_FILEPORT))
2151 fproc->fi_guardflags |= PROC_FI_GUARD_FILEPORT;
2152 }
2153 }
2154
2155
2156
2157 int
2158 fill_vnodeinfo(vnode_t vp, struct vnode_info *vinfo)
2159 {
2160 vfs_context_t context;
2161 struct stat64 sb;
2162 int error = 0;
2163
2164 bzero(&sb, sizeof(struct stat64));
2165 context = vfs_context_create((vfs_context_t)0);
2166 error = vn_stat(vp, &sb, NULL, 1, context);
2167 (void)vfs_context_rele(context);
2168
2169 munge_vinfo_stat(&sb, &vinfo->vi_stat);
2170
2171 if (error != 0)
2172 goto out;
2173
2174 if (vp->v_mount != dead_mountp) {
2175 vinfo->vi_fsid = vp->v_mount->mnt_vfsstat.f_fsid;
2176 } else {
2177 vinfo->vi_fsid.val[0] = 0;
2178 vinfo->vi_fsid.val[1] = 0;
2179 }
2180 vinfo->vi_type = vp->v_type;
2181 out:
2182 return(error);
2183 }
2184
2185 int
2186 pid_socketinfo(socket_t so, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2187 {
2188 #if SOCKETS
2189 struct socket_fdinfo s;
2190 int error = 0;
2191
2192 bzero(&s, sizeof(struct socket_fdinfo));
2193 fill_fileinfo(fp, proc, fd, &s.pfi);
2194 if ((error = fill_socketinfo(so, &s.psi)) == 0) {
2195 if ((error = copyout(&s, buffer, sizeof(struct socket_fdinfo))) == 0)
2196 *retval = sizeof(struct socket_fdinfo);
2197 }
2198 return (error);
2199 #else
2200 #pragma unused(so, fp, proc, fd, buffer)
2201 *retval = 0;
2202 return (ENOTSUP);
2203 #endif
2204 }
2205
2206 int
2207 pid_pseminfo(struct psemnode *psem, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2208 {
2209 struct psem_fdinfo pseminfo;
2210 int error = 0;
2211
2212 bzero(&pseminfo, sizeof(struct psem_fdinfo));
2213 fill_fileinfo(fp, proc, fd, &pseminfo.pfi);
2214
2215 if ((error = fill_pseminfo(psem, &pseminfo.pseminfo)) == 0) {
2216 if ((error = copyout(&pseminfo, buffer, sizeof(struct psem_fdinfo))) == 0)
2217 *retval = sizeof(struct psem_fdinfo);
2218 }
2219
2220 return(error);
2221 }
2222
2223 int
2224 pid_pshminfo(struct pshmnode *pshm, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2225 {
2226 struct pshm_fdinfo pshminfo;
2227 int error = 0;
2228
2229 bzero(&pshminfo, sizeof(struct pshm_fdinfo));
2230 fill_fileinfo(fp, proc, fd, &pshminfo.pfi);
2231
2232 if ((error = fill_pshminfo(pshm, &pshminfo.pshminfo)) == 0) {
2233 if ((error = copyout(&pshminfo, buffer, sizeof(struct pshm_fdinfo))) == 0)
2234 *retval = sizeof(struct pshm_fdinfo);
2235 }
2236
2237 return(error);
2238 }
2239
2240 int
2241 pid_pipeinfo(struct pipe * p, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2242 {
2243 struct pipe_fdinfo pipeinfo;
2244 int error = 0;
2245
2246 bzero(&pipeinfo, sizeof(struct pipe_fdinfo));
2247 fill_fileinfo(fp, proc, fd, &pipeinfo.pfi);
2248 if ((error = fill_pipeinfo(p, &pipeinfo.pipeinfo)) == 0) {
2249 if ((error = copyout(&pipeinfo, buffer, sizeof(struct pipe_fdinfo))) == 0)
2250 *retval = sizeof(struct pipe_fdinfo);
2251 }
2252
2253 return(error);
2254 }
2255
2256 int
2257 pid_kqueueinfo(struct kqueue * kq, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2258 {
2259 struct kqueue_fdinfo kqinfo;
2260 int error = 0;
2261
2262 bzero(&kqinfo, sizeof(struct kqueue_fdinfo));
2263
2264 /* not all kq's are associated with a file (e.g. workqkq) */
2265 if (fp) {
2266 assert(fd >= 0);
2267 fill_fileinfo(fp, proc, fd, &kqinfo.pfi);
2268 }
2269
2270 if ((error = fill_kqueueinfo(kq, &kqinfo.kqueueinfo)) == 0) {
2271 if ((error = copyout(&kqinfo, buffer, sizeof(struct kqueue_fdinfo))) == 0)
2272 *retval = sizeof(struct kqueue_fdinfo);
2273 }
2274
2275 return(error);
2276 }
2277
2278 int
2279 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)
2280 {
2281 return ENOTSUP;
2282 }
2283
2284
2285 /************************** proc_pidfdinfo routine ***************************/
2286 int
2287 proc_pidfdinfo(int pid, int flavor, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
2288 {
2289 proc_t p;
2290 int error = ENOTSUP;
2291 struct fileproc * fp = NULL;
2292 uint32_t size;
2293
2294 switch (flavor) {
2295 case PROC_PIDFDVNODEINFO:
2296 size = PROC_PIDFDVNODEINFO_SIZE;
2297 break;
2298 case PROC_PIDFDVNODEPATHINFO:
2299 size = PROC_PIDFDVNODEPATHINFO_SIZE;
2300 break;
2301 case PROC_PIDFDSOCKETINFO:
2302 size = PROC_PIDFDSOCKETINFO_SIZE;
2303 break;
2304 case PROC_PIDFDPSEMINFO:
2305 size = PROC_PIDFDPSEMINFO_SIZE;
2306 break;
2307 case PROC_PIDFDPSHMINFO:
2308 size = PROC_PIDFDPSHMINFO_SIZE;
2309 break;
2310 case PROC_PIDFDPIPEINFO:
2311 size = PROC_PIDFDPIPEINFO_SIZE;
2312 break;
2313 case PROC_PIDFDKQUEUEINFO:
2314 size = PROC_PIDFDKQUEUEINFO_SIZE;
2315 break;
2316 case PROC_PIDFDKQUEUE_EXTINFO:
2317 size = PROC_PIDFDKQUEUE_EXTINFO_SIZE;
2318 if (buffer == (user_addr_t)0)
2319 size = 0;
2320 break;
2321 case PROC_PIDFDATALKINFO:
2322 size = PROC_PIDFDATALKINFO_SIZE;
2323 break;
2324
2325 default:
2326 return(EINVAL);
2327
2328 }
2329
2330 if (buffersize < size)
2331 return(ENOMEM);
2332
2333 if ((p = proc_find(pid)) == PROC_NULL) {
2334 error = ESRCH;
2335 goto out;
2336 }
2337
2338 /* Do we have permission to look into this? */
2339 if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDFDINFO, flavor, CHECK_SAME_USER)))
2340 goto out1;
2341
2342 switch (flavor) {
2343 case PROC_PIDFDVNODEINFO: {
2344 vnode_t vp;
2345 uint32_t vid=0;
2346
2347 if ((error = fp_getfvpandvid(p, fd, &fp, &vp, &vid)) !=0) {
2348 goto out1;
2349 }
2350 /* no need to be under the fdlock */
2351 error = pid_vnodeinfo(vp, vid, fp, p, fd, buffer, buffersize, retval);
2352 }
2353 break;
2354
2355 case PROC_PIDFDVNODEPATHINFO: {
2356 vnode_t vp;
2357 uint32_t vid=0;
2358
2359 if ((error = fp_getfvpandvid(p, fd, &fp, &vp, &vid)) !=0) {
2360 goto out1;
2361 }
2362
2363 /* no need to be under the fdlock */
2364 error = pid_vnodeinfopath(vp, vid, fp, p, fd, buffer, buffersize, retval);
2365 }
2366 break;
2367
2368 case PROC_PIDFDSOCKETINFO: {
2369 socket_t so;
2370
2371 if ((error = fp_getfsock(p, fd, &fp, &so)) !=0) {
2372 goto out1;
2373 }
2374 /* no need to be under the fdlock */
2375 error = pid_socketinfo(so, fp, p, fd, buffer, buffersize, retval);
2376 }
2377 break;
2378
2379 case PROC_PIDFDPSEMINFO: {
2380 struct psemnode * psem;
2381
2382 if ((error = fp_getfpsem(p, fd, &fp, &psem)) !=0) {
2383 goto out1;
2384 }
2385 /* no need to be under the fdlock */
2386 error = pid_pseminfo(psem, fp, p, fd, buffer, buffersize, retval);
2387 }
2388 break;
2389
2390 case PROC_PIDFDPSHMINFO: {
2391 struct pshmnode * pshm;
2392
2393 if ((error = fp_getfpshm(p, fd, &fp, &pshm)) !=0) {
2394 goto out1;
2395 }
2396 /* no need to be under the fdlock */
2397 error = pid_pshminfo(pshm, fp, p, fd, buffer, buffersize, retval);
2398 }
2399 break;
2400
2401 case PROC_PIDFDPIPEINFO: {
2402 struct pipe * cpipe;
2403
2404 if ((error = fp_getfpipe(p, fd, &fp, &cpipe)) !=0) {
2405 goto out1;
2406 }
2407 /* no need to be under the fdlock */
2408 error = pid_pipeinfo(cpipe, fp, p, fd, buffer, buffersize, retval);
2409 }
2410 break;
2411
2412 case PROC_PIDFDKQUEUEINFO: {
2413 struct kqueue * kq;
2414
2415 if (fd == -1) {
2416 if ((kq = p->p_fd->fd_wqkqueue) == NULL) {
2417 /* wqkqueue is initialized on-demand */
2418 error = 0;
2419 break;
2420 }
2421 } else if ((error = fp_getfkq(p, fd, &fp, &kq)) != 0) {
2422 goto out1;
2423 }
2424
2425 /* no need to be under the fdlock */
2426 error = pid_kqueueinfo(kq, fp, p, fd, buffer, buffersize, retval);
2427 }
2428 break;
2429
2430 case PROC_PIDFDKQUEUE_EXTINFO: {
2431 struct kqueue * kq;
2432
2433 if (fd == -1) {
2434 if ((kq = p->p_fd->fd_wqkqueue) == NULL) {
2435 /* wqkqueue is initialized on-demand */
2436 error = 0;
2437 break;
2438 }
2439 } else if ((error = fp_getfkq(p, fd, &fp, &kq)) != 0) {
2440 goto out1;
2441 }
2442 error = pid_kqueue_extinfo(p, kq, buffer, buffersize, retval);
2443 }
2444 break;
2445
2446 default: {
2447 error = EINVAL;
2448 goto out1;
2449 }
2450 }
2451
2452 if (fp) {
2453 fp_drop(p, fd, fp , 0);
2454 }
2455 out1 :
2456 proc_rele(p);
2457 out:
2458 return(error);
2459 }
2460
2461 #define MAX_UPTRS 16392
2462
2463 int
2464 proc_pidlistuptrs(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval)
2465 {
2466 uint32_t count = 0;
2467 int error = 0;
2468 void *kbuf = NULL;
2469 int32_t nuptrs = 0;
2470
2471 if (buffer != USER_ADDR_NULL) {
2472 count = buffersize / sizeof(uint64_t);
2473 if (count > MAX_UPTRS) {
2474 count = MAX_UPTRS;
2475 }
2476 if (count > 0) {
2477 buffersize = count * sizeof(uint64_t);
2478 kbuf = kalloc(buffersize);
2479 bzero(kbuf, buffersize);
2480 assert(kbuf != NULL);
2481 } else {
2482 buffersize = 0;
2483 }
2484 } else {
2485 buffersize = 0;
2486 }
2487
2488 nuptrs = kevent_proc_copy_uptrs(p, kbuf, buffersize);
2489
2490 if (kbuf) {
2491 size_t copysize;
2492 if (os_mul_overflow(nuptrs, sizeof(uint64_t), &copysize)) {
2493 error = ERANGE;
2494 goto out;
2495 }
2496 if (copysize > buffersize) {
2497 copysize = buffersize;
2498 }
2499 error = copyout(kbuf, buffer, copysize);
2500 }
2501
2502 out:
2503 *retval = nuptrs;
2504
2505 if (kbuf) {
2506 kfree(kbuf, buffersize);
2507 kbuf = NULL;
2508 }
2509
2510 return error;
2511 }
2512
2513 /*
2514 * Helper function for proc_pidfileportinfo
2515 */
2516
2517 struct fileport_info_args {
2518 int fia_flavor;
2519 user_addr_t fia_buffer;
2520 uint32_t fia_buffersize;
2521 int32_t *fia_retval;
2522 };
2523
2524 static kern_return_t
2525 proc_fileport_info(__unused mach_port_name_t name,
2526 struct fileglob *fg, void *arg)
2527 {
2528 struct fileport_info_args *fia = arg;
2529 struct fileproc __fileproc, *fp = &__fileproc;
2530 int error;
2531
2532 bzero(fp, sizeof (*fp));
2533 fp->f_fglob = fg;
2534
2535 switch (fia->fia_flavor) {
2536 case PROC_PIDFILEPORTVNODEPATHINFO: {
2537 vnode_t vp;
2538
2539 if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
2540 error = ENOTSUP;
2541 break;
2542 }
2543 vp = (struct vnode *)fg->fg_data;
2544 error = pid_vnodeinfopath(vp, vnode_vid(vp), fp, PROC_NULL, 0,
2545 fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
2546 } break;
2547
2548 case PROC_PIDFILEPORTSOCKETINFO: {
2549 socket_t so;
2550
2551 if (FILEGLOB_DTYPE(fg) != DTYPE_SOCKET) {
2552 error = EOPNOTSUPP;
2553 break;
2554 }
2555 so = (socket_t)fg->fg_data;
2556 error = pid_socketinfo(so, fp, PROC_NULL, 0,
2557 fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
2558 } break;
2559
2560 case PROC_PIDFILEPORTPSHMINFO: {
2561 struct pshmnode *pshm;
2562
2563 if (FILEGLOB_DTYPE(fg) != DTYPE_PSXSHM) {
2564 error = EBADF; /* ick - mirror fp_getfpshm */
2565 break;
2566 }
2567 pshm = (struct pshmnode *)fg->fg_data;
2568 error = pid_pshminfo(pshm, fp, PROC_NULL, 0,
2569 fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
2570 } break;
2571
2572 case PROC_PIDFILEPORTPIPEINFO: {
2573 struct pipe *cpipe;
2574
2575 if (FILEGLOB_DTYPE(fg) != DTYPE_PIPE) {
2576 error = EBADF; /* ick - mirror fp_getfpipe */
2577 break;
2578 }
2579 cpipe = (struct pipe *)fg->fg_data;
2580 error = pid_pipeinfo(cpipe, fp, PROC_NULL, 0,
2581 fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
2582 } break;
2583
2584 default:
2585 error = EINVAL;
2586 break;
2587 }
2588
2589 return (error);
2590 }
2591
2592 /************************* proc_pidfileportinfo routine *********************/
2593 int
2594 proc_pidfileportinfo(int pid, int flavor, mach_port_name_t name,
2595 user_addr_t buffer, uint32_t buffersize, int32_t *retval)
2596 {
2597 proc_t p;
2598 int error = ENOTSUP;
2599 uint32_t size;
2600 struct fileport_info_args fia;
2601
2602 /* fileport types are restricted by file_issendable() */
2603
2604 switch (flavor) {
2605 case PROC_PIDFILEPORTVNODEPATHINFO:
2606 size = PROC_PIDFILEPORTVNODEPATHINFO_SIZE;
2607 break;
2608 case PROC_PIDFILEPORTSOCKETINFO:
2609 size = PROC_PIDFILEPORTSOCKETINFO_SIZE;
2610 break;
2611 case PROC_PIDFILEPORTPSHMINFO:
2612 size = PROC_PIDFILEPORTPSHMINFO_SIZE;
2613 break;
2614 case PROC_PIDFILEPORTPIPEINFO:
2615 size = PROC_PIDFILEPORTPIPEINFO_SIZE;
2616 break;
2617 default:
2618 return (EINVAL);
2619 }
2620
2621 if (buffersize < size)
2622 return (ENOMEM);
2623 if ((p = proc_find(pid)) == PROC_NULL) {
2624 error = ESRCH;
2625 goto out;
2626 }
2627
2628 /* Do we have permission to look into this? */
2629 if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDFILEPORTINFO, flavor, CHECK_SAME_USER)))
2630 goto out1;
2631
2632 fia.fia_flavor = flavor;
2633 fia.fia_buffer = buffer;
2634 fia.fia_buffersize = buffersize;
2635 fia.fia_retval = retval;
2636
2637 if (fileport_invoke(p->task, name,
2638 proc_fileport_info, &fia, &error) != KERN_SUCCESS)
2639 error = EINVAL;
2640 out1:
2641 proc_rele(p);
2642 out:
2643 return (error);
2644 }
2645
2646 int
2647 proc_security_policy(proc_t targetp, __unused int callnum, __unused int flavor, boolean_t check_same_user)
2648 {
2649 #if CONFIG_MACF
2650 int error = 0;
2651
2652 if ((error = mac_proc_check_proc_info(current_proc(), targetp, callnum, flavor)))
2653 return (error);
2654 #endif
2655
2656 /* The 'listpids' call doesn't have a target proc */
2657 if (targetp == PROC_NULL) {
2658 assert(callnum == PROC_INFO_CALL_LISTPIDS && check_same_user == NO_CHECK_SAME_USER);
2659 return (0);
2660 }
2661
2662 /*
2663 * Check for 'get information for processes owned by other users' privilege
2664 * root has this privilege by default
2665 */
2666 if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO, 0) == 0)
2667 check_same_user = FALSE;
2668
2669 if (check_same_user) {
2670 kauth_cred_t target_cred;
2671 uid_t target_uid;
2672
2673 target_cred = kauth_cred_proc_ref(targetp);
2674 target_uid = kauth_cred_getuid(target_cred);
2675 kauth_cred_unref(&target_cred);
2676
2677 if (kauth_getuid() != target_uid)
2678 return(EPERM);
2679 }
2680
2681 return(0);
2682 }
2683
2684 int
2685 proc_kernmsgbuf(user_addr_t buffer, uint32_t buffersize, int32_t * retval)
2686 {
2687 if (suser(kauth_cred_get(), (u_short *)0) == 0) {
2688 return(log_dmesg(buffer, buffersize, retval));
2689 } else
2690 return(EPERM);
2691 }
2692
2693 /* ********* process control sets on self only */
2694 int
2695 proc_setcontrol(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, __unused int32_t * retval)
2696 {
2697 struct proc * pself = PROC_NULL;
2698 int error = 0;
2699 uint32_t pcontrol = (uint32_t)arg;
2700 struct uthread *ut = NULL;
2701 char name_buf[MAXTHREADNAMESIZE];
2702
2703 pself = current_proc();
2704 if (pid != pself->p_pid)
2705 return(EINVAL);
2706
2707 /* Do we have permission to look into this? */
2708 if ((error = proc_security_policy(pself, PROC_INFO_CALL_SETCONTROL, flavor, NO_CHECK_SAME_USER)))
2709 goto out;
2710
2711 switch (flavor) {
2712 case PROC_SELFSET_PCONTROL: {
2713 if (pcontrol > P_PCMAX)
2714 return(EINVAL);
2715 proc_lock(pself);
2716 /* reset existing control setting while retaining action state */
2717 pself->p_pcaction &= PROC_ACTION_MASK;
2718 /* set new control state */
2719 pself->p_pcaction |= pcontrol;
2720 proc_unlock(pself);
2721 }
2722 break;
2723
2724 case PROC_SELFSET_THREADNAME: {
2725 /*
2726 * This is a bit ugly, as it copies the name into the kernel, and then
2727 * invokes bsd_setthreadname again to copy it into the uthread name
2728 * buffer. Hopefully this isn't such a hot codepath that an additional
2729 * MAXTHREADNAMESIZE copy is a big issue.
2730 */
2731 if (buffersize > (MAXTHREADNAMESIZE - 1)) {
2732 return ENAMETOOLONG;
2733 }
2734
2735 ut = current_uthread();
2736
2737 bzero(name_buf, MAXTHREADNAMESIZE);
2738 error = copyin(buffer, name_buf, buffersize);
2739
2740 if (!error) {
2741 bsd_setthreadname(ut, name_buf);
2742 }
2743 }
2744 break;
2745
2746 case PROC_SELFSET_VMRSRCOWNER: {
2747 /* need to to be superuser */
2748 if (suser(kauth_cred_get(), (u_short *)0) != 0) {
2749 error = EPERM;
2750 goto out;
2751 }
2752
2753 proc_lock(pself);
2754 /* reset existing control setting while retaining action state */
2755 pself->p_lflag |= P_LVMRSRCOWNER;
2756 proc_unlock(pself);
2757 }
2758 break;
2759
2760 case PROC_SELFSET_DELAYIDLESLEEP: {
2761 /* mark or clear the process property to delay idle sleep disk IO */
2762 if (pcontrol != 0)
2763 OSBitOrAtomic(P_DELAYIDLESLEEP, &pself->p_flag);
2764 else
2765 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP), &pself->p_flag);
2766 }
2767 break;
2768
2769 default:
2770 error = ENOTSUP;
2771 }
2772
2773 out:
2774 return(error);
2775 }
2776
2777 #if CONFIG_MEMORYSTATUS
2778
2779 int
2780 proc_dirtycontrol(int pid, int flavor, uint64_t arg, int32_t *retval) {
2781 struct proc *target_p;
2782 int error = 0;
2783 uint32_t pcontrol = (uint32_t)arg;
2784 kauth_cred_t my_cred, target_cred;
2785 boolean_t self = FALSE;
2786 boolean_t child = FALSE;
2787 boolean_t zombref = FALSE;
2788 pid_t selfpid;
2789
2790 target_p = proc_find(pid);
2791
2792 if (target_p == PROC_NULL) {
2793 if (flavor == PROC_DIRTYCONTROL_GET) {
2794 target_p = proc_find_zombref(pid);
2795 zombref = 1;
2796 }
2797
2798 if (target_p == PROC_NULL)
2799 return(ESRCH);
2800
2801 }
2802
2803 my_cred = kauth_cred_get();
2804 target_cred = kauth_cred_proc_ref(target_p);
2805
2806 /* Do we have permission to look into this? */
2807 if ((error = proc_security_policy(target_p, PROC_INFO_CALL_DIRTYCONTROL, flavor, NO_CHECK_SAME_USER)))
2808 goto out;
2809
2810 selfpid = proc_selfpid();
2811 if (pid == selfpid) {
2812 self = TRUE;
2813 } else if (target_p->p_ppid == selfpid) {
2814 child = TRUE;
2815 }
2816
2817 switch (flavor) {
2818 case PROC_DIRTYCONTROL_TRACK: {
2819 /* Only allow the process itself, its parent, or root */
2820 if ((self == FALSE) && (child == FALSE) && kauth_cred_issuser(kauth_cred_get()) != TRUE) {
2821 error = EPERM;
2822 goto out;
2823 }
2824
2825 error = memorystatus_dirty_track(target_p, pcontrol);
2826 }
2827 break;
2828
2829 case PROC_DIRTYCONTROL_SET: {
2830 /* Check privileges; use cansignal() here since the process could be terminated */
2831 if (!cansignal(current_proc(), my_cred, target_p, SIGKILL, 0)) {
2832 error = EPERM;
2833 goto out;
2834 }
2835
2836 error = memorystatus_dirty_set(target_p, self, pcontrol);
2837 }
2838 break;
2839
2840 case PROC_DIRTYCONTROL_GET: {
2841 /* No permissions check - dirty state is freely available */
2842 if (retval) {
2843 *retval = memorystatus_dirty_get(target_p);
2844 } else {
2845 error = EINVAL;
2846 }
2847 }
2848 break;
2849
2850 case PROC_DIRTYCONTROL_CLEAR: {
2851 /* Check privileges; use cansignal() here since the process could be terminated */
2852 if (!cansignal(current_proc(), my_cred, target_p, SIGKILL, 0)) {
2853 error = EPERM;
2854 goto out;
2855 }
2856
2857 error = memorystatus_dirty_clear(target_p, pcontrol);
2858 }
2859 break;
2860 }
2861
2862 out:
2863 if (zombref)
2864 proc_drop_zombref(target_p);
2865 else
2866 proc_rele(target_p);
2867
2868 kauth_cred_unref(&target_cred);
2869
2870 return(error);
2871 }
2872 #else
2873
2874 int
2875 proc_dirtycontrol(__unused int pid, __unused int flavor, __unused uint64_t arg, __unused int32_t *retval) {
2876 return ENOTSUP;
2877 }
2878
2879 #endif /* CONFIG_MEMORYSTATUS */
2880
2881 /*
2882 * proc_terminate() provides support for sudden termination.
2883 * SIGKILL is issued to tracked, clean processes; otherwise,
2884 * SIGTERM is sent.
2885 */
2886
2887 int
2888 proc_terminate(int pid, int32_t *retval)
2889 {
2890 int error = 0;
2891 proc_t p;
2892 kauth_cred_t uc = kauth_cred_get();
2893 int sig;
2894
2895 #if 0
2896 /* XXX: Check if these are necessary */
2897 AUDIT_ARG(pid, pid);
2898 AUDIT_ARG(signum, sig);
2899 #endif
2900
2901 if (pid <= 0 || retval == NULL) {
2902 return (EINVAL);
2903 }
2904
2905 if ((p = proc_find(pid)) == NULL) {
2906 return (ESRCH);
2907 }
2908
2909 #if 0
2910 /* XXX: Check if these are necessary */
2911 AUDIT_ARG(process, p);
2912 #endif
2913
2914 /* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
2915 if (!cansignal(current_proc(), uc, p, SIGKILL, 0)) {
2916 error = EPERM;
2917 goto out;
2918 }
2919
2920 /* Not allowed to sudden terminate yourself */
2921 if (p == current_proc()) {
2922 error = EPERM;
2923 goto out;
2924 }
2925
2926 #if CONFIG_MEMORYSTATUS
2927 /* Determine requisite signal to issue */
2928 sig = memorystatus_on_terminate(p);
2929 #else
2930 sig = SIGTERM;
2931 #endif
2932
2933 proc_set_task_policy(p->task, TASK_POLICY_ATTRIBUTE,
2934 TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
2935
2936 psignal(p, sig);
2937 *retval = sig;
2938
2939 out:
2940 proc_rele(p);
2941
2942 return error;
2943 }
2944
2945 /*
2946 * copy stat64 structure into vinfo_stat structure.
2947 */
2948 static void
2949 munge_vinfo_stat(struct stat64 *sbp, struct vinfo_stat *vsbp)
2950 {
2951 bzero(vsbp, sizeof(struct vinfo_stat));
2952
2953 vsbp->vst_dev = sbp->st_dev;
2954 vsbp->vst_mode = sbp->st_mode;
2955 vsbp->vst_nlink = sbp->st_nlink;
2956 vsbp->vst_ino = sbp->st_ino;
2957 vsbp->vst_uid = sbp->st_uid;
2958 vsbp->vst_gid = sbp->st_gid;
2959 vsbp->vst_atime = sbp->st_atimespec.tv_sec;
2960 vsbp->vst_atimensec = sbp->st_atimespec.tv_nsec;
2961 vsbp->vst_mtime = sbp->st_mtimespec.tv_sec;
2962 vsbp->vst_mtimensec = sbp->st_mtimespec.tv_nsec;
2963 vsbp->vst_ctime = sbp->st_ctimespec.tv_sec;
2964 vsbp->vst_ctimensec = sbp->st_ctimespec.tv_nsec;
2965 vsbp->vst_birthtime = sbp->st_birthtimespec.tv_sec;
2966 vsbp->vst_birthtimensec = sbp->st_birthtimespec.tv_nsec;
2967 vsbp->vst_size = sbp->st_size;
2968 vsbp->vst_blocks = sbp->st_blocks;
2969 vsbp->vst_blksize = sbp->st_blksize;
2970 vsbp->vst_flags = sbp->st_flags;
2971 vsbp->vst_gen = sbp->st_gen;
2972 vsbp->vst_rdev = sbp->st_rdev;
2973 vsbp->vst_qspare[0] = sbp->st_qspare[0];
2974 vsbp->vst_qspare[1] = sbp->st_qspare[1];
2975 }
2976
2977 int
2978 proc_pid_rusage(int pid, int flavor, user_addr_t buffer, __unused int32_t *retval)
2979 {
2980 proc_t p;
2981 int error;
2982 int zombie = 0;
2983
2984 if ((p = proc_find(pid)) == PROC_NULL) {
2985 if ((p = proc_find_zombref(pid)) == PROC_NULL) {
2986 return (ESRCH);
2987 }
2988 zombie = 1;
2989 }
2990
2991 /* Do we have permission to look into this? */
2992 if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDRUSAGE, flavor, CHECK_SAME_USER)))
2993 goto out;
2994
2995 error = proc_get_rusage(p, flavor, buffer, zombie);
2996
2997 out:
2998 if (zombie)
2999 proc_drop_zombref(p);
3000 else
3001 proc_rele(p);
3002
3003 return (error);
3004 }
3005
3006 void
3007 proc_archinfo(proc_t p, struct proc_archinfo *pai)
3008 {
3009 proc_lock(p);
3010 pai->p_cputype = p->p_cputype;
3011 pai->p_cpusubtype = p->p_cpusubtype;
3012 proc_unlock(p);
3013 }
3014
3015 void
3016 proc_pidcoalitioninfo(proc_t p, struct proc_pidcoalitioninfo *ppci)
3017 {
3018 bzero(ppci, sizeof(*ppci));
3019 proc_coalitionids(p, ppci->coalition_id);
3020 }
3021
3022 int
3023 proc_pidexitreasoninfo(proc_t p, struct proc_exitreasoninfo *peri, struct proc_exitreasonbasicinfo *pberi)
3024 {
3025 uint32_t reason_data_size = 0;
3026 int error = 0;
3027 pid_t selfpid = proc_selfpid();
3028
3029 proc_lock(p);
3030
3031 /*
3032 * One (and only one) of peri and pberi must be non-NULL.
3033 */
3034 assert((peri != NULL) || (pberi != NULL));
3035 assert((peri == NULL) || (pberi == NULL));
3036
3037 /*
3038 * Allow access to the parent of the exiting
3039 * child or the parent debugger only.
3040 */
3041 do {
3042 if (p->p_ppid == selfpid)
3043 break; /* parent => ok */
3044
3045 if ((p->p_lflag & P_LTRACED) != 0 &&
3046 (p->p_oppid == selfpid))
3047 break; /* parent-in-waiting => ok */
3048
3049 proc_unlock(p);
3050 return EACCES;
3051 } while (0);
3052
3053 if (p->p_exit_reason == OS_REASON_NULL) {
3054 proc_unlock(p);
3055 return ENOENT;
3056 }
3057
3058 if (p->p_exit_reason->osr_kcd_buf != NULL) {
3059 reason_data_size = kcdata_memory_get_used_bytes(&p->p_exit_reason->osr_kcd_descriptor);
3060 }
3061
3062 if (peri != NULL) {
3063 peri->eri_namespace = p->p_exit_reason->osr_namespace;
3064 peri->eri_code = p->p_exit_reason->osr_code;
3065 peri->eri_flags = p->p_exit_reason->osr_flags;
3066
3067 if ((peri->eri_kcd_buf == 0) || (peri->eri_reason_buf_size < reason_data_size)) {
3068 proc_unlock(p);
3069 return ENOMEM;
3070 }
3071
3072 peri->eri_reason_buf_size = reason_data_size;
3073 if (reason_data_size != 0) {
3074 error = copyout(p->p_exit_reason->osr_kcd_buf, peri->eri_kcd_buf, reason_data_size);
3075 }
3076 } else {
3077 pberi->beri_namespace = p->p_exit_reason->osr_namespace;
3078 pberi->beri_code = p->p_exit_reason->osr_code;
3079 pberi->beri_flags = p->p_exit_reason->osr_flags;
3080 pberi->beri_reason_buf_size = reason_data_size;
3081 }
3082
3083 proc_unlock(p);
3084
3085 return error;
3086 }
3087
3088 /*
3089 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
3090 * It mimics the data that is typically captured by the
3091 * EVFILT_PROC, NOTE_EXIT event mechanism.
3092 * See filt_proc() in kern_event.c.
3093 */
3094 int
3095 proc_pidnoteexit(proc_t p, uint64_t flags, uint32_t *data)
3096 {
3097 uint32_t exit_data = 0;
3098 uint32_t exit_flags = (uint32_t)flags;
3099
3100 proc_lock(p);
3101
3102 /*
3103 * Allow access to the parent of the exiting
3104 * child or the parent debugger only.
3105 */
3106 do {
3107 pid_t selfpid = proc_selfpid();
3108
3109 if (p->p_ppid == selfpid)
3110 break; /* parent => ok */
3111
3112 if ((p->p_lflag & P_LTRACED) != 0 &&
3113 (p->p_oppid == selfpid))
3114 break; /* parent-in-waiting => ok */
3115
3116 proc_unlock(p);
3117 return (EACCES);
3118 } while (0);
3119
3120 if ((exit_flags & NOTE_EXITSTATUS) != 0) {
3121 /* The signal and exit status */
3122 exit_data |= (p->p_xstat & NOTE_PDATAMASK);
3123 }
3124
3125 if ((exit_flags & NOTE_EXIT_DETAIL) != 0) {
3126 /* The exit detail */
3127 if ((p->p_lflag & P_LTERM_DECRYPTFAIL) != 0) {
3128 exit_data |= NOTE_EXIT_DECRYPTFAIL;
3129 }
3130
3131 if ((p->p_lflag & P_LTERM_JETSAM) != 0) {
3132 exit_data |= NOTE_EXIT_MEMORY;
3133
3134 switch (p->p_lflag & P_JETSAM_MASK) {
3135 case P_JETSAM_VMPAGESHORTAGE:
3136 exit_data |= NOTE_EXIT_MEMORY_VMPAGESHORTAGE;
3137 break;
3138 case P_JETSAM_VMTHRASHING:
3139 exit_data |= NOTE_EXIT_MEMORY_VMTHRASHING;
3140 break;
3141 case P_JETSAM_FCTHRASHING:
3142 exit_data |= NOTE_EXIT_MEMORY_FCTHRASHING;
3143 break;
3144 case P_JETSAM_VNODE:
3145 exit_data |= NOTE_EXIT_MEMORY_VNODE;
3146 break;
3147 case P_JETSAM_HIWAT:
3148 exit_data |= NOTE_EXIT_MEMORY_HIWAT;
3149 break;
3150 case P_JETSAM_PID:
3151 exit_data |= NOTE_EXIT_MEMORY_PID;
3152 break;
3153 case P_JETSAM_IDLEEXIT:
3154 exit_data |= NOTE_EXIT_MEMORY_IDLE;
3155 break;
3156 }
3157 }
3158
3159 if ((p->p_csflags & CS_KILLED) != 0) {
3160 exit_data |= NOTE_EXIT_CSERROR;
3161 }
3162 }
3163
3164 proc_unlock(p);
3165
3166 *data = exit_data;
3167
3168 return (0);
3169 }
3170
3171 int
3172 proc_piddynkqueueinfo(int pid, int flavor, kqueue_id_t kq_id,
3173 user_addr_t ubuf, uint32_t bufsize, int32_t *retval)
3174 {
3175 proc_t p;
3176 int err;
3177
3178 if (ubuf == USER_ADDR_NULL) {
3179 return EFAULT;
3180 }
3181
3182 p = proc_find(pid);
3183 if (p == PROC_NULL) {
3184 return ESRCH;
3185 }
3186
3187 err = proc_security_policy(p, PROC_INFO_CALL_PIDDYNKQUEUEINFO, 0, CHECK_SAME_USER);
3188 if (err) {
3189 goto out;
3190 }
3191
3192 switch (flavor) {
3193 case PROC_PIDDYNKQUEUE_INFO:
3194 err = kevent_copyout_dynkqinfo(p, kq_id, ubuf, bufsize, retval);
3195 break;
3196 case PROC_PIDDYNKQUEUE_EXTINFO:
3197 err = kevent_copyout_dynkqextinfo(p, kq_id, ubuf, bufsize, retval);
3198 break;
3199 default:
3200 err = ENOTSUP;
3201 break;
3202 }
3203
3204 out:
3205 proc_rele(p);
3206
3207 return err;
3208 }