17 #include "web_client.h"
18 #include "plugins_d.h"
24 void sig_handler(int signo)
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);
43 //unlink("/var/run/netdata.pid");
44 info("NetData exiting. Bye bye...");
49 info("Ignoring signal %d. Errno: %d (%s)", signo, errno, strerror(errno));
54 info("Received SIGCHLD (signal %d).", signo);
59 info("Signal %d received. Falling back to default action for it.", signo);
60 signal(signo, SIG_DFL);
65 char rundir[FILENAME_MAX + 1] = "/run/netdata";
66 char pidfile[FILENAME_MAX + 1] = "";
67 void prepare_rundir() {
69 mkdir("/run/user", 0775);
70 snprintf(rundir, FILENAME_MAX, "/run/user/%d", getpid());
72 snprintf(rundir, FILENAME_MAX, "/run/user/%d/netdata", getpid());
75 snprintf(pidfile, FILENAME_MAX, "%s/netdata.pid", rundir);
77 if(mkdir(rundir, 0775) != 0)
78 fprintf(stderr, "Cannot create directory '%s' (%s).", rundir, strerror(errno));
81 int become_user(const char *username)
83 struct passwd *pw = getpwnam(username);
85 fprintf(stderr, "User %s is not present. Error: %s\n", username, strerror(errno));
89 if(chown(rundir, pw->pw_uid, pw->pw_gid) != 0) {
90 fprintf(stderr, "Cannot chown directory '%s' to user %s. Error: %s\n", rundir, username, strerror(errno));
94 if(setgid(pw->pw_gid) != 0) {
95 fprintf(stderr, "Cannot switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
98 if(setegid(pw->pw_gid) != 0) {
99 fprintf(stderr, "Cannot effectively switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
102 if(setuid(pw->pw_uid) != 0) {
103 fprintf(stderr, "Cannot switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
106 if(seteuid(pw->pw_uid) != 0) {
107 fprintf(stderr, "Cannot effectively switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
114 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 // open the files before forking
119 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null = -1;
121 if(input && *input) {
122 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
123 fprintf(stderr, "Cannot open input file '%s' (%s).", input, strerror(errno));
128 if(output && *output) {
129 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
130 fprintf(stderr, "Cannot open output log file '%s' (%s).", output, strerror(errno));
131 if(input_fd != -1) close(input_fd);
136 if(error && *error) {
137 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
138 fprintf(stderr, "Cannot open error log file '%s' (%s).", error, strerror(errno));
139 if(input_fd != -1) close(input_fd);
140 if(output_fd != -1) close(output_fd);
145 if(access && *access && access_fd) {
146 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
147 fprintf(stderr, "Cannot open access log file '%s' (%s).", access, strerror(errno));
148 if(input_fd != -1) close(input_fd);
149 if(output_fd != -1) close(output_fd);
150 if(error_fd != -1) close(error_fd);
155 *access_fp = fdopen(*access_fd, "w");
157 fprintf(stderr, "Cannot migrate file's '%s' fd %d (%s).\n", access, *access_fd, strerror(errno));
158 if(input_fd != -1) close(input_fd);
159 if(output_fd != -1) close(output_fd);
160 if(error_fd != -1) close(error_fd);
168 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
169 perror("Cannot open /dev/null");
170 if(input_fd != -1) close(input_fd);
171 if(output_fd != -1) close(output_fd);
172 if(error_fd != -1) close(error_fd);
173 if(access && access_fd && *access_fd != -1) {
189 perror("cannot fork");
193 exit(0); // the parent
196 // become session leader
200 signal(SIGCHLD, SIG_IGN);
201 signal(SIGHUP, SIG_IGN);
202 signal(SIGWINCH, SIG_IGN);
207 perror("cannot fork");
211 exit(0); // the parent
214 // Set new file permissions
218 if(close_all_files) {
219 for(i = sysconf(_SC_OPEN_MAX); i > 0; i--)
221 ((access_fd && i != *access_fd) || !access_fd)
231 close(STDOUT_FILENO);
232 close(STDERR_FILENO);
235 // put the opened files
236 // to our standard file descriptors
238 if(input_fd != STDIN_FILENO) {
239 dup2(input_fd, STDIN_FILENO);
244 else dup2(dev_null, STDIN_FILENO);
246 if(output_fd != -1) {
247 if(output_fd != STDOUT_FILENO) {
248 dup2(output_fd, STDOUT_FILENO);
253 else dup2(dev_null, STDOUT_FILENO);
256 if(error_fd != STDERR_FILENO) {
257 dup2(error_fd, STDERR_FILENO);
262 else dup2(dev_null, STDERR_FILENO);
265 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
268 // generate our pid file
271 int fd = open(pidfile, O_RDWR | O_CREAT, 0666);
274 sprintf(b, "%d\n", getpid());
275 i = write(fd, b, strlen(b));