moonbridge
changeset 280:1a4f89f4c712
Modified behavior of moonbridge_io.signalsocket(...); Fixed usage of moonbridge_io.signalsocket(...) in moonbridge_http module
author | jbe |
---|---|
date | Tue Jun 06 21:58:26 2017 +0200 (2017-06-06) |
parents | dec24bd2592c |
children | 6bb191b6ead5 |
files | moonbridge_http.lua moonbridge_io.c reference.txt |
line diff
1.1 --- a/moonbridge_http.lua Tue Jun 06 20:19:09 2017 +0200 1.2 +++ b/moonbridge_http.lua Tue Jun 06 21:58:26 2017 +0200 1.3 @@ -203,10 +203,10 @@ 1.4 local response_timeout = default("response_timeout", 3600) 1.5 local drain_timeout = default("drain_timeout", 2) 1.6 local poll = options.poll_function or moonbridge_io.poll 1.7 - -- install SIGTERM handler: 1.8 - local sigterm_socket = moonbridge_io.signalsocket("TERM") 1.9 -- return socket handler: 1.10 return function(socket) 1.11 + -- install SIGTERM handler and initialize some variables: 1.12 + local sigterm_socket = moonbridge_io.signalsocket("TERM") 1.13 local socket_set = {[socket] = true} -- used for poll function 1.14 local socket_term_set = { -- used for poll function 1.15 [socket] = true,
2.1 --- a/moonbridge_io.c Tue Jun 06 20:19:09 2017 +0200 2.2 +++ b/moonbridge_io.c Tue Jun 06 21:58:26 2017 +0200 2.3 @@ -66,6 +66,7 @@ 2.4 #define MOONBR_IO_CHILD_MT_REGKEY "moonbridge_io_child" 2.5 #define MOONBR_IO_CHILD_PT_REGKEY "moonbridge_io_child_pt" 2.6 #define MOONBR_IO_SIGNALS_REGKEY "moonbridge_io_signals" 2.7 +#define MOONBR_IO_SIGNALSOCKETS_REGKEY "moonbridge_io_signalsockets" 2.8 2.9 #ifdef MOONBR_IO_USE_TLS 2.10 2.11 @@ -1641,13 +1642,20 @@ 2.12 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, moonbr_io_signalfds+2*sig)) { 2.13 luaL_error(L, "Could not create socket pair for signal queueing"); 2.14 } 2.15 - errno = 0; 2.16 - signal(sig, moonbr_io_signalhandler); 2.17 - if (errno) luaL_error(L, "Could not install signal handler (invalid signal number %i?)", sig); 2.18 } 2.19 - fd = dup(moonbr_io_signalfd_read(sig)); 2.20 + errno = 0; 2.21 + signal(sig, moonbr_io_signalhandler); 2.22 + if (errno) luaL_error(L, "Could not install signal handler (invalid signal number %i?)", sig); 2.23 + lua_getfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALSOCKETS_REGKEY); 2.24 + lua_pushinteger(L, sig); 2.25 + lua_gettable(L, -2); 2.26 + if (!lua_isnil(L, -1)) return 1; 2.27 + fd = dup(moonbr_io_signalfd_read(sig)); /* avoid close on Lua error */ 2.28 if (fd < 0) luaL_error(L, "Could not create duplicated file descriptor for signal socket"); 2.29 moonbr_io_pushhandle(L, fd); 2.30 + lua_pushinteger(L, sig); 2.31 + lua_pushvalue(L, -2); 2.32 + lua_settable(L, -5); /* store reference to signal socket in cache table */ 2.33 return 1; 2.34 } 2.35 2.36 @@ -2110,6 +2118,9 @@ 2.37 lua_pushvalue(L, -1); 2.38 lua_setfield(L, -3, "signals"); 2.39 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALS_REGKEY); 2.40 + 2.41 + lua_newtable(L); // signal sockets 2.42 + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALSOCKETS_REGKEY); 2.43 2.44 luaL_setfuncs(L, moonbr_io_module_funcs, 0); 2.45 return 1;
3.1 --- a/reference.txt Tue Jun 06 20:19:09 2017 +0200 3.2 +++ b/reference.txt Tue Jun 06 21:58:26 2017 +0200 3.3 @@ -399,15 +399,14 @@ 3.4 ### moonbridge_io.signalsocket(signal) 3.5 3.6 This function installs a signal handler. As argument, either the signal number 3.7 -is passed (e.g. 15) or a name (e.g. "TERM"). 3.8 - 3.9 -The function returns a new socket object that receives a character (".") each 3.10 -time a signal is received. 3.11 +is passed (e.g. 15) or a name (e.g. "TERM"). The function returns a socket 3.12 +object that receives a character (".") each time a signal is received. 3.13 3.14 -Note that each socket object has an independent buffer. It is thus recommended 3.15 -to always read all bytes, e.g. by using the expression: 3.16 +The function can be called multiple times, in which case the same socket object 3.17 +is returned. The returned socket should never be closed by the caller. 3.18 3.19 -#(assert(sigsock:read_nb())) > 0 -- true if signal occurred 3.20 +The process should not be forked after calling this function (except for 3.21 +replacing the process with another program). 3.22 3.23 3.24 ### moonbridge_io.tcpconnect(hostname, port)