chown /var/lib/netdata and /var/cache/netdata only if we created them; chown the pidfile too when started as root; #1204
Costa Tsaousis (ktsaou) committed
Nov 4, 2016 at 00:13 UTC
5b479f02ed6c7ce098d2a61df0716631976c5b5b
1 file changed
+15
-4
src/daemon.c
+15
-4
@@ -60,11 +60,17 @@ static void chown_open_file(int fd, uid_t uid, gid_t gid) {
60
61
void create_needed_dir(const char *dir, uid_t uid, gid_t gid)
62
{
63
- if(mkdir(dir, 0755) == -1 && errno != EEXIST)
64
- error("Cannot create directory '%s'", dir);
63
+ // attempt to create the directory
64
+ if(mkdir(dir, 0755) == 0) {
65
+ // we created it
66
66
- if(chown(dir, uid, gid) == -1)
67
- error("Cannot chown directory '%s' to %u:%u", dir, (unsigned int)uid, (unsigned int)gid);
67
+ // chown it to match the required user
68
+ if(chown(dir, uid, gid) == -1)
69
+ error("Cannot chown directory '%s' to %u:%u", dir, (unsigned int)uid, (unsigned int)gid);
70
+ }
71
+ else if(errno != EEXIST)
72
+ // log an error only if the directory does not exist
73
+ error("Cannot create directory '%s'", dir);
74
}
75
76
int become_user(const char *username, int pid_fd)
@@ -81,6 +87,11 @@ int become_user(const char *username, int pid_fd)
87
create_needed_dir(CACHE_DIR, uid, gid);
88
create_needed_dir(VARLIB_DIR, uid, gid);
89
90
+ if(pidfile[0]) {
91
+ if(chown(pidfile, uid, gid) == -1)
92
+ error("Cannot chown '%s' to %u:%u", pidfile, (unsigned int)uid, (unsigned int)gid);
93
+ }
94
+
95
int ngroups = (int)sysconf(_SC_NGROUPS_MAX);
96
gid_t *supplementary_groups = NULL;
97
if(ngroups) {