From d281d4ca0580eb413ca9c7549622762ac856ffb4 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 28 May 2023 16:49:15 +0900 Subject: [PATCH 1/3] Remove usage of IO internals. --- ext/io/nonblock/extconf.rb | 2 ++ ext/io/nonblock/nonblock.c | 57 +++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/ext/io/nonblock/extconf.rb b/ext/io/nonblock/extconf.rb index d813a01..5321593 100644 --- a/ext/io/nonblock/extconf.rb +++ b/ext/io/nonblock/extconf.rb @@ -2,6 +2,8 @@ require 'mkmf' target = "io/nonblock" +have_func("rb_io_descriptor") + hdr = %w"fcntl.h" if have_macro("O_NONBLOCK", hdr) and (have_macro("F_GETFL", hdr) or have_macro("F_SETFL", hdr)) diff --git a/ext/io/nonblock/nonblock.c b/ext/io/nonblock/nonblock.c index b8a40ff..cdcf791 100644 --- a/ext/io/nonblock/nonblock.c +++ b/ext/io/nonblock/nonblock.c @@ -17,6 +17,16 @@ #endif #include +#ifndef HAVE_RB_IO_DESCRIPTOR +static int +rb_io_descriptor(VALUE io) +{ + rb_io_t *fptr; + GetOpenFile(io, fptr); + return fptr->fd; +} +#endif + #ifdef F_GETFL static int get_fcntl_flags(int fd) @@ -39,10 +49,8 @@ get_fcntl_flags(int fd) static VALUE rb_io_nonblock_p(VALUE io) { - rb_io_t *fptr; - GetOpenFile(io, fptr); - if (get_fcntl_flags(fptr->fd) & O_NONBLOCK) - return Qtrue; + if (get_fcntl_flags(rb_io_descriptor(io)) & O_NONBLOCK) + return Qtrue; return Qfalse; } #else @@ -122,15 +130,13 @@ io_nonblock_set(int fd, int f, int nb) * */ static VALUE -rb_io_nonblock_set(VALUE io, VALUE nb) +rb_io_nonblock_set(VALUE self, VALUE value) { - rb_io_t *fptr; - GetOpenFile(io, fptr); - if (RTEST(nb)) - rb_io_set_nonblock(fptr); - else - io_nonblock_set(fptr->fd, get_fcntl_flags(fptr->fd), RTEST(nb)); - return io; + int descriptor = rb_io_descriptor(self); + + io_nonblock_set(rb_io_descriptor(descriptor), get_fcntl_flags(descriptor), RTEST(value)); + + return self; } static VALUE @@ -152,24 +158,25 @@ io_nonblock_restore(VALUE arg) * The original mode is restored after the block is executed. */ static VALUE -rb_io_nonblock_block(int argc, VALUE *argv, VALUE io) +rb_io_nonblock_block(int argc, VALUE *argv, VALUE self) { int nb = 1; - rb_io_t *fptr; - int f, restore[2]; - GetOpenFile(io, fptr); + int descriptor = rb_io_descriptor(self); + if (argc > 0) { - VALUE v; - rb_scan_args(argc, argv, "01", &v); - nb = RTEST(v); + VALUE v; + rb_scan_args(argc, argv, "01", &v); + nb = RTEST(v); } - f = get_fcntl_flags(fptr->fd); - restore[0] = fptr->fd; - restore[1] = f; - if (!io_nonblock_set(fptr->fd, f, nb)) - return rb_yield(io); - return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore); + + int current_flags = get_fcntl_flags(descriptor); + int restore[2] = {descriptor, current_flags}; + + if (!io_nonblock_set(descriptor, current_flags, nb)) + return rb_yield(self); + + return rb_ensure(rb_yield, self, io_nonblock_restore, (VALUE)restore); } #else #define rb_io_nonblock_set rb_f_notimplement From c872fd1497299918d2524575e273f5ba99889d51 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 28 May 2023 17:23:41 +0900 Subject: [PATCH 2/3] Restore compatibility path. --- ext/io/nonblock/nonblock.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ext/io/nonblock/nonblock.c b/ext/io/nonblock/nonblock.c index cdcf791..a49eef8 100644 --- a/ext/io/nonblock/nonblock.c +++ b/ext/io/nonblock/nonblock.c @@ -132,9 +132,15 @@ io_nonblock_set(int fd, int f, int nb) static VALUE rb_io_nonblock_set(VALUE self, VALUE value) { - int descriptor = rb_io_descriptor(self); - - io_nonblock_set(rb_io_descriptor(descriptor), get_fcntl_flags(descriptor), RTEST(value)); + if (RTEST(value)) { + rb_io_t *fptr; + GetOpenFile(self, fptr); + rb_io_set_nonblock(fptr); + } + else { + int descriptor = rb_io_descriptor(self); + io_nonblock_set(rb_io_descriptor(descriptor), get_fcntl_flags(descriptor), RTEST(value)); + } return self; } From eaf7d5995318edd19537cd039de041b436c258b1 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 28 May 2023 17:38:01 +0900 Subject: [PATCH 3/3] Fix bug. --- ext/io/nonblock/nonblock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/io/nonblock/nonblock.c b/ext/io/nonblock/nonblock.c index a49eef8..048c3aa 100644 --- a/ext/io/nonblock/nonblock.c +++ b/ext/io/nonblock/nonblock.c @@ -139,7 +139,7 @@ rb_io_nonblock_set(VALUE self, VALUE value) } else { int descriptor = rb_io_descriptor(self); - io_nonblock_set(rb_io_descriptor(descriptor), get_fcntl_flags(descriptor), RTEST(value)); + io_nonblock_set(descriptor, get_fcntl_flags(descriptor), RTEST(value)); } return self;