Module Name:    src
Committed By:   christos
Date:           Sun Apr 29 23:50:22 UTC 2012

Modified Files:
        src/usr.bin/mail: cmd1.c cmd2.c cmd3.c collect.c edit.c fio.c lex.c
            list.c mime_attach.c mime_child.c mime_detach.c names.c popen.c
            quit.c send.c support.c

Log Message:
set close on exec for all opened files.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/usr.bin/mail/cmd1.c
cvs rdiff -u -r1.24 -r1.25 src/usr.bin/mail/cmd2.c
cvs rdiff -u -r1.41 -r1.42 src/usr.bin/mail/cmd3.c
cvs rdiff -u -r1.44 -r1.45 src/usr.bin/mail/collect.c
cvs rdiff -u -r1.26 -r1.27 src/usr.bin/mail/edit.c src/usr.bin/mail/list.c \
    src/usr.bin/mail/popen.c
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/mail/fio.c
cvs rdiff -u -r1.40 -r1.41 src/usr.bin/mail/lex.c
cvs rdiff -u -r1.13 -r1.14 src/usr.bin/mail/mime_attach.c
cvs rdiff -u -r1.7 -r1.8 src/usr.bin/mail/mime_child.c \
    src/usr.bin/mail/mime_detach.c
cvs rdiff -u -r1.28 -r1.29 src/usr.bin/mail/names.c
cvs rdiff -u -r1.27 -r1.28 src/usr.bin/mail/quit.c
cvs rdiff -u -r1.36 -r1.37 src/usr.bin/mail/send.c
cvs rdiff -u -r1.23 -r1.24 src/usr.bin/mail/support.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/mail/cmd1.c
diff -u src/usr.bin/mail/cmd1.c:1.31 src/usr.bin/mail/cmd1.c:1.32
--- src/usr.bin/mail/cmd1.c:1.31	Fri Sep 16 11:39:27 2011
+++ src/usr.bin/mail/cmd1.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: cmd1.c,v 1.31 2011/09/16 15:39:27 joerg Exp $	*/
+/*	$NetBSD: cmd1.c,v 1.32 2012/04/29 23:50:22 christos Exp $	*/
 
 /*-
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)cmd1.c	8.2 (Berkeley) 4/20/95";
 #else
-__RCSID("$NetBSD: cmd1.c,v 1.31 2011/09/16 15:39:27 joerg Exp $");
+__RCSID("$NetBSD: cmd1.c,v 1.32 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -461,7 +461,7 @@ pipecmd(void *v)
 		goto close_pipe;
 
 	sig_check();
-	obuf = Popen(cmd, "w");
+	obuf = Popen(cmd, "we");
 	if (obuf == NULL) {
 		warn("pipecmd: Popen failed: %s", cmd);
 		return 1;

Index: src/usr.bin/mail/cmd2.c
diff -u src/usr.bin/mail/cmd2.c:1.24 src/usr.bin/mail/cmd2.c:1.25
--- src/usr.bin/mail/cmd2.c:1.24	Fri Apr 10 09:08:24 2009
+++ src/usr.bin/mail/cmd2.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: cmd2.c,v 1.24 2009/04/10 13:08:24 christos Exp $	*/
+/*	$NetBSD: cmd2.c,v 1.25 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)cmd2.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: cmd2.c,v 1.24 2009/04/10 13:08:24 christos Exp $");
+__RCSID("$NetBSD: cmd2.c,v 1.25 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -230,7 +230,7 @@ save1(char str[], int markmsg, const cha
 		disp = "[Appended]";
 	else
 		disp = "[New file]";
-	if ((obuf = Fopen(fn, "a")) == NULL) {
+	if ((obuf = Fopen(fn, "ae")) == NULL) {
 		warn(NULL);
 		return 1;
 	}

Index: src/usr.bin/mail/cmd3.c
diff -u src/usr.bin/mail/cmd3.c:1.41 src/usr.bin/mail/cmd3.c:1.42
--- src/usr.bin/mail/cmd3.c:1.41	Sat Apr 11 10:22:32 2009
+++ src/usr.bin/mail/cmd3.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: cmd3.c,v 1.41 2009/04/11 14:22:32 christos Exp $	*/
+/*	$NetBSD: cmd3.c,v 1.42 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)cmd3.c	8.2 (Berkeley) 4/20/95";
 #else
-__RCSID("$NetBSD: cmd3.c,v 1.41 2009/04/11 14:22:32 christos Exp $");
+__RCSID("$NetBSD: cmd3.c,v 1.42 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -501,7 +501,7 @@ bounce_one(int msgno, const char **smarg
 	(void)snprintf(mailtempname, sizeof(mailtempname),
 	    "%s/mail.RsXXXXXXXXXX", tmpdir);
 	if ((fd = mkstemp(mailtempname)) == -1 ||
-	    (obuf = Fdopen(fd, "w+")) == NULL) {
+	    (obuf = Fdopen(fd, "we+")) == NULL) {
 		if (fd != -1)
 			(void)close(fd);
 		warn("%s", mailtempname);

Index: src/usr.bin/mail/collect.c
diff -u src/usr.bin/mail/collect.c:1.44 src/usr.bin/mail/collect.c:1.45
--- src/usr.bin/mail/collect.c:1.44	Fri Sep 16 11:39:27 2011
+++ src/usr.bin/mail/collect.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: collect.c,v 1.44 2011/09/16 15:39:27 joerg Exp $	*/
+/*	$NetBSD: collect.c,v 1.45 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)collect.c	8.2 (Berkeley) 4/19/94";
 #else
-__RCSID("$NetBSD: collect.c,v 1.44 2011/09/16 15:39:27 joerg Exp $");
+__RCSID("$NetBSD: collect.c,v 1.45 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -97,7 +97,7 @@ exwrite(const char name[], FILE *fp, int
 		(void)fprintf(stderr, "File exists\n");
 		return -1;
 	}
-	if ((of = Fopen(name, "w")) == NULL) {
+	if ((of = Fopen(name, "we")) == NULL) {
 		warn("%s", name);
 		return -1;
 	}
@@ -165,7 +165,7 @@ mespipe(FILE *fp, char cmd[])
 	(void)snprintf(tempname, sizeof(tempname),
 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
 	if ((fd = mkstemp(tempname)) == -1 ||
-	    (nf = Fdopen(fd, "w+")) == NULL) {
+	    (nf = Fdopen(fd, "we+")) == NULL) {
 		if (fd != -1)
 			(void)close(fd);
 		warn("%s", tempname);
@@ -286,7 +286,7 @@ savedeadletter(FILE *fp)
 		return;
 	cp = getdeadletter();
 	m = umask(077);
-	dbuf = Fopen(cp, "a");
+	dbuf = Fopen(cp, "ae");
 	(void)umask(m);
 	if (dbuf == NULL)
 		return;
@@ -397,7 +397,7 @@ collect(struct header *hp, int printhead
 	(void)snprintf(mailtempname, sizeof(mailtempname),
 	    "%s/mail.RsXXXXXXXXXX", tmpdir);
 	if ((fd = mkstemp(mailtempname)) == -1 ||
-	    (collf = Fdopen(fd, "w+")) == NULL) {
+	    (collf = Fdopen(fd, "we+")) == NULL) {
 		if (fd != -1)
 			(void)close(fd);
 		warn("%s", mailtempname);
@@ -628,7 +628,7 @@ collect(struct header *hp, int printhead
 				(void)snprintf(tempname, sizeof(tempname),
 				    "%s/mail.ReXXXXXXXXXX", tmpdir);
 				if ((fd = mkstemp(tempname)) == -1 ||
-				    (fbuf = Fdopen(fd, "w+")) == NULL) {
+				    (fbuf = Fdopen(fd, "we+")) == NULL) {
 					if (fd != -1)
 						(void)close(fd);
 					warn("%s", tempname);
@@ -660,7 +660,7 @@ collect(struct header *hp, int printhead
 				(void)printf("%s: Directory\n", cp);
 				break;
 			}
-			else if ((fbuf = Fopen(cp, "r")) == NULL) {
+			else if ((fbuf = Fopen(cp, "re")) == NULL) {
 				warn("%s", cp);
 				break;
 			}

Index: src/usr.bin/mail/edit.c
diff -u src/usr.bin/mail/edit.c:1.26 src/usr.bin/mail/edit.c:1.27
--- src/usr.bin/mail/edit.c:1.26	Mon Jan 16 12:38:16 2012
+++ src/usr.bin/mail/edit.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: edit.c,v 1.26 2012/01/16 17:38:16 christos Exp $	*/
+/*	$NetBSD: edit.c,v 1.27 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)edit.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: edit.c,v 1.26 2012/01/16 17:38:16 christos Exp $");
+__RCSID("$NetBSD: edit.c,v 1.27 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -77,7 +77,7 @@ run_editor(FILE *fp, off_t size, int edi
 		(void)unlink(tempname);
 		goto out;
 	}
-	if ((nf = Fdopen(t, "w")) == NULL) {
+	if ((nf = Fdopen(t, "we")) == NULL) {
 		(void)close(t);
 		warn("%s", tempname);
 		(void)unlink(tempname);
@@ -134,7 +134,7 @@ run_editor(FILE *fp, off_t size, int edi
 	/*
 	 * Now switch to new file.
 	 */
