Turbostat plugin: relax dependency on root
[collectd.git] / src / turbostat.c
index c767cf1..cd2cf43 100644 (file)
@@ -57,6 +57,7 @@
 #include <ctype.h>
 #include <sched.h>
 #include <cpuid.h>
+#include <sys/capability.h>
 
 #define PLUGIN_NAME "turbostat"
 
@@ -145,10 +146,10 @@ static double rapl_energy_units;
                                        /* 0x642 MSR_PP1_POLICY */
 #define        TJMAX_DEFAULT   100
 
-cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
-size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
+static cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
+static size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
 
-struct thread_data {
+static struct thread_data {
        unsigned long long tsc;
        unsigned long long aperf;
        unsigned long long mperf;
@@ -160,7 +161,7 @@ struct thread_data {
 #define CPU_IS_FIRST_CORE_IN_PACKAGE   0x4
 } *thread_delta, *thread_even, *thread_odd;
 
-struct core_data {
+static struct core_data {
        unsigned long long c3;
        unsigned long long c6;
        unsigned long long c7;
@@ -168,7 +169,7 @@ struct core_data {
        unsigned int core_id;
 } *core_delta, *core_even, *core_odd;
 
-struct pkg_data {
+static struct pkg_data {
        unsigned long long pc2;
        unsigned long long pc3;
        unsigned long long pc6;
@@ -213,7 +214,7 @@ struct cpu_topology {
        _Bool first_thread_in_core;
 };
 
-struct topology {
+static struct topology {
        int max_cpu_id;
        int num_packages;
        int num_cores;
@@ -221,7 +222,7 @@ struct topology {
        struct cpu_topology *cpus;
 } topology;
 
-cdtime_t time_even, time_odd, time_delta;
+static cdtime_t time_even, time_odd, time_delta;
 
 static const char *config_keys[] =
 {
@@ -1485,19 +1486,56 @@ out:
 }
 
 static int
-turbostat_init(void)
+check_permissions(void)
 {
-       struct stat sb;
-       int ret;
+       struct __user_cap_header_struct cap_header_data;
+       cap_user_header_t cap_header = &cap_header_data;
+       struct __user_cap_data_struct cap_data_data;
+       cap_user_data_t cap_data = &cap_data_data;
+       int ret = 0;
+
+       if (getuid() == 0) {
+               /* We have everything we need */
+               return 0;
+       }
 
-       if (getuid() != 0) {
-               ERROR("Turbostat plugin: Initialization failed: this plugin "
-                     "requires collectd to run as root in order to read "
-                     "special CPU registers");
+       /* check for CAP_SYS_RAWIO */
+       cap_header->pid = getpid();
+       cap_header->version = _LINUX_CAPABILITY_VERSION;
+       if (capget(cap_header, cap_data) < 0) {
+               ERROR("Turbostat plugin: capget failed");
                return -1;
        }
 
-       DO_OR_GOTO_ERR(probe_cpu());
+       if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
+               WARNING("Turbostat plugin: Collectd doesn't have the "
+                       "CAP_SYS_RAWIO capability. If you don't want to run "
+                       "collectd as root, try running \"setcap "
+                       "cap_sys_rawio=ep\" on collectd binary");
+               ret = -1;
+       }
+
+       if (euidaccess("/dev/cpu/0/msr", R_OK)) {
+               WARNING("Turbostat plugin: Collectd cannot open"
+                       "/dev/cpu/0/msr. If you don't want to run collectd as "
+                       "root, you need to change the ownership (chown) and "
+                       "permissions on /dev/cpu/*/msr to allow such access");
+               ret = -1;
+       }
+
+       if (ret != 0)
+               ERROR("Turbostat plugin: Initialization failed: this plugin "
+                     "requires collectd to either to run as root or give "
+                     "collectd a special capability (CAP_SYS_RAWIO) and read "
+                      "access to /dev/cpu/*/msr (see previous warnings)");
+       return ret;
+}
+
+static int
+turbostat_init(void)
+{
+       struct stat sb;
+       int ret;
 
        if (stat("/dev/cpu/0/msr", &sb)) {
                ERROR("Turbostat plugin: Initialization failed: /dev/cpu/0/msr"
@@ -1507,6 +1545,10 @@ turbostat_init(void)
                return -1;
        }
 
+       DO_OR_GOTO_ERR(check_permissions());
+
+       DO_OR_GOTO_ERR(probe_cpu());
+
        DO_OR_GOTO_ERR(setup_all_buffers());
 
        plugin_register_read(PLUGIN_NAME, turbostat_read);