+ 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:
+ proc_set_task_policy(current_task(), thread,
+ TASK_POLICY_INTERNAL, policy_flavor,
+ policy);
+ break;
+ case IOPOL_CMD_GET:
+ policy = proc_get_task_policy(current_task(), thread,
+ 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 */
+void proc_apply_task_networkbg(void * bsd_info, thread_t thread);
+
+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;