diff options
Diffstat (limited to 'posix/gawkmisc.c')
-rw-r--r-- | posix/gawkmisc.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/posix/gawkmisc.c b/posix/gawkmisc.c index 7fb5f1ef..6032fcdf 100644 --- a/posix/gawkmisc.c +++ b/posix/gawkmisc.c @@ -165,11 +165,30 @@ os_close_on_exec(fd, name, what, dir) int fd; const char *name, *what, *dir; { + int curflags = 0; + if (fd <= 2) /* sanity */ return; - if (fcntl(fd, F_SETFD, 1) < 0) - warning(_("%s %s `%s': could not set close-on-exec: (fcntl: %s)"), + /* + * Per POSIX, use Read/Modify/Write - get the flags, + * add FD_CLOEXEC, set the flags back. + */ + + if ((curflags = fcntl(fd, F_GETFD)) < 0) { + warning(_("%s %s `%s': could not get fd flags: (fcntl F_GETFD: %s)"), + what, dir, name, strerror(errno)); + return; + } + +#ifndef FD_CLOEXEC +#define FD_CLOEXEC 1 +#endif + + curflags |= FD_CLOEXEC; + + if (fcntl(fd, F_SETFD, curflags) < 0) + warning(_("%s %s `%s': could not set close-on-exec: (fcntl F_SETFD: %s)"), what, dir, name, strerror(errno)); } |