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