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_exit(int signo)
33 error_log_limit_unlimited();
34 error("Received signal %d. Exiting...", signo);
39 void sig_handler_save(int signo)
42 info("Received signal %d to save the database...", signo);
47 static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
52 if(fstat(fd, &buf) == -1) {
53 error("Cannot fstat() fd %d", fd);
57 if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
58 if(fchown(fd, uid, gid) == -1)
59 error("Cannot fchown() fd %d.", fd);
63 int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
65 struct passwd *pw = getpwnam(username);
67 error("User %s is not present.", username);
71 uid_t uid = pw->pw_uid;
72 gid_t gid = pw->pw_gid;
74 int ngroups = sysconf(_SC_NGROUPS_MAX);
75 gid_t *supplementary_groups = NULL;
77 supplementary_groups = malloc(sizeof(gid_t) * ngroups);
78 if(supplementary_groups) {
79 if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
80 error("Cannot get supplementary groups of user '%s'.", username);
81 free(supplementary_groups);
82 supplementary_groups = NULL;
86 else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
89 properly_chown_netdata_generated_file(access_fd, uid, gid);
90 properly_chown_netdata_generated_file(output_fd, uid, gid);
91 properly_chown_netdata_generated_file(error_fd, uid, gid);
92 properly_chown_netdata_generated_file(pid_fd, uid, gid);
94 if(supplementary_groups && ngroups) {
95 if(setgroups(ngroups, supplementary_groups) == -1)
96 error("Cannot set supplementary groups for user '%s'", username);
98 free(supplementary_groups);
99 supplementary_groups = NULL;
103 if(setresgid(gid, gid, gid) != 0) {
104 error("Cannot switch to user's %s group (gid: %u).", username, gid);
108 if(setresuid(uid, uid, uid) != 0) {
109 error("Cannot switch to user %s (uid: %u).", username, uid);
113 if(setgid(gid) != 0) {
114 error("Cannot switch to user's %s group (gid: %u).", username, gid);
117 if(setegid(gid) != 0) {
118 error("Cannot effectively switch to user's %s group (gid: %u).", username, gid);
121 if(setuid(uid) != 0) {
122 error("Cannot switch to user %s (uid: %u).", username, uid);
125 if(seteuid(uid) != 0) {
126 error("Cannot effectively switch to user %s (uid: %u).", username, uid);
133 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)
137 // open the files before forking
138 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
140 if(input && *input) {
141 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
142 error("Cannot open input file '%s'.", input);
147 if(output && *output && strcmp(output, "/dev/null") != 0) {
148 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
149 error("Cannot open output log file '%s'", output);
150 if(input_fd != -1) close(input_fd);
155 if(error && *error && strcmp(error, "/dev/null") != 0) {
156 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
157 error("Cannot open error log file '%s'.", error);
158 if(input_fd != -1) close(input_fd);
159 if(output_fd != -1) close(output_fd);
164 if(access && *access && access_fd && strcmp(access, "/dev/null") != 0) {
165 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
166 error("Cannot open access log file '%s'", access);
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 *access_fp = fdopen(*access_fd, "w");
176 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
177 if(input_fd != -1) close(input_fd);
178 if(output_fd != -1) close(output_fd);
179 if(error_fd != -1) close(error_fd);
184 if(setvbuf(*access_fp, NULL, _IOLBF, 0) != 0)
185 error("Cannot set line buffering on access.log");
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.");
229 perror("cannot fork");
233 exit(0); // the parent
237 // Set new file permissions
241 if(close_all_files) {
243 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
245 ((access_fd && i != *access_fd) || !access_fd)
255 close(STDOUT_FILENO);
256 close(STDERR_FILENO);
259 // put the opened files
260 // to our standard file descriptors
262 if(input_fd != STDIN_FILENO) {
263 dup2(input_fd, STDIN_FILENO);
268 else dup2(dev_null, STDIN_FILENO);
270 if(output_fd != -1) {
271 if(output_fd != STDOUT_FILENO) {
272 dup2(output_fd, STDOUT_FILENO);
276 if(setvbuf(stdout, NULL, _IOLBF, 0) != 0)
277 error("Cannot set line buffering on debug.log");
279 output_fd = STDOUT_FILENO;
281 else dup2(dev_null, STDOUT_FILENO);
284 if(error_fd != STDERR_FILENO) {
285 dup2(error_fd, STDERR_FILENO);
289 if(setvbuf(stderr, NULL, _IOLBF, 0) != 0)
290 error("Cannot set line buffering on error.log");
292 error_fd = STDERR_FILENO;
294 else dup2(dev_null, STDERR_FILENO);
297 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
300 // generate our pid file
303 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
305 if(ftruncate(pidfd, 0) != 0)
306 error("Cannot truncate pidfile '%s'.", pidfile);
309 sprintf(b, "%d\n", getpid());
310 ssize_t i = write(pidfd, b, strlen(b));
312 error("Cannot write pidfile '%s'.", pidfile);
314 else error("Failed to open pidfile '%s'.", pidfile);
318 if(become_user(user, (access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
319 error("Cannot become user '%s'. Continuing as we are.", user);
321 else info("Successfully became user '%s'.", user);