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)
37 static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
42 if(fstat(fd, &buf) == -1) {
43 error("Cannot fstat() fd %d", fd);
47 if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
48 if(fchown(fd, uid, gid) == -1)
49 error("Cannot fchown() fd %d.", fd);
53 int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
55 struct passwd *pw = getpwnam(username);
57 error("User %s is not present.", username);
61 uid_t uid = pw->pw_uid;
62 gid_t gid = pw->pw_gid;
64 int ngroups = sysconf(_SC_NGROUPS_MAX);
65 gid_t *supplementary_groups = NULL;
67 supplementary_groups = malloc(sizeof(gid_t) * ngroups);
68 if(supplementary_groups) {
69 if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
70 error("Cannot get supplementary groups of user '%s'.", username);
71 free(supplementary_groups);
72 supplementary_groups = NULL;
76 else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
79 properly_chown_netdata_generated_file(access_fd, uid, gid);
80 properly_chown_netdata_generated_file(output_fd, uid, gid);
81 properly_chown_netdata_generated_file(error_fd, uid, gid);
82 properly_chown_netdata_generated_file(pid_fd, uid, gid);
84 if(supplementary_groups && ngroups) {
85 if(setgroups(ngroups, supplementary_groups) == -1)
86 error("Cannot set supplementary groups for user '%s'", username);
88 free(supplementary_groups);
89 supplementary_groups = NULL;
93 if(setresgid(gid, gid, gid) != 0) {
94 error("Cannot switch to user's %s group (gid: %d).", username, gid);
98 if(setresuid(uid, uid, uid) != 0) {
99 error("Cannot switch to user %s (uid: %d).", username, uid);
103 if(setgid(gid) != 0) {
104 error("Cannot switch to user's %s group (gid: %d).", username, gid);
107 if(setegid(gid) != 0) {
108 error("Cannot effectively switch to user's %s group (gid: %d).", username, gid);
111 if(setuid(uid) != 0) {
112 error("Cannot switch to user %s (uid: %d).", username, uid);
115 if(seteuid(uid) != 0) {
116 error("Cannot effectively switch to user %s (uid: %d).", username, uid);
123 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)
127 // open the files before forking
128 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
130 if(input && *input) {
131 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
132 error("Cannot open input file '%s'.", input);
137 if(output && *output && strcmp(output, "/dev/null") != 0) {
138 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
139 error("Cannot open output log file '%s'", output);
140 if(input_fd != -1) close(input_fd);
145 if(error && *error && strcmp(error, "/dev/null") != 0) {
146 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
147 error("Cannot open error log file '%s'.", error);
148 if(input_fd != -1) close(input_fd);
149 if(output_fd != -1) close(output_fd);
154 if(access && *access && access_fd && strcmp(access, "/dev/null") != 0) {
155 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
156 error("Cannot open access log file '%s'", access);
157 if(input_fd != -1) close(input_fd);
158 if(output_fd != -1) close(output_fd);
159 if(error_fd != -1) close(error_fd);
164 *access_fp = fdopen(*access_fd, "w");
166 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
167 if(input_fd != -1) close(input_fd);
168 if(output_fd != -1) close(output_fd);
169 if(error_fd != -1) close(error_fd);
174 if(setvbuf(*access_fp, NULL, _IOLBF, 0) != 0)
175 error("Cannot set line buffering on access.log");
179 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
180 perror("Cannot open /dev/null");
181 if(input_fd != -1) close(input_fd);
182 if(output_fd != -1) close(output_fd);
183 if(error_fd != -1) close(error_fd);
184 if(access && access_fd && *access_fd != -1) {
201 perror("cannot fork");
205 exit(0); // the parent
208 // become session leader
210 perror("Cannot become session leader.");
219 perror("cannot fork");
223 exit(0); // the parent
227 // Set new file permissions
231 if(close_all_files) {
233 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
235 ((access_fd && i != *access_fd) || !access_fd)
245 close(STDOUT_FILENO);
246 close(STDERR_FILENO);
249 // put the opened files
250 // to our standard file descriptors
252 if(input_fd != STDIN_FILENO) {
253 dup2(input_fd, STDIN_FILENO);
258 else dup2(dev_null, STDIN_FILENO);
260 if(output_fd != -1) {
261 if(output_fd != STDOUT_FILENO) {
262 dup2(output_fd, STDOUT_FILENO);
266 if(setvbuf(stdout, NULL, _IOLBF, 0) != 0)
267 error("Cannot set line buffering on debug.log");
269 output_fd = STDOUT_FILENO;
271 else dup2(dev_null, STDOUT_FILENO);
274 if(error_fd != STDERR_FILENO) {
275 dup2(error_fd, STDERR_FILENO);
279 if(setvbuf(stderr, NULL, _IOLBF, 0) != 0)
280 error("Cannot set line buffering on error.log");
282 error_fd = STDERR_FILENO;
284 else dup2(dev_null, STDERR_FILENO);
287 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
290 // generate our pid file
293 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
295 if(ftruncate(pidfd, 0) != 0)
296 error("Cannot truncate pidfile '%s'.", pidfile);
299 sprintf(b, "%d\n", getpid());
300 ssize_t i = write(pidfd, b, strlen(b));
302 error("Cannot write pidfile '%s'.", pidfile);
304 else error("Failed to open pidfile '%s'.", pidfile);
308 if(become_user(user, (*access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
309 error("Cannot become user '%s'. Continuing as we are.", user);
311 else info("Successfully became user '%s'.", user);