+/*
+ * Get the darwin background state of the originator. If the current
+ * process app type is App, then it is the originator, else if it is
+ * a Daemon, then creator of the Resource Accounting attribute of
+ * the current thread voucher is the originator of the work.
+ */
+int
+proc_get_originatorbgstate(uint32_t *is_backgrounded)
+{
+ uint32_t bgstate;
+ proc_t p = current_proc();
+ uint32_t flagsp;
+ kern_return_t kr;
+ pid_t pid;
+ int ret;
+ thread_t thread = current_thread();
+
+ bgstate = proc_get_effective_thread_policy(thread, TASK_POLICY_DARWIN_BG);
+
+ /* If current thread or task backgrounded, return background */
+ if (bgstate) {
+ *is_backgrounded = 1;
+ return 0;
+ }
+
+ /* Check if current process app type is App, then return foreground */
+ proc_get_darwinbgstate(p->task, &flagsp);
+ if ((flagsp & PROC_FLAG_APPLICATION) == PROC_FLAG_APPLICATION) {
+ *is_backgrounded = 0;
+ return 0;
+ }
+
+ /*
+ * Get the current voucher origin pid and it's bgstate.The pid
+ * returned here might not be valid or may have been recycled.
+ */
+ kr = thread_get_current_voucher_origin_pid(&pid);
+ if (kr != KERN_SUCCESS) {
+ if (kr == KERN_INVALID_TASK)
+ return ESRCH;
+ else if (kr == KERN_INVALID_VALUE)
+ return ENOATTR;
+ else
+ return EINVAL;
+ }
+
+ ret = proc_pidbackgrounded(pid, is_backgrounded);
+ return ret;
+}
+