-
Notifications
You must be signed in to change notification settings - Fork 1
/
sigwait.c
104 lines (84 loc) · 1.66 KB
/
sigwait.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
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include "util.h"
static const char *signame[] = {
"",
"HUP",
"INT",
"QUIT",
"ILL",
"TRAP",
"ABRT",
"BUS",
"FPE",
"KILL",
"USR1",
"SEGV",
"USR2",
"PIPE",
"ALRM",
"TERM",
"STKFLT",
"CHLD",
"CONT",
"STOP",
"TSTP",
#define MAX_SIG SIGTSTP
NULL
};
static int sig_str2type(char *s)
{
int i = 0;
for (i = 0; signame[i]; i ++) {
if (!strcmp(signame[i], s)) {
return i;
}
} // End for
return -1;
} // sig_str2type
static void sig_handler(
int sig,
siginfo_t *info,
void *p
)
{
(void)p;
printf("Received signal %s (%d) from process#%d\n", strsignal(sig), sig, info->si_pid);
} // sig_handler
int main(int ac, char *av[])
{
int rc;
int i;
int sig;
struct sigaction action;
sigset_t sigset;
if (ac < 2) {
fprintf(stderr, "Usage: %s sig1 sig2...\n", basename(av[0]));
return 1;
}
for (i = 1; av[i]; i ++) {
sig = sig_str2type(av[i]);
if (sig > 0) {
sigemptyset(&sigset);
action.sa_sigaction = sig_handler;
action.sa_mask = sigset;
action.sa_flags = SA_SIGINFO;
rc = sigaction(sig, &action, NULL);
if (rc != 0) {
ERR("sigaction(%s): '%m' (%d)\n", av[i], errno);
return 1;
}
} else {
fprintf(stderr, "Unknown signal '%s'\n", av[i]);
return 1;
}
} // End for
while (1) {
pause();
}
return 0;
} // main