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