[前][次][番号順一覧][スレッド一覧]

ruby-changes:26942

From: shugo <ko1@a...>
Date: Thu, 31 Jan 2013 13:59:43 +0900 (JST)
Subject: [ruby-changes:26942] shugo:r38994 (trunk): * ext/socket/raddrinfo.c (rsock_unix_sockaddr_len): return

shugo	2013-01-31 13:59:31 +0900 (Thu, 31 Jan 2013)

  New Revision: 38994

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38994

  Log:
    * 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 files:
    trunk/ChangeLog
    trunk/ext/socket/lib/socket.rb
    trunk/ext/socket/raddrinfo.c
    trunk/test/socket/test_unix.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38993)
+++ ChangeLog	(revision 38994)
@@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jan 31 13:54:44 2013  Shugo Maeda  <shugo@r...>
+
+	* 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@f...>
 
 	* ext/socket/basicsocket.c (bsock_getsockname): ignore truncated
Index: ext/socket/raddrinfo.c
===================================================================
--- ext/socket/raddrinfo.c	(revision 38993)
+++ ext/socket/raddrinfo.c	(revision 38994)
@@ -446,7 +446,11 @@ socklen_t https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L446
 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);
Index: ext/socket/lib/socket.rb
===================================================================
--- ext/socket/lib/socket.rb	(revision 38993)
+++ ext/socket/lib/socket.rb	(revision 38994)
@@ -819,7 +819,7 @@ class Socket < BasicSocket https://github.com/ruby/ruby/blob/trunk/ext/socket/lib/socket.rb#L819
     private
 
     def unix_socket_abstract_name?(path)
-      /linux/ =~ RUBY_PLATFORM && /\A\0/ =~ path
+      /linux/ =~ RUBY_PLATFORM && /\A(\0|\z)/ =~ path
     end
   end
 
Index: test/socket/test_unix.rb
===================================================================
--- test/socket/test_unix.rb	(revision 38993)
+++ test/socket/test_unix.rb	(revision 38994)
@@ -542,7 +542,11 @@ class TestSocket_UNIXSocket < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/socket/test_unix.rb#L542
       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 @@ class TestSocket_UNIXSocket < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/socket/test_unix.rb#L569
       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?)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]