diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-31 03:07:26 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-31 03:07:26 +0000 |
commit | da5ae5544d4abd95b94df303018684847e1d88f8 (patch) | |
tree | 12ad92bf54a608c5c7ff123027728bd5d1505f59 /io.c | |
parent | bdc85361e48340b4f9d6b1039a8c338a2cb64138 (diff) | |
download | ruby-da5ae5544d4abd95b94df303018684847e1d88f8.tar.gz |
* io.c (rb_cloexec_dup2): check oldfd == newfd at first.
pointed by KOSAKI Motohiro. [ruby-dev:44713]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33579 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r-- | io.c | 35 |
1 files changed, 21 insertions, 14 deletions
@@ -239,25 +239,32 @@ rb_cloexec_dup2(int oldfd, int newfd) { int ret; + /* When oldfd == newfd, dup2 succeeds but dup3 fails with EINVAL. + * rb_cloexec_dup2 succeeds as dup2. */ + if (oldfd == newfd) { + ret = newfd; + } + else { #if defined(HAVE_DUP3) && defined(O_CLOEXEC) - static int try_dup3 = 1; - if (2 < newfd && try_dup3) { - ret = dup3(oldfd, newfd, O_CLOEXEC); - if (ret != -1) - return ret; - /* dup3 is available since Linux 2.6.27. */ - if (errno == ENOSYS) { - try_dup3 = 0; + static int try_dup3 = 1; + if (2 < newfd && try_dup3) { + ret = dup3(oldfd, newfd, O_CLOEXEC); + if (ret != -1) + return ret; + /* dup3 is available since Linux 2.6.27. */ + if (errno == ENOSYS) { + try_dup3 = 0; + ret = dup2(oldfd, newfd); + } + } + else { ret = dup2(oldfd, newfd); } - } - else { - ret = dup2(oldfd, newfd); - } #else - ret = dup2(oldfd, newfd); + ret = dup2(oldfd, newfd); #endif - if (ret == -1) return -1; + if (ret == -1) return -1; + } fd_set_cloexec(ret); return ret; } |