CLOEXEC support in fdopen, freopen, and mkostemp/mkostemps.
Change-Id: I74ea88e0d4973d6ab3c57da7d8bb643c31592b14
This commit is contained in:
@@ -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;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: freopen.c,v 1.13 2009/11/09 00:18:27 kurt Exp $ */
|
||||
/* $OpenBSD: freopen.c,v 1.14 2014/08/31 02:21:18 guenther Exp $ */
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
@@ -134,7 +134,7 @@ freopen(const char *file, const char *mode, FILE *fp)
|
||||
* assume stderr is always fd STDERR_FILENO, even if being freopen'd.
|
||||
*/
|
||||
if (wantfd >= 0 && f != wantfd) {
|
||||
if (dup2(f, wantfd) >= 0) {
|
||||
if (dup3(f, wantfd, oflags & O_CLOEXEC) >= 0) {
|
||||
(void) close(f);
|
||||
f = wantfd;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: mktemp.c,v 1.33 2014/05/06 22:55:27 millert Exp $ */
|
||||
/* $OpenBSD: mktemp.c,v 1.34 2014/08/31 02:21:18 guenther Exp $ */
|
||||
/*
|
||||
* Copyright (c) 1996-1998, 2008 Theo de Raadt
|
||||
* Copyright (c) 1997, 2008-2009 Todd C. Miller
|
||||
@@ -35,12 +35,14 @@
|
||||
#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
|
||||
#define MIN_X 6
|
||||
|
||||
#define MKOTEMP_FLAGS (O_APPEND | O_CLOEXEC | O_DSYNC | O_RSYNC | O_SYNC)
|
||||
|
||||
#ifndef nitems
|
||||
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
|
||||
#endif
|
||||
|
||||
static int
|
||||
mktemp_internal(char *path, int slen, int mode)
|
||||
mktemp_internal(char *path, int slen, int mode, int flags)
|
||||
{
|
||||
char *start, *cp, *ep;
|
||||
const char *tempchars = TEMPCHARS;
|
||||
@@ -63,6 +65,12 @@ mktemp_internal(char *path, int slen, int mode)
|
||||
return(-1);
|
||||
}
|
||||
|
||||
if (flags & ~MKOTEMP_FLAGS) {
|
||||
errno = EINVAL;
|
||||
return(-1);
|
||||
}
|
||||
flags |= O_CREAT | O_EXCL | O_RDWR;
|
||||
|
||||
tries = INT_MAX;
|
||||
do {
|
||||
cp = start;
|
||||
@@ -85,7 +93,7 @@ mktemp_internal(char *path, int slen, int mode)
|
||||
return(errno == ENOENT ? 0 : -1);
|
||||
break;
|
||||
case MKTEMP_FILE:
|
||||
fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
|
||||
fd = open(path, flags, S_IRUSR|S_IWUSR);
|
||||
if (fd != -1 || errno != EEXIST)
|
||||
return(fd);
|
||||
break;
|
||||
@@ -107,7 +115,7 @@ char *_mktemp(char *);
|
||||
char *
|
||||
_mktemp(char *path)
|
||||
{
|
||||
if (mktemp_internal(path, 0, MKTEMP_NAME) == -1)
|
||||
if (mktemp_internal(path, 0, MKTEMP_NAME, 0) == -1)
|
||||
return(NULL);
|
||||
return(path);
|
||||
}
|
||||
@@ -121,16 +129,28 @@ mktemp(char *path)
|
||||
return(_mktemp(path));
|
||||
}
|
||||
|
||||
int
|
||||
mkostemps(char *path, int slen, int flags)
|
||||
{
|
||||
return(mktemp_internal(path, slen, MKTEMP_FILE, flags));
|
||||
}
|
||||
|
||||
int
|
||||
mkstemp(char *path)
|
||||
{
|
||||
return(mktemp_internal(path, 0, MKTEMP_FILE));
|
||||
return(mktemp_internal(path, 0, MKTEMP_FILE, 0));
|
||||
}
|
||||
|
||||
int
|
||||
mkostemp(char *path, int flags)
|
||||
{
|
||||
return(mktemp_internal(path, 0, MKTEMP_FILE, flags));
|
||||
}
|
||||
|
||||
int
|
||||
mkstemps(char *path, int slen)
|
||||
{
|
||||
return(mktemp_internal(path, slen, MKTEMP_FILE));
|
||||
return(mktemp_internal(path, slen, MKTEMP_FILE, 0));
|
||||
}
|
||||
|
||||
char *
|
||||
@@ -138,6 +158,6 @@ mkdtemp(char *path)
|
||||
{
|
||||
int error;
|
||||
|
||||
error = mktemp_internal(path, 0, MKTEMP_DIR);
|
||||
error = mktemp_internal(path, 0, MKTEMP_DIR, 0);
|
||||
return(error ? NULL : path);
|
||||
}
|
||||
|
Reference in New Issue
Block a user