-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlsignal.c
435 lines (398 loc) · 9.95 KB
/
lsignal.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* lsignal.c -- Signal Handler Library for Lua
*
* Copyright (C) 2010 Patrick J. Donnelly ([email protected])
*
* This software is distributed under the same license as Lua 5.0:
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#define LUA_LIB_NAME "signal"
#define LUA_LIB_VERSION "1.2.0"
#define LUA_SIGNAL_NAME "LUA_SIGNAL"
#if !(defined(_POSIX_SOURCE) || defined(sun) || defined(__sun))
#define INCLUDE_KILL 1
#define INCLUDE_PAUSE 1
#define USE_SIGACTION 1
#endif
#include <lua.h>
#include <lauxlib.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
struct lua_signal
{
const char *name; /* name of the signal */
const int sig; /* the signal */
};
static const struct lua_signal lua_signals[] = {
/* ANSI C signals */
#ifdef SIGABRT
{"SIGABRT", SIGABRT},
#endif
#ifdef SIGFPE
{"SIGFPE", SIGFPE},
#endif
#ifdef SIGILL
{"SIGILL", SIGILL},
#endif
#ifdef SIGINT
{"SIGINT", SIGINT},
#endif
#ifdef SIGSEGV
{"SIGSEGV", SIGSEGV},
#endif
#ifdef SIGTERM
{"SIGTERM", SIGTERM},
#endif
/* posix signals */
#ifdef SIGHUP
{"SIGHUP", SIGHUP},
#endif
#ifdef SIGQUIT
{"SIGQUIT", SIGQUIT},
#endif
#ifdef SIGTRAP
{"SIGTRAP", SIGTRAP},
#endif
#ifdef SIGKILL
{"SIGKILL", SIGKILL},
#endif
#ifdef SIGUSR1
{"SIGUSR1", SIGUSR1},
#endif
#ifdef SIGUSR2
{"SIGUSR2", SIGUSR2},
#endif
#ifdef SIGPIPE
{"SIGPIPE", SIGPIPE},
#endif
#ifdef SIGALRM
{"SIGALRM", SIGALRM},
#endif
#ifdef SIGCHLD
{"SIGCHLD", SIGCHLD},
#endif
#ifdef SIGCONT
{"SIGCONT", SIGCONT},
#endif
#ifdef SIGSTOP
{"SIGSTOP", SIGSTOP},
#endif
#ifdef SIGTSTP
{"SIGTSTP", SIGTSTP},
#endif
#ifdef SIGTTIN
{"SIGTTIN", SIGTTIN},
#endif
#ifdef SIGTTOU
{"SIGTTOU", SIGTTOU},
#endif
/* some BSD signals */
#ifdef SIGIOT
{"SIGIOT", SIGIOT},
#endif
#ifdef SIGBUS
{"SIGBUS", SIGBUS},
#endif
#ifdef SIGCLD
{"SIGCLD", SIGCLD},
#endif
#ifdef SIGURG
{"SIGURG", SIGURG},
#endif
#ifdef SIGXCPU
{"SIGXCPU", SIGXCPU},
#endif
#ifdef SIGXFSZ
{"SIGXFSZ", SIGXFSZ},
#endif
#ifdef SIGVTALRM
{"SIGVTALRM", SIGVTALRM},
#endif
#ifdef SIGPROF
{"SIGPROF", SIGPROF},
#endif
#ifdef SIGWINCH
{"SIGWINCH", SIGWINCH},
#endif
#ifdef SIGPOLL
{"SIGPOLL", SIGPOLL},
#endif
#ifdef SIGIO
{"SIGIO", SIGIO},
#endif
/* add odd signals */
#ifdef SIGSTKFLT
{"SIGSTKFLT", SIGSTKFLT}, /* stack fault */
#endif
#ifdef SIGSYS
{"SIGSYS", SIGSYS},
#endif
{NULL, 0}
};
/*
* The signal counts in the 1st half of the array are modified by
* the handler. The corresponding signal counts in the 2nd half
* are modifed by the hook routine.
*/
static volatile sig_atomic_t *signal_stack = NULL;
static int signal_stack_top;
static lua_State *ML = NULL;
static struct hook {
lua_Hook hook;
int mask;
int count;
} old_hook = {NULL, 0, 0};
static void hook (lua_State *L, lua_Debug *ar)
{
int i, j;
assert(L == ML);
for (i = 0; i < signal_stack_top; i++)
while (signal_stack[i] != signal_stack[i+signal_stack_top])
{
lua_getfield(L, LUA_REGISTRYINDEX, LUA_SIGNAL_NAME);
lua_pushinteger(L, i);
lua_rawget(L, -2);
lua_replace(L, -2); /* replace _R.LUA_SIGNAL_NAME */
assert(lua_isfunction(L, -1));
for (j = 0; lua_signals[j].name != NULL; j++)
if (lua_signals[j].sig == i)
{
lua_pushstring(L, lua_signals[j].name);
break;
}
if (lua_signals[j].name == NULL) lua_pushliteral(L, "");
lua_pushinteger(L, i);
lua_call(L, 2, 0);
signal_stack[i+signal_stack_top]++;
}
lua_sethook(ML, old_hook.hook, old_hook.mask, old_hook.count);
old_hook.hook = NULL;
}
static void handle (int sig)
{
assert(ML != NULL);
if (old_hook.hook == NULL) /* replace it */
{
old_hook.hook = lua_gethook(ML);
old_hook.mask = lua_gethookmask(ML);
old_hook.count = lua_gethookcount(ML);
lua_sethook(ML, hook, LUA_MASKCOUNT, 1);
}
signal_stack[sig]++;
}
static int get_signal (lua_State *L, int idx)
{
switch (lua_type(L, idx))
{
case LUA_TNUMBER:
return (int) lua_tointeger(L, idx);
case LUA_TSTRING:
lua_pushvalue(L, idx);
lua_rawget(L, LUA_ENVIRONINDEX);
if (!lua_isnumber(L, -1))
return luaL_argerror(L, idx, "invalid signal string");
lua_replace(L, idx);
return (int) lua_tointeger(L, idx);
default:
return luaL_argerror(L, idx, "expected signal string/number");
}
}
static int status (lua_State *L, int s)
{
if (s)
{
lua_pushboolean(L, 1);
return 1;
}
else
{
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
}
/*
* old_handler[, err] == signal(signal [, func])
*
* signal = signal number or string
* func/"ignore"/"default" = Lua function to call
*/
static int l_signal (lua_State *L)
{
enum {IGNORE, DEFAULT, SET};
static const char *options[] = {"ignore", "default", NULL};
int sig = get_signal(L, 1);
int option;
if (lua_isstring(L, 2))
option = luaL_checkoption(L, 2, NULL, options);
else if (lua_isnil(L, 2))
option = DEFAULT;
else
option = (luaL_checktype(L, 2, LUA_TFUNCTION), SET);
lua_pushvalue(L, 1);
lua_rawget(L, LUA_ENVIRONINDEX); /* return old handler */
lua_pushvalue(L, 1);
switch (option)
{
case IGNORE:
lua_pushnil(L);
lua_rawset(L, LUA_ENVIRONINDEX);
signal(sig, SIG_IGN);
signal_stack[sig+signal_stack_top] = signal_stack[sig] = 0;
break;
case DEFAULT:
lua_pushnil(L);
lua_rawset(L, LUA_ENVIRONINDEX);
signal(sig, SIG_DFL);
signal_stack[sig+signal_stack_top] = signal_stack[sig] = 0;
break;
case SET:
lua_pushvalue(L, 2);
lua_rawset(L, LUA_ENVIRONINDEX);
#if USE_SIGACTION
{
struct sigaction act;
act.sa_handler = handle;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(sig, &act, NULL))
return status(L, 0);
}
#else
if (signal(sig, handle) == SIG_ERR)
return status(L, 0);
#endif
break;
default: assert(0);
}
return 1;
}
/*
* status, err = raise(signal)
*
* signal = signal number or string
*/
static int l_raise (lua_State *L)
{
return status(L, raise(get_signal(L, 1)) == 0);
}
#if INCLUDE_KILL
/* define some posix only functions */
/*
* status, err = kill(pid, signal)
*
* pid = process id
* signal = signal number or string
*/
static int l_kill (lua_State *L)
{
return status(L, kill(luaL_checkinteger(L, 1), get_signal(L, 2)) == 0);
}
#endif
#if INCLUDE_PAUSE
static int l_pause (lua_State *L) /* race condition free */
{
sigset_t mask, old_mask;
if (sigfillset(&mask) == -1) return status(L, 0);
if (sigprocmask(SIG_BLOCK, &mask, &old_mask) == -1) return status(L, 0);
if (sigsuspend(&old_mask) != -1) abort(); /* that's strange */
return status(L, 0);
}
#endif
static int interrupted (lua_State *L)
{
return luaL_error(L, "interrupted!");
}
static int library_gc (lua_State *L)
{
lua_getfield(L, LUA_REGISTRYINDEX, LUA_SIGNAL_NAME);
lua_pushnil(L);
while (lua_next(L, -2))
{
if (lua_isnumber(L, -2)) /* <signal, function> */
signal((int) lua_tointeger(L, -2), SIG_DFL);
lua_pop(L, 1); /* value */
}
signal_stack = NULL;
ML = NULL;
old_hook.hook = NULL;
signal_stack_top = 0;
return 0;
}
int luaopen_signal (lua_State *L)
{
static const struct luaL_Reg lib[] = {
{"signal", l_signal},
{"raise", l_raise},
#if INCLUDE_KILL
{"kill", l_kill},
#endif
#if INCLUDE_PAUSE
{"pause", l_pause},
#endif
{NULL, NULL}
};
int i;
int max_signal;
ML = L;
if (lua_pushthread(L))
lua_pop(L, 1);
else
luaL_error(L, "library should be opened by the main thread");
/* environment */
lua_newtable(L);
lua_replace(L, LUA_ENVIRONINDEX);
lua_pushvalue(L, LUA_ENVIRONINDEX);
lua_setfield(L, LUA_REGISTRYINDEX, LUA_SIGNAL_NAME); /* for hooks */
/* add the library */
luaL_register(L, LUA_LIB_NAME, lib);
lua_pushliteral(L, LUA_LIB_VERSION);
lua_setfield(L, -2, "version");
for (i = 0, max_signal = 0; lua_signals[i].name != NULL; i++)
if (lua_signals[i].sig > max_signal)
max_signal = lua_signals[i].sig+1; /* +1 !!! (for < loops) */
signal_stack = lua_newuserdata(L, sizeof(volatile sig_atomic_t)*max_signal*2);
lua_newtable(L);
lua_pushcfunction(L, library_gc);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2); /* when userdata is gc'd, close library */
memset((void *) signal_stack, 0, sizeof(volatile sig_atomic_t)*max_signal*2);
signal_stack_top = max_signal;
lua_pushboolean(L, 1);
lua_rawset(L, LUA_ENVIRONINDEX);
while (i--) /* i set from previous for loop */
{
lua_pushstring(L, lua_signals[i].name);
lua_pushinteger(L, lua_signals[i].sig);
lua_rawset(L, LUA_ENVIRONINDEX); /* add copy to environment table */
lua_pushstring(L, lua_signals[i].name);
lua_pushinteger(L, lua_signals[i].sig);
lua_settable(L, -3); /* add copy to signal table */
}
/* set default interrupt handler */
lua_getfield(L, -1, "signal");
lua_pushinteger(L, SIGINT);
lua_pushcfunction(L, interrupted);
lua_call(L, 2, 0);
return 1;
}