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