moonbridge

changeset 251:f3988723732a

Call tls_close always before closing file descriptor
author jbe
date Sat Sep 03 11:41:26 2016 +0200 (2016-09-03)
parents 28bffa2af1ec
children d39f818aff02
files moonbridge_io.c
line diff
     1.1 --- a/moonbridge_io.c	Fri Aug 26 23:15:56 2016 +0200
     1.2 +++ b/moonbridge_io.c	Sat Sep 03 11:41:26 2016 +0200
     1.3 @@ -129,6 +129,9 @@
     1.4    flags = fcntl(handle->fd, F_GETFL, 0);
     1.5    if (flags == -1) {
     1.6      moonbr_io_errmsg();
     1.7 +#ifdef MOONBR_IO_USE_TLS
     1.8 +    if (handle->tls) tls_close(handle->tls);
     1.9 +#endif
    1.10      close(handle->fd);
    1.11      handle->fd = -1;
    1.12      handle->closed = 1;
    1.13 @@ -138,7 +141,10 @@
    1.14    else flags &= ~O_NONBLOCK;
    1.15    if (fcntl(handle->fd, F_SETFL, flags) == -1) {
    1.16      moonbr_io_errmsg();
    1.17 -    close(handle->fd);
    1.18 + #ifdef MOONBR_IO_USE_TLS
    1.19 +    if (handle->tls) tls_close(handle->tls);
    1.20 +#endif
    1.21 +   close(handle->fd);
    1.22      handle->fd = -1;
    1.23      handle->closed = 1;
    1.24      luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
    1.25 @@ -155,6 +161,9 @@
    1.26    }
    1.27    if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
    1.28      moonbr_io_errmsg();
    1.29 +#ifdef MOONBR_IO_USE_TLS
    1.30 +    if (handle->tls) tls_close(handle->tls);
    1.31 +#endif
    1.32      close(handle->fd);
    1.33      handle->fd = -1;
    1.34      handle->closed = 1;
    1.35 @@ -174,6 +183,9 @@
    1.36    if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
    1.37  #endif
    1.38      moonbr_io_errmsg();
    1.39 +#ifdef MOONBR_IO_USE_TLS
    1.40 +    if (handle->tls) tls_close(handle->tls);
    1.41 +#endif
    1.42      close(handle->fd);
    1.43      handle->fd = -1;
    1.44      handle->closed = 1;
    1.45 @@ -752,6 +764,9 @@
    1.46        return 2;
    1.47      }
    1.48    } else {
    1.49 +#ifdef MOONBR_IO_USE_TLS
    1.50 +    if (handle->tls) tls_close(handle->tls);
    1.51 +#endif
    1.52      if (close(handle->fd)) {
    1.53        moonbr_io_errmsg();
    1.54        handle->fd = -1;
    1.55 @@ -775,12 +790,18 @@
    1.56        lua_pushvalue(L, 1);
    1.57        if (lua_pcall(L, 1, 2, 0)) {
    1.58          handle->closed = 1;
    1.59 +#ifdef MOONBR_IO_USE_TLS
    1.60 +        if (handle->tls) tls_close(handle->tls);
    1.61 +#endif
    1.62          close(handle->fd);
    1.63          handle->fd = -1;
    1.64          lua_error(L);
    1.65        }
    1.66        handle->closed = 1;
    1.67        if (!lua_toboolean(L, -2)) {
    1.68 +#ifdef MOONBR_IO_USE_TLS
    1.69 +        if (handle->tls) tls_close(handle->tls);
    1.70 +#endif
    1.71          close(handle->fd);
    1.72          handle->fd = -1;
    1.73          return 2;
    1.74 @@ -793,6 +814,9 @@
    1.75      handle->closed = 1;
    1.76    }
    1.77    if (handle->fd >= 0) {
    1.78 +#ifdef MOONBR_IO_USE_TLS
    1.79 +    if (handle->tls) tls_close(handle->tls);
    1.80 +#endif
    1.81      if (close(handle->fd)) {
    1.82        moonbr_io_errmsg();
    1.83        handle->fd = -1;
    1.84 @@ -985,7 +1009,7 @@
    1.85    lua_pushcfunction(L, moonbr_io_pushhandle_impl);
    1.86    lua_pushlightuserdata(L, &fd);
    1.87    if (lua_pcall(L, 1, 1, 0)) {
    1.88 -    if (fd != -1) close(fd);
    1.89 +    if (fd != -1) close(fd);  // TODO: correct to close file descriptor here?
    1.90      lua_error(L);
    1.91    }
    1.92  }

Impressum / About Us