+ /* 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);
+ memset(ru, 0, sizeof(*ru));
+ switch(flavor) {
+ case RUSAGE_INFO_V4:
+ ru->ri_logical_writes = get_task_logical_writes(p->task);
+ ru->ri_lifetime_max_phys_footprint = get_task_phys_footprint_lifetime_max(p->task);
+ fill_task_monotonic_rusage(p->task, ru);
+ /* fall through */
+
+ 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;
+ }
+}
+
+int
+proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie)
+{
+ rusage_info_current ri_current;
+
+ int error = 0;
+ size_t size = 0;
+
+ switch (flavor) {
+ case RUSAGE_INFO_V0:
+ size = sizeof(struct rusage_info_v0);
+ break;
+
+ case RUSAGE_INFO_V1:
+ size = sizeof(struct rusage_info_v1);
+ break;
+
+ case RUSAGE_INFO_V2:
+ size = sizeof(struct rusage_info_v2);
+ break;
+
+ case RUSAGE_INFO_V3:
+ size = sizeof(struct rusage_info_v3);