+ error = proc_pidpathinfo_internal(p, arg, buf, buffersize, retval);
+ if (error == 0) {
+ error = copyout(buf, buffer, len);
+ }
+ kfree(buf, buffersize);
+ return(error);
+}
+
+int
+proc_pidpathinfo_internal(proc_t p, __unused uint64_t arg, char *buf, uint32_t buffersize, __unused int32_t *retval)
+{
+ int vid, error;
+ vnode_t tvp;
+ vnode_t nvp = NULLVP;
+ int len = buffersize;
+
+ tvp = p->p_textvp;
+
+ if (tvp == NULLVP)
+ return(ESRCH);
+
+ vid = vnode_vid(tvp);
+ error = vnode_getwithvid(tvp, vid);
+ if (error == 0) {
+ error = vn_getpath_fsenter(tvp, buf, &len);
+ vnode_put(tvp);
+ if (error == 0) {
+ error = vnode_lookup(buf, 0, &nvp, vfs_context_current());
+ if ((error == 0) && ( nvp != NULLVP))
+ vnode_put(nvp);
+ }
+ }
+ return(error);
+}
+
+
+int
+proc_pidworkqueueinfo(proc_t p, struct proc_workqueueinfo *pwqinfo)
+{
+ int error = 0;
+
+ bzero(pwqinfo, sizeof(struct proc_workqueueinfo));
+
+ error = fill_procworkqueue(p, pwqinfo);
+ if (error)
+ return(ESRCH);
+ else
+ return(0);
+
+}
+
+
+void
+proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo)
+{
+ p_uniqidinfo->p_uniqueid = proc_uniqueid(p);
+ proc_getexecutableuuid(p, (unsigned char *)&p_uniqidinfo->p_uuid, sizeof(p_uniqidinfo->p_uuid));
+ p_uniqidinfo->p_puniqueid = proc_puniqueid(p);
+ p_uniqidinfo->p_reserve2 = 0;
+ p_uniqidinfo->p_reserve3 = 0;
+ p_uniqidinfo->p_reserve4 = 0;
+}
+
+
+static int
+proc_piduuidinfo(pid_t pid, uuid_t uuid_buf, uint32_t buffersize)
+{
+ struct proc * p = PROC_NULL;
+ int zombref = 0;
+
+ if (buffersize < sizeof(uuid_t))
+ return EINVAL;
+
+ if ((p = proc_find(pid)) == PROC_NULL) {
+ p = proc_find_zombref(pid);
+ zombref = 1;
+ }
+ if (p == PROC_NULL) {
+ return ESRCH;
+ }
+
+ proc_getexecutableuuid(p, (unsigned char *)uuid_buf, buffersize);
+
+ if (zombref)
+ proc_drop_zombref(p);
+ else
+ proc_rele(p);
+
+ return 0;
+}
+
+/*
+ * Function to get the uuid and pid of the originator of the voucher.
+ */
+int
+proc_pidoriginatorpid_uuid(uuid_t uuid, uint32_t buffersize, pid_t *pid)
+{
+ pid_t originator_pid;
+ kern_return_t kr;
+ int error;
+
+ /*
+ * Get the current voucher origin pid. The pid returned here
+ * might not be valid or may have been recycled.
+ */
+ kr = thread_get_current_voucher_origin_pid(&originator_pid);
+ /* If errors, convert errors to appropriate format */
+ if (kr) {
+ if (kr == KERN_INVALID_TASK)
+ error = ESRCH;
+ else if (kr == KERN_INVALID_VALUE)
+ error = ENOATTR;
+ else
+ error = EINVAL;
+ return error;
+ }
+
+ *pid = originator_pid;
+ error = proc_piduuidinfo(originator_pid, uuid, buffersize);
+ return error;
+}
+
+/*
+ * Function to get the uuid of the originator of the voucher.
+ */
+int
+proc_pidoriginatoruuid(uuid_t uuid, uint32_t buffersize)
+{
+ pid_t originator_pid;
+ return (proc_pidoriginatorpid_uuid(uuid, buffersize, &originator_pid));
+}
+
+/***************************** proc_pidoriginatorinfo ***************************/
+
+int
+proc_pidoriginatorinfo(int pid, int flavor, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
+{
+ int error = ENOTSUP;
+ uint32_t size;
+
+ switch (flavor) {
+ case PROC_PIDORIGINATOR_UUID:
+ size = PROC_PIDORIGINATOR_UUID_SIZE;
+ break;
+ case PROC_PIDORIGINATOR_BGSTATE:
+ size = PROC_PIDORIGINATOR_BGSTATE_SIZE;
+ break;
+ case PROC_PIDORIGINATOR_PID_UUID:
+ size = PROC_PIDORIGINATOR_PID_UUID_SIZE;
+ break;
+ default:
+ return(EINVAL);
+ }
+
+ if (buffersize < size)
+ return(ENOMEM);
+
+ if (pid != 0 && pid != proc_selfpid())
+ return (EINVAL);
+
+ switch (flavor) {
+ case PROC_PIDORIGINATOR_UUID: {
+ uuid_t uuid;
+
+ error = proc_pidoriginatoruuid(uuid, sizeof(uuid));
+ if (error != 0)
+ goto out;
+
+ error = copyout(uuid, buffer, size);
+ if (error == 0)
+ *retval = size;
+ }
+ break;
+
+ case PROC_PIDORIGINATOR_PID_UUID: {
+ struct proc_originatorinfo originator_info;
+ bzero(&originator_info, sizeof(originator_info));
+
+ error = proc_pidoriginatorpid_uuid(originator_info.originator_uuid,
+ sizeof(uuid_t), &originator_info.originator_pid);
+ if (error != 0)
+ goto out;
+
+ error = copyout(&originator_info, buffer, size);
+ if (error == 0)
+ *retval = size;
+ }
+ break;
+
+ case PROC_PIDORIGINATOR_BGSTATE: {
+ uint32_t is_backgrounded;
+ error = proc_get_originatorbgstate(&is_backgrounded);
+ if (error)
+ goto out;
+
+ error = copyout(&is_backgrounded, buffer, size);
+ if (error == 0)
+ *retval = size;
+ }
+ break;
+
+ default:
+ error = ENOTSUP;
+ }
+out:
+ return error;
+}
+
+/***************************** proc_listcoalitions ***************************/
+int proc_listcoalitions(int flavor, int type, user_addr_t buffer,
+ uint32_t buffersize, int32_t *retval)
+{
+#if CONFIG_COALITIONS
+ int error = ENOTSUP;
+ int coal_type;
+ uint32_t elem_size;
+ void *coalinfo = NULL;
+ uint32_t k_buffersize = 0, copyout_sz = 0;
+ int ncoals = 0, ncoals_ = 0;
+
+ /* struct procinfo_coalinfo; */
+
+ switch (flavor) {
+ case LISTCOALITIONS_ALL_COALS:
+ elem_size = LISTCOALITIONS_ALL_COALS_SIZE;
+ coal_type = -1;
+ break;
+ case LISTCOALITIONS_SINGLE_TYPE:
+ elem_size = LISTCOALITIONS_SINGLE_TYPE_SIZE;
+ coal_type = type;
+ break;
+ default:
+ return EINVAL;
+ }
+
+ /* find the total number of coalitions */
+ ncoals = coalitions_get_list(coal_type, NULL, 0);
+
+ if (ncoals == 0 || buffer == 0 || buffersize == 0) {
+ /*
+ * user just wants buffer size
+ * or there are no coalitions
+ */
+ error = 0;
+ *retval = (int)(ncoals * elem_size);
+ goto out;
+ }
+
+ k_buffersize = ncoals * elem_size;
+ coalinfo = kalloc((vm_size_t)k_buffersize);
+ if (!coalinfo) {
+ error = ENOMEM;
+ goto out;
+ }
+ bzero(coalinfo, k_buffersize);
+
+ switch (flavor) {
+ case LISTCOALITIONS_ALL_COALS:
+ case LISTCOALITIONS_SINGLE_TYPE:
+ ncoals_ = coalitions_get_list(coal_type, coalinfo, ncoals);
+ break;
+ default:
+ panic("memory corruption?!");
+ }
+
+ if (ncoals_ == 0) {
+ /* all the coalitions disappeared... weird but valid */
+ error = 0;
+ *retval = 0;
+ goto out;
+ }
+
+ /*
+ * Some coalitions may have disappeared between our initial check,
+ * and the the actual list acquisition.
+ * Only copy out what we really need.
+ */
+ copyout_sz = k_buffersize;
+ if (ncoals_ < ncoals)
+ copyout_sz = ncoals_ * elem_size;
+
+ /*
+ * copy the list up to user space
+ * (we're guaranteed to have a non-null pointer/size here)
+ */
+ error = copyout(coalinfo, buffer,
+ copyout_sz < buffersize ? copyout_sz : buffersize);
+
+ if (error == 0)
+ *retval = (int)copyout_sz;
+
+out:
+ if (coalinfo)
+ kfree(coalinfo, k_buffersize);
+
+ return error;
+#else
+ /* no coalition support */
+ (void)flavor;
+ (void)type;
+ (void)buffer;
+ (void)buffersize;
+ (void)retval;
+ return ENOTSUP;
+#endif
+}
+
+
+/*************************** proc_can_use_forgeound_hw **************************/
+int proc_can_use_foreground_hw(int pid, user_addr_t u_reason, uint32_t reasonsize, int32_t *retval)
+{
+ proc_t p = PROC_NULL;
+ int error = 0;
+ uint32_t reason = PROC_FGHW_ERROR;
+ uint32_t isBG = 0;
+ task_t task = TASK_NULL;
+#if CONFIG_COALITIONS
+ coalition_t coal = COALITION_NULL;
+#endif
+
+ *retval = 0;
+
+ if (pid <= 0) {
+ error = EINVAL;
+ reason = PROC_FGHW_ERROR;
+ goto out;
+ }
+
+ p = proc_find(pid);
+ if (p == PROC_NULL) {
+ error = ESRCH;
+ reason = PROC_FGHW_ERROR;
+ goto out;
+ }
+
+#if CONFIG_COALITIONS
+ if (p != current_proc() &&
+ !kauth_cred_issuser(kauth_cred_get())) {
+ error = EPERM;
+ reason = PROC_FGHW_ERROR;
+ goto out;
+ }
+
+ task = p->task;
+ task_reference(task);
+ if (coalition_is_leader(task, COALITION_TYPE_JETSAM, &coal) == FALSE) {
+ /* current task is not a coalition leader: find the leader */
+ task_deallocate(task);
+ task = coalition_get_leader(coal);
+ }
+
+ if (task != TASK_NULL) {
+ /*
+ * If task is non-null, then it is the coalition leader of the
+ * current process' coalition. This could be the same task as
+ * the current_task, and that's OK.
+ */
+ uint32_t flags = 0;
+ int role;
+
+ proc_get_darwinbgstate(task, &flags);
+ if ((flags & PROC_FLAG_APPLICATION) != PROC_FLAG_APPLICATION) {
+ /*
+ * Coalition leader is not an application, continue
+ * searching for other ways this task could gain
+ * access to HW
+ */
+ reason = PROC_FGHW_DAEMON_LEADER;
+ goto no_leader;
+ }
+
+ if (proc_get_effective_task_policy(task, TASK_POLICY_DARWIN_BG)) {
+ /*
+ * If the leader of the current process' coalition has
+ * been marked as DARWIN_BG, then it definitely should
+ * not be using foreground hardware resources.
+ */
+ reason = PROC_FGHW_LEADER_BACKGROUND;
+ goto out;
+ }
+
+ role = proc_get_effective_task_policy(task, TASK_POLICY_ROLE);
+ switch (role) {
+ case TASK_FOREGROUND_APPLICATION: /* DARWIN_ROLE_UI_FOCAL */
+ case TASK_BACKGROUND_APPLICATION: /* DARWIN_ROLE_UI */
+ /*
+ * The leader of this coalition is a focal, UI app:
+ * access granted
+ * TODO: should extensions/plugins be allowed to use
+ * this hardware?
+ */
+ *retval = 1;
+ reason = PROC_FGHW_OK;
+ goto out;
+ case TASK_DEFAULT_APPLICATION: /* DARWIN_ROLE_UI_NON_FOCAL */
+ case TASK_NONUI_APPLICATION: /* DARWIN_ROLE_NON_UI */
+ case TASK_THROTTLE_APPLICATION:
+ case TASK_UNSPECIFIED:
+ default:
+ /* non-focal, non-ui apps don't get access */
+ reason = PROC_FGHW_LEADER_NONUI;
+ goto out;
+ }
+ }
+
+no_leader:
+ if (task != TASK_NULL) {
+ task_deallocate(task);
+ task = TASK_NULL;
+ }
+#endif /* CONFIG_COALITIONS */
+
+ /*
+ * There is no reasonable semantic to investigate the currently
+ * adopted voucher of an arbitrary thread in a non-current process.
+ * We return '0'
+ */
+ if (p != current_proc()) {
+ error = EINVAL;
+ goto out;
+ }
+
+ /*
+ * In the absence of coalitions, fall back to a voucher-based lookup
+ * where a daemon can used foreground HW if it's operating on behalf
+ * of a foreground application.
+ * NOTE: this is equivalent to a call to
+ * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
+ */
+ isBG = 1;
+ error = proc_get_originatorbgstate(&isBG);
+ switch (error) {
+ case 0:
+ break;
+ case ESRCH:
+ reason = PROC_FGHW_NO_ORIGINATOR;
+ error = 0;
+ goto out;
+ case ENOATTR:
+ reason = PROC_FGHW_NO_VOUCHER_ATTR;
+ error = 0;
+ goto out;
+ case EINVAL:
+ reason = PROC_FGHW_DAEMON_NO_VOUCHER;
+ error = 0;
+ goto out;
+ default:
+ /* some other error occurred: report that to the caller */
+ reason = PROC_FGHW_VOUCHER_ERROR;
+ goto out;
+ }
+
+ if (isBG) {
+ reason = PROC_FGHW_ORIGINATOR_BACKGROUND;
+ error = 0;
+ } else {
+ /*
+ * The process itself is either a foreground app, or has
+ * adopted a voucher originating from an app that's still in
+ * the foreground
+ */
+ reason = PROC_FGHW_DAEMON_OK;
+ *retval = 1;
+ }
+
+out:
+ if (task != TASK_NULL)
+ task_deallocate(task);
+ if (p != PROC_NULL)
+ proc_rele(p);
+ if (reasonsize >= sizeof(reason) && u_reason != (user_addr_t)0)
+ (void)copyout(&reason, u_reason, sizeof(reason));
+ return error;
+}
+
+
+/********************************** proc_pidinfo ********************************/
+
+
+int
+proc_pidinfo(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
+{
+ struct proc * p = PROC_NULL;
+ int error = ENOTSUP;
+ int gotref = 0;
+ int findzomb = 0;
+ int shortversion = 0;
+ uint32_t size;
+ int zombie = 0;
+ int thuniqueid = 0;
+ int uniqidversion = 0;
+ boolean_t check_same_user;
+
+ switch (flavor) {
+ case PROC_PIDLISTFDS:
+ size = PROC_PIDLISTFD_SIZE;
+ if (buffer == (user_addr_t)0)
+ size = 0;
+ break;
+ case PROC_PIDTBSDINFO:
+ size = PROC_PIDTBSDINFO_SIZE;
+ break;
+ case PROC_PIDTASKINFO:
+ size = PROC_PIDTASKINFO_SIZE;
+ break;
+ case PROC_PIDTASKALLINFO:
+ size = PROC_PIDTASKALLINFO_SIZE;
+ break;
+ case PROC_PIDTHREADINFO:
+ size = PROC_PIDTHREADINFO_SIZE;
+ break;
+ case PROC_PIDLISTTHREADS:
+ size = PROC_PIDLISTTHREADS_SIZE;
+ break;
+ case PROC_PIDREGIONINFO:
+ size = PROC_PIDREGIONINFO_SIZE;
+ break;
+ case PROC_PIDREGIONPATHINFO:
+ size = PROC_PIDREGIONPATHINFO_SIZE;
+ break;
+ case PROC_PIDVNODEPATHINFO:
+ size = PROC_PIDVNODEPATHINFO_SIZE;
+ break;
+ case PROC_PIDTHREADPATHINFO:
+ size = PROC_PIDTHREADPATHINFO_SIZE;
+ break;
+ case PROC_PIDPATHINFO:
+ size = MAXPATHLEN;
+ break;
+ case PROC_PIDWORKQUEUEINFO:
+ /* kernel does not have workq info */
+ if (pid == 0)
+ return(EINVAL);
+ else
+ size = PROC_PIDWORKQUEUEINFO_SIZE;
+ break;
+ case PROC_PIDT_SHORTBSDINFO:
+ size = PROC_PIDT_SHORTBSDINFO_SIZE;
+ break;
+ case PROC_PIDLISTFILEPORTS:
+ size = PROC_PIDLISTFILEPORTS_SIZE;
+ if (buffer == (user_addr_t)0)
+ size = 0;
+ break;
+ case PROC_PIDTHREADID64INFO:
+ size = PROC_PIDTHREADID64INFO_SIZE;
+ break;
+ case PROC_PIDUNIQIDENTIFIERINFO:
+ size = PROC_PIDUNIQIDENTIFIERINFO_SIZE;
+ break;
+ case PROC_PIDT_BSDINFOWITHUNIQID:
+ size = PROC_PIDT_BSDINFOWITHUNIQID_SIZE;
+ break;
+ case PROC_PIDARCHINFO:
+ size = PROC_PIDARCHINFO_SIZE;
+ break;
+ case PROC_PIDCOALITIONINFO:
+ size = PROC_PIDCOALITIONINFO_SIZE;
+ break;
+ case PROC_PIDNOTEEXIT:
+ /*
+ * Set findzomb explicitly because arg passed
+ * in is used as note exit status bits.
+ */
+ size = PROC_PIDNOTEEXIT_SIZE;
+ findzomb = 1;
+ break;
+ case PROC_PIDEXITREASONINFO:
+ size = PROC_PIDEXITREASONINFO_SIZE;
+ findzomb = 1;
+ break;
+ case PROC_PIDEXITREASONBASICINFO:
+ size = PROC_PIDEXITREASONBASICINFOSIZE;
+ findzomb = 1;
+ break;
+ case PROC_PIDREGIONPATHINFO2:
+ size = PROC_PIDREGIONPATHINFO2_SIZE;
+ break;
+ case PROC_PIDREGIONPATHINFO3:
+ size = PROC_PIDREGIONPATHINFO3_SIZE;
+ break;
+ default:
+ return(EINVAL);
+ }
+
+ if (buffersize < size)
+ return(ENOMEM);
+
+ if ((flavor == PROC_PIDPATHINFO) && (buffersize > PROC_PIDPATHINFO_MAXSIZE)) {
+ return(EOVERFLOW);
+ }
+
+ /* Check if we need to look for zombies */
+ if ((flavor == PROC_PIDTBSDINFO) || (flavor == PROC_PIDT_SHORTBSDINFO) || (flavor == PROC_PIDT_BSDINFOWITHUNIQID)
+ || (flavor == PROC_PIDUNIQIDENTIFIERINFO)) {
+ if (arg)
+ findzomb = 1;
+ }
+
+ if ((p = proc_find(pid)) == PROC_NULL) {
+ if (findzomb)
+ p = proc_find_zombref(pid);
+ if (p == PROC_NULL) {
+ error = ESRCH;
+ goto out;
+ }
+ zombie = 1;
+ } else {
+ gotref = 1;
+ }
+
+ /* Certain operations don't require privileges */
+ switch (flavor) {
+ case PROC_PIDT_SHORTBSDINFO:
+ case PROC_PIDUNIQIDENTIFIERINFO:
+ case PROC_PIDPATHINFO:
+ case PROC_PIDCOALITIONINFO:
+ check_same_user = NO_CHECK_SAME_USER;
+ break;
+ default:
+ check_same_user = CHECK_SAME_USER;
+ break;
+ }
+
+ /* Do we have permission to look into this? */
+ if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDINFO, flavor, check_same_user)))
+ goto out;
+
+ switch (flavor) {
+ case PROC_PIDLISTFDS: {
+ error = proc_pidfdlist(p, buffer, buffersize, retval);
+ }
+ break;
+
+ case PROC_PIDUNIQIDENTIFIERINFO: {
+ struct proc_uniqidentifierinfo p_uniqidinfo;
+ bzero(&p_uniqidinfo, sizeof(p_uniqidinfo));
+ proc_piduniqidentifierinfo(p, &p_uniqidinfo);
+ error = copyout(&p_uniqidinfo, buffer, sizeof(struct proc_uniqidentifierinfo));
+ if (error == 0)
+ *retval = sizeof(struct proc_uniqidentifierinfo);
+ }
+ break;
+
+ case PROC_PIDT_SHORTBSDINFO:
+ shortversion = 1;
+ case PROC_PIDT_BSDINFOWITHUNIQID:
+ case PROC_PIDTBSDINFO: {
+ struct proc_bsdinfo pbsd;
+ struct proc_bsdshortinfo pbsd_short;
+ struct proc_bsdinfowithuniqid pbsd_uniqid;
+
+ if (flavor == PROC_PIDT_BSDINFOWITHUNIQID)
+ uniqidversion = 1;
+
+ if (shortversion != 0) {
+ error = proc_pidshortbsdinfo(p, &pbsd_short, zombie);
+ } else {
+ error = proc_pidbsdinfo(p, &pbsd, zombie);
+ if (uniqidversion != 0) {
+ bzero(&pbsd_uniqid, sizeof(pbsd_uniqid));
+ proc_piduniqidentifierinfo(p, &pbsd_uniqid.p_uniqidentifier);
+ pbsd_uniqid.pbsd = pbsd;
+ }
+ }
+
+ if (error == 0) {
+ if (shortversion != 0) {
+ error = copyout(&pbsd_short, buffer, sizeof(struct proc_bsdshortinfo));
+ if (error == 0)
+ *retval = sizeof(struct proc_bsdshortinfo);
+ } else if (uniqidversion != 0) {
+ error = copyout(&pbsd_uniqid, buffer, sizeof(struct proc_bsdinfowithuniqid));
+ if (error == 0)
+ *retval = sizeof(struct proc_bsdinfowithuniqid);
+ } else {
+ error = copyout(&pbsd, buffer, sizeof(struct proc_bsdinfo));
+ if (error == 0)
+ *retval = sizeof(struct proc_bsdinfo);
+ }
+ }
+ }
+ break;
+
+ case PROC_PIDTASKINFO: {
+ struct proc_taskinfo ptinfo;
+
+ error = proc_pidtaskinfo(p, &ptinfo);
+ if (error == 0) {
+ error = copyout(&ptinfo, buffer, sizeof(struct proc_taskinfo));
+ if (error == 0)
+ *retval = sizeof(struct proc_taskinfo);
+ }
+ }
+ break;
+
+ case PROC_PIDTASKALLINFO: {
+ struct proc_taskallinfo pall;
+ bzero(&pall, sizeof(pall));
+ error = proc_pidbsdinfo(p, &pall.pbsd, 0);
+ error = proc_pidtaskinfo(p, &pall.ptinfo);
+ if (error == 0) {
+ error = copyout(&pall, buffer, sizeof(struct proc_taskallinfo));
+ if (error == 0)
+ *retval = sizeof(struct proc_taskallinfo);
+ }
+ }
+ break;
+
+ case PROC_PIDTHREADID64INFO:
+ thuniqueid = 1;
+ case PROC_PIDTHREADINFO:{
+ struct proc_threadinfo pthinfo;
+
+ error = proc_pidthreadinfo(p, arg, thuniqueid, &pthinfo);
+ if (error == 0) {
+ error = copyout(&pthinfo, buffer, sizeof(struct proc_threadinfo));
+ if (error == 0)
+ *retval = sizeof(struct proc_threadinfo);
+ }
+ }
+ break;
+
+ case PROC_PIDLISTTHREADS:{
+ error = proc_pidlistthreads(p, buffer, buffersize, retval);
+ }
+ break;
+
+ case PROC_PIDREGIONINFO:{
+ error = proc_pidregioninfo(p, arg, buffer, buffersize, retval);
+ }
+ break;
+
+
+ case PROC_PIDREGIONPATHINFO:{
+ error = proc_pidregionpathinfo(p, arg, buffer, buffersize, retval);
+ }
+ break;
+
+ case PROC_PIDREGIONPATHINFO2:{
+ error = proc_pidregionpathinfo2(p, arg, buffer, buffersize, retval);
+ }
+ break;
+
+ case PROC_PIDREGIONPATHINFO3:{
+ error = proc_pidregionpathinfo3(p, arg, buffer, buffersize, retval);
+ }
+ break;
+
+ case PROC_PIDVNODEPATHINFO:{
+ error = proc_pidvnodepathinfo(p, arg, buffer, buffersize, retval);
+ }
+ break;
+
+
+ case PROC_PIDTHREADPATHINFO:{
+ struct proc_threadwithpathinfo pinfo;
+
+ error = proc_pidthreadpathinfo(p, arg, &pinfo);
+ if (error == 0) {
+ error = copyout((caddr_t)&pinfo, buffer, sizeof(struct proc_threadwithpathinfo));
+ if (error == 0)
+ *retval = sizeof(struct proc_threadwithpathinfo);
+ }
+ }
+ break;
+
+ case PROC_PIDPATHINFO: {
+ error = proc_pidpathinfo(p, arg, buffer, buffersize, retval);
+ }
+ break;
+
+
+ case PROC_PIDWORKQUEUEINFO:{
+ struct proc_workqueueinfo pwqinfo;
+
+ error = proc_pidworkqueueinfo(p, &pwqinfo);
+ if (error == 0) {
+ error = copyout(&pwqinfo, buffer, sizeof(struct proc_workqueueinfo));
+ if (error == 0)
+ *retval = sizeof(struct proc_workqueueinfo);
+ }
+ }
+ break;
+
+ case PROC_PIDLISTFILEPORTS: {
+ error = proc_pidfileportlist(p, buffer, buffersize, retval);
+ }
+ break;
+
+ case PROC_PIDARCHINFO: {
+ struct proc_archinfo pai;
+ bzero(&pai, sizeof(pai));
+ proc_archinfo(p, &pai);
+ error = copyout(&pai, buffer, sizeof(struct proc_archinfo));
+ if (error == 0) {
+ *retval = sizeof(struct proc_archinfo);
+ }
+ }
+ break;
+
+ case PROC_PIDCOALITIONINFO: {
+ struct proc_pidcoalitioninfo pci;
+ proc_pidcoalitioninfo(p, &pci);
+ error = copyout(&pci, buffer, sizeof(struct proc_pidcoalitioninfo));
+ if (error == 0) {
+ *retval = sizeof(struct proc_pidcoalitioninfo);
+ }
+ }
+ break;
+
+ case PROC_PIDNOTEEXIT: {
+ uint32_t data;
+ error = proc_pidnoteexit(p, arg, &data);
+ if (error == 0) {
+ error = copyout(&data, buffer, sizeof(data));
+ if (error == 0) {
+ *retval = sizeof(data);
+ }
+ }
+ }
+ break;
+
+ case PROC_PIDEXITREASONINFO: {
+ struct proc_exitreasoninfo eri;
+
+ error = copyin(buffer, &eri, sizeof(eri));
+ if (error != 0) {
+ break;
+ }
+
+ error = proc_pidexitreasoninfo(p, &eri, NULL);
+ if (error == 0) {
+ error = copyout(&eri, buffer, sizeof(eri));
+ if (error == 0) {
+ *retval = sizeof(eri);
+ }
+ }
+ }
+ break;
+
+ case PROC_PIDEXITREASONBASICINFO: {
+ struct proc_exitreasonbasicinfo beri;
+
+ bzero(&beri, sizeof(struct proc_exitreasonbasicinfo));
+
+ error = proc_pidexitreasoninfo(p, NULL, &beri);
+ if (error == 0) {
+ error = copyout(&beri, buffer, sizeof(beri));
+ if (error == 0) {
+ *retval = sizeof(beri);
+ }
+ }
+ }
+ break;
+
+ default:
+ error = ENOTSUP;
+ }
+
+out:
+ if (gotref)
+ proc_rele(p);
+ else if (zombie)
+ proc_drop_zombref(p);
+ return(error);
+}
+
+
+int
+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)
+{
+ struct vnode_fdinfo vfi;
+ int error= 0;
+
+ if ((error = vnode_getwithvid(vp, vid)) != 0) {
+ return(error);
+ }
+ bzero(&vfi, sizeof(struct vnode_fdinfo));
+ fill_fileinfo(fp, proc, fd, &vfi.pfi);
+ error = fill_vnodeinfo(vp, &vfi.pvi);
+ vnode_put(vp);
+ if (error == 0) {
+ error = copyout((caddr_t)&vfi, buffer, sizeof(struct vnode_fdinfo));
+ if (error == 0)
+ *retval = sizeof(struct vnode_fdinfo);
+ }
+ return(error);
+}
+
+int
+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)
+{
+ struct vnode_fdinfowithpath vfip;
+ int count, error= 0;
+
+ if ((error = vnode_getwithvid(vp, vid)) != 0) {
+ return(error);
+ }
+ bzero(&vfip, sizeof(struct vnode_fdinfowithpath));
+ fill_fileinfo(fp, proc, fd, &vfip.pfi);
+ error = fill_vnodeinfo(vp, &vfip.pvip.vip_vi) ;
+ if (error == 0) {
+ count = MAXPATHLEN;
+ vn_getpath(vp, &vfip.pvip.vip_path[0], &count);
+ vfip.pvip.vip_path[MAXPATHLEN-1] = 0;
+ vnode_put(vp);
+ error = copyout((caddr_t)&vfip, buffer, sizeof(struct vnode_fdinfowithpath));
+ if (error == 0)
+ *retval = sizeof(struct vnode_fdinfowithpath);
+ } else
+ vnode_put(vp);
+ return(error);
+}
+
+void
+fill_fileinfo(struct fileproc * fp, proc_t proc, int fd, struct proc_fileinfo * fproc)
+{
+ fproc->fi_openflags = fp->f_fglob->fg_flag;
+ fproc->fi_status = 0;
+ fproc->fi_offset = fp->f_fglob->fg_offset;
+ fproc->fi_type = FILEGLOB_DTYPE(fp->f_fglob);
+ if (fp->f_fglob->fg_count > 1)
+ fproc->fi_status |= PROC_FP_SHARED;
+ if (proc != PROC_NULL) {
+ if ((FDFLAGS_GET(proc, fd) & UF_EXCLOSE) != 0)
+ fproc->fi_status |= PROC_FP_CLEXEC;
+ if ((FDFLAGS_GET(proc, fd) & UF_FORKCLOSE) != 0)
+ fproc->fi_status |= PROC_FP_CLFORK;
+ }
+ if (FILEPROC_TYPE(fp) == FTYPE_GUARDED) {
+ fproc->fi_status |= PROC_FP_GUARDED;
+ fproc->fi_guardflags = 0;
+ if (fp_isguarded(fp, GUARD_CLOSE))
+ fproc->fi_guardflags |= PROC_FI_GUARD_CLOSE;
+ if (fp_isguarded(fp, GUARD_DUP))
+ fproc->fi_guardflags |= PROC_FI_GUARD_DUP;
+ if (fp_isguarded(fp, GUARD_SOCKET_IPC))
+ fproc->fi_guardflags |= PROC_FI_GUARD_SOCKET_IPC;
+ if (fp_isguarded(fp, GUARD_FILEPORT))
+ fproc->fi_guardflags |= PROC_FI_GUARD_FILEPORT;
+ }
+}
+
+
+
+int
+fill_vnodeinfo(vnode_t vp, struct vnode_info *vinfo)
+{
+ vfs_context_t context;
+ struct stat64 sb;
+ int error = 0;
+
+ bzero(&sb, sizeof(struct stat64));
+ context = vfs_context_create((vfs_context_t)0);
+ error = vn_stat(vp, &sb, NULL, 1, context);
+ (void)vfs_context_rele(context);
+
+ munge_vinfo_stat(&sb, &vinfo->vi_stat);
+
+ if (error != 0)
+ goto out;
+
+ if (vp->v_mount != dead_mountp) {
+ vinfo->vi_fsid = vp->v_mount->mnt_vfsstat.f_fsid;
+ } else {
+ vinfo->vi_fsid.val[0] = 0;
+ vinfo->vi_fsid.val[1] = 0;
+ }
+ vinfo->vi_type = vp->v_type;
+out:
+ return(error);
+}
+
+int
+pid_socketinfo(socket_t so, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
+{
+#if SOCKETS
+ struct socket_fdinfo s;
+ int error = 0;
+
+ bzero(&s, sizeof(struct socket_fdinfo));
+ fill_fileinfo(fp, proc, fd, &s.pfi);
+ if ((error = fill_socketinfo(so, &s.psi)) == 0) {
+ if ((error = copyout(&s, buffer, sizeof(struct socket_fdinfo))) == 0)
+ *retval = sizeof(struct socket_fdinfo);
+ }
+ return (error);
+#else
+#pragma unused(so, fp, proc, fd, buffer)
+ *retval = 0;
+ return (ENOTSUP);
+#endif
+}
+
+int
+pid_pseminfo(struct psemnode *psem, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
+{
+ struct psem_fdinfo pseminfo;
+ int error = 0;
+
+ bzero(&pseminfo, sizeof(struct psem_fdinfo));
+ fill_fileinfo(fp, proc, fd, &pseminfo.pfi);
+
+ if ((error = fill_pseminfo(psem, &pseminfo.pseminfo)) == 0) {
+ if ((error = copyout(&pseminfo, buffer, sizeof(struct psem_fdinfo))) == 0)
+ *retval = sizeof(struct psem_fdinfo);
+ }
+
+ return(error);
+}
+
+int
+pid_pshminfo(struct pshmnode *pshm, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
+{
+ struct pshm_fdinfo pshminfo;
+ int error = 0;
+
+ bzero(&pshminfo, sizeof(struct pshm_fdinfo));
+ fill_fileinfo(fp, proc, fd, &pshminfo.pfi);
+
+ if ((error = fill_pshminfo(pshm, &pshminfo.pshminfo)) == 0) {
+ if ((error = copyout(&pshminfo, buffer, sizeof(struct pshm_fdinfo))) == 0)
+ *retval = sizeof(struct pshm_fdinfo);
+ }
+
+ return(error);
+}
+
+int
+pid_pipeinfo(struct pipe * p, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
+{
+ struct pipe_fdinfo pipeinfo;
+ int error = 0;
+
+ bzero(&pipeinfo, sizeof(struct pipe_fdinfo));
+ fill_fileinfo(fp, proc, fd, &pipeinfo.pfi);
+ if ((error = fill_pipeinfo(p, &pipeinfo.pipeinfo)) == 0) {
+ if ((error = copyout(&pipeinfo, buffer, sizeof(struct pipe_fdinfo))) == 0)
+ *retval = sizeof(struct pipe_fdinfo);
+ }
+
+ return(error);
+}
+
+int
+pid_kqueueinfo(struct kqueue * kq, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
+{
+ struct kqueue_fdinfo kqinfo;
+ int error = 0;
+
+ bzero(&kqinfo, sizeof(struct kqueue_fdinfo));
+
+ /* not all kq's are associated with a file (e.g. workqkq) */
+ if (fp) {
+ assert(fd >= 0);
+ fill_fileinfo(fp, proc, fd, &kqinfo.pfi);
+ }
+
+ if ((error = fill_kqueueinfo(kq, &kqinfo.kqueueinfo)) == 0) {
+ if ((error = copyout(&kqinfo, buffer, sizeof(struct kqueue_fdinfo))) == 0)
+ *retval = sizeof(struct kqueue_fdinfo);
+ }
+
+ return(error);
+}
+
+int
+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)
+{
+ return ENOTSUP;
+}
+
+
+/************************** proc_pidfdinfo routine ***************************/
+int
+proc_pidfdinfo(int pid, int flavor, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
+{
+ proc_t p;
+ int error = ENOTSUP;
+ struct fileproc * fp = NULL;
+ uint32_t size;
+
+ switch (flavor) {
+ case PROC_PIDFDVNODEINFO:
+ size = PROC_PIDFDVNODEINFO_SIZE;
+ break;
+ case PROC_PIDFDVNODEPATHINFO:
+ size = PROC_PIDFDVNODEPATHINFO_SIZE;
+ break;
+ case PROC_PIDFDSOCKETINFO:
+ size = PROC_PIDFDSOCKETINFO_SIZE;
+ break;
+ case PROC_PIDFDPSEMINFO:
+ size = PROC_PIDFDPSEMINFO_SIZE;
+ break;
+ case PROC_PIDFDPSHMINFO:
+ size = PROC_PIDFDPSHMINFO_SIZE;
+ break;
+ case PROC_PIDFDPIPEINFO:
+ size = PROC_PIDFDPIPEINFO_SIZE;
+ break;
+ case PROC_PIDFDKQUEUEINFO:
+ size = PROC_PIDFDKQUEUEINFO_SIZE;
+ break;
+ case PROC_PIDFDKQUEUE_EXTINFO:
+ size = PROC_PIDFDKQUEUE_EXTINFO_SIZE;
+ if (buffer == (user_addr_t)0)
+ size = 0;
+ break;
+ case PROC_PIDFDATALKINFO:
+ size = PROC_PIDFDATALKINFO_SIZE;
+ break;
+
+ default:
+ return(EINVAL);
+
+ }
+
+ if (buffersize < size)
+ return(ENOMEM);
+
+ if ((p = proc_find(pid)) == PROC_NULL) {
+ error = ESRCH;
+ goto out;