]> arthur.barton.de Git - netdata.git/blob - src/daemon.c
Merge pull request #457 from ktsaou/master
[netdata.git] / src / daemon.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <pwd.h>
13 #include <grp.h>
14 #include <pthread.h>
15 #include <sys/wait.h>
16 #include <sys/stat.h>
17
18 #include "common.h"
19 #include "appconfig.h"
20 #include "log.h"
21 #include "web_client.h"
22 #include "plugins_d.h"
23 #include "rrd.h"
24 #include "popen.h"
25 #include "main.h"
26 #include "daemon.h"
27
28 char pidfile[FILENAME_MAX + 1] = "";
29 int pidfd = -1;
30
31 void sig_handler(int signo)
32 {
33         if(signo)
34                 netdata_exit = 1;
35 }
36
37 static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
38         if(fd == -1) return;
39
40         struct stat buf;
41
42         if(fstat(fd, &buf) == -1) {
43                 error("Cannot fstat() fd %d", fd);
44                 return;
45         }
46
47         if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
48                 if(fchown(fd, uid, gid) == -1)
49                         error("Cannot fchown() fd %d.", fd);
50         }
51 }
52
53 int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
54 {
55         struct passwd *pw = getpwnam(username);
56         if(!pw) {
57                 error("User %s is not present.", username);
58                 return -1;
59         }
60
61         uid_t uid = pw->pw_uid;
62         gid_t gid = pw->pw_gid;
63
64         int ngroups =  sysconf(_SC_NGROUPS_MAX);
65         gid_t *supplementary_groups = NULL;
66         if(ngroups) {
67                 supplementary_groups = malloc(sizeof(gid_t) * ngroups);
68                 if(supplementary_groups) {
69                         if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
70                                 error("Cannot get supplementary groups of user '%s'.", username);
71                                 free(supplementary_groups);
72                                 supplementary_groups = NULL;
73                                 ngroups = 0;
74                         }
75                 }
76                 else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
77         }
78
79         properly_chown_netdata_generated_file(access_fd, uid, gid);
80         properly_chown_netdata_generated_file(output_fd, uid, gid);
81         properly_chown_netdata_generated_file(error_fd, uid, gid);
82         properly_chown_netdata_generated_file(pid_fd, uid, gid);
83
84         if(supplementary_groups && ngroups) {
85                 if(setgroups(ngroups, supplementary_groups) == -1)
86                         error("Cannot set supplementary groups for user '%s'", username);
87
88                 free(supplementary_groups);
89                 supplementary_groups = NULL;
90                 ngroups = 0;
91         }
92
93         if(setresgid(gid, gid, gid) != 0) {
94                 error("Cannot switch to user's %s group (gid: %d).", username, gid);
95                 return -1;
96         }
97
98         if(setresuid(uid, uid, uid) != 0) {
99                 error("Cannot switch to user %s (uid: %d).", username, uid);
100                 return -1;
101         }
102
103         if(setgid(gid) != 0) {
104                 error("Cannot switch to user's %s group (gid: %d).", username, gid);
105                 return -1;
106         }
107         if(setegid(gid) != 0) {
108                 error("Cannot effectively switch to user's %s group (gid: %d).", username, gid);
109                 return -1;
110         }
111         if(setuid(uid) != 0) {
112                 error("Cannot switch to user %s (uid: %d).", username, uid);
113                 return -1;
114         }
115         if(seteuid(uid) != 0) {
116                 error("Cannot effectively switch to user %s (uid: %d).", username, uid);
117                 return -1;
118         }
119
120         return(0);
121 }
122
123 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)
124 {
125         fflush(NULL);
126
127         // open the files before forking
128         int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
129
130         if(input && *input) {
131                 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
132                         error("Cannot open input file '%s'.", input);
133                         return -1;
134                 }
135         }
136
137         if(output && *output && strcmp(output, "/dev/null") != 0) {
138                 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
139                         error("Cannot open output log file '%s'", output);
140                         if(input_fd != -1) close(input_fd);
141                         return -1;
142                 }
143         }
144
145         if(error && *error && strcmp(error, "/dev/null") != 0) {
146                 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
147                         error("Cannot open error log file '%s'.", error);
148                         if(input_fd != -1) close(input_fd);
149                         if(output_fd != -1) close(output_fd);
150                         return -1;
151                 }
152         }
153
154         if(access && *access && access_fd && strcmp(access, "/dev/null") != 0) {
155                 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
156                         error("Cannot open access log file '%s'", access);
157                         if(input_fd != -1) close(input_fd);
158                         if(output_fd != -1) close(output_fd);
159                         if(error_fd != -1) close(error_fd);
160                         return -1;
161                 }
162
163                 if(access_fp) {
164                         *access_fp = fdopen(*access_fd, "w");
165                         if(!*access_fp) {
166                                 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
167                                 if(input_fd != -1) close(input_fd);
168                                 if(output_fd != -1) close(output_fd);
169                                 if(error_fd != -1) close(error_fd);
170                                 close(*access_fd);
171                                 *access_fd = -1;
172                                 return -1;
173                         }
174                         if(setvbuf(*access_fp, NULL, _IOLBF, 0) != 0)
175                                 error("Cannot set line buffering on access.log");
176                 }
177         }
178
179         if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
180                 perror("Cannot open /dev/null");
181                 if(input_fd != -1) close(input_fd);
182                 if(output_fd != -1) close(output_fd);
183                 if(error_fd != -1) close(error_fd);
184                 if(access && access_fd && *access_fd != -1) {
185                         close(*access_fd);
186                         *access_fd = -1;
187                         if(access_fp) {
188                                 fclose(*access_fp);
189                                 *access_fp = NULL;
190                         }
191                 }
192                 return -1;
193         }
194
195         // all files opened
196         // lets do it
197
198         if(!dont_fork) {
199                 int i = fork();
200                 if(i == -1) {
201                         perror("cannot fork");
202                         exit(1);
203                 }
204                 if(i != 0) {
205                         exit(0); // the parent
206                 }
207
208                 // become session leader
209                 if (setsid() < 0) {
210                         perror("Cannot become session leader.");
211                         exit(2);
212                 }
213         }
214
215         // fork() again
216         if(!dont_fork) {
217                 int i = fork();
218                 if(i == -1) {
219                         perror("cannot fork");
220                         exit(1);
221                 }
222                 if(i != 0) {
223                         exit(0); // the parent
224                 }
225         }
226
227         // Set new file permissions
228         umask(0);
229
230         // close all files
231         if(close_all_files) {
232                 int i;
233                 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
234                         if(
235                                 ((access_fd && i != *access_fd) || !access_fd)
236                                 && i != dev_null
237                                 && i != input_fd
238                                 && i != output_fd
239                                 && i != error_fd
240                                 && fd_is_valid(i)
241                                 ) close(i);
242         }
243         else {
244                 close(STDIN_FILENO);
245                 close(STDOUT_FILENO);
246                 close(STDERR_FILENO);
247         }
248
249         // put the opened files
250         // to our standard file descriptors
251         if(input_fd != -1) {
252                 if(input_fd != STDIN_FILENO) {
253                         dup2(input_fd, STDIN_FILENO);
254                         close(input_fd);
255                 }
256                 input_fd = -1;
257         }
258         else dup2(dev_null, STDIN_FILENO);
259
260         if(output_fd != -1) {
261                 if(output_fd != STDOUT_FILENO) {
262                         dup2(output_fd, STDOUT_FILENO);
263                         close(output_fd);
264                 }
265
266                 if(setvbuf(stdout, NULL, _IOLBF, 0) != 0)
267                         error("Cannot set line buffering on debug.log");
268
269                 output_fd = STDOUT_FILENO;
270         }
271         else dup2(dev_null, STDOUT_FILENO);
272
273         if(error_fd != -1) {
274                 if(error_fd != STDERR_FILENO) {
275                         dup2(error_fd, STDERR_FILENO);
276                         close(error_fd);
277                 }
278
279                 if(setvbuf(stderr, NULL, _IOLBF, 0) != 0)
280                         error("Cannot set line buffering on error.log");
281
282                 error_fd = STDERR_FILENO;
283         }
284         else dup2(dev_null, STDERR_FILENO);
285
286         // close /dev/null
287         if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
288                 close(dev_null);
289
290         // generate our pid file
291         int pidfd = -1;
292         if(pidfile[0]) {
293                 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
294                 if(pidfd >= 0) {
295                         if(ftruncate(pidfd, 0) != 0)
296                                 error("Cannot truncate pidfile '%s'.", pidfile);
297
298                         char b[100];
299                         sprintf(b, "%d\n", getpid());
300                         ssize_t i = write(pidfd, b, strlen(b));
301                         if(i <= 0)
302                                 error("Cannot write pidfile '%s'.", pidfile);
303                 }
304                 else error("Failed to open pidfile '%s'.", pidfile);
305         }
306
307         if(user && *user) {
308                 if(become_user(user, (*access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
309                         error("Cannot become user '%s'. Continuing as we are.", user);
310                 }
311                 else info("Successfully became user '%s'.", user);
312         }
313
314         if(pidfd != -1) {
315                 close(pidfd);
316                 pidfd = -1;
317         }
318
319         return(0);
320 }