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] = "";
30 void sig_handler(int signo)
33 error_log_limit_unlimited();
34 error("Received signal %d. Exiting...", signo);
39 static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
44 if(fstat(fd, &buf) == -1) {
45 error("Cannot fstat() fd %d", fd);
49 if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
50 if(fchown(fd, uid, gid) == -1)
51 error("Cannot fchown() fd %d.", fd);
55 int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
57 struct passwd *pw = getpwnam(username);
59 error("User %s is not present.", username);
63 uid_t uid = pw->pw_uid;
64 gid_t gid = pw->pw_gid;
66 int ngroups = sysconf(_SC_NGROUPS_MAX);
67 gid_t *supplementary_groups = NULL;
69 supplementary_groups = malloc(sizeof(gid_t) * ngroups);
70 if(supplementary_groups) {
71 if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
72 error("Cannot get supplementary groups of user '%s'.", username);
73 free(supplementary_groups);
74 supplementary_groups = NULL;
78 else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
81 properly_chown_netdata_generated_file(access_fd, uid, gid);
82 properly_chown_netdata_generated_file(output_fd, uid, gid);
83 properly_chown_netdata_generated_file(error_fd, uid, gid);
84 properly_chown_netdata_generated_file(pid_fd, uid, gid);
86 if(supplementary_groups && ngroups) {
87 if(setgroups(ngroups, supplementary_groups) == -1)
88 error("Cannot set supplementary groups for user '%s'", username);
90 free(supplementary_groups);
91 supplementary_groups = NULL;
95 if(setresgid(gid, gid, gid) != 0) {
96 error("Cannot switch to user's %s group (gid: %u).", username, gid);
100 if(setresuid(uid, uid, uid) != 0) {
101 error("Cannot switch to user %s (uid: %u).", username, uid);
105 if(setgid(gid) != 0) {
106 error("Cannot switch to user's %s group (gid: %u).", username, gid);
109 if(setegid(gid) != 0) {
110 error("Cannot effectively switch to user's %s group (gid: %u).", username, gid);
113 if(setuid(uid) != 0) {
114 error("Cannot switch to user %s (uid: %u).", username, uid);
117 if(seteuid(uid) != 0) {
118 error("Cannot effectively switch to user %s (uid: %u).", username, uid);
125 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)
129 // open the files before forking
130 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
132 if(input && *input) {
133 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
134 error("Cannot open input file '%s'.", input);
139 if(output && *output && strcmp(output, "/dev/null") != 0) {
140 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
141 error("Cannot open output log file '%s'", output);
142 if(input_fd != -1) close(input_fd);
147 if(error && *error && strcmp(error, "/dev/null") != 0) {
148 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
149 error("Cannot open error log file '%s'.", error);
150 if(input_fd != -1) close(input_fd);
151 if(output_fd != -1) close(output_fd);
156 if(access && *access && access_fd && strcmp(access, "/dev/null") != 0) {
157 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
158 error("Cannot open access log file '%s'", access);
159 if(input_fd != -1) close(input_fd);
160 if(output_fd != -1) close(output_fd);
161 if(error_fd != -1) close(error_fd);
166 *access_fp = fdopen(*access_fd, "w");
168 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
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 if(setvbuf(*access_fp, NULL, _IOLBF, 0) != 0)
177 error("Cannot set line buffering on access.log");
181 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
182 perror("Cannot open /dev/null");
183 if(input_fd != -1) close(input_fd);
184 if(output_fd != -1) close(output_fd);
185 if(error_fd != -1) close(error_fd);
186 if(access && access_fd && *access_fd != -1) {
203 perror("cannot fork");
207 exit(0); // the parent
210 // become session leader
212 perror("Cannot become session leader.");
221 perror("cannot fork");
225 exit(0); // the parent
229 // Set new file permissions
233 if(close_all_files) {
235 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
237 ((access_fd && i != *access_fd) || !access_fd)
247 close(STDOUT_FILENO);
248 close(STDERR_FILENO);
251 // put the opened files
252 // to our standard file descriptors
254 if(input_fd != STDIN_FILENO) {
255 dup2(input_fd, STDIN_FILENO);
260 else dup2(dev_null, STDIN_FILENO);
262 if(output_fd != -1) {
263 if(output_fd != STDOUT_FILENO) {
264 dup2(output_fd, STDOUT_FILENO);
268 if(setvbuf(stdout, NULL, _IOLBF, 0) != 0)
269 error("Cannot set line buffering on debug.log");
271 output_fd = STDOUT_FILENO;
273 else dup2(dev_null, STDOUT_FILENO);
276 if(error_fd != STDERR_FILENO) {
277 dup2(error_fd, STDERR_FILENO);
281 if(setvbuf(stderr, NULL, _IOLBF, 0) != 0)
282 error("Cannot set line buffering on error.log");
284 error_fd = STDERR_FILENO;
286 else dup2(dev_null, STDERR_FILENO);
289 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
292 // generate our pid file
295 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
297 if(ftruncate(pidfd, 0) != 0)
298 error("Cannot truncate pidfile '%s'.", pidfile);
301 sprintf(b, "%d\n", getpid());
302 ssize_t i = write(pidfd, b, strlen(b));
304 error("Cannot write pidfile '%s'.", pidfile);
306 else error("Failed to open pidfile '%s'.", pidfile);
310 if(become_user(user, (access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
311 error("Cannot become user '%s'. Continuing as we are.", user);
313 else info("Successfully became user '%s'.", user);