summaryrefslogtreecommitdiffstats
path: root/src/daemon.c
blob: 268814798af9543714b30192ce23f62ff3dff0d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <execinfo.h>

#include "common.h"
#include "appconfig.h"
#include "log.h"
#include "web_client.h"
#include "plugins_d.h"
#include "rrd.h"
#include "popen.h"
#include "main.h"
#include "daemon.h"

void sig_handler(int signo)
{
	switch(signo) {
		case SIGILL:
		case SIGABRT:
		case SIGFPE:
		case SIGSEGV:
		case SIGBUS:
		case SIGSYS:
		case SIGTRAP:
		case SIGXCPU:
		case SIGXFSZ:
			infoerr("Death signaled exit (signal %d).", signo);
			signal(signo, SIG_DFL);
			break;

		case SIGKILL:
		case SIGTERM:
		case SIGQUIT:
		case SIGINT:
		case SIGHUP:
		case SIGUSR1:
		case SIGUSR2:
			infoerr("Signaled exit (signal %d).", signo);
			signal(SIGPIPE, SIG_IGN);
			signal(SIGTERM, SIG_IGN);
			signal(SIGQUIT, SIG_IGN);
			signal(SIGHUP,  SIG_IGN);
			signal(SIGINT,  SIG_IGN);
			signal(SIGCHLD, SIG_IGN);
			netdata_cleanup_and_exit(1);
			break;

		case SIGPIPE:
			infoerr("Signaled PIPE (signal %d).", signo);
			// this is received when web clients send a reset
			// no need to log it.
			// infoerr("Ignoring signal %d.", signo);
			break;

		default:
			info("Signal %d received. Falling back to default action for it.", signo);
			signal(signo, SIG_DFL);
			break;
	}
}

char rundir[FILENAME_MAX + 1] = "/var/run/netdata";
char pidfile[FILENAME_MAX + 1] = "";
void prepare_rundir() {
	if(getuid() != 0) {
		mkdir("/run/user", 0775);
		snprintf(rundir, FILENAME_MAX, "/run/user/%d", getuid());
		mkdir(rundir, 0775);
		snprintf(rundir, FILENAME_MAX, "/run/user/%d/netdata", getuid());
	}

	snprintf(pidfile, FILENAME_MAX, "%s/netdata.pid", rundir);

	if(mkdir(rundir, 0775) != 0) {
		if(errno != EEXIST) error("Cannot create directory '%s'.", rundir);
	}
}

int become_user(const char *username)
{
	struct passwd *pw = getpwnam(username);
	if(!pw) {
		error("User %s is not present.", username);
		return -1;
	}

	if(chown(rundir, pw->pw_uid, pw->pw_gid) != 0) {
		error("Cannot chown directory '%s' to user %s.", rundir, username);
		return -1;
	}

	if(setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
		error("Cannot switch to user's %s group (gid: %d).", username, pw->pw_gid);
		return -1;
	}

	if(setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
		error("Cannot switch to user %s (uid: %d).", username, pw->pw_uid);
		return -1;
	}

	if(setgid(pw->pw_gid) != 0) {
		error("Cannot switch to user's %s group (gid: %d).", username, pw->pw_gid);
		return -1;
	}
	if(setegid(pw->pw_gid) != 0) {
		error("Cannot effectively switch to user's %s group (gid: %d).", username, pw->pw_gid);
		return -1;
	}
	if(setuid(pw->pw_uid) != 0) {
		error("Cannot switch to user %s (uid: %d).", username, pw->pw_uid);
		return -1;
	}
	if(seteuid(pw->pw_uid) != 0) {
		error("Cannot effectively switch to user %s (uid: %d).", username, pw->pw_uid);
		return -1;
	}

	return(0);
}

