Software /
code /
prosody
Comparison
util-src/signal.c @ 6615:8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 03 Apr 2015 19:52:48 +0200 |
parent | 6413:a552f4170aed |
child | 6620:50eaefeec013 |
comparison
equal
deleted
inserted
replaced
6613:2aae36312eb9 | 6615:8e4572a642cb |
---|---|
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | 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 | 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, | 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 | 25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
26 * OTHER DEALINGS IN THE SOFTWARE. | 26 * OTHER DEALINGS IN THE SOFTWARE. |
27 */ | 27 */ |
28 | 28 |
29 #include <signal.h> | 29 #include <signal.h> |
30 #include <stdlib.h> | 30 #include <stdlib.h> |
31 | 31 |
38 | 38 |
39 #ifndef lsig | 39 #ifndef lsig |
40 | 40 |
41 #define lsig | 41 #define lsig |
42 | 42 |
43 struct lua_signal | 43 struct lua_signal { |
44 { | 44 char* name; /* name of the signal */ |
45 char *name; /* name of the signal */ | 45 int sig; /* the signal */ |
46 int sig; /* the signal */ | |
47 }; | 46 }; |
48 | 47 |
49 #endif | 48 #endif |
50 | 49 |
51 #define LUA_SIGNAL "lua_signal" | 50 #define LUA_SIGNAL "lua_signal" |
52 | 51 |
53 static const struct lua_signal lua_signals[] = { | 52 static const struct lua_signal lua_signals[] = { |
54 /* ANSI C signals */ | 53 /* ANSI C signals */ |
55 #ifdef SIGABRT | 54 #ifdef SIGABRT |
56 {"SIGABRT", SIGABRT}, | 55 {"SIGABRT", SIGABRT}, |
57 #endif | 56 #endif |
58 #ifdef SIGFPE | 57 #ifdef SIGFPE |
59 {"SIGFPE", SIGFPE}, | 58 {"SIGFPE", SIGFPE}, |
60 #endif | 59 #endif |
61 #ifdef SIGILL | 60 #ifdef SIGILL |
62 {"SIGILL", SIGILL}, | 61 {"SIGILL", SIGILL}, |
63 #endif | 62 #endif |
64 #ifdef SIGINT | 63 #ifdef SIGINT |
65 {"SIGINT", SIGINT}, | 64 {"SIGINT", SIGINT}, |
66 #endif | 65 #endif |
67 #ifdef SIGSEGV | 66 #ifdef SIGSEGV |
68 {"SIGSEGV", SIGSEGV}, | 67 {"SIGSEGV", SIGSEGV}, |
69 #endif | 68 #endif |
70 #ifdef SIGTERM | 69 #ifdef SIGTERM |
71 {"SIGTERM", SIGTERM}, | 70 {"SIGTERM", SIGTERM}, |
72 #endif | 71 #endif |
73 /* posix signals */ | 72 /* posix signals */ |
74 #ifdef SIGHUP | 73 #ifdef SIGHUP |
75 {"SIGHUP", SIGHUP}, | 74 {"SIGHUP", SIGHUP}, |
76 #endif | 75 #endif |
77 #ifdef SIGQUIT | 76 #ifdef SIGQUIT |
78 {"SIGQUIT", SIGQUIT}, | 77 {"SIGQUIT", SIGQUIT}, |
79 #endif | 78 #endif |
80 #ifdef SIGTRAP | 79 #ifdef SIGTRAP |
81 {"SIGTRAP", SIGTRAP}, | 80 {"SIGTRAP", SIGTRAP}, |
82 #endif | 81 #endif |
83 #ifdef SIGKILL | 82 #ifdef SIGKILL |
84 {"SIGKILL", SIGKILL}, | 83 {"SIGKILL", SIGKILL}, |
85 #endif | 84 #endif |
86 #ifdef SIGUSR1 | 85 #ifdef SIGUSR1 |
87 {"SIGUSR1", SIGUSR1}, | 86 {"SIGUSR1", SIGUSR1}, |
88 #endif | 87 #endif |
89 #ifdef SIGUSR2 | 88 #ifdef SIGUSR2 |
90 {"SIGUSR2", SIGUSR2}, | 89 {"SIGUSR2", SIGUSR2}, |
91 #endif | 90 #endif |
92 #ifdef SIGPIPE | 91 #ifdef SIGPIPE |
93 {"SIGPIPE", SIGPIPE}, | 92 {"SIGPIPE", SIGPIPE}, |
94 #endif | 93 #endif |
95 #ifdef SIGALRM | 94 #ifdef SIGALRM |
96 {"SIGALRM", SIGALRM}, | 95 {"SIGALRM", SIGALRM}, |
97 #endif | 96 #endif |
98 #ifdef SIGCHLD | 97 #ifdef SIGCHLD |
99 {"SIGCHLD", SIGCHLD}, | 98 {"SIGCHLD", SIGCHLD}, |
100 #endif | 99 #endif |
101 #ifdef SIGCONT | 100 #ifdef SIGCONT |
102 {"SIGCONT", SIGCONT}, | 101 {"SIGCONT", SIGCONT}, |
103 #endif | 102 #endif |
104 #ifdef SIGSTOP | 103 #ifdef SIGSTOP |
105 {"SIGSTOP", SIGSTOP}, | 104 {"SIGSTOP", SIGSTOP}, |
106 #endif | 105 #endif |
107 #ifdef SIGTTIN | 106 #ifdef SIGTTIN |
108 {"SIGTTIN", SIGTTIN}, | 107 {"SIGTTIN", SIGTTIN}, |
109 #endif | 108 #endif |
110 #ifdef SIGTTOU | 109 #ifdef SIGTTOU |
111 {"SIGTTOU", SIGTTOU}, | 110 {"SIGTTOU", SIGTTOU}, |
112 #endif | 111 #endif |
113 /* some BSD signals */ | 112 /* some BSD signals */ |
114 #ifdef SIGIOT | 113 #ifdef SIGIOT |
115 {"SIGIOT", SIGIOT}, | 114 {"SIGIOT", SIGIOT}, |
116 #endif | 115 #endif |
117 #ifdef SIGBUS | 116 #ifdef SIGBUS |
118 {"SIGBUS", SIGBUS}, | 117 {"SIGBUS", SIGBUS}, |
119 #endif | 118 #endif |
120 #ifdef SIGCLD | 119 #ifdef SIGCLD |
121 {"SIGCLD", SIGCLD}, | 120 {"SIGCLD", SIGCLD}, |
122 #endif | 121 #endif |
123 #ifdef SIGURG | 122 #ifdef SIGURG |
124 {"SIGURG", SIGURG}, | 123 {"SIGURG", SIGURG}, |
125 #endif | 124 #endif |
126 #ifdef SIGXCPU | 125 #ifdef SIGXCPU |
127 {"SIGXCPU", SIGXCPU}, | 126 {"SIGXCPU", SIGXCPU}, |
128 #endif | 127 #endif |
129 #ifdef SIGXFSZ | 128 #ifdef SIGXFSZ |
130 {"SIGXFSZ", SIGXFSZ}, | 129 {"SIGXFSZ", SIGXFSZ}, |
131 #endif | 130 #endif |
132 #ifdef SIGVTALRM | 131 #ifdef SIGVTALRM |
133 {"SIGVTALRM", SIGVTALRM}, | 132 {"SIGVTALRM", SIGVTALRM}, |
134 #endif | 133 #endif |
135 #ifdef SIGPROF | 134 #ifdef SIGPROF |
136 {"SIGPROF", SIGPROF}, | 135 {"SIGPROF", SIGPROF}, |
137 #endif | 136 #endif |
138 #ifdef SIGWINCH | 137 #ifdef SIGWINCH |
139 {"SIGWINCH", SIGWINCH}, | 138 {"SIGWINCH", SIGWINCH}, |
140 #endif | 139 #endif |
141 #ifdef SIGPOLL | 140 #ifdef SIGPOLL |
142 {"SIGPOLL", SIGPOLL}, | 141 {"SIGPOLL", SIGPOLL}, |
143 #endif | 142 #endif |
144 #ifdef SIGIO | 143 #ifdef SIGIO |
145 {"SIGIO", SIGIO}, | 144 {"SIGIO", SIGIO}, |
146 #endif | 145 #endif |
147 /* add odd signals */ | 146 /* add odd signals */ |
148 #ifdef SIGSTKFLT | 147 #ifdef SIGSTKFLT |
149 {"SIGSTKFLT", SIGSTKFLT}, /* stack fault */ | 148 {"SIGSTKFLT", SIGSTKFLT}, /* stack fault */ |
150 #endif | 149 #endif |
151 #ifdef SIGSYS | 150 #ifdef SIGSYS |
152 {"SIGSYS", SIGSYS}, | 151 {"SIGSYS", SIGSYS}, |
153 #endif | 152 #endif |
154 {NULL, 0} | 153 {NULL, 0} |
155 }; | 154 }; |
156 | 155 |
157 static lua_State *Lsig = NULL; | 156 static lua_State* Lsig = NULL; |
158 static lua_Hook Hsig = NULL; | 157 static lua_Hook Hsig = NULL; |
159 static int Hmask = 0; | 158 static int Hmask = 0; |
160 static int Hcount = 0; | 159 static int Hcount = 0; |
161 | 160 |
162 static struct signal_event | 161 static struct signal_event { |
163 { | |
164 int Nsig; | 162 int Nsig; |
165 struct signal_event *next_event; | 163 struct signal_event* next_event; |
166 } *signal_queue = NULL; | 164 }* signal_queue = NULL; |
167 | 165 |
168 static struct signal_event *last_event = NULL; | 166 static struct signal_event* last_event = NULL; |
169 | 167 |
170 static void sighook(lua_State *L, lua_Debug *ar) | 168 static void sighook(lua_State* L, lua_Debug* ar) { |
171 { | 169 struct signal_event* event; |
172 struct signal_event *event; | 170 /* restore the old hook */ |
173 /* restore the old hook */ | 171 lua_sethook(L, Hsig, Hmask, Hcount); |
174 lua_sethook(L, Hsig, Hmask, Hcount); | 172 |
175 | 173 lua_pushstring(L, LUA_SIGNAL); |
176 lua_pushstring(L, LUA_SIGNAL); | 174 lua_gettable(L, LUA_REGISTRYINDEX); |
177 lua_gettable(L, LUA_REGISTRYINDEX); | 175 |
178 | 176 while((event = signal_queue)) { |
179 while((event = signal_queue)) | 177 lua_pushnumber(L, event->Nsig); |
180 { | 178 lua_gettable(L, -2); |
181 lua_pushnumber(L, event->Nsig); | 179 lua_call(L, 0, 0); |
182 lua_gettable(L, -2); | 180 signal_queue = event->next_event; |
183 lua_call(L, 0, 0); | 181 free(event); |
184 signal_queue = event->next_event; | 182 }; |
185 free(event); | 183 |
186 }; | 184 lua_pop(L, 1); /* pop lua_signal table */ |
187 | 185 |
188 lua_pop(L, 1); /* pop lua_signal table */ | 186 } |
189 | 187 |
190 } | 188 static void handle(int sig) { |
191 | 189 if(!signal_queue) { |
192 static void handle(int sig) | 190 /* Store the existing debug hook (if any) and its parameters */ |
193 { | 191 Hsig = lua_gethook(Lsig); |
194 if(!signal_queue) | 192 Hmask = lua_gethookmask(Lsig); |
195 { | 193 Hcount = lua_gethookcount(Lsig); |
196 /* Store the existing debug hook (if any) and its parameters */ | 194 |
197 Hsig = lua_gethook(Lsig); | 195 signal_queue = malloc(sizeof(struct signal_event)); |
198 Hmask = lua_gethookmask(Lsig); | 196 signal_queue->Nsig = sig; |
199 Hcount = lua_gethookcount(Lsig); | 197 signal_queue->next_event = NULL; |
200 | 198 |
201 signal_queue = malloc(sizeof(struct signal_event)); | 199 last_event = signal_queue; |
202 signal_queue->Nsig = sig; | 200 |
203 signal_queue->next_event = NULL; | 201 /* Set our new debug hook */ |
204 | 202 lua_sethook(Lsig, sighook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); |
205 last_event = signal_queue; | 203 } else { |
206 | 204 last_event->next_event = malloc(sizeof(struct signal_event)); |
207 /* Set our new debug hook */ | 205 last_event->next_event->Nsig = sig; |
208 lua_sethook(Lsig, sighook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); | 206 last_event->next_event->next_event = NULL; |
209 } | 207 |
210 else | 208 last_event = last_event->next_event; |
211 { | 209 } |
212 last_event->next_event = malloc(sizeof(struct signal_event)); | |
213 last_event->next_event->Nsig = sig; | |
214 last_event->next_event->next_event = NULL; | |
215 | |
216 last_event = last_event->next_event; | |
217 } | |
218 } | 210 } |
219 | 211 |
220 /* | 212 /* |
221 * l_signal == signal(signal [, func [, chook]]) | 213 * l_signal == signal(signal [, func [, chook]]) |
222 * | 214 * |
224 * func = Lua function to call | 216 * func = Lua function to call |
225 * chook = catch within C functions | 217 * chook = catch within C functions |
226 * if caught, Lua function _must_ | 218 * if caught, Lua function _must_ |
227 * exit, as the stack is most likely | 219 * exit, as the stack is most likely |
228 * in an unstable state. | 220 * in an unstable state. |
229 */ | 221 */ |
230 | 222 |
231 static int l_signal(lua_State *L) | 223 static int l_signal(lua_State* L) { |
232 { | 224 int args = lua_gettop(L); |
233 int args = lua_gettop(L); | 225 int t, sig; /* type, signal */ |
234 int t, sig; /* type, signal */ | 226 |
235 | 227 /* get type of signal */ |
236 /* get type of signal */ | 228 luaL_checkany(L, 1); |
237 luaL_checkany(L, 1); | 229 t = lua_type(L, 1); |
238 t = lua_type(L, 1); | 230 |
239 if (t == LUA_TNUMBER) | 231 if(t == LUA_TNUMBER) { |
240 sig = (int) lua_tonumber(L, 1); | 232 sig = (int) lua_tonumber(L, 1); |
241 else if (t == LUA_TSTRING) | 233 } else if(t == LUA_TSTRING) { |
242 { | 234 lua_pushstring(L, LUA_SIGNAL); |
243 lua_pushstring(L, LUA_SIGNAL); | 235 lua_gettable(L, LUA_REGISTRYINDEX); |
244 lua_gettable(L, LUA_REGISTRYINDEX); | 236 lua_pushvalue(L, 1); |
245 lua_pushvalue(L, 1); | 237 lua_gettable(L, -2); |
246 lua_gettable(L, -2); | 238 |
247 if (!lua_isnumber(L, -1)) | 239 if(!lua_isnumber(L, -1)) { |
248 luaL_error(L, "invalid signal string"); | 240 luaL_error(L, "invalid signal string"); |
249 sig = (int) lua_tonumber(L, -1); | 241 } |
250 lua_pop(L, 1); /* get rid of number we pushed */ | 242 |
251 } else | 243 sig = (int) lua_tonumber(L, -1); |
252 luaL_checknumber(L, 1); /* will always error, with good error msg */ | 244 lua_pop(L, 1); /* get rid of number we pushed */ |
253 | 245 } else { |
254 /* set handler */ | 246 luaL_checknumber(L, 1); /* will always error, with good error msg */ |
255 if (args == 1 || lua_isnil(L, 2)) /* clear handler */ | 247 } |
256 { | 248 |
257 lua_pushstring(L, LUA_SIGNAL); | 249 /* set handler */ |
258 lua_gettable(L, LUA_REGISTRYINDEX); | 250 if(args == 1 || lua_isnil(L, 2)) { /* clear handler */ |
259 lua_pushnumber(L, sig); | 251 lua_pushstring(L, LUA_SIGNAL); |
260 lua_gettable(L, -2); /* return old handler */ | 252 lua_gettable(L, LUA_REGISTRYINDEX); |
261 lua_pushnumber(L, sig); | 253 lua_pushnumber(L, sig); |
262 lua_pushnil(L); | 254 lua_gettable(L, -2); /* return old handler */ |
263 lua_settable(L, -4); | 255 lua_pushnumber(L, sig); |
264 lua_remove(L, -2); /* remove LUA_SIGNAL table */ | 256 lua_pushnil(L); |
265 signal(sig, SIG_DFL); | 257 lua_settable(L, -4); |
266 } else | 258 lua_remove(L, -2); /* remove LUA_SIGNAL table */ |
267 { | 259 signal(sig, SIG_DFL); |
268 luaL_checktype(L, 2, LUA_TFUNCTION); | 260 } else { |
269 | 261 luaL_checktype(L, 2, LUA_TFUNCTION); |
270 lua_pushstring(L, LUA_SIGNAL); | 262 |
271 lua_gettable(L, LUA_REGISTRYINDEX); | 263 lua_pushstring(L, LUA_SIGNAL); |
272 | 264 lua_gettable(L, LUA_REGISTRYINDEX); |
273 lua_pushnumber(L, sig); | 265 |
274 lua_pushvalue(L, 2); | 266 lua_pushnumber(L, sig); |
275 lua_settable(L, -3); | 267 lua_pushvalue(L, 2); |
276 | 268 lua_settable(L, -3); |
277 /* Set the state for the handler */ | 269 |
278 Lsig = L; | 270 /* Set the state for the handler */ |
279 | 271 Lsig = L; |
280 if (lua_toboolean(L, 3)) /* c hook? */ | 272 |
281 { | 273 if(lua_toboolean(L, 3)) { /* c hook? */ |
282 if (signal(sig, handle) == SIG_ERR) | 274 if(signal(sig, handle) == SIG_ERR) { |
283 lua_pushboolean(L, 0); | 275 lua_pushboolean(L, 0); |
284 else | 276 } else { |
285 lua_pushboolean(L, 1); | 277 lua_pushboolean(L, 1); |
286 } else /* lua_hook */ | 278 } |
287 { | 279 } else { /* lua_hook */ |
288 if (signal(sig, handle) == SIG_ERR) | 280 if(signal(sig, handle) == SIG_ERR) { |
289 lua_pushboolean(L, 0); | 281 lua_pushboolean(L, 0); |
290 else | 282 } else { |
291 lua_pushboolean(L, 1); | 283 lua_pushboolean(L, 1); |
292 } | 284 } |
293 } | 285 } |
294 return 1; | 286 } |
287 | |
288 return 1; | |
295 } | 289 } |
296 | 290 |
297 /* | 291 /* |
298 * l_raise == raise(signal) | 292 * l_raise == raise(signal) |
299 * | 293 * |
300 * signal = signal number or string | 294 * signal = signal number or string |
301 */ | 295 */ |
302 | 296 |
303 static int l_raise(lua_State *L) | 297 static int l_raise(lua_State* L) { |
304 { | 298 /* int args = lua_gettop(L); */ |
305 /* int args = lua_gettop(L); */ | 299 int t = 0; /* type */ |
306 int t = 0; /* type */ | 300 lua_Number ret; |
307 lua_Number ret; | 301 |
308 | 302 luaL_checkany(L, 1); |
309 luaL_checkany(L, 1); | 303 |
310 | 304 t = lua_type(L, 1); |
311 t = lua_type(L, 1); | 305 |
312 if (t == LUA_TNUMBER) | 306 if(t == LUA_TNUMBER) { |
313 { | 307 ret = (lua_Number) raise((int) lua_tonumber(L, 1)); |
314 ret = (lua_Number) raise((int) lua_tonumber(L, 1)); | 308 lua_pushnumber(L, ret); |
315 lua_pushnumber(L, ret); | 309 } else if(t == LUA_TSTRING) { |
316 } else if (t == LUA_TSTRING) | 310 lua_pushstring(L, LUA_SIGNAL); |
317 { | 311 lua_gettable(L, LUA_REGISTRYINDEX); |
318 lua_pushstring(L, LUA_SIGNAL); | 312 lua_pushvalue(L, 1); |
319 lua_gettable(L, LUA_REGISTRYINDEX); | 313 lua_gettable(L, -2); |
320 lua_pushvalue(L, 1); | 314 |
321 lua_gettable(L, -2); | 315 if(!lua_isnumber(L, -1)) { |
322 if (!lua_isnumber(L, -1)) | 316 luaL_error(L, "invalid signal string"); |
323 luaL_error(L, "invalid signal string"); | 317 } |
324 ret = (lua_Number) raise((int) lua_tonumber(L, -1)); | 318 |
325 lua_pop(L, 1); /* get rid of number we pushed */ | 319 ret = (lua_Number) raise((int) lua_tonumber(L, -1)); |
326 lua_pushnumber(L, ret); | 320 lua_pop(L, 1); /* get rid of number we pushed */ |
327 } else | 321 lua_pushnumber(L, ret); |
328 luaL_checknumber(L, 1); /* will always error, with good error msg */ | 322 } else { |
329 | 323 luaL_checknumber(L, 1); /* will always error, with good error msg */ |
330 return 1; | 324 } |
325 | |
326 return 1; | |
331 } | 327 } |
332 | 328 |
333 #if defined(__unix__) || defined(__APPLE__) | 329 #if defined(__unix__) || defined(__APPLE__) |
334 | 330 |
335 /* define some posix only functions */ | 331 /* define some posix only functions */ |
337 /* | 333 /* |
338 * l_kill == kill(pid, signal) | 334 * l_kill == kill(pid, signal) |
339 * | 335 * |
340 * pid = process id | 336 * pid = process id |
341 * signal = signal number or string | 337 * signal = signal number or string |
342 */ | 338 */ |
343 | 339 |
344 static int l_kill(lua_State *L) | 340 static int l_kill(lua_State* L) { |
345 { | 341 int t; /* type */ |
346 int t; /* type */ | 342 lua_Number ret; /* return value */ |
347 lua_Number ret; /* return value */ | 343 |
348 | 344 luaL_checknumber(L, 1); /* must be int for pid */ |
349 luaL_checknumber(L, 1); /* must be int for pid */ | 345 luaL_checkany(L, 2); /* check for a second arg */ |
350 luaL_checkany(L, 2); /* check for a second arg */ | 346 |
351 | 347 t = lua_type(L, 2); |
352 t = lua_type(L, 2); | 348 |
353 if (t == LUA_TNUMBER) | 349 if(t == LUA_TNUMBER) { |
354 { | 350 ret = (lua_Number) kill((int) lua_tonumber(L, 1), |
355 ret = (lua_Number) kill((int) lua_tonumber(L, 1), | 351 (int) lua_tonumber(L, 2)); |
356 (int) lua_tonumber(L, 2)); | 352 lua_pushnumber(L, ret); |
357 lua_pushnumber(L, ret); | 353 } else if(t == LUA_TSTRING) { |
358 } else if (t == LUA_TSTRING) | 354 lua_pushstring(L, LUA_SIGNAL); |
359 { | 355 lua_gettable(L, LUA_REGISTRYINDEX); |
360 lua_pushstring(L, LUA_SIGNAL); | 356 lua_pushvalue(L, 2); |
361 lua_gettable(L, LUA_REGISTRYINDEX); | 357 lua_gettable(L, -2); |
362 lua_pushvalue(L, 2); | 358 |
363 lua_gettable(L, -2); | 359 if(!lua_isnumber(L, -1)) { |
364 if (!lua_isnumber(L, -1)) | 360 luaL_error(L, "invalid signal string"); |
365 luaL_error(L, "invalid signal string"); | 361 } |
366 ret = (lua_Number) kill((int) lua_tonumber(L, 1), | 362 |
367 (int) lua_tonumber(L, -1)); | 363 ret = (lua_Number) kill((int) lua_tonumber(L, 1), |
368 lua_pop(L, 1); /* get rid of number we pushed */ | 364 (int) lua_tonumber(L, -1)); |
369 lua_pushnumber(L, ret); | 365 lua_pop(L, 1); /* get rid of number we pushed */ |
370 } else | 366 lua_pushnumber(L, ret); |
371 luaL_checknumber(L, 2); /* will always error, with good error msg */ | 367 } else { |
372 return 1; | 368 luaL_checknumber(L, 2); /* will always error, with good error msg */ |
369 } | |
370 | |
371 return 1; | |
373 } | 372 } |
374 | 373 |
375 #endif | 374 #endif |
376 | 375 |
377 static const struct luaL_Reg lsignal_lib[] = { | 376 static const struct luaL_Reg lsignal_lib[] = { |
378 {"signal", l_signal}, | 377 {"signal", l_signal}, |
379 {"raise", l_raise}, | 378 {"raise", l_raise}, |
380 #if defined(__unix__) || defined(__APPLE__) | 379 #if defined(__unix__) || defined(__APPLE__) |
381 {"kill", l_kill}, | 380 {"kill", l_kill}, |
382 #endif | 381 #endif |
383 {NULL, NULL} | 382 {NULL, NULL} |
384 }; | 383 }; |
385 | 384 |
386 int luaopen_util_signal(lua_State *L) | 385 int luaopen_util_signal(lua_State* L) { |
387 { | 386 int i = 0; |
388 int i = 0; | 387 |
389 | 388 /* add the library */ |
390 /* add the library */ | 389 lua_newtable(L); |
391 lua_newtable(L); | 390 luaL_register(L, NULL, lsignal_lib); |
392 luaL_register(L, NULL, lsignal_lib); | 391 |
393 | 392 /* push lua_signals table into the registry */ |
394 /* push lua_signals table into the registry */ | 393 /* put the signals inside the library table too, |
395 /* put the signals inside the library table too, | 394 * they are only a reference */ |
396 * they are only a reference */ | 395 lua_pushstring(L, LUA_SIGNAL); |
397 lua_pushstring(L, LUA_SIGNAL); | 396 lua_newtable(L); |
398 lua_newtable(L); | 397 |
399 | 398 while(lua_signals[i].name != NULL) { |
400 while (lua_signals[i].name != NULL) | 399 /* registry table */ |
401 { | 400 lua_pushstring(L, lua_signals[i].name); |
402 /* registry table */ | 401 lua_pushnumber(L, lua_signals[i].sig); |
403 lua_pushstring(L, lua_signals[i].name); | 402 lua_settable(L, -3); |
404 lua_pushnumber(L, lua_signals[i].sig); | 403 /* signal table */ |
405 lua_settable(L, -3); | 404 lua_pushstring(L, lua_signals[i].name); |
406 /* signal table */ | 405 lua_pushnumber(L, lua_signals[i].sig); |
407 lua_pushstring(L, lua_signals[i].name); | 406 lua_settable(L, -5); |
408 lua_pushnumber(L, lua_signals[i].sig); | 407 i++; |
409 lua_settable(L, -5); | 408 } |
410 i++; | 409 |
411 } | 410 /* add newtable to the registry */ |
412 | 411 lua_settable(L, LUA_REGISTRYINDEX); |
413 /* add newtable to the registry */ | 412 |
414 lua_settable(L, LUA_REGISTRYINDEX); | 413 return 1; |
415 | 414 } |
416 return 1; | |
417 } |