158f0484fSRodney W. Grimes.\" Copyright (c) 1989, 1991, 1993 258f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 358f0484fSRodney W. Grimes.\" 458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 658f0484fSRodney W. Grimes.\" are met: 758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 858f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 1058f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 1158f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12*fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors 1358f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1458f0484fSRodney W. Grimes.\" without specific prior written permission. 1558f0484fSRodney W. Grimes.\" 1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2658f0484fSRodney W. Grimes.\" SUCH DAMAGE. 2758f0484fSRodney W. Grimes.\" 2858f0484fSRodney W. Grimes.\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93 297f3dea24SPeter Wemm.\" $FreeBSD$ 3058f0484fSRodney W. Grimes.\" 3165ba8dffSJilles Tjoelker.Dd August 8, 2013 3258f0484fSRodney W. Grimes.Dt MKTEMP 3 3358f0484fSRodney W. Grimes.Os 3458f0484fSRodney W. Grimes.Sh NAME 3558f0484fSRodney W. Grimes.Nm mktemp 3658f0484fSRodney W. Grimes.Nd make temporary file name (unique) 3725bb73e0SAlexey Zelkin.Sh LIBRARY 3825bb73e0SAlexey Zelkin.Lb libc 3958f0484fSRodney W. Grimes.Sh SYNOPSIS 40fc0bd00fSJilles Tjoelker.In stdlib.h 4158f0484fSRodney W. Grimes.Ft char * 4258f0484fSRodney W. Grimes.Fn mktemp "char *template" 4358f0484fSRodney W. Grimes.Ft int 4458f0484fSRodney W. Grimes.Fn mkstemp "char *template" 4565ba8dffSJilles Tjoelker.Ft int 4665ba8dffSJilles Tjoelker.Fn mkostemp "char *template" "int oflags" 4765ba8dffSJilles Tjoelker.Ft int 4865ba8dffSJilles Tjoelker.Fn mkostemps "char *template" "int suffixlen" "int oflags" 492f253e75SWarner Losh.Ft char * 502f253e75SWarner Losh.Fn mkdtemp "char *template" 51fc0bd00fSJilles Tjoelker.In unistd.h 52fc0bd00fSJilles Tjoelker.Ft int 53fc0bd00fSJilles Tjoelker.Fn mkstemps "char *template" "int suffixlen" 5458f0484fSRodney W. Grimes.Sh DESCRIPTION 5558f0484fSRodney W. GrimesThe 5658f0484fSRodney W. Grimes.Fn mktemp 5758f0484fSRodney W. Grimesfunction 5858f0484fSRodney W. Grimestakes the given file name template and overwrites a portion of it 5958f0484fSRodney W. Grimesto create a file name. 60dc3f2db7SKris KennawayThis file name is guaranteed not to exist at the time of function invocation 61dc3f2db7SKris Kennawayand is suitable for use 6258f0484fSRodney W. Grimesby the application. 6358f0484fSRodney W. GrimesThe template may be any file name with some number of 6458f0484fSRodney W. Grimes.Ql X Ns s 6558f0484fSRodney W. Grimesappended 6658f0484fSRodney W. Grimesto it, for example 67dc3f2db7SKris Kennaway.Pa /tmp/temp.XXXXXX . 6858f0484fSRodney W. GrimesThe trailing 6958f0484fSRodney W. Grimes.Ql X Ns s 70dc3f2db7SKris Kennawayare replaced with a 71dc3f2db7SKris Kennawayunique alphanumeric combination. 7258f0484fSRodney W. GrimesThe number of unique file names 7358f0484fSRodney W. Grimes.Fn mktemp 7458f0484fSRodney W. Grimescan return depends on the number of 7558f0484fSRodney W. Grimes.Ql X Ns s 7658f0484fSRodney W. Grimesprovided; six 7758f0484fSRodney W. Grimes.Ql X Ns s 7858f0484fSRodney W. Grimeswill 7958f0484fSRodney W. Grimesresult in 8058f0484fSRodney W. Grimes.Fn mktemp 81dc3f2db7SKris Kennawayselecting one of 56800235584 (62 ** 6) possible temporary file names. 8258f0484fSRodney W. Grimes.Pp 8358f0484fSRodney W. GrimesThe 8458f0484fSRodney W. Grimes.Fn mkstemp 8558f0484fSRodney W. Grimesfunction 8658f0484fSRodney W. Grimesmakes the same replacement to the template and creates the template file, 8758f0484fSRodney W. Grimesmode 0600, returning a file descriptor opened for reading and writing. 8858f0484fSRodney W. GrimesThis avoids the race between testing for a file's existence and opening it 8958f0484fSRodney W. Grimesfor use. 902f253e75SWarner Losh.Pp 912f253e75SWarner LoshThe 9265ba8dffSJilles Tjoelker.Fn mkostemp 9365ba8dffSJilles Tjoelkerfunction 9465ba8dffSJilles Tjoelkeris like 9565ba8dffSJilles Tjoelker.Fn mkstemp 9665ba8dffSJilles Tjoelkerbut allows specifying additional 9765ba8dffSJilles Tjoelker.Xr open 2 9865ba8dffSJilles Tjoelkerflags (defined in 9965ba8dffSJilles Tjoelker.In fcntl.h ) . 10065ba8dffSJilles TjoelkerThe permitted flags are 10165ba8dffSJilles Tjoelker.Dv O_APPEND , 10265ba8dffSJilles Tjoelker.Dv O_DIRECT , 10365ba8dffSJilles Tjoelker.Dv O_SHLOCK , 10465ba8dffSJilles Tjoelker.Dv O_EXLOCK , 10565ba8dffSJilles Tjoelker.Dv O_SYNC 10665ba8dffSJilles Tjoelkerand 10765ba8dffSJilles Tjoelker.Dv O_CLOEXEC . 10865ba8dffSJilles Tjoelker.Pp 10965ba8dffSJilles TjoelkerThe 1102ee87840SWarner Losh.Fn mkstemps 11165ba8dffSJilles Tjoelkerand 11265ba8dffSJilles Tjoelker.Fn mkostemps 11365ba8dffSJilles Tjoelkerfunctions act the same as 11465ba8dffSJilles Tjoelker.Fn mkstemp 11565ba8dffSJilles Tjoelkerand 11665ba8dffSJilles Tjoelker.Fn mkostemp 11765ba8dffSJilles Tjoelkerrespectively, 11865ba8dffSJilles Tjoelkerexcept they permit a suffix to exist in the template. 11926ba999eSWarner LoshThe template should be of the form 1202ee87840SWarner Losh.Pa /tmp/tmpXXXXXXsuffix . 1211fae73b1SRuslan ErmilovThe 1222ee87840SWarner Losh.Fn mkstemps 12365ba8dffSJilles Tjoelkerand 12465ba8dffSJilles Tjoelker.Fn mkostemps 1251fae73b1SRuslan Ermilovfunction 12665ba8dffSJilles Tjoelkerare told the length of the suffix string. 1272ee87840SWarner Losh.Pp 1282ee87840SWarner LoshThe 1292f253e75SWarner Losh.Fn mkdtemp 1302f253e75SWarner Loshfunction makes the same replacement to the template as in 131facc6767SRuslan Ermilov.Fn mktemp 1322f253e75SWarner Loshand creates the template directory, mode 0700. 13358f0484fSRodney W. Grimes.Sh RETURN VALUES 13458f0484fSRodney W. GrimesThe 13558f0484fSRodney W. Grimes.Fn mktemp 1362f253e75SWarner Loshand 1372f253e75SWarner Losh.Fn mkdtemp 1382f253e75SWarner Loshfunctions return a pointer to the template on success and 13958f0484fSRodney W. Grimes.Dv NULL 14058f0484fSRodney W. Grimeson failure. 14158f0484fSRodney W. GrimesThe 14265ba8dffSJilles Tjoelker.Fn mkstemp , 14365ba8dffSJilles Tjoelker.Fn mkostemp 14426ba999eSWarner Losh.Fn mkstemps 14565ba8dffSJilles Tjoelkerand 14665ba8dffSJilles Tjoelker.Fn mkostemps 14726ba999eSWarner Loshfunctions 14826ba999eSWarner Loshreturn \-1 if no suitable file could be created. 14958f0484fSRodney W. GrimesIf either call fails an error code is placed in the global variable 15058f0484fSRodney W. Grimes.Va errno . 15158f0484fSRodney W. Grimes.Sh ERRORS 15258f0484fSRodney W. GrimesThe 15326ba999eSWarner Losh.Fn mkstemp , 15465ba8dffSJilles Tjoelker.Fn mkostemp , 15565ba8dffSJilles Tjoelker.Fn mkstemps , 15665ba8dffSJilles Tjoelker.Fn mkostemps 1572f253e75SWarner Loshand 1582f253e75SWarner Losh.Fn mkdtemp 15958f0484fSRodney W. Grimesfunctions 16058f0484fSRodney W. Grimesmay set 16158f0484fSRodney W. Grimes.Va errno 16258f0484fSRodney W. Grimesto one of the following values: 16303fc6303SAlexey Zelkin.Bl -tag -width Er 16458f0484fSRodney W. Grimes.It Bq Er ENOTDIR 16558f0484fSRodney W. GrimesThe pathname portion of the template is not an existing directory. 16658f0484fSRodney W. Grimes.El 16758f0484fSRodney W. Grimes.Pp 16858f0484fSRodney W. GrimesThe 16965ba8dffSJilles Tjoelker.Fn mkostemp 17065ba8dffSJilles Tjoelkerand 17165ba8dffSJilles Tjoelker.Fn mkostemps 17265ba8dffSJilles Tjoelkerfunctions 17365ba8dffSJilles Tjoelkermay also set 17465ba8dffSJilles Tjoelker.Va errno 17565ba8dffSJilles Tjoelkerto the following value: 17665ba8dffSJilles Tjoelker.Bl -tag -width Er 17765ba8dffSJilles Tjoelker.It Bq Er EINVAL 17865ba8dffSJilles TjoelkerThe 17965ba8dffSJilles Tjoelker.Fa oflags 18065ba8dffSJilles Tjoelkerargument is invalid. 18165ba8dffSJilles Tjoelker.El 18265ba8dffSJilles Tjoelker.Pp 18365ba8dffSJilles TjoelkerThe 18426ba999eSWarner Losh.Fn mkstemp , 18565ba8dffSJilles Tjoelker.Fn mkostemp , 18665ba8dffSJilles Tjoelker.Fn mkstemps , 18765ba8dffSJilles Tjoelker.Fn mkostemps 1882f253e75SWarner Loshand 1892f253e75SWarner Losh.Fn mkdtemp 19058f0484fSRodney W. Grimesfunctions 19158f0484fSRodney W. Grimesmay also set 19258f0484fSRodney W. Grimes.Va errno 19358f0484fSRodney W. Grimesto any value specified by the 19458f0484fSRodney W. Grimes.Xr stat 2 19558f0484fSRodney W. Grimesfunction. 19658f0484fSRodney W. Grimes.Pp 19758f0484fSRodney W. GrimesThe 19865ba8dffSJilles Tjoelker.Fn mkstemp , 19965ba8dffSJilles Tjoelker.Fn mkostemp , 20026ba999eSWarner Losh.Fn mkstemps 20165ba8dffSJilles Tjoelkerand 20265ba8dffSJilles Tjoelker.Fn mkostemps 20326ba999eSWarner Loshfunctions 20458f0484fSRodney W. Grimesmay also set 20558f0484fSRodney W. Grimes.Va errno 20658f0484fSRodney W. Grimesto any value specified by the 20758f0484fSRodney W. Grimes.Xr open 2 20858f0484fSRodney W. Grimesfunction. 2092f253e75SWarner Losh.Pp 2102f253e75SWarner LoshThe 2112f253e75SWarner Losh.Fn mkdtemp 2122f253e75SWarner Loshfunction 2132f253e75SWarner Loshmay also set 2142f253e75SWarner Losh.Va errno 2152f253e75SWarner Loshto any value specified by the 2162f253e75SWarner Losh.Xr mkdir 2 2172f253e75SWarner Loshfunction. 218d428a98dSMike Pritchard.Sh NOTES 219d428a98dSMike PritchardA common problem that results in a core dump is that the programmer 220d428a98dSMike Pritchardpasses in a read-only string to 2212f253e75SWarner Losh.Fn mktemp , 22226ba999eSWarner Losh.Fn mkstemp , 22326ba999eSWarner Losh.Fn mkstemps 224d428a98dSMike Pritchardor 2252f253e75SWarner Losh.Fn mkdtemp . 226d428a98dSMike PritchardThis is common with programs that were developed before 227588a200cSRuslan Ermilov.St -isoC 228417349c4SMike Pritchardcompilers were common. 229d428a98dSMike PritchardFor example, calling 230d428a98dSMike Pritchard.Fn mkstemp 231d428a98dSMike Pritchardwith an argument of 232d428a98dSMike Pritchard.Qq /tmp/tempfile.XXXXXX 233d428a98dSMike Pritchardwill result in a core dump due to 234d428a98dSMike Pritchard.Fn mkstemp 235d428a98dSMike Pritchardattempting to modify the string constant that was given. 236fc0bd00fSJilles Tjoelker.Pp 237fc0bd00fSJilles TjoelkerThe 238fc0bd00fSJilles Tjoelker.Fn mkdtemp , 239fc0bd00fSJilles Tjoelker.Fn mkstemp 240fc0bd00fSJilles Tjoelkerand 241fc0bd00fSJilles Tjoelker.Fn mktemp 242fc0bd00fSJilles Tjoelkerfunction prototypes are also available from 243fc0bd00fSJilles Tjoelker.In unistd.h . 24424a0682cSRuslan Ermilov.Sh SEE ALSO 24524a0682cSRuslan Ermilov.Xr chmod 2 , 24624a0682cSRuslan Ermilov.Xr getpid 2 , 24724a0682cSRuslan Ermilov.Xr mkdir 2 , 24824a0682cSRuslan Ermilov.Xr open 2 , 24924a0682cSRuslan Ermilov.Xr stat 2 250fc0bd00fSJilles Tjoelker.Sh STANDARDS 251fc0bd00fSJilles TjoelkerThe 252fc0bd00fSJilles Tjoelker.Fn mkstemp 253fc0bd00fSJilles Tjoelkerand 254fc0bd00fSJilles Tjoelker.Fn mkdtemp 255fc0bd00fSJilles Tjoelkerfunctions are expected to conform to 256fc0bd00fSJilles Tjoelker.St -p1003.1-2008 . 257fc0bd00fSJilles TjoelkerThe 258fc0bd00fSJilles Tjoelker.Fn mktemp 259fc0bd00fSJilles Tjoelkerfunction is expected to conform to 260fc0bd00fSJilles Tjoelker.St -p1003.1-2001 261fc0bd00fSJilles Tjoelkerand is not specified by 262fc0bd00fSJilles Tjoelker.St -p1003.1-2008 . 263fc0bd00fSJilles TjoelkerThe 26465ba8dffSJilles Tjoelker.Fn mkostemp , 265fc0bd00fSJilles Tjoelker.Fn mkstemps 26665ba8dffSJilles Tjoelkerand 26765ba8dffSJilles Tjoelker.Fn mkostemps 26865ba8dffSJilles Tjoelkerfunctions do not conform to any standard. 26924a0682cSRuslan Ermilov.Sh HISTORY 27024a0682cSRuslan ErmilovA 27124a0682cSRuslan Ermilov.Fn mktemp 27224a0682cSRuslan Ermilovfunction appeared in 27324a0682cSRuslan Ermilov.At v7 . 27424a0682cSRuslan ErmilovThe 27524a0682cSRuslan Ermilov.Fn mkstemp 27624a0682cSRuslan Ermilovfunction appeared in 27724a0682cSRuslan Ermilov.Bx 4.4 . 27824a0682cSRuslan ErmilovThe 27924a0682cSRuslan Ermilov.Fn mkdtemp 28024a0682cSRuslan Ermilovfunction first appeared in 28124a0682cSRuslan Ermilov.Ox 2.2 , 28224a0682cSRuslan Ermilovand later in 28324a0682cSRuslan Ermilov.Fx 3.2 . 28424a0682cSRuslan ErmilovThe 28524a0682cSRuslan Ermilov.Fn mkstemps 28624a0682cSRuslan Ermilovfunction first appeared in 28724a0682cSRuslan Ermilov.Ox 2.4 , 28824a0682cSRuslan Ermilovand later in 28924a0682cSRuslan Ermilov.Fx 3.4 . 29065ba8dffSJilles TjoelkerThe 29165ba8dffSJilles Tjoelker.Fn mkostemp 29265ba8dffSJilles Tjoelkerand 29365ba8dffSJilles Tjoelker.Fn mkostemps 29465ba8dffSJilles Tjoelkerfunctions appeared in 29565ba8dffSJilles Tjoelker.Fx 10.0 . 2962f253e75SWarner Losh.Sh BUGS 297dc3f2db7SKris KennawayThis family of functions produces filenames which can be guessed, 298dc3f2db7SKris Kennawaythough the risk is minimized when large numbers of 299dc3f2db7SKris Kennaway.Ql X Ns s 300dc3f2db7SKris Kennawayare used to 301dc3f2db7SKris Kennawayincrease the number of possible temporary filenames. 30271207448SSheldon HearnThis makes the race in 30371207448SSheldon Hearn.Fn mktemp , 304dc3f2db7SKris Kennawaybetween testing for a file's existence (in the 305dc3f2db7SKris Kennaway.Fn mktemp 306dc3f2db7SKris Kennawayfunction call) 307dc3f2db7SKris Kennawayand opening it for use 308dc3f2db7SKris Kennaway(later in the user application) 30971207448SSheldon Hearnparticularly dangerous from a security perspective. 31071207448SSheldon HearnWhenever it is possible, 3112f253e75SWarner Losh.Fn mkstemp 31265ba8dffSJilles Tjoelkeror 31365ba8dffSJilles Tjoelker.Fn mkostemp 314dc3f2db7SKris Kennawayshould be used instead, since it does not have the race condition. 315dc3f2db7SKris KennawayIf 316dc3f2db7SKris Kennaway.Fn mkstemp 317dc3f2db7SKris Kennawaycannot be used, the filename created by 318dc3f2db7SKris Kennaway.Fn mktemp 319dc3f2db7SKris Kennawayshould be created using the 320dc3f2db7SKris Kennaway.Dv O_EXCL 321dc3f2db7SKris Kennawayflag to 322dc3f2db7SKris Kennaway.Xr open 2 323dc3f2db7SKris Kennawayand the return status of the call should be tested for failure. 324dc3f2db7SKris KennawayThis will ensure that the program does not continue blindly 325dc3f2db7SKris Kennawayin the event that an attacker has already created the file 326dc3f2db7SKris Kennawaywith the intention of manipulating or reading its contents. 327