diff options
author | jilles <jilles@FreeBSD.org> | 2013-08-09 17:24:23 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2013-08-09 17:24:23 +0000 |
commit | 2895e1352cf3788606924d800c3a5c589520ea00 (patch) | |
tree | b0079b8a44c97413e3e872df791a811d58d8fcb2 /lib | |
parent | 8b37b80e65821eac7fb71791878a7af0827f61b1 (diff) | |
download | FreeBSD-src-2895e1352cf3788606924d800c3a5c589520ea00.zip FreeBSD-src-2895e1352cf3788606924d800c3a5c589520ea00.tar.gz |
Add mkostemp() and mkostemps().
These are like mkstemp() and mkstemps() but allow passing open(2) flags like
O_CLOEXEC.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/stdio/Makefile.inc | 3 | ||||
-rw-r--r-- | lib/libc/stdio/Symbol.map | 2 | ||||
-rw-r--r-- | lib/libc/stdio/mktemp.3 | 87 | ||||
-rw-r--r-- | lib/libc/stdio/mktemp.c | 35 |
4 files changed, 106 insertions, 21 deletions
diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index 0062d3d..419d031 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -60,7 +60,8 @@ MLINKS+=getc.3 fgetc.3 getc.3 getc_unlocked.3 getc.3 getchar.3 \ getc.3 getchar_unlocked.3 getc.3 getw.3 MLINKS+=getline.3 getdelim.3 MLINKS+=getwc.3 fgetwc.3 getwc.3 getwchar.3 -MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3 mktemp.3 mkstemps.3 +MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3 mktemp.3 mkstemps.3 \ + mktemp.3 mkostemp.3 mktemp.3 mkostemps.3 MLINKS+=open_memstream.3 open_wmemstream.3 MLINKS+=printf.3 asprintf.3 printf.3 dprintf.3 printf.3 fprintf.3 \ printf.3 snprintf.3 printf.3 sprintf.3 \ diff --git a/lib/libc/stdio/Symbol.map b/lib/libc/stdio/Symbol.map index 538b29a..d2a8c92 100644 --- a/lib/libc/stdio/Symbol.map +++ b/lib/libc/stdio/Symbol.map @@ -158,6 +158,8 @@ FBSD_1.3 { fmemopen; open_memstream; open_wmemstream; + mkostemp; + mkostemps; }; FBSDprivate_1.0 { diff --git a/lib/libc/stdio/mktemp.3 b/lib/libc/stdio/mktemp.3 index 6a46cf0..816a6b6 100644 --- a/lib/libc/stdio/mktemp.3 +++ b/lib/libc/stdio/mktemp.3 @@ -28,7 +28,7 @@ .\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd July 5, 2013 +.Dd August 8, 2013 .Dt MKTEMP 3 .Os .Sh NAME @@ -42,6 +42,10 @@ .Fn mktemp "char *template" .Ft int .Fn mkstemp "char *template" +.Ft int +.Fn mkostemp "char *template" "int oflags" +.Ft int +.Fn mkostemps "char *template" "int suffixlen" "int oflags" .Ft char * .Fn mkdtemp "char *template" .In unistd.h @@ -85,16 +89,41 @@ This avoids the race between testing for a file's existence and opening it for use. .Pp The +.Fn mkostemp +function +is like +.Fn mkstemp +but allows specifying additional +.Xr open 2 +flags (defined in +.In fcntl.h ) . +The permitted flags are +.Dv O_APPEND , +.Dv O_DIRECT , +.Dv O_SHLOCK , +.Dv O_EXLOCK , +.Dv O_SYNC +and +.Dv O_CLOEXEC . +.Pp +The .Fn mkstemps -function acts the same as -.Fn mkstemp , -except it permits a suffix to exist in the template. +and +.Fn mkostemps +functions act the same as +.Fn mkstemp +and +.Fn mkostemp +respectively, +except they permit a suffix to exist in the template. The template should be of the form .Pa /tmp/tmpXXXXXXsuffix . The .Fn mkstemps +and +.Fn mkostemps function -is told the length of the suffix string. +are told the length of the suffix string. .Pp The .Fn mkdtemp @@ -110,9 +139,11 @@ functions return a pointer to the template on success and .Dv NULL on failure. The -.Fn mkstemp -and +.Fn mkstemp , +.Fn mkostemp .Fn mkstemps +and +.Fn mkostemps functions return \-1 if no suitable file could be created. If either call fails an error code is placed in the global variable @@ -120,7 +151,9 @@ If either call fails an error code is placed in the global variable .Sh ERRORS The .Fn mkstemp , -.Fn mkstemps +.Fn mkostemp , +.Fn mkstemps , +.Fn mkostemps and .Fn mkdtemp functions @@ -133,8 +166,25 @@ The pathname portion of the template is not an existing directory. .El .Pp The +.Fn mkostemp +and +.Fn mkostemps +functions +may also set +.Va errno +to the following value: +.Bl -tag -width Er +.It Bq Er EINVAL +The +.Fa oflags +argument is invalid. +.El +.Pp +The .Fn mkstemp , -.Fn mkstemps +.Fn mkostemp , +.Fn mkstemps , +.Fn mkostemps and .Fn mkdtemp functions @@ -145,9 +195,11 @@ to any value specified by the function. .Pp The -.Fn mkstemp -and +.Fn mkstemp , +.Fn mkostemp , .Fn mkstemps +and +.Fn mkostemps functions may also set .Va errno @@ -209,8 +261,11 @@ function is expected to conform to and is not specified by .St -p1003.1-2008 . The +.Fn mkostemp , .Fn mkstemps -function does not conform to any standard. +and +.Fn mkostemps +functions do not conform to any standard. .Sh HISTORY A .Fn mktemp @@ -232,6 +287,12 @@ function first appeared in .Ox 2.4 , and later in .Fx 3.4 . +The +.Fn mkostemp +and +.Fn mkostemps +functions appeared in +.Fx 10.0 . .Sh BUGS This family of functions produces filenames which can be guessed, though the risk is minimized when large numbers of @@ -248,6 +309,8 @@ and opening it for use particularly dangerous from a security perspective. Whenever it is possible, .Fn mkstemp +or +.Fn mkostemp should be used instead, since it does not have the race condition. If .Fn mkstemp diff --git a/lib/libc/stdio/mktemp.c b/lib/libc/stdio/mktemp.c index 58783dd..6fc552a 100644 --- a/lib/libc/stdio/mktemp.c +++ b/lib/libc/stdio/mktemp.c @@ -47,17 +47,33 @@ __FBSDID("$FreeBSD$"); char *_mktemp(char *); -static int _gettemp(char *, int *, int, int); +static int _gettemp(char *, int *, int, int, int); static const unsigned char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int +mkostemps(char *path, int slen, int oflags) +{ + int fd; + + return (_gettemp(path, &fd, 0, slen, oflags) ? fd : -1); +} + +int mkstemps(char *path, int slen) { int fd; - return (_gettemp(path, &fd, 0, slen) ? fd : -1); + return (_gettemp(path, &fd, 0, slen, 0) ? fd : -1); +} + +int +mkostemp(char *path, int oflags) +{ + int fd; + + return (_gettemp(path, &fd, 0, 0, oflags) ? fd : -1); } int @@ -65,19 +81,19 @@ mkstemp(char *path) { int fd; - return (_gettemp(path, &fd, 0, 0) ? fd : -1); + return (_gettemp(path, &fd, 0, 0, 0) ? fd : -1); } char * mkdtemp(char *path) { - return (_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL); + return (_gettemp(path, (int *)NULL, 1, 0, 0) ? path : (char *)NULL); } char * _mktemp(char *path) { - return (_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL); + return (_gettemp(path, (int *)NULL, 0, 0, 0) ? path : (char *)NULL); } __warn_references(mktemp, @@ -90,7 +106,7 @@ mktemp(char *path) } static int -_gettemp(char *path, int *doopen, int domkdir, int slen) +_gettemp(char *path, int *doopen, int domkdir, int slen, int oflags) { char *start, *trv, *suffp, *carryp; char *pad; @@ -99,7 +115,9 @@ _gettemp(char *path, int *doopen, int domkdir, int slen) uint32_t rand; char carrybuf[MAXPATHLEN]; - if ((doopen != NULL && domkdir) || slen < 0) { + if ((doopen != NULL && domkdir) || slen < 0 || + (oflags & ~(O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC | + O_CLOEXEC)) != 0) { errno = EINVAL; return (0); } @@ -151,7 +169,8 @@ _gettemp(char *path, int *doopen, int domkdir, int slen) for (;;) { if (doopen) { if ((*doopen = - _open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0) + _open(path, O_CREAT|O_EXCL|O_RDWR|oflags, 0600)) >= + 0) return (1); if (errno != EEXIST) return (0); |