]> arthur.barton.de Git - netdata.git/blob - src/daemon.c
improvements identified via static code analysis with cppcheck
[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     if(setresgid(gid, gid, gid) != 0) {
121         error("Cannot switch to user's %s group (gid: %u).", username, gid);
122         return -1;
123     }
124
125     if(setresuid(uid, uid, uid) != 0) {
126         error("Cannot switch to user %s (uid: %u).", username, uid);
127         return -1;
128     }
129
130     if(setgid(gid) != 0) {
131         error("Cannot switch to user's %s group (gid: %u).", username, gid);
132         return -1;
133     }
134     if(setegid(gid) != 0) {
135         error("Cannot effectively switch to user's %s group (gid: %u).", username, gid);
136         return -1;
137     }
138     if(setuid(uid) != 0) {
139         error("Cannot switch to user %s (uid: %u).", username, uid);
140         return -1;
141     }
142     if(seteuid(uid) != 0) {
143         error("Cannot effectively switch to user %s (uid: %u).", username, uid);
144         return -1;
145     }
146
147     return(0);
148 }
149
150 void oom_score_adj(int score) {
151     int done = 0;
152     int fd = open("/proc/self/oom_score_adj", O_WRONLY);
153     if(fd != -1) {
154         char buf[10 + 1];
155         ssize_t len = snprintfz(buf, 10, "%d", score);
156         if(write(fd, buf, len) == len) done = 1;
157         close(fd);
158     }
159
160     if(!done)
161         error("Cannot adjust my Out-Of-Memory score to %d.", score);
162     else
163         debug(D_SYSTEM, "Adjusted my Out-Of-Memory score to %d.", score);
164 }
165
166 int sched_setscheduler_idle(void) {
167 #ifdef SCHED_IDLE
168     const struct sched_param param = {
169         .sched_priority = 0
170     };
171
172     int i = sched_setscheduler(0, SCHED_IDLE, &param);
173     if(i != 0)
174         error("Cannot adjust my scheduling priority to IDLE.");
175     else
176         debug(D_SYSTEM, "Adjusted my scheduling priority to IDLE.");
177
178     return i;
179 #else
180     return -1;
181 #endif
182 }
183
184 int become_daemon(int dont_fork, const char *user)
185 {
186     if(!dont_fork) {
187         int i = fork();
188         if(i == -1) {
189             perror("cannot fork");
190             exit(1);
191         }
192         if(i != 0) {
193             exit(0); // the parent
194         }
195
196         // become session leader
197         if (setsid() < 0) {
198             perror("Cannot become session leader.");
199             exit(2);
200         }
201
202         // fork() again
203         i = fork();
204         if(i == -1) {
205             perror("cannot fork");
206             exit(1);
207         }
208         if(i != 0) {
209             exit(0); // the parent
210         }
211     }
212
213     // generate our pid file
214     int pidfd = -1;
215     if(pidfile[0]) {
216         pidfd = open(pidfile, O_WRONLY | O_CREAT, 0644);
217         if(pidfd >= 0) {
218             if(ftruncate(pidfd, 0) != 0)
219                 error("Cannot truncate pidfile '%s'.", pidfile);
220
221             char b[100];
222             sprintf(b, "%d\n", getpid());
223             ssize_t i = write(pidfd, b, strlen(b));
224             if(i <= 0)
225                 error("Cannot write pidfile '%s'.", pidfile);
226         }
227         else error("Failed to open pidfile '%s'.", pidfile);
228     }
229
230     // Set new file permissions
231     umask(0002);
232
233     // adjust my Out-Of-Memory score
234     oom_score_adj(1000);
235
236     // never become a problem
237     if(sched_setscheduler_idle() != 0) {
238         if(nice(19) == -1) error("Cannot lower my CPU priority.");
239         else debug(D_SYSTEM, "Set my nice value to 19.");
240     }
241
242     if(user && *user) {
243         if(become_user(user, pidfd) != 0) {
244             error("Cannot become user '%s'. Continuing as we are.", user);
245         }
246         else debug(D_SYSTEM, "Successfully became user '%s'.", user);
247     }
248     else {
249         create_needed_dir(CACHE_DIR, getuid(), getgid());
250         create_needed_dir(VARLIB_DIR, getuid(), getgid());
251     }
252
253     if(pidfd != -1)
254         close(pidfd);
255
256     return(0);
257 }