-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsvhub.c
More file actions
345 lines (270 loc) · 5.75 KB
/
svhub.c
File metadata and controls
345 lines (270 loc) · 5.75 KB
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
#include <sys/creds.h>
#include <sys/file.h>
#include <sys/fpath.h>
#include <sys/fprop.h>
#include <sys/ppoll.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <format.h>
#include <main.h>
#include <sigset.h>
#include <string.h>
#include <util.h>
#include "common.h"
#include "svhub.h"
#define BOOTCLOCKOFFSET 1000
#define NPFDS (1+NCONNS+NPROCS)
const char* rbscript;
char** environ;
void* origbrk;
static short flagged;
static sigset_t defsigset;
static struct timespec timetowait;
time_t passtime;
static struct pollfd pfds[NPFDS];
static int npfds;
static short pfdkeys[NPFDS];
static short pollset;
/* A single handler for all signals we care about. */
static void sighandler(int sig)
{
switch(sig) {
case SIGPWR:
stop_into("poweroff");
break;
case SIGINT:
case SIGTERM:
stop_into("reboot");
break;
case SIGCHLD:
request(F_WAIT_PIDS);
break;
}
}
static int sigprocmask(int how, sigset_t* mask, sigset_t* mold)
{
int ret;
if((ret = sys_sigprocmask(how, mask, mold)) < 0)
report("sigprocmask", NULL, ret);
return ret;
}
static int sigaction(int sig, struct sigaction* sa, char* tag)
{
int ret;
if((ret = sys_sigaction(sig, sa, NULL)) < 0)
report("sigaction", tag, ret);
return ret;
}
int setup_signals(void)
{
SIGHANDLER(sa, sighandler, SA_RESTART);
int ret = 0;
sigemptyset(&sa.mask);
sigaddset(&sa.mask, SIGCHLD);
ret |= sigprocmask(SIG_BLOCK, &sa.mask, &defsigset);
sigaddset(&sa.mask, SIGINT);
sigaddset(&sa.mask, SIGPWR);
sigaddset(&sa.mask, SIGTERM);
sigaddset(&sa.mask, SIGHUP);
ret |= sigaction(SIGINT, &sa, NULL);
ret |= sigaction(SIGPWR, &sa, NULL);
ret |= sigaction(SIGTERM, &sa, NULL);
ret |= sigaction(SIGHUP, &sa, NULL);
/* SIGCHLD is only allowed to arrive in ppoll,
so SA_RESTART just does not make sense. */
sa.flags &= ~SA_RESTART;
ret |= sigaction(SIGCHLD, &sa, NULL);
if(ret) report("signal setup failed", NULL, 0);
return ret;
}
void set_passtime(void)
{
struct timespec tp = { 0, 0 };
long ret;
if((ret = sys_clock_gettime(CLOCK_MONOTONIC, &tp)) < 0)
report("clock_gettime", "CLOCK_MONOTONIC", ret);
else
passtime = BOOTCLOCKOFFSET + tp.sec;
}
void wakeupin(int seconds)
{
time_t ttw = timetowait.sec;
if(ttw >= 0 && ttw < seconds)
return;
timetowait.sec = seconds;
timetowait.nsec = 0;
}
static void close_proc_pipe(struct proc* rc)
{
sys_close(rc->pipefd);
rc->pipefd = -1;
pollset = 0;
}
static void close_conn(struct conn* cn)
{
sys_close(cn->fd);
memzero(cn, sizeof(*cn));
pollset = 0;
}
static void close_ctrl(int fd)
{
sys_close(fd);
ctrlfd = -1;
pollset = 0;
}
static void recv_ctrl(struct pollfd* pf)
{
if(pf->revents & POLLIN)
accept_ctrl(pf->fd);
if(pf->revents & ~POLLIN)
close_ctrl(pf->fd);
}
static void recv_conn(struct pollfd* pf, struct conn* cn)
{
if(pf->revents & POLLIN)
handle_conn(cn);
if(pf->revents & ~POLLIN)
close_conn(cn);
}
static void recv_proc(struct pollfd* pf, struct proc* rc)
{
if(pf->revents & POLLIN)
if(read_into_ring_buf(rc, pf->fd) >= 0)
return;
if(pf->revents)
close_proc_pipe(rc);
}
static void check_polled_fds(void)
{
int i, key;
for(i = 0; i < npfds; i++)
if((key = pfdkeys[i]) == 0)
recv_ctrl(&pfds[i]);
else if(key > 0)
recv_proc(&pfds[i], &procs[key-1]);
else if(key < 0)
recv_conn(&pfds[i], &conns[-key-1]);
}
static void add_polled_fd(int fd, int key)
{
if(npfds >= NPFDS)
return;
int i = npfds++;
struct pollfd* pf = &pfds[i];
pf->fd = fd;
pf->events = POLLIN;
pfdkeys[i] = key;
}
void update_poll_fds(void)
{
int i, fd;
npfds = 0;
if(ctrlfd > 0)
add_polled_fd(ctrlfd, 0);
for(i = 0; i < nconns; i++)
if((fd = conns[i].fd) > 0)
add_polled_fd(fd, -1 - i);
for(i = 0; i < nprocs; i++)
if((fd = procs[i].pipefd) > 0)
add_polled_fd(fd, 1 + i);
}
static void msleep(int ms)
{
struct timespec sp = { ms/1000, (ms%1000) * 1000000 };
sys_nanosleep(&sp, NULL);
}
void wait_poll(void)
{
struct timespec* ts;
if(timetowait.sec > 0 || timetowait.nsec > 0)
ts = &timetowait;
else
ts = NULL;
if(!pollset)
update_poll_fds();
passtime = 0;
int r = sys_ppoll(pfds, npfds, ts, &defsigset);
if(r == -EINTR) {
/* we're ok here, sighandlers did their job */
} else if(r < 0) {
report("ppoll", NULL, r);
msleep(1000);
} else if(r > 0) {
check_polled_fds();
} else { /* timeout has been reached */
timetowait.sec = 0;
timetowait.nsec = 0;
request(F_CHECK_PROCS);
}
}
int stop_into(const char* script)
{
int ret;
FMTBUF(p, e, path, 100);
p = fmtstr(p, e, BOOTDIR "/");
p = fmtstr(p, e, script);
FMTEND(p, e);
if((ret = sys_access(path, X_OK)) < 0)
return ret;
rbscript = script;
stop_all_procs();
return 0;
}
static int exec_next(void)
{
FMTBUF(p, e, path, 100);
p = fmtstr(p, e, BOOTDIR "/");
p = fmtstr(p, e, rbscript);
FMTEND(p, e);
char* argv[] = { path, NULL };
int ret = sys_execve(path, argv, environ);
report("exec", path, ret);
return -1; /* cause kernel panic */
}
void request(int flags)
{
flagged |= flags;
}
static int need_to(int flag)
{
int ret = flagged & flag;
flagged &= ~flag;
return ret;
}
int main(int argc, char** argv)
{
environ = argv + argc + 1;
if(argc > 1)
report("ignoring extra arguments", NULL, 0);
setup_ctrl();
origbrk = sys_brk(NULL);
if(setup_signals())
goto reboot;
if(reload_procs())
goto reboot;
check_procs();
update_poll_fds();
while(1) {
wait_poll();
if(need_to(F_WAIT_PIDS))
wait_pids();
if(need_to(F_CHECK_PROCS))
check_procs();
if(need_to(F_SETUP_CTRL))
setup_ctrl();
if(need_to(F_RELOAD_PROCS))
reload_procs();
if(need_to(F_TRIM_RING))
trim_ring_area();
if(need_to(F_UPDATE_PFDS))
update_poll_fds();
if(need_to(F_EXIT_LOOP))
break;
}
reboot:
sys_unlink(CONTROL);
return exec_next();
};