From 39a1ee098a93d3f7b3a91dd03acf16a1883393d5 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Wed, 31 Jul 2013 14:39:03 -0700 Subject: [PATCH] sh: Fix compilation for older DragonFly versions. O_CLOEXEC and F_DUPFD_CLOEXEC were added in the past year, and since sh is a bootstrap tool, older versions of DragonFly won't be able to compile this latest version. Reviewed-by: swildner --- bin/sh/input.c | 11 +++++++++++ bin/sh/jobs.c | 19 +++++++++++++++++++ bin/sh/main.c | 4 ++++ bin/sh/redir.c | 8 ++++++++ 4 files changed, 42 insertions(+) diff --git a/bin/sh/input.c b/bin/sh/input.c index 811eaaf68c..f470748b40 100644 --- a/bin/sh/input.c +++ b/bin/sh/input.c @@ -389,10 +389,18 @@ setinputfile(const char *fname, int push) int fd2; INTOFF; +#ifndef O_CLOEXEC + if ((fd = open(fname, O_RDONLY)) < 0) +#else if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0) +#endif error("cannot open %s: %s", fname, strerror(errno)); if (fd < 10) { +#ifndef F_DUPFD_CLOEXEC + fd2 = fcntl(fd, F_DUPFD, 10); +#else fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10); +#endif close(fd); if (fd2 < 0) error("Out of file descriptors"); @@ -411,6 +419,9 @@ setinputfile(const char *fname, int push) void setinputfd(int fd, int push) { +#if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC) + fcntl(fd, F_SETFD, FD_CLOEXEC); +#endif if (push) { pushfile(); parsefile->buf = ckmalloc(BUFSIZ + 1); diff --git a/bin/sh/jobs.c b/bin/sh/jobs.c index a5b818332d..4ce9064087 100644 --- a/bin/sh/jobs.c +++ b/bin/sh/jobs.c @@ -123,12 +123,20 @@ setjobctl(int on) if (on) { if (ttyfd != -1) close(ttyfd); +#ifndef O_CLOEXEC + if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) { +#else if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) { +#endif i = 0; while (i <= 2 && !isatty(i)) i++; if (i > 2 || +#ifndef F_DUPFD_CLOEXEC + (ttyfd = fcntl(i, F_DUPFD, 10)) < 0) +#else (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) +#endif goto out; } if (ttyfd < 10) { @@ -136,7 +144,11 @@ setjobctl(int on) * Keep our TTY file descriptor out of the way of * the user's redirections. */ +#ifndef F_DUPFD_CLOEXEC + if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) { +#else if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) { +#endif close(ttyfd); ttyfd = -1; goto out; @@ -144,6 +156,13 @@ setjobctl(int on) close(ttyfd); ttyfd = i; } +#if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC) + if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) { + close(ttyfd); + ttyfd = -1; + goto out; + } +#endif do { /* while we are in the background */ initialpgrp = tcgetpgrp(ttyfd); if (initialpgrp < 0) { diff --git a/bin/sh/main.c b/bin/sh/main.c index 04e06ae499..ced93f3aa1 100644 --- a/bin/sh/main.c +++ b/bin/sh/main.c @@ -247,7 +247,11 @@ read_profile(const char *name) if (expandedname == NULL) return; INTOFF; +#ifndef O_CLOEXEC + if ((fd = open(expandedname, O_RDONLY)) >= 0) +#else if ((fd = open(expandedname, O_RDONLY | O_CLOEXEC)) >= 0) +#endif setinputfd(fd, 1); INTON; if (fd < 0) diff --git a/bin/sh/redir.c b/bin/sh/redir.c index 04b5477f25..7c03c36f7f 100644 --- a/bin/sh/redir.c +++ b/bin/sh/redir.c @@ -115,7 +115,11 @@ redirect(union node *redir, int flags) if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { INTOFF; +#ifndef F_DUPFD_CLOEXEC + if ((i = fcntl(fd, F_DUPFD, 10)) == -1) { +#else if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) { +#endif switch (errno) { case EBADF: i = CLOSED; @@ -126,6 +130,10 @@ redirect(union node *redir, int flags) break; } } +#if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC) + else + fcntl(i, F_SETFD, FD_CLOEXEC); +#endif sv->renamed[fd] = i; INTON; } -- 2.41.0