]> arthur.barton.de Git - netdata.git/blob - src/daemon.c
Merge pull request #831 from paulfantom/master
[netdata.git] / src / daemon.c
1 #include "common.h"
2 #include <sched.h>
3
4 char pidfile[FILENAME_MAX + 1] = "";
5
6 void sig_handler_exit(int signo)
7 {
8     if(signo) {
9         error_log_limit_unlimited();
10         error("Received signal %d. Exiting...", signo);
11         netdata_exit = 1;
12     }
13 }
14
15 void sig_handler_logrotate(int signo)
16 {
17     if(signo) {
18         error_log_limit_reset();
19         info("Received signal %d to re-open the log files", signo);
20         reopen_all_log_files();
21     }
22 }
23
24 void sig_handler_save(int signo)
25 {
26     if(signo) {
27         error_log_limit_reset();
28         info("Received signal %d to save the database...", signo);
29         rrdset_save_all();
30     }
31 }
32
33 void sig_handler_reload_health(int signo)
34 {
35     if(signo) {
36         error_log_limit_reset();
37         info("Received signal %d to reload health configuration...", signo);
38         health_reload();
39     }
40 }
41
42 static void chown_open_file(int fd, uid_t uid, gid_t gid) {
43     if(fd == -1) return;
44
45     struct stat buf;
46
47     if(fstat(fd, &buf) == -1) {
48         error("Cannot fstat() fd %d", fd);
49         return;
50     }
51
52     if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
53         if(fchown(fd, uid, gid) == -1)
54             error("Cannot fchown() fd %d.", fd);
55     }
56 }
57
58 int become_user(const char *username, int pid_fd)
59 {
60     struct passwd *pw = getpwnam(username);
61     if(!pw) {
62         error("User %s is not present.", username);
63         return -1;
64     }
65
66     uid_t uid = pw->pw_uid;
67     gid_t gid = pw->pw_gid;
68
69     int ngroups = (int)sysconf(_SC_NGROUPS_MAX);
70     gid_t *supplementary_groups = NULL;
71     if(ngroups) {
72         supplementary_groups = mallocz(sizeof(gid_t) * ngroups);
73         if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
74             error("Cannot get supplementary groups of user '%s'.", username);
75             freez(supplementary_groups);
76             supplementary_groups = NULL;
77             ngroups = 0;
78         }
79     }
80
81     chown_open_file(STDOUT_FILENO, uid, gid);
82     chown_open_file(STDERR_FILENO, uid, gid);
83     chown_open_file(stdaccess_fd, uid, gid);
84     chown_open_file(pid_fd, uid, gid);
85
86     if(supplementary_groups && ngroups) {
87         if(setgroups(ngroups, supplementary_groups) == -1)
88             error("Cannot set supplementary groups for user '%s'", username);
89
90         freez(supplementary_groups);
91         supplementary_groups = NULL;
92         ngroups = 0;
93     }
94
95     if(setresgid(gid, gid, gid) != 0) {
96         error("Cannot switch to user's %s group (gid: %u).", username, gid);
97         return -1;
98     }
99
100     if(setresuid(uid, uid, uid) != 0) {
101         error("Cannot switch to user %s (uid: %u).", username, uid);
102         return -1;
103     }
104
105     if(setgid(gid) != 0) {
106         error("Cannot switch to user's %s group (gid: %u).", username, gid);
107         return -1;
108     }
109     if(setegid(gid) != 0) {
110         error("Cannot effectively switch to user's %s group (gid: %u).", username, gid);
111         return -1;
112     }
113     if(setuid(uid) != 0) {
114         error("Cannot switch to user %s (uid: %u).", username, uid);
115         return -1;
116     }
117     if(seteuid(uid) != 0) {
118         error("Cannot effectively switch to user %s (uid: %u).", username, uid);
119         return -1;
120     }
121
122     return(0);
123 }
124
125 void oom_score_adj(int score) {
126     int done = 0;
127     int fd = open("/proc/self/oom_score_adj", O_WRONLY);
128     if(fd != -1) {
129         char buf[10 + 1];
130         ssize_t len = snprintfz(buf, 10, "%d", score);
131         if(write(fd, buf, len) == len) done = 1;
132         close(fd);
133     }
134
135     if(!done)
136         error("Cannot adjust my Out-Of-Memory score to %d.", score);
137     else
138         info("Adjusted my Out-Of-Memory score to %d.", score);
139 }
140
141 int sched_setscheduler_idle(void) {
142 #ifdef SCHED_IDLE
143     const struct sched_param param = {
144         .sched_priority = 0
145     };
146
147     int i = sched_setscheduler(0, SCHED_IDLE, &param);
148     if(i != 0)
149         error("Cannot adjust my scheduling priority to IDLE.");
150     else
151         info("Adjusted my scheduling priority to IDLE.");
152
153     return i;
154 #else
155     return -1;
156 #endif
157 }
158
159 int become_daemon(int dont_fork, const char *user)
160 {
161     if(!dont_fork) {
162         int i = fork();
163         if(i == -1) {
164             perror("cannot fork");
165             exit(1);
166         }
167         if(i != 0) {
168             exit(0); // the parent
169         }
170
171         // become session leader
172         if (setsid() < 0) {
173             perror("Cannot become session leader.");
174             exit(2);
175         }
176
177         // fork() again
178         i = fork();
179         if(i == -1) {
180             perror("cannot fork");
181             exit(1);
182         }
183         if(i != 0) {
184             exit(0); // the parent
185         }
186     }
187
188     // generate our pid file
189     int pidfd = -1;
190     if(pidfile[0]) {
191         pidfd = open(pidfile, O_WRONLY | O_CREAT, 0644);
192         if(pidfd >= 0) {
193             if(ftruncate(pidfd, 0) != 0)
194                 error("Cannot truncate pidfile '%s'.", pidfile);
195
196             char b[100];
197             sprintf(b, "%d\n", getpid());
198             ssize_t i = write(pidfd, b, strlen(b));
199             if(i <= 0)
200                 error("Cannot write pidfile '%s'.", pidfile);
201         }
202         else error("Failed to open pidfile '%s'.", pidfile);
203     }
204
205     // Set new file permissions
206     umask(0002);
207
208     // adjust my Out-Of-Memory score
209     oom_score_adj(1000);
210
211     // never become a problem
212     if(sched_setscheduler_idle() != 0) {
213         if(nice(19) == -1) error("Cannot lower my CPU priority.");
214         else info("Set my nice value to 19.");
215     }
216
217     if(user && *user) {
218         if(become_user(user, pidfd) != 0) {
219             error("Cannot become user '%s'. Continuing as we are.", user);
220         }
221         else info("Successfully became user '%s'.", user);
222     }
223
224     if(pidfd != -1) {
225         close(pidfd);
226         pidfd = -1;
227     }
228
229     return(0);
230 }