aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2025-03-09 15:50:01 +0100
committerMike Pall <mike>2025-03-09 15:50:01 +0100
commit423ac2144b0447c925596f4d93e073ce501ed4e1 (patch)
treeb1537bfeca7429baa05d8b175b3208a01fe55163
parent54dc2fa5d77ec35df302dc9bedc6ad7b0b4eae25 (diff)
downloadluajit-423ac2144b0447c925596f4d93e073ce501ed4e1.tar.gz
luajit-423ac2144b0447c925596f4d93e073ce501ed4e1.tar.bz2
luajit-423ac2144b0447c925596f4d93e073ce501ed4e1.zip
Improve CLI signal handling on POSIX.
-rw-r--r--src/luajit.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/luajit.c b/src/luajit.c
index a725db1c..2e2e9150 100644
--- a/src/luajit.c
+++ b/src/luajit.c
@@ -35,6 +35,21 @@
35 35
36#if !LJ_TARGET_CONSOLE 36#if !LJ_TARGET_CONSOLE
37#include <signal.h> 37#include <signal.h>
38
39#if LJ_TARGET_POSIX
40/* Improve signal handling on POSIX. Try CTRL-C on: luajit -e 'io.read()' */
41static void signal_set(int sig, void (*h)(int))
42{
43 struct sigaction sa;
44 memset(&sa, 0, sizeof(sa));
45 sa.sa_handler = h;
46 sigemptyset(&sa.sa_mask);
47 sigaction(sig, &sa, NULL);
48}
49#else
50#define signal_set signal
51#endif
52
38#endif 53#endif
39 54
40static lua_State *globalL = NULL; 55static lua_State *globalL = NULL;
@@ -54,8 +69,8 @@ static void lstop(lua_State *L, lua_Debug *ar)
54 69
55static void laction(int i) 70static void laction(int i)
56{ 71{
57 signal(i, SIG_DFL); /* if another SIGINT happens before lstop, 72 /* Terminate process if another SIGINT happens (double CTRL-C). */
58 terminate process (default action) */ 73 signal_set(i, SIG_DFL);
59 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); 74 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
60} 75}
61#endif 76#endif
@@ -117,11 +132,11 @@ static int docall(lua_State *L, int narg, int clear)
117 lua_pushcfunction(L, traceback); /* push traceback function */ 132 lua_pushcfunction(L, traceback); /* push traceback function */
118 lua_insert(L, base); /* put it under chunk and args */ 133 lua_insert(L, base); /* put it under chunk and args */
119#if !LJ_TARGET_CONSOLE 134#if !LJ_TARGET_CONSOLE
120 signal(SIGINT, laction); 135 signal_set(SIGINT, laction);
121#endif 136#endif
122 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); 137 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
123#if !LJ_TARGET_CONSOLE 138#if !LJ_TARGET_CONSOLE
124 signal(SIGINT, SIG_DFL); 139 signal_set(SIGINT, SIG_DFL);
125#endif 140#endif
126 lua_remove(L, base); /* remove traceback function */ 141 lua_remove(L, base); /* remove traceback function */
127 /* force a complete garbage collection in case of errors */ 142 /* force a complete garbage collection in case of errors */