11 #include <sys/types.h>
19 #include "appconfig.h"
21 #include "web_client.h"
22 #include "plugins_d.h"
28 char pidfile[FILENAME_MAX + 1] = "";
31 void sig_handler(int signo)
43 infoerr("Death signaled exit (signal %d).", signo);
44 signal(signo, SIG_DFL);
54 infoerr("Signaled exit (signal %d).", signo);
55 signal(SIGPIPE, SIG_IGN);
56 signal(SIGTERM, SIG_IGN);
57 signal(SIGQUIT, SIG_IGN);
58 signal(SIGHUP, SIG_IGN);
59 signal(SIGINT, SIG_IGN);
60 signal(SIGCHLD, SIG_IGN);
61 netdata_cleanup_and_exit(1);
65 infoerr("Signaled PIPE (signal %d).", signo);
66 // this is received when web clients send a reset
68 // infoerr("Ignoring signal %d.", signo);
72 info("Signal %d received. Falling back to default action for it.", signo);
73 signal(signo, SIG_DFL);
78 int become_user(const char *username)
80 struct passwd *pw = getpwnam(username);
82 error("User %s is not present.", username);
86 uid_t uid = pw->pw_uid;
87 gid_t gid = pw->pw_gid;
89 int ngroups = sysconf(_SC_NGROUPS_MAX);
90 gid_t *supplementary_groups = NULL;
92 supplementary_groups = malloc(sizeof(gid_t) * ngroups);
93 if(supplementary_groups) {
94 if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
95 error("Cannot get supplementary groups of user '%s'.", username);
96 free(supplementary_groups);
97 supplementary_groups = NULL;
101 else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
104 if(pidfile[0] && getuid() != uid) {
105 // we are dropping privileges
106 if(chown(pidfile, uid, gid) != 0)
107 error("Cannot chown pidfile '%s' to user '%s'", pidfile, username);
109 else if(pidfd != -1) {
110 // not need to keep it open
115 else if(pidfd != -1) {
116 // not need to keep it open
121 if(supplementary_groups && ngroups) {
122 if(setgroups(ngroups, supplementary_groups) == -1)
123 error("Cannot set supplementary groups for user '%s'", username);
125 free(supplementary_groups);
126 supplementary_groups = NULL;
130 if(setresgid(gid, gid, gid) != 0) {
131 error("Cannot switch to user's %s group (gid: %d).", username, gid);
135 if(setresuid(uid, uid, uid) != 0) {
136 error("Cannot switch to user %s (uid: %d).", username, uid);
140 if(setgid(gid) != 0) {
141 error("Cannot switch to user's %s group (gid: %d).", username, gid);
144 if(setegid(gid) != 0) {
145 error("Cannot effectively switch to user's %s group (gid: %d).", username, gid);
148 if(setuid(uid) != 0) {
149 error("Cannot switch to user %s (uid: %d).", username, uid);
152 if(seteuid(uid) != 0) {
153 error("Cannot effectively switch to user %s (uid: %d).", username, uid);
160 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)
164 // open the files before forking
165 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
167 if(input && *input) {
168 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
169 error("Cannot open input file '%s'.", input);
174 if(output && *output) {
175 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
176 error("Cannot open output log file '%s'", output);
177 if(input_fd != -1) close(input_fd);
182 if(error && *error) {
183 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
184 error("Cannot open error log file '%s'.", error);
185 if(input_fd != -1) close(input_fd);
186 if(output_fd != -1) close(output_fd);
191 if(access && *access && access_fd) {
192 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
193 error("Cannot open access log file '%s'", access);
194 if(input_fd != -1) close(input_fd);
195 if(output_fd != -1) close(output_fd);
196 if(error_fd != -1) close(error_fd);
201 *access_fp = fdopen(*access_fd, "w");
203 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
204 if(input_fd != -1) close(input_fd);
205 if(output_fd != -1) close(output_fd);
206 if(error_fd != -1) close(error_fd);
214 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
215 perror("Cannot open /dev/null");
216 if(input_fd != -1) close(input_fd);
217 if(output_fd != -1) close(output_fd);
218 if(error_fd != -1) close(error_fd);
219 if(access && access_fd && *access_fd != -1) {
236 perror("cannot fork");
240 exit(0); // the parent
243 // become session leader
245 perror("Cannot become session leader.");
250 signal(SIGCHLD, SIG_IGN);
251 signal(SIGHUP, SIG_IGN);
252 signal(SIGWINCH, SIG_IGN);
258 perror("cannot fork");
262 exit(0); // the parent
266 // Set new file permissions
270 if(close_all_files) {
272 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
274 ((access_fd && i != *access_fd) || !access_fd)
284 close(STDOUT_FILENO);
285 close(STDERR_FILENO);
288 // put the opened files
289 // to our standard file descriptors
291 if(input_fd != STDIN_FILENO) {
292 dup2(input_fd, STDIN_FILENO);
297 else dup2(dev_null, STDIN_FILENO);
299 if(output_fd != -1) {
300 if(output_fd != STDOUT_FILENO) {
301 dup2(output_fd, STDOUT_FILENO);
306 else dup2(dev_null, STDOUT_FILENO);
309 if(error_fd != STDERR_FILENO) {
310 dup2(error_fd, STDERR_FILENO);
315 else dup2(dev_null, STDERR_FILENO);
318 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
321 // generate our pid file
323 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
325 if(ftruncate(pidfd, 0) != 0)
326 error("Cannot truncate pidfile '%s'.", pidfile);
329 sprintf(b, "%d\n", getpid());
330 ssize_t i = write(pidfd, b, strlen(b));
332 error("Cannot write pidfile '%s'.", pidfile);
334 // don't close it, we might need it at exit
337 else error("Failed to open pidfile '%s'.", pidfile);
341 if(become_user(user) != 0) {
342 error("Cannot become user '%s'. Continuing as we are.", user);
344 else info("Successfully became user '%s'.", user);