aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: ab630c600f16c87121872c2619721f86db1a907c (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
/* This file is part of Ping903
   Copyright (C) 2020 Sergey Poznyakoff
  
   Ping903 is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
  
   Ping903 is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with Ping903.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sysexits.h>
#include "ping903.h"
#include "json.h"
#include "defs.h"

static char *config_file = DEFAULT_CONFIG_FILE;
int verbose;

enum state {
	RUNNING,         /* Program is running */
	TERMINATING,     /* Program is terminating */
	CHILD_TERM,      /* SIGTERM has been sent to the child */
	CHILD_KILL
};
volatile enum state state;

static void
sigterm(int sig)
{
	if (state == RUNNING)
		state = TERMINATING;
}

static void
sigalrm(int sig)
{
	if (state == CHILD_TERM)
		state = CHILD_KILL;
}

int fatal_signals[] = {
	SIGHUP,
	SIGINT,
	SIGQUIT,
	SIGTERM,
	0
};

static void
sentinel(void)
{
	pid_t pid = 0;
	int i;
	struct sigaction act;
	pid_t child_pid = 0;
	int status;
	
	act.sa_flags = 0;
	sigemptyset(&act.sa_mask);

	act.sa_handler = sigalrm;
	sigaction(SIGALRM, &act, NULL);

	for (i = 0; fatal_signals[i]; i++)
		sigaddset(&act.sa_mask, fatal_signals[i]);
	act.sa_handler = sigterm;
	for (i = 0; fatal_signals[i]; i++)
		sigaction(fatal_signals[i], &act, NULL);
	
	while (1) {
		if (pid == 0) {
			if (state != RUNNING)
				break;
			pid = fork();
			if (pid == -1) {
				error("fork: %s", strerror(errno));
				break;
			}
			if (pid == 0) {
				ping903();
				exit(0);
			}
		}
		
                if (child_pid > 0) {
			child_pid = 0;
			if (WIFEXITED(status)) {
				int code = WEXITSTATUS(status);
				if (code || verbose > 1)
					error("child exited with status %d",
					      code);
			} else if (WIFSIGNALED(status)) {
				char const *coremsg = "";
#ifdef WCOREDUMP
				if (WCOREDUMP(status))
					coremsg = " (core dumped)";
#endif
				error("child terminated on signal %d%s",
				      WTERMSIG(status), coremsg);
			} else if (WIFSTOPPED(status)) {
				error("child stopped on signal %d",
				      WSTOPSIG(status));
				continue;
			} else {
				error("child terminated with unrecognized status %d", status);
			}
			/* restart the child */
			pid = 0;
			continue;
		}

		switch (state) {
		case RUNNING:
			break;
			
		case TERMINATING:
			kill(pid, SIGTERM);
			alarm(5);
			state = CHILD_TERM;
			break;
		case CHILD_TERM:
			break;
			
		case CHILD_KILL:
			kill(pid, SIGKILL);
			return;
		}
		
		child_pid = wait(&status);
		if (child_pid == -1) {
			if (errno != EINTR || verbose > 1)
				error("wait: %s", strerror(errno));
		}
	}
}

char *pidfile;

void
pidfile_remove(void)
{
	if (pidfile && unlink(pidfile))
		error("cannot unlink pidfile `%s': %s",
		      pidfile, strerror(errno));
}

void
pidfile_create(void)
{
	FILE *fp;

	if (!pidfile)
		return;

	fp = fopen(pidfile, "w");
	if (!fp) {
		error("cannot create pidfile `%s': %s",
		      pidfile, strerror(errno));
		exit(1);
	}
	fprintf(fp, "%lu", (unsigned long) getpid());
	fclose(fp);
}

/* Check whether pidfile exists and if so, whether its PID is still
   active. Exit if it is. */
void
pidfile_check(void)
{
	unsigned long pid;
	FILE *fp;

	if (!pidfile)
		return;

	fp = fopen(pidfile, "r");

	if (fp) {
		if (fscanf(fp, "%lu", &pid) != 1) {
			error("cannot get pid from pidfile `%s'", pidfile);
		} else {
			if (kill(pid, 0) == 0) {
				error("%s appears to run with pid %lu. "
				      "If it does not, remove `%s' and retry.",
				      progname, pid, pidfile);
				exit(1);
			}
		}

		fclose(fp);
		if (unlink(pidfile)) {
			error("cannot unlink pidfile `%s': %s",
			      pidfile, strerror(errno));
			exit(1);
		}
	} else if (errno != ENOENT) {
		error("cannot open pidfile `%s': %s",
		      pidfile, strerror(errno));
		exit(1);
	}
}

void
usage(void)
{
	printf("Usage: %s [-fhsVv] [-c FILE]\n", progname);
	printf("High-performance ICMP monitoring daemon.\n");
	printf("\n");
	printf("Options:\n\n");
	printf("  -c FILE        read configuration from FILE\n");
	printf("  -f             remain in foreground\n");
	printf("  -h             print this help test\n");
	printf("  -s             don't start supervisor process\n");
	printf("  -V             print program version and exit\n");
	printf("  -v             additional verbosity\n");
	printf("\n");
	printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
	printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
}

void
version(void)
{
	printf("%s\n", PACKAGE_STRING);
	printf("%s", COPYLEFT);
}

int
main(int argc, char **argv)
{
	int c;
	int foreground = 0;
	int single_process = 0;

	set_progname(argv[0]);

	if (argc == 2) {
		if (strcmp(argv[1], "--help") == 0) {
			usage();
			exit(0);
		}
		if (strcmp(argv[1], "--version") == 0) {
			version();
			exit(0);
		}
	}
	while ((c = getopt(argc, argv, "c:fhsVv")) != EOF) {
		switch (c) {
		case 'c':
			config_file = optarg;
			break;
		case 'f':
			foreground = 1;
			break;
		case 'h':
			usage();
			exit(0);
                case 's':
			single_process = 1;
			break;
                case 'v':
			verbose++;
			break;
		case 'V':
			version();
			exit(0);
                default:
			exit(EX_USAGE);
		}
	}

	pinger_setup();
	if (readconfig(config_file))
		exit(EX_CONFIG);
	
	pidfile_check();

        if (!foreground) {
		if (daemon(0, 0)) {
			error("daemon failed: %s", strerror(errno));
			exit(EX_OSERR);
		}
		syslog_enable();
	}
	pidfile_create();
        if (single_process)
		ping903();
	else
		sentinel();

	pidfile_remove();
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.