aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-20 06:41:24 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-20 06:41:24 +0000
commitde004e3a5cbaf4ae586e493a2b6911f463ffe6e3 (patch)
tree97c1414aaab4fc0a825e19ebe6c1c4ac884194b4
parentc6b9f2913a66e9c9d1746b3ba3564858dd5eb5b3 (diff)
downloadruby-de004e3a5cbaf4ae586e493a2b6911f463ffe6e3.tar.gz
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@yhbt.net> at [ruby-core:57943]. [Bug #9036] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog9
-rw-r--r--io.c12
2 files changed, 17 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 168c89f85f..4f45a6e0ca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Sun Oct 20 15:41:22 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@yhbt.net> at [ruby-core:57943].
+ [Bug #9036]
+
Sun Oct 20 15:29:05 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* error.c (rb_syserr_fail_path_in): new function split from
diff --git a/io.c b/io.c
index 47b86ed240..9ad7bff972 100644
--- a/io.c
+++ b/io.c
@@ -6665,10 +6665,14 @@ rb_io_reopen(int argc, VALUE *argv, VALUE file)
}
}
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;