commit 131e3fd814a6e818b412407a222186aab08f3525
parent 9d067ab73b6befa0a5418f1df35c711f6c6918b3
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 24 Nov 2020 14:41:23 -0300
Avoid using 'signal' when 'sigaction' is available
The semantics of 'signal' varies a lot among different implementations;
'sigaction' ensures a more consistent behavior.
Diffstat:
M | lua.c | | | 26 | +++++++++++++++++++++++--- |
1 file changed, 23 insertions(+), 3 deletions(-)
diff --git a/lua.c b/lua.c
@@ -37,6 +37,26 @@ static lua_State *globalL = NULL;
static const char *progname = LUA_PROGNAME;
+#if defined(LUA_USE_POSIX) /* { */
+
+/*
+** Use 'sigaction' when available.
+*/
+static void setsignal (int sig, void (*handler)(int)) {
+ struct sigaction sa;
+ sa.sa_handler = handler;
+ sa.sa_flags = 0;
+ sigemptyset(&sa.sa_mask); /* do not mask any signal */
+ sigaction(sig, &sa, NULL);
+}
+
+#else /* }{ */
+
+#define setsignal signal
+
+#endif /* } */
+
+
/*
** Hook set by signal function to stop the interpreter.
*/
@@ -55,7 +75,7 @@ static void lstop (lua_State *L, lua_Debug *ar) {
*/
static void laction (int i) {
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
- signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
+ setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
lua_sethook(globalL, lstop, flag, 1);
}
@@ -135,9 +155,9 @@ static int docall (lua_State *L, int narg, int nres) {
lua_pushcfunction(L, msghandler); /* push message handler */
lua_insert(L, base); /* put it under function and args */
globalL = L; /* to be available to 'laction' */
- signal(SIGINT, laction); /* set C-signal handler */
+ setsignal(SIGINT, laction); /* set C-signal handler */
status = lua_pcall(L, narg, nres, base);
- signal(SIGINT, SIG_DFL); /* reset C-signal handler */
+ setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */
lua_remove(L, base); /* remove message handler from the stack */
return status;
}