+ return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
+ PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
+}
+
+
+/*
+ * Turn on the CPU usage monitor using the supplied parameters, and make
+ * violations of the monitor fatal.
+ *
+ * Returns: 0 on success;
+ * -1 on failure and sets errno
+ */
+int
+proc_set_cpumon_params_fatal(pid_t pid, int percentage, int interval)
+{
+ int current_percentage = 0;
+ int current_interval = 0; /* intervals are in seconds */
+ int ret = 0;
+
+ if ((percentage <= 0) || (interval <= 0)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ /*
+ * Do a simple query to see if CPU monitoring is
+ * already active. If either the percentage or the
+ * interval is nonzero, then CPU monitoring is
+ * already in use for this process.
+ *
+ * XXX: need set...() and set..fatal() to behave similarly.
+ * Currently, this check prevents 1st party apps (which get a
+ * default non-fatal monitor) not to get a fatal monitor.
+ */
+ (void)proc_get_cpumon_params(pid, ¤t_percentage, ¤t_interval);
+ if (current_percentage || current_interval) {
+ /*
+ * The CPU monitor appears to be active.
+ * We choose not to disturb those settings.
+ */
+ errno = EBUSY;
+ return -1;
+ }
+
+ if ((ret = proc_set_cpumon_params(pid, percentage, interval)) != 0) {
+ /* Failed to activate the CPU monitor */
+ return ret;
+ }
+
+ if ((ret = proc_rlimit_control(pid, RLIMIT_CPU_USAGE_MONITOR, (void *)(uintptr_t)CPUMON_MAKE_FATAL)) != 0) {
+ /* Failed to set termination, back out the CPU monitor settings. */
+ (void)proc_disable_cpumon(pid);
+ }
+
+ return ret;