2 * signal.c -- Signal Handler Library for Lua
4 * Version: 1.000+changes
6 * Copyright (C) 2007 Patrick J. Donnelly (batrick@batbytes.com)
8 * This software is distributed under the same license as Lua 5.0:
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
41 char *name; /* name of the signal */
42 int sig; /* the signal */
47 #define LUA_SIGNAL "lua_signal"
49 static const struct lua_signal lua_signals[] = {
101 {"SIGSTOP", SIGSTOP},
104 {"SIGTTIN", SIGTTIN},
107 {"SIGTTOU", SIGTTOU},
109 /* some BSD signals */
123 {"SIGXCPU", SIGXCPU},
126 {"SIGXFSZ", SIGXFSZ},
129 {"SIGVTALRM", SIGVTALRM},
132 {"SIGPROF", SIGPROF},
135 {"SIGWINCH", SIGWINCH},
138 {"SIGPOLL", SIGPOLL},
143 /* add odd signals */
145 {"SIGSTKFLT", SIGSTKFLT}, /* stack fault */
153 static lua_State *Lsig = NULL;
154 static lua_Hook Hsig = NULL;
155 static int Hmask = 0;
156 static int Hcount = 0;
158 static struct signal_event
161 struct signal_event *next_event;
162 } *signal_queue = NULL;
164 static struct signal_event *last_event = NULL;
166 static void sighook(lua_State *L, lua_Debug *ar)
168 struct signal_event *event;
169 /* restore the old hook */
170 lua_sethook(L, Hsig, Hmask, Hcount);
172 lua_pushstring(L, LUA_SIGNAL);
173 lua_gettable(L, LUA_REGISTRYINDEX);
175 while((event = signal_queue))
177 lua_pushnumber(L, event->Nsig);
180 signal_queue = event->next_event;
184 lua_pop(L, 1); /* pop lua_signal table */
188 static void handle(int sig)
192 /* Store the existing debug hook (if any) and its parameters */
193 Hsig = lua_gethook(Lsig);
194 Hmask = lua_gethookmask(Lsig);
195 Hcount = lua_gethookcount(Lsig);
197 signal_queue = malloc(sizeof(struct signal_event));
198 signal_queue->Nsig = sig;
199 signal_queue->next_event = NULL;
201 last_event = signal_queue;
203 /* Set our new debug hook */
204 lua_sethook(Lsig, sighook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
208 last_event->next_event = malloc(sizeof(struct signal_event));
209 last_event->next_event->Nsig = sig;
210 last_event->next_event->next_event = NULL;
212 last_event = last_event->next_event;
217 * l_signal == signal(signal [, func [, chook]])
219 * signal = signal number or string
220 * func = Lua function to call
221 * chook = catch within C functions
222 * if caught, Lua function _must_
223 * exit, as the stack is most likely
224 * in an unstable state.
227 static int l_signal(lua_State *L)
229 int args = lua_gettop(L);
230 int t, sig; /* type, signal */
232 /* get type of signal */
235 if (t == LUA_TNUMBER)
236 sig = (int) lua_tonumber(L, 1);
237 else if (t == LUA_TSTRING)
239 lua_pushstring(L, LUA_SIGNAL);
240 lua_gettable(L, LUA_REGISTRYINDEX);
243 if (!lua_isnumber(L, -1))
244 luaL_error(L, "invalid signal string");
245 sig = (int) lua_tonumber(L, -1);
246 lua_pop(L, 1); /* get rid of number we pushed */
248 luaL_checknumber(L, 1); /* will always error, with good error msg */
251 if (args == 1 || lua_isnil(L, 2)) /* clear handler */
253 lua_pushstring(L, LUA_SIGNAL);
254 lua_gettable(L, LUA_REGISTRYINDEX);
255 lua_pushnumber(L, sig);
256 lua_gettable(L, -2); /* return old handler */
257 lua_pushnumber(L, sig);
260 lua_remove(L, -2); /* remove LUA_SIGNAL table */
261 signal(sig, SIG_DFL);
264 luaL_checktype(L, 2, LUA_TFUNCTION);
266 lua_pushstring(L, LUA_SIGNAL);
267 lua_gettable(L, LUA_REGISTRYINDEX);
269 lua_pushnumber(L, sig);
273 /* Set the state for the handler */
276 if (lua_toboolean(L, 3)) /* c hook? */
278 if (signal(sig, handle) == SIG_ERR)
279 lua_pushboolean(L, 0);
281 lua_pushboolean(L, 1);
282 } else /* lua_hook */
284 if (signal(sig, handle) == SIG_ERR)
285 lua_pushboolean(L, 0);
287 lua_pushboolean(L, 1);
294 * l_raise == raise(signal)
296 * signal = signal number or string
299 static int l_raise(lua_State *L)
301 /* int args = lua_gettop(L); */
302 int t = 0; /* type */
308 if (t == LUA_TNUMBER)
310 ret = (lua_Number) raise((int) lua_tonumber(L, 1));
311 lua_pushnumber(L, ret);
312 } else if (t == LUA_TSTRING)
314 lua_pushstring(L, LUA_SIGNAL);
315 lua_gettable(L, LUA_REGISTRYINDEX);
318 if (!lua_isnumber(L, -1))
319 luaL_error(L, "invalid signal string");
320 ret = (lua_Number) raise((int) lua_tonumber(L, -1));
321 lua_pop(L, 1); /* get rid of number we pushed */
322 lua_pushnumber(L, ret);
324 luaL_checknumber(L, 1); /* will always error, with good error msg */
329 #if defined(__unix__) || defined(__APPLE__)
331 /* define some posix only functions */
334 * l_kill == kill(pid, signal)
337 * signal = signal number or string
340 static int l_kill(lua_State *L)
343 lua_Number ret; /* return value */
345 luaL_checknumber(L, 1); /* must be int for pid */
346 luaL_checkany(L, 2); /* check for a second arg */
349 if (t == LUA_TNUMBER)
351 ret = (lua_Number) kill((int) lua_tonumber(L, 1),
352 (int) lua_tonumber(L, 2));
353 lua_pushnumber(L, ret);
354 } else if (t == LUA_TSTRING)
356 lua_pushstring(L, LUA_SIGNAL);
357 lua_gettable(L, LUA_REGISTRYINDEX);
360 if (!lua_isnumber(L, -1))
361 luaL_error(L, "invalid signal string");
362 ret = (lua_Number) kill((int) lua_tonumber(L, 1),
363 (int) lua_tonumber(L, -1));
364 lua_pop(L, 1); /* get rid of number we pushed */
365 lua_pushnumber(L, ret);
367 luaL_checknumber(L, 2); /* will always error, with good error msg */
373 static const struct luaL_Reg lsignal_lib[] = {
374 {"signal", l_signal},
376 #if defined(__unix__) || defined(__APPLE__)
382 int luaopen_util_signal(lua_State *L)
386 /* add the library */
387 luaL_register(L, "signal", lsignal_lib);
389 /* push lua_signals table into the registry */
390 /* put the signals inside the library table too,
391 * they are only a reference */
392 lua_pushstring(L, LUA_SIGNAL);
393 lua_createtable(L, 0, 0);
395 while (lua_signals[i].name != NULL)
398 lua_pushstring(L, lua_signals[i].name);
399 lua_pushnumber(L, lua_signals[i].sig);
402 lua_pushstring(L, lua_signals[i].name);
403 lua_pushnumber(L, lua_signals[i].sig);
408 /* add newtable to the registry */
409 lua_settable(L, LUA_REGISTRYINDEX);