+ copy->pl_refcnt = 1;
+ /* hang on to reference to old till process exits */
+ p->p_olimit = p->p_limit;
+ p->p_limit = copy;
+ proc_list_unlock();
+
+ return(0);
+}
+
+/*
+ * iopolicysys
+ *
+ * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
+ *
+ * Parameters: cmd Policy command
+ * arg Pointer to policy arguments
+ *
+ * Returns: 0 Success
+ * EINVAL Invalid command or invalid policy arguments
+ *
+ */
+
+static int
+iopolicysys_disk(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
+static int
+iopolicysys_vfs(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
+
+int
+iopolicysys(struct proc *p, struct iopolicysys_args *uap, int32_t *retval)
+{
+ int error = 0;
+ struct _iopol_param_t iop_param;
+
+ if ((error = copyin(uap->arg, &iop_param, sizeof(iop_param))) != 0)
+ goto out;
+
+ switch (iop_param.iop_iotype) {
+ case IOPOL_TYPE_DISK:
+ error = iopolicysys_disk(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
+ if (error == EIDRM) {
+ *retval = -2;
+ error = 0;
+ }
+ if (error)
+ goto out;
+ break;
+ case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY:
+ error = iopolicysys_vfs(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
+ if (error)
+ goto out;
+ break;
+ default:
+ error = EINVAL;
+ goto out;
+ }
+
+ /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
+ if (uap->cmd == IOPOL_CMD_GET) {
+ error = copyout((caddr_t)&iop_param, uap->arg, sizeof(iop_param));
+ if (error)
+ goto out;
+ }
+
+out:
+ return (error);
+}
+
+static int
+iopolicysys_disk(struct proc *p __unused, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
+{
+ int error = 0;
+ thread_t thread;
+ int policy_flavor;
+
+ /* Validate scope */
+ switch (scope) {
+ case IOPOL_SCOPE_PROCESS:
+ thread = THREAD_NULL;
+ policy_flavor = TASK_POLICY_IOPOL;
+ break;
+
+ case IOPOL_SCOPE_THREAD:
+ thread = current_thread();
+ policy_flavor = TASK_POLICY_IOPOL;
+
+ /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
+ if (cmd == IOPOL_CMD_SET && thread_has_qos_policy(thread)) {
+ switch (policy) {
+ case IOPOL_DEFAULT:
+ case IOPOL_PASSIVE:
+ break;
+ case IOPOL_UTILITY:
+ case IOPOL_THROTTLE:
+ case IOPOL_IMPORTANT:
+ case IOPOL_STANDARD:
+ if (!thread_is_static_param(thread)) {
+ thread_remove_qos_policy(thread);
+ /*
+ * This is not an error case, this is to return a marker to user-space that
+ * we stripped the thread of its QoS class.
+ */
+ error = EIDRM;
+ break;
+ }
+ /* otherwise, fall through to the error case. */
+ default:
+ error = EINVAL;
+ goto out;
+ }
+ }
+ break;
+
+ case IOPOL_SCOPE_DARWIN_BG:
+ thread = THREAD_NULL;
+ policy_flavor = TASK_POLICY_DARWIN_BG_IOPOL;
+ break;
+
+ default:
+ error = EINVAL;
+ goto out;
+ }
+
+ /* Validate policy */
+ if (cmd == IOPOL_CMD_SET) {
+ switch (policy) {
+ case IOPOL_DEFAULT:
+ if (scope == IOPOL_SCOPE_DARWIN_BG) {
+ /* the current default BG throttle level is UTILITY */
+ policy = IOPOL_UTILITY;
+ } else {
+ policy = IOPOL_IMPORTANT;
+ }
+ break;
+ case IOPOL_UTILITY:
+ /* fall-through */
+ case IOPOL_THROTTLE:
+ /* These levels are OK */
+ break;
+ case IOPOL_IMPORTANT:
+ /* fall-through */
+ case IOPOL_STANDARD:
+ /* fall-through */
+ case IOPOL_PASSIVE:
+ if (scope == IOPOL_SCOPE_DARWIN_BG) {
+ /* These levels are invalid for BG */
+ error = EINVAL;
+ goto out;
+ } else {
+ /* OK for other scopes */
+ }
+ break;
+ default:
+ error = EINVAL;
+ goto out;
+ }
+ }
+
+ /* Perform command */
+ switch(cmd) {
+ case IOPOL_CMD_SET:
+ if (thread != THREAD_NULL)
+ proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor, policy);
+ else
+ proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor, policy);
+ break;
+ case IOPOL_CMD_GET:
+ if (thread != THREAD_NULL)
+ policy = proc_get_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor);
+ else
+ policy = proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor);
+ iop_param->iop_policy = policy;
+ break;
+ default:
+ error = EINVAL; /* unknown command */
+ break;
+ }
+
+out:
+ return (error);
+}
+
+static int
+iopolicysys_vfs(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
+{
+ int error = 0;
+
+ /* Validate scope */
+ switch (scope) {
+ case IOPOL_SCOPE_PROCESS:
+ /* Only process OK */
+ break;
+ default:
+ error = EINVAL;
+ goto out;
+ }
+
+ /* Validate policy */
+ if (cmd == IOPOL_CMD_SET) {
+ switch (policy) {
+ case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
+ /* fall-through */
+ case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
+ /* These policies are OK */
+ break;
+ default:
+ error = EINVAL;
+ goto out;
+ }
+ }
+
+ /* Perform command */
+ switch(cmd) {
+ case IOPOL_CMD_SET:
+ if (0 == kauth_cred_issuser(kauth_cred_get())) {
+ /* If it's a non-root process, it needs to have the entitlement to set the policy */
+ boolean_t entitled = FALSE;
+ entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
+ if (!entitled) {
+ error = EPERM;
+ goto out;
+ }
+ }
+
+ switch (policy) {
+ case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
+ OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY), &p->p_vfs_iopolicy);
+ break;
+ case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
+ OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY, &p->p_vfs_iopolicy);
+ break;
+ default:
+ error = EINVAL;
+ goto out;
+ }
+
+ break;
+ case IOPOL_CMD_GET:
+ iop_param->iop_policy = (p->p_vfs_iopolicy & P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY)
+ ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
+ : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT;
+ break;
+ default:
+ error = EINVAL; /* unknown command */
+ break;
+ }
+
+out:
+ return (error);
+}
+
+/* BSD call back function for task_policy networking changes */
+void
+proc_apply_task_networkbg(void * bsd_info, thread_t thread)
+{
+ assert(bsd_info != PROC_NULL);
+
+ pid_t pid = proc_pid((proc_t)bsd_info);
+
+ proc_t p = proc_find(pid);
+
+ if (p != PROC_NULL) {
+ assert(p == (proc_t)bsd_info);
+
+ do_background_socket(p, thread);
+ proc_rele(p);
+ }
+}
+
+void
+gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor)
+{
+ struct rusage_info_child *ri_child;
+
+ assert(p->p_stats != NULL);
+ switch(flavor) {
+
+ case RUSAGE_INFO_V3:
+ fill_task_qos_rusage(p->task, ru);
+ fill_task_billed_usage(p->task, ru);
+ /* fall through */
+
+ case RUSAGE_INFO_V2:
+ fill_task_io_rusage(p->task, ru);
+ /* fall through */
+
+ case RUSAGE_INFO_V1:
+ /*
+ * p->p_stats->ri_child statistics are protected under proc lock.
+ */
+ proc_lock(p);
+
+ ri_child = &(p->p_stats->ri_child);
+ ru->ri_child_user_time = ri_child->ri_child_user_time;
+ ru->ri_child_system_time = ri_child->ri_child_system_time;
+ ru->ri_child_pkg_idle_wkups = ri_child->ri_child_pkg_idle_wkups;
+ ru->ri_child_interrupt_wkups = ri_child->ri_child_interrupt_wkups;
+ ru->ri_child_pageins = ri_child->ri_child_pageins;
+ ru->ri_child_elapsed_abstime = ri_child->ri_child_elapsed_abstime;
+
+ proc_unlock(p);
+ /* fall through */
+
+ case RUSAGE_INFO_V0:
+ proc_getexecutableuuid(p, (unsigned char *)&ru->ri_uuid, sizeof (ru->ri_uuid));
+ fill_task_rusage(p->task, ru);
+ ru->ri_proc_start_abstime = p->p_stats->ps_start;
+ }
+}
+
+static void
+rusage_info_conversion(rusage_info_t ri_info, rusage_info_current *ri_current, int flavor)
+{
+ struct rusage_info_v0 *ri_v0;
+ struct rusage_info_v1 *ri_v1;
+ struct rusage_info_v2 *ri_v2;
+
+ switch (flavor) {
+
+ case RUSAGE_INFO_V2:
+ ri_v2 = (struct rusage_info_v2 *)ri_info;
+ ri_v2->ri_diskio_bytesread = ri_current->ri_diskio_bytesread;
+ ri_v2->ri_diskio_byteswritten = ri_current->ri_diskio_byteswritten;
+ /* fall through */
+
+ case RUSAGE_INFO_V1:
+ ri_v1 = (struct rusage_info_v1 *)ri_info;
+ ri_v1->ri_child_user_time = ri_current->ri_child_user_time;
+ ri_v1->ri_child_system_time = ri_current->ri_child_system_time;
+ ri_v1->ri_child_pkg_idle_wkups = ri_current->ri_child_pkg_idle_wkups;
+ ri_v1->ri_child_interrupt_wkups = ri_current->ri_child_interrupt_wkups;
+ ri_v1->ri_child_pageins = ri_current->ri_child_pageins;
+ ri_v1->ri_child_elapsed_abstime = ri_current->ri_child_elapsed_abstime;
+ /* fall through */
+
+ case RUSAGE_INFO_V0:
+ ri_v0 = (struct rusage_info_v0 *)ri_info;
+ memcpy(&ri_v0->ri_uuid[0], &ri_current->ri_uuid[0], sizeof(ri_v0->ri_uuid));
+ ri_v0->ri_user_time = ri_current->ri_user_time;
+ ri_v0->ri_system_time = ri_current->ri_system_time;
+ ri_v0->ri_pkg_idle_wkups = ri_current->ri_pkg_idle_wkups;
+ ri_v0->ri_interrupt_wkups = ri_current->ri_interrupt_wkups;
+ ri_v0->ri_pageins = ri_current->ri_pageins;
+ ri_v0->ri_wired_size = ri_current->ri_wired_size;
+ ri_v0->ri_resident_size = ri_current->ri_resident_size;
+ ri_v0->ri_phys_footprint = ri_current->ri_phys_footprint;
+ ri_v0->ri_proc_start_abstime = ri_current->ri_proc_start_abstime;
+ ri_v0->ri_proc_exit_abstime = ri_current->ri_proc_exit_abstime;
+
+ break;
+
+ default:
+ break;
+ }
+}
+
+
+int
+proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie)
+{
+ struct rusage_info_v0 ri_v0;
+ struct rusage_info_v1 ri_v1;
+ struct rusage_info_v2 ri_v2;
+ struct rusage_info_v3 ri_v3;
+
+ rusage_info_current ri_current;
+
+ int error = 0;
+
+ switch (flavor) {
+ case RUSAGE_INFO_V0:
+ /*
+ * If task is still alive, collect info from the live task itself.
+ * Otherwise, look to the cached info in the zombie proc.
+ */
+ if (p->p_ru == NULL) {
+ gather_rusage_info(p, &ri_current, flavor);
+ ri_current.ri_proc_exit_abstime = 0;
+ rusage_info_conversion(&ri_v0, &ri_current, flavor);
+ } else {
+ rusage_info_conversion(&ri_v0, &p->p_ru->ri, flavor);
+ }
+ error = copyout(&ri_v0, buffer, sizeof (ri_v0));
+ break;
+
+ case RUSAGE_INFO_V1:
+ /*
+ * If task is still alive, collect info from the live task itself.
+ * Otherwise, look to the cached info in the zombie proc.
+ */
+ if (p->p_ru == NULL) {
+ gather_rusage_info(p, &ri_current, flavor);
+ ri_current.ri_proc_exit_abstime = 0;
+ rusage_info_conversion(&ri_v1, &ri_current, flavor);
+ } else {
+ rusage_info_conversion(&ri_v1, &p->p_ru->ri, flavor);
+ }
+ error = copyout(&ri_v1, buffer, sizeof (ri_v1));
+ break;
+
+ case RUSAGE_INFO_V2:
+ /*
+ * If task is still alive, collect info from the live task itself.
+ * Otherwise, look to the cached info in the zombie proc.
+ */
+ if (p->p_ru == NULL) {
+ gather_rusage_info(p, &ri_current, flavor);
+ ri_current.ri_proc_exit_abstime = 0;
+ rusage_info_conversion(&ri_v2, &ri_current, flavor);
+ } else {
+ rusage_info_conversion(&ri_v2, &p->p_ru->ri, flavor);
+ }
+ error = copyout(&ri_v2, buffer, sizeof (ri_v2));
+ break;
+
+ case RUSAGE_INFO_V3:
+ /*
+ * If task is still alive, collect info from the live task itself.
+ * Otherwise, look to the cached info in the zombie proc.
+ */
+ if (p->p_ru == NULL) {
+ gather_rusage_info(p, &ri_v3, flavor);
+ ri_v3.ri_proc_exit_abstime = 0;
+ } else {
+ ri_v3 = p->p_ru->ri;
+ }
+ error = copyout(&ri_v3, buffer, sizeof (ri_v3));
+ break;
+
+ default:
+ error = EINVAL;
+ break;
+ }
+
+ return (error);
+}
+
+static int
+mach_to_bsd_rv(int mach_rv)
+{
+ int bsd_rv = 0;
+
+ switch (mach_rv) {
+ case KERN_SUCCESS:
+ bsd_rv = 0;
+ break;
+ case KERN_INVALID_ARGUMENT:
+ bsd_rv = EINVAL;
+ break;
+ default:
+ panic("unknown error %#x", mach_rv);
+ }
+
+ return bsd_rv;
+}
+
+/*
+ * Resource limit controls
+ *
+ * uap->flavor available flavors:
+ *
+ * RLIMIT_WAKEUPS_MONITOR
+ */
+int
+proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *uap, __unused int32_t *retval)
+{
+ proc_t targetp;
+ int error = 0;
+ struct proc_rlimit_control_wakeupmon wakeupmon_args;
+ uint32_t cpumon_flags;
+ uint32_t cpulimits_flags;
+ kauth_cred_t my_cred, target_cred;
+
+ /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
+ if (uap->pid == -1) {
+ targetp = proc_self();
+ } else {
+ targetp = proc_find(uap->pid);
+ }
+
+ /* proc_self() can return NULL for an exiting process */
+ if (targetp == PROC_NULL) {
+ return (ESRCH);
+ }
+
+ my_cred = kauth_cred_get();
+ target_cred = kauth_cred_proc_ref(targetp);
+
+ if (!kauth_cred_issuser(my_cred) && kauth_cred_getruid(my_cred) &&
+ kauth_cred_getuid(my_cred) != kauth_cred_getuid(target_cred) &&
+ kauth_cred_getruid(my_cred) != kauth_cred_getuid(target_cred)) {
+ proc_rele(targetp);
+ kauth_cred_unref(&target_cred);
+ return (EACCES);
+ }
+
+ switch (uap->flavor) {
+ case RLIMIT_WAKEUPS_MONITOR:
+ if ((error = copyin(uap->arg, &wakeupmon_args, sizeof (wakeupmon_args))) != 0) {
+ break;
+ }
+ if ((error = mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp->task, &wakeupmon_args.wm_flags,
+ &wakeupmon_args.wm_rate))) != 0) {
+ break;
+ }
+ error = copyout(&wakeupmon_args, uap->arg, sizeof (wakeupmon_args));
+ break;
+ case RLIMIT_CPU_USAGE_MONITOR:
+ cpumon_flags = uap->arg; // XXX temporarily stashing flags in argp (12592127)
+ error = mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp->task, &cpumon_flags));
+ break;
+ case RLIMIT_THREAD_CPULIMITS:
+ cpulimits_flags = (uint32_t)uap->arg; // only need a limited set of bits, pass in void * argument
+
+ if (uap->pid != -1) {
+ error = EINVAL;
+ break;
+ }
+
+ uint8_t percent = 0;
+ uint32_t ms_refill = 0;
+ uint64_t ns_refill;
+
+ percent = (uint8_t)(cpulimits_flags & 0xffU); /* low 8 bits for percent */
+ ms_refill = (cpulimits_flags >> 8) & 0xffffff; /* next 24 bits represent ms refill value */
+ if (percent >= 100) {
+ error = EINVAL;
+ break;
+ }
+
+ ns_refill = ((uint64_t)ms_refill) * NSEC_PER_MSEC;
+
+ error = mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK, percent, ns_refill));
+ break;
+ default:
+ error = EINVAL;
+ break;
+ }
+
+ proc_rele(targetp);
+ kauth_cred_unref(&target_cred);
+
+ /*
+ * Return value from this function becomes errno to userland caller.
+ */
+ return (error);
+}
+
+/*
+ * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
+ */
+int thread_selfusage(struct proc *p __unused, struct thread_selfusage_args *uap __unused, uint64_t *retval)
+{
+ uint64_t runtime;
+
+ runtime = thread_get_runtime_self();
+ *retval = runtime;
+
+ return (0);