Annotate

util-src/lsignal.c @ 988:8cb9a0d59e78

util.lsignal: Add lsignal library for catching POSIX signals
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Apr 2009 02:53:50 +0100
child 1841:0dc1daa94e7f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 #include "lua.h"
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 #include "lauxlib.h"
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 #ifndef lsig
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 #define lsig
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 struct lua_signal
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 char *name; /* name of the signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 int sig; /* the signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 };
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 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 #define LUA_SIGNAL "lua_signal"
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 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
49 /* ANSI C signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 #ifdef SIGABRT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 {"SIGABRT", SIGABRT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 #ifdef SIGFPE
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 {"SIGFPE", SIGFPE},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 #ifdef SIGILL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 {"SIGILL", SIGILL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 #ifdef SIGINT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 {"SIGINT", SIGINT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 #ifdef SIGSEGV
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 {"SIGSEGV", SIGSEGV},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 #ifdef SIGTERM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 {"SIGTERM", SIGTERM},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 /* posix signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 #ifdef SIGHUP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 {"SIGHUP", SIGHUP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 #ifdef SIGQUIT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 {"SIGQUIT", SIGQUIT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 #ifdef SIGTRAP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 {"SIGTRAP", SIGTRAP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 #ifdef SIGKILL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 {"SIGKILL", SIGKILL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 #ifdef SIGUSR1
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 {"SIGUSR1", SIGUSR1},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 #ifdef SIGUSR2
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 {"SIGUSR2", SIGUSR2},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 #ifdef SIGPIPE
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 {"SIGPIPE", SIGPIPE},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 #ifdef SIGALRM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 {"SIGALRM", SIGALRM},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 #ifdef SIGCHLD
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 {"SIGCHLD", SIGCHLD},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 #ifdef SIGCONT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 {"SIGCONT", SIGCONT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 #ifdef SIGSTOP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 {"SIGSTOP", SIGSTOP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 #ifdef SIGTTIN
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 {"SIGTTIN", SIGTTIN},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 #ifdef SIGTTOU
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 {"SIGTTOU", SIGTTOU},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 /* some BSD signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 #ifdef SIGIOT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 {"SIGIOT", SIGIOT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 #ifdef SIGBUS
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 {"SIGBUS", SIGBUS},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 #ifdef SIGCLD
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 {"SIGCLD", SIGCLD},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 #ifdef SIGURG
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 {"SIGURG", SIGURG},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 #ifdef SIGXCPU
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 {"SIGXCPU", SIGXCPU},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 #ifdef SIGXFSZ
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 {"SIGXFSZ", SIGXFSZ},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 #ifdef SIGVTALRM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 {"SIGVTALRM", SIGVTALRM},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 #ifdef SIGPROF
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 {"SIGPROF", SIGPROF},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 #ifdef SIGWINCH
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 {"SIGWINCH", SIGWINCH},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 #ifdef SIGPOLL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 {"SIGPOLL", SIGPOLL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 #ifdef SIGIO
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 {"SIGIO", SIGIO},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 /* add odd signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 #ifdef SIGSTKFLT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 {"SIGSTKFLT", SIGSTKFLT}, /* stack fault */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 #ifdef SIGSYS
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 {"SIGSYS", SIGSYS},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 {NULL, 0}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 };
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 static int Nsig = 0;
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
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 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
159 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 lua_pushnumber(L, Nsig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 lua_call(L, 0, 0);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 /* set the old hook */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 lua_sethook(L, Hsig, Hmask, Hcount);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 }
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 static void handle(int sig)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 Hsig = lua_gethook(Lsig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 Hmask = lua_gethookmask(Lsig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 Hcount = lua_gethookcount(Lsig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 Nsig = sig;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 lua_sethook(Lsig, sighook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 /*
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 switch (sig)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 case SIGABRT: ;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 case SIGFPE: ;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 case SIGILL: ;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 case SIGINT: ;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 case SIGSEGV: ;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 case SIGTERM: ;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 } */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 }
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 /*
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 * l_signal == signal(signal [, func [, chook]])
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 * func = Lua function to call
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 * chook = catch within C functions
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 * if caught, Lua function _must_
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 * 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
199 * in an unstable state.
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 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
203 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 int args = lua_gettop(L);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 int t, sig; /* type, signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 /* get type of signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 luaL_checkany(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 t = lua_type(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 if (t == LUA_TNUMBER)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 sig = (int) lua_tonumber(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 else if (t == LUA_TSTRING)
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 lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 lua_pushvalue(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 sig = (int) lua_tonumber(L, -1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 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
222 } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 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
224
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 /* set handler */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 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
227 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 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
232 lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 lua_pushnil(L);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 lua_settable(L, -4);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 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
236 signal(sig, SIG_DFL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 luaL_checktype(L, 2, LUA_TFUNCTION);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 lua_pushvalue(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 lua_settable(L, -3);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 /* Set the state for the handler */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 Lsig = L;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 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
252 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 if (signal(sig, handle) == SIG_ERR)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 lua_pushboolean(L, 0);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 lua_pushboolean(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 } else /* lua_hook */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 if (signal(sig, handle) == SIG_ERR)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 lua_pushboolean(L, 0);
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 lua_pushboolean(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 }
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 return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 }
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 /*
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 * l_raise == raise(signal)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 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
275 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 /* int args = lua_gettop(L); */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 int t = 0; /* type */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 lua_Number ret;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 luaL_checkany(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 t = lua_type(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 if (t == LUA_TNUMBER)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 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
286 lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 } else if (t == LUA_TSTRING)
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 lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 lua_pushvalue(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 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
296 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
297 lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 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
300
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 }
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 #ifdef _POSIX_SOURCE
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 /* define some posix only functions */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307
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 * l_kill == kill(pid, signal)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 * pid = process id
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 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
316 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 int t; /* type */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 lua_Number ret; /* return value */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 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
321 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
322
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 t = lua_type(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 if (t == LUA_TNUMBER)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 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
327 (int) lua_tonumber(L, 2));
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 } else if (t == LUA_TSTRING)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 lua_pushvalue(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 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
338 (int) lua_tonumber(L, -1));
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 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
340 lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 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
343 return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 }
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 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
349 {"signal", l_signal},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 {"raise", l_raise},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 #ifdef _POSIX_SOURCE
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 {"kill", l_kill},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 #endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 {NULL, NULL}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 };
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 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
358 {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 int i = 0;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 /* add the library */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 luaL_register(L, "signal", lsignal_lib);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 /* 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
365 /* 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
366 * they are only a reference */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 lua_createtable(L, 0, 0);
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 while (lua_signals[i].name != NULL)
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 /* registry table */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 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
374 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
375 lua_settable(L, -3);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 /* signal table */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 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
378 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
379 lua_settable(L, -5);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 i++;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 }
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 /* add newtable to the registry */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 lua_settable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 }