catsit/service.c

362 lines
9.2 KiB
C
Raw Normal View History

2020-08-14 18:52:51 +02:00
/* 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/>.
*/
2020-08-14 23:36:31 +02:00
#include <assert.h>
#include <err.h>
#include <errno.h>
2020-08-14 22:51:32 +02:00
#include <fcntl.h>
2020-08-14 23:36:31 +02:00
#include <limits.h>
2020-08-14 22:17:29 +02:00
#include <paths.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
2020-08-14 18:52:51 +02:00
#include <stdio.h>
#include <stdlib.h>
2020-08-14 21:56:10 +02:00
#include <string.h>
#include <sys/time.h>
2020-08-15 00:38:14 +02:00
#include <sys/wait.h>
2020-08-14 23:36:31 +02:00
#include <syslog.h>
#include <time.h>
2020-08-14 22:17:29 +02:00
#include <unistd.h>
2020-08-14 18:52:51 +02:00
#include "daemon.h"
enum {
ExitNotFound = 127,
ExitNoExec = 126,
};
2020-08-14 18:52:51 +02:00
const char *serviceDir = "/";
2020-08-14 22:17:29 +02:00
uid_t serviceUID;
gid_t serviceGID;
char *serviceEnviron[EnvironLen] = {
[SHELL] = "SHELL=" _PATH_BSHELL,
[PATH] = "PATH=" _PATH_DEFPATH,
};
2020-08-14 21:56:10 +02:00
struct Prepend prepend;
struct Services services;
static void serviceFree(struct Service *service) {
free(service->name);
free(service->command);
2020-08-14 22:51:32 +02:00
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]);
}
2020-08-14 21:56:10 +02:00
}
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];
2020-08-14 22:51:32 +02:00
*service = (struct Service) {
.outPipe = { -1, -1 },
.errPipe = { -1, -1 },
.restartInterval = restartInterval,
};
2020-08-14 21:56:10 +02:00
service->name = strdup(name);
if (!service->name) goto err;
service->command = strdup(command);
if (!service->command) goto err;
if (name[0] == '@') service->privileged = true;
2020-08-14 22:51:32 +02:00
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);
2020-08-14 21:56:10 +02:00
services.len++;
return 0;
err:
serviceFree(service);
return -1;
}
2020-08-14 23:36:31 +02:00
2020-08-17 05:01:25 +02:00
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];
}
2020-08-18 04:39:54 +02:00
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];
}
2020-08-15 21:29:58 +02:00
void serviceStatus(struct Service *service) {
2020-08-18 04:39:54 +02:00
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
2020-08-15 21:29:58 +02:00
if (service->state == Stop && service->intent == Stop) {
syslog(LOG_NOTICE, "%s[] is stopped", service->name);
2020-08-15 21:29:58 +02:00
} else if (service->state == Stop && service->intent == Start) {
2020-08-18 04:39:54 +02:00
struct timespec timeleft;
2020-08-15 21:29:58 +02:00
timespecsub(&service->restartDeadline, &now, &timeleft);
syslog(
2020-10-21 01:55:11 +02:00
LOG_NOTICE, "%s[] is restarting in %s",
service->name, humanize(timeleft)
2020-08-15 21:29:58 +02:00
);
} else if (service->state == Stop && service->intent == Restart) {
syslog(LOG_NOTICE, "%s[] is restarting", service->name);
2020-08-15 21:29:58 +02:00
} else if (service->state == Start && service->intent == Start) {
2020-08-18 04:39:54 +02:00
struct timespec uptime;
timespecsub(&now, &service->startTime, &uptime);
syslog(
LOG_NOTICE, "%s[%d] is started (up %s)",
service->name, service->pid, humanize(uptime)
);
2020-08-15 21:29:58 +02:00
} else if (service->state == Start && service->intent == Stop) {
syslog(LOG_NOTICE, "%s[%d] is stopping", service->name, service->pid);
2020-08-15 21:29:58 +02:00
} else if (service->state == Start && service->intent == Restart) {
syslog(
LOG_NOTICE, "%s[%d] is stopping for restart",
2020-08-15 21:57:45 +02:00
service->name, service->pid
2020-08-15 21:29:58 +02:00
);
}
}
2020-08-15 00:38:14 +02:00
static void setDeadline(struct Service *service) {
clock_gettime(CLOCK_MONOTONIC, &service->restartDeadline);
2020-08-15 00:38:14 +02:00
timespecadd(
&service->restartDeadline, &service->restartInterval,
&service->restartDeadline
);
}
2020-08-14 23:36:31 +02:00
void serviceStart(struct Service *service) {
if (service->state != Stop) return;
2020-08-14 23:36:31 +02:00
if (service->intent == Start) {
struct timespec backoff = service->restartInterval;
timespecadd(&backoff, &backoff, &service->restartInterval);
} else {
service->restartInterval = restartInterval;
}
2020-08-15 00:38:14 +02:00
setDeadline(service);
service->intent = Start;
2020-08-14 23:36:31 +02:00
service->pid = fork();
if (service->pid < 0) {
syslog(LOG_WARNING, "fork: %m");
2020-08-14 23:36:31 +02:00
return;
}
if (service->pid) {
service->state = Start;
clock_gettime(CLOCK_MONOTONIC, &service->startTime);
syslog(LOG_NOTICE, "%s[%d] started", service->name, service->pid);
2020-08-14 23:36:31 +02:00
return;
}
setpgid(0, 0);
2020-08-14 23:36:31 +02:00
dup2(service->outPipe[1], STDOUT_FILENO);
dup2(service->errPipe[1], STDERR_FILENO);
int error = chdir(serviceDir);
if (error) err(ExitNoExec, "%s", serviceDir);
2020-08-14 23:36:31 +02:00
if (!service->privileged) {
error = setgid(serviceGID);
if (error) err(ExitNoExec, "setgid");
2020-08-14 23:36:31 +02:00
if (!getuid()) {
error = setgroups(1, &serviceGID);
if (error) err(ExitNoExec, "setgroups");
}
2020-08-16 22:13:43 +02:00
error = setuid(serviceUID);
if (error) err(ExitNoExec, "setuid");
}
2020-08-14 23:36:31 +02:00
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");
2020-08-14 23:36:31 +02:00
}
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");
2020-08-14 23:36:31 +02:00
execle(
_PATH_BSHELL,
_PATH_BSHELL, "-c", command, service->name, NULL,
serviceEnviron
);
err(ExitNoExec, "%s", _PATH_BSHELL);
2020-08-14 23:36:31 +02:00
}
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",
2020-08-15 21:57:45 +02:00
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);
}
}
2020-08-15 00:38:14 +02:00
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);
}
2020-08-15 00:38:14 +02:00
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) {
2020-08-15 21:57:45 +02:00
syslog(LOG_WARNING, "reaping unknown child %d", pid);
2020-08-15 00:38:14 +02:00
return;
}
serviceFlush(service);
2020-08-15 00:38:14 +02:00
service->state = Stop;
if (WIFEXITED(status)) {
int exit = WEXITSTATUS(status);
if (
exit == ExitNotFound ||
exit == ExitNoExec ||
setTest(&stopExits, exit)
) {
2020-08-15 00:38:14 +02:00
service->intent = Stop;
}
if (exit) {
syslog(
2020-08-15 21:57:45 +02:00
LOG_WARNING, "%s[%d] exited %d",
service->name, pid, exit
2020-08-15 00:38:14 +02:00
);
}
2020-08-16 00:36:30 +02:00
} else if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM) {
2020-08-15 00:38:14 +02:00
syslog(
2020-08-15 21:57:45 +02:00
LOG_WARNING, "%s[%d] signaled %s",
service->name, pid, sys_signame[WTERMSIG(status)]
2020-08-15 00:38:14 +02:00
);
}
if (service->intent == Start) {
struct timespec uptime;
clock_gettime(CLOCK_MONOTONIC, &uptime);
timespecsub(&uptime, &service->startTime, &uptime);
if (timespeccmp(&uptime, &resetInterval, >=)) {
service->restartInterval = restartInterval;
}
2020-08-15 00:38:14 +02:00
setDeadline(service);
syslog(
2020-10-21 01:55:11 +02:00
LOG_NOTICE, "%s[%d] restarting in %s",
service->name, pid, humanize(service->restartInterval)
2020-08-15 00:38:14 +02:00
);
} else {
syslog(LOG_NOTICE, "%s[%d] stopped", service->name, pid);
2020-08-15 00:38:14 +02:00
}
if (service->intent == Restart) {
serviceStart(service);
}
}