mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38939 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
76ade818ae
commit
ad55d141eb
6 changed files with 92 additions and 10 deletions
16
ChangeLog
16
ChangeLog
|
@ -1,3 +1,19 @@
|
|||
Fri Jan 25 16:47:31 2013 Shugo Maeda <shugo@ruby-lang.org>
|
||||
|
||||
* 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.
|
||||
|
||||
Fri Jan 25 13:02:27 2013 Eric Hodel <drbrain@segment7.net>
|
||||
|
||||
* lib/drb/drb.rb: Updated documentation based on patch from Vincent
|
||||
|
|
|
@ -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.
|
||||
#
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -531,4 +531,34 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase
|
|||
}
|
||||
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
|
||||
assert_equal(name, c.remote_address.unix_path)
|
||||
}
|
||||
}
|
||||
assert(s0.closed?)
|
||||
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
|
||||
assert_equal(name, c.remote_address.unix_path)
|
||||
}
|
||||
}
|
||||
assert(s0.closed?)
|
||||
end
|
||||
|
||||
end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM
|
||||
|
|
Loading…
Add table
Reference in a new issue