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