+
+/*
+ * 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.
+ */
+ (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, CPUMON_MAKE_FATAL)) != 0) {
+ /* Failed to set termination, back out the CPU monitor settings. */
+ (void)proc_disable_cpumon(pid);
+ }
+
+ return (ret);
+}
+