ruby-changes:27044
From: usa <ko1@a...>
Date: Wed, 6 Feb 2013 14:31:05 +0900 (JST)
Subject: [ruby-changes:27044] usa:r39096 (ruby_1_9_3): merge revision(s) 35474,35479,38939,38943,38963,38991,38994: [Backport #7786]
usa 2013-02-06 14:30:55 +0900 (Wed, 06 Feb 2013) New Revision: 39096 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39096 Log: merge revision(s) 35474,35479,38939,38943,38963,38991,38994: [Backport #7786] * ext/socket/raddrinfo.c (init_unix_addrinfo): support the longest path in sockaddr_un. (inspect_sockaddr): ditto. (addrinfo_mdump): ditto. (addrinfo_mload): ditto. (rsock_unixpath_str): new function. (rsock_unixpath): removed. (rsock_unixaddr): use rsock_unixpath_str. * ext/socket/socket.c (sock_s_pack_sockaddr_un): support the longest path in sockaddr_un. (sock_s_unpack_sockaddr_un): ditto. (sock_s_gethostbyaddr): unused variable removed. * ext/socket/unixsocket.c (rsock_init_unixsock): support the longest path in sockaddr_un. * ext/socket/rubysocket.h (rsock_unixpath_str): declared. (rsock_unixpath): removed. * test/socket/test_unix.rb: comment out test_nul because abstract unix sockets may contain NULs. * ext/socket/socket.c (sock_s_pack_sockaddr_un): support the longest path in sockaddr_un, really. reported by nagachika. http://d.hatena.ne.jp/nagachika/20120426/ruby_trunk_changes_35474_35476 * ext/socket/raddrinfo.c (rsock_unixpath_len, init_unix_addrinfo), ext/socket/unixsocket.c (unixsock_connect_internal, rsock_init_unixsock): calculate the correct address length of an abstract socket. Without this fix, sizeof(struct sockaddr_un) is specified as the length of an abstract socket for bind(2) or connect(2), so the address of the socket is filled with extra NUL characters. See unix(7) for details. * ext/socket/lib/socket.rb (unix_server_socket): don't access the file system if the platform is Linux and path starts with NUL, which means that the socket is an abstract socket. * test/socket/test_unix.rb: related test. * ext/socket/raddrinfo (rsock_unix_sockaddr_len): renamed from rsock_unixpath_len, because it returns not the length of the path, but the length of a socket address for the path. * ext/socket/socket.c (sock_s_pack_sockaddr_un): calculate the correct address length of an abstract socket. * test/socket/test_unix.rb: related test. * ext/socket/unixsocket.c (rsock_init_unixsock): use rb_inspect() because rb_sys_fail_str() fails if its argument contains NUL. * test/socket/test_unix.rb: related test. * ext/socket/raddrinfo.c (rsock_unix_sockaddr_len): return sizeof(sa_familiy_t) if path is empty. see "Autobind Feature" in unix(7) for details. * ext/socket/lib/socket.rb (unix_socket_abstract_name?): treat an empty path as an abstract name. * test/socket/test_unix.rb: related test. Modified directories: branches/ruby_1_9_3/ Modified files: branches/ruby_1_9_3/ext/socket/lib/socket.rb branches/ruby_1_9_3/ext/socket/raddrinfo.c branches/ruby_1_9_3/ext/socket/rubysocket.h branches/ruby_1_9_3/ext/socket/socket.c branches/ruby_1_9_3/ext/socket/unixsocket.c branches/ruby_1_9_3/test/socket/test_unix.rb branches/ruby_1_9_3/version.h Index: ruby_1_9_3/ext/socket/rubysocket.h =================================================================== --- ruby_1_9_3/ext/socket/rubysocket.h (revision 39095) +++ ruby_1_9_3/ext/socket/rubysocket.h (revision 39096) @@ -233,8 +233,9 @@ VALUE rsock_make_hostent(VALUE host, str https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/rubysocket.h#L233 int rsock_revlookup_flag(VALUE revlookup, int *norevlookup); #ifdef HAVE_SYS_UN_H -const char* rsock_unixpath(struct sockaddr_un *sockaddr, socklen_t len); +VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len); VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len); +socklen_t rsock_unix_sockaddr_len(VALUE path); #endif int rsock_socket(int domain, int type, int proto); Index: ruby_1_9_3/ext/socket/raddrinfo.c =================================================================== --- ruby_1_9_3/ext/socket/raddrinfo.c (revision 39095) +++ ruby_1_9_3/ext/socket/raddrinfo.c (revision 39096) @@ -421,20 +421,46 @@ rsock_ipaddr(struct sockaddr *sockaddr, https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/raddrinfo.c#L421 } #ifdef HAVE_SYS_UN_H -const char* -rsock_unixpath(struct sockaddr_un *sockaddr, socklen_t len) +VALUE +rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len) { - if (sockaddr->sun_path < (char*)sockaddr + len) - return sockaddr->sun_path; + char *s, *e; + s = sockaddr->sun_path; + e = (char *)sockaddr + len; + while (s < e && *(e-1) == '\0') + e--; + if (s <= e) + return rb_str_new(s, e-s); else - return ""; + return rb_str_new2(""); } VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len) { return rb_assoc_new(rb_str_new2("AF_UNIX"), - rb_str_new2(rsock_unixpath(sockaddr, len))); + rsock_unixpath_str(sockaddr, len)); +} + +socklen_t +rsock_unix_sockaddr_len(VALUE path) +{ +#ifdef __linux__ + if (RSTRING_LEN(path) == 0) { + /* autobind; see unix(7) for details. */ + return (socklen_t) sizeof(sa_family_t); + } + else if (RSTRING_PTR(path)[0] == '\0') { + /* abstract namespace; see unix(7) for details. */ + return (socklen_t) offsetof(struct sockaddr_un, sun_path) + + RSTRING_LEN(path); + } + else { +#endif + return (socklen_t) sizeof(struct sockaddr_un); +#ifdef __linux__ + } +#endif } #endif @@ -763,19 +789,22 @@ static void https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/raddrinfo.c#L789 init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype) { struct sockaddr_un un; + socklen_t len; StringValue(path); - if (sizeof(un.sun_path) <= (size_t)RSTRING_LEN(path)) - rb_raise(rb_eArgError, "too long unix socket path (max: %dbytes)", - (int)sizeof(un.sun_path)-1); + if (sizeof(un.sun_path) < (size_t)RSTRING_LEN(path)) + rb_raise(rb_eArgError, + "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)", + (size_t)RSTRING_LEN(path), sizeof(un.sun_path)); MEMZERO(&un, struct sockaddr_un, 1); un.sun_family = AF_UNIX; memcpy((void*)&un.sun_path, RSTRING_PTR(path), RSTRING_LEN(path)); - init_addrinfo(rai, (struct sockaddr *)&un, (socklen_t)sizeof(un), + len = rsock_unix_sockaddr_len(path); + init_addrinfo(rai, (struct sockaddr *)&un, len, PF_UNIX, socktype, 0, Qnil, Qnil); } #endif @@ -996,9 +1025,11 @@ inspect_sockaddr(VALUE addrinfo, VALUE r https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/raddrinfo.c#L1025 case AF_UNIX: { struct sockaddr_un *addr = (struct sockaddr_un *)&rai->addr; - char *p, *s, *t, *e; + char *p, *s, *e; s = addr->sun_path; e = (char*)addr + rai->sockaddr_len; + while (s < e && *(e-1) == '\0') + e--; if (e < s) rb_str_cat2(ret, "too-short-AF_UNIX-sockaddr"); else if (s == e) @@ -1006,28 +1037,17 @@ inspect_sockaddr(VALUE addrinfo, VALUE r https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/raddrinfo.c#L1037 else { int printable_only = 1; p = s; - while (p < e && *p != '\0') { + while (p < e) { printable_only = printable_only && ISPRINT(*p) && !ISSPACE(*p); p++; } - t = p; - while (p < e && *p == '\0') - p++; - if (printable_only && /* only printable, no space */ - t < e && /* NUL terminated */ - p == e) { /* no data after NUL */ - if (s == t) - rb_str_cat2(ret, "empty-path-AF_UNIX-sockaddr"); - else if (s[0] == '/') /* absolute path */ - rb_str_cat2(ret, s); - else - rb_str_catf(ret, "AF_UNIX %s", s); + if (printable_only) { /* only printable, no space */ + if (s[0] != '/') /* relative path */ + rb_str_cat2(ret, "AF_UNIX "); + rb_str_cat(ret, s, p - s); } else { rb_str_cat2(ret, "AF_UNIX"); - e = (char *)addr->sun_path + sizeof(addr->sun_path); - while (s < e && *(e-1) == '\0') - e--; while (s < e) rb_str_catf(ret, ":%02x", (unsigned char)*s++); } @@ -1201,7 +1221,7 @@ addrinfo_mdump(VALUE self) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/raddrinfo.c#L1221 struct sockaddr_un *su = (struct sockaddr_un *)&rai->addr; char *s, *e; s = su->sun_path; - e = (char*)s + sizeof(su->sun_path); + e = (char*)su + rai->sockaddr_len; while (s < e && *(e-1) == '\0') e--; sockaddr = rb_str_new(s, e-s); @@ -1298,12 +1318,14 @@ addrinfo_mload(VALUE self, VALUE ary) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/raddrinfo.c#L1318 case AF_UNIX: { struct sockaddr_un uaddr; - memset(&uaddr, 0, sizeof(uaddr)); + MEMZERO(&uaddr, struct sockaddr_un, 1); uaddr.sun_family = AF_UNIX; StringValue(v); - if (sizeof(uaddr.sun_path) <= (size_t)RSTRING_LEN(v)) - rb_raise(rb_eSocket, "too long AF_UNIX path"); + if (sizeof(uaddr.sun_path) < (size_t)RSTRING_LEN(v)) + rb_raise(rb_eSocket, + "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)", + (size_t)RSTRING_LEN(v), sizeof(uaddr.sun_path)); memcpy(uaddr.sun_path, RSTRING_PTR(v), RSTRING_LEN(v)); len = (socklen_t)sizeof(uaddr); memcpy(&ss, &uaddr, len); Index: ruby_1_9_3/ext/socket/lib/socket.rb =================================================================== --- ruby_1_9_3/ext/socket/lib/socket.rb (revision 39095) +++ ruby_1_9_3/ext/socket/lib/socket.rb (revision 39096) @@ -716,12 +716,14 @@ class Socket < BasicSocket https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/lib/socket.rb#L716 # } # def self.unix_server_socket(path) - begin - st = File.lstat(path) - rescue Errno::ENOENT - end - if st && st.socket? && st.owned? - File.unlink path + if !unix_socket_abstract_name?(path) + begin + st = File.lstat(path) + rescue Errno::ENOENT + end + if st && st.socket? && st.owned? + File.unlink path + end end s = Addrinfo.unix(path).listen if block_given? @@ -729,13 +731,23 @@ class Socket < BasicSocket https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/lib/socket.rb#L731 yield s ensure s.close if !s.closed? - File.unlink path + if !unix_socket_abstract_name?(path) + File.unlink path + end end else s end end + class << self + private + + def unix_socket_abstract_name?(path) + /linux/ =~ RUBY_PLATFORM && /\A(\0|\z)/ =~ path + end + end + # creates a UNIX socket server on _path_. # It calls the block for each socket accepted. # Index: ruby_1_9_3/ext/socket/socket.c =================================================================== --- ruby_1_9_3/ext/socket/socket.c (revision 39095) +++ ruby_1_9_3/ext/socket/socket.c (revision 39096) @@ -954,13 +954,12 @@ sock_s_gethostbyaddr(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/socket.c#L954 { VALUE addr, family; struct hostent *h; - struct sockaddr *sa; char **pch; VALUE ary, names; int t = AF_INET; rb_scan_args(argc, argv, "11", &addr, &family); - sa = (struct sockaddr*)StringValuePtr(addr); + StringValue(addr); if (!NIL_P(family)) { t = rsock_family_arg(family); } @@ -1371,18 +1370,17 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/socket.c#L1370 sock_s_pack_sockaddr_un(VALUE self, VALUE path) { struct sockaddr_un sockaddr; - char *sun_path; VALUE addr; + StringValue(path); MEMZERO(&sockaddr, struct sockaddr_un, 1); sockaddr.sun_family = AF_UNIX; - sun_path = StringValueCStr(path); - if (sizeof(sockaddr.sun_path) <= strlen(sun_path)) { - rb_raise(rb_eArgError, "too long unix socket path (max: %dbytes)", - (int)sizeof(sockaddr.sun_path)-1); + if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) { + rb_raise(rb_eArgError, "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)", + (size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path)); } - strncpy(sockaddr.sun_path, sun_path, sizeof(sockaddr.sun_path)-1); - addr = rb_str_new((char*)&sockaddr, sizeof(sockaddr)); + memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path)); + addr = rb_str_new((char*)&sockaddr, rsock_unix_sockaddr_len(path)); OBJ_INFECT(addr, path); return addr; @@ -1404,7 +1402,6 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/socket.c#L1402 sock_s_unpack_sockaddr_un(VALUE self, VALUE addr) { struct sockaddr_un * sockaddr; - const char *sun_path; VALUE path; sockaddr = (struct sockaddr_un*)SockAddrStringValuePtr(addr); @@ -1420,13 +1417,7 @@ sock_s_unpack_sockaddr_un(VALUE self, VA https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/socket.c#L1417 rb_raise(rb_eTypeError, "too long sockaddr_un - %ld longer than %d", RSTRING_LEN(addr), (int)sizeof(struct sockaddr_un)); } - sun_path = rsock_unixpath(sockaddr, RSTRING_LENINT(addr)); - if (sizeof(struct sockaddr_un) == RSTRING_LEN(addr) && - sun_path == sockaddr->sun_path && - sun_path + strlen(sun_path) == RSTRING_PTR(addr) + RSTRING_LEN(addr)) { - rb_raise(rb_eArgError, "sockaddr_un.sun_path not NUL terminated"); - } - path = rb_str_new2(sun_path); + path = rsock_unixpath_str(sockaddr, RSTRING_LENINT(addr)); OBJ_INFECT(path, addr); return path; } Index: ruby_1_9_3/ext/socket/unixsocket.c =================================================================== --- ruby_1_9_3/ext/socket/unixsocket.c (revision 39095) +++ ruby_1_9_3/ext/socket/unixsocket.c (revision 39096) @@ -13,6 +13,7 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/unixsocket.c#L13 #ifdef HAVE_SYS_UN_H struct unixsock_arg { struct sockaddr_un *sockaddr; + socklen_t sockaddrlen; int fd; }; @@ -21,13 +22,14 @@ unixsock_connect_internal(VALUE a) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/unixsocket.c#L22 { struct unixsock_arg *arg = (struct unixsock_arg *)a; return (VALUE)rsock_connect(arg->fd, (struct sockaddr*)arg->sockaddr, - (socklen_t)sizeof(*arg->sockaddr), 0); + arg->sockaddrlen, 0); } VALUE rsock_init_unixsock(VALUE sock, VALUE path, int server) { struct sockaddr_un sockaddr; + socklen_t sockaddrlen; int fd, status; rb_io_t *fptr; @@ -39,19 +41,21 @@ rsock_init_unixsock(VALUE sock, VALUE pa https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/unixsocket.c#L41 MEMZERO(&sockaddr, struct sockaddr_un, 1); sockaddr.sun_family = AF_UNIX; - if (sizeof(sockaddr.sun_path) <= (size_t)RSTRING_LEN(path)) { - rb_raise(rb_eArgError, "too long unix socket path (max: %dbytes)", - (int)sizeof(sockaddr.sun_path)-1); + if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) { + rb_raise(rb_eArgError, "too long unix socket path (%ldbytes given but %dbytes max)", + RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path)); } memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path)); + sockaddrlen = rsock_unix_sockaddr_len(path); if (server) { - status = bind(fd, (struct sockaddr*)&sockaddr, (socklen_t)sizeof(sockaddr)); + status = bind(fd, (struct sockaddr*)&sockaddr, sockaddrlen); } else { int prot; struct unixsock_arg arg; arg.sockaddr = &sockaddr; + arg.sockaddrlen = sockaddrlen; arg.fd = fd; status = (int)rb_protect(unixsock_connect_internal, (VALUE)&arg, &prot); if (prot) { @@ -62,7 +66,7 @@ rsock_init_unixsock(VALUE sock, VALUE pa https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/socket/unixsocket.c#L66 if (status < 0) { close(fd); - rb_sys_fail(sockaddr.sun_path); + rb_sys_fail_str(rb_inspect(path)); } if (server) { Index: ruby_1_9_3/version.h =================================================================== --- ruby_1_9_3/version.h (revision 39095) +++ ruby_1_9_3/version.h (revision 39096) @@ -1,5 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/version.h#L1 #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 380 +#define RUBY_PATCHLEVEL 381 #define RUBY_RELEASE_DATE "2013-02-06" #define RUBY_RELEASE_YEAR 2013 Index: ruby_1_9_3/test/socket/test_unix.rb =================================================================== --- ruby_1_9_3/test/socket/test_unix.rb (revision 39095) +++ ruby_1_9_3/test/socket/test_unix.rb (revision 39096) @@ -324,8 +324,11 @@ class TestSocket_UNIXSocket < Test::Unit https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/socket/test_unix.rb#L324 assert_raise(ArgumentError) { UNIXServer.new("a" * 300) } end - def test_nul - assert_raise(ArgumentError) { Socket.sockaddr_un("a\0b") } + def test_abstract_namespace + return if /linux/ !~ RUBY_PLATFORM + addr = Socket.pack_sockaddr_un("\0foo") + assert_match(/\0foo\z/, addr) + assert_equal("\0foo", Socket.unpack_sockaddr_un(addr)) end def test_dgram_pair @@ -507,4 +510,69 @@ class TestSocket_UNIXSocket < Test::Unit https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/socket/test_unix.rb#L510 } end + def test_abstract_unix_server + return if /linux/ !~ RUBY_PLATFORM + name = "\0ruby-test_unix" + s0 = nil + UNIXServer.open(name) {|s| + assert_equal(name, s.local_address.unix_path) + s0 = s + UNIXSocket.open(name) {|c| + sock = s.accept + begin + assert_equal(name, c.remote_address.unix_path) + ensure + sock.close + end + } + } + assert(s0.closed?) + end + + def test_abstract_unix_socket_econnrefused + return if /linux/ !~ RUBY_PLATFORM + name = "\0ruby-test_unix" + assert_raise(Errno::ECONNREFUSED) do + UNIXSocket.open(name) {} + end + end + + def test_abstract_unix_server_socket + return if /linux/ !~ RUBY_PLATFORM + name = "\0ruby-test_unix" + s0 = nil + Socket.unix_server_socket(name) {|s| + assert_equal(name, s.local_address.unix_path) + s0 = s + Socket.unix(name) {|c| + sock, = s.accept + begin + assert_equal(name, c.remote_address.unix_path) + ensure + sock.close + end + } + } + assert(s0.closed?) + end + + def test_autobind + return if /linux/ !~ RUBY_PLATFORM + s0 = nil + Socket.unix_server_socket("") {|s| + name = s.local_address.unix_path + assert_match(/\A\0[0-9a-f]{5}\z/, name) + s0 = s + Socket.unix(name) {|c| + sock, = s.accept + begin + assert_equal(name, c.remote_address.unix_path) + ensure + sock.close + end + } + } + assert(s0.closed?) + end + end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM Property changes on: ruby_1_9_3 ___________________________________________________________________ Modified: svn:mergeinfo Merged /trunk:r35474,35479,38939,38943,38963,38991,38994 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/