@cryptotaxi247 / netdata-1 / commits / 27104d717

make log files owned by user netdata, fixes #453

Costa Tsaousis committed May 26, 2016 at 10:28 UTC 27104d717db7c1e28ce0b952a3126ee8c30c0534
3 files changed +31 -34
src/daemon.c
+30 -24
@@ -34,7 +34,7 @@ void sig_handler(int signo)
34 netdata_exit = 1;
35 }
36
37 -int become_user(const char *username)
37 +int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
38 {
39 struct passwd *pw = getpwnam(username);
40 if(!pw) {
@@ -60,21 +60,26 @@ int become_user(const char *username)
60 else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
61 }
62
63 - if(pidfile[0] && getuid() != uid) {
64 - // we are dropping privileges
65 - if(chown(pidfile, uid, gid) != 0)
66 - error("Cannot chown pidfile '%s' to user '%s'", pidfile, username);
63 + if(getuid() != uid || getgid() != gid) {
64 + if (access_fd != -1) {
65 + if (fchown(access_fd, uid, gid) == -1)
66 + error("Cannot set the ownership of access log file.");
67 + }
68
68 - else if(pidfd != -1) {
69 - // not need to keep it open
70 - close(pidfd);
71 - pidfd = -1;
69 + if (output_fd != -1) {
70 + if (fchown(output_fd, uid, gid) == -1)
71 + error("Cannot set the ownership of output log file.");
72 + }
73 +
74 + if (error_fd != -1) {
75 + if (fchown(error_fd, uid, gid) == -1)
76 + error("Cannot set the ownership of error log file.");
77 + }
78 +
79 + if (pid_fd != -1) {
80 + if(fchown(pid_fd, uid, gid) != 0)
81 + error("Cannot set the ownership of pid file.");
82 }
73 - }
74 - else if(pidfd != -1) {
75 - // not need to keep it open
76 - close(pidfd);
77 - pidfd = -1;
83 }
84
85 if(supplementary_groups && ngroups) {
@@ -130,7 +135,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
135 }
136 }
137
133 - if(output && *output) {
138 + if(output && *output && strcmp(output, "/dev/null") != 0) {
139 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
140 error("Cannot open output log file '%s'", output);
141 if(input_fd != -1) close(input_fd);
@@ -138,7 +143,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
143 }
144 }
145
141 - if(error && *error) {
146 + if(error && *error && strcmp(error, "/dev/null") != 0) {
147 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
148 error("Cannot open error log file '%s'.", error);
149 if(input_fd != -1) close(input_fd);
@@ -147,7 +152,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
152 }
153 }
154
150 - if(access && *access && access_fd) {
155 + if(access && *access && access_fd && strcmp(access, "/dev/null") != 0) {
156 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
157 error("Cannot open access log file '%s'", access);
158 if(input_fd != -1) close(input_fd);
@@ -262,7 +267,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
267 if(setvbuf(stdout, NULL, _IOLBF, 0) != 0)
268 error("Cannot set line buffering on debug.log");
269
265 - output_fd = -1;
270 + output_fd = STDOUT_FILENO;
271 }
272 else dup2(dev_null, STDOUT_FILENO);
273
@@ -275,7 +280,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
280 if(setvbuf(stderr, NULL, _IOLBF, 0) != 0)
281 error("Cannot set line buffering on error.log");
282
278 - error_fd = -1;
283 + error_fd = STDERR_FILENO;
284 }
285 else dup2(dev_null, STDERR_FILENO);
286
@@ -284,6 +289,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
289 close(dev_null);
290
291 // generate our pid file
292 + int pidfd = -1;
293 if(pidfile[0]) {
294 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
295 if(pidfd >= 0) {
@@ -295,21 +301,21 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
301 ssize_t i = write(pidfd, b, strlen(b));
302 if(i <= 0)
303 error("Cannot write pidfile '%s'.", pidfile);
298 -
299 - // don't close it, we might need it at exit
300 - // close(pidfd);
304 }
305 else error("Failed to open pidfile '%s'.", pidfile);
306 }
307
308 if(user && *user) {
306 - if(become_user(user) != 0) {
309 + if(become_user(user, (*access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
310 error("Cannot become user '%s'. Continuing as we are.", user);
311 }
312 else info("Successfully became user '%s'.", user);
313 }
311 - else if(pidfd != -1)
314 +
315 + if(pidfd != -1) {
316 close(pidfd);
317 + pidfd = -1;
318 + }
319
320 return(0);
321 }
src/daemon.h
+1 -2
@@ -3,13 +3,12 @@
3
4 extern void sig_handler(int signo);
5
6 -extern int become_user(const char *username);
6 +extern int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd);
7
8 extern int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp);
9
10 extern void netdata_cleanup_and_exit(int i);
11
12 extern char pidfile[];
13 -extern int pidfd;
13
14 #endif /* NETDATA_DAEMON_H */
src/main.c
-8
@@ -49,14 +49,6 @@ void netdata_cleanup_and_exit(int ret)
49 // let it log a few more error messages
50 error_log_limit_reset();
51
52 - if(pidfd != -1) {
53 - if(ftruncate(pidfd, 0) != 0)
54 - error("Cannot truncate pidfile '%s'.", pidfile);
55 -
56 - close(pidfd);
57 - pidfd = -1;
58 - }
59 -
52 if(pidfile[0]) {
53 if(unlink(pidfile) != 0)
54 error("Cannot unlink pidfile '%s'.", pidfile);