2009-01-16 23:11:27 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
unixsocket.c -
|
|
|
|
|
|
|
|
created at: Thu Mar 31 12:21:29 JST 1994
|
|
|
|
|
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "rubysocket.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
struct unixsock_arg {
|
|
|
|
struct sockaddr_un *sockaddr;
|
2013-01-25 03:15:26 -05:00
|
|
|
socklen_t sockaddrlen;
|
2009-01-16 23:11:27 -05:00
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
2010-04-28 04:14:13 -04:00
|
|
|
unixsock_connect_internal(VALUE a)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2010-04-28 04:14:13 -04:00
|
|
|
struct unixsock_arg *arg = (struct unixsock_arg *)a;
|
2009-03-01 01:48:22 -05:00
|
|
|
return (VALUE)rsock_connect(arg->fd, (struct sockaddr*)arg->sockaddr,
|
2013-01-25 03:15:26 -05:00
|
|
|
arg->sockaddrlen, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
2018-03-28 07:30:27 -04:00
|
|
|
static VALUE
|
|
|
|
unixsock_path_value(VALUE path)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
#define TO_STR_FOR_LINUX_ABSTRACT_NAMESPACE 0
|
|
|
|
|
|
|
|
VALUE name = path;
|
|
|
|
#if TO_STR_FOR_LINUX_ABSTRACT_NAMESPACE
|
|
|
|
const int isstr = !NIL_P(name = rb_check_string_type(name));
|
|
|
|
#else
|
|
|
|
const int isstr = RB_TYPE_P(name, T_STRING);
|
|
|
|
#endif
|
|
|
|
if (isstr) {
|
|
|
|
if (RSTRING_LEN(name) == 0 || RSTRING_PTR(name)[0] == '\0') {
|
|
|
|
rb_check_safe_obj(name);
|
|
|
|
return name; /* ignore encoding */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return rb_get_path(path);
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_init_unixsock(VALUE sock, VALUE path, int server)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
struct sockaddr_un sockaddr;
|
2013-01-25 03:15:26 -05:00
|
|
|
socklen_t sockaddrlen;
|
2009-01-16 23:11:27 -05:00
|
|
|
int fd, status;
|
|
|
|
rb_io_t *fptr;
|
|
|
|
|
2018-03-28 07:30:27 -04:00
|
|
|
path = unixsock_path_value(path);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2013-02-15 17:19:05 -05:00
|
|
|
INIT_SOCKADDR_UN(&sockaddr, sizeof(struct sockaddr_un));
|
2012-04-25 23:34:12 -04:00
|
|
|
if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) {
|
2012-04-24 08:59:32 -04:00
|
|
|
rb_raise(rb_eArgError, "too long unix socket path (%ldbytes given but %dbytes max)",
|
2012-04-25 23:34:12 -04:00
|
|
|
RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path));
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
|
2013-01-25 18:33:50 -05:00
|
|
|
sockaddrlen = rsock_unix_sockaddr_len(path);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2014-05-28 11:42:09 -04:00
|
|
|
fd = rsock_socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
rsock_sys_fail_path("socket(2)", path);
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
if (server) {
|
2013-01-25 03:15:26 -05:00
|
|
|
status = bind(fd, (struct sockaddr*)&sockaddr, sockaddrlen);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int prot;
|
|
|
|
struct unixsock_arg arg;
|
|
|
|
arg.sockaddr = &sockaddr;
|
2013-01-25 03:15:26 -05:00
|
|
|
arg.sockaddrlen = sockaddrlen;
|
2009-01-16 23:11:27 -05:00
|
|
|
arg.fd = fd;
|
2010-04-28 04:14:13 -04:00
|
|
|
status = (int)rb_protect(unixsock_connect_internal, (VALUE)&arg, &prot);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (prot) {
|
|
|
|
close(fd);
|
|
|
|
rb_jump_tag(prot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status < 0) {
|
2015-12-23 03:57:48 -05:00
|
|
|
int e = errno;
|
2009-01-16 23:11:27 -05:00
|
|
|
close(fd);
|
2015-12-23 03:57:48 -05:00
|
|
|
rsock_syserr_fail_path(e, "connect(2)", path);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
2010-04-09 10:53:19 -04:00
|
|
|
if (server) {
|
2011-08-11 19:20:15 -04:00
|
|
|
if (listen(fd, SOMAXCONN) < 0) {
|
2015-12-23 03:57:48 -05:00
|
|
|
int e = errno;
|
2010-04-09 10:53:19 -04:00
|
|
|
close(fd);
|
2015-12-23 03:57:48 -05:00
|
|
|
rsock_syserr_fail_path(e, "listen(2)", path);
|
2010-04-09 10:53:19 -04:00
|
|
|
}
|
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_init_sock(sock, fd);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (server) {
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
fptr->pathv = rb_str_new_frozen(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* UNIXSocket.new(path) => unixsocket
|
|
|
|
*
|
|
|
|
* Creates a new UNIX client socket connected to _path_.
|
|
|
|
*
|
2016-10-04 23:57:32 -04:00
|
|
|
* require 'socket'
|
|
|
|
*
|
2009-01-16 23:11:27 -05:00
|
|
|
* s = UNIXSocket.new("/tmp/sock")
|
|
|
|
* s.send "hello", 0
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
unix_init(VALUE sock, VALUE path)
|
|
|
|
{
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_init_unixsock(sock, path, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* unixsocket.path => path
|
|
|
|
*
|
|
|
|
* Returns the path of the local address of unixsocket.
|
|
|
|
*
|
|
|
|
* s = UNIXServer.new("/tmp/sock")
|
|
|
|
* p s.path #=> "/tmp/sock"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
unix_path(VALUE sock)
|
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
if (NIL_P(fptr->pathv)) {
|
|
|
|
struct sockaddr_un addr;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t len = (socklen_t)sizeof(addr);
|
2013-01-30 07:01:51 -05:00
|
|
|
socklen_t len0 = len;
|
2009-01-16 23:11:27 -05:00
|
|
|
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
|
2013-04-05 22:39:44 -04:00
|
|
|
rsock_sys_fail_path("getsockname(2)", fptr->pathv);
|
2013-01-30 07:01:51 -05:00
|
|
|
if (len0 < len) len = len0;
|
2012-04-25 23:34:12 -04:00
|
|
|
fptr->pathv = rb_obj_freeze(rsock_unixpath_str(&addr, len));
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
return rb_str_dup(fptr->pathv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2015-06-16 10:40:20 -04:00
|
|
|
* unixsocket.recvfrom(maxlen [, flags[, outbuf]]) => [mesg, unixaddress]
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* Receives a message via _unixsocket_.
|
|
|
|
*
|
|
|
|
* _maxlen_ is the maximum number of bytes to receive.
|
|
|
|
*
|
|
|
|
* _flags_ should be a bitwise OR of Socket::MSG_* constants.
|
|
|
|
*
|
2015-06-15 16:02:43 -04:00
|
|
|
* _outbuf_ will contain only the received data after the method call
|
|
|
|
* even if it is not empty at the beginning.
|
|
|
|
*
|
2009-01-16 23:11:27 -05:00
|
|
|
* s1 = Socket.new(:UNIX, :DGRAM, 0)
|
2009-02-05 06:01:43 -05:00
|
|
|
* s1_ai = Addrinfo.unix("/tmp/sock1")
|
2009-01-16 23:11:27 -05:00
|
|
|
* s1.bind(s1_ai)
|
|
|
|
*
|
|
|
|
* s2 = Socket.new(:UNIX, :DGRAM, 0)
|
2009-02-05 06:01:43 -05:00
|
|
|
* s2_ai = Addrinfo.unix("/tmp/sock2")
|
2009-01-16 23:11:27 -05:00
|
|
|
* s2.bind(s2_ai)
|
|
|
|
* s3 = UNIXSocket.for_fd(s2.fileno)
|
|
|
|
*
|
|
|
|
* s1.send "a", 0, s2_ai
|
|
|
|
* p s3.recvfrom(10) #=> ["a", ["AF_UNIX", "/tmp/sock1"]]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
unix_recvfrom(int argc, VALUE *argv, VALUE sock)
|
|
|
|
{
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_s_recvfrom(sock, argc, argv, RECV_UNIX);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
2013-05-08 23:08:58 -04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && defined(SCM_RIGHTS)
|
2009-01-16 23:11:27 -05:00
|
|
|
#define FD_PASSING_BY_MSG_CONTROL 1
|
|
|
|
#else
|
|
|
|
#define FD_PASSING_BY_MSG_CONTROL 0
|
|
|
|
#endif
|
|
|
|
|
2013-05-09 08:12:17 -04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
|
2009-01-16 23:11:27 -05:00
|
|
|
#define FD_PASSING_BY_MSG_ACCRIGHTS 1
|
|
|
|
#else
|
|
|
|
#define FD_PASSING_BY_MSG_ACCRIGHTS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct iomsg_arg {
|
|
|
|
int fd;
|
|
|
|
struct msghdr msg;
|
|
|
|
};
|
|
|
|
|
2010-04-28 03:28:53 -04:00
|
|
|
#if defined(HAVE_SENDMSG) && (FD_PASSING_BY_MSG_CONTROL || FD_PASSING_BY_MSG_ACCRIGHTS)
|
2009-01-16 23:11:27 -05:00
|
|
|
static VALUE
|
|
|
|
sendmsg_blocking(void *data)
|
|
|
|
{
|
|
|
|
struct iomsg_arg *arg = data;
|
|
|
|
return sendmsg(arg->fd, &arg->msg, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* unixsocket.send_io(io) => nil
|
|
|
|
*
|
|
|
|
* Sends _io_ as file descriptor passing.
|
|
|
|
*
|
|
|
|
* s1, s2 = UNIXSocket.pair
|
|
|
|
*
|
|
|
|
* s1.send_io STDOUT
|
|
|
|
* stdout = s2.recv_io
|
|
|
|
*
|
|
|
|
* p STDOUT.fileno #=> 1
|
|
|
|
* p stdout.fileno #=> 6
|
|
|
|
*
|
|
|
|
* stdout.puts "hello" # outputs "hello\n" to standard output.
|
2015-12-07 13:39:47 -05:00
|
|
|
*
|
|
|
|
* _io_ may be any kind of IO object or integer file descriptor.
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
unix_send_io(VALUE sock, VALUE val)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
rb_io_t *fptr;
|
|
|
|
struct iomsg_arg arg;
|
|
|
|
struct iovec vec[1];
|
|
|
|
char buf[1];
|
|
|
|
|
|
|
|
#if FD_PASSING_BY_MSG_CONTROL
|
2013-04-16 10:07:50 -04:00
|
|
|
union {
|
2009-01-16 23:11:27 -05:00
|
|
|
struct cmsghdr hdr;
|
2013-04-16 10:07:50 -04:00
|
|
|
char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
|
2009-01-16 23:11:27 -05:00
|
|
|
} cmsg;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rb_obj_is_kind_of(val, rb_cIO)) {
|
|
|
|
rb_io_t *valfptr;
|
|
|
|
GetOpenFile(val, valfptr);
|
|
|
|
fd = valfptr->fd;
|
|
|
|
}
|
|
|
|
else if (FIXNUM_P(val)) {
|
|
|
|
fd = FIX2INT(val);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eTypeError, "neither IO nor file descriptor");
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
|
|
|
|
arg.msg.msg_name = NULL;
|
|
|
|
arg.msg.msg_namelen = 0;
|
|
|
|
|
|
|
|
/* Linux and Solaris doesn't work if msg_iov is NULL. */
|
|
|
|
buf[0] = '\0';
|
|
|
|
vec[0].iov_base = buf;
|
|
|
|
vec[0].iov_len = 1;
|
|
|
|
arg.msg.msg_iov = vec;
|
|
|
|
arg.msg.msg_iovlen = 1;
|
|
|
|
|
|
|
|
#if FD_PASSING_BY_MSG_CONTROL
|
|
|
|
arg.msg.msg_control = (caddr_t)&cmsg;
|
2010-04-28 04:14:13 -04:00
|
|
|
arg.msg.msg_controllen = (socklen_t)CMSG_LEN(sizeof(int));
|
2009-01-16 23:11:27 -05:00
|
|
|
arg.msg.msg_flags = 0;
|
|
|
|
MEMZERO((char*)&cmsg, char, sizeof(cmsg));
|
2010-04-28 04:14:13 -04:00
|
|
|
cmsg.hdr.cmsg_len = (socklen_t)CMSG_LEN(sizeof(int));
|
2009-01-16 23:11:27 -05:00
|
|
|
cmsg.hdr.cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg.hdr.cmsg_type = SCM_RIGHTS;
|
2009-02-27 10:21:23 -05:00
|
|
|
memcpy(CMSG_DATA(&cmsg.hdr), &fd, sizeof(int));
|
2009-01-16 23:11:27 -05:00
|
|
|
#else
|
|
|
|
arg.msg.msg_accrights = (caddr_t)&fd;
|
|
|
|
arg.msg.msg_accrightslen = sizeof(fd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
arg.fd = fptr->fd;
|
2011-06-13 10:11:23 -04:00
|
|
|
while ((int)BLOCKING_REGION_FD(sendmsg_blocking, &arg) == -1) {
|
|
|
|
if (!rb_io_wait_writable(arg.fd))
|
2013-04-05 22:39:44 -04:00
|
|
|
rsock_sys_fail_path("sendmsg(2)", fptr->pathv);
|
2011-06-13 10:11:23 -04:00
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
return Qnil;
|
2009-04-18 10:10:06 -04:00
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
#else
|
2009-04-18 10:10:06 -04:00
|
|
|
#define unix_send_io rb_f_notimplement
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
|
|
|
|
2010-04-28 03:28:53 -04:00
|
|
|
#if defined(HAVE_RECVMSG) && (FD_PASSING_BY_MSG_CONTROL || FD_PASSING_BY_MSG_ACCRIGHTS)
|
2009-01-16 23:11:27 -05:00
|
|
|
static VALUE
|
|
|
|
recvmsg_blocking(void *data)
|
|
|
|
{
|
|
|
|
struct iomsg_arg *arg = data;
|
2011-10-31 18:32:49 -04:00
|
|
|
int flags = 0;
|
2011-11-06 01:51:43 -04:00
|
|
|
return rsock_recvmsg(arg->fd, &arg->msg, flags);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* unixsocket.recv_io([klass [, mode]]) => io
|
|
|
|
*
|
2014-10-10 20:33:08 -04:00
|
|
|
* Example
|
|
|
|
*
|
2009-01-16 23:11:27 -05:00
|
|
|
* UNIXServer.open("/tmp/sock") {|serv|
|
2010-04-22 04:04:13 -04:00
|
|
|
* UNIXSocket.open("/tmp/sock") {|c|
|
2009-01-16 23:11:27 -05:00
|
|
|
* s = serv.accept
|
|
|
|
*
|
2010-04-22 04:04:13 -04:00
|
|
|
* c.send_io STDOUT
|
|
|
|
* stdout = s.recv_io
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* p STDOUT.fileno #=> 1
|
|
|
|
* p stdout.fileno #=> 7
|
|
|
|
*
|
|
|
|
* stdout.puts "hello" # outputs "hello\n" to standard output.
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2015-12-07 13:39:47 -05:00
|
|
|
* _klass_ will determine the class of _io_ returned (using the
|
|
|
|
* IO.for_fd singleton method or similar).
|
|
|
|
* If _klass_ is +nil+, an integer file descriptor is returned.
|
|
|
|
*
|
|
|
|
* _mode_ is the same as the argument passed to IO.for_fd
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
unix_recv_io(int argc, VALUE *argv, VALUE sock)
|
|
|
|
{
|
|
|
|
VALUE klass, mode;
|
|
|
|
rb_io_t *fptr;
|
|
|
|
struct iomsg_arg arg;
|
|
|
|
struct iovec vec[2];
|
|
|
|
char buf[1];
|
2018-06-24 18:08:15 -04:00
|
|
|
unsigned int gc_reason = 0;
|
|
|
|
enum {
|
|
|
|
GC_REASON_EMSGSIZE = 0x1,
|
|
|
|
GC_REASON_TRUNCATE = 0x2,
|
2018-06-24 23:14:48 -04:00
|
|
|
GC_REASON_ENOMEM = 0x4
|
2018-06-24 18:08:15 -04:00
|
|
|
};
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
int fd;
|
|
|
|
#if FD_PASSING_BY_MSG_CONTROL
|
2013-04-16 10:07:50 -04:00
|
|
|
union {
|
2009-01-16 23:11:27 -05:00
|
|
|
struct cmsghdr hdr;
|
2013-04-16 10:07:50 -04:00
|
|
|
char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
|
2009-01-16 23:11:27 -05:00
|
|
|
} cmsg;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "02", &klass, &mode);
|
|
|
|
if (argc == 0)
|
|
|
|
klass = rb_cIO;
|
|
|
|
if (argc <= 1)
|
|
|
|
mode = Qnil;
|
|
|
|
|
2018-06-24 18:08:15 -04:00
|
|
|
retry:
|
2009-01-16 23:11:27 -05:00
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
|
|
|
|
arg.msg.msg_name = NULL;
|
|
|
|
arg.msg.msg_namelen = 0;
|
|
|
|
|
|
|
|
vec[0].iov_base = buf;
|
|
|
|
vec[0].iov_len = sizeof(buf);
|
|
|
|
arg.msg.msg_iov = vec;
|
|
|
|
arg.msg.msg_iovlen = 1;
|
|
|
|
|
|
|
|
#if FD_PASSING_BY_MSG_CONTROL
|
|
|
|
arg.msg.msg_control = (caddr_t)&cmsg;
|
2010-04-28 04:14:13 -04:00
|
|
|
arg.msg.msg_controllen = (socklen_t)CMSG_SPACE(sizeof(int));
|
2009-01-16 23:11:27 -05:00
|
|
|
arg.msg.msg_flags = 0;
|
2010-04-28 04:14:13 -04:00
|
|
|
cmsg.hdr.cmsg_len = (socklen_t)CMSG_LEN(sizeof(int));
|
2009-01-16 23:11:27 -05:00
|
|
|
cmsg.hdr.cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg.hdr.cmsg_type = SCM_RIGHTS;
|
2009-02-27 10:21:23 -05:00
|
|
|
fd = -1;
|
|
|
|
memcpy(CMSG_DATA(&cmsg.hdr), &fd, sizeof(int));
|
2009-01-16 23:11:27 -05:00
|
|
|
#else
|
|
|
|
arg.msg.msg_accrights = (caddr_t)&fd;
|
|
|
|
arg.msg.msg_accrightslen = sizeof(fd);
|
|
|
|
fd = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
arg.fd = fptr->fd;
|
2011-06-13 10:11:23 -04:00
|
|
|
while ((int)BLOCKING_REGION_FD(recvmsg_blocking, &arg) == -1) {
|
2018-06-24 18:08:15 -04:00
|
|
|
int e = errno;
|
|
|
|
if (e == EMSGSIZE && !(gc_reason & GC_REASON_EMSGSIZE)) {
|
|
|
|
/* FreeBSD gets here when we're out of FDs */
|
|
|
|
gc_reason |= GC_REASON_EMSGSIZE;
|
|
|
|
rb_gc_for_fd(EMFILE);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
else if (e == ENOMEM && !(gc_reason & GC_REASON_ENOMEM)) {
|
|
|
|
/* ENOMEM is documented in recvmsg manpages */
|
|
|
|
gc_reason |= GC_REASON_ENOMEM;
|
|
|
|
rb_gc_for_fd(e);
|
|
|
|
goto retry;
|
|
|
|
}
|
2011-06-13 10:11:23 -04:00
|
|
|
if (!rb_io_wait_readable(arg.fd))
|
2018-06-24 18:08:15 -04:00
|
|
|
rsock_syserr_fail_path(e, "recvmsg(2)", fptr->pathv);
|
2011-06-13 10:11:23 -04:00
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
#if FD_PASSING_BY_MSG_CONTROL
|
2010-11-11 07:30:39 -05:00
|
|
|
if (arg.msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr)) {
|
2018-06-24 18:08:15 -04:00
|
|
|
/* FreeBSD and Linux both get here when we're out of FDs */
|
|
|
|
if (!(gc_reason & GC_REASON_TRUNCATE)) {
|
|
|
|
gc_reason |= GC_REASON_TRUNCATE;
|
|
|
|
rb_gc_for_fd(EMFILE);
|
|
|
|
goto retry;
|
|
|
|
}
|
2009-02-25 09:03:42 -05:00
|
|
|
rb_raise(rb_eSocket,
|
|
|
|
"file descriptor was not passed (msg_controllen=%d smaller than sizeof(struct cmsghdr)=%d)",
|
|
|
|
(int)arg.msg.msg_controllen, (int)sizeof(struct cmsghdr));
|
|
|
|
}
|
|
|
|
if (cmsg.hdr.cmsg_level != SOL_SOCKET) {
|
|
|
|
rb_raise(rb_eSocket,
|
|
|
|
"file descriptor was not passed (cmsg_level=%d, %d expected)",
|
|
|
|
cmsg.hdr.cmsg_level, SOL_SOCKET);
|
|
|
|
}
|
|
|
|
if (cmsg.hdr.cmsg_type != SCM_RIGHTS) {
|
|
|
|
rb_raise(rb_eSocket,
|
|
|
|
"file descriptor was not passed (cmsg_type=%d, %d expected)",
|
|
|
|
cmsg.hdr.cmsg_type, SCM_RIGHTS);
|
|
|
|
}
|
2010-11-11 07:30:39 -05:00
|
|
|
if (arg.msg.msg_controllen < (socklen_t)CMSG_LEN(sizeof(int))) {
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_raise(rb_eSocket,
|
|
|
|
"file descriptor was not passed (msg_controllen=%d smaller than CMSG_LEN(sizeof(int))=%d)",
|
|
|
|
(int)arg.msg.msg_controllen, (int)CMSG_LEN(sizeof(int)));
|
|
|
|
}
|
2010-11-11 07:30:39 -05:00
|
|
|
if ((socklen_t)CMSG_SPACE(sizeof(int)) < arg.msg.msg_controllen) {
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_raise(rb_eSocket,
|
|
|
|
"file descriptor was not passed (msg_controllen=%d bigger than CMSG_SPACE(sizeof(int))=%d)",
|
|
|
|
(int)arg.msg.msg_controllen, (int)CMSG_SPACE(sizeof(int)));
|
|
|
|
}
|
|
|
|
if (cmsg.hdr.cmsg_len != CMSG_LEN(sizeof(int))) {
|
2011-07-23 04:13:37 -04:00
|
|
|
rsock_discard_cmsg_resource(&arg.msg, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_raise(rb_eSocket,
|
|
|
|
"file descriptor was not passed (cmsg_len=%d, %d expected)",
|
|
|
|
(int)cmsg.hdr.cmsg_len, (int)CMSG_LEN(sizeof(int)));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (arg.msg.msg_accrightslen != sizeof(fd)) {
|
|
|
|
rb_raise(rb_eSocket,
|
2013-05-08 15:58:38 -04:00
|
|
|
"file descriptor was not passed (accrightslen=%d, %d expected)",
|
2009-01-16 23:11:27 -05:00
|
|
|
arg.msg.msg_accrightslen, (int)sizeof(fd));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if FD_PASSING_BY_MSG_CONTROL
|
2009-02-27 10:21:23 -05:00
|
|
|
memcpy(&fd, CMSG_DATA(&cmsg.hdr), sizeof(int));
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
2014-01-28 09:37:34 -05:00
|
|
|
|
|
|
|
rb_update_max_fd(fd);
|
|
|
|
|
|
|
|
if (rsock_cmsg_cloexec_state < 0)
|
|
|
|
rsock_cmsg_cloexec_state = rsock_detect_cloexec(fd);
|
|
|
|
if (rsock_cmsg_cloexec_state == 0 || fd <= 2)
|
|
|
|
rb_maygvl_fd_fix_cloexec(fd);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
if (klass == Qnil)
|
|
|
|
return INT2FIX(fd);
|
|
|
|
else {
|
|
|
|
ID for_fd;
|
|
|
|
int ff_argc;
|
|
|
|
VALUE ff_argv[2];
|
|
|
|
CONST_ID(for_fd, "for_fd");
|
|
|
|
ff_argc = mode == Qnil ? 1 : 2;
|
|
|
|
ff_argv[0] = INT2FIX(fd);
|
|
|
|
ff_argv[1] = mode;
|
2016-07-29 07:57:14 -04:00
|
|
|
return rb_funcallv(klass, for_fd, ff_argc, ff_argv);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
2009-04-18 10:10:06 -04:00
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
#else
|
2009-04-18 10:10:06 -04:00
|
|
|
#define unix_recv_io rb_f_notimplement
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* unixsocket.addr => [address_family, unix_path]
|
|
|
|
*
|
|
|
|
* Returns the local address as an array which contains
|
|
|
|
* address_family and unix_path.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* serv = UNIXServer.new("/tmp/sock")
|
|
|
|
* p serv.addr #=> ["AF_UNIX", "/tmp/sock"]
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
unix_addr(VALUE sock)
|
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
struct sockaddr_un addr;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t len = (socklen_t)sizeof addr;
|
2013-01-30 07:01:51 -05:00
|
|
|
socklen_t len0 = len;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
|
|
|
|
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
|
2013-04-05 22:39:44 -04:00
|
|
|
rsock_sys_fail_path("getsockname(2)", fptr->pathv);
|
2013-01-30 07:01:51 -05:00
|
|
|
if (len0 < len) len = len0;
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_unixaddr(&addr, len);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* unixsocket.peeraddr => [address_family, unix_path]
|
|
|
|
*
|
|
|
|
* Returns the remote address as an array which contains
|
|
|
|
* address_family and unix_path.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* serv = UNIXServer.new("/tmp/sock")
|
|
|
|
* c = UNIXSocket.new("/tmp/sock")
|
|
|
|
* p c.peeraddr #=> ["AF_UNIX", "/tmp/sock"]
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
unix_peeraddr(VALUE sock)
|
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
struct sockaddr_un addr;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t len = (socklen_t)sizeof addr;
|
2013-01-30 07:01:51 -05:00
|
|
|
socklen_t len0 = len;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
|
|
|
|
if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
|
2013-04-05 22:39:44 -04:00
|
|
|
rsock_sys_fail_path("getpeername(2)", fptr->pathv);
|
2013-01-30 07:01:51 -05:00
|
|
|
if (len0 < len) len = len0;
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_unixaddr(&addr, len);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* UNIXSocket.pair([type [, protocol]]) => [unixsocket1, unixsocket2]
|
|
|
|
* UNIXSocket.socketpair([type [, protocol]]) => [unixsocket1, unixsocket2]
|
|
|
|
*
|
2014-10-10 20:33:08 -04:00
|
|
|
* Creates a pair of sockets connected to each other.
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
|
|
|
|
*
|
|
|
|
* _protocol_ should be a protocol defined in the domain.
|
|
|
|
* 0 is default protocol for the domain.
|
|
|
|
*
|
|
|
|
* s1, s2 = UNIXSocket.pair
|
|
|
|
* s1.send "a", 0
|
|
|
|
* s1.send "b", 0
|
|
|
|
* p s2.recv(10) #=> "ab"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
unix_s_socketpair(int argc, VALUE *argv, VALUE klass)
|
|
|
|
{
|
|
|
|
VALUE domain, type, protocol;
|
2009-02-04 11:20:05 -05:00
|
|
|
VALUE args[3];
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2010-05-21 05:10:23 -04:00
|
|
|
domain = INT2FIX(PF_UNIX);
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_scan_args(argc, argv, "02", &type, &protocol);
|
|
|
|
if (argc == 0)
|
|
|
|
type = INT2FIX(SOCK_STREAM);
|
|
|
|
if (argc <= 1)
|
|
|
|
protocol = INT2FIX(0);
|
|
|
|
|
2009-02-04 11:20:05 -05:00
|
|
|
args[0] = domain;
|
|
|
|
args[1] = type;
|
|
|
|
args[2] = protocol;
|
|
|
|
|
2009-03-01 01:48:22 -05:00
|
|
|
return rsock_sock_s_socketpair(3, args, klass);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2010-03-22 12:15:21 -04:00
|
|
|
rsock_init_unixsocket(void)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
2011-08-15 19:08:39 -04:00
|
|
|
/*
|
|
|
|
* Document-class: UNIXSocket < BasicSocket
|
|
|
|
*
|
|
|
|
* UNIXSocket represents a UNIX domain stream client socket.
|
|
|
|
*/
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_cUNIXSocket = rb_define_class("UNIXSocket", rb_cBasicSocket);
|
|
|
|
rb_define_method(rb_cUNIXSocket, "initialize", unix_init, 1);
|
|
|
|
rb_define_method(rb_cUNIXSocket, "path", unix_path, 0);
|
|
|
|
rb_define_method(rb_cUNIXSocket, "addr", unix_addr, 0);
|
|
|
|
rb_define_method(rb_cUNIXSocket, "peeraddr", unix_peeraddr, 0);
|
|
|
|
rb_define_method(rb_cUNIXSocket, "recvfrom", unix_recvfrom, -1);
|
|
|
|
rb_define_method(rb_cUNIXSocket, "send_io", unix_send_io, 1);
|
|
|
|
rb_define_method(rb_cUNIXSocket, "recv_io", unix_recv_io, -1);
|
|
|
|
rb_define_singleton_method(rb_cUNIXSocket, "socketpair", unix_s_socketpair, -1);
|
|
|
|
rb_define_singleton_method(rb_cUNIXSocket, "pair", unix_s_socketpair, -1);
|
|
|
|
#endif
|
|
|
|
}
|