[前][次][番号順一覧][スレッド一覧]

ruby-changes:31294

From: nobu <ko1@a...>
Date: Sun, 20 Oct 2013 15:41:29 +0900 (JST)
Subject: [ruby-changes:31294] nobu:r43373 (trunk): io.c: make IO#reopen("pathname") atomic

nobu	2013-10-20 15:41:24 +0900 (Sun, 20 Oct 2013)

  New Revision: 43373

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43373

  Log:
    io.c: make IO#reopen("pathname") atomic
    
    * io.c (rb_io_reopen): create a new, temporary FD via rb_sysopen and
      call rb_cloexec_dup2 on it to atomically replace the file fptr->fd
      points to.  This leaves no possible window where fptr->fd is invalid
      to userspace (even for any threads running w/o GVL).  based on the
      patch by Eric Wong <normalperson@y...> at [ruby-core:57943].
      [Bug #9036]

  Modified files:
    trunk/ChangeLog
    trunk/io.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43372)
+++ ChangeLog	(revision 43373)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Oct 20 15:41:22 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* io.c (rb_io_reopen): create a new, temporary FD via rb_sysopen and
+	  call rb_cloexec_dup2 on it to atomically replace the file fptr->fd
+	  points to.  This leaves no possible window where fptr->fd is invalid
+	  to userspace (even for any threads running w/o GVL).  based on the
+	  patch by Eric Wong <normalperson@y...> at [ruby-core:57943].
+	  [Bug #9036]
+
 Sun Oct 20 15:29:05 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* error.c (rb_syserr_fail_path_in): new function split from
Index: io.c
===================================================================
--- io.c	(revision 43372)
+++ io.c	(revision 43373)
@@ -6665,10 +6665,14 @@ rb_io_reopen(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/io.c#L6665
         }
     }
     else {
-        if (close(fptr->fd) < 0)
-            rb_sys_fail_path(fptr->pathv);
-        fptr->fd = -1;
-        fptr->fd = rb_sysopen(fptr->pathv, oflags, 0666);
+	int tmpfd = rb_sysopen(fptr->pathv, oflags, 0666);
+	int err = 0;
+	if (rb_cloexec_dup2(tmpfd, fptr->fd) < 0)
+	    err = errno;
+	(void)close(tmpfd);
+	if (err) {
+	    rb_syserr_fail_path(err, fptr->pathv);
+	}
     }
 
     return file;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]