]> arthur.barton.de Git - netdata.git/blob - src/daemon.c
83e00bb30fe5c21a1037987912c83aa6bf9b22b1
[netdata.git] / src / daemon.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <signal.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <pwd.h>
10 #include <pthread.h>
11 #include <sys/wait.h>
12 #include <sys/stat.h>
13 #include <execinfo.h>
14
15 #include "config.h"
16 #include "log.h"
17 #include "common.h"
18 #include "web_client.h"
19 #include "plugins_d.h"
20 #include "rrd.h"
21 #include "popen.h"
22 #include "main.h"
23 #include "daemon.h"
24
25 #define BACKTRACE_SIZE 4096
26
27 void print_backtrace()
28 {
29         void *buffer[BACKTRACE_SIZE];
30         int nptrs;
31
32         nptrs = backtrace(buffer, BACKTRACE_SIZE);
33         fprintf(stderr, "\n\nSTACK TRACE (%d addresses):\n\n", nptrs);
34         backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
35         fprintf(stderr, "\n\n");
36 }
37
38 void sig_handler(int signo)
39 {
40         switch(signo) {
41                 case SIGILL:
42                 case SIGABRT:
43                 case SIGFPE:
44                 case SIGSEGV:
45                 case SIGBUS:
46                 case SIGSYS:
47                 case SIGTRAP:
48                 case SIGXCPU:
49                 case SIGXFSZ:
50                         info("Death signaled exit (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
51                         print_backtrace();
52                         signal(signo, SIG_DFL);
53                         break;
54
55                 case SIGKILL:
56                 case SIGTERM:
57                 case SIGQUIT:
58                 case SIGINT:
59                 case SIGHUP:
60                 case SIGUSR1:
61                 case SIGUSR2:
62                         info("Signaled exit (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
63                         print_backtrace();
64                         signal(SIGPIPE, SIG_IGN);
65                         signal(SIGTERM, SIG_IGN);
66                         signal(SIGQUIT, SIG_IGN);
67                         signal(SIGHUP,  SIG_IGN);
68                         signal(SIGINT,  SIG_IGN);
69                         signal(SIGCHLD, SIG_IGN);
70                         kill_childs();
71                         rrdset_free_all();
72                         //unlink("/var/run/netdata.pid");
73                         info("NetData exiting. Bye bye...");
74                         exit(1);
75                         break;
76
77                 case SIGPIPE:
78                         info("Signaled PIPE (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
79                         // this is received when web clients send a reset
80                         // no need to log it.
81                         // info("Ignoring signal %d. Errno: %d (%s)", signo, errno, strerror(errno));
82                         break;
83
84                 default:
85                         info("Signal %d received. Falling back to default action for it.", signo);
86                         signal(signo, SIG_DFL);
87                         break;
88         }
89 }
90
91 char rundir[FILENAME_MAX + 1] = "/var/run/netdata";
92 char pidfile[FILENAME_MAX + 1] = "";
93 void prepare_rundir() {
94         if(getuid() != 0) {
95                 mkdir("/run/user", 0775);
96                 snprintf(rundir, FILENAME_MAX, "/run/user/%d", getuid());
97                 mkdir(rundir, 0775);
98                 snprintf(rundir, FILENAME_MAX, "/run/user/%d/netdata", getuid());
99         }
100         
101         snprintf(pidfile, FILENAME_MAX, "%s/netdata.pid", rundir);
102
103         if(mkdir(rundir, 0775) != 0) {
104                 if(errno != EEXIST) fprintf(stderr, "Cannot create directory '%s' (%s).", rundir, strerror(errno));
105         }
106 }
107
108 int become_user(const char *username)
109 {
110         struct passwd *pw = getpwnam(username);
111         if(!pw) {
112                 fprintf(stderr, "User %s is not present. Error: %s\n", username, strerror(errno));
113                 return -1;
114         }
115
116         if(chown(rundir, pw->pw_uid, pw->pw_gid) != 0) {
117                 fprintf(stderr, "Cannot chown directory '%s' to user %s. Error: %s\n", rundir, username, strerror(errno));
118                 return -1;
119         }
120
121         if(setgid(pw->pw_gid) != 0) {
122                 fprintf(stderr, "Cannot switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
123                 return -1;
124         }
125         if(setegid(pw->pw_gid) != 0) {
126                 fprintf(stderr, "Cannot effectively switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
127                 return -1;
128         }
129         if(setuid(pw->pw_uid) != 0) {
130                 fprintf(stderr, "Cannot switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
131                 return -1;
132         }
133         if(seteuid(pw->pw_uid) != 0) {
134                 fprintf(stderr, "Cannot effectively switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
135                 return -1;
136         }
137
138         return(0);
139 }
140
141 int become_daemon(int dont_fork, int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
142 {
143         fflush(NULL);
144
145         // open the files before forking
146         int input_fd = -1, output_fd = -1, error_fd = -1, dev_null = -1;
147
148         if(input && *input) {
149                 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
150                         fprintf(stderr, "Cannot open input file '%s' (%s).", input, strerror(errno));
151                         return -1;
152                 }
153         }
154
155         if(output && *output) {
156                 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
157                         fprintf(stderr, "Cannot open output log file '%s' (%s).", output, strerror(errno));
158                         if(input_fd != -1) close(input_fd);
159                         return -1;
160                 }
161         }
162
163         if(error && *error) {
164                 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
165                         fprintf(stderr, "Cannot open error log file '%s' (%s).", error, strerror(errno));
166                         if(input_fd != -1) close(input_fd);
167                         if(output_fd != -1) close(output_fd);
168                         return -1;
169                 }
170         }
171
172         if(access && *access && access_fd) {
173                 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
174                         fprintf(stderr, "Cannot open access log file '%s' (%s).", access, strerror(errno));
175                         if(input_fd != -1) close(input_fd);
176                         if(output_fd != -1) close(output_fd);
177                         if(error_fd != -1) close(error_fd);
178                         return -1;
179                 }
180
181                 if(access_fp) {
182                         *access_fp = fdopen(*access_fd, "w");
183                         if(!*access_fp) {
184                                 fprintf(stderr, "Cannot migrate file's '%s' fd %d (%s).\n", access, *access_fd, strerror(errno));
185                                 if(input_fd != -1) close(input_fd);
186                                 if(output_fd != -1) close(output_fd);
187                                 if(error_fd != -1) close(error_fd);
188                                 close(*access_fd);
189                                 *access_fd = -1;
190                                 return -1;
191                         }
192                 }
193         }
194         
195         if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
196                 perror("Cannot open /dev/null");
197                 if(input_fd != -1) close(input_fd);
198                 if(output_fd != -1) close(output_fd);
199                 if(error_fd != -1) close(error_fd);
200                 if(access && access_fd && *access_fd != -1) {
201                         close(*access_fd);
202                         *access_fd = -1;
203                         if(access_fp) {
204                                 fclose(*access_fp);
205                                 *access_fp = NULL;
206                         }
207                 }
208                 return -1;
209         }
210
211         // all files opened
212         // lets do it
213
214         if(!dont_fork) {
215                 int i = fork();
216                 if(i == -1) {
217                         perror("cannot fork");
218                         exit(1);
219                 }
220                 if(i != 0) {
221                         exit(0); // the parent
222                 }
223
224                 // become session leader
225                 if (setsid() < 0) {
226                         perror("Cannot become session leader.");
227                         exit(2);
228                 }
229         }
230
231         signal(SIGCHLD,  SIG_IGN);
232         signal(SIGHUP,   SIG_IGN);
233         signal(SIGWINCH, SIG_IGN);
234
235         // fork() again
236         if(!dont_fork) {
237                 int i = fork();
238                 if(i == -1) {
239                         perror("cannot fork");
240                         exit(1);
241                 }
242                 if(i != 0) {
243                         exit(0); // the parent
244                 }
245         }
246
247         // Set new file permissions
248         umask(0);
249
250         // close all files
251         if(close_all_files) {
252                 int i;
253                 for(i = sysconf(_SC_OPEN_MAX); i > 0; i--)
254                         if(   
255                                 ((access_fd && i != *access_fd) || !access_fd)
256                                 && i != dev_null
257                                 && i != input_fd
258                                 && i != output_fd
259                                 && i != error_fd
260                                 && fd_is_valid(i)
261                                 ) close(i);
262         }
263         else {
264                 close(STDIN_FILENO);
265                 close(STDOUT_FILENO);
266                 close(STDERR_FILENO);
267         }
268
269         // put the opened files
270         // to our standard file descriptors
271         if(input_fd != -1) {
272                 if(input_fd != STDIN_FILENO) {
273                         dup2(input_fd, STDIN_FILENO);
274                         close(input_fd);
275                 }
276                 input_fd = -1;
277         }
278         else dup2(dev_null, STDIN_FILENO);
279         
280         if(output_fd != -1) {
281                 if(output_fd != STDOUT_FILENO) {
282                         dup2(output_fd, STDOUT_FILENO);
283                         close(output_fd);
284                 }
285                 output_fd = -1;
286         }
287         else dup2(dev_null, STDOUT_FILENO);
288
289         if(error_fd != -1) {
290                 if(error_fd != STDERR_FILENO) {
291                         dup2(error_fd, STDERR_FILENO);
292                         close(error_fd);
293                 }
294                 error_fd = -1;
295         }
296         else dup2(dev_null, STDERR_FILENO);
297
298         // close /dev/null
299         if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
300                 close(dev_null);
301
302         // generate our pid file
303         {
304                 unlink(pidfile);
305                 int fd = open(pidfile, O_RDWR | O_CREAT, 0666);
306                 if(fd >= 0) {
307                         char b[100];
308                         sprintf(b, "%d\n", getpid());
309                         int i = write(fd, b, strlen(b));
310                         if(i <= 0) perror("Cannot write pid to file.");
311                         close(fd);
312                 }
313         }
314
315         return(0);
316 }