4 char pidfile[FILENAME_MAX + 1] = "";
6 void sig_handler_exit(int signo)
9 error_log_limit_unlimited();
10 error("Received signal %d. Exiting...", signo);
15 void sig_handler_save(int signo)
18 info("Received signal %d to save the database...", signo);
23 static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
28 if(fstat(fd, &buf) == -1) {
29 error("Cannot fstat() fd %d", fd);
33 if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
34 if(fchown(fd, uid, gid) == -1)
35 error("Cannot fchown() fd %d.", fd);
39 int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
41 struct passwd *pw = getpwnam(username);
43 error("User %s is not present.", username);
47 uid_t uid = pw->pw_uid;
48 gid_t gid = pw->pw_gid;
50 int ngroups = (int)sysconf(_SC_NGROUPS_MAX);
51 gid_t *supplementary_groups = NULL;
53 supplementary_groups = mallocz(sizeof(gid_t) * ngroups);
54 if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
55 error("Cannot get supplementary groups of user '%s'.", username);
56 freez(supplementary_groups);
57 supplementary_groups = NULL;
62 properly_chown_netdata_generated_file(access_fd, uid, gid);
63 properly_chown_netdata_generated_file(output_fd, uid, gid);
64 properly_chown_netdata_generated_file(error_fd, uid, gid);
65 properly_chown_netdata_generated_file(pid_fd, uid, gid);
67 if(supplementary_groups && ngroups) {
68 if(setgroups(ngroups, supplementary_groups) == -1)
69 error("Cannot set supplementary groups for user '%s'", username);
71 freez(supplementary_groups);
72 supplementary_groups = NULL;
76 if(setresgid(gid, gid, gid) != 0) {
77 error("Cannot switch to user's %s group (gid: %u).", username, gid);
81 if(setresuid(uid, uid, uid) != 0) {
82 error("Cannot switch to user %s (uid: %u).", username, uid);
86 if(setgid(gid) != 0) {
87 error("Cannot switch to user's %s group (gid: %u).", username, gid);
90 if(setegid(gid) != 0) {
91 error("Cannot effectively switch to user's %s group (gid: %u).", username, gid);
94 if(setuid(uid) != 0) {
95 error("Cannot switch to user %s (uid: %u).", username, uid);
98 if(seteuid(uid) != 0) {
99 error("Cannot effectively switch to user %s (uid: %u).", username, uid);
106 void oom_score_adj(int score) {
108 int fd = open("/proc/self/oom_score_adj", O_WRONLY);
111 ssize_t len = snprintfz(buf, 10, "%d", score);
112 if(write(fd, buf, len) == len) done = 1;
117 error("Cannot adjust my Out-Of-Memory score to %d.", score);
119 info("Adjusted my Out-Of-Memory score to %d.", score);
122 int sched_setscheduler_idle(void) {
123 const struct sched_param param = {
127 int i = sched_setscheduler(0, SCHED_IDLE, ¶m);
129 error("Cannot adjust my scheduling priority to IDLE.");
131 info("Adjusted my scheduling priority to IDLE.");
136 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)
140 // open the files before forking
141 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
143 if(input && *input) {
144 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
145 error("Cannot open input file '%s'.", input);
150 if(output && *output && strcmp(output, "/dev/null") != 0) {
151 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
152 error("Cannot open output log file '%s'", output);
153 if(input_fd != -1) close(input_fd);
158 if(error && *error && strcmp(error, "/dev/null") != 0) {
159 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
160 error("Cannot open error log file '%s'.", error);
161 if(input_fd != -1) close(input_fd);
162 if(output_fd != -1) close(output_fd);
167 if(access && *access && access_fd && strcmp(access, "/dev/null") != 0) {
168 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
169 error("Cannot open access log file '%s'", access);
170 if(input_fd != -1) close(input_fd);
171 if(output_fd != -1) close(output_fd);
172 if(error_fd != -1) close(error_fd);
177 *access_fp = fdopen(*access_fd, "w");
179 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
180 if(input_fd != -1) close(input_fd);
181 if(output_fd != -1) close(output_fd);
182 if(error_fd != -1) close(error_fd);
187 if(setvbuf(*access_fp, NULL, _IOLBF, 0) != 0)
188 error("Cannot set line buffering on access.log");
192 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
193 perror("Cannot open /dev/null");
194 if(input_fd != -1) close(input_fd);
195 if(output_fd != -1) close(output_fd);
196 if(error_fd != -1) close(error_fd);
197 if(access && access_fd && *access_fd != -1) {
214 perror("cannot fork");
218 exit(0); // the parent
221 // become session leader
223 perror("Cannot become session leader.");
230 perror("cannot fork");
234 exit(0); // the parent
239 if(close_all_files) {
241 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
243 ((access_fd && i != *access_fd) || !access_fd)
253 close(STDOUT_FILENO);
254 close(STDERR_FILENO);
257 // put the opened files
258 // to our standard file descriptors
260 if(input_fd != STDIN_FILENO) {
261 dup2(input_fd, STDIN_FILENO);
266 else dup2(dev_null, STDIN_FILENO);
268 if(output_fd != -1) {
269 if(output_fd != STDOUT_FILENO) {
270 dup2(output_fd, STDOUT_FILENO);
274 if(setvbuf(stdout, NULL, _IOLBF, 0) != 0)
275 error("Cannot set line buffering on debug.log");
277 output_fd = STDOUT_FILENO;
279 else dup2(dev_null, STDOUT_FILENO);
282 if(error_fd != STDERR_FILENO) {
283 dup2(error_fd, STDERR_FILENO);
287 if(setvbuf(stderr, NULL, _IOLBF, 0) != 0)
288 error("Cannot set line buffering on error.log");
290 error_fd = STDERR_FILENO;
292 else dup2(dev_null, STDERR_FILENO);
295 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
298 // generate our pid file
301 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
303 if(ftruncate(pidfd, 0) != 0)
304 error("Cannot truncate pidfile '%s'.", pidfile);
307 sprintf(b, "%d\n", getpid());
308 ssize_t i = write(pidfd, b, strlen(b));
310 error("Cannot write pidfile '%s'.", pidfile);
312 else error("Failed to open pidfile '%s'.", pidfile);
315 // Set new file permissions
318 // adjust my Out-Of-Memory score
321 // never become a problem
322 if(sched_setscheduler_idle() != 0) {
323 if(nice(19) == -1) error("Cannot lower my CPU priority.");
324 else info("Set my nice value to 19.");
328 if(become_user(user, (access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
329 error("Cannot become user '%s'. Continuing as we are.", user);
331 else info("Successfully became user '%s'.", user);