moonbridge
changeset 295:1eef86d3b925
Make moonbridge_io.poll(...) check for SIGTERM also when wakeup is forced due to TLS negotiation or closed local socket
author | jbe |
---|---|
date | Sat Jun 17 20:07:03 2017 +0200 (2017-06-17) |
parents | c78bb327ef6a |
children | ce109800ae2d |
files | moonbridge_io.c |
line diff
1.1 --- a/moonbridge_io.c Mon Jun 12 15:38:27 2017 +0200 1.2 +++ b/moonbridge_io.c Sat Jun 17 20:07:03 2017 +0200 1.3 @@ -1629,13 +1629,13 @@ 1.4 1.5 #define moonbr_io_poll_tls() \ 1.6 if (!handle->tlshandshake) { \ 1.7 - lua_pushboolean(L, 1); \ 1.8 - return 1; \ 1.9 + force_wakeup = 1; \ 1.10 + continue; \ 1.11 } \ 1.12 if (handle->tlshandshake == TLS_WANT_POLLIN) { \ 1.13 if (fd < 0) { \ 1.14 - lua_pushboolean(L, 1); \ 1.15 - return 1; \ 1.16 + force_wakeup = 1; \ 1.17 + continue; \ 1.18 } \ 1.19 FD_SET(fd, &readfds); \ 1.20 if (fd+1 > nfds) nfds = fd+1; \ 1.21 @@ -1643,8 +1643,8 @@ 1.22 } \ 1.23 if (handle->tlshandshake == TLS_WANT_POLLOUT) { \ 1.24 if (fd < 0) { \ 1.25 - lua_pushboolean(L, 1); \ 1.26 - return 1; \ 1.27 + force_wakeup = 1; \ 1.28 + continue; \ 1.29 } \ 1.30 FD_SET(fd, &writefds); \ 1.31 if (fd+1 > nfds) nfds = fd+1; \ 1.32 @@ -1661,6 +1661,7 @@ 1.33 int nfds = 0; 1.34 fd_set readfds, writefds, exceptfds; 1.35 struct timespec timeout = {0, }; 1.36 + int force_wakeup = 0; 1.37 int use_timeout = 0; // negative for negative timeout 1.38 int check_sigterm = 0; 1.39 sigset_t mask, orig_mask; 1.40 @@ -1683,8 +1684,8 @@ 1.41 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */ 1.42 handle->readbufin != handle->readbufout /* data pending in buffer */ 1.43 ) { 1.44 - lua_pushboolean(L, 1); 1.45 - return 1; 1.46 + force_wakeup = 1; 1.47 + continue; 1.48 } 1.49 } else { 1.50 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY); 1.51 @@ -1739,14 +1740,19 @@ 1.52 luaL_argcheck(L, 0, 3, "not a valid timeout"); 1.53 } 1.54 } 1.55 + if (use_timeout < 0) force_wakeup = 1; 1.56 if (!lua_isnoneornil(L, 4)) luaL_checktype(L, 4, LUA_TBOOLEAN); 1.57 check_sigterm = lua_toboolean(L, 4); 1.58 if (check_sigterm) { 1.59 - sigemptyset(&mask); 1.60 - sigaddset(&mask, SIGTERM); 1.61 - if (use_timeout >= 0) sigprocmask(SIG_BLOCK, &mask, &orig_mask); 1.62 + if (!force_wakeup) { 1.63 + sigemptyset(&mask); 1.64 + sigaddset(&mask, SIGTERM); 1.65 + if (sigprocmask(SIG_BLOCK, &mask, &orig_mask)) abort(); 1.66 + } 1.67 if (moonbr_io_sigterm_flag) { 1.68 - if (use_timeout >= 0) if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); 1.69 + if (!force_wakeup) { 1.70 + if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); 1.71 + } 1.72 lua_pushboolean(L, 0); 1.73 lua_pushliteral(L, "SIGTERM received"); 1.74 lua_pushboolean(L, 1); 1.75 @@ -1763,41 +1769,42 @@ 1.76 return 2; 1.77 } 1.78 } 1.79 - status = pselect( 1.80 - nfds, &readfds, &writefds, &exceptfds, 1.81 - use_timeout ? &timeout : NULL, 1.82 - check_sigterm ? &orig_mask : NULL 1.83 - ); 1.84 - if (check_sigterm) { 1.85 - if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); 1.86 - if (moonbr_io_sigterm_flag) { 1.87 + if (!force_wakeup) { 1.88 + status = pselect( 1.89 + nfds, &readfds, &writefds, &exceptfds, 1.90 + use_timeout ? &timeout : NULL, 1.91 + check_sigterm ? &orig_mask : NULL 1.92 + ); 1.93 + if (check_sigterm) { 1.94 + if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); 1.95 + if (moonbr_io_sigterm_flag) { 1.96 + lua_pushboolean(L, 0); 1.97 + lua_pushliteral(L, "SIGTERM received"); 1.98 + lua_pushboolean(L, 1); 1.99 + return 3; 1.100 + } 1.101 + } 1.102 + if (status == -1) { 1.103 + if (errno == EINTR) { 1.104 + lua_pushboolean(L, 1); 1.105 + return 1; 1.106 + } else { 1.107 + moonbr_io_prepare_errmsg(); 1.108 + return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg); 1.109 + } 1.110 + } else if (status == 0) { 1.111 lua_pushboolean(L, 0); 1.112 - lua_pushliteral(L, "SIGTERM received"); 1.113 - lua_pushboolean(L, 1); 1.114 - return 3; 1.115 + lua_pushliteral(L, "Timeout"); 1.116 + if (check_sigterm) { 1.117 + lua_pushboolean(L, 0); 1.118 + return 3; 1.119 + } else { 1.120 + return 2; 1.121 + } 1.122 } 1.123 } 1.124 - if (status == -1) { 1.125 - if (errno == EINTR) { 1.126 - lua_pushboolean(L, 1); 1.127 - return 1; 1.128 - } else { 1.129 - moonbr_io_prepare_errmsg(); 1.130 - return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg); 1.131 - } 1.132 - } else if (status == 0) { 1.133 - lua_pushboolean(L, 0); 1.134 - lua_pushliteral(L, "Timeout"); 1.135 - if (check_sigterm) { 1.136 - lua_pushboolean(L, 0); 1.137 - return 3; 1.138 - } else { 1.139 - return 2; 1.140 - } 1.141 - } else { 1.142 - lua_pushboolean(L, 1); 1.143 - return 1; 1.144 - } 1.145 + lua_pushboolean(L, 1); 1.146 + return 1; 1.147 } 1.148 1.149 static int moonbr_io_timeref(lua_State *L) {