15 static VALUE sym_EAGAIN
;
16 #define WAITALL 0x4000000
18 /* taken from haproxy */
19 #define MAX_AT_ONCE (1 << 30)
21 #ifndef F_LINUX_SPECIFIC_BASE
22 # define F_LINUX_SPECIFIC_BASE 1024
26 # define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
27 # define F_GETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 8)
31 # define rb_io_t OpenFile
35 # define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
37 # if !HAVE_RB_IO_T || (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8)
38 # define FPTR_TO_FD(fptr) fileno(fptr->f)
40 # define FPTR_TO_FD(fptr) fptr->fd
45 # define SSIZET2NUM(x) LONG2NUM(x)
48 # define NUM2SSIZET(x) NUM2LONG(x)
51 # define SIZET2NUM(x) ULONG2NUM(x)
54 # define NUM2SIZET(x) NUM2ULONG(x)
57 static int my_fileno(VALUE io
)
63 case T_FIXNUM
: return FIX2INT(io
);
65 GetOpenFile(io
, fptr
);
66 return FPTR_TO_FD(fptr
);
69 io
= rb_convert_type(io
, T_FILE
, "IO", "to_io");
75 static int check_fileno(VALUE io
)
77 int saved_errno
= errno
;
78 int fd
= my_fileno(io
);
82 #ifndef HAVE_RB_THREAD_BLOCKING_REGION
83 /* partial emulation of the 1.9 rb_thread_blocking_region under 1.8 */
85 # define RUBY_UBF_IO ((rb_unblock_function_t *)-1)
86 typedef void rb_unblock_function_t(void *);
87 typedef VALUE
rb_blocking_function_t(void *);
89 rb_thread_blocking_region(
90 rb_blocking_function_t
*fn
, void *data1
,
91 rb_unblock_function_t
*ubf
, void *data2
)
95 assert(RUBY_UBF_IO
== ubf
&& "RUBY_UBF_IO required for emulation");
103 #endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
106 # define RSTRING_PTR(s) (RSTRING(s)->ptr)
109 # define RSTRING_LEN(s) (RSTRING(s)->len)
112 # define RARRAY_LEN(s) (RARRAY(s)->len)
115 static VALUE
io_run(rb_blocking_function_t
*fn
, void *data
)
117 return rb_thread_blocking_region(fn
, data
, RUBY_UBF_IO
, 0);
129 static VALUE
nogvl_splice(void *ptr
)
131 struct splice_args
*a
= ptr
;
133 if (a
->len
> MAX_AT_ONCE
)
134 a
->len
= MAX_AT_ONCE
;
136 return (VALUE
)splice(a
->fd_in
, a
->off_in
, a
->fd_out
, a
->off_out
,
140 static ssize_t
do_splice(int argc
, VALUE
*argv
, unsigned dflags
)
143 VALUE io_in
, off_in
, io_out
, off_out
, len
, flags
;
144 struct splice_args a
;
149 rb_scan_args(argc
, argv
, "51",
150 &io_in
, &off_in
, &io_out
, &off_out
, &len
, &flags
);
152 a
.off_in
= NIL_P(off_in
) ? NULL
: (i
= NUM2OFFT(off_in
), &i
);
153 a
.off_out
= NIL_P(off_out
) ? NULL
: (o
= NUM2OFFT(off_out
), &o
);
154 a
.len
= NUM2SIZET(len
);
155 a
.flags
= NIL_P(flags
) ? dflags
: NUM2UINT(flags
) | dflags
;
156 waitall
= a
.flags
& WAITALL
;
161 a
.fd_in
= check_fileno(io_in
);
162 a
.fd_out
= check_fileno(io_out
);
163 bytes
= (ssize_t
)io_run(nogvl_splice
, &a
);
167 if (waitall
&& errno
== EAGAIN
) {
168 rb_io_wait_readable(check_fileno(io_in
));
170 rb_io_wait_writable(check_fileno(io_out
));
176 } else if (bytes
== 0) {
178 } else if (waitall
) {
180 if ((a
.len
-= bytes
) == 0)
194 * IO.splice(io_in, off_in, io_out, off_out, len) => integer
195 * IO.splice(io_in, off_in, io_out, off_out, len, flags) => integer
197 * Splice +len+ bytes from/to a pipe. Either +io_in+ or +io_out+
198 * MUST be a pipe. +io_in+ and +io_out+ may BOTH be pipes as of
199 * Linux 2.6.31 or later.
201 * +off_in+ and +off_out+ if non-nil may be used to
202 * specify an offset for the non-pipe file descriptor.
204 * +flags+ defaults to zero if unspecified.
205 * +flags+ may be a bitmask of the following flags:
207 * * IO::Splice::F_MOVE
208 * * IO::Splice::F_NONBLOCK
209 * * IO::Splice::F_MORE
210 * * IO::Splice::WAITALL
212 * Returns the number of bytes spliced.
213 * Raises EOFError when +io_in+ has reached end of file.
214 * Raises Errno::EAGAIN if the IO::Splice::F_NONBLOCK flag is set
215 * and the pipe has no data to read from or space to write to. May
216 * also raise Errno::EAGAIN if the non-pipe descriptor has no data
217 * to read from or space to write to.
219 * As splice never exposes buffers to userspace, it will not take
220 * into account userspace buffering done by Ruby or stdio. It is
221 * also not subject to encoding/decoding filters under Ruby 1.9.
223 * Consider using IO.trysplice if +io_out+ is a pipe or if you are using
224 * non-blocking I/O on both descriptors as it avoids the cost of raising
225 * common Errno::EAGAIN exceptions.
227 * See manpage for full documentation:
228 * http://kernel.org/doc/man-pages/online/pages/man2/splice.2.html
230 static VALUE
my_splice(int argc
, VALUE
*argv
, VALUE self
)
232 ssize_t n
= do_splice(argc
, argv
, 0);
237 rb_sys_fail("splice");
238 return SSIZET2NUM(n
);
243 * IO.trysplice(io_in, off_in, io_out, off_out, len) => integer
244 * IO.trysplice(io_in, off_in, io_out, off_out, len, flags) => integer
246 * Exactly like IO.splice, except +:EAGAIN+ is returned when either
247 * the read or write end would block instead of raising Errno::EAGAIN.
249 * IO::Splice::F_NONBLOCK is always passed for the pipe descriptor,
250 * but this can still block if the non-pipe descriptor is blocking.
252 * See IO.splice documentation for more details.
254 * This method is recommended whenever +io_out+ is a pipe.
256 static VALUE
trysplice(int argc
, VALUE
*argv
, VALUE self
)
258 ssize_t n
= do_splice(argc
, argv
, SPLICE_F_NONBLOCK
);
265 rb_sys_fail("splice");
267 return SSIZET2NUM(n
);
277 /* runs without GVL */
278 static VALUE
nogvl_tee(void *ptr
)
280 struct tee_args
*a
= ptr
;
282 if (a
->len
> MAX_AT_ONCE
)
283 a
->len
= MAX_AT_ONCE
;
285 return (VALUE
)tee(a
->fd_in
, a
->fd_out
, a
->len
, a
->flags
);
288 static ssize_t
do_tee(int argc
, VALUE
*argv
, unsigned dflags
)
290 VALUE io_in
, io_out
, len
, flags
;
296 rb_scan_args(argc
, argv
, "31", &io_in
, &io_out
, &len
, &flags
);
297 a
.len
= (size_t)NUM2SIZET(len
);
298 a
.flags
= NIL_P(flags
) ? dflags
: NUM2UINT(flags
) | dflags
;
299 waitall
= a
.flags
& WAITALL
;
304 a
.fd_in
= check_fileno(io_in
);
305 a
.fd_out
= check_fileno(io_out
);
306 bytes
= (ssize_t
)io_run(nogvl_tee
, &a
);
310 if (waitall
&& errno
== EAGAIN
) {
311 rb_io_wait_readable(check_fileno(io_in
));
313 rb_io_wait_writable(check_fileno(io_out
));
319 } else if (bytes
== 0) {
321 } else if (waitall
) {
323 if ((a
.len
-= bytes
) == 0)
335 * IO.tee(io_in, io_out, len) => integer
336 * IO.tee(io_in, io_out, len, flags) => integer
338 * Copies up to +len+ bytes of data from +io_in+ to +io_out+. +io_in+
339 * and +io_out+ must both refer to pipe descriptors. +io_in+ and +io_out+
340 * may not be endpoints of the same pipe.
342 * +flags+ may be zero (the default) or a combination of:
343 * * IO::Splice::F_NONBLOCK
344 * * IO::Splice::WAITALL
346 * Other IO::Splice flags are currently unimplemented or have no effect.
348 * Returns the number of bytes duplicated if successful.
349 * Raises EOFError when +io_in+ is closed and emptied.
350 * Raises Errno::EAGAIN when +io_in+ is empty and/or +io_out+ is full
351 * and +flags+ contains IO::Splice::F_NONBLOCK
353 * Consider using IO.trytee if you are using IO::Splice::F_NONBLOCK
354 * as it avoids the cost of raising common Errno::EAGAIN exceptions.
356 * See manpage for full documentation:
357 * http://kernel.org/doc/man-pages/online/pages/man2/tee.2.html
359 static VALUE
my_tee(int argc
, VALUE
*argv
, VALUE self
)
361 ssize_t n
= do_tee(argc
, argv
, 0);
368 return SSIZET2NUM(n
);
373 * IO.trytee(io_in, io_out, len) => integer
374 * IO.trytee(io_in, io_out, len, flags) => integer
376 * Exactly like IO.tee, except +:EAGAIN+ is returned when either
377 * the read or write end would block instead of raising Errno::EAGAIN.
379 * IO::Splice::F_NONBLOCK is always passed for the pipe descriptor,
380 * but this can still block if the non-pipe descriptor is blocking.
382 * See IO.tee documentation for more details.
384 static VALUE
trytee(int argc
, VALUE
*argv
, VALUE self
)
386 ssize_t n
= do_tee(argc
, argv
, SPLICE_F_NONBLOCK
);
396 return SSIZET2NUM(n
);
399 struct vmsplice_args
{
402 unsigned long nr_segs
;
406 static VALUE
nogvl_vmsplice(void *ptr
)
408 struct vmsplice_args
*a
= ptr
;
410 return (VALUE
)vmsplice(a
->fd
, a
->iov
, a
->nr_segs
, a
->flags
);
413 /* this can't be a function since we use alloca() */
414 #define ARY2IOVEC(iov,iovcnt,expect,ary) \
418 iovcnt = (unsigned long)RARRAY_LEN(ary); \
419 if (iovcnt > IOV_MAX) \
420 rb_raise(rb_eArgError, "array is larger than IOV_MAX"); \
421 iov = tmp = alloca(sizeof(struct iovec) * iovcnt); \
423 for (i = 0; i < iovcnt; tmp++, i++) { \
424 VALUE cur = rb_ary_entry(ary, (long)i); \
425 Check_Type(cur, T_STRING); \
426 tmp->iov_base = RSTRING_PTR(cur); \
427 tmp->iov_len = RSTRING_LEN(cur); \
428 expect += tmp->iov_len; \
432 static void advance_vmsplice_args(struct vmsplice_args
*a
, long n
)
434 struct iovec
*new_iov
= a
->iov
;
437 /* skip over iovecs we've already written completely */
438 for (i
= 0; i
< a
->nr_segs
; i
++, new_iov
++) {
442 * partially written iov,
443 * modify and retry with current iovec in
446 if (new_iov
->iov_len
> (size_t)n
) {
447 VALUE base
= (VALUE
)new_iov
->iov_base
;
449 new_iov
->iov_len
-= n
;
450 new_iov
->iov_base
= (void *)(base
+ n
);
454 n
-= new_iov
->iov_len
;
457 /* setup to retry without the already-written iovecs */
464 * IO.vmsplice(io, string_array) => integer
465 * IO.vmsplice(io, string_array, flags) => integer
466 * IO.vmsplice(io, string) => integer
467 * IO.vmsplice(io, string, flags) => integer
469 * Transfers an array of strings into the pipe descriptor given by io.
470 * +io+ must be the writable end of a pipe.
472 * This may allow the kernel to avoid data copies in some cases.
473 * but is (probably) of limited usefulness in Ruby. If you have
474 * use cases or ideas for making this more useful for Ruby users,
475 * please tell us at ruby.io.splice@librelist.org!
477 * Also consider the "sendfile" RubyGem or IO.copy_stream in Ruby 1.9
478 * if you want to do zero-copy file transfers to pipes or sockets. As
479 * of Linux 2.6.33, sendfile(2) can copy to any output descriptor,
482 * See manpage for full documentation:
483 * http://kernel.org/doc/man-pages/online/pages/man2/vmsplice.2.html
485 static VALUE
my_vmsplice(int argc
, VALUE
* argv
, VALUE self
)
489 struct vmsplice_args a
;
493 VALUE io
, data
, flags
;
495 rb_scan_args(argc
, argv
, "21", &io
, &data
, &flags
);
497 switch (TYPE(data
)) {
499 iov
.iov_base
= RSTRING_PTR(data
);
500 iov
.iov_len
= (size_t)(left
= (ssize_t
)RSTRING_LEN(data
));
505 ARY2IOVEC(a
.iov
, a
.nr_segs
, left
, data
);
508 rb_raise(rb_eTypeError
, "wrong argument type %s "
509 "(expected a String or Array of strings)",
510 rb_obj_classname(data
));
513 a
.flags
= NIL_P(flags
) ? 0 : NUM2UINT(flags
);
516 a
.fd
= check_fileno(io
);
517 n
= (ssize_t
)io_run(nogvl_vmsplice
, &a
);
520 if (errno
== EAGAIN
) {
521 if (a
.flags
& SPLICE_F_NONBLOCK
)
522 rb_sys_fail("vmsplice");
523 if (rb_io_wait_writable(check_fileno(io
)))
525 /* fall through on error */
528 * unlikely to hit this case, return the
529 * already written bytes, we'll let the next
530 * write (or close) fail instead
536 rb_sys_fail("vmsplice");
543 advance_vmsplice_args(&a
, n
);
546 return SSIZET2NUM(rv
);
551 * reader, writer = IO.pipe
552 * reader.pipe_size => integer
554 * Returns the pipe capacity of the underlying pipe in bytes. The
555 * default capacity is 65536 bytes since Linux 2.6.11, and 4096 bytes
556 * in previous kernels.
558 * Since the pipe is a circular buffer in the same kernel, the size
559 * of the reader is exactly the same as the size of the writer.
561 * This method is only exposed on Linux 2.6.35 or later.
563 static VALUE
pipe_size(VALUE self
)
565 int size
= fcntl(my_fileno(self
), F_GETPIPE_SZ
);
568 rb_sys_fail("fcntl(F_GETPIPE_SZ)");
570 return INT2NUM(size
);
575 * reader, writer = IO.pipe
576 * reader.pipe_size = integer
578 * Sets and returns the pipe capacity of the underlying pipe in bytes.
580 * This MUST be a power-of-two, or Errno::EINVAL will be raised.
581 * Linux will silently increase this to be equal to the page size
582 * (4096 bytes on most architectures) if the specified value is
583 * less than the size of a page.
585 * For users without CAP_SYS_RESOURCE, this raises Errno::EPERM when
586 * attempting to specify a value greater than the value in
587 * /proc/sys/fs/pipe-max-size.
589 * Since the pipe is a circular buffer in the same kernel, the size
590 * of the reader is exactly the same as the size of the writer.
592 * Raises Errno::EBUSY if the assigned value is less than
593 * the currently filled portion of the pipe.
595 * This method is only exposed on Linux 2.6.35 or later.
597 static VALUE
set_pipe_size(VALUE self
, VALUE size
)
599 int fd
= my_fileno(self
);
600 int bytes
= NUM2INT(size
);
601 int rv
= fcntl(fd
, F_SETPIPE_SZ
, bytes
);
604 if (errno
== ENOMEM
) {
606 rv
= fcntl(fd
, F_SETPIPE_SZ
, bytes
);
609 rb_sys_fail("fcntl(F_SETPIPE_SZ)");
615 static int can_mod_pipe_size(void)
618 * pipe2 appeared in Linux 2.6.27, F_*PIPE_SZ appeared in 2.6.35,
619 * thus not having pipe2 automatically disqualifies us from having
624 int rc
= pipe2(fds
, O_CLOEXEC
);
627 rc
= fcntl(fds
[0], F_GETPIPE_SZ
);
634 * weird error, but don't raise during init, this could be
641 #else /* ! HAVE_PIPE2 */
643 #endif /* ! HAVE_PIPE2 */
646 void Init_io_splice_ext(void)
648 VALUE mSplice
= rb_define_module_under(rb_cIO
, "Splice");
650 rb_define_singleton_method(rb_cIO
, "splice", my_splice
, -1);
651 rb_define_singleton_method(rb_cIO
, "trysplice", trysplice
, -1);
652 rb_define_singleton_method(rb_cIO
, "tee", my_tee
, -1);
653 rb_define_singleton_method(rb_cIO
, "trytee", trytee
, -1);
654 rb_define_singleton_method(rb_cIO
, "vmsplice", my_vmsplice
, -1);
657 * Attempt to move pages instead of copying. This is only a hint
658 * and support for it was removed in Linux 2.6.21. It will be
659 * re-added for FUSE filesystems only in Linux 2.6.35.
661 rb_define_const(mSplice
, "F_MOVE", UINT2NUM(SPLICE_F_MOVE
));
662 assert(WAITALL
!= SPLICE_F_MOVE
&& "WAITALL == F_MOVE");
665 * Do not block on pipe I/O. This flag only affects the pipe(s)
666 * being spliced from/to and has no effect on the non-pipe
667 * descriptor (which requires non-blocking operation to be set
670 * The non-blocking flag (O_NONBLOCK) on the pipe descriptors
671 * themselves are ignored by this family of functions, and
672 * using this flag is the only way to get non-blocking operation
675 * It is highly recommended this flag be set (or IO.trysplice used)
676 * whenever splicing from a socket into a pipe unless there is
677 * another (native) thread or process doing a blocking read on that
678 * pipe. Otherwise it is possible to block a single-threaded process
679 * if the socket buffers are larger than the pipe buffers.
681 rb_define_const(mSplice
, "F_NONBLOCK", UINT2NUM(SPLICE_F_NONBLOCK
));
682 assert(WAITALL
!= SPLICE_F_NONBLOCK
&& "WAITALL == F_NONBLOCK");
685 * Indicate that there may be more data coming into the outbound
686 * descriptor. This can allow the kernel to avoid sending partial
687 * frames from sockets. Currently only used with splice.
689 rb_define_const(mSplice
, "F_MORE", UINT2NUM(SPLICE_F_MORE
));
690 assert(WAITALL
!= SPLICE_F_MORE
&& "WAITALL == F_MORE");
693 * Only usable by vmsplice. This flag probably not useful in the
694 * context of Ruby applications which cannot control alignment.
696 rb_define_const(mSplice
, "F_GIFT", UINT2NUM(SPLICE_F_GIFT
));
697 assert(WAITALL
!= SPLICE_F_GIFT
&& "WAITALL == F_GIFT");
700 * Retry until the requested transfer is complete, this will
701 * cause IO.splice/IO.tee to never return less than the requested
702 * transfer size unless an error occored.
704 * IO.vmsplice always defaults to this behavior.
706 rb_define_const(mSplice
, "WAITALL", UINT2NUM(WAITALL
));
709 * The maximum size of an atomic write to a pipe
710 * POSIX requires this to be at least 512 bytes.
711 * Under Linux, this is 4096 bytes.
713 rb_define_const(mSplice
, "PIPE_BUF", UINT2NUM(PIPE_BUF
));
716 * The maximum size we're allowed to splice at once. Larger
717 * sizes will be broken up and retried if the WAITALL flag or
718 * IO::Splice.copy_stream is used.
720 rb_define_const(mSplice
, "MAX_AT_ONCE", SIZET2NUM(MAX_AT_ONCE
));
722 if (can_mod_pipe_size()) {
723 rb_define_method(rb_cIO
, "pipe_size", pipe_size
, 0);
724 rb_define_method(rb_cIO
, "pipe_size=", set_pipe_size
, 1);
727 * fcntl() command constant used to return the size of a pipe.
728 * This constant is only defined when running Linux 2.6.35
729 * or later. For convenience, use IO#pipe_size instead.
731 rb_define_const(mSplice
, "F_GETPIPE_SZ",
732 UINT2NUM(F_GETPIPE_SZ
));
735 * fcntl() command constant used to set the size of a pipe.
736 * This constant is only defined when running Linux 2.6.35
737 * or later. For convenience, use IO#pipe_size= instead.
739 rb_define_const(mSplice
, "F_SETPIPE_SZ",
740 UINT2NUM(F_SETPIPE_SZ
));
743 sym_EAGAIN
= ID2SYM(rb_intern("EAGAIN"));