moonbridge

changeset 257:77a7ca00ad34

Simplified error handling with new macro(s)
author jbe
date Sun Sep 18 02:19:21 2016 +0200 (2016-09-18)
parents ff7eea59be93
children 56c12449bba0
files moonbridge_io.c
line diff
     1.1 --- a/moonbridge_io.c	Sun Sep 18 00:59:31 2016 +0200
     1.2 +++ b/moonbridge_io.c	Sun Sep 18 02:19:21 2016 +0200
     1.3 @@ -42,9 +42,18 @@
     1.4  #define MOONBR_IO_LISTEN_BACKLOG 1024
     1.5  
     1.6  #define MOONBR_IO_STRERROR_R_MSG "Error detail unavailable due to noncompliant strerror_r() implementation"
     1.7 -#define moonbr_io_errmsg() \
     1.8 +#define moonbr_io_prepare_errmsg() \
     1.9    char errmsg[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG; \
    1.10    strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
    1.11 +#define moonbr_io_return_prepared_errmsg() \
    1.12 +  lua_pushnil(L); \
    1.13 +  lua_pushstring(L, errmsg); \
    1.14 +  return 2
    1.15 +#define moonbr_io_return_errmsg() \
    1.16 +  do { \
    1.17 +    moonbr_io_prepare_errmsg(); \
    1.18 +    moonbr_io_return_prepared_errmsg(); \
    1.19 +  } while (0)
    1.20  
    1.21  #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
    1.22  #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
    1.23 @@ -202,12 +211,7 @@
    1.24      return 2;
    1.25    }
    1.26    handle->readerr = 1;
    1.27 -  if (moonbr_io_handle_set_nonblocking(L, handle, nonblocking)) {
    1.28 -    moonbr_io_errmsg();
    1.29 -    lua_pushnil(L);
    1.30 -    lua_pushstring(L, errmsg);
    1.31 -    return 2;
    1.32 -  }
    1.33 +  if (moonbr_io_handle_set_nonblocking(L, handle, nonblocking)) moonbr_io_return_errmsg();
    1.34    if (!drain) luaL_buffinit(L, &luabuf);
    1.35    while (1) {
    1.36      remaining = -1;
    1.37 @@ -311,12 +315,7 @@
    1.38        handle->readerr = 0;
    1.39        return 2;
    1.40      }
    1.41 -    if (bytesread < 0) {
    1.42 -      moonbr_io_errmsg();
    1.43 -      lua_pushnil(L);
    1.44 -      lua_pushstring(L, errmsg);
    1.45 -      return 2;
    1.46 -    }
    1.47 +    if (bytesread < 0) moonbr_io_return_errmsg();
    1.48      handle->readbufin = bytesread;
    1.49    }
    1.50  }
    1.51 @@ -508,12 +507,7 @@
    1.52      return 2;
    1.53    }
    1.54    handle->writeerr = 1;
    1.55 -  if (moonbr_io_handle_set_nonblocking(L, handle, nonblocking)) {
    1.56 -    moonbr_io_errmsg();
    1.57 -    lua_pushnil(L);
    1.58 -    lua_pushstring(L, errmsg);
    1.59 -    return 2;
    1.60 -  }
    1.61 +  if (moonbr_io_handle_set_nonblocking(L, handle, nonblocking)) moonbr_io_return_errmsg();
    1.62    top = lua_gettop(L);
    1.63    lua_getuservalue(L, 1);
    1.64    lua_getfield(L, -1, "writequeue");
    1.65 @@ -548,12 +542,7 @@
    1.66          handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
    1.67          handle->writebufin = MOONBR_IO_WRITEBUFLEN;
    1.68          while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
    1.69 -          if (moonbr_io_handle_set_nopush(L, handle, 1)) {
    1.70 -            moonbr_io_errmsg();
    1.71 -            lua_pushnil(L);
    1.72 -            lua_pushstring(L, errmsg);
    1.73 -            return 2;
    1.74 -          }
    1.75 +          if (moonbr_io_handle_set_nopush(L, handle, 1)) moonbr_io_return_errmsg();
    1.76  #ifdef MOONBR_IO_USE_TLS
    1.77            moonbr_io_write_tls(
    1.78              handle->writebuf + handle->writebufout,
    1.79 @@ -568,24 +557,14 @@
    1.80            if (written < 0) {
    1.81              if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
    1.82                goto moonbr_io_write_impl_block;
    1.83 -            } else if (errno != EINTR) {
    1.84 -              moonbr_io_errmsg();
    1.85 -              lua_pushnil(L);
    1.86 -              lua_pushstring(L, errmsg);
    1.87 -              return 2;
    1.88 -            }
    1.89 +            } else if (errno != EINTR) moonbr_io_return_errmsg();
    1.90            } else {
    1.91              handle->writebufout += written;
    1.92              handle->writeleft -= written;
    1.93              if (handle->flushedleft) {
    1.94                if (written >= handle->flushedleft) {
    1.95                  handle->flushedleft = 0;
    1.96 -                if (moonbr_io_handle_set_nopush(L, handle, 0)) {
    1.97 -                  moonbr_io_errmsg();
    1.98 -                  lua_pushnil(L);
    1.99 -                  lua_pushstring(L, errmsg);
   1.100 -                  return 2;
   1.101 -                }
   1.102 +                if (moonbr_io_handle_set_nopush(L, handle, 0)) moonbr_io_return_errmsg();
   1.103                } else {
   1.104                  handle->flushedleft -= written;
   1.105                }
   1.106 @@ -602,12 +581,7 @@
   1.107      lua_rawseti(L, -2, handle->writeqout++);
   1.108    }
   1.109    while (handle->flushedleft) {
   1.110 -    if (moonbr_io_handle_set_nopush(L, handle, 1)) {
   1.111 -      moonbr_io_errmsg();
   1.112 -      lua_pushnil(L);
   1.113 -      lua_pushstring(L, errmsg);
   1.114 -      return 2;
   1.115 -    }
   1.116 +    if (moonbr_io_handle_set_nopush(L, handle, 1)) moonbr_io_return_errmsg();
   1.117  #ifdef MOONBR_IO_USE_TLS
   1.118      moonbr_io_write_tls(
   1.119        handle->writebuf + handle->writebufout,
   1.120 @@ -622,24 +596,14 @@
   1.121      if (written < 0) {
   1.122        if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
   1.123          goto moonbr_io_write_impl_block;
   1.124 -      } else if (errno != EINTR) {
   1.125 -        moonbr_io_errmsg();
   1.126 -        lua_pushnil(L);
   1.127 -        lua_pushstring(L, errmsg);
   1.128 -        return 2;
   1.129 -      }
   1.130 +      } else if (errno != EINTR) moonbr_io_return_errmsg();
   1.131      } else {
   1.132        handle->writebufout += written;
   1.133        handle->writeleft -= written;
   1.134        if (handle->flushedleft) {
   1.135          if (written >= handle->flushedleft) {
   1.136            handle->flushedleft = 0;
   1.137 -          if (moonbr_io_handle_set_nopush(L, handle, 0)) {
   1.138 -            moonbr_io_errmsg();
   1.139 -            lua_pushnil(L);
   1.140 -            lua_pushstring(L, errmsg);
   1.141 -            return 2;
   1.142 -          }
   1.143 +          if (moonbr_io_handle_set_nopush(L, handle, 0)) moonbr_io_return_errmsg();
   1.144          } else {
   1.145            handle->flushedleft -= written;
   1.146          }
   1.147 @@ -755,22 +719,14 @@
   1.148    }
   1.149    handle->finished = 1;
   1.150    if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
   1.151 -    if (shutdown(handle->fd, SHUT_WR)) {
   1.152 -      moonbr_io_errmsg();
   1.153 -      lua_pushnil(L);
   1.154 -      lua_pushstring(L, errmsg);
   1.155 -      return 2;
   1.156 -    }
   1.157 +    if (shutdown(handle->fd, SHUT_WR)) moonbr_io_return_errmsg();
   1.158    } else {
   1.159  #ifdef MOONBR_IO_USE_TLS
   1.160      if (handle->tls) tls_close(handle->tls);
   1.161  #endif
   1.162      if (close(handle->fd)) {
   1.163 -      moonbr_io_errmsg();
   1.164        handle->fd = -1;
   1.165 -      lua_pushnil(L);
   1.166 -      lua_pushstring(L, errmsg);
   1.167 -      return 2;
   1.168 +      moonbr_io_return_errmsg();
   1.169      }
   1.170      handle->fd = -1;  /* fake EOF on read */
   1.171    }
   1.172 @@ -802,12 +758,10 @@
   1.173        handle->closed = 1;
   1.174      }
   1.175      if (moonbr_io_handle_set_linger(L, handle, -1)) {
   1.176 -      moonbr_io_errmsg();
   1.177 +      moonbr_io_prepare_errmsg();
   1.178        close(handle->fd);
   1.179        handle->fd = -1;
   1.180 -      lua_pushnil(L);
   1.181 -      lua_pushstring(L, errmsg);
   1.182 -      return 2;
   1.183 +      moonbr_io_return_prepared_errmsg();
   1.184      }
   1.185    } else {
   1.186      handle->closed = 1;
   1.187 @@ -817,11 +771,8 @@
   1.188      if (handle->tls) tls_close(handle->tls);
   1.189  #endif
   1.190      if (close(handle->fd)) {
   1.191 -      moonbr_io_errmsg();
   1.192        handle->fd = -1;
   1.193 -      lua_pushnil(L);
   1.194 -      lua_pushstring(L, errmsg);
   1.195 -      return 2;
   1.196 +      moonbr_io_return_errmsg();
   1.197      }
   1.198      handle->fd = -1;
   1.199    }
   1.200 @@ -881,7 +832,7 @@
   1.201    addrlen = sizeof(addr);
   1.202    if (getsockname(*fd, &addr, &addrlen)) {
   1.203      if (errno != ENOTSOCK) {
   1.204 -      moonbr_io_errmsg();
   1.205 +      moonbr_io_prepare_errmsg();
   1.206        luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
   1.207      }
   1.208      handle->issock = 0;
   1.209 @@ -911,7 +862,7 @@
   1.210  #endif
   1.211    handle->fd = *fd;  /* required for set_linger call */
   1.212    if (moonbr_io_handle_set_linger(L, handle, 0)) {
   1.213 -    moonbr_io_errmsg();
   1.214 +    moonbr_io_prepare_errmsg();
   1.215      handle->fd = -1;
   1.216      luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
   1.217    }
   1.218 @@ -927,7 +878,7 @@
   1.219      const char *addrstr;
   1.220      addrlen = sizeof(addr_in6);
   1.221      if (getsockname(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
   1.222 -      moonbr_io_errmsg();
   1.223 +      moonbr_io_prepare_errmsg();
   1.224        luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
   1.225      }
   1.226      if (addrlen > sizeof(addr_in6)) {
   1.227 @@ -935,7 +886,7 @@
   1.228      }
   1.229      addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
   1.230      if (!addrstr) {
   1.231 -      moonbr_io_errmsg();
   1.232 +      moonbr_io_prepare_errmsg();
   1.233        luaL_error(L, "Could not format local IP address: %s", errmsg);
   1.234      } else {
   1.235        lua_pushstring(L, addrstr);
   1.236 @@ -944,7 +895,7 @@
   1.237      lua_pushinteger(L, ntohs(addr_in6.sin6_port));
   1.238      lua_setfield(L, -2, "local_tcpport");
   1.239      if (getpeername(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
   1.240 -      moonbr_io_errmsg();
   1.241 +      moonbr_io_prepare_errmsg();
   1.242        luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
   1.243      }
   1.244      if (addrlen > sizeof(addr_in6)) {
   1.245 @@ -952,7 +903,7 @@
   1.246      }
   1.247      addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
   1.248      if (!addrstr) {
   1.249 -      moonbr_io_errmsg();
   1.250 +      moonbr_io_prepare_errmsg();
   1.251        luaL_error(L, "Could not format remote IP address: %s", errmsg);
   1.252      } else {
   1.253        lua_pushstring(L, addrstr);
   1.254 @@ -966,7 +917,7 @@
   1.255      const char *addrstr;
   1.256      addrlen = sizeof(addr_in);
   1.257      if (getsockname(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
   1.258 -      moonbr_io_errmsg();
   1.259 +      moonbr_io_prepare_errmsg();
   1.260        luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
   1.261      }
   1.262      if (addrlen > sizeof(addr_in)) {
   1.263 @@ -974,7 +925,7 @@
   1.264      }
   1.265      addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
   1.266      if (!addrstr) {
   1.267 -      moonbr_io_errmsg();
   1.268 +      moonbr_io_prepare_errmsg();
   1.269        luaL_error(L, "Could not format local IP address: %s", errmsg);
   1.270      } else {
   1.271        lua_pushstring(L, addrstr);
   1.272 @@ -983,7 +934,7 @@
   1.273      lua_pushinteger(L, ntohs(addr_in.sin_port));
   1.274      lua_setfield(L, -2, "local_tcpport");
   1.275      if (getpeername(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
   1.276 -      moonbr_io_errmsg();
   1.277 +      moonbr_io_prepare_errmsg();
   1.278        luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
   1.279      }
   1.280      if (addrlen > sizeof(addr_in)) {
   1.281 @@ -991,7 +942,7 @@
   1.282      }
   1.283      addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
   1.284      if (!addrstr) {
   1.285 -      moonbr_io_errmsg();
   1.286 +      moonbr_io_prepare_errmsg();
   1.287        luaL_error(L, "Could not format remote IP address: %s", errmsg);
   1.288      } else {
   1.289        lua_pushstring(L, addrstr);
   1.290 @@ -1054,25 +1005,13 @@
   1.291      SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
   1.292      0
   1.293    );
   1.294 -  if (sock < 0) {
   1.295 -    moonbr_io_errmsg();
   1.296 -    lua_pushnil(L);
   1.297 -    lua_pushstring(L, errmsg);
   1.298 -    return 2;
   1.299 -  }
   1.300 +  if (sock < 0) moonbr_io_return_errmsg();
   1.301    if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
   1.302      if (!nonblocking && errno == EINTR) {
   1.303 -      moonbr_io_errmsg();
   1.304 +      moonbr_io_prepare_errmsg();
   1.305        close(sock);
   1.306 -      lua_pushnil(L);
   1.307 -      lua_pushstring(L, errmsg);
   1.308 -      return 2;
   1.309 -    } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
   1.310 -      moonbr_io_errmsg();
   1.311 -      lua_pushnil(L);
   1.312 -      lua_pushstring(L, errmsg);
   1.313 -      return 2;
   1.314 -    }
   1.315 +      moonbr_io_return_prepared_errmsg();
   1.316 +    } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) moonbr_io_return_errmsg();
   1.317    }
   1.318    moonbr_io_pushhandle(L, sock);
   1.319    return 1;
   1.320 @@ -1102,7 +1041,7 @@
   1.321    if (errcode) {
   1.322      freeaddrinfo(res);
   1.323      if (errcode == EAI_SYSTEM) {
   1.324 -      moonbr_io_errmsg();
   1.325 +      moonbr_io_prepare_errmsg();
   1.326        lua_pushnil(L);
   1.327        lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
   1.328      } else {
   1.329 @@ -1125,26 +1064,17 @@
   1.330      addrinfo->ai_protocol
   1.331    );
   1.332    if (sock < 0) {
   1.333 -    moonbr_io_errmsg();
   1.334 +    moonbr_io_prepare_errmsg();
   1.335      freeaddrinfo(res);
   1.336 -    lua_pushnil(L);
   1.337 -    lua_pushstring(L, errmsg);
   1.338 -    return 2;
   1.339 +    moonbr_io_return_prepared_errmsg();
   1.340    }
   1.341    if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
   1.342      freeaddrinfo(res);
   1.343      if (!nonblocking && errno == EINTR) {
   1.344 -      moonbr_io_errmsg();
   1.345 +      moonbr_io_prepare_errmsg();
   1.346        close(sock);
   1.347 -      lua_pushnil(L);
   1.348 -      lua_pushstring(L, errmsg);
   1.349 -      return 2;
   1.350 -    } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
   1.351 -      moonbr_io_errmsg();
   1.352 -      lua_pushnil(L);
   1.353 -      lua_pushstring(L, errmsg);
   1.354 -      return 2;
   1.355 -    }
   1.356 +      moonbr_io_return_prepared_errmsg();
   1.357 +    } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) moonbr_io_return_errmsg();
   1.358    } else {
   1.359      freeaddrinfo(res);
   1.360    }
   1.361 @@ -1183,25 +1113,16 @@
   1.362      SOCK_STREAM | SOCK_CLOEXEC,
   1.363      0
   1.364    );
   1.365 -  if (sock < 0) {
   1.366 -    moonbr_io_errmsg();
   1.367 -    lua_pushnil(L);
   1.368 -    lua_pushstring(L, errmsg);
   1.369 -    return 2;
   1.370 -  }
   1.371 +  if (sock < 0) moonbr_io_return_errmsg();
   1.372    if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
   1.373 -    moonbr_io_errmsg();
   1.374 +    moonbr_io_prepare_errmsg();
   1.375      close(sock);
   1.376 -    lua_pushnil(L);
   1.377 -    lua_pushstring(L, errmsg);
   1.378 -    return 2;
   1.379 +    moonbr_io_return_prepared_errmsg();
   1.380    }
   1.381    if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
   1.382 -    moonbr_io_errmsg();
   1.383 +    moonbr_io_prepare_errmsg();
   1.384      close(sock);
   1.385 -    lua_pushnil(L);
   1.386 -    lua_pushstring(L, errmsg);
   1.387 -    return 2;
   1.388 +    moonbr_io_return_prepared_errmsg();
   1.389    }
   1.390    listener->fd = sock;
   1.391    listener->addrfam = AF_LOCAL;
   1.392 @@ -1229,7 +1150,7 @@
   1.393    if (errcode) {
   1.394      freeaddrinfo(res);
   1.395      if (errcode == EAI_SYSTEM) {
   1.396 -      moonbr_io_errmsg();
   1.397 +      moonbr_io_prepare_errmsg();
   1.398        lua_pushnil(L);
   1.399        lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
   1.400      } else {
   1.401 @@ -1253,16 +1174,14 @@
   1.402      addrinfo->ai_protocol
   1.403    );
   1.404    if (sock < 0) {
   1.405 -    moonbr_io_errmsg();
   1.406 +    moonbr_io_prepare_errmsg();
   1.407      freeaddrinfo(res);
   1.408 -    lua_pushnil(L);
   1.409 -    lua_pushstring(L, errmsg);
   1.410 -    return 2;
   1.411 +    moonbr_io_return_prepared_errmsg();
   1.412    }
   1.413    {
   1.414      static const int reuseval = 1;
   1.415      if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
   1.416 -      moonbr_io_errmsg();
   1.417 +      moonbr_io_prepare_errmsg();
   1.418        freeaddrinfo(res);
   1.419        close(sock);
   1.420        lua_pushnil(L);
   1.421 @@ -1271,20 +1190,16 @@
   1.422      }
   1.423    }
   1.424    if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
   1.425 -    moonbr_io_errmsg();
   1.426 +    moonbr_io_prepare_errmsg();
   1.427      freeaddrinfo(res);
   1.428      close(sock);
   1.429 -    lua_pushnil(L);
   1.430 -    lua_pushstring(L, errmsg);
   1.431 -    return 2;
   1.432 +    moonbr_io_return_prepared_errmsg();
   1.433    }
   1.434    freeaddrinfo(res);
   1.435    if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
   1.436 -    moonbr_io_errmsg();
   1.437 +    moonbr_io_prepare_errmsg();
   1.438      close(sock);
   1.439 -    lua_pushnil(L);
   1.440 -    lua_pushstring(L, errmsg);
   1.441 -    return 2;
   1.442 +    moonbr_io_return_prepared_errmsg();
   1.443    }
   1.444    listener->fd = sock;
   1.445    listener->nonblocking = -1;
   1.446 @@ -1300,7 +1215,7 @@
   1.447      int flags;
   1.448      flags = fcntl(listener->fd, F_GETFL, 0);
   1.449      if (flags == -1) {
   1.450 -      moonbr_io_errmsg();
   1.451 +      moonbr_io_prepare_errmsg();
   1.452        close(listener->fd);
   1.453        listener->fd = -1;
   1.454        luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
   1.455 @@ -1308,7 +1223,7 @@
   1.456      if (nonblocking) flags |= O_NONBLOCK;
   1.457      else flags &= ~O_NONBLOCK;
   1.458      if (fcntl(listener->fd, F_SETFL, flags) == -1) {
   1.459 -      moonbr_io_errmsg();
   1.460 +      moonbr_io_prepare_errmsg();
   1.461        close(listener->fd);
   1.462        listener->fd = -1;
   1.463        luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
   1.464 @@ -1320,7 +1235,7 @@
   1.465      fd = accept(listener->fd, NULL, NULL);
   1.466      if (fd != -1) {
   1.467        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
   1.468 -        moonbr_io_errmsg();
   1.469 +        moonbr_io_prepare_errmsg();
   1.470          close(listener->fd);
   1.471          listener->fd = -1;
   1.472          close(fd);
   1.473 @@ -1335,12 +1250,7 @@
   1.474          lua_pushboolean(L, 0);
   1.475          lua_pushliteral(L, "No incoming connection pending");
   1.476          return 2;
   1.477 -      } else if (errno != EINTR) {
   1.478 -        moonbr_io_errmsg();
   1.479 -        lua_pushnil(L);
   1.480 -        lua_pushstring(L, errmsg);
   1.481 -        return 2;
   1.482 -      }
   1.483 +      } else if (errno != EINTR) moonbr_io_return_errmsg();
   1.484      } else {
   1.485        moonbr_io_pushhandle(L, fd);
   1.486        return 1;
   1.487 @@ -1366,16 +1276,14 @@
   1.488    addrlen = sizeof(addr);
   1.489    if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
   1.490    if (close(listener->fd)) {
   1.491 -    moonbr_io_errmsg();
   1.492 +    moonbr_io_prepare_errmsg();
   1.493      listener->fd = -1;
   1.494      if (addrlen && addrlen <= sizeof(addr)) {
   1.495        if (stat(addr.sun_path, &sb) == 0) {
   1.496          if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
   1.497        }
   1.498      }
   1.499 -    lua_pushnil(L);
   1.500 -    lua_pushstring(L, errmsg);
   1.501 -    return 2;
   1.502 +    moonbr_io_return_prepared_errmsg();
   1.503    }
   1.504    listener->fd = -1;
   1.505    if (addrlen && addrlen <= sizeof(addr)) {
   1.506 @@ -1412,13 +1320,13 @@
   1.507    lua_setuservalue(L, -2);
   1.508    luaL_setmetatable(L, MOONBR_IO_CHILD_MT_REGKEY);
   1.509    if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockin)) {
   1.510 -    moonbr_io_errmsg();
   1.511 +    moonbr_io_prepare_errmsg();
   1.512      lua_pushnil(L);
   1.513      lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
   1.514      return 2;
   1.515    }
   1.516    if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockout)) {
   1.517 -    moonbr_io_errmsg();
   1.518 +    moonbr_io_prepare_errmsg();
   1.519      close(sockin[0]);
   1.520      close(sockin[1]);
   1.521      lua_pushnil(L);
   1.522 @@ -1426,7 +1334,7 @@
   1.523      return 2;
   1.524    }
   1.525    if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockerr)) {
   1.526 -    moonbr_io_errmsg();
   1.527 +    moonbr_io_prepare_errmsg();
   1.528      close(sockin[0]);
   1.529      close(sockin[1]);
   1.530      close(sockout[0]);
   1.531 @@ -1437,7 +1345,7 @@
   1.532    }
   1.533    child->pid = vfork();
   1.534    if (child->pid == -1) {
   1.535 -    moonbr_io_errmsg();
   1.536 +    moonbr_io_prepare_errmsg();
   1.537      close(sockin[0]);
   1.538      close(sockin[1]);
   1.539      close(sockout[0]);
   1.540 @@ -1476,7 +1384,7 @@
   1.541      close(sockerr[0]);
   1.542      while (waitpid(child->pid, &status, 0) == -1) {
   1.543        if (errno != EINTR) {
   1.544 -        moonbr_io_errmsg();
   1.545 +        moonbr_io_prepare_errmsg();
   1.546          luaL_error(L, "Error in waitpid call after unsuccessful exec: %s", errmsg);
   1.547        }
   1.548      }
   1.549 @@ -1516,7 +1424,7 @@
   1.550      int status;
   1.551      while (waitpid(child->pid, &status, 0) == -1) {
   1.552        if (errno != EINTR) {
   1.553 -        moonbr_io_errmsg();
   1.554 +        moonbr_io_prepare_errmsg();
   1.555          luaL_error(L, "Error in waitpid call after error creating socket handles: %s", errmsg);
   1.556        }
   1.557      }
   1.558 @@ -1556,12 +1464,12 @@
   1.559    if (child->pid) {
   1.560      int status;
   1.561      if (kill(child->pid, SIGKILL)) {
   1.562 -      moonbr_io_errmsg();
   1.563 +      moonbr_io_prepare_errmsg();
   1.564        luaL_error(L, "Error in kill call during garbage collection: %s", errmsg);
   1.565      }
   1.566      while (waitpid(child->pid, &status, 0) == -1) {
   1.567        if (errno != EINTR) {
   1.568 -        moonbr_io_errmsg();
   1.569 +        moonbr_io_prepare_errmsg();
   1.570          luaL_error(L, "Error in waitpid call during garbage collection: %s", errmsg);
   1.571        }
   1.572      }
   1.573 @@ -1576,7 +1484,7 @@
   1.574    sig = luaL_optinteger(L, 2, SIGTERM);
   1.575    if (!child->pid) luaL_error(L, "Attempt to kill an already collected child process");
   1.576    if (kill(child->pid, sig)) {
   1.577 -    moonbr_io_errmsg();
   1.578 +    moonbr_io_prepare_errmsg();
   1.579      luaL_error(L, "Error in kill call: %s", errmsg);
   1.580    }
   1.581    lua_settop(L, 1);
   1.582 @@ -1591,7 +1499,7 @@
   1.583    if (!child->pid) luaL_error(L, "Attempt to wait for an already collected child process");
   1.584    while ((waitedpid = waitpid(child->pid, &status, nonblocking ? WNOHANG : 0)) == -1) {
   1.585      if (errno != EINTR) {
   1.586 -      moonbr_io_errmsg();
   1.587 +      moonbr_io_prepare_errmsg();
   1.588        luaL_error(L, "Error in waitpid call: %s", errmsg);
   1.589      }
   1.590    }
   1.591 @@ -1770,7 +1678,7 @@
   1.592        lua_pushliteral(L, "Signal received while polling file descriptors");
   1.593        return 2;
   1.594      } else {
   1.595 -      moonbr_io_errmsg();
   1.596 +      moonbr_io_prepare_errmsg();
   1.597        return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
   1.598      }
   1.599    } else if (status == 0) {

Impressum / About Us