int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
{
	fflush(NULL);

	// open the files before forking
	int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;

	if(input && *input) {
		if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
			error("Cannot open input file '%s'.", input);
			return -1;
		}
	}

	if(output && *output) {
		if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
			error("Cannot open output log file '%s'", output);
			if(input_fd != -1) close(input_fd);
			return -1;
		}
	}

	if(error && *error) {
		if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
			error("Cannot open error log file '%s'.", error);
			if(input_fd != -1) close(input_fd);
			if(output_fd != -1) close(output_fd);
			return -1;
		}
	}

	if(access && *access && access_fd) {
		if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
			error("Cannot open access log file '%s'", access);
			if(input_fd != -1) close(input_fd);
			if(output_fd != -1) close(output_fd);
			if(error_fd != -1) close(error_fd);
			return -1;
		}

		if(access_fp) {
			*access_fp = fdopen(*access_fd, "w");
			if(!*access_fp) {
				error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
				if(input_fd != -1) close(input_fd);
				if(output_fd != -1) close(output_fd);
				if(error_fd != -1) close(error_fd);
				close(*access_fd);
				*access_fd = -1;
				return -1;
			}
		}
	}

	if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
		perror("Cannot open /dev/null");
		if(input_fd != -1) close(input_fd);
		if(output_fd != -1) close(output_fd);
		if(error_fd != -1) close(error_fd);
		if(access && access_fd && *access_fd != -1) {
			close(*access_fd);
			*access_fd = -1;
			if(access_fp) {
				fclose(*access_fp);
				*access_fp = NULL;
			}
		}
		return -1;
	}

	// all files opened
	// lets do it

	if(!dont_fork) {
		int i = fork();
		if(i == -1) {
			perror("cannot fork");
			exit(1);
		}
		if(i != 0) {
			exit(0); // the parent
		}

		// become session leader
		if (setsid() < 0) {
			perror("Cannot become session leader.");
			exit(2);
		}
	}

	signal(SIGCHLD,  SIG_IGN);
	signal(SIGHUP,   SIG_IGN);
	signal(SIGWINCH, SIG_IGN);

	// fork() again
	if(!dont_fork) {
		int i = fork();
		if(i == -1) {
			perror("cannot fork");
			exit(1);
		}
		if(i != 0) {
			exit(0); // the parent
		}
	}

	// Set new file permissions
	umask(0);

	// close all files
	if(close_all_files) {
		int i;
		for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
			if(
				((access_fd && i != *access_fd) || !access_fd)
				&& i != dev_null
				&& i != input_fd
				&& i != output_fd
				&& i != error_fd
				&& fd_is_valid(i)
				) close(i);
	}
	else {
		close(STDIN_FILENO);
		close(STDOUT_FILENO);
		close(STDERR_FILENO);
	}

	// put the opened files
	// to our standard file descriptors
	if(input_fd != -1) {
		if(input_fd != STDIN_FILENO) {
			dup2(input_fd, STDIN_FILENO);
			close(input_fd);
		}
		input_fd = -1;
	}
	else dup2(dev_null, STDIN_FILENO);

	if(output_fd != -1) {
		if(output_fd != STDOUT_FILENO) {
			dup2(output_fd, STDOUT_FILENO);
			close(output_fd);
		}
		output_fd = -1;
	}
	else dup2(dev_null, STDOUT_FILENO);

	if(error_fd != -1) {
		if(error_fd != STDERR_FILENO) {
			dup2(error_fd, STDERR_FILENO);
			close(error_fd);
		}
		error_fd = -1;
	}
	else dup2(dev_null, STDERR_FILENO);

	// close /dev/null
	if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
		close(dev_null);

	// generate our pid file
	{
		unlink(pidfile);
		int fd = open(pidfile, O_RDWR | O_CREAT, 0666);
		if(fd >= 0) {
			char b[100];
			sprintf(b, "%d\n", getpid());
			ssize_t i = write(fd, b, strlen(b));
			if(i <= 0) perror("Cannot write pid to file.");
			close(fd);
		}
	}

	if(user && *user) {
		if(become_user(user) != 0) {
			error("Cannot become user '%s'. Continuing as we are.", user);
		}
		else info("Successfully became user '%s'.", user);
	}

	return(0);
}