-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpause.c
46 lines (39 loc) · 924 Bytes
/
pause.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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
static void done(int sig) {
psignal(sig, "shutting down; caught signal");
exit(0);
}
static void reap(int _) {
while (waitpid(-1, NULL, WNOHANG) > 0);
}
int main() {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = done;
if (sigaction(SIGTERM, &sa, NULL) < 0) {
perror("failed to set up SIGTERM handler");
exit(1);
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = done;
if (sigaction(SIGINT, &sa, NULL) < 0) {
perror("failed to set up SIGINT handler");
exit(1);
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = reap;
sa.sa_flags = SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &sa, NULL) < 0) {
perror("failed to set up SIGCHLD handler");
exit(1);
}
for (;;) pause();
fprintf(stderr, "oops: pause loop bailed out somehow...\n");
exit(2);
}