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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/* Copyright (C) 2020 C. 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 <limits.h>
#include <paths.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 <time.h>
#include <unistd.h>
#include "daemon.h"
enum {
ExitNotFound = 127,
ExitNoExec = 126,
};
const char *serviceDir = "/";
uid_t serviceUID;
gid_t serviceGID;
char *serviceEnviron[EnvironLen] = {
[SHELL] = "SHELL=" _PATH_BSHELL,
[PATH] = "PATH=" _PATH_DEFPATH,
};
struct Prepend prepend;
struct Services services;
static void serviceFree(struct Service *service) {
free(service->name);
free(service->command);
if (service->outPipe[0] >= 0) {
close(service->outPipe[0]);
close(service->outPipe[1]);
}
if (service->errPipe[0] >= 0) {
close(service->errPipe[0]);
close(service->errPipe[1]);
}
}
int serviceAdd(const char *name, const char *command) {
struct Service *service = NULL;
for (size_t i = 0; i < services.len; ++i) {
if (!strcmp(services.ptr[i].name, name)) {
service = &services.ptr[i];
break;
}
}
if (service) {
char *dup = strdup(command);
if (!dup) return -1;
free(service->command);
service->command = dup;
return 0;
}
if (services.len == services.cap) {
size_t cap = (services.cap ? services.cap * 2 : 8);
void *ptr = realloc(services.ptr, sizeof(*services.ptr) * cap);
if (!ptr) return -1;
services.cap = cap;
services.ptr = ptr;
}
service = &services.ptr[services.len];
*service = (struct Service) {
.outPipe = { -1, -1 },
.errPipe = { -1, -1 },
.restartInterval = restartInterval,
};
service->name = strdup(name);
if (!service->name) goto err;
service->command = strdup(command);
if (!service->command) goto err;
if (name[0] == '@') service->privileged = true;
int error = pipe2(service->outPipe, O_CLOEXEC);
if (error) goto err;
error = pipe2(service->errPipe, O_CLOEXEC);
if (error) goto err;
fcntl(service->outPipe[0], F_SETFL, O_NONBLOCK);
fcntl(service->errPipe[0], F_SETFL, O_NONBLOCK);
services.len++;
return 0;
err:
serviceFree(service);
return -1;
}
void serviceDrop(size_t index) {
struct Service *service = &services.ptr[index];
if (service->state != Stop) return;
syslog(LOG_NOTICE, "%s[] dropped", service->name);
serviceFree(service);
services.ptr[index] = services.ptr[--services.len];
}
static const char *humanize(struct timespec uptime) {
static char buf[256];
int days = uptime.tv_sec / (24 * 60 * 60);
uptime.tv_sec %= (24 * 60 * 60);
int hours = uptime.tv_sec / (60 * 60);
uptime.tv_sec %= (60 * 60);
int mins = uptime.tv_sec / 60;
uptime.tv_sec %= 60;
int d, h, m, s;
snprintf(
buf, sizeof(buf), "%n%dd %n%dh %n%dm %n%ds",
&d, days, &h, hours, &m, mins, &s, (int)uptime.tv_sec
);
if (days) return &buf[d];
if (hours) return &buf[h];
if (mins) return &buf[m];
return &buf[s];
}
void serviceStatus(struct Service *service) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (service->state == Stop && service->intent == Stop) {
syslog(LOG_NOTICE, "%s[] is stopped", service->name);
} else if (service->state == Stop && service->intent == Start) {
struct timespec timeleft;
timespecsub(&service->restartDeadline, &now, &timeleft);
syslog(
LOG_NOTICE, "%s[] is restarting in %ds",
service->name, (int)timeleft.tv_sec
);
} else if (service->state == Stop && service->intent == Restart) {
syslog(LOG_NOTICE, "%s[] is restarting", service->name);
} else if (service->state == Start && service->intent == Start) {
struct timespec uptime;
timespecsub(&now, &service->startTime, &uptime);
syslog(
LOG_NOTICE, "%s[%d] is started (up %s)",
service->name, service->pid, humanize(uptime)
);
} else if (service->state == Start && service->intent == Stop) {
syslog(LOG_NOTICE, "%s[%d] is stopping", service->name, service->pid);
} else if (service->state == Start && service->intent == Restart) {
syslog(
LOG_NOTICE, "%s[%d] is stopping for restart",
service->name, service->pid
);
}
}
static void setDeadline(struct Service *service) {
clock_gettime(CLOCK_MONOTONIC, &service->restartDeadline);
timespecadd(
&service->restartDeadline, &service->restartInterval,
&service->restartDeadline
);
}
void serviceStart(struct Service *service) {
if (service->state != Stop) return;
if (service->intent == Start) {
struct timespec backoff = service->restartInterval;
timespecadd(&backoff, &backoff, &service->restartInterval);
} else {
service->restartInterval = restartInterval;
}
setDeadline(service);
service->intent = Start;
service->pid = fork();
if (service->pid < 0) {
syslog(LOG_WARNING, "fork: %m");
return;
}
if (service->pid) {
service->state = Start;
clock_gettime(CLOCK_MONOTONIC, &service->startTime);
syslog(LOG_NOTICE, "%s[%d] started", service->name, service->pid);
return;
}
setpgid(0, 0);
dup2(service->outPipe[1], STDOUT_FILENO);
dup2(service->errPipe[1], STDERR_FILENO);
int error = chdir(serviceDir);
if (error) err(ExitNoExec, "%s", serviceDir);
if (!service->privileged) {
error = setgid(serviceGID);
if (error) err(ExitNoExec, "setgid");
if (!getuid()) {
error = setgroups(1, &serviceGID);
if (error) err(ExitNoExec, "setgroups");
}
error = setuid(serviceUID);
if (error) err(ExitNoExec, "setuid");
}
size_t len = 0;
char command[ARG_MAX];
for (size_t i = 0; i < prepend.len; ++i) {
int n = snprintf(
&command[len], sizeof(command) - len, "%s;", prepend.commands[i]
);
assert(n > 0);
len += n;
if (len >= sizeof(command)) errx(ExitNoExec, "command truncated");
}
int n = snprintf(
&command[len], sizeof(command) - len, "exec %s", service->command
);
assert(n > 0);
len += n;
if (len >= sizeof(command)) errx(ExitNoExec, "command truncated");
execle(
_PATH_BSHELL,
_PATH_BSHELL, "-c", command, service->name, NULL,
serviceEnviron
);
err(ExitNoExec, "%s", _PATH_BSHELL);
}
void serviceSignal(struct Service *service, int signal) {
if (service->state != Start) return;
int error = killpg(service->pid, signal);
if (error) {
syslog(
LOG_WARNING, "killpg(%s[%d], %s): %m",
service->name, service->pid, sys_signame[signal]
);
}
}
void serviceStop(struct Service *service) {
service->intent = Stop;
serviceSignal(service, SIGTERM);
}
void serviceRestart(struct Service *service) {
service->intent = Restart;
if (service->state == Start) {
serviceSignal(service, SIGTERM);
} else {
serviceStart(service);
}
}
static void serviceLog(struct Service *service, const char *log) {
syslog(LOG_NOTICE, "%s[%d]: %s", service->name, service->pid, log);
}
void serviceRead(struct Service *service) {
const char *out;
while (NULL != (out = lineRead(&service->outLine, service->outPipe[0]))) {
serviceLog(service, out);
}
if (errno != EAGAIN) syslog(LOG_WARNING, "read: %m");
const char *err;
while (NULL != (err = lineRead(&service->errLine, service->errPipe[0]))) {
serviceLog(service, err);
}
if (errno != EAGAIN) syslog(LOG_WARNING, "read: %m");
}
static void serviceFlush(struct Service *service) {
const char *out = lineFlush(&service->outLine);
const char *err = lineFlush(&service->errLine);
if (out) serviceLog(service, out);
if (err) serviceLog(service, err);
}
void serviceReap(pid_t pid, int status) {
struct Service *service = NULL;
for (size_t i = 0; i < services.len; ++i) {
if (services.ptr[i].state != Start) continue;
if (services.ptr[i].pid != pid) continue;
service = &services.ptr[i];
break;
}
if (!service) {
syslog(LOG_WARNING, "reaping unknown child %d", pid);
return;
}
serviceFlush(service);
service->state = Stop;
if (WIFEXITED(status)) {
int exit = WEXITSTATUS(status);
if (
exit == ExitNotFound ||
exit == ExitNoExec ||
setTest(&stopExits, exit)
) {
service->intent = Stop;
}
if (exit) {
syslog(
LOG_WARNING, "%s[%d] exited %d",
service->name, pid, exit
);
}
} else if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM) {
syslog(
LOG_WARNING, "%s[%d] signaled %s",
service->name, pid, sys_signame[WTERMSIG(status)]
);
}
if (service->intent == Start) {
struct timespec uptime;
clock_gettime(CLOCK_MONOTONIC, &uptime);
timespecsub(&uptime, &service->startTime, &uptime);
if (timespeccmp(&uptime, &resetInterval, >=)) {
service->restartInterval = restartInterval;
}
setDeadline(service);
syslog(
LOG_NOTICE, "%s[%d] restarting in %ds",
service->name, pid, (int)service->restartInterval.tv_sec
);
} else {
syslog(LOG_NOTICE, "%s[%d] stopped", service->name, pid);
}
if (service->intent == Restart) {
serviceStart(service);
}
}
|