added pid to /run/netdata/netdata.pid
Costa Tsaousis (ktsaou) committed
Dec 7, 2014 at 04:53 UTC
34f3851488fe475373b27807eaf66930300f7f4e
1 file changed
+27
-4
netdata.c
+27
-4
@@ -7090,6 +7090,22 @@ void sig_handler(int signo)
7090
}
7091
}
7092
7093
+char rundir[FILENAME_MAX + 1] = "/run/netdata";
7094
+char pidfile[FILENAME_MAX + 1] = "";
7095
+void prepare_rundir() {
7096
+ if(getuid() != 0) {
7097
+ mkdir("/run/user", 0775);
7098
+ snprintf(rundir, FILENAME_MAX, "/run/user/%d", getpid());
7099
+ mkdir(rundir, 0775);
7100
+ snprintf(rundir, FILENAME_MAX, "/run/user/%d/netdata", getpid());
7101
+ }
7102
+
7103
+ snprintf(pidfile, FILENAME_MAX, "%s/netdata.pid", rundir);
7104
+
7105
+ if(mkdir(rundir, 0775) != 0)
7106
+ fprintf(stderr, "Cannot create directory '%s' (%s).", rundir, strerror(errno));
7107
+}
7108
+
7109
int become_user(const char *username)
7110
{
7111
struct passwd *pw = getpwnam(username);
@@ -7097,6 +7113,12 @@ int become_user(const char *username)
7113
fprintf(stderr, "User %s is not present. Error: %s\n", username, strerror(errno));
7114
return -1;
7115
}
7116
+
7117
+ if(chown(rundir, pw->pw_uid, pw->pw_gid) != 0) {
7118
+ fprintf(stderr, "Cannot chown directory '%s' to user %s. Error: %s\n", rundir, username, strerror(errno));
7119
+ return -1;
7120
+ }
7121
+
7122
if(setgid(pw->pw_gid) != 0) {
7123
fprintf(stderr, "Cannot switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
7124
return -1;
@@ -7276,10 +7298,10 @@ int become_daemon(int close_all_files, const char *input, const char *output, co
7298
if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
7299
close(dev_null);
7300
7279
-/* // generate our pid file
7301
+ // generate our pid file
7302
{
7281
- unlink("/var/run/netdata.pid");
7282
- int fd = open("/var/run/netdata.pid", O_RDWR | O_CREAT, 0666);
7303
+ unlink(pidfile);
7304
+ int fd = open(pidfile, O_RDWR | O_CREAT, 0666);
7305
if(fd >= 0) {
7306
char b[100];
7307
sprintf(b, "%d\n", getpid());
@@ -7287,7 +7309,7 @@ int become_daemon(int close_all_files, const char *input, const char *output, co
7309
close(fd);
7310
}
7311
}
7290
-*/
7312
+
7313
return(0);
7314
}
7315
@@ -7561,6 +7583,7 @@ int main(int argc, char **argv)
7583
7584
// --------------------------------------------------------------------
7585
7586
+ prepare_rundir();
7587
char *user = config_get("global", "run as user", (getuid() == 0)?"nobody":"");
7588
if(*user) {
7589
if(become_user(user) != 0) {