Index: ext/socket/rubysocket.h =================================================================== --- ext/socket/rubysocket.h (revision 36295) +++ ext/socket/rubysocket.h (working copy) @@ -306,4 +306,8 @@ void rsock_init_addrinfo(void); void rsock_init_sockopt(void); void rsock_init_socket_init(void); +void rsock_sys_fail_host_port(const char *, VALUE, VALUE); +void rsock_sys_fail_path(const char *, VALUE); +void rsock_sys_fail_sockaddr(const char *, VALUE, VALUE); + #endif Index: ext/socket/udpsocket.c =================================================================== --- ext/socket/udpsocket.c (revision 36295) +++ ext/socket/udpsocket.c (working copy) @@ -93,7 +93,7 @@ udp_connect(VALUE sock, VALUE host, VALU arg.fd = fptr->fd; ret = rb_ensure(udp_connect_internal, (VALUE)&arg, rsock_freeaddrinfo, (VALUE)arg.res); - if (!ret) rb_sys_fail("connect(2)"); + if (!ret) rsock_sys_fail_host_port("connect(2)", host, port); return INT2FIX(0); } @@ -126,7 +126,9 @@ udp_bind(VALUE sock, VALUE host, VALUE p return INT2FIX(0); } freeaddrinfo(res0); - rb_sys_fail("bind(2)"); + + rsock_sys_fail_host_port("bind(2)", host, port); + return INT2FIX(0); } @@ -187,7 +189,7 @@ udp_send(int argc, VALUE *argv, VALUE so } } freeaddrinfo(res0); - rb_sys_fail("sendto(2)"); + rsock_sys_fail_host_port("sendto(2)", host, port); return INT2FIX(n); } Index: ext/socket/init.c =================================================================== --- ext/socket/init.c (revision 36295) +++ ext/socket/init.c (working copy) @@ -47,7 +47,7 @@ rsock_init_sock(VALUE sock, int fd) struct stat sbuf; if (fstat(fd, &sbuf) < 0) - rb_sys_fail(0); + rb_sys_fail("fstat(2)"); rb_update_max_fd(fd); if (!S_ISSOCK(sbuf.st_mode)) rb_raise(rb_eArgError, "not a socket file descriptor"); @@ -465,14 +465,14 @@ make_fd_nonblock(int fd) #ifdef F_GETFL flags = fcntl(fd, F_GETFL); if (flags == -1) { - rb_sys_fail(0); + rb_sys_fail("fnctl(2)"); } #else flags = 0; #endif flags |= O_NONBLOCK; if (fcntl(fd, F_SETFL, flags) == -1) { - rb_sys_fail(0); + rb_sys_fail("fnctl(2)"); } } @@ -574,7 +574,7 @@ rsock_s_accept(VALUE klass, int fd, stru retry = 0; goto retry; } - rb_sys_fail(0); + rb_sys_fail("accept(2)"); } rb_update_max_fd(fd2); if (!klass) return INT2NUM(fd2); Index: ext/socket/ipsocket.c =================================================================== --- ext/socket/ipsocket.c (revision 36295) +++ ext/socket/ipsocket.c (working copy) @@ -43,7 +43,7 @@ init_inetsock_internal(struct inetsock_a { int type = arg->type; struct addrinfo *res; - int fd, status = 0; + int fd, status = 0, local = 0; const char *syscall = 0; arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv, SOCK_STREAM, @@ -81,6 +81,7 @@ init_inetsock_internal(struct inetsock_a else { if (arg->local.res) { status = bind(fd, arg->local.res->ai_addr, arg->local.res->ai_addrlen); + local = status; syscall = "bind(2)"; } @@ -99,7 +100,17 @@ init_inetsock_internal(struct inetsock_a break; } if (status < 0) { - rb_sys_fail(syscall); + VALUE host, port; + + if (local < 0) { + host = arg->local.host; + port = arg->local.serv; + } else { + host = arg->remote.host; + port = arg->remote.serv; + } + + rsock_sys_fail_host_port(syscall, host, port); } arg->fd = -1; Index: ext/socket/socket.c =================================================================== --- ext/socket/socket.c (revision 36295) +++ ext/socket/socket.c (working copy) @@ -10,6 +10,40 @@ #include "rubysocket.h" +static VALUE sock_s_unpack_sockaddr_in(VALUE, VALUE); + +void +rsock_sys_fail_host_port(const char *mesg, VALUE host, VALUE port) +{ + VALUE message; + + port = rb_String(port); + + message = rb_sprintf("%s for \"%s\" port %s", + mesg, StringValueCStr(host), StringValueCStr(port)); + + rb_sys_fail_str(message); +} + +void +rsock_sys_fail_path(const char *mesg, VALUE path) +{ + VALUE message = rb_sprintf("%s for \"%s\"", + mesg, StringValueCStr(path)); + + rb_sys_fail_str(message); +} + +void +rsock_sys_fail_sockaddr(const char *mesg, VALUE sock, VALUE addr) +{ + VALUE host_port = sock_s_unpack_sockaddr_in(sock, addr); + + rsock_sys_fail_host_port(mesg, + RARRAY_PTR(host_port)[1], + RARRAY_PTR(host_port)[0]); +} + static void setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv) { @@ -306,7 +340,7 @@ sock_connect(VALUE sock, VALUE addr) fd = fptr->fd; n = rsock_connect(fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr), 0); if (n < 0) { - rb_sys_fail("connect(2)"); + rsock_sys_fail_sockaddr("connect(2)", sock, addr); } return INT2FIX(n); @@ -368,7 +402,7 @@ sock_connect_nonblock(VALUE sock, VALUE if (n < 0) { if (errno == EINPROGRESS) rb_mod_sys_fail(rb_mWaitWritable, "connect(2) would block"); - rb_sys_fail("connect(2)"); + rsock_sys_fail_sockaddr("connect(2)", sock, addr); } return INT2FIX(n); @@ -468,7 +502,7 @@ sock_bind(VALUE sock, VALUE addr) SockAddrStringValue(addr); GetOpenFile(sock, fptr); if (bind(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr)) < 0) - rb_sys_fail("bind(2)"); + rsock_sys_fail_sockaddr("bind(2)", sock, addr); return INT2FIX(0); } @@ -903,7 +937,7 @@ sock_gethostname(VALUE obj) rb_secure(3); if (gethostname(buf, (int)sizeof buf - 1) < 0) - rb_sys_fail("gethostname"); + rb_sys_fail("gethostname(3)"); buf[sizeof buf - 1] = '\0'; return rb_str_new2(buf); @@ -1586,7 +1620,7 @@ socket_s_ip_address_list(VALUE self) fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) - rb_sys_fail("socket"); + rb_sys_fail("socket(2)"); memset(&ln, 0, sizeof(ln)); ln.lifn_family = AF_UNSPEC; @@ -1656,7 +1690,7 @@ socket_s_ip_address_list(VALUE self) fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) - rb_sys_fail("socket"); + rb_sys_fail("socket(2)"); bufsize = sizeof(initbuf); buf = initbuf; Index: ext/socket/basicsocket.c =================================================================== --- ext/socket/basicsocket.c (revision 36295) +++ ext/socket/basicsocket.c (working copy) @@ -80,7 +80,7 @@ bsock_shutdown(int argc, VALUE *argv, VA } GetOpenFile(sock, fptr); if (shutdown(fptr->fd, how) == -1) - rb_sys_fail(0); + rb_sys_fail("shutdown(2)"); return INT2FIX(0); } @@ -247,11 +247,9 @@ bsock_setsockopt(int argc, VALUE *argv, break; } -#define rb_sys_fail_path(path) rb_sys_fail_str(path) - rb_io_check_closed(fptr); if (setsockopt(fptr->fd, level, option, v, vlen) < 0) - rb_sys_fail_path(fptr->pathv); + rsock_sys_fail_path("setsockopt(2)", fptr->pathv); return INT2FIX(0); } @@ -332,7 +330,7 @@ bsock_getsockopt(VALUE sock, VALUE lev, rb_io_check_closed(fptr); if (getsockopt(fptr->fd, level, option, buf, &len) < 0) - rb_sys_fail_path(fptr->pathv); + rsock_sys_fail_path("getsockopt(2)", fptr->pathv); return rsock_sockopt_new(family, level, option, rb_str_new(buf, len)); } @@ -427,7 +425,7 @@ bsock_getpeereid(VALUE self) gid_t egid; GetOpenFile(self, fptr); if (getpeereid(fptr->fd, &euid, &egid) == -1) - rb_sys_fail("getpeereid"); + rb_sys_fail("getpeereid(3)"); return rb_assoc_new(UIDT2NUM(euid), GIDT2NUM(egid)); #elif defined(SO_PEERCRED) /* GNU/Linux */ rb_io_t *fptr; @@ -443,7 +441,7 @@ bsock_getpeereid(VALUE self) VALUE ret; GetOpenFile(self, fptr); if (getpeerucred(fptr->fd, &uc) == -1) - rb_sys_fail("getpeerucred"); + rb_sys_fail("getpeerucred(3C)"); ret = rb_assoc_new(UIDT2NUM(ucred_geteuid(uc)), GIDT2NUM(ucred_getegid(uc))); ucred_free(uc); return ret; Index: ext/socket/unixsocket.c =================================================================== --- ext/socket/unixsocket.c (revision 36295) +++ ext/socket/unixsocket.c (working copy) @@ -34,7 +34,7 @@ rsock_init_unixsock(VALUE sock, VALUE pa SafeStringValue(path); fd = rsock_socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { - rb_sys_fail("socket(2)"); + rsock_sys_fail_path("socket(2)", path); } MEMZERO(&sockaddr, struct sockaddr_un, 1); @@ -62,13 +62,13 @@ rsock_init_unixsock(VALUE sock, VALUE pa if (status < 0) { close(fd); - rb_sys_fail_str(path); + rsock_sys_fail_path("connect(2)", path); } if (server) { if (listen(fd, SOMAXCONN) < 0) { close(fd); - rb_sys_fail("listen(2)"); + rsock_sys_fail_path("listen(2)", path); } } @@ -117,7 +117,7 @@ unix_path(VALUE sock) struct sockaddr_un addr; socklen_t len = (socklen_t)sizeof(addr); if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0) - rb_sys_fail(0); + rsock_sys_fail_path("getsockname(2)", fptr->pathv); fptr->pathv = rb_obj_freeze(rsock_unixpath_str(&addr, len)); } return rb_str_dup(fptr->pathv); @@ -250,7 +250,7 @@ unix_send_io(VALUE sock, VALUE val) arg.fd = fptr->fd; while ((int)BLOCKING_REGION_FD(sendmsg_blocking, &arg) == -1) { if (!rb_io_wait_writable(arg.fd)) - rb_sys_fail("sendmsg(2)"); + rsock_sys_fail_path("sendmsg(2)", fptr->pathv); } return Qnil; @@ -338,7 +338,7 @@ unix_recv_io(int argc, VALUE *argv, VALU arg.fd = fptr->fd; while ((int)BLOCKING_REGION_FD(recvmsg_blocking, &arg) == -1) { if (!rb_io_wait_readable(arg.fd)) - rb_sys_fail("recvmsg(2)"); + rsock_sys_fail_path("recvmsg(2)", fptr->pathv); } #if FD_PASSING_BY_MSG_CONTROL @@ -424,7 +424,7 @@ unix_addr(VALUE sock) GetOpenFile(sock, fptr); if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0) - rb_sys_fail("getsockname(2)"); + rsock_sys_fail_path("getsockname(2)", fptr->pathv); return rsock_unixaddr(&addr, len); } @@ -450,7 +450,7 @@ unix_peeraddr(VALUE sock) GetOpenFile(sock, fptr); if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0) - rb_sys_fail("getpeername(2)"); + rsock_sys_fail_path("getpeername(2)", fptr->pathv); return rsock_unixaddr(&addr, len); } Index: test/socket/test_tcp.rb =================================================================== --- test/socket/test_tcp.rb (revision 36295) +++ test/socket/test_tcp.rb (working copy) @@ -6,6 +6,23 @@ end class TestSocket_TCPSocket < Test::Unit::TestCase + def test_initialize_failure + s = TCPServer.new("localhost", nil) + server_port = s.addr[1] + + c = TCPSocket.new("localhost", server_port) + client_port = c.addr[1] + + begin + # TCPServer.new uses SO_REUSEADDR so we must create a failure on the + # local address. + TCPSocket.new("localhost", server_port, "localhost", client_port) + flunk "expected SystemCallError" + rescue SystemCallError => e + assert_match "for \"localhost\" port #{client_port}", e.message + end + end + def test_recvfrom svr = TCPServer.new("localhost", 0) th = Thread.new { Index: test/socket/test_socket.rb =================================================================== --- test/socket/test_socket.rb (revision 36295) +++ test/socket/test_socket.rb (working copy) @@ -70,6 +70,22 @@ class TestSocket < Test::Unit::TestCase } end + def test_bind + Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|bound| + bound.bind(Socket.sockaddr_in(0, "127.0.0.1")) + addr = bound.getsockname + port, = Socket.unpack_sockaddr_in(addr) + + Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|s| + e = assert_raises(Errno::EADDRINUSE) do + s.bind(Socket.sockaddr_in(port, "127.0.0.1")) + end + + assert_match "bind(2) for \"127.0.0.1\" port #{port}", e.message + } + } + end + def test_getaddrinfo # This should not send a DNS query because AF_UNIX. assert_raise(SocketError) { Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") } Index: test/socket/test_udp.rb =================================================================== --- test/socket/test_udp.rb (revision 36295) +++ test/socket/test_udp.rb (working copy) @@ -36,4 +36,30 @@ class TestSocket_UDPSocket < Test::Unit: s.bind(host, 2000) } end + + def test_bind_addrinuse + host = "127.0.0.1" + port = 2001 + + in_use = UDPSocket.new + in_use.bind(host, port) + + s = UDPSocket.new + + e = assert_raises(Errno::EADDRINUSE) do + s.bind(host, port) + end + + assert_match "bind(2) for \"#{host}\" port #{port}", e.message + end + + def test_send_too_long + u = UDPSocket.new + + e = assert_raises Errno::EMSGSIZE do + u.send "\0" * 100_000, 0, "127.0.0.1", 7 # echo + end + + assert_match 'for "127.0.0.1" port 7', e.message + end end if defined?(UDPSocket)