Software /
code /
prosody
Annotate
util-src/lsignal.c @ 2790:fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 10 Jan 2010 00:28:48 +0000 |
parent | 2770:716260e3b017 |
child | 2791:e23b642bdfd1 |
rev | line source |
---|---|
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 /* |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 * lsignal.h -- Signal Handler Library for Lua |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 * Version: 1.000 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 * Copyright (C) 2007 Patrick J. Donnelly (batrick@unm.edu) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 * This software is distributed under the same license as Lua 5.0: |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 * Permission is hereby granted, free of charge, to any person obtaining a |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 * copy of this software and associated documentation files (the "Software"), |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 * to deal in the Software without restriction, including without limitation |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 * and/or sell copies of the Software, and to permit persons to whom the |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 * Software is furnished to do so, subject to the following conditions: |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 * The above copyright notice and this permission notice shall be included |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 * in all copies or substantial portions of the Software. |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 * OTHER DEALINGS IN THE SOFTWARE. |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 #include <signal.h> |
2790
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
30 #include <malloc.h> |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 #include "lua.h" |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 #include "lauxlib.h" |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 #ifndef lsig |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 #define lsig |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 struct lua_signal |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 char *name; /* name of the signal */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 int sig; /* the signal */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 }; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 #define LUA_SIGNAL "lua_signal" |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 static const struct lua_signal lua_signals[] = { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 /* ANSI C signals */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 #ifdef SIGABRT |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 {"SIGABRT", SIGABRT}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 #ifdef SIGFPE |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 {"SIGFPE", SIGFPE}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 #ifdef SIGILL |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 {"SIGILL", SIGILL}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 #ifdef SIGINT |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 {"SIGINT", SIGINT}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 #ifdef SIGSEGV |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 {"SIGSEGV", SIGSEGV}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 #ifdef SIGTERM |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 {"SIGTERM", SIGTERM}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 /* posix signals */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 #ifdef SIGHUP |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 {"SIGHUP", SIGHUP}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 #ifdef SIGQUIT |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 {"SIGQUIT", SIGQUIT}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 #ifdef SIGTRAP |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 {"SIGTRAP", SIGTRAP}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 #ifdef SIGKILL |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 {"SIGKILL", SIGKILL}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 #ifdef SIGUSR1 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 {"SIGUSR1", SIGUSR1}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 #ifdef SIGUSR2 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 {"SIGUSR2", SIGUSR2}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 #ifdef SIGPIPE |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 {"SIGPIPE", SIGPIPE}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 #ifdef SIGALRM |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 {"SIGALRM", SIGALRM}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 #ifdef SIGCHLD |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 {"SIGCHLD", SIGCHLD}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 #ifdef SIGCONT |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 {"SIGCONT", SIGCONT}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 #ifdef SIGSTOP |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 {"SIGSTOP", SIGSTOP}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 #ifdef SIGTTIN |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 {"SIGTTIN", SIGTTIN}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 #ifdef SIGTTOU |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 {"SIGTTOU", SIGTTOU}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 /* some BSD signals */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 #ifdef SIGIOT |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 {"SIGIOT", SIGIOT}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 #ifdef SIGBUS |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 {"SIGBUS", SIGBUS}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 #ifdef SIGCLD |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 {"SIGCLD", SIGCLD}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 #ifdef SIGURG |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 {"SIGURG", SIGURG}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 #ifdef SIGXCPU |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 {"SIGXCPU", SIGXCPU}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 #ifdef SIGXFSZ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 {"SIGXFSZ", SIGXFSZ}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 #ifdef SIGVTALRM |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 {"SIGVTALRM", SIGVTALRM}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 #ifdef SIGPROF |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 {"SIGPROF", SIGPROF}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 #ifdef SIGWINCH |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 {"SIGWINCH", SIGWINCH}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 #ifdef SIGPOLL |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 {"SIGPOLL", SIGPOLL}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 #ifdef SIGIO |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 {"SIGIO", SIGIO}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 /* add odd signals */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 #ifdef SIGSTKFLT |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 {"SIGSTKFLT", SIGSTKFLT}, /* stack fault */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 #ifdef SIGSYS |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 {"SIGSYS", SIGSYS}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 {NULL, 0} |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 }; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 static lua_State *Lsig = NULL; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 static lua_Hook Hsig = NULL; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 static int Hmask = 0; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 static int Hcount = 0; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 |
2790
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
158 static struct signal_event |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
159 { |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
160 int Nsig; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
161 struct signal_event *next_event; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
162 } *signal_queue = NULL; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
163 |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
164 static struct signal_event *last_event = NULL; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
165 |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 static void sighook(lua_State *L, lua_Debug *ar) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 lua_pushstring(L, LUA_SIGNAL); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 lua_gettable(L, LUA_REGISTRYINDEX); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 |
2790
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
171 struct signal_event *event; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
172 while((event = signal_queue)) |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
173 { |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
174 lua_pushnumber(L, event->Nsig); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
175 lua_gettable(L, -2); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
176 lua_call(L, 0, 0); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
177 signal_queue = event->next_event; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
178 free(event); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
179 }; |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 |
2790
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
181 lua_pop(L, 1); /* pop lua_signal table */ |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
182 |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
183 /* restore the old hook */ |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 lua_sethook(L, Hsig, Hmask, Hcount); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 } |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 static void handle(int sig) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 { |
2790
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
189 if(!signal_queue) |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
190 { |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
191 /* Store the existing debug hook (if any) and its parameters */ |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
192 Hsig = lua_gethook(Lsig); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
193 Hmask = lua_gethookmask(Lsig); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
194 Hcount = lua_gethookcount(Lsig); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
195 |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
196 signal_queue = malloc(sizeof(struct signal_event)); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
197 signal_queue->Nsig = sig; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
198 signal_queue->next_event = NULL; |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 |
2790
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
200 last_event = signal_queue; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
201 |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
202 /* Set our new debug hook */ |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
203 lua_sethook(Lsig, sighook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
204 } |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
205 else |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 { |
2790
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
207 last_event->next_event = malloc(sizeof(struct signal_event)); |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
208 last_event->next_event->Nsig = sig; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
209 last_event->next_event->next_event = NULL; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
210 |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
211 last_event = last_event->next_event; |
fa717c0be65c
util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents:
2770
diff
changeset
|
212 } |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 } |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 /* |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 * l_signal == signal(signal [, func [, chook]]) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 * signal = signal number or string |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 * func = Lua function to call |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 * chook = catch within C functions |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 * if caught, Lua function _must_ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 * exit, as the stack is most likely |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 * in an unstable state. |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 static int l_signal(lua_State *L) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 int args = lua_gettop(L); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 int t, sig; /* type, signal */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 /* get type of signal */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 luaL_checkany(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 t = lua_type(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 if (t == LUA_TNUMBER) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 sig = (int) lua_tonumber(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 else if (t == LUA_TSTRING) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 lua_pushstring(L, LUA_SIGNAL); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 lua_gettable(L, LUA_REGISTRYINDEX); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 lua_pushvalue(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 lua_gettable(L, -2); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 if (!lua_isnumber(L, -1)) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 luaL_error(L, "invalid signal string"); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 sig = (int) lua_tonumber(L, -1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 lua_pop(L, 1); /* get rid of number we pushed */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 } else |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 luaL_checknumber(L, 1); /* will always error, with good error msg */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 /* set handler */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 if (args == 1 || lua_isnil(L, 2)) /* clear handler */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 lua_pushstring(L, LUA_SIGNAL); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 lua_gettable(L, LUA_REGISTRYINDEX); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 lua_pushnumber(L, sig); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 lua_gettable(L, -2); /* return old handler */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 lua_pushnumber(L, sig); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 lua_pushnil(L); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 lua_settable(L, -4); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
259 lua_remove(L, -2); /* remove LUA_SIGNAL table */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 signal(sig, SIG_DFL); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
261 } else |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
262 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 luaL_checktype(L, 2, LUA_TFUNCTION); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 lua_pushstring(L, LUA_SIGNAL); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 lua_gettable(L, LUA_REGISTRYINDEX); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 lua_pushnumber(L, sig); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 lua_pushvalue(L, 2); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 lua_settable(L, -3); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 /* Set the state for the handler */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 Lsig = L; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 if (lua_toboolean(L, 3)) /* c hook? */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 if (signal(sig, handle) == SIG_ERR) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 lua_pushboolean(L, 0); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
279 else |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 lua_pushboolean(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 } else /* lua_hook */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 if (signal(sig, handle) == SIG_ERR) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
284 lua_pushboolean(L, 0); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 else |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 lua_pushboolean(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 } |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
288 } |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 return 1; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 } |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
292 /* |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 * l_raise == raise(signal) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
295 * signal = signal number or string |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
296 */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
297 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
298 static int l_raise(lua_State *L) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
299 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
300 /* int args = lua_gettop(L); */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
301 int t = 0; /* type */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
302 lua_Number ret; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
303 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
304 luaL_checkany(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
305 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
306 t = lua_type(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
307 if (t == LUA_TNUMBER) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
308 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
309 ret = (lua_Number) raise((int) lua_tonumber(L, 1)); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
310 lua_pushnumber(L, ret); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
311 } else if (t == LUA_TSTRING) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
312 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
313 lua_pushstring(L, LUA_SIGNAL); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
314 lua_gettable(L, LUA_REGISTRYINDEX); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
315 lua_pushvalue(L, 1); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
316 lua_gettable(L, -2); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
317 if (!lua_isnumber(L, -1)) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
318 luaL_error(L, "invalid signal string"); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
319 ret = (lua_Number) raise((int) lua_tonumber(L, -1)); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
320 lua_pop(L, 1); /* get rid of number we pushed */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
321 lua_pushnumber(L, ret); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 } else |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
323 luaL_checknumber(L, 1); /* will always error, with good error msg */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
324 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 return 1; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
326 } |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
327 |
1841
0dc1daa94e7f
util.signal: Change ifdef to allow signal.kill() on Solaris (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
988
diff
changeset
|
328 #if defined _POSIX_SOURCE || (defined(sun) || defined(__sun)) |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
329 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
330 /* define some posix only functions */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
331 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
332 /* |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
333 * l_kill == kill(pid, signal) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
334 * |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
335 * pid = process id |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
336 * signal = signal number or string |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
337 */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
338 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
339 static int l_kill(lua_State *L) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
340 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
341 int t; /* type */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
342 lua_Number ret; /* return value */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
343 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
344 luaL_checknumber(L, 1); /* must be int for pid */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
345 luaL_checkany(L, 2); /* check for a second arg */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
346 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
347 t = lua_type(L, 2); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
348 if (t == LUA_TNUMBER) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
349 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
350 ret = (lua_Number) kill((int) lua_tonumber(L, 1), |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
351 (int) lua_tonumber(L, 2)); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
352 lua_pushnumber(L, ret); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
353 } else if (t == LUA_TSTRING) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
354 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
355 lua_pushstring(L, LUA_SIGNAL); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
356 lua_gettable(L, LUA_REGISTRYINDEX); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
357 lua_pushvalue(L, 2); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
358 lua_gettable(L, -2); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
359 if (!lua_isnumber(L, -1)) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
360 luaL_error(L, "invalid signal string"); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
361 ret = (lua_Number) kill((int) lua_tonumber(L, 1), |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
362 (int) lua_tonumber(L, -1)); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
363 lua_pop(L, 1); /* get rid of number we pushed */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
364 lua_pushnumber(L, ret); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
365 } else |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
366 luaL_checknumber(L, 2); /* will always error, with good error msg */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
367 return 1; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
368 } |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
369 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
370 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
371 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
372 static const struct luaL_Reg lsignal_lib[] = { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
373 {"signal", l_signal}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
374 {"raise", l_raise}, |
2770
716260e3b017
util.signal: Make kill() available on Solaris; really fixes #136.
Waqas Hussain <waqas20@gmail.com>
parents:
1841
diff
changeset
|
375 #if defined _POSIX_SOURCE || (defined(sun) || defined(__sun)) |
988
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
376 {"kill", l_kill}, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
377 #endif |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
378 {NULL, NULL} |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
379 }; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
380 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
381 int luaopen_util_signal(lua_State *L) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
382 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
383 int i = 0; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
384 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
385 /* add the library */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
386 luaL_register(L, "signal", lsignal_lib); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
387 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
388 /* push lua_signals table into the registry */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
389 /* put the signals inside the library table too, |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
390 * they are only a reference */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
391 lua_pushstring(L, LUA_SIGNAL); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
392 lua_createtable(L, 0, 0); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
393 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
394 while (lua_signals[i].name != NULL) |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
395 { |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
396 /* registry table */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
397 lua_pushstring(L, lua_signals[i].name); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
398 lua_pushnumber(L, lua_signals[i].sig); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
399 lua_settable(L, -3); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
400 /* signal table */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
401 lua_pushstring(L, lua_signals[i].name); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
402 lua_pushnumber(L, lua_signals[i].sig); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
403 lua_settable(L, -5); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
404 i++; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
405 } |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
406 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
407 /* add newtable to the registry */ |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
408 lua_settable(L, LUA_REGISTRYINDEX); |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
409 |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
410 return 1; |
8cb9a0d59e78
util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
411 } |