11 #include <sys/types.h>
18 #include "appconfig.h"
20 #include "web_client.h"
21 #include "plugins_d.h"
27 char pidfile[FILENAME_MAX + 1] = "";
30 void sig_handler(int signo)
42 infoerr("Death signaled exit (signal %d).", signo);
43 signal(signo, SIG_DFL);
53 infoerr("Signaled exit (signal %d).", signo);
54 signal(SIGPIPE, SIG_IGN);
55 signal(SIGTERM, SIG_IGN);
56 signal(SIGQUIT, SIG_IGN);
57 signal(SIGHUP, SIG_IGN);
58 signal(SIGINT, SIG_IGN);
59 signal(SIGCHLD, SIG_IGN);
60 netdata_cleanup_and_exit(1);
64 infoerr("Signaled PIPE (signal %d).", signo);
65 // this is received when web clients send a reset
67 // infoerr("Ignoring signal %d.", signo);
71 info("Signal %d received. Falling back to default action for it.", signo);
72 signal(signo, SIG_DFL);
77 int become_user(const char *username)
79 struct passwd *pw = getpwnam(username);
81 error("User %s is not present.", username);
85 uid_t uid = pw->pw_uid;
86 gid_t gid = pw->pw_gid;
88 if(pidfile[0] && getuid() != uid) {
89 // we are dropping privileges
90 if(chown(pidfile, uid, gid) != 0)
91 error("Cannot chown pidfile '%s' to user '%s'", pidfile, username);
93 else if(pidfd != -1) {
94 // not need to keep it open
99 else if(pidfd != -1) {
100 // not need to keep it open
105 if(setresgid(gid, gid, gid) != 0) {
106 error("Cannot switch to user's %s group (gid: %d).", username, gid);
110 if(setresuid(uid, uid, uid) != 0) {
111 error("Cannot switch to user %s (uid: %d).", username, uid);
115 if(setgid(gid) != 0) {
116 error("Cannot switch to user's %s group (gid: %d).", username, gid);
119 if(setegid(gid) != 0) {
120 error("Cannot effectively switch to user's %s group (gid: %d).", username, gid);
123 if(setuid(uid) != 0) {
124 error("Cannot switch to user %s (uid: %d).", username, uid);
127 if(seteuid(uid) != 0) {
128 error("Cannot effectively switch to user %s (uid: %d).", username, uid);
135 int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
139 // open the files before forking
140 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
142 if(input && *input) {
143 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
144 error("Cannot open input file '%s'.", input);
149 if(output && *output) {
150 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
151 error("Cannot open output log file '%s'", output);
152 if(input_fd != -1) close(input_fd);
157 if(error && *error) {
158 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
159 error("Cannot open error log file '%s'.", error);
160 if(input_fd != -1) close(input_fd);
161 if(output_fd != -1) close(output_fd);
166 if(access && *access && access_fd) {
167 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
168 error("Cannot open access log file '%s'", access);
169 if(input_fd != -1) close(input_fd);
170 if(output_fd != -1) close(output_fd);
171 if(error_fd != -1) close(error_fd);
176 *access_fp = fdopen(*access_fd, "w");
178 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
179 if(input_fd != -1) close(input_fd);
180 if(output_fd != -1) close(output_fd);
181 if(error_fd != -1) close(error_fd);
189 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
190 perror("Cannot open /dev/null");
191 if(input_fd != -1) close(input_fd);
192 if(output_fd != -1) close(output_fd);
193 if(error_fd != -1) close(error_fd);
194 if(access && access_fd && *access_fd != -1) {
211 perror("cannot fork");
215 exit(0); // the parent
218 // become session leader
220 perror("Cannot become session leader.");
225 signal(SIGCHLD, SIG_IGN);
226 signal(SIGHUP, SIG_IGN);
227 signal(SIGWINCH, SIG_IGN);
233 perror("cannot fork");
237 exit(0); // the parent
241 // Set new file permissions
245 if(close_all_files) {
247 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
249 ((access_fd && i != *access_fd) || !access_fd)
259 close(STDOUT_FILENO);
260 close(STDERR_FILENO);
263 // put the opened files
264 // to our standard file descriptors
266 if(input_fd != STDIN_FILENO) {
267 dup2(input_fd, STDIN_FILENO);
272 else dup2(dev_null, STDIN_FILENO);
274 if(output_fd != -1) {
275 if(output_fd != STDOUT_FILENO) {
276 dup2(output_fd, STDOUT_FILENO);
281 else dup2(dev_null, STDOUT_FILENO);
284 if(error_fd != STDERR_FILENO) {
285 dup2(error_fd, STDERR_FILENO);
290 else dup2(dev_null, STDERR_FILENO);
293 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
296 // generate our pid file
298 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
300 if(ftruncate(pidfd, 0) != 0)
301 error("Cannot truncate pidfile '%s'.", pidfile);
304 sprintf(b, "%d\n", getpid());
305 ssize_t i = write(pidfd, b, strlen(b));
307 error("Cannot write pidfile '%s'.", pidfile);
309 // don't close it, we might need it at exit
312 else error("Failed to open pidfile '%s'.", pidfile);
316 if(become_user(user) != 0) {
317 error("Cannot become user '%s'. Continuing as we are.", user);
319 else info("Successfully became user '%s'.", user);