summaryrefslogtreecommitdiff
path: root/process.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-09 11:35:52 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-09 11:35:52 +0000
commit00478a98ac8161d9ff663a7770b5ee4ddf6495ac (patch)
treeacc3275ea341b134cd68f663a3d07741a84a44a3 /process.c
parente98d4947b71e8a3ecf6a3e4dd8542f8397ae2384 (diff)
* process.c (fd_clear_cloexec): Extracted from run_exec_dup2.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50192 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'process.c')
-rw-r--r--process.c39
1 files changed, 24 insertions, 15 deletions
diff --git a/process.c b/process.c
index ea683e7..0910599 100644
--- a/process.c
+++ b/process.c
@@ -2515,6 +2515,29 @@ run_exec_dup2_tmpbuf_size(long n)
return sizeof(struct run_exec_dup2_fd_pair) * n;
}
+/* This function should be async-signal-safe. Actually it is. */
+static int
+fd_clear_cloexec(int fd, char *errmsg, size_t errmsg_buflen)
+{
+#ifdef F_GETFD
+ int ret;
+ ret = fcntl(fd, F_GETFD); /* async-signal-safe */
+ if (ret == -1) {
+ ERRMSG("fcntl(F_GETFD)");
+ return -1;
+ }
+ if (ret & FD_CLOEXEC) {
+ ret &= ~FD_CLOEXEC;
+ ret = fcntl(fd, F_SETFD, ret); /* async-signal-safe */
+ if (ret == -1) {
+ ERRMSG("fcntl(F_SETFD)");
+ return -1;
+ }
+ }
+#endif
+ return 0;
+}
+
/* This function should be async-signal-safe when sargp is NULL. Hopefully it is. */
static int
run_exec_dup2(VALUE ary, VALUE tmpbuf, struct rb_execarg *sargp, char *errmsg, size_t errmsg_buflen)
@@ -2584,22 +2607,8 @@ run_exec_dup2(VALUE ary, VALUE tmpbuf, struct rb_execarg *sargp, char *errmsg, s
if (pairs[i].oldfd == -1)
continue;
if (pairs[i].oldfd == pairs[i].newfd) { /* self cycle */
-#ifdef F_GETFD
- int fd = pairs[i].oldfd;
- ret = fcntl(fd, F_GETFD); /* async-signal-safe */
- if (ret == -1) {
- ERRMSG("fcntl(F_GETFD)");
+ if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1) /* async-signal-safe */
goto fail;
- }
- if (ret & FD_CLOEXEC) {
- ret &= ~FD_CLOEXEC;
- ret = fcntl(fd, F_SETFD, ret); /* async-signal-safe */
- if (ret == -1) {
- ERRMSG("fcntl(F_SETFD)");
- goto fail;
- }
- }
-#endif
pairs[i].oldfd = -1;
continue;
}