Permalink
Browse files

* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38994 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
1 parent 1ff38a5 commit 4ccfb2743f38d54eeecc5a229b3a8edf2eb84f3c @shugo shugo committed Jan 31, 2013
Showing with 46 additions and 4 deletions.
  1. +11 −0 ChangeLog
  2. +1 −1 ext/socket/lib/socket.rb
  3. +5 −1 ext/socket/raddrinfo.c
  4. +29 −2 test/socket/test_unix.rb
View
@@ -1,3 +1,14 @@
+Thu Jan 31 13:54:44 2013 Shugo Maeda <shugo@ruby-lang.org>
+
+ * 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.
+
Wed Jan 30 20:58:50 2013 Tanaka Akira <akr@fsij.org>
* ext/socket/basicsocket.c (bsock_getsockname): ignore truncated
@@ -819,7 +819,7 @@ class << self
private
def unix_socket_abstract_name?(path)
- /linux/ =~ RUBY_PLATFORM && /\A\0/ =~ path
+ /linux/ =~ RUBY_PLATFORM && /\A(\0|\z)/ =~ path
end
end
@@ -446,7 +446,11 @@ socklen_t
rsock_unix_sockaddr_len(VALUE path)
{
#ifdef __linux__
- if (RSTRING_PTR(path)[0] == '\0') {
+ 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);
@@ -542,7 +542,11 @@ def test_abstract_unix_server
s0 = s
UNIXSocket.open(name) {|c|
sock = s.accept
- assert_equal(name, c.remote_address.unix_path)
+ begin
+ assert_equal(name, c.remote_address.unix_path)
+ ensure
+ sock.close
+ end
}
}
assert(s0.closed?)
@@ -565,7 +569,30 @@ def test_abstract_unix_server_socket
s0 = s
Socket.unix(name) {|c|
sock, = s.accept
- assert_equal(name, c.remote_address.unix_path)
+ 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?)

0 comments on commit 4ccfb27

Please sign in to comment.