changeset 14015:e5fc7d0d7c24

3687 fopen() O_CLOEXEC support via the "e" flag Reviewed by Robert Mustacchi <rm@joyent.com> Reviewed by Richard Lowe <richlowe@richlowe.net> Approved by Dan McDonald <danmcd@nexenta.com>
author Theo Schlossnagle <jesus@omniti.com>
date Wed, 17 Apr 2013 10:28:26 -0400
parents 626936c65627
children 9d68e7a6a0c3
files usr/src/lib/libc/port/stdio/_endopen.c usr/src/man/man3c/fopen.3c
diffstat 2 files changed, 43 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/lib/libc/port/stdio/_endopen.c	Fri Apr 12 17:49:42 2013 -0400
+++ b/usr/src/lib/libc/port/stdio/_endopen.c	Wed Apr 17 10:28:26 2013 -0400
@@ -27,7 +27,7 @@
 /*	Copyright (c) 1988 AT&T	*/
 /*	  All Rights Reserved  	*/
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
+/* Copyright (c) 2013 OmniTI Computer Consulting, Inc. All rights reserved. */
 
 /*
  *	This routine is a special case, in that it is aware of
@@ -55,8 +55,8 @@
 FILE *
 _endopen(const char *name, const char *type, FILE *iop, int largefile)
 {
-	int oflag, fd, fflag;
-	char plus;
+	int oflag, fd, fflag, eflag, plusflag;
+	const char *echr;
 
 	if (iop == NULL)
 		return (NULL);
@@ -77,10 +77,27 @@
 		fflag = _IOWRT;
 		break;
 	}
-	/* UNIX ignores 'b' and treats text and binary the same */
-	if ((plus = type[1]) == 'b')
-		plus = type[2];
-	if (plus == '+') {
+
+	plusflag = 0;
+	eflag = 0;
+	for (echr = type + 1; *echr != '\0'; echr++) {
+		switch (*echr) {
+		/* UNIX ignores 'b' and treats text and binary the same */
+		default:
+			break;
+		case '+':
+			plusflag = 1;
+			break;
+		case 'e':
+			eflag = 1;
+			break;
+		}
+	}
+	if (eflag) {
+		/* Subsequent to a mode flag, 'e' indicates O_CLOEXEC */
+		oflag = oflag | O_CLOEXEC;
+	}
+	if (plusflag) {
 		oflag = (oflag & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
 		fflag = _IORW;
 	}
--- a/usr/src/man/man3c/fopen.3c	Fri Apr 12 17:49:42 2013 -0400
+++ b/usr/src/man/man3c/fopen.3c	Wed Apr 17 10:28:26 2013 -0400
@@ -2,6 +2,7 @@
 .\" Copyright 1989 AT&T.
 .\" Copyright (c) 2006, Sun Microsystems, Inc.  All Rights Reserved.
 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
+.\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc.  All Rights Reserved.
 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
 .\" http://www.opengroup.org/bookstore/.
 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
@@ -28,11 +29,11 @@
 .sp
 .LP
 The argument \fImode\fR points to a string beginning with one of the following
-sequences:
+base sequences:
 .sp
 .ne 2
 .na
-\fB\fBr\fR or \fBrb\fR\fR
+\fB\fBr\fR\fR 
 .ad
 .RS 20n
 Open file for reading.
@@ -41,7 +42,7 @@
 .sp
 .ne 2
 .na
-\fB\fBw\fR or \fBwb\fR\fR
+\fB\fBw\fR\fR
 .ad
 .RS 20n
 Truncate to  zero  length or create file for writing.
@@ -50,7 +51,7 @@
 .sp
 .ne 2
 .na
-\fB\fBa\fR or \fBab\fR\fR
+\fB\fBa\fR\fR
 .ad
 .RS 20n
 Append; open or create file for writing at end-of-file.
@@ -59,7 +60,7 @@
 .sp
 .ne 2
 .na
-\fB\fBr+\fR or \fBrb+\fR or \fBr+b\fR\fR
+\fB\fBr+\fR\fR
 .ad
 .RS 20n
 Open file for update (reading and writing).
@@ -68,7 +69,7 @@
 .sp
 .ne 2
 .na
-\fB\fBw+\fR or \fBwb+\fR or \fBw+b\fR\fR
+\fB\fBw+\fR\fR
 .ad
 .RS 20n
 Truncate to zero length or create file for update.
@@ -77,7 +78,7 @@
 .sp
 .ne 2
 .na
-\fB\fBa+\fR or \fBab+\fR or \fBa+b\fR\fR
+\fB\fBa+\fR\fR
 .ad
 .RS 20n
 Append; open or create file for update, writing at end-of-file.
@@ -85,12 +86,22 @@
 
 .sp
 .LP
+In addition to the base sequences for the \fImode\fR argument above, two
+additional flags are supported via the \fBb\fR character and the \fBe\fR
+character.  Order of these additional flags (including the \fB+\fR) does
+not matter.
+.sp
+.LP
 The character \fBb\fR has no effect, but is allowed for ISO C standard
 conformance (see \fBstandards\fR(5)). Opening a file with read mode (\fBr\fR as
 the first character in the \fImode\fR argument) fails if the file does not
 exist or cannot be read.
 .sp
 .LP
+The character \fBe\fR will cause the underlying file descriptor to be
+opened with the O_CLOEXEC flag as described in \fBopen\fR(2).
+.sp
+.LP
 Opening a file with append mode (\fBa\fR as the first character in the
 \fImode\fR argument) causes all subsequent writes to the file to be forced to
 the then current end-of-file, regardless of intervening calls to
@@ -392,4 +403,4 @@
 .LP
 \fBenable_extended_FILE_stdio\fR(3C), \fBfclose\fR(3C), \fBfdopen\fR(3C),
 \fBfflush\fR(3C), \fBfreopen\fR(3C), \fBfsetpos\fR(3C), \fBrewind\fR(3C),
-\fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5)
+\fBopen\fR(2), \fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5)