io_splice RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: ruby.io.splice@librelist.org
Subject: [PATCH] set close-on-exec in pipe size modification check
Date: Sun, 21 Apr 2013 03:28:15 +0000	[thread overview]
Message-ID: <20130421032815.GA26451@dcvr.yhbt.net> (raw)
In-Reply-To: <20130421032815.GA26451@dcvr.yhbt.net>

This prevents a file descriptor leak in case another thread
forks and execs while we are in the middle of loading.  This
places a hard dependency on pipe2, but pipe2 appeared in Linux
before pipe size modification.
---
 ext/io_splice/extconf.rb      |  1 +
 ext/io_splice/io_splice_ext.c | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/ext/io_splice/extconf.rb b/ext/io_splice/extconf.rb
index 27127c8..52dab83 100644
--- a/ext/io_splice/extconf.rb
+++ b/ext/io_splice/extconf.rb
@@ -3,6 +3,7 @@
 
 have_func('splice', %w(fcntl.h)) or abort 'splice(2) not defined'
 have_func('tee', %w(fcntl.h)) or abort 'tee(2) not defined'
+have_func('pipe2', %w(fcntl.h unistd.h))
 have_func('rb_thread_blocking_region')
 have_macro('F_GETPIPE_SZ', %w(fcntl.h))
 have_macro('F_SETPIPE_SZ', %w(fcntl.h))
diff --git a/ext/io_splice/io_splice_ext.c b/ext/io_splice/io_splice_ext.c
index e429266..1c938c3 100644
--- a/ext/io_splice/io_splice_ext.c
+++ b/ext/io_splice/io_splice_ext.c
@@ -619,8 +619,14 @@ static VALUE set_pipe_size(VALUE self, VALUE size)
 
 static int can_mod_pipe_size(void)
 {
+	/*
+	 * pipe2 appeared in Linux 2.6.27, F_*PIPE_SZ appeared in 2.6.35,
+	 * thus not having pipe2 automatically disqualifies us from having
+	 * F_*PIPE_SZ support
+	 */
+#ifdef HAVE_PIPE2
 	int fds[2];
-	int rc = pipe(fds);
+	int rc = pipe2(fds, O_CLOEXEC);
 
 	if (rc == 0) {
 		rc = fcntl(fds[0], F_GETPIPE_SZ);
@@ -629,11 +635,17 @@ static int can_mod_pipe_size(void)
 		(void)close(fds[0]);
 		(void)close(fds[1]);
 	} else {
-		/* weird error, but don't raise during init */
+		/*
+		 * weird error, but don't raise during init, this could be
+		 * ENOSYS, even..
+		 */
 		rc = 0;
 	}
 	errno = 0;
 	return rc;
+#else  /* ! HAVE_PIPE2 */
+	return 0;
+#endif /* ! HAVE_PIPE2 */
 }
 
 void Init_io_splice_ext(void)
-- 
Eric Wong



           reply	other threads:[~2013-04-21  3:28 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <20130421032815.GA26451@dcvr.yhbt.net>]

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/ruby_io_splice/

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

  git send-email \
    --in-reply-to=20130421032815.GA26451@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=ruby.io.splice@librelist.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/ruby_io_splice.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).