# HG changeset patch # User jbe # Date 1497722823 -7200 # Node ID 1eef86d3b9255ee107045832692d813739d2f7ca # Parent c78bb327ef6ad39d72e367daf152446c9546211b Make moonbridge_io.poll(...) check for SIGTERM also when wakeup is forced due to TLS negotiation or closed local socket diff -r c78bb327ef6a -r 1eef86d3b925 moonbridge_io.c --- a/moonbridge_io.c Mon Jun 12 15:38:27 2017 +0200 +++ b/moonbridge_io.c Sat Jun 17 20:07:03 2017 +0200 @@ -1629,13 +1629,13 @@ #define moonbr_io_poll_tls() \ if (!handle->tlshandshake) { \ - lua_pushboolean(L, 1); \ - return 1; \ + force_wakeup = 1; \ + continue; \ } \ if (handle->tlshandshake == TLS_WANT_POLLIN) { \ if (fd < 0) { \ - lua_pushboolean(L, 1); \ - return 1; \ + force_wakeup = 1; \ + continue; \ } \ FD_SET(fd, &readfds); \ if (fd+1 > nfds) nfds = fd+1; \ @@ -1643,8 +1643,8 @@ } \ if (handle->tlshandshake == TLS_WANT_POLLOUT) { \ if (fd < 0) { \ - lua_pushboolean(L, 1); \ - return 1; \ + force_wakeup = 1; \ + continue; \ } \ FD_SET(fd, &writefds); \ if (fd+1 > nfds) nfds = fd+1; \ @@ -1661,6 +1661,7 @@ int nfds = 0; fd_set readfds, writefds, exceptfds; struct timespec timeout = {0, }; + int force_wakeup = 0; int use_timeout = 0; // negative for negative timeout int check_sigterm = 0; sigset_t mask, orig_mask; @@ -1683,8 +1684,8 @@ fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */ handle->readbufin != handle->readbufout /* data pending in buffer */ ) { - lua_pushboolean(L, 1); - return 1; + force_wakeup = 1; + continue; } } else { listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY); @@ -1739,14 +1740,19 @@ luaL_argcheck(L, 0, 3, "not a valid timeout"); } } + if (use_timeout < 0) force_wakeup = 1; if (!lua_isnoneornil(L, 4)) luaL_checktype(L, 4, LUA_TBOOLEAN); check_sigterm = lua_toboolean(L, 4); if (check_sigterm) { - sigemptyset(&mask); - sigaddset(&mask, SIGTERM); - if (use_timeout >= 0) sigprocmask(SIG_BLOCK, &mask, &orig_mask); + if (!force_wakeup) { + sigemptyset(&mask); + sigaddset(&mask, SIGTERM); + if (sigprocmask(SIG_BLOCK, &mask, &orig_mask)) abort(); + } if (moonbr_io_sigterm_flag) { - if (use_timeout >= 0) if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); + if (!force_wakeup) { + if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); + } lua_pushboolean(L, 0); lua_pushliteral(L, "SIGTERM received"); lua_pushboolean(L, 1); @@ -1763,41 +1769,42 @@ return 2; } } - status = pselect( - nfds, &readfds, &writefds, &exceptfds, - use_timeout ? &timeout : NULL, - check_sigterm ? &orig_mask : NULL - ); - if (check_sigterm) { - if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); - if (moonbr_io_sigterm_flag) { + if (!force_wakeup) { + status = pselect( + nfds, &readfds, &writefds, &exceptfds, + use_timeout ? &timeout : NULL, + check_sigterm ? &orig_mask : NULL + ); + if (check_sigterm) { + if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); + if (moonbr_io_sigterm_flag) { + lua_pushboolean(L, 0); + lua_pushliteral(L, "SIGTERM received"); + lua_pushboolean(L, 1); + return 3; + } + } + if (status == -1) { + if (errno == EINTR) { + lua_pushboolean(L, 1); + return 1; + } else { + moonbr_io_prepare_errmsg(); + return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg); + } + } else if (status == 0) { lua_pushboolean(L, 0); - lua_pushliteral(L, "SIGTERM received"); - lua_pushboolean(L, 1); - return 3; + lua_pushliteral(L, "Timeout"); + if (check_sigterm) { + lua_pushboolean(L, 0); + return 3; + } else { + return 2; + } } } - if (status == -1) { - if (errno == EINTR) { - lua_pushboolean(L, 1); - return 1; - } else { - moonbr_io_prepare_errmsg(); - return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg); - } - } else if (status == 0) { - lua_pushboolean(L, 0); - lua_pushliteral(L, "Timeout"); - if (check_sigterm) { - lua_pushboolean(L, 0); - return 3; - } else { - return 2; - } - } else { - lua_pushboolean(L, 1); - return 1; - } + lua_pushboolean(L, 1); + return 1; } static int moonbr_io_timeref(lua_State *L) {