On Wed, Feb 14, 2007 at 12:10:37AM +0900, noreply / rubyforge.org wrote:
> Bugs item #8597, was opened at 2007-02-13 16:10
> You can respond by visiting: 
> http://rubyforge.org/tracker/?func=detail&atid=1698&aid=8597&group_id=426
> 
> Category: Standard Library
> Group: 1.8.4
> Status: Open
> Resolution: None
> Priority: 3
> Submitted By: Kristoffer Lund?n (stoffe)
> Assigned to: Nobody (None)
> Summary: Socket library should support abstract unix sockets
> 
> Initial Comment:
> See this post about abstract unix sockets, discussion is about implementing native D-BUS support in Ruby: http://sourceforge.net/mailarchive/message.php?msg_id=38052001

This is easy to patch for ruby 1.9 SVN, see below:

% svn diff
Index: ext/socket/socket.c
===================================================================
--- ext/socket/socket.c	(revision 11768)
+++ ext/socket/socket.c	(working copy)
@@ -1573,6 +1573,7 @@
 #ifdef HAVE_SYS_UN_H
 struct unixsock_arg {
     struct sockaddr_un *sockaddr;
+    int addrlen;
     int fd;
 };
 
@@ -1580,14 +1581,14 @@
 unixsock_connect_internal(struct unixsock_arg *arg)
 {
     return (VALUE)ruby_connect(arg->fd, (struct sockaddr*)arg->sockaddr,
-			       sizeof(*arg->sockaddr), 0);
+			       arg->addrlen, 0);
 }
 
 static VALUE
 init_unixsock(VALUE sock, VALUE path, int server)
 {
     struct sockaddr_un sockaddr;
-    int fd, status;
+    int fd, status, addrlen;
     OpenFile *fptr;
 
     SafeStringValue(path);
@@ -1603,14 +1604,17 @@
             (int)sizeof(sockaddr.sun_path)-1);
     }
     memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
+    addrlen = (long) ((char *) &((struct sockaddr_un *) 0)->sun_path) +
+        RSTRING_LEN(path);
 
     if (server) {
-        status = bind(fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr));
+        status = bind(fd, (struct sockaddr*)&sockaddr, addrlen);
     }
     else {
 	int prot;
 	struct unixsock_arg arg;
 	arg.sockaddr = &sockaddr;
+	arg.addrlen = addrlen;
 	arg.fd = fd;
         status = rb_protect((VALUE(*)(VALUE))unixsock_connect_internal,
 			    (VALUE)&arg, &prot);


The only thing this patch does is to always give the exact length of the
real contents of the sockaddr_un struct to the bind() and connect()
calls (which is necessary for abstract sockets, since they are not
zero-terminated, so it needs to know the exact length).  This makes
abstract sockets work correctly.

Cheers,
Bram Senders