-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtrap.c
More file actions
89 lines (72 loc) · 1.72 KB
/
trap.c
File metadata and controls
89 lines (72 loc) · 1.72 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
#include <sys/file.h>
#include <sys/sched.h>
#include <sys/signal.h>
#include <format.h>
#include <util.h>
/* Signal trap, to be used as a stub for service scripts in ./rc
Trapping signals (normally sent by init) makes it clear
what's going on, specifically when and why the process dies.
Usage: trap [tag] [sleep-interval]
Tagging output helps a lot when several instances say something
at the same time. */
const char* tag = "trap";
int interval = 1000;
#define BUF 1024
static char saybuf[BUF];
void say(const char* what)
{
char* p = saybuf;
char* e = saybuf + sizeof(saybuf);
p = fmtstr(p, e, tag);
p = fmtstr(p, e, ": ");
p = fmtstr(p, e, what);
p = fmtstr(p, e, "\n");
sys_write(STDOUT, saybuf, p - saybuf);
}
void sighandler(int sig)
{
switch(sig) {
case SIGKILL: say("dying on SIGKILL"); _exit(0); break;
case SIGINT: say("dying on SIGINT"); _exit(0); break;
case SIGTERM: say("dying on SIGTERM"); _exit(0); break;
case SIGHUP: say("got SIGHUP"); break;
case SIGCONT: say("got SIGCONT, continuing"); break;
default: say("ignoring unexpected signal"); break;
}
}
void trapsig(void)
{
SIGHANDLER(sa, sighandler, SA_RESTART);
sys_sigaction(SIGINT, &sa, NULL);
sys_sigaction(SIGTERM, &sa, NULL);
sys_sigaction(SIGHUP, &sa, NULL);
sys_sigaction(SIGKILL, &sa, NULL);
sys_sigaction(SIGCONT, &sa, NULL);
}
void sleepx(int sec)
{
struct timespec tr;
struct timespec ts = {
.sec = sec,
.nsec = 0
};
while(1) {
if(!sys_nanosleep(&ts, &tr))
break;
if(tr.sec <= 0)
break;
ts = tr;
};
}
int main(int argc, char** argv)
{
if(argc > 1)
tag = argv[1];
if(argc > 2)
parseint(argv[2], &interval);
trapsig();
say("starting");
sleepx(interval);
say("normal exit");
return 0;
}