+static int
+do_background_proc(struct proc *curp, struct proc *targetp, int priority)
+{
+ int error = 0;
+ kauth_cred_t ucred;
+ kauth_cred_t target_cred;
+#if CONFIG_EMBEDDED
+ task_category_policy_data_t info;
+#endif
+
+ ucred = kauth_cred_get();
+ target_cred = kauth_cred_proc_ref(targetp);
+
+ if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
+ kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
+ kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred))
+ {
+ error = EPERM;
+ goto out;
+ }
+
+#if CONFIG_MACF
+ error = mac_proc_check_sched(curp, targetp);
+ if (error)
+ goto out;
+#endif
+
+#if !CONFIG_EMBEDDED
+ if (priority == PRIO_DARWIN_NONUI)
+ error = proc_apply_task_gpuacc(targetp->task, TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_NOACCESS);
+ else
+ error = proc_set1_bgtaskpolicy(targetp->task, priority);
+ if (error)
+ goto out;
+#else /* !CONFIG_EMBEDDED */
+
+ /* set the max scheduling priority on the task */
+ if (priority == PRIO_DARWIN_BG) {
+ info.role = TASK_THROTTLE_APPLICATION;
+ }
+ else if (priority == PRIO_DARWIN_NONUI) {
+ info.role = TASK_NONUI_APPLICATION;
+ }
+ else {
+ info.role = TASK_DEFAULT_APPLICATION;
+ }
+
+ error = task_policy_set(targetp->task,
+ TASK_CATEGORY_POLICY,
+ (task_policy_t) &info,
+ TASK_CATEGORY_POLICY_COUNT);
+
+ if (error)
+ goto out;
+
+ proc_lock(targetp);
+
+ /* mark proc structure as backgrounded */
+ if (priority == PRIO_DARWIN_BG) {
+ targetp->p_lflag |= P_LBACKGROUND;
+ } else {
+ targetp->p_lflag &= ~P_LBACKGROUND;
+ }
+
+ /* set or reset the disk I/O priority */
+ targetp->p_iopol_disk = (priority == PRIO_DARWIN_BG ?
+ IOPOL_THROTTLE : IOPOL_DEFAULT);
+
+ proc_unlock(targetp);
+#endif /* !CONFIG_EMBEDDED */
+
+out:
+ kauth_cred_unref(&target_cred);
+ return (error);
+}
+
+static void
+do_background_socket(struct proc *p, thread_t thread, int priority)
+{
+ struct filedesc *fdp;
+ struct fileproc *fp;
+ int i;
+
+ if (priority == PRIO_DARWIN_BG) {
+ /*
+ * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
+ * the sockets with the background flag. There's nothing
+ * to do here for the PRIO_DARWIN_THREAD case.
+ */
+ if (thread == NULL) {
+ proc_fdlock(p);
+ fdp = p->p_fd;
+
+ for (i = 0; i < fdp->fd_nfiles; i++) {
+ struct socket *sockp;
+
+ fp = fdp->fd_ofiles[i];
+ if (fp == NULL || (fdp->fd_ofileflags[i] & UF_RESERVED) != 0 ||
+ fp->f_fglob->fg_type != DTYPE_SOCKET) {
+ continue;
+ }
+ sockp = (struct socket *)fp->f_fglob->fg_data;
+ socket_set_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
+ sockp->so_background_thread = NULL;
+ }
+ proc_fdunlock(p);
+ }
+
+ } else {
+
+ /* disable networking IO throttle.
+ * NOTE - It is a known limitation of the current design that we
+ * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
+ * sockets created by other threads within this process.
+ */
+ proc_fdlock(p);
+ fdp = p->p_fd;
+ for ( i = 0; i < fdp->fd_nfiles; i++ ) {
+ struct socket *sockp;
+
+ fp = fdp->fd_ofiles[ i ];
+ if ( fp == NULL || (fdp->fd_ofileflags[ i ] & UF_RESERVED) != 0 ||
+ fp->f_fglob->fg_type != DTYPE_SOCKET ) {
+ continue;
+ }
+ sockp = (struct socket *)fp->f_fglob->fg_data;
+ /* skip if only clearing this thread's sockets */
+ if ((thread) && (sockp->so_background_thread != thread)) {
+ continue;
+ }
+ socket_clear_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
+ sockp->so_background_thread = NULL;
+ }
+ proc_fdunlock(p);
+ }
+}
+
+
+/*
+ * do_background_thread
+ * Returns: 0 Success
+ * XXX - todo - does this need a MACF hook?
+ *
+ * NOTE: To maintain binary compatibility with PRIO_DARWIN_THREAD with respect
+ * to network traffic management, UT_BACKGROUND_TRAFFIC_MGT is set/cleared
+ * along with UT_BACKGROUND flag, as the latter alone no longer implies
+ * any form of traffic regulation (it simply means that the thread is
+ * background.) With PRIO_DARWIN_PROCESS, any form of network traffic
+ * management must be explicitly requested via whatever means appropriate,
+ * and only TRAFFIC_MGT_SO_BACKGROUND is set via do_background_socket().
+ */
+static int
+do_background_thread(struct proc *curp __unused, thread_t thread, int priority)
+{
+ struct uthread *ut;
+#if !CONFIG_EMBEDDED
+ int error = 0;
+#else /* !CONFIG_EMBEDDED */
+ thread_precedence_policy_data_t policy;
+#endif /* !CONFIG_EMBEDDED */
+
+ ut = get_bsdthread_info(thread);
+
+ /* Backgrounding is unsupported for threads in vfork */
+ if ( (ut->uu_flag & UT_VFORK) != 0) {
+ return(EPERM);
+ }
+
+#if !CONFIG_EMBEDDED
+ error = proc_set1_bgthreadpolicy(curp->task, thread_tid(thread), priority);
+ return(error);
+#else /* !CONFIG_EMBEDDED */
+ if ( (priority & PRIO_DARWIN_BG) == 0 ) {
+ /* turn off backgrounding of thread */
+ if ( (ut->uu_flag & UT_BACKGROUND) == 0 ) {
+ /* already off */
+ return(0);
+ }
+
+ /*
+ * Clear background bit in thread and disable disk IO
+ * throttle as well as network traffic management.
+ * The corresponding socket flags for sockets created by
+ * this thread will be cleared in do_background_socket().
+ */
+ ut->uu_flag &= ~(UT_BACKGROUND | UT_BACKGROUND_TRAFFIC_MGT);
+ ut->uu_iopol_disk = IOPOL_NORMAL;
+
+ /* reset thread priority (we did not save previous value) */
+ policy.importance = 0;
+ thread_policy_set( thread, THREAD_PRECEDENCE_POLICY,
+ (thread_policy_t)&policy,
+ THREAD_PRECEDENCE_POLICY_COUNT );
+ return(0);
+ }
+
+ /* background this thread */
+ if ( (ut->uu_flag & UT_BACKGROUND) != 0 ) {
+ /* already backgrounded */
+ return(0);
+ }
+
+ /*
+ * Tag thread as background and throttle disk IO, as well
+ * as regulate network traffics. Future sockets created
+ * by this thread will have their corresponding socket
+ * flags set at socket create time.
+ */
+ ut->uu_flag |= (UT_BACKGROUND | UT_BACKGROUND_TRAFFIC_MGT);
+ ut->uu_iopol_disk = IOPOL_THROTTLE;
+
+ policy.importance = INT_MIN;
+ thread_policy_set( thread, THREAD_PRECEDENCE_POLICY,
+ (thread_policy_t)&policy,
+ THREAD_PRECEDENCE_POLICY_COUNT );
+
+ /* throttle networking IO happens in socket( ) syscall.
+ * If UT_{BACKGROUND,BACKGROUND_TRAFFIC_MGT} is set in the current
+ * thread then TRAFFIC_MGT_SO_{BACKGROUND,BG_REGULATE} is set.
+ * Existing sockets are taken care of by do_background_socket().
+ */
+#endif /* !CONFIG_EMBEDDED */
+ return(0);
+}
+
+#if CONFIG_EMBEDDED
+int mach_do_background_thread(thread_t thread, int prio);
+