summaryrefslogtreecommitdiff
path: root/ext/socket
diff options
context:
space:
mode:
Diffstat (limited to 'ext/socket')
-rw-r--r--ext/socket/lib/socket.rb26
-rw-r--r--ext/socket/raddrinfo.c21
-rw-r--r--ext/socket/rubysocket.h1
-rw-r--r--ext/socket/unixsocket.c8
4 files changed, 46 insertions, 10 deletions
diff --git a/ext/socket/lib/socket.rb b/ext/socket/lib/socket.rb
index 10ab13b953..981b937329 100644
--- a/ext/socket/lib/socket.rb
+++ b/ext/socket/lib/socket.rb
@@ -791,12 +791,14 @@ class Socket < BasicSocket
# }
#
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?
@@ -804,13 +806,23 @@ class Socket < BasicSocket
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/ =~ path
+ end
+ end
+
# creates a UNIX socket server on _path_.
# It calls the block for each socket accepted.
#
diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c
index 6a5129efa8..fdc3d4e61c 100644
--- a/ext/socket/raddrinfo.c
+++ b/ext/socket/raddrinfo.c
@@ -441,6 +441,23 @@ rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len)
return rb_assoc_new(rb_str_new2("AF_UNIX"),
rsock_unixpath_str(sockaddr, len));
}
+
+socklen_t
+rsock_unixpath_len(VALUE path)
+{
+#ifdef __linux__
+ 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
struct hostent_arg {
@@ -769,6 +786,7 @@ static void
init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
{
struct sockaddr_un un;
+ socklen_t len;
StringValue(path);
@@ -782,7 +800,8 @@ init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
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_unixpath_len(path);
+ init_addrinfo(rai, (struct sockaddr *)&un, len,
PF_UNIX, socktype, 0, Qnil, Qnil);
}
#endif
diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h
index 33cb51a5c9..c27b164188 100644
--- a/ext/socket/rubysocket.h
+++ b/ext/socket/rubysocket.h
@@ -240,6 +240,7 @@ int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
#ifdef HAVE_SYS_UN_H
VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len);
VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len);
+socklen_t rsock_unixpath_len(VALUE path);
#endif
int rsock_socket(int domain, int type, int proto);
diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c
index c8557cb4c9..e7693356b5 100644
--- a/ext/socket/unixsocket.c
+++ b/ext/socket/unixsocket.c
@@ -13,6 +13,7 @@
#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)
{
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;
@@ -44,14 +46,16 @@ rsock_init_unixsock(VALUE sock, VALUE path, int server)
RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path));
}
memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
+ sockaddrlen = rsock_unixpath_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) {