-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait-for-signal.c
295 lines (267 loc) · 6.54 KB
/
wait-for-signal.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
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
/* wait-for-signal.c: Sleep until one of given signals received,
* otherwise SIGUSR1.
*
* Copyright © 2023 Revolution Robotics, Inc.
*
* This file is part of inhibit-suspend.
*/
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef INT_MAX
# define INT_MAX __INT_MAX__
#endif
#ifndef INT_MIN
# define INT_MIN (-__INT_MAX__ - 1)
#endif
typedef void (*sighandler_t)(int);
static bool validate_int (char *, int *);
static int decode_signal (char *);
static void handle_signal (int);
int suspend_until_signal (sigset_t *, int [], int);
char *signal_names[] = {
"EXIT",
"SIGHUP",
"SIGINT",
"SIGQUIT",
"SIGILL",
"SIGTRAP",
"SIGABRT",
"SIGBUS",
"SIGFPE",
"SIGKILL",
"SIGUSR1",
"SIGSEGV",
"SIGUSR2",
"SIGPIPE",
"SIGALRM",
"SIGTERM",
"SIGSTKFLT",
"SIGCHLD",
"SIGCONT",
"SIGSTOP",
"SIGTSTP",
"SIGTTIN",
"SIGTTOU",
"SIGURG",
"SIGXCPU",
"SIGXFSZ",
"SIGVTALRM",
"SIGPROF",
"SIGWINCH",
"SIGIO",
"SIGPWR",
"SIGSYS",
NULL,
NULL,
"SIGRTMIN",
"SIGRTMIN+1",
"SIGRTMIN+2",
"SIGRTMIN+3",
"SIGRTMIN+4",
"SIGRTMIN+5",
"SIGRTMIN+6",
"SIGRTMIN+7",
"SIGRTMIN+8",
"SIGRTMIN+9",
"SIGRTMIN+10",
"SIGRTMIN+11",
"SIGRTMIN+12",
"SIGRTMIN+13",
"SIGRTMIN+14",
"SIGRTMIN+15",
"SIGRTMAX-14",
"SIGRTMAX-13",
"SIGRTMAX-12",
"SIGRTMAX-11",
"SIGRTMAX-10",
"SIGRTMAX-9",
"SIGRTMAX-8",
"SIGRTMAX-7",
"SIGRTMAX-6",
"SIGRTMAX-5",
"SIGRTMAX-4",
"SIGRTMAX-3",
"SIGRTMAX-2",
"SIGRTMAX-1",
"SIGRTMAX"
};
int ignore_signals[] = {
SIGHUP,
SIGINT,
SIGQUIT,
SIGUSR1,
SIGUSR2,
SIGPIPE,
SIGALRM,
SIGCHLD,
SIGTSTP
};
int signal_caught = 0;
sigset_t signal_mask;
char *pgm = NULL;
int
main (int argc, char *argv[])
{
pgm = strchr (argv[0], '/') ? strrchr (argv[0], '/') + 1 : argv[0];
int ignore_signals_size = sizeof ignore_signals / sizeof ignore_signals[0];
int signo = SIGUSR1;
/* Populate signal_mask. */
if (sigemptyset (&signal_mask) < 0)
{
fprintf (stderr, "%s\n", strerror (errno));
return 1;
}
else if (argc < 2)
{
if (signal (signo, (sighandler_t) handle_signal) == SIG_ERR
|| sigaddset (&signal_mask, signo) < 0)
{
fprintf (stderr, "%s\n", strerror (errno));
return 1;
}
}
else
for (int i = 1; i < argc; ++i)
if ((signo = decode_signal(argv[i])) == -1)
{
fprintf (stderr, "Usage: %s [sigspec] \n", pgm);
return 1;
}
else if (signal (signo, (sighandler_t) handle_signal) == SIG_ERR
|| sigaddset (&signal_mask, signo) < 0)
{
fprintf (stderr, "%s\n", strerror (errno));
return 1;
}
exit (suspend_until_signal (&signal_mask,
ignore_signals,
ignore_signals_size));
}
int
suspend_until_signal (sigset_t *signal_mask_p,
int ignore_signals[],
int ignore_signals_size)
{
sigset_t previous_signal_mask;
/* Block signals in signal_mask. */
if (sigprocmask (SIG_BLOCK, signal_mask_p, &previous_signal_mask) < 0)
{
fprintf (stderr, "%s\n", strerror (errno));
return 1;
}
signal_caught = 0;
/* Ignore other signals. */
for (int i = 0; i < ignore_signals_size; ++i)
if (!sigismember (signal_mask_p, ignore_signals[i]))
signal(ignore_signals[i], SIG_IGN);
/* Unblock signal_mask until signal handler called. */
while (!signal_caught)
if (sigsuspend (&previous_signal_mask) == -1 && errno != EINTR)
{
fprintf (stderr, "%s\n", strerror (errno));
return 1;
}
/* Restore other signals. */
for (int i = 0; i < ignore_signals_size; ++i)
if (!sigismember (signal_mask_p, ignore_signals[i]))
signal(ignore_signals[i], SIG_DFL);
signal_caught = 0;
/* Unblock signal_mask. */
if (sigprocmask (SIG_UNBLOCK, signal_mask_p, NULL) < 0)
{
fprintf (stderr, "%s\n", strerror (errno));
return 1;
}
return 0;
}
/*
* decode_signal: Derived from bash function of the same name (see:
* https://git.savannah.gnu.org/cgit/bash.git/tree/trap.c#n231)
*
* Given STRING, return corresponding integer signal number, e.g., if
* STRING is "2", "SIGINT", or "INT", then 2 is returned. Return -1 if
* STRING doesn't contain a valid signal descriptor.
*/
static int
decode_signal (char *string)
{
int signo;
char *name;
char *short_name;
/* Try converting string to signo using (int) strtol. */
if (validate_int (string, &signo))
return (0 < signo && signo < NSIG) ? signo : -1;
#if defined (SIGRTMIN) && defined (SIGRTMAX)
if (strncasecmp (string, "SIGRTMIN+", 9) == 0)
{
if (validate_int (string+9, &signo)
&& signo >= 0 && signo <= SIGRTMAX - SIGRTMIN)
return (SIGRTMIN + signo);
else
return -1;
}
else if (strncasecmp (string, "RTMIN+", 6) == 0)
{
if (validate_int (string+6, &signo) && signo >= 0
&& signo <= SIGRTMAX - SIGRTMIN)
return (SIGRTMIN + signo);
else
return -1;
}
else if (strncasecmp (string, "SIGRTMAX-", 9) == 0)
{
if (validate_int (string+9, &signo) && signo >= 0
&& signo <= SIGRTMAX - SIGRTMIN)
return (SIGRTMAX - signo);
else
return -1;
}
else if (strncasecmp (string, "RTMAX-", 6) == 0)
{
if (validate_int (string+6, &signo) && signo >= 0
&& signo <= SIGRTMAX - SIGRTMIN)
return (SIGRTMAX - signo);
else
return -1;
}
#endif /* SIGRTMIN && SIGRTMAX */
for (signo = 1; signo < NSIG; ++signo)
{
if ((name = signal_names[signo]) == NULL)
continue;
else if (strcasecmp (string, name) == 0)
return ((int) signo);
/* A leading `SIG' may be omitted. */
short_name = name + 3;
if (strcasecmp (string, short_name) == 0)
return ((int) signo);
}
return -1;
}
static bool
validate_int (char *s, int *ip)
{
char *endp = NULL;
long l = strtol(s, &endp, 10);
/* Long and/or Integer overflow or underflow */
if (errno == ERANGE || l < INT_MIN || INT_MAX < l)
return false;
/* Conversion successful */
else if (*endp == '\0')
{
*ip = (int) l;
return true;
}
/* Conversion incomplete */
return false;
}
static void
handle_signal (int signo)
{
if (sigismember (&signal_mask, signo))
signal_caught = 1;
}