]> arthur.barton.de Git - netdata.git/blob - src/daemon.c
allow users to configure netdata oom score
[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_unlimited();
19         info("Received signal %d to re-open the log files", signo);
20         reopen_all_log_files();
21         error_log_limit_reset();
22     }
23 }
24
25 void sig_handler_save(int signo)
26 {
27     if(signo) {
28         error_log_limit_unlimited();
29         info("Received signal %d to save the database...", signo);
30         rrdset_save_all();
31         error_log_limit_reset();
32     }
33 }
34
35 void sig_handler_reload_health(int signo)
36 {
37     if(signo) {
38         error_log_limit_unlimited();
39         info("Received signal %d to reload health configuration...", signo);
40         health_reload();
41         error_log_limit_reset();
42     }
43 }
44
45 static void chown_open_file(int fd, uid_t uid, gid_t gid) {
46     if(fd == -1) return;
47
48     struct stat buf;
49
50     if(fstat(fd, &buf) == -1) {
51         error("Cannot fstat() fd %d", fd);
52         return;
53     }
54
55     if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
56         if(fchown(fd, uid, gid) == -1)
57             error("Cannot fchown() fd %d.", fd);
58     }
59 }
60
61 void create_needed_dir(const char *dir, uid_t uid, gid_t gid)
62 {
63     // attempt to create the directory
64     if(mkdir(dir, 0755) == 0) {
65         // we created it
66
67         // chown it to match the required user
68         if(chown(dir, uid, gid) == -1)
69             error("Cannot chown directory '%s' to %u:%u", dir, (unsigned int)uid, (unsigned int)gid);
70     }
71     else if(errno != EEXIST)
72         // log an error only if the directory does not exist
73         error("Cannot create directory '%s'", dir);
74 }
75
76 int become_user(const char *username, int pid_fd)
77 {
78     struct passwd *pw = getpwnam(username);
79     if(!pw) {
80         error("User %s is not present.", username);
81         return -1;
82     }
83
84     uid_t uid = pw->pw_uid;
85     gid_t gid = pw->pw_gid;
86
87     create_needed_dir(CACHE_DIR, uid, gid);
88     create_needed_dir(VARLIB_DIR, uid, gid);
89
90     if(pidfile[0]) {
91         if(chown(pidfile, uid, gid) == -1)
92             error("Cannot chown '%s' to %u:%u", pidfile, (unsigned int)uid, (unsigned int)gid);
93     }
94
95     int ngroups = (int)sysconf(_SC_NGROUPS_MAX);
96     gid_t *supplementary_groups = NULL;
97     if(ngroups) {
98         supplementary_groups = mallocz(sizeof(gid_t) * ngroups);
99         if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
100             error("Cannot get supplementary groups of user '%s'.", username);
101             freez(supplementary_groups);
102             supplementary_groups = NULL;
103             ngroups = 0;
104         }
105     }
106
107     chown_open_file(STDOUT_FILENO, uid, gid);
108     chown_open_file(STDERR_FILENO, uid, gid);
109     chown_open_file(stdaccess_fd, uid, gid);
110     chown_open_file(pid_fd, uid, gid);
111
112     if(supplementary_groups && ngroups) {
113         if(setgroups(ngroups, supplementary_groups) == -1)
114             error("Cannot set supplementary groups for user '%s'", username);
115
116         freez(supplementary_groups);
117         ngroups = 0;
118     }
119
120 #ifdef __APPLE__
121     if(setregid(gid, gid) != 0) {
122 #else
123     if(setresgid(gid, gid, gid) != 0) {
124 #endif /* __APPLE__ */
125         error("Cannot switch to user's %s group (gid: %u).", username, gid);
126         return -1;
127     }
128
129 #ifdef __APPLE__
130     if(setreuid(uid, uid) != 0) {
131 #else
132     if(setresuid(uid, uid, uid) != 0) {
133 #endif /* __APPLE__ */
134         error("Cannot switch to user %s (uid: %u).", username, uid);
135         return -1;
136     }
137
138     if(setgid(gid) != 0) {
139         error("Cannot switch to user's %s group (gid: %u).", username, gid);
140         return -1;
141     }
142     if(setegid(gid) != 0) {
143         error("Cannot effectively switch to user's %s group (gid: %u).", username, gid);
144         return -1;
145     }
146     if(setuid(uid) != 0) {
147         error("Cannot switch to user %s (uid: %u).", username, uid);
148         return -1;
149     }
150     if(seteuid(uid) != 0) {
151         error("Cannot effectively switch to user %s (uid: %u).", username, uid);
152         return -1;
153     }
154
155     return(0);
156 }
157
158 void oom_score_adj(int score) {
159     int done = 0;
160     int fd = open("/proc/self/oom_score_adj", O_WRONLY);
161     if(fd != -1) {
162         char buf[10 + 1];
163         ssize_t len = snprintfz(buf, 10, "%d", score);
164         if(write(fd, buf, len) == len) done = 1;
165         close(fd);
166     }
167
168     if(!done)
169         error("Cannot adjust my Out-Of-Memory score to %d.", score);
170     else
171         debug(D_SYSTEM, "Adjusted my Out-Of-Memory score to %d.", score);
172 }
173
174 int sched_setscheduler_idle(void) {
175 #ifdef SCHED_IDLE
176     const struct sched_param param = {
177         .sched_priority = 0
178     };
179
180     int i = sched_setscheduler(0, SCHED_IDLE, &param);
181     if(i != 0)
182         error("Cannot adjust my scheduling priority to IDLE.");
183     else
184         debug(D_SYSTEM, "Adjusted my scheduling priority to IDLE.");
185
186     return i;
187 #else
188     return -1;
189 #endif
190 }
191
192 int become_daemon(int dont_fork, const char *user, int oom_score)
193 {
194     if(!dont_fork) {
195         int i = fork();
196         if(i == -1) {
197             perror("cannot fork");
198             exit(1);
199         }
200         if(i != 0) {
201             exit(0); // the parent
202         }
203
204         // become session leader
205         if (setsid() < 0) {
206             perror("Cannot become session leader.");
207             exit(2);
208         }
209
210         // fork() again
211         i = fork();
212         if(i == -1) {
213             perror("cannot fork");
214             exit(1);
215         }
216         if(i != 0) {
217             exit(0); // the parent
218         }
219     }
220
221     // generate our pid file
222     int pidfd = -1;
223     if(pidfile[0]) {
224         pidfd = open(pidfile, O_WRONLY | O_CREAT, 0644);
225         if(pidfd >= 0) {
226             if(ftruncate(pidfd, 0) != 0)
227                 error("Cannot truncate pidfile '%s'.", pidfile);
228
229             char b[100];
230             sprintf(b, "%d\n", getpid());
231             ssize_t i = write(pidfd, b, strlen(b));
232             if(i <= 0)
233                 error("Cannot write pidfile '%s'.", pidfile);
234         }
235         else error("Failed to open pidfile '%s'.", pidfile);
236     }
237
238     // Set new file permissions
239     umask(0007);
240
241     // adjust my Out-Of-Memory score
242     oom_score_adj(oom_score);
243
244     // never become a problem
245     if(sched_setscheduler_idle() != 0) {
246         if(nice(19) == -1) error("Cannot lower my CPU priority.");
247         else debug(D_SYSTEM, "Set my nice value to 19.");
248     }
249
250     if(user && *user) {
251         if(become_user(user, pidfd) != 0) {
252             error("Cannot become user '%s'. Continuing as we are.", user);
253         }
254         else debug(D_SYSTEM, "Successfully became user '%s'.", user);
255     }
256     else {
257         create_needed_dir(CACHE_DIR, getuid(), getgid());
258         create_needed_dir(VARLIB_DIR, getuid(), getgid());
259     }
260
261     if(pidfd != -1)
262         close(pidfd);
263
264     return(0);
265 }