added option -t to test if it has the permissions to read /proc/PID/io files
Costa Tsaousis (ktsaou) committed
Feb 26, 2017 at 20:04 UTC
48757ca9daf3438bf10dce4c7efb1865200d6673
2 files changed
+43
-18
src/apps_plugin.c
+41
-18
@@ -2902,6 +2902,22 @@ static void send_charts_updates_to_netdata(struct target *root, const char *type
2902
// ----------------------------------------------------------------------------
2903
// parse command line arguments
2904
2905
+int check_proc_1_io() {
2906
+ int ret = 0;
2907
+
2908
+ procfile *ff = procfile_open("/proc/1/io", NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
2909
+ if(!ff) goto cleanup;
2910
+
2911
+ ff = procfile_readall(ff);
2912
+ if(!ff) goto cleanup;
2913
+
2914
+ ret = 1;
2915
+
2916
+cleanup:
2917
+ procfile_close(ff);
2918
+ return ret;
2919
+}
2920
+
2921
static void parse_args(int argc, char **argv)
2922
{
2923
int i, freq = 0;
@@ -2921,6 +2937,15 @@ static void parse_args(int argc, char **argv)
2937
exit(0);
2938
}
2939
2940
+ if(strcmp("test-permissions", argv[i]) == 0 || strcmp("-t", argv[i]) == 0) {
2941
+ if(!check_proc_1_io()) {
2942
+ perror("Tried to read /proc/1/io and it failed");
2943
+ exit(1);
2944
+ }
2945
+ printf("OK\n");
2946
+ exit(0);
2947
+ }
2948
+
2949
if(strcmp("debug", argv[i]) == 0) {
2950
debug = 1;
2951
// debug_flags = 0xffffffff;
@@ -3093,8 +3118,6 @@ int main(int argc, char **argv) {
3118
// set the name for logging
3119
program_name = "apps.plugin";
3120
3096
- info("started on pid %d", getpid());
3097
-
3121
// disable syslog for apps.plugin
3122
error_log_syslog = 0;
3123
@@ -3136,27 +3159,27 @@ int main(int argc, char **argv) {
3159
3160
parse_args(argc, argv);
3161
3139
- if(!check_capabilities()) {
3140
- if(!am_i_running_as_root()) {
3141
- uid_t uid = getuid(), euid = geteuid();
3162
+ if(!check_capabilities() && !am_i_running_as_root() && !check_proc_1_io()) {
3163
+ uid_t uid = getuid(), euid = geteuid();
3164
#ifdef HAVE_CAPABILITY
3143
- error("apps.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
3144
- "Without these, apps.plugin cannot report disk I/O utilization of other processes. "
3145
- "To enable capabilities run: sudo setcap cap_dac_read_search,cap_sys_ptrace+ep %s; "
3146
- "To enable setuid to root run: sudo chown root %s; sudo chmod 4755 %s; "
3147
- , uid, euid, argv[0], argv[0], argv[0]
3148
- );
3165
+ error("apps.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
3166
+ "Without these, apps.plugin cannot report disk I/O utilization of other processes. "
3167
+ "To enable capabilities run: sudo setcap cap_dac_read_search,cap_sys_ptrace+ep %s; "
3168
+ "To enable setuid to root run: sudo chown root %s; sudo chmod 4755 %s; "
3169
+ , uid, euid, argv[0], argv[0], argv[0]
3170
+ );
3171
#else
3150
- error("apps.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
3151
- "Without these, apps.plugin cannot report disk I/O utilization of other processes. "
3152
- "Your system does not support capabilities. "
3153
- "To enable setuid to root run: sudo chown root %s; sudo chmod 4755 %s; "
3154
- , uid, euid, argv[0], argv[0]
3155
- );
3172
+ error("apps.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
3173
+ "Without these, apps.plugin cannot report disk I/O utilization of other processes. "
3174
+ "Your system does not support capabilities. "
3175
+ "To enable setuid to root run: sudo chown root %s; sudo chmod 4755 %s; "
3176
+ , uid, euid, argv[0], argv[0]
3177
+ );
3178
#endif
3157
- }
3179
}
3180
3181
+ info("started on pid %d", getpid());
3182
+
3183
all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
3184
all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
3185
src/procfile.c
+2
@@ -128,6 +128,8 @@ static inline void pflines_free(pflines *fl) {
128
// The procfile
129
130
void procfile_close(procfile *ff) {
131
+ if(unlikely(!ff)) return;
132
+
133
debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", procfile_filename(ff));
134
135
if(likely(ff->lines)) pflines_free(ff->lines);