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)
36 static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
41 if(fstat(fd, &buf) == -1) {
42 error("Cannot fstat() fd %d", fd);
46 if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
47 if(fchown(fd, uid, gid) == -1)
48 error("Cannot fchown() fd %d.", fd);
52 int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
54 struct passwd *pw = getpwnam(username);
56 error("User %s is not present.", username);
60 uid_t uid = pw->pw_uid;
61 gid_t gid = pw->pw_gid;
63 int ngroups = sysconf(_SC_NGROUPS_MAX);
64 gid_t *supplementary_groups = NULL;
66 supplementary_groups = malloc(sizeof(gid_t) * ngroups);
67 if(supplementary_groups) {
68 if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
69 error("Cannot get supplementary groups of user '%s'.", username);
70 free(supplementary_groups);
71 supplementary_groups = NULL;
75 else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
78 properly_chown_netdata_generated_file(access_fd, uid, gid);
79 properly_chown_netdata_generated_file(output_fd, uid, gid);
80 properly_chown_netdata_generated_file(error_fd, uid, gid);
81 properly_chown_netdata_generated_file(pid_fd, uid, gid);
83 if(supplementary_groups && ngroups) {
84 if(setgroups(ngroups, supplementary_groups) == -1)
85 error("Cannot set supplementary groups for user '%s'", username);
87 free(supplementary_groups);
88 supplementary_groups = NULL;
92 if(setresgid(gid, gid, gid) != 0) {
93 error("Cannot switch to user's %s group (gid: %d).", username, gid);
97 if(setresuid(uid, uid, uid) != 0) {
98 error("Cannot switch to user %s (uid: %d).", username, uid);
102 if(setgid(gid) != 0) {
103 error("Cannot switch to user's %s group (gid: %d).", username, gid);
106 if(setegid(gid) != 0) {
107 error("Cannot effectively switch to user's %s group (gid: %d).", username, gid);
110 if(setuid(uid) != 0) {
111 error("Cannot switch to user %s (uid: %d).", username, uid);
114 if(seteuid(uid) != 0) {
115 error("Cannot effectively switch to user %s (uid: %d).", username, uid);
122 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)
126 // open the files before forking
127 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
129 if(input && *input) {
130 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
131 error("Cannot open input file '%s'.", input);
136 if(output && *output && strcmp(output, "/dev/null") != 0) {
137 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
138 error("Cannot open output log file '%s'", output);
139 if(input_fd != -1) close(input_fd);
144 if(error && *error && strcmp(error, "/dev/null") != 0) {
145 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
146 error("Cannot open error log file '%s'.", error);
147 if(input_fd != -1) close(input_fd);
148 if(output_fd != -1) close(output_fd);
153 if(access && *access && access_fd && strcmp(access, "/dev/null") != 0) {
154 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
155 error("Cannot open access log file '%s'", access);
156 if(input_fd != -1) close(input_fd);
157 if(output_fd != -1) close(output_fd);
158 if(error_fd != -1) close(error_fd);
163 *access_fp = fdopen(*access_fd, "w");
165 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
166 if(input_fd != -1) close(input_fd);
167 if(output_fd != -1) close(output_fd);
168 if(error_fd != -1) close(error_fd);
173 if(setvbuf(*access_fp, NULL, _IOLBF, 0) != 0)
174 error("Cannot set line buffering on access.log");
178 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
179 perror("Cannot open /dev/null");
180 if(input_fd != -1) close(input_fd);
181 if(output_fd != -1) close(output_fd);
182 if(error_fd != -1) close(error_fd);
183 if(access && access_fd && *access_fd != -1) {
200 perror("cannot fork");
204 exit(0); // the parent
207 // become session leader
209 perror("Cannot become session leader.");
218 perror("cannot fork");
222 exit(0); // the parent
226 // Set new file permissions
230 if(close_all_files) {
232 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
234 ((access_fd && i != *access_fd) || !access_fd)
244 close(STDOUT_FILENO);
245 close(STDERR_FILENO);
248 // put the opened files
249 // to our standard file descriptors
251 if(input_fd != STDIN_FILENO) {
252 dup2(input_fd, STDIN_FILENO);
257 else dup2(dev_null, STDIN_FILENO);
259 if(output_fd != -1) {
260 if(output_fd != STDOUT_FILENO) {
261 dup2(output_fd, STDOUT_FILENO);
265 if(setvbuf(stdout, NULL, _IOLBF, 0) != 0)
266 error("Cannot set line buffering on debug.log");
268 output_fd = STDOUT_FILENO;
270 else dup2(dev_null, STDOUT_FILENO);
273 if(error_fd != STDERR_FILENO) {
274 dup2(error_fd, STDERR_FILENO);
278 if(setvbuf(stderr, NULL, _IOLBF, 0) != 0)
279 error("Cannot set line buffering on error.log");
281 error_fd = STDERR_FILENO;
283 else dup2(dev_null, STDERR_FILENO);
286 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
289 // generate our pid file
292 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
294 if(ftruncate(pidfd, 0) != 0)
295 error("Cannot truncate pidfile '%s'.", pidfile);
298 sprintf(b, "%d\n", getpid());
299 ssize_t i = write(pidfd, b, strlen(b));
301 error("Cannot write pidfile '%s'.", pidfile);
303 else error("Failed to open pidfile '%s'.", pidfile);
307 if(become_user(user, (*access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
308 error("Cannot become user '%s'. Continuing as we are.", user);
310 else info("Successfully became user '%s'.", user);