diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-11-24 14:41:50 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-11-24 14:41:50 -0300 |
commit | 131e3fd814a6e818b412407a222186aab08f3525 (patch) | |
tree | 88224c7adda723ae6a558bb4e3e391f50f8292cf | |
parent | 9d067ab73b6befa0a5418f1df35c711f6c6918b3 (diff) | |
download | lua-131e3fd814a6e818b412407a222186aab08f3525.tar.gz lua-131e3fd814a6e818b412407a222186aab08f3525.tar.bz2 lua-131e3fd814a6e818b412407a222186aab08f3525.zip |
Avoid using 'signal' when 'sigaction' is available
The semantics of 'signal' varies a lot among different implementations;
'sigaction' ensures a more consistent behavior.
-rw-r--r-- | lua.c | 26 |
1 files changed, 23 insertions, 3 deletions
@@ -37,6 +37,26 @@ static lua_State *globalL = NULL; | |||
37 | static const char *progname = LUA_PROGNAME; | 37 | static const char *progname = LUA_PROGNAME; |
38 | 38 | ||
39 | 39 | ||
40 | #if defined(LUA_USE_POSIX) /* { */ | ||
41 | |||
42 | /* | ||
43 | ** Use 'sigaction' when available. | ||
44 | */ | ||
45 | static void setsignal (int sig, void (*handler)(int)) { | ||
46 | struct sigaction sa; | ||
47 | sa.sa_handler = handler; | ||
48 | sa.sa_flags = 0; | ||
49 | sigemptyset(&sa.sa_mask); /* do not mask any signal */ | ||
50 | sigaction(sig, &sa, NULL); | ||
51 | } | ||
52 | |||
53 | #else /* }{ */ | ||
54 | |||
55 | #define setsignal signal | ||
56 | |||
57 | #endif /* } */ | ||
58 | |||
59 | |||
40 | /* | 60 | /* |
41 | ** Hook set by signal function to stop the interpreter. | 61 | ** Hook set by signal function to stop the interpreter. |
42 | */ | 62 | */ |
@@ -55,7 +75,7 @@ static void lstop (lua_State *L, lua_Debug *ar) { | |||
55 | */ | 75 | */ |
56 | static void laction (int i) { | 76 | static void laction (int i) { |
57 | int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; | 77 | int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; |
58 | signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ | 78 | setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ |
59 | lua_sethook(globalL, lstop, flag, 1); | 79 | lua_sethook(globalL, lstop, flag, 1); |
60 | } | 80 | } |
61 | 81 | ||
@@ -135,9 +155,9 @@ static int docall (lua_State *L, int narg, int nres) { | |||
135 | lua_pushcfunction(L, msghandler); /* push message handler */ | 155 | lua_pushcfunction(L, msghandler); /* push message handler */ |
136 | lua_insert(L, base); /* put it under function and args */ | 156 | lua_insert(L, base); /* put it under function and args */ |
137 | globalL = L; /* to be available to 'laction' */ | 157 | globalL = L; /* to be available to 'laction' */ |
138 | signal(SIGINT, laction); /* set C-signal handler */ | 158 | setsignal(SIGINT, laction); /* set C-signal handler */ |
139 | status = lua_pcall(L, narg, nres, base); | 159 | status = lua_pcall(L, narg, nres, base); |
140 | signal(SIGINT, SIG_DFL); /* reset C-signal handler */ | 160 | setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */ |
141 | lua_remove(L, base); /* remove message handler from the stack */ | 161 | lua_remove(L, base); /* remove message handler from the stack */ |
142 | return status; | 162 | return status; |
143 | } | 163 | } |