From 9ed3ed5fcb3f19eccd3d29848ae2ff0cfd577de9 Mon Sep 17 00:00:00 2001 From: Michael Kilburn Date: Mon, 26 Feb 2018 12:55:20 -0600 Subject: [PATCH] unix: fix several instances of lost errno This commit preserves errno properly in the following cases: - new_socket() was previously discarding errno in some cases, and replacing it with the result of getsockname(). - uv__close() was not preserving errno when __MVS__ is defined. Fixes: https://github.com/libuv/libuv/issues/1756 PR-URL: https://github.com/libuv/libuv/pull/1763 Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno Reviewed-By: Colin Ihrig --- src/unix/core.c | 2 +- src/unix/internal.h | 2 +- src/unix/tcp.c | 10 ++++------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/unix/core.c b/src/unix/core.c index 8cf129d3..18c8fcd8 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -536,7 +536,7 @@ int uv__close_nocheckstdio(int fd) { int uv__close(int fd) { assert(fd > STDERR_FILENO); /* Catch stdio close bugs. */ #if defined(__MVS__) - epoll_file_close(fd); + SAVE_ERRNO(epoll_file_close(fd)); #endif return uv__close_nocheckstdio(fd); } diff --git a/src/unix/internal.h b/src/unix/internal.h index 2bb3773c..b6df1ab9 100644 --- a/src/unix/internal.h +++ b/src/unix/internal.h @@ -190,7 +190,7 @@ int uv__cloexec_ioctl(int fd, int set); int uv__cloexec_fcntl(int fd, int set); int uv__nonblock_ioctl(int fd, int set); int uv__nonblock_fcntl(int fd, int set); -int uv__close(int fd); +int uv__close(int fd); /* preserves errno */ int uv__close_nocheckstdio(int fd); int uv__socket(int domain, int type, int protocol); int uv__dup(int fd); diff --git a/src/unix/tcp.c b/src/unix/tcp.c index b991320c..336d8e29 100644 --- a/src/unix/tcp.c +++ b/src/unix/tcp.c @@ -49,16 +49,14 @@ static int new_socket(uv_tcp_t* handle, int domain, unsigned long flags) { /* Bind this new socket to an arbitrary port */ slen = sizeof(saddr); memset(&saddr, 0, sizeof(saddr)); - err = getsockname(uv__stream_fd(handle), (struct sockaddr*) &saddr, &slen); - if (err) { + if (getsockname(uv__stream_fd(handle), (struct sockaddr*) &saddr, &slen)) { uv__close(sockfd); - return err; + return UV__ERR(errno); } - err = bind(uv__stream_fd(handle), (struct sockaddr*) &saddr, slen); - if (err) { + if (bind(uv__stream_fd(handle), (struct sockaddr*) &saddr, slen)) { uv__close(sockfd); - return err; + return UV__ERR(errno); } }