Method: UNIXSocket#send_io
- Defined in:
- unixsocket.c
#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.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'unixsocket.c', line 202
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;
}
|