File: C:/Ruby27-x64/share/doc/ruby/html/UNIXSocket.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class UNIXSocket - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="BasicSocket.html">BasicSocket</a>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-c-pair">::pair</a>
<li ><a href="#method-c-socketpair">::socketpair</a>
<li ><a href="#method-i-addr">#addr</a>
<li ><a href="#method-i-path">#path</a>
<li ><a href="#method-i-peeraddr">#peeraddr</a>
<li ><a href="#method-i-recv_io">#recv_io</a>
<li ><a href="#method-i-recvfrom">#recvfrom</a>
<li ><a href="#method-i-send_io">#send_io</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-UNIXSocket">
<h1 id="class-UNIXSocket" class="class">
class UNIXSocket
</h1>
<section class="description">
<p><a href="UNIXSocket.html"><code>UNIXSocket</code></a> represents a UNIX domain stream client socket.</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
new(path) → unixsocket
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a new UNIX client socket connected to <em>path</em>.</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'socket'</span>
<span class="ruby-identifier">s</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">"/tmp/sock"</span>)
<span class="ruby-identifier">s</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">"hello"</span>, <span class="ruby-value">0</span>
</pre>
<div class="method-source-code" id="new-source">
<pre>static VALUE
unix_init(VALUE sock, VALUE path)
{
return rsock_init_unixsock(sock, path, 0);
}</pre>
</div>
</div>
</div>
<div id="method-c-pair" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
pair([type [, protocol]]) → [unixsocket1, unixsocket2]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a pair of sockets connected to each other.</p>
<p><em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.</p>
<p><em>protocol</em> should be a protocol defined in the domain. 0 is default protocol for the domain.</p>
<pre class="ruby"><span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">pair</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">"a"</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">"b"</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=> "ab"</span>
</pre>
<div class="method-source-code" id="pair-source">
<pre>static VALUE
unix_s_socketpair(int argc, VALUE *argv, VALUE klass)
{
VALUE domain, type, protocol;
VALUE args[3];
domain = INT2FIX(PF_UNIX);
rb_scan_args(argc, argv, "02", &type, &protocol);
if (argc == 0)
type = INT2FIX(SOCK_STREAM);
if (argc <= 1)
protocol = INT2FIX(0);
args[0] = domain;
args[1] = type;
args[2] = protocol;
return rsock_sock_s_socketpair(3, args, klass);
}</pre>
</div>
</div>
</div>
<div id="method-c-socketpair" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
socketpair([type [, protocol]]) → [unixsocket1, unixsocket2]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a pair of sockets connected to each other.</p>
<p><em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.</p>
<p><em>protocol</em> should be a protocol defined in the domain. 0 is default protocol for the domain.</p>
<pre class="ruby"><span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">pair</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">"a"</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">"b"</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=> "ab"</span>
</pre>
<div class="method-source-code" id="socketpair-source">
<pre>static VALUE
unix_s_socketpair(int argc, VALUE *argv, VALUE klass)
{
VALUE domain, type, protocol;
VALUE args[3];
domain = INT2FIX(PF_UNIX);
rb_scan_args(argc, argv, "02", &type, &protocol);
if (argc == 0)
type = INT2FIX(SOCK_STREAM);
if (argc <= 1)
protocol = INT2FIX(0);
args[0] = domain;
args[1] = type;
args[2] = protocol;
return rsock_sock_s_socketpair(3, args, klass);
}</pre>
</div>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-addr" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
addr → [address_family, unix_path]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the local address as an array which contains address_family and unix_path.</p>
<p>Example</p>
<pre class="ruby"><span class="ruby-identifier">serv</span> = <span class="ruby-constant">UNIXServer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">"/tmp/sock"</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">serv</span>.<span class="ruby-identifier">addr</span> <span class="ruby-comment">#=> ["AF_UNIX", "/tmp/sock"]</span>
</pre>
<div class="method-source-code" id="addr-source">
<pre>static VALUE
unix_addr(VALUE sock)
{
rb_io_t *fptr;
struct sockaddr_un addr;
socklen_t len = (socklen_t)sizeof addr;
socklen_t len0 = len;
GetOpenFile(sock, fptr);
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rsock_sys_fail_path("getsockname(2)", fptr->pathv);
if (len0 < len) len = len0;
return rsock_unixaddr(&addr, len);
}</pre>
</div>
</div>
</div>
<div id="method-i-path" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
path → path
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the path of the local address of unixsocket.</p>
<pre class="ruby"><span class="ruby-identifier">s</span> = <span class="ruby-constant">UNIXServer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">"/tmp/sock"</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">path</span> <span class="ruby-comment">#=> "/tmp/sock"</span>
</pre>
<div class="method-source-code" id="path-source">
<pre>static VALUE
unix_path(VALUE sock)
{
rb_io_t *fptr;
GetOpenFile(sock, fptr);
if (NIL_P(fptr->pathv)) {
struct sockaddr_un addr;
socklen_t len = (socklen_t)sizeof(addr);
socklen_t len0 = len;
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rsock_sys_fail_path("getsockname(2)", fptr->pathv);
if (len0 < len) len = len0;
fptr->pathv = rb_obj_freeze(rsock_unixpath_str(&addr, len));
}
return rb_str_dup(fptr->pathv);
}</pre>
</div>
</div>
</div>
<div id="method-i-peeraddr" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
peeraddr → [address_family, unix_path]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the remote address as an array which contains address_family and unix_path.</p>
<p>Example</p>
<pre class="ruby"><span class="ruby-identifier">serv</span> = <span class="ruby-constant">UNIXServer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">"/tmp/sock"</span>)
<span class="ruby-identifier">c</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">"/tmp/sock"</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">c</span>.<span class="ruby-identifier">peeraddr</span> <span class="ruby-comment">#=> ["AF_UNIX", "/tmp/sock"]</span>
</pre>
<div class="method-source-code" id="peeraddr-source">
<pre>static VALUE
unix_peeraddr(VALUE sock)
{
rb_io_t *fptr;
struct sockaddr_un addr;
socklen_t len = (socklen_t)sizeof addr;
socklen_t len0 = len;
GetOpenFile(sock, fptr);
if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rsock_sys_fail_path("getpeername(2)", fptr->pathv);
if (len0 < len) len = len0;
return rsock_unixaddr(&addr, len);
}</pre>
</div>
</div>
</div>
<div id="method-i-recv_io" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
recv_io([klass [, mode]]) → io
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Example</p>
<pre class="ruby"><span class="ruby-constant">UNIXServer</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">"/tmp/sock"</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">serv</span><span class="ruby-operator">|</span>
<span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">"/tmp/sock"</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">s</span> = <span class="ruby-identifier">serv</span>.<span class="ruby-identifier">accept</span>
<span class="ruby-identifier">c</span>.<span class="ruby-identifier">send_io</span> <span class="ruby-constant">STDOUT</span>
<span class="ruby-identifier">stdout</span> = <span class="ruby-identifier">s</span>.<span class="ruby-identifier">recv_io</span>
<span class="ruby-identifier">p</span> <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">fileno</span> <span class="ruby-comment">#=> 1</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">fileno</span> <span class="ruby-comment">#=> 7</span>
<span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">"hello"</span> <span class="ruby-comment"># outputs "hello\n" to standard output.</span>
}
}
</pre>
<p><em>klass</em> will determine the class of <em>io</em> returned (using the <a href="IO.html#method-c-for_fd"><code>IO.for_fd</code></a> singleton method or similar). If <em>klass</em> is <code>nil</code>, an integer file descriptor is returned.</p>
<p><em>mode</em> is the same as the argument passed to <a href="IO.html#method-c-for_fd"><code>IO.for_fd</code></a></p>
<div class="method-source-code" id="recv_io-source">
<pre>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];
unsigned int gc_reason = 0;
enum {
GC_REASON_EMSGSIZE = 0x1,
GC_REASON_TRUNCATE = 0x2,
GC_REASON_ENOMEM = 0x4
};
int fd;
#if FD_PASSING_BY_MSG_CONTROL
union {
struct cmsghdr hdr;
char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
} cmsg;
#endif
rb_scan_args(argc, argv, "02", &klass, &mode);
if (argc == 0)
klass = rb_cIO;
if (argc <= 1)
mode = Qnil;
retry:
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;
arg.msg.msg_controllen = (socklen_t)CMSG_SPACE(sizeof(int));
arg.msg.msg_flags = 0;
cmsg.hdr.cmsg_len = (socklen_t)CMSG_LEN(sizeof(int));
cmsg.hdr.cmsg_level = SOL_SOCKET;
cmsg.hdr.cmsg_type = SCM_RIGHTS;
fd = -1;
memcpy(CMSG_DATA(&cmsg.hdr), &fd, sizeof(int));
#else
arg.msg.msg_accrights = (caddr_t)&fd;
arg.msg.msg_accrightslen = sizeof(fd);
fd = -1;
#endif
arg.fd = fptr->fd;
while ((int)BLOCKING_REGION_FD(recvmsg_blocking, &arg) == -1) {
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;
}
if (!rb_io_wait_readable(arg.fd))
rsock_syserr_fail_path(e, "recvmsg(2)", fptr->pathv);
}
#if FD_PASSING_BY_MSG_CONTROL
if (arg.msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr)) {
/* 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;
}
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);
}
if (arg.msg.msg_controllen < (socklen_t)CMSG_LEN(sizeof(int))) {
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)));
}
if ((socklen_t)CMSG_SPACE(sizeof(int)) < arg.msg.msg_controllen) {
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))) {
rsock_discard_cmsg_resource(&arg.msg, 0);
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,
"file descriptor was not passed (accrightslen=%d, %d expected)",
arg.msg.msg_accrightslen, (int)sizeof(fd));
}
#endif
#if FD_PASSING_BY_MSG_CONTROL
memcpy(&fd, CMSG_DATA(&cmsg.hdr), sizeof(int));
#endif
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);
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;
return rb_funcallv(klass, for_fd, ff_argc, ff_argv);
}
}</pre>
</div>
</div>
</div>
<div id="method-i-recvfrom" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
recvfrom(maxlen [, flags[, outbuf]]) → [mesg, unixaddress]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Receives a message via <em>unixsocket</em>.</p>
<p><em>maxlen</em> is the maximum number of bytes to receive.</p>
<p><em>flags</em> should be a bitwise OR of Socket::MSG_* constants.</p>
<p><em>outbuf</em> will contain only the received data after the method call even if it is not empty at the beginning.</p>
<pre class="ruby"><span class="ruby-identifier">s1</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">s1_ai</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">unix</span>(<span class="ruby-string">"/tmp/sock1"</span>)
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">bind</span>(<span class="ruby-identifier">s1_ai</span>)
<span class="ruby-identifier">s2</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">s2_ai</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">unix</span>(<span class="ruby-string">"/tmp/sock2"</span>)
<span class="ruby-identifier">s2</span>.<span class="ruby-identifier">bind</span>(<span class="ruby-identifier">s2_ai</span>)
<span class="ruby-identifier">s3</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">for_fd</span>(<span class="ruby-identifier">s2</span>.<span class="ruby-identifier">fileno</span>)
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">"a"</span>, <span class="ruby-value">0</span>, <span class="ruby-identifier">s2_ai</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s3</span>.<span class="ruby-identifier">recvfrom</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=> ["a", ["AF_UNIX", "/tmp/sock1"]]</span>
</pre>
<div class="method-source-code" id="recvfrom-source">
<pre>static VALUE
unix_recvfrom(int argc, VALUE *argv, VALUE sock)
{
return rsock_s_recvfrom(sock, argc, argv, RECV_UNIX);
}</pre>
</div>
</div>
</div>
<div id="method-i-send_io" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
send_io(io) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sends <em>io</em> as file descriptor passing.</p>
<pre class="ruby"><span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">pair</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send_io</span> <span class="ruby-constant">STDOUT</span>
<span class="ruby-identifier">stdout</span> = <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv_io</span>
<span class="ruby-identifier">p</span> <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">fileno</span> <span class="ruby-comment">#=> 1</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">fileno</span> <span class="ruby-comment">#=> 6</span>
<span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">"hello"</span> <span class="ruby-comment"># outputs "hello\n" to standard output.</span>
</pre>
<p><em>io</em> may be any kind of <a href="IO.html"><code>IO</code></a> object or integer file descriptor.</p>
<div class="method-source-code" id="send_io-source">
<pre>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
union {
struct cmsghdr hdr;
char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
} 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;
arg.msg.msg_controllen = (socklen_t)CMSG_LEN(sizeof(int));
arg.msg.msg_flags = 0;
MEMZERO((char*)&cmsg, char, sizeof(cmsg));
cmsg.hdr.cmsg_len = (socklen_t)CMSG_LEN(sizeof(int));
cmsg.hdr.cmsg_level = SOL_SOCKET;
cmsg.hdr.cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(&cmsg.hdr), &fd, sizeof(int));
#else
arg.msg.msg_accrights = (caddr_t)&fd;
arg.msg.msg_accrightslen = sizeof(fd);
#endif
arg.fd = fptr->fd;
while ((int)BLOCKING_REGION_FD(sendmsg_blocking, &arg) == -1) {
if (!rb_io_wait_writable(arg.fd))
rsock_sys_fail_path("sendmsg(2)", fptr->pathv);
}
return Qnil;
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>