]> arthur.barton.de Git - netdata.git/blob - src/daemon.c
Merge remote-tracking branch 'firehol/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     const struct sched_param param = {
143         .sched_priority = 0
144     };
145
146     int i = sched_setscheduler(0, SCHED_IDLE, &param);
147     if(i != 0)
148         error("Cannot adjust my scheduling priority to IDLE.");
149     else
150         info("Adjusted my scheduling priority to IDLE.");
151
152     return i;
153 }
154
155 int become_daemon(int dont_fork, const char *user)
156 {
157     if(!dont_fork) {
158         int i = fork();
159         if(i == -1) {
160             perror("cannot fork");
161             exit(1);
162         }
163         if(i != 0) {
164             exit(0); // the parent
165         }
166
167         // become session leader
168         if (setsid() < 0) {
169             perror("Cannot become session leader.");
170             exit(2);
171         }
172
173         // fork() again
174         i = fork();
175         if(i == -1) {
176             perror("cannot fork");
177             exit(1);
178         }
179         if(i != 0) {
180             exit(0); // the parent
181         }
182     }
183
184     // generate our pid file
185     int pidfd = -1;
186     if(pidfile[0]) {
187         pidfd = open(pidfile, O_WRONLY | O_CREAT, 0644);
188         if(pidfd >= 0) {
189             if(ftruncate(pidfd, 0) != 0)
190                 error("Cannot truncate pidfile '%s'.", pidfile);
191
192             char b[100];
193             sprintf(b, "%d\n", getpid());
194             ssize_t i = write(pidfd, b, strlen(b));
195             if(i <= 0)
196                 error("Cannot write pidfile '%s'.", pidfile);
197         }
198         else error("Failed to open pidfile '%s'.", pidfile);
199     }
200
201     // Set new file permissions
202     umask(0002);
203
204     // adjust my Out-Of-Memory score
205     oom_score_adj(1000);
206
207     // never become a problem
208     if(sched_setscheduler_idle() != 0) {
209         if(nice(19) == -1) error("Cannot lower my CPU priority.");
210         else info("Set my nice value to 19.");
211     }
212
213     if(user && *user) {
214         if(become_user(user, pidfd) != 0) {
215             error("Cannot become user '%s'. Continuing as we are.", user);
216         }
217         else info("Successfully became user '%s'.", user);
218     }
219
220     if(pidfd != -1) {
221         close(pidfd);
222         pidfd = -1;
223     }
224
225     return(0);
226 }