summaryrefslogtreecommitdiff
path: root/ext/socket
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-23 08:57:48 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-23 08:57:48 +0000
commitf4166e2dd7a4d9be95f160e19303ddeeb5d27ab4 (patch)
tree54a609327994753d5579a07797f199fff86c3dc4 /ext/socket
parentd15f30882ae8cd6acd27fed5cbb046b5c7c15549 (diff)
prefer rb_syserr_fail
* file.c, io.c, util.c: prefer rb_syserr_fail with saved errno over setting errno then call rb_sys_fail, not to be clobbered potentially and to reduce thread local errno accesses. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53264 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket')
-rw-r--r--ext/socket/ancdata.c16
-rw-r--r--ext/socket/init.c18
-rw-r--r--ext/socket/socket.c7
-rw-r--r--ext/socket/unixsocket.c6
4 files changed, 28 insertions, 19 deletions
diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c
index c54df04..b19f605 100644
--- a/ext/socket/ancdata.c
+++ b/ext/socket/ancdata.c
@@ -1275,18 +1275,20 @@ bsock_sendmsg_internal(VALUE sock, VALUE data, VALUE vflags,
ss = rb_sendmsg(fptr->fd, &mh, flags);
if (ss == -1) {
+ int e;
if (!nonblock && rb_io_wait_writable(fptr->fd)) {
rb_io_check_closed(fptr);
goto retry;
}
- if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN)) {
+ e = errno;
+ if (nonblock && (e == EWOULDBLOCK || e == EAGAIN)) {
if (ex == Qfalse) {
return sym_wait_writable;
}
rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE,
"sendmsg(2) would block");
}
- rb_sys_fail("sendmsg(2)");
+ rb_syserr_fail(e, "sendmsg(2)");
}
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
RB_GC_GUARD(controls_str);
@@ -1547,18 +1549,20 @@ bsock_recvmsg_internal(VALUE sock,
ss = rb_recvmsg(fptr->fd, &mh, flags);
if (ss == -1) {
+ int e;
if (!nonblock && rb_io_wait_readable(fptr->fd)) {
rb_io_check_closed(fptr);
goto retry;
}
- if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN)) {
+ e = errno;
+ if (nonblock && (e == EWOULDBLOCK || e == EAGAIN)) {
if (ex == Qfalse) {
return sym_wait_readable;
}
rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "recvmsg(2) would block");
}
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
- if (!gc_done && (errno == EMFILE || errno == EMSGSIZE)) {
+ if (!gc_done && (e == EMFILE || e == EMSGSIZE)) {
/*
* When SCM_RIGHTS hit the file descriptors limit:
* - Linux 2.6.18 causes success with MSG_CTRUNC
@@ -1571,11 +1575,11 @@ bsock_recvmsg_internal(VALUE sock,
goto retry;
}
#else
- if (NIL_P(vmaxdatlen) && grow_buffer && errno == EMSGSIZE)
+ if (NIL_P(vmaxdatlen) && grow_buffer && e == EMSGSIZE)
ss = (ssize_t)iov.iov_len;
else
#endif
- rb_sys_fail("recvmsg(2)");
+ rb_syserr_fail(e, "recvmsg(2)");
}
if (grow_buffer) {
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 66da534..797e4a1 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -62,8 +62,7 @@ rsock_init_sock(VALUE sock, int fd)
rb_io_t *fp;
if (!is_socket(fd) || rb_reserved_fd_p(fd)) {
- errno = EBADF;
- rb_sys_fail("not a socket file descriptor");
+ rb_syserr_fail(EBADF, "not a socket file descriptor");
}
rb_update_max_fd(fd);
@@ -246,7 +245,8 @@ rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str,
alen = len0;
if (slen < 0) {
- switch (errno) {
+ int e = errno;
+ switch (e) {
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
@@ -255,7 +255,7 @@ rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str,
return sym_wait_readable;
rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "recvfrom(2) would block");
}
- rb_sys_fail("recvfrom(2)");
+ rb_syserr_fail(e, "recvfrom(2)");
}
if (slen != RSTRING_LEN(str)) {
rb_str_set_len(str, slen);
@@ -558,7 +558,8 @@ rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr,
rb_io_set_nonblock(fptr);
fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len, 1);
if (fd2 < 0) {
- switch (errno) {
+ int e = errno;
+ switch (e) {
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
@@ -571,7 +572,7 @@ rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr,
return sym_wait_readable;
rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "accept(2) would block");
}
- rb_sys_fail("accept(2)");
+ rb_syserr_fail(e, "accept(2)");
}
rb_update_max_fd(fd2);
return rsock_init_sock(rb_obj_alloc(klass), fd2);
@@ -604,7 +605,8 @@ rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
rsock_maybe_wait_fd(fd);
fd2 = (int)BLOCKING_REGION_FD(accept_blocking, &arg);
if (fd2 < 0) {
- switch (errno) {
+ int e = errno;
+ switch (e) {
case EMFILE:
case ENFILE:
case ENOMEM:
@@ -617,7 +619,7 @@ rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
retry = 0;
goto retry;
}
- rb_sys_fail("accept(2)");
+ rb_syserr_fail(e, "accept(2)");
}
rb_update_max_fd(fd2);
if (!klass) return INT2NUM(fd2);
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index 0514bb6..d71a94a 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -452,18 +452,19 @@ sock_connect_nonblock(VALUE sock, VALUE addr, VALUE ex)
rb_io_set_nonblock(fptr);
n = connect(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr));
if (n < 0) {
- if (errno == EINPROGRESS) {
+ int e = errno;
+ if (e == EINPROGRESS) {
if (ex == Qfalse) {
return sym_wait_writable;
}
rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE, "connect(2) would block");
}
- if (errno == EISCONN) {
+ if (e == EISCONN) {
if (ex == Qfalse) {
return INT2FIX(0);
}
}
- rsock_sys_fail_raddrinfo_or_sockaddr("connect(2)", addr, rai);
+ rsock_syserr_fail_raddrinfo_or_sockaddr(e, "connect(2)", addr, rai);
}
return INT2FIX(n);
diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c
index 2442bc1..f73f127 100644
--- a/ext/socket/unixsocket.c
+++ b/ext/socket/unixsocket.c
@@ -65,14 +65,16 @@ rsock_init_unixsock(VALUE sock, VALUE path, int server)
}
if (status < 0) {
+ int e = errno;
close(fd);
- rsock_sys_fail_path("connect(2)", path);
+ rsock_syserr_fail_path(e, "connect(2)", path);
}
if (server) {
if (listen(fd, SOMAXCONN) < 0) {
+ int e = errno;
close(fd);
- rsock_sys_fail_path("listen(2)", path);
+ rsock_syserr_fail_path(e, "listen(2)", path);
}
}