-	if ((nf = Fopen(tempname, "a+")) == NULL) {
+	if ((nf = Fopen(tempname, "ae+")) == NULL) {
 		warn("%s", tempname);
 		(void)unlink(tempname);
 		goto out;
Index: src/usr.bin/mail/list.c
diff -u src/usr.bin/mail/list.c:1.26 src/usr.bin/mail/list.c:1.27
--- src/usr.bin/mail/list.c:1.26	Tue Jan 12 09:45:31 2010
+++ src/usr.bin/mail/list.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: list.c,v 1.26 2010/01/12 14:45:31 christos Exp $	*/
+/*	$NetBSD: list.c,v 1.27 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)list.c	8.4 (Berkeley) 5/1/95";
 #else
-__RCSID("$NetBSD: list.c,v 1.26 2010/01/12 14:45:31 christos Exp $");
+__RCSID("$NetBSD: list.c,v 1.27 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -626,7 +626,7 @@ matchbody(int (*cmpfn)(void *, char *, s
 		fp = NULL;
 		if ((fd = mkstemp(tempname)) != -1) {
 			(void)unlink(tempname);
-			if ((fp = Fdopen(fd, "w+")) == NULL)
+			if ((fp = Fdopen(fd, "we+")) == NULL)
 				(void)close(fd);
 		}
 		if (fp == NULL) {
Index: src/usr.bin/mail/popen.c
diff -u src/usr.bin/mail/popen.c:1.26 src/usr.bin/mail/popen.c:1.27
--- src/usr.bin/mail/popen.c:1.26	Tue Jan 12 09:43:31 2010
+++ src/usr.bin/mail/popen.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: popen.c,v 1.26 2010/01/12 14:43:31 christos Exp $	*/
+/*	$NetBSD: popen.c,v 1.27 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)popen.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: popen.c,v 1.26 2010/01/12 14:43:31 christos Exp $");
+__RCSID("$NetBSD: popen.c,v 1.27 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -126,10 +126,8 @@ Fopen(const char *fn, const char *mode)
 {
 	FILE *fp;
 
-	if ((fp = fopen(fn, mode)) != NULL) {
+	if ((fp = fopen(fn, mode)) != NULL)
 		register_file(fp, 0, 0);
-		(void)fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
-	}
 	return fp;
 }
 
@@ -138,10 +136,8 @@ Fdopen(int fd, const char *mode)
 {
 	FILE *fp;
 
-	if ((fp = fdopen(fd, mode)) != NULL) {
+	if ((fp = fdopen(fd, mode)) != NULL)
 		register_file(fp, 0, 0);
-		(void)fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
-	}
 	return fp;
 }
 
@@ -247,10 +243,8 @@ Popen(const char *cmd, const char *mode)
 	FILE *fp;
 	char *shellcmd;
 
-	if (pipe(p) < 0)
+	if (pipe2(p, O_CLOEXEC) < 0)
 		return NULL;
-	(void)fcntl(p[READ], F_SETFD, FD_CLOEXEC);
-	(void)fcntl(p[WRITE], F_SETFD, FD_CLOEXEC);
 	if (*mode == 'r') {
 		myside = p[READ];
 		hisside = fd0 = fd1 = p[WRITE];

Index: src/usr.bin/mail/fio.c
diff -u src/usr.bin/mail/fio.c:1.34 src/usr.bin/mail/fio.c:1.35
--- src/usr.bin/mail/fio.c:1.34	Tue Jan 12 09:45:31 2010
+++ src/usr.bin/mail/fio.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: fio.c,v 1.34 2010/01/12 14:45:31 christos Exp $	*/
+/*	$NetBSD: fio.c,v 1.35 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)fio.c	8.2 (Berkeley) 4/20/95";
 #else
-__RCSID("$NetBSD: fio.c,v 1.34 2010/01/12 14:45:31 christos Exp $");
+__RCSID("$NetBSD: fio.c,v 1.35 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -189,7 +189,7 @@ setptr(FILE *ibuf, off_t offset)
 	/* Get temporary file. */
 	(void)snprintf(linebuf, LINESIZE, "%s/mail.XXXXXX", tmpdir);
 	if ((c = mkstemp(linebuf)) == -1 ||
-	    (mestmp = Fdopen(c, "r+")) == NULL) {
+	    (mestmp = Fdopen(c, "re+")) == NULL) {
 		(void)fprintf(stderr, "mail: can't open %s\n", linebuf);
 		exit(1);
 	}

Index: src/usr.bin/mail/lex.c
diff -u src/usr.bin/mail/lex.c:1.40 src/usr.bin/mail/lex.c:1.41
--- src/usr.bin/mail/lex.c:1.40	Fri Sep 16 11:39:27 2011
+++ src/usr.bin/mail/lex.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: lex.c,v 1.40 2011/09/16 15:39:27 joerg Exp $	*/
+/*	$NetBSD: lex.c,v 1.41 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)lex.c	8.2 (Berkeley) 4/20/95";
 #else
-__RCSID("$NetBSD: lex.c,v 1.40 2011/09/16 15:39:27 joerg Exp $");
+__RCSID("$NetBSD: lex.c,v 1.41 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -173,7 +173,7 @@ setfile(const char *name)
 	if ((name = expand(name)) == NULL)
 		return -1;
 
-	if ((ibuf = Fopen(name, "r")) == NULL) {
+	if ((ibuf = Fopen(name, "re")) == NULL) {
 		if (!isedit && errno == ENOENT)
 			goto nomail;
 		warn("Can't open `%s'", name);
@@ -238,12 +238,10 @@ setfile(const char *name)
 	(void)snprintf(tempname, sizeof(tempname),
 	    "%s/mail.RxXXXXXXXXXX", tmpdir);
 	if ((fd = mkstemp(tempname)) == -1 ||
-	    (otf = fdopen(fd, "w")) == NULL)
+	    (otf = fdopen(fd, "we")) == NULL)
 		err(EXIT_FAILURE, "Can't create tmp file `%s'", tempname);
-	(void)fcntl(fileno(otf), F_SETFD, FD_CLOEXEC);
-	if ((itf = fopen(tempname, "r")) == NULL)
+	if ((itf = fopen(tempname, "re")) == NULL)
 		err(EXIT_FAILURE, "Can't create tmp file `%s'", tempname);
-	(void)fcntl(fileno(itf), F_SETFD, FD_CLOEXEC);
 	(void)rm(tempname);
 	setptr(ibuf, (off_t)0);
 	setmsize(get_abs_msgCount());
@@ -279,7 +277,7 @@ incfile(void)
 
 	omsgCount = get_abs_msgCount();
 
-	ibuf = Fopen(mailname, "r");
+	ibuf = Fopen(mailname, "re");
 	if (ibuf == NULL)
 		return -1;
 	sig_check();
@@ -449,7 +447,7 @@ setup_piping(const char *cmd, char *cmdl
 		cp++;
 
 		if (c == '|') {
-			if ((fout = Popen(cp, "w")) == NULL) {
+			if ((fout = Popen(cp, "we")) == NULL) {
 				warn("Popen: %s", cp);
 				return -1;
 			}
@@ -457,7 +455,7 @@ setup_piping(const char *cmd, char *cmdl
 		else {
 			const char *mode;
 			assert(c == '>');
-			mode = *cp == '>' ? "a" : "w";
+			mode = *cp == '>' ? "ae" : "we";
 			if (*cp == '>')
 				cp++;
 
@@ -475,7 +473,7 @@ setup_piping(const char *cmd, char *cmdl
 		if (pager == NULL || *pager == '\0')
 			pager = _PATH_MORE;
 
-		if ((fout = Popen(pager, "w")) == NULL) {
+		if ((fout = Popen(pager, "we")) == NULL) {
 			warn("Popen: %s", pager);
 			return -1;
 		}
@@ -1097,7 +1095,7 @@ load(const char *name)
 {
 	FILE *in, *oldin;
 
-	if ((in = Fopen(name, "r")) == NULL)
+	if ((in = Fopen(name, "re")) == NULL)
 		return;
 	oldin = input;
 	input = in;

Index: src/usr.bin/mail/mime_attach.c
diff -u src/usr.bin/mail/mime_attach.c:1.13 src/usr.bin/mail/mime_attach.c:1.14
--- src/usr.bin/mail/mime_attach.c:1.13	Sat Apr 11 10:22:32 2009
+++ src/usr.bin/mail/mime_attach.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: mime_attach.c,v 1.13 2009/04/11 14:22:32 christos Exp $	*/
+/*	$NetBSD: mime_attach.c,v 1.14 2012/04/29 23:50:22 christos Exp $	*/
 
 /*-
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 
 #include <sys/cdefs.h>
 #ifndef __lint__
-__RCSID("$NetBSD: mime_attach.c,v 1.13 2009/04/11 14:22:32 christos Exp $");
+__RCSID("$NetBSD: mime_attach.c,v 1.14 2012/04/29 23:50:22 christos Exp $");
 #endif /* not __lint__ */
 
 #include <assert.h>
@@ -271,7 +271,7 @@ content_encoding_by_name(const char *fil
 {
 	FILE *fp;
 	const char *enc;
-	fp = Fopen(filename, "r");
+	fp = Fopen(filename, "re");
 	if (fp == NULL) {
 		warn("content_encoding_by_name: %s", filename);
 		return MIME_TRANSFER_BASE64;	/* safe */
@@ -291,7 +291,7 @@ content_encoding_by_fileno(int fd, const
 
 	cur_pos = lseek(fd, (off_t)0, SEEK_CUR);
 	if ((fd2 = dup(fd)) == -1 ||
-	    (fp = Fdopen(fd2, "r")) == NULL) {
+	    (fp = Fdopen(fd2, "re")) == NULL) {
 		warn("content_encoding_by_fileno");
 		if (fd2 != -1)
 			(void)close(fd2);
@@ -354,7 +354,7 @@ content_type_by_name(char *filename)
 			int ch;
 
 			if (sb.st_size == 0 || filename == NULL ||
-			    (fp = Fopen(filename, "r")) == NULL)
+			    (fp = Fopen(filename, "re")) == NULL)
 				return "text/plain";
 
 			ch = fgetc(fp);
@@ -543,7 +543,7 @@ fput_attachment(FILE *fo, struct attachm
 
 	switch (ap->a_type) {
 	case ATTACH_FNAME:
-		fi = Fopen(ap->a_name, "r");
+		fi = Fopen(ap->a_name, "re");
 		if (fi == NULL)
 			err(EXIT_FAILURE, "Fopen: %s", ap->a_name);
 		break;
@@ -554,7 +554,7 @@ fput_attachment(FILE *fo, struct attachm
 		 * finished with the attachment, so the Fclose() below
 		 * is OK for now.  This will be changed in the future.
 		 */
-		fi = Fdopen(ap->a_fileno, "r");
+		fi = Fdopen(ap->a_fileno, "re");
 		if (fi == NULL)
 			err(EXIT_FAILURE, "Fdopen: %d", ap->a_fileno);
 		break;
@@ -567,7 +567,7 @@ fput_attachment(FILE *fo, struct attachm
 		(void)snprintf(mailtempname, sizeof(mailtempname),
 		    "%s/mail.RsXXXXXXXXXX", tmpdir);
 		if ((fd = mkstemp(mailtempname)) == -1 ||
-		    (fi = Fdopen(fd, "w+")) == NULL) {
+		    (fi = Fdopen(fd, "we+")) == NULL) {
 			if (fd != -1)
 				(void)close(fd);
 			err(EXIT_FAILURE, "%s", mailtempname);
@@ -612,7 +612,7 @@ mktemp_file(FILE **nfo, FILE **nfi, cons
 	(void)snprintf(tempname, sizeof(tempname), "%s/%sXXXXXXXXXX",
 	    tmpdir, hint);
 	if ((fd = mkstemp(tempname)) == -1 ||
-	    (*nfo = Fdopen(fd, "w")) == NULL) {
+	    (*nfo = Fdopen(fd, "we")) == NULL) {
 		if (fd != -1)
 			(void)close(fd);
 		warn("%s", tempname);
@@ -620,7 +620,7 @@ mktemp_file(FILE **nfo, FILE **nfi, cons
 	}
 	(void)rm(tempname);
 	if ((fd2 = dup(fd)) == -1 ||
-	    (*nfi = Fdopen(fd2, "r")) == NULL) {
+	    (*nfi = Fdopen(fd2, "re")) == NULL) {
 		warn("%s", tempname);
 		(void)Fclose(*nfo);
 		return -1;

Index: src/usr.bin/mail/mime_child.c
diff -u src/usr.bin/mail/mime_child.c:1.7 src/usr.bin/mail/mime_child.c:1.8
--- src/usr.bin/mail/mime_child.c:1.7	Mon Apr 28 16:24:14 2008
+++ src/usr.bin/mail/mime_child.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: mime_child.c,v 1.7 2008/04/28 20:24:14 martin Exp $	*/
+/*	$NetBSD: mime_child.c,v 1.8 2012/04/29 23:50:22 christos Exp $	*/
 
 /*-
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 #ifndef __lint__
-__RCSID("$NetBSD: mime_child.c,v 1.7 2008/04/28 20:24:14 martin Exp $");
+__RCSID("$NetBSD: mime_child.c,v 1.8 2012/04/29 23:50:22 christos Exp $");
 #endif /* not __lint__ */
 
 #include <assert.h>
@@ -95,11 +95,9 @@ get_cmd_flags(const char *cmd, const cha
 static int
 prepare_pipe(sigset_t *nset, int p[2])
 {
-	if (pipe(p) == -1)
+	if (pipe2(p, O_CLOEXEC) == -1)
 		return -1;
 
-	(void)fcntl(p[READ],  F_SETFD, FD_CLOEXEC);
-	(void)fcntl(p[WRITE], F_SETFD, FD_CLOEXEC);
 	/*
 	 * We _must_ ignore SIGINT and SIGPIPE or the child
 	 * will end up in our earlier handlers.
Index: src/usr.bin/mail/mime_detach.c
diff -u src/usr.bin/mail/mime_detach.c:1.7 src/usr.bin/mail/mime_detach.c:1.8
--- src/usr.bin/mail/mime_detach.c:1.7	Tue Feb 28 17:30:44 2012
+++ src/usr.bin/mail/mime_detach.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: mime_detach.c,v 1.7 2012/02/28 22:30:44 joerg Exp $	*/
+/*	$NetBSD: mime_detach.c,v 1.8 2012/04/29 23:50:22 christos Exp $	*/
 
 /*-
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 #ifndef __lint__
-__RCSID("$NetBSD: mime_detach.c,v 1.7 2012/02/28 22:30:44 joerg Exp $");
+__RCSID("$NetBSD: mime_detach.c,v 1.8 2012/04/29 23:50:22 christos Exp $");
 #endif /* not __lint__ */
 
 #include <assert.h>
@@ -125,8 +125,8 @@ detach_open_core(char *fname, const char
 
 	flags = (detach_ctl.overwrite ? 0 : O_EXCL) | O_CREAT | O_TRUNC | O_WRONLY;
 
-	if ((fd = open(fname, flags, 0600)) != -1 &&
-	    Fdopen(fd, "w") != NULL)
+	if ((fd = open(fname, flags | O_CLOEXEC, 0600)) != -1 &&
+	    Fdopen(fd, "we") != NULL)
 		return DETACH_OPEN_OK;
 
 	if (detach_ctl.ask && fd == -1 && errno == EEXIST) {
@@ -147,7 +147,7 @@ detach_open_core(char *fname, const char
 				detach_ctl.ask = 0;
 				/* FALLTHROUGH */
 		case 'o':
-			if (Fopen(fname, "w") != NULL)
+			if (Fopen(fname, "we") != NULL)
 				return DETACH_OPEN_OK;
 			break;
 

Index: src/usr.bin/mail/names.c
diff -u src/usr.bin/mail/names.c:1.28 src/usr.bin/mail/names.c:1.29
--- src/usr.bin/mail/names.c:1.28	Tue Jan 12 09:45:31 2010
+++ src/usr.bin/mail/names.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: names.c,v 1.28 2010/01/12 14:45:31 christos Exp $	*/
+/*	$NetBSD: names.c,v 1.29 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)names.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: names.c,v 1.28 2010/01/12 14:45:31 christos Exp $");
+__RCSID("$NetBSD: names.c,v 1.29 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -278,14 +278,14 @@ outof(struct name *names, FILE *fo, stru
 			(void)snprintf(tempname, sizeof(tempname),
 			    "%s/mail.ReXXXXXXXXXXXX", tmpdir);
 			if ((fd = mkstemp(tempname)) == -1 ||
-			    (fout = Fdopen(fd, "a")) == NULL) {
+			    (fout = Fdopen(fd, "ae")) == NULL) {
 				if (fd != -1)
 					(void)close(fd);
 				warn("%s", tempname);
 				senderr++;
 				goto cant;
 			}
-			image = open(tempname, O_RDWR);
+			image = open(tempname, O_RDWR | O_CLOEXEC);
 			(void)unlink(tempname);
 			if (image < 0) {
 				warn("%s", tempname);
@@ -293,7 +293,6 @@ outof(struct name *names, FILE *fo, stru
 				(void)Fclose(fout);
 				goto cant;
 			}
-			(void)fcntl(image, F_SETFD, FD_CLOEXEC);
 			(void)fprintf(fout, "From %s %s", myname, date);
 #ifdef MIME_SUPPORT
 			(void)puthead(hp, fout, GTO|GSUBJECT|GCC|GMISC|GMIME|GNL);
@@ -347,7 +346,7 @@ outof(struct name *names, FILE *fo, stru
 			free_child(pid);
 		} else {
 			int f;
-			if ((fout = Fopen(fname, "a")) == NULL) {
+			if ((fout = Fopen(fname, "ae")) == NULL) {
 				warn("%s", fname);
 				senderr++;
 				goto cant;
@@ -356,7 +355,7 @@ outof(struct name *names, FILE *fo, stru
 				warn("dup");
 				fin = NULL;
 			} else
-				fin = Fdopen(f, "r");
+				fin = Fdopen(f, "re");
 			if (fin == NULL) {
 				(void)fprintf(stderr, "Can't reopen image\n");
 				(void)Fclose(fout);

Index: src/usr.bin/mail/quit.c
diff -u src/usr.bin/mail/quit.c:1.27 src/usr.bin/mail/quit.c:1.28
--- src/usr.bin/mail/quit.c:1.27	Fri Apr 10 09:08:25 2009
+++ src/usr.bin/mail/quit.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: quit.c,v 1.27 2009/04/10 13:08:25 christos Exp $	*/
+/*	$NetBSD: quit.c,v 1.28 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)quit.c	8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: quit.c,v 1.27 2009/04/10 13:08:25 christos Exp $");
+__RCSID("$NetBSD: quit.c,v 1.28 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -79,7 +79,7 @@ writeback(FILE *res)
 	FILE *obuf;
 
 	p = 0;
-	if ((obuf = Fopen(mailname, "r+")) == NULL) {
+	if ((obuf = Fopen(mailname, "re+")) == NULL) {
 		warn("%s", mailname);
 		return -1;
 	}
@@ -150,7 +150,7 @@ edstop(jmp_buf jmpbuf)
 
 	readstat = NULL;
 	if (Tflag != NULL) {
-		if ((readstat = Fopen(Tflag, "w")) == NULL)
+		if ((readstat = Fopen(Tflag, "we")) == NULL)
 			Tflag = NULL;
 	}
 	for (mp = get_message(1), gotcha = 0; mp; mp = next_message(mp)) {
@@ -176,14 +176,14 @@ edstop(jmp_buf jmpbuf)
 		(void)snprintf(tempname, sizeof(tempname),
 		    "%s/mbox.XXXXXXXXXX", tmpdir);
 		if ((fd = mkstemp(tempname)) == -1 ||
-		    (obuf = Fdopen(fd, "w")) == NULL) {
+		    (obuf = Fdopen(fd, "we")) == NULL) {
 			warn("%s", tempname);
 			if (fd != -1)
 				(void)close(fd);
 			sig_release();
 			longjmp(jmpbuf, -11);
 		}
-		if ((ibuf = Fopen(mailname, "r")) == NULL) {
+		if ((ibuf = Fopen(mailname, "re")) == NULL) {
 			warn("%s", mailname);
 			(void)Fclose(obuf);
 			(void)rm(tempname);
@@ -204,7 +204,7 @@ edstop(jmp_buf jmpbuf)
 		}
 		(void)Fclose(ibuf);
 		(void)Fclose(obuf);
-		if ((ibuf = Fopen(tempname, "r")) == NULL) {
+		if ((ibuf = Fopen(tempname, "re")) == NULL) {
 			warn("%s", tempname);
 			(void)rm(tempname);
 			sig_release();
@@ -214,7 +214,7 @@ edstop(jmp_buf jmpbuf)
 	}
 	(void)printf("\"%s\" ", mailname);
 	(void)fflush(stdout);
-	if ((obuf = Fopen(mailname, "r+")) == NULL) {
+	if ((obuf = Fopen(mailname, "re+")) == NULL) {
 		warn("%s", mailname);
 		sig_release();
 		longjmp(jmpbuf, -1);
@@ -307,7 +307,7 @@ quit(jmp_buf jmpbuf)
 	 * a message.
 	 */
 
-	fbuf = Fopen(mailname, "r");
+	fbuf = Fopen(mailname, "re");
 	if (fbuf == NULL)
 		goto newmail;
 	if (flock(fileno(fbuf), LOCK_EX) == -1) {
@@ -324,7 +324,7 @@ nolock:
 		(void)snprintf(tempname, sizeof(tempname),
 		    "%s/mail.RqXXXXXXXXXX", tmpdir);
 		if ((fd = mkstemp(tempname)) == -1 ||
-		    (rbuf = Fdopen(fd, "w")) == NULL) {
+		    (rbuf = Fdopen(fd, "we")) == NULL) {
 		    	if (fd != -1)
 				(void)close(fd);
 			goto newmail;
@@ -351,7 +351,7 @@ nolock:
 			return;
 		}
 		(void)Fclose(rbuf);
-		if ((rbuf = Fopen(tempname, "r")) == NULL)
+		if ((rbuf = Fopen(tempname, "re")) == NULL)
 			goto newmail;
 		(void)rm(tempname);
 	}
@@ -380,7 +380,7 @@ nolock:
 	}
 	modify = 0;
 	if (Tflag != NULL) {
-		if ((readstat = Fopen(Tflag, "w")) == NULL)
+		if ((readstat = Fopen(Tflag, "we")) == NULL)
 			Tflag = NULL;
 	}
 	for (c = 0, p = 0, mp = get_message(1); mp; mp = next_message(mp)) {
@@ -430,7 +430,7 @@ nolock:
 		(void)snprintf(tempname, sizeof(tempname),
 		    "%s/mail.RmXXXXXXXXXX", tmpdir);
 		if ((fd = mkstemp(tempname)) == -1 ||
-		    (obuf = Fdopen(fd, "w")) == NULL) {
+		    (obuf = Fdopen(fd, "we")) == NULL) {
 			warn("%s", tempname);
 			if (fd != -1)
 				(void)close(fd);
@@ -438,7 +438,7 @@ nolock:
 			dot_unlock(mailname);
 			return;
 		}
-		if ((ibuf = Fopen(tempname, "r")) == NULL) {
+		if ((ibuf = Fopen(tempname, "re")) == NULL) {
 			warn("%s", tempname);
 			(void)rm(tempname);
 			(void)Fclose(obuf);
@@ -447,7 +447,7 @@ nolock:
 			return;
 		}
 		(void)rm(tempname);
-		if ((abuf = Fopen(mbox, "r")) != NULL) {
+		if ((abuf = Fopen(mbox, "re")) != NULL) {
 			while ((c = getc(abuf)) != EOF)
 				(void)putc(c, obuf);
 			(void)Fclose(abuf);
@@ -463,7 +463,7 @@ nolock:
 		(void)Fclose(obuf);
 		if ((fd = creat(mbox, 0600)) != -1)
 			(void)close(fd);
-		if ((obuf = Fopen(mbox, "r+")) == NULL) {
+		if ((obuf = Fopen(mbox, "re+")) == NULL) {
 			warn("%s", mbox);
 			(void)Fclose(ibuf);
 			(void)Fclose(fbuf);
@@ -472,7 +472,7 @@ nolock:
 		}
 	}
 	else {
-		if ((obuf = Fopen(mbox, "a")) == NULL) {
+		if ((obuf = Fopen(mbox, "ae")) == NULL) {
 			warn("%s", mbox);
 			(void)Fclose(fbuf);
 			dot_unlock(mailname);
@@ -544,7 +544,7 @@ nolock:
 
 cream:
 	if (rbuf != NULL) {
-		abuf = Fopen(mailname, "r+");
+		abuf = Fopen(mailname, "re+");
 		if (abuf == NULL)
 			goto newmail;
 		while ((c = getc(rbuf)) != EOF)

Index: src/usr.bin/mail/send.c
diff -u src/usr.bin/mail/send.c:1.36 src/usr.bin/mail/send.c:1.37
--- src/usr.bin/mail/send.c:1.36	Mon Apr 13 06:03:58 2009
+++ src/usr.bin/mail/send.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: send.c,v 1.36 2009/04/13 10:03:58 he Exp $	*/
+/*	$NetBSD: send.c,v 1.37 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)send.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: send.c,v 1.36 2009/04/13 10:03:58 he Exp $");
+__RCSID("$NetBSD: send.c,v 1.37 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -487,13 +487,13 @@ infix(struct header *hp, FILE *fi)
 	(void)snprintf(tempname, sizeof(tempname),
 	    "%s/mail.RsXXXXXXXXXX", tmpdir);
 	if ((fd = mkstemp(tempname)) == -1 ||
-	    (nfo = Fdopen(fd, "w")) == NULL) {
+	    (nfo = Fdopen(fd, "we")) == NULL) {
 		if (fd != -1)
 			(void)close(fd);
 		warn("%s", tempname);
 		return fi;
 	}
-	if ((nfi = Fopen(tempname, "r")) == NULL) {
+	if ((nfi = Fopen(tempname, "re")) == NULL) {
 		warn("%s", tempname);
 		(void)Fclose(nfo);
 		(void)rm(tempname);
@@ -553,7 +553,7 @@ savemail(const char name[], FILE *fi)
 	int afterblank;
 
 	m = umask(077);
-	fo = Fopen(name, "a");
+	fo = Fopen(name, "ae");
 	(void)umask(m);
 	if (fo == NULL) {
 		warn("%s", name);

Index: src/usr.bin/mail/support.c
diff -u src/usr.bin/mail/support.c:1.23 src/usr.bin/mail/support.c:1.24
--- src/usr.bin/mail/support.c:1.23	Tue May 24 08:33:22 2011
+++ src/usr.bin/mail/support.c	Sun Apr 29 19:50:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: support.c,v 1.23 2011/05/24 12:33:22 joerg Exp $	*/
+/*	$NetBSD: support.c,v 1.24 2012/04/29 23:50:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)aux.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: support.c,v 1.23 2011/05/24 12:33:22 joerg Exp $");
+__RCSID("$NetBSD: support.c,v 1.24 2012/04/29 23:50:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -336,7 +336,7 @@ source(void *v)
 
 	if ((cp = expand(*arglist)) == NULL)
 		return 1;
-	if ((fi = Fopen(cp, "r")) == NULL) {
+	if ((fi = Fopen(cp, "re")) == NULL) {
 		warn("%s", cp);
 		return 1;
 	}
@@ -732,7 +732,7 @@ cathelp(const char *fname)
 	FILE *f;
 	size_t len;
 
-	if ((f = Fopen(fname, "r")) == NULL) {
+	if ((f = Fopen(fname, "re")) == NULL) {
 		warn("%s", fname);
 		return;
 	}

Reply via email to