From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS47066 71.19.144.0/20 X-Spam-Status: No, score=-1.9 required=3.0 tests=AWL,BAYES_00, MSGID_FROM_MTA_HEADER shortcircuit=no autolearn=unavailable version=3.3.2 Path: news.gmane.org!not-for-mail From: Eric Wong Newsgroups: gmane.comp.lang.ruby.io-splice.general Subject: [PATCH] set close-on-exec in pipe size modification check Date: Sun, 21 Apr 2013 03:28:15 +0000 Message-ID: <20130421032815.GA26451@dcvr.yhbt.net> References: <20130421032815.GA26451@dcvr.yhbt.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1366514896 10464 80.91.229.3 (21 Apr 2013 03:28:16 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 21 Apr 2013 03:28:16 +0000 (UTC) To: ruby.io.splice@librelist.org Original-X-From: ruby.io.splice@librelist.org Sun Apr 21 05:28:20 2013 Return-path: Envelope-to: gclrig-ruby.io.splice@m.gmane.org In-Reply-To: <20130421032815.GA26451@dcvr.yhbt.net> List-Archive: List-Help: List-Id: List-Post: List-Subscribe: List-Unsubscribe: Precedence: list Original-Sender: ruby.io.splice@librelist.org Xref: news.gmane.org gmane.comp.lang.ruby.io-splice.general:43 Archived-At: Received: from zedshaw2.xen.prgmr.com ([71.19.156.177]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UTkwu-0006Pq-7P for gclrig-ruby.io.splice@m.gmane.org; Sun, 21 Apr 2013 05:28:20 +0200 Received: from zedshaw2.xen.prgmr.com (unknown [IPv6:::1]) by zedshaw2.xen.prgmr.com (Postfix) with ESMTP id 8E68D73DE0 for ; Sun, 21 Apr 2013 03:29:35 +0000 (UTC) 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