summary refs log tree commit diff
path: root/kitd.c
blob: 26cfa28183581d1e80447efdba13c97d12a6d292 (plain) (blame)
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
/* Copyright (C) 2023  June McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <syslog.h>
#include <unistd.h>

struct Line {
	size_t len;
	char buf[1024];
};

static void lineFill(struct Line *line, int fd) {
	size_t cap = sizeof(line->buf)-1 - line->len;
	ssize_t len = read(fd, &line->buf[line->len], cap);
	if (len < 0 && errno != EAGAIN) {
		syslog(LOG_ERR, "read: %m");
		return;
	}
	line->len += len;
}

static void lineFlush(struct Line *line, int priority) {
	assert(line->len < sizeof(line->buf));
	line->buf[line->len] = '\0';

	if (line->len == sizeof(line->buf)-1) {
		syslog(priority, "%s", line->buf);
		line->len = 0;
		return;
	}

	char *ptr = line->buf;
	for (char *nl; NULL != (nl = strchr(ptr, '\n')); ptr = &nl[1]) {
		*nl = '\0';
		syslog(priority, "%s", ptr);
	}
	line->len -= ptr - line->buf;
	memmove(line->buf, ptr, line->len);
}

static const char *humanize(const struct timespec *interval) {
	static char buf[256];
	if (!interval->tv_sec) {
		snprintf(buf, sizeof(buf), "%dms", (int)(interval->tv_nsec / 1000000));
		return buf;
	}
	int s = interval->tv_sec;
	int d = s / (24*60*60);
	s %= 24*60*60;
	int h = s / (60*60);
	s %= 60*60;
	int m = s / 60;
	s %= 60;
	if (d) {
		snprintf(buf, sizeof(buf), "%dd %dh %dm %ds", d, h, m, s);
	} else if (h) {
		snprintf(buf, sizeof(buf), "%dh %dm %ds", h, m, s);
	} else if (m) {
		snprintf(buf, sizeof(buf), "%dm %ds", m, s);
	} else {
		snprintf(buf, sizeof(buf), "%ds", s);
	}
	return buf;
}

static volatile sig_atomic_t signals[NSIG];
static void signalHandler(int signal) {
	signals[signal] = 1;
}

static void parseInterval(struct timespec *interval, const char *millis) {
	unsigned long ms = strtoul(millis, NULL, 10);
	interval->tv_sec = ms / 1000;
	interval->tv_nsec = 1000000 * (ms % 1000);
}

int main(int argc, char *argv[]) {
	int error;

	bool daemonize = true;
	const char *name = NULL;
	struct timespec cooloff = { .tv_sec = 15 * 60 };
	struct timespec restart = { .tv_sec = 1 };
	for (int opt; 0 < (opt = getopt(argc, argv, "c:dn:t:"));) {
		switch (opt) {
			break; case 'c': parseInterval(&cooloff, optarg);
			break; case 'd': daemonize = false;
			break; case 'n': name = optarg;
			break; case 't': parseInterval(&restart, optarg);
			break; default: return 1;
		}
	}
	argc -= optind;
	argv += optind;
	if (!argc) errx(1, "no command");
	if (!name) {
		name = strrchr(argv[0], '/');
		name = (name ? &name[1] : argv[0]);
	}

	error = pledge("stdio rpath wpath proc exec", NULL);
	if (error) err(1, "pledge");

	int stdoutRW[2];
	error = pipe2(stdoutRW, O_CLOEXEC);
	if (error) err(1, "pipe2");

	int stderrRW[2];
	error = pipe2(stderrRW, O_CLOEXEC);
	if (error) err(1, "pipe2");

	fcntl(stdoutRW[0], F_SETFL, O_NONBLOCK);
	fcntl(stderrRW[0], F_SETFL, O_NONBLOCK);

	openlog(name, LOG_NDELAY | LOG_PID | LOG_PERROR, LOG_DAEMON);
	if (daemonize) {
		error = daemon(0, 0);
		if (error) {
			syslog(LOG_ERR, "daemon: %m");
			return 1;
		}
	}
	setproctitle("%s", name);

	signal(SIGHUP, signalHandler);
	signal(SIGINT, signalHandler);
	signal(SIGTERM, signalHandler);
	signal(SIGCHLD, signalHandler);
	signal(SIGINFO, signalHandler);
	signal(SIGUSR1, signalHandler);
	signal(SIGUSR2, signalHandler);

	pid_t child = 0;
	bool stop = false;
	struct timespec now = {0};
	struct timespec uptime = {0};
	struct timespec timeout = {0};
	struct timespec deadline = {0};
	struct timespec interval = restart;

	sigset_t mask;
	sigemptyset(&mask);
	struct pollfd fds[2] = {
		{ .fd = stdoutRW[0], .events = POLLIN },
		{ .fd = stderrRW[0], .events = POLLIN },
	};
	struct Line stdoutBuffer = {0};
	struct Line stderrBuffer = {0};

	for (;;) {
		if (signals[SIGHUP]) {
			interval = restart;
			if (child) killpg(child, SIGTERM);
			signals[SIGHUP] = 0;
		}

		if (signals[SIGINT] || signals[SIGTERM]) {
			stop = true;
			int sig = (signals[SIGINT] ? SIGINT : SIGTERM);
			if (child) {
				killpg(child, sig);
			} else {
				break;
			}
			signals[sig] = 0;
		}

		if (signals[SIGINFO]) {
			clock_gettime(CLOCK_MONOTONIC, &now);
			if (child) {
				timespecsub(&now, &uptime, &timeout);
				syslog(LOG_INFO, "child %d up %s", child, humanize(&timeout));
			} else {
				timespecsub(&deadline, &now, &timeout);
				syslog(LOG_INFO, "restarting in %s", humanize(&timeout));
			}
			signals[SIGINFO] = 0;
		}

		if (signals[SIGUSR1] || signals[SIGUSR2]) {
			int sig = (signals[SIGUSR1] ? SIGUSR1 : SIGUSR2);
			if (child) killpg(child, sig);
			signals[sig] = 0;
		}

		if (signals[SIGCHLD]) {
			int status;
			pid_t pid = wait(&status);
			signals[SIGCHLD] = 0;
			if (pid < 0) {
				syslog(LOG_ERR, "wait: %m");
				continue;
			}
			if (pid != child) {
				syslog(LOG_NOTICE, "unknown child %d", pid);
				continue;
			}
			child = 0;
			setproctitle("%s", name);

			if (WIFEXITED(status)) {
				int exit = WEXITSTATUS(status);
				if (exit == 127) stop = true;
				if (exit) syslog(LOG_NOTICE, "child exited %d", exit);
			} else if (WIFSIGNALED(status)) {
				int sig = WTERMSIG(status);
				if (sig != SIGTERM) {
					syslog(LOG_NOTICE, "child got signal %s", sys_signame[sig]);
				}
			}

			if (stop) break;
			clock_gettime(CLOCK_MONOTONIC, &now);
			timespecsub(&now, &uptime, &uptime);
			if (timespeccmp(&uptime, &cooloff, >=)) {
				interval = restart;
			}
			syslog(LOG_INFO, "restarting in %s", humanize(&interval));
			timespecadd(&now, &interval, &deadline);
			timespecadd(&interval, &interval, &interval);
		}

		if (!child) {
			clock_gettime(CLOCK_MONOTONIC, &now);
			if (timespeccmp(&deadline, &now, <)) {
				timespecclear(&timeout);
			} else {
				timespecsub(&deadline, &now, &timeout);
			}
		}
		int nfds = ppoll(fds, 2, (child ? NULL : &timeout), &mask);
		if (nfds < 0 && errno != EINTR) {
			syslog(LOG_ERR, "ppoll: %m");
			continue;
		}

		if (nfds > 0 && fds[0].revents) {
			lineFill(&stdoutBuffer, fds[0].fd);
			lineFlush(&stdoutBuffer, LOG_INFO);
		}
		if (nfds > 0 && fds[1].revents) {
			lineFill(&stderrBuffer, fds[1].fd);
			lineFlush(&stderrBuffer, LOG_NOTICE);
		}

		if (!child) {
			child = fork();
			if (child < 0) {
				syslog(LOG_ERR, "fork: %m");
				return 1;
			}
			if (child) {
				clock_gettime(CLOCK_MONOTONIC, &uptime);
				setproctitle("%s [%d]", name, child);
			} else {
				setpgid(0, 0);
				dup2(stdoutRW[1], STDOUT_FILENO);
				dup2(stderrRW[1], STDERR_FILENO);
				execvp(argv[0], (char *const *)argv);
				err(127, "%s", argv[0]);
			}
		}
	}
}