summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio/fdopen.c
diff options
context:
space:
mode:
authorguenther <guenther@openbsd.org>2014-08-31 02:21:18 +0000
committerguenther <guenther@openbsd.org>2014-08-31 02:21:18 +0000
commited42a740c17f7bea88482a716bacbc46a06b7249 (patch)
tree41266fb76fcd0cc0229b872cae91df94a24ad430 /lib/libc/stdio/fdopen.c
parentregen (diff)
downloadwireguard-openbsd-ed42a740c17f7bea88482a716bacbc46a06b7249.tar.xz
wireguard-openbsd-ed42a740c17f7bea88482a716bacbc46a06b7249.zip
Add additional userland interfaces for setting close-on-exec on fds
when creating them: mkostemp(), mkostemps(), the 'e' mode letter for fopen(), freopen(), fdopen(), and popen(). The close-on-exec flag will be cleared by the action created by posix_spawn_file_actions_adddup2(). Also, add support for the C11 'x' mode letter for fopen() and freopen(), setting O_EXCL when possibly creating files. Note: this requires kernel support for pipe2() and dup3()! ok millert@
Diffstat (limited to 'lib/libc/stdio/fdopen.c')
-rw-r--r--lib/libc/stdio/fdopen.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c
index 3e47f2c7426..1c0c8132fd2 100644
--- a/lib/libc/stdio/fdopen.c
+++ b/lib/libc/stdio/fdopen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fdopen.c,v 1.6 2008/04/21 12:28:35 otto Exp $ */
+/* $OpenBSD: fdopen.c,v 1.7 2014/08/31 02:21:18 guenther Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -66,6 +66,7 @@ fdopen(int fd, const char *mode)
if ((fp = __sfp()) == NULL)
return (NULL);
fp->_flags = flags;
+
/*
* If opened for appending, but underlying descriptor does not have
* O_APPEND bit set, assert __SAPP so that __swrite() will lseek to
@@ -73,6 +74,13 @@ fdopen(int fd, const char *mode)
*/
if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
fp->_flags |= __SAPP;
+
+ /*
+ * If close-on-exec was requested, then turn it on if not already
+ */
+ if ((oflags & O_CLOEXEC) && !((tmp = fcntl(fd, F_GETFD)) & FD_CLOEXEC))
+ fcntl(fd, F_SETFD, tmp | FD_CLOEXEC);
+
fp->_file = fd;
fp->_cookie = fp;
fp->_read = __sread;