-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemond.c
175 lines (155 loc) · 3.85 KB
/
daemond.c
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
/* daemond - process supervisor that can run as PID 1 (init) */
#include <dirent.h>
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "service.h"
#include "util.h"
#ifndef ismodified
#define ismodified(newtime, prevtime) ( \
newtime.tv_sec != prevtime.tv_sec || newtime.tv_nsec != prevtime.tv_nsec \
)
#endif
const char *argv0;
char **next_program;
time_t timeout;
sigset_t sigmask_sync;
volatile sig_atomic_t termflag;
Service *services;
static void usage(void) {
dprintf(2, "usage: %s [-t timeout] [next_program [arg...]]\n", argv0);
exit(1);
}
static void scan(void) {
{
static struct timespec scantime;
struct stat st;
if (stat(execdir, &st) < 0) {
LOG("failed to stat execdir: %s", err());
} else if (ismodified(st.st_mtim, scantime)) {
scantime = st.st_mtim;
} else {
return;
}
}
// i don't like dirent
DIR *dir = opendir(execdir);
if (!dir) LOG("failed to open execdir: %s", err());
struct dirent *srvfile;
while ((srvfile = readdir(dir))) {
if (*srvfile->d_name == '.') continue;
Service **pos = service_from_name(&services, srvfile->d_name);
if (*pos) continue;
Service *srv = service(srvfile->d_name);
if (!srv) continue;
service_spawn(srv);
if (srv->pid > 0) {
service_insert(pos, srv);
LOG("%s service added", srv->name); service_destroy(srv);
continue;
}
service_destroy(srv);
}
closedir(dir);
}
static void reap(void) {
int status;
pid_t pid;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
Service **srv = service_from_pid(&services, pid);
const char *name = *srv ? (*srv)->name : "";
if (WIFEXITED(status)) {
LOG("%s[%li] exited with code %i",
name, (long)pid, (int)WEXITSTATUS(status)
);
} else {
int sig = WTERMSIG(status);
LOG("%s[%li] terminated by signal %s[%i]",
name, (long)pid, strsignal(sig), sig
);
}
if (*srv) {
service_spawn(*srv);
if ((*srv)->pid < 0) {
service_destroy(service_delete(srv));
LOG("%s service removed", name);
}
}
}
}
static void loop(void) {
scan();
reap();
int nfds = 0;
fd_set readfds;
FD_ZERO(&readfds);
for (Service *srv = services; srv; srv = srv->next) {
if (srv->killfd < 0) continue;
if (srv->killfd >= nfds) nfds = srv->killfd + 1;
FD_SET(srv->killfd, &readfds);
}
nfds = pselect(nfds, &readfds, NULL, NULL,
timeout > 0 ? &(struct timespec){.tv_sec = timeout} : NULL,
&sigmask_sync
);
for (Service *srv = services; srv && nfds > 0; srv = srv->next) {
if (FD_ISSET(srv->killfd, &readfds)) {
service_handlekill(srv);
--nfds;
}
}
}
static void terminate(int sig) {
termflag = 1;
}
static void signop(int sig) {/* interrupts pselect */}
static void exec_next(void) {
execvp(*next_program, next_program);
LOG("failed to exec next_program: %s", err());
}
int main(int argc, char **argv) {
int c;
argv0 = *argv;
while ((c = getopt(argc, argv, "t:")) >= 0) {
switch (c) {
case 't':
{
char *endptr;
long i = strtol(optarg, &endptr, 10);
if (!*optarg || *endptr || i < 0) usage();
timeout = i;
break;
}
default:
usage();
}
}
next_program = argv + optind;
if (*next_program) atexit(exec_next);
struct sigaction sa = {0};
errno = 0;
if (sigemptyset(&sa.sa_mask) < 0) {
DIE("failed to init signal mask: %s", err());
}
sigaddset(&sa.sa_mask, SIGCHLD);
sigaddset(&sa.sa_mask, SIGINT);
sigaddset(&sa.sa_mask, SIGTERM);
if (errno || sigprocmask(SIG_BLOCK, &sa.sa_mask, &sigmask_sync) < 0) {
DIE("failed to set signal mask: %s", err());
}
sa.sa_handler = terminate;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sa.sa_handler = signop;
sa.sa_flags = SA_NOCLDSTOP;
sigaction(SIGCHLD, &sa, NULL);
if (errno) DIE("failed to set signal handlers: %s", err());
while (!termflag) loop();
}