moonbridge

diff moonbridge_io.c @ 288:524bb61496b5

Changed behavior of moonbridge_io.poll(...); Renamed moonbridge_io.sigterm_setup() to moonbridge_io.catch_sigterm(); Removed moonbridge_io.sigterm_received()
author jbe
date Sun Jun 11 03:50:28 2017 +0200 (2017-06-11)
parents a7395fb91ec3
children 70f047d32a0f
line diff
     1.1 --- a/moonbridge_io.c	Sun Jun 11 01:27:52 2017 +0200
     1.2 +++ b/moonbridge_io.c	Sun Jun 11 03:50:28 2017 +0200
     1.3 @@ -1615,16 +1615,11 @@
     1.4    moonbr_io_sigterm_flag = 1;
     1.5  }
     1.6  
     1.7 -int moonbr_io_sigterm_setup(lua_State *L) {
     1.8 +int moonbr_io_catch_sigterm(lua_State *L) {
     1.9    signal(SIGTERM, moonbr_io_sigterm_handler);
    1.10    return 0;
    1.11  }
    1.12  
    1.13 -static int moonbr_io_sigterm_received(lua_State *L) {
    1.14 -  lua_pushboolean(L, moonbr_io_sigterm_flag);
    1.15 -  return 1;
    1.16 -}
    1.17 -
    1.18  static int moonbr_io_getpid(lua_State *L) {
    1.19    lua_pushinteger(L, getpid());
    1.20    return 1;
    1.21 @@ -1666,7 +1661,7 @@
    1.22    int nfds = 0;
    1.23    fd_set readfds, writefds, exceptfds;
    1.24    struct timespec timeout = {0, };
    1.25 -  int use_timeout = 0;
    1.26 +  int use_timeout = 0;  // negative for negative timeout
    1.27    int check_sigterm = 0;
    1.28    sigset_t mask, orig_mask;
    1.29    int status;
    1.30 @@ -1735,36 +1730,50 @@
    1.31      lua_Number n;
    1.32      n = lua_tonumberx(L, 3, &isnum);
    1.33      if (isnum && n<0) {
    1.34 -      lua_pushboolean(L, 0);
    1.35 -      lua_pushliteral(L, "Negative timeout");
    1.36 -      return 2;
    1.37 +      use_timeout = -1;
    1.38      } else if (isnum && n>=0 && n<100000000) {
    1.39 +      use_timeout = 1;
    1.40        timeout.tv_sec = n;
    1.41        timeout.tv_nsec = 1e9 * (n - timeout.tv_sec);
    1.42      } else {
    1.43        luaL_argcheck(L, 0, 3, "not a valid timeout");
    1.44      }
    1.45 -    use_timeout = 1;
    1.46    }
    1.47    if (!lua_isnoneornil(L, 4)) luaL_checktype(L, 4, LUA_TBOOLEAN);
    1.48    check_sigterm = lua_toboolean(L, 4);
    1.49    if (check_sigterm) {
    1.50      sigemptyset(&mask);
    1.51      sigaddset(&mask, SIGTERM);
    1.52 -    sigprocmask(SIG_BLOCK, &mask, &orig_mask);
    1.53 +    if (use_timeout >= 0) sigprocmask(SIG_BLOCK, &mask, &orig_mask);
    1.54      if (moonbr_io_sigterm_flag) {
    1.55 -      if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort();
    1.56 +      if (use_timeout >= 0) if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort();
    1.57        lua_pushboolean(L, 0);
    1.58        lua_pushliteral(L, "SIGTERM received");
    1.59 +      lua_pushboolean(L, 1);
    1.60 +      return 3;
    1.61 +    }
    1.62 +  }
    1.63 +  if (use_timeout < 0) {
    1.64 +    lua_pushboolean(L, 0);
    1.65 +    lua_pushliteral(L, "Negative timeout");
    1.66 +    if (check_sigterm) {
    1.67 +      lua_pushboolean(L, 0);
    1.68 +      return 3;
    1.69 +    } else {
    1.70        return 2;
    1.71      }
    1.72    }
    1.73 -  status = pselect(nfds, &readfds, &writefds, &exceptfds, use_timeout ? &timeout : NULL, check_sigterm ? &orig_mask : NULL);
    1.74 +  status = pselect(
    1.75 +    nfds, &readfds, &writefds, &exceptfds,
    1.76 +    use_timeout ? &timeout : NULL,
    1.77 +    check_sigterm ? &orig_mask : NULL
    1.78 +  );
    1.79    if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort();
    1.80 -  if (moonbr_io_sigterm_flag) {
    1.81 +  if (check_sigterm && moonbr_io_sigterm_flag) {
    1.82      lua_pushboolean(L, 0);
    1.83      lua_pushliteral(L, "SIGTERM received");
    1.84 -    return 2;
    1.85 +    lua_pushboolean(L, 1);
    1.86 +    return 3;
    1.87    }
    1.88    if (status == -1) {
    1.89      if (errno == EINTR) {
    1.90 @@ -1776,8 +1785,13 @@
    1.91      }
    1.92    } else if (status == 0) {
    1.93      lua_pushboolean(L, 0);
    1.94 -    lua_pushliteral(L, "Timeout while polling file descriptors");
    1.95 -    return 2;
    1.96 +    lua_pushliteral(L, "Timeout while waiting for I/O");
    1.97 +    if (check_sigterm) {
    1.98 +      lua_pushboolean(L, 0);
    1.99 +      return 3;
   1.100 +    } else {
   1.101 +      return 2;
   1.102 +    }
   1.103    } else {
   1.104      lua_pushboolean(L, 1);
   1.105      return 1;
   1.106 @@ -2000,8 +2014,7 @@
   1.107    {"locallisten", moonbr_io_locallisten},
   1.108    {"tcplisten", moonbr_io_tcplisten},
   1.109    {"exec", moonbr_io_exec},
   1.110 -  {"sigterm_setup", moonbr_io_sigterm_setup},
   1.111 -  {"sigterm_received", moonbr_io_sigterm_received},
   1.112 +  {"catch_sigterm", moonbr_io_catch_sigterm},
   1.113    {"getpid", moonbr_io_getpid},
   1.114    {"poll", moonbr_io_poll},
   1.115    {"timeref", moonbr_io_timeref},

Impressum / About Us