sleepy_penguin RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: sleepy-penguin@bogomips.org
Subject: [PATCH] kqueue: ensure close-on-exec flag is set
Date: Fri, 14 Apr 2017 01:54:36 +0000	[thread overview]
Message-ID: <20170414015436.18476-1-e@80x24.org> (raw)

Calling IO::new apparently does not set the close-on-exec flag
in Ruby 2.x (tested with 2.2.6 on FreeBSD), so we must set it
ourselves.

It's not a huge deal to be missing close-on-exec for kqueue,
since kqueue already has close-on-fork behavior(!) and it's rare
for a process using kqueue to exec without forking, first.
---
 ext/sleepy_penguin/kqueue.c | 6 +++++-
 test/test_kqueue.rb         | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/ext/sleepy_penguin/kqueue.c b/ext/sleepy_penguin/kqueue.c
index 430bc88..d7e8d8e 100644
--- a/ext/sleepy_penguin/kqueue.c
+++ b/ext/sleepy_penguin/kqueue.c
@@ -110,6 +110,7 @@ static VALUE s_new(VALUE klass)
 {
 	VALUE rv;
 	int fd = kqueue();
+	int flags;
 
 	if (fd < 0) {
 		/*
@@ -122,9 +123,12 @@ static VALUE s_new(VALUE klass)
 			rb_sys_fail("kqueue");
 	}
 
+	flags = fcntl(fd, F_GETFD);
+	if (flags != -1)
+		fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+
 	rv = INT2FIX(fd);
 
-	/* This will set FD_CLOEXEC on Ruby 2.0.0+: */
 	return rb_call_super(1, &rv);
 }
 
diff --git a/test/test_kqueue.rb b/test/test_kqueue.rb
index ae3203d..9d853c1 100644
--- a/test/test_kqueue.rb
+++ b/test/test_kqueue.rb
@@ -6,6 +6,7 @@ class TestKqueue < Test::Unit::TestCase
   def test_kqueue
     kq = Kqueue.new
     assert_kind_of IO, kq.to_io
+    assert_predicate kq.to_io, :close_on_exec?
     rd, wr = IO.pipe
     ev = Kevent[rd.fileno, EvFilt::READ, Ev::ADD|Ev::ONESHOT, 0, 0, rd]
     thr = Thread.new do
-- 
EW


                 reply	other threads:[~2017-04-14  1:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/sleepy_penguin/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170414015436.18476-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=sleepy-penguin@bogomips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/sleepy_penguin.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).