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. 12fbbd9655SWarner 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.\" 2807657474SMark Johnston.Dd July 29, 2019 2958f0484fSRodney W. Grimes.Dt MKTEMP 3 3058f0484fSRodney W. Grimes.Os 3158f0484fSRodney W. Grimes.Sh NAME 3258f0484fSRodney W. Grimes.Nm mktemp 3358f0484fSRodney W. Grimes.Nd make temporary file name (unique) 3425bb73e0SAlexey Zelkin.Sh LIBRARY 3525bb73e0SAlexey Zelkin.Lb libc 3658f0484fSRodney W. Grimes.Sh SYNOPSIS 37fc0bd00fSJilles Tjoelker.In stdlib.h 3858f0484fSRodney W. Grimes.Ft char * 3958f0484fSRodney W. Grimes.Fn mktemp "char *template" 4058f0484fSRodney W. Grimes.Ft int 4158f0484fSRodney W. Grimes.Fn mkstemp "char *template" 4265ba8dffSJilles Tjoelker.Ft int 4365ba8dffSJilles Tjoelker.Fn mkostemp "char *template" "int oflags" 4465ba8dffSJilles Tjoelker.Ft int 4565ba8dffSJilles Tjoelker.Fn mkostemps "char *template" "int suffixlen" "int oflags" 4607657474SMark Johnston.Ft int 4707657474SMark Johnston.Fn mkostempsat "int dfd" "char *template" "int suffixlen" "int oflags" 482f253e75SWarner Losh.Ft char * 492f253e75SWarner Losh.Fn mkdtemp "char *template" 50fc0bd00fSJilles Tjoelker.In unistd.h 51fc0bd00fSJilles Tjoelker.Ft int 52fc0bd00fSJilles Tjoelker.Fn mkstemps "char *template" "int suffixlen" 5358f0484fSRodney W. Grimes.Sh DESCRIPTION 5458f0484fSRodney W. GrimesThe 5558f0484fSRodney W. Grimes.Fn mktemp 5658f0484fSRodney W. Grimesfunction 5758f0484fSRodney W. Grimestakes the given file name template and overwrites a portion of it 5858f0484fSRodney W. Grimesto create a file name. 59dc3f2db7SKris KennawayThis file name is guaranteed not to exist at the time of function invocation 60dc3f2db7SKris Kennawayand is suitable for use 6158f0484fSRodney W. Grimesby the application. 6258f0484fSRodney W. GrimesThe template may be any file name with some number of 6358f0484fSRodney W. Grimes.Ql X Ns s 6458f0484fSRodney W. Grimesappended 6558f0484fSRodney W. Grimesto it, for example 6695b17d3bSWolfram Schneider.Pa /tmp/temp.XXXXXXXXXX . 6758f0484fSRodney W. GrimesThe trailing 6858f0484fSRodney W. Grimes.Ql X Ns s 69dc3f2db7SKris Kennawayare replaced with a 70dc3f2db7SKris Kennawayunique alphanumeric combination. 7158f0484fSRodney W. GrimesThe number of unique file names 7258f0484fSRodney W. Grimes.Fn mktemp 7358f0484fSRodney W. Grimescan return depends on the number of 7458f0484fSRodney W. Grimes.Ql X Ns s 7558f0484fSRodney W. Grimesprovided; six 7658f0484fSRodney W. Grimes.Ql X Ns s 7758f0484fSRodney W. Grimeswill 7858f0484fSRodney W. Grimesresult in 7958f0484fSRodney W. Grimes.Fn mktemp 80dc3f2db7SKris Kennawayselecting one of 56800235584 (62 ** 6) possible temporary file names. 8158f0484fSRodney W. Grimes.Pp 8258f0484fSRodney W. GrimesThe 8358f0484fSRodney W. Grimes.Fn mkstemp 8458f0484fSRodney W. Grimesfunction 8558f0484fSRodney W. Grimesmakes the same replacement to the template and creates the template file, 8658f0484fSRodney W. Grimesmode 0600, returning a file descriptor opened for reading and writing. 8758f0484fSRodney W. GrimesThis avoids the race between testing for a file's existence and opening it 8858f0484fSRodney W. Grimesfor use. 892f253e75SWarner Losh.Pp 902f253e75SWarner LoshThe 9165ba8dffSJilles Tjoelker.Fn mkostemp 9265ba8dffSJilles Tjoelkerfunction 9365ba8dffSJilles Tjoelkeris like 9465ba8dffSJilles Tjoelker.Fn mkstemp 9565ba8dffSJilles Tjoelkerbut allows specifying additional 9665ba8dffSJilles Tjoelker.Xr open 2 9765ba8dffSJilles Tjoelkerflags (defined in 9865ba8dffSJilles Tjoelker.In fcntl.h ) . 9965ba8dffSJilles TjoelkerThe permitted flags are 10065ba8dffSJilles Tjoelker.Dv O_APPEND , 10165ba8dffSJilles Tjoelker.Dv O_DIRECT , 10265ba8dffSJilles Tjoelker.Dv O_SHLOCK , 10365ba8dffSJilles Tjoelker.Dv O_EXLOCK , 10465ba8dffSJilles Tjoelker.Dv O_SYNC 10565ba8dffSJilles Tjoelkerand 10665ba8dffSJilles Tjoelker.Dv O_CLOEXEC . 10765ba8dffSJilles Tjoelker.Pp 10865ba8dffSJilles TjoelkerThe 1092ee87840SWarner Losh.Fn mkstemps 11065ba8dffSJilles Tjoelkerand 11165ba8dffSJilles Tjoelker.Fn mkostemps 11265ba8dffSJilles Tjoelkerfunctions act the same as 11365ba8dffSJilles Tjoelker.Fn mkstemp 11465ba8dffSJilles Tjoelkerand 11565ba8dffSJilles Tjoelker.Fn mkostemp 11665ba8dffSJilles Tjoelkerrespectively, 11765ba8dffSJilles Tjoelkerexcept they permit a suffix to exist in the template. 11826ba999eSWarner LoshThe template should be of the form 11995b17d3bSWolfram Schneider.Pa /tmp/tmpXXXXXXXXXXsuffix . 1201fae73b1SRuslan ErmilovThe 1212ee87840SWarner Losh.Fn mkstemps 12265ba8dffSJilles Tjoelkerand 12365ba8dffSJilles Tjoelker.Fn mkostemps 1241fae73b1SRuslan Ermilovfunction 12565ba8dffSJilles Tjoelkerare told the length of the suffix string. 1262ee87840SWarner Losh.Pp 1272ee87840SWarner LoshThe 12807657474SMark Johnston.Fn mkostempsat 12907657474SMark Johnstonfunction acts the same as 13007657474SMark Johnston.Fn mkostemps 13107657474SMark Johnstonbut takes an additional directory descriptor as a parameter. 13207657474SMark JohnstonThe temporary file is created relative to the corresponding 13307657474SMark Johnstondirectory, or to the current working directory if the special 13407657474SMark Johnstonvalue 13507657474SMark Johnston.Dv AT_FDCWD 13607657474SMark Johnstonis specified. 13707657474SMark JohnstonIf the template path is an absolute path, the 13807657474SMark Johnston.Fa dfd 13907657474SMark Johnstonparameter is ignored and the behavior is identical to 14007657474SMark Johnston.Fn mkostemps . 14107657474SMark Johnston.Pp 14207657474SMark JohnstonThe 1432f253e75SWarner Losh.Fn mkdtemp 1442f253e75SWarner Loshfunction makes the same replacement to the template as in 145facc6767SRuslan Ermilov.Fn mktemp 1462f253e75SWarner Loshand creates the template directory, mode 0700. 14758f0484fSRodney W. Grimes.Sh RETURN VALUES 14858f0484fSRodney W. GrimesThe 14958f0484fSRodney W. Grimes.Fn mktemp 1502f253e75SWarner Loshand 1512f253e75SWarner Losh.Fn mkdtemp 1522f253e75SWarner Loshfunctions return a pointer to the template on success and 15358f0484fSRodney W. Grimes.Dv NULL 15458f0484fSRodney W. Grimeson failure. 15558f0484fSRodney W. GrimesThe 15665ba8dffSJilles Tjoelker.Fn mkstemp , 15765ba8dffSJilles Tjoelker.Fn mkostemp 15826ba999eSWarner Losh.Fn mkstemps 15965ba8dffSJilles Tjoelkerand 16065ba8dffSJilles Tjoelker.Fn mkostemps 16126ba999eSWarner Loshfunctions 16226ba999eSWarner Loshreturn \-1 if no suitable file could be created. 16358f0484fSRodney W. GrimesIf either call fails an error code is placed in the global variable 16458f0484fSRodney W. Grimes.Va errno . 16558f0484fSRodney W. Grimes.Sh ERRORS 16658f0484fSRodney W. GrimesThe 16726ba999eSWarner Losh.Fn mkstemp , 16865ba8dffSJilles Tjoelker.Fn mkostemp , 16965ba8dffSJilles Tjoelker.Fn mkstemps , 17065ba8dffSJilles Tjoelker.Fn mkostemps 1712f253e75SWarner Loshand 1722f253e75SWarner Losh.Fn mkdtemp 17358f0484fSRodney W. Grimesfunctions 17458f0484fSRodney W. Grimesmay set 17558f0484fSRodney W. Grimes.Va errno 17658f0484fSRodney W. Grimesto one of the following values: 17703fc6303SAlexey Zelkin.Bl -tag -width Er 17858f0484fSRodney W. Grimes.It Bq Er ENOTDIR 17958f0484fSRodney W. GrimesThe pathname portion of the template is not an existing directory. 18058f0484fSRodney W. Grimes.El 18158f0484fSRodney W. Grimes.Pp 18258f0484fSRodney W. GrimesThe 18365ba8dffSJilles Tjoelker.Fn mkostemp 18465ba8dffSJilles Tjoelkerand 18565ba8dffSJilles Tjoelker.Fn mkostemps 18665ba8dffSJilles Tjoelkerfunctions 18765ba8dffSJilles Tjoelkermay also set 18865ba8dffSJilles Tjoelker.Va errno 18965ba8dffSJilles Tjoelkerto the following value: 19065ba8dffSJilles Tjoelker.Bl -tag -width Er 19165ba8dffSJilles Tjoelker.It Bq Er EINVAL 19265ba8dffSJilles TjoelkerThe 19365ba8dffSJilles Tjoelker.Fa oflags 19465ba8dffSJilles Tjoelkerargument is invalid. 19565ba8dffSJilles Tjoelker.El 19665ba8dffSJilles Tjoelker.Pp 19765ba8dffSJilles TjoelkerThe 19826ba999eSWarner Losh.Fn mkstemp , 19965ba8dffSJilles Tjoelker.Fn mkostemp , 20065ba8dffSJilles Tjoelker.Fn mkstemps , 20165ba8dffSJilles Tjoelker.Fn mkostemps 2022f253e75SWarner Loshand 2032f253e75SWarner Losh.Fn mkdtemp 20458f0484fSRodney W. Grimesfunctions 20558f0484fSRodney W. Grimesmay also set 20658f0484fSRodney W. Grimes.Va errno 20758f0484fSRodney W. Grimesto any value specified by the 20858f0484fSRodney W. Grimes.Xr stat 2 20958f0484fSRodney W. Grimesfunction. 21058f0484fSRodney W. Grimes.Pp 21158f0484fSRodney W. GrimesThe 21265ba8dffSJilles Tjoelker.Fn mkstemp , 21365ba8dffSJilles Tjoelker.Fn mkostemp , 21426ba999eSWarner Losh.Fn mkstemps 21565ba8dffSJilles Tjoelkerand 21665ba8dffSJilles Tjoelker.Fn mkostemps 21726ba999eSWarner Loshfunctions 21858f0484fSRodney W. Grimesmay also set 21958f0484fSRodney W. Grimes.Va errno 22058f0484fSRodney W. Grimesto any value specified by the 22158f0484fSRodney W. Grimes.Xr open 2 22258f0484fSRodney W. Grimesfunction. 2232f253e75SWarner Losh.Pp 2242f253e75SWarner LoshThe 2252f253e75SWarner Losh.Fn mkdtemp 2262f253e75SWarner Loshfunction 2272f253e75SWarner Loshmay also set 2282f253e75SWarner Losh.Va errno 2292f253e75SWarner Loshto any value specified by the 2302f253e75SWarner Losh.Xr mkdir 2 2312f253e75SWarner Loshfunction. 232d428a98dSMike Pritchard.Sh NOTES 233d428a98dSMike PritchardA common problem that results in a core dump is that the programmer 234d428a98dSMike Pritchardpasses in a read-only string to 2352f253e75SWarner Losh.Fn mktemp , 23626ba999eSWarner Losh.Fn mkstemp , 23726ba999eSWarner Losh.Fn mkstemps 238d428a98dSMike Pritchardor 2392f253e75SWarner Losh.Fn mkdtemp . 240d428a98dSMike PritchardThis is common with programs that were developed before 241588a200cSRuslan Ermilov.St -isoC 242417349c4SMike Pritchardcompilers were common. 243d428a98dSMike PritchardFor example, calling 244d428a98dSMike Pritchard.Fn mkstemp 245d428a98dSMike Pritchardwith an argument of 24695b17d3bSWolfram Schneider.Qq /tmp/tempfile.XXXXXXXXXX 247d428a98dSMike Pritchardwill result in a core dump due to 248d428a98dSMike Pritchard.Fn mkstemp 249d428a98dSMike Pritchardattempting to modify the string constant that was given. 250fc0bd00fSJilles Tjoelker.Pp 251fc0bd00fSJilles TjoelkerThe 252fc0bd00fSJilles Tjoelker.Fn mkdtemp , 253fc0bd00fSJilles Tjoelker.Fn mkstemp 254fc0bd00fSJilles Tjoelkerand 255fc0bd00fSJilles Tjoelker.Fn mktemp 256fc0bd00fSJilles Tjoelkerfunction prototypes are also available from 257fc0bd00fSJilles Tjoelker.In unistd.h . 25824a0682cSRuslan Ermilov.Sh SEE ALSO 25924a0682cSRuslan Ermilov.Xr chmod 2 , 26024a0682cSRuslan Ermilov.Xr getpid 2 , 26124a0682cSRuslan Ermilov.Xr mkdir 2 , 26224a0682cSRuslan Ermilov.Xr open 2 , 26324a0682cSRuslan Ermilov.Xr stat 2 264fc0bd00fSJilles Tjoelker.Sh STANDARDS 265fc0bd00fSJilles TjoelkerThe 266fc0bd00fSJilles Tjoelker.Fn mkstemp 267fc0bd00fSJilles Tjoelkerand 268fc0bd00fSJilles Tjoelker.Fn mkdtemp 269fc0bd00fSJilles Tjoelkerfunctions are expected to conform to 270fc0bd00fSJilles Tjoelker.St -p1003.1-2008 . 271fc0bd00fSJilles TjoelkerThe 272fc0bd00fSJilles Tjoelker.Fn mktemp 273fc0bd00fSJilles Tjoelkerfunction is expected to conform to 274fc0bd00fSJilles Tjoelker.St -p1003.1-2001 275fc0bd00fSJilles Tjoelkerand is not specified by 276fc0bd00fSJilles Tjoelker.St -p1003.1-2008 . 277fc0bd00fSJilles TjoelkerThe 278*dfa0ac74SEd Maste.Fn mkostemp 279*dfa0ac74SEd Mastefunction conforms to 280*dfa0ac74SEd Maste.St -p1003.1-2024 . 281*dfa0ac74SEd MasteThe 28207657474SMark Johnston.Fn mkstemps , 28365ba8dffSJilles Tjoelker.Fn mkostemps 28407657474SMark Johnstonand 28507657474SMark Johnston.Fn mkostempsat 28665ba8dffSJilles Tjoelkerfunctions do not conform to any standard. 28724a0682cSRuslan Ermilov.Sh HISTORY 28824a0682cSRuslan ErmilovA 28924a0682cSRuslan Ermilov.Fn mktemp 29024a0682cSRuslan Ermilovfunction appeared in 29124a0682cSRuslan Ermilov.At v7 . 29224a0682cSRuslan ErmilovThe 29324a0682cSRuslan Ermilov.Fn mkstemp 29424a0682cSRuslan Ermilovfunction appeared in 29524a0682cSRuslan Ermilov.Bx 4.4 . 29624a0682cSRuslan ErmilovThe 29724a0682cSRuslan Ermilov.Fn mkdtemp 29824a0682cSRuslan Ermilovfunction first appeared in 29924a0682cSRuslan Ermilov.Ox 2.2 , 30024a0682cSRuslan Ermilovand later in 30124a0682cSRuslan Ermilov.Fx 3.2 . 30224a0682cSRuslan ErmilovThe 30324a0682cSRuslan Ermilov.Fn mkstemps 30424a0682cSRuslan Ermilovfunction first appeared in 30524a0682cSRuslan Ermilov.Ox 2.4 , 30624a0682cSRuslan Ermilovand later in 30724a0682cSRuslan Ermilov.Fx 3.4 . 30865ba8dffSJilles TjoelkerThe 30965ba8dffSJilles Tjoelker.Fn mkostemp 31065ba8dffSJilles Tjoelkerand 31165ba8dffSJilles Tjoelker.Fn mkostemps 31265ba8dffSJilles Tjoelkerfunctions appeared in 31365ba8dffSJilles Tjoelker.Fx 10.0 . 31407657474SMark JohnstonThe 31507657474SMark Johnston.Fn mkostempsat 31607657474SMark Johnstonfunction appeared in 31707657474SMark Johnston.Fx 13.0 . 3182f253e75SWarner Losh.Sh BUGS 319dc3f2db7SKris KennawayThis family of functions produces filenames which can be guessed, 320dc3f2db7SKris Kennawaythough the risk is minimized when large numbers of 321dc3f2db7SKris Kennaway.Ql X Ns s 322dc3f2db7SKris Kennawayare used to 323dc3f2db7SKris Kennawayincrease the number of possible temporary filenames. 32471207448SSheldon HearnThis makes the race in 32571207448SSheldon Hearn.Fn mktemp , 326dc3f2db7SKris Kennawaybetween testing for a file's existence (in the 327dc3f2db7SKris Kennaway.Fn mktemp 328dc3f2db7SKris Kennawayfunction call) 329dc3f2db7SKris Kennawayand opening it for use 330dc3f2db7SKris Kennaway(later in the user application) 33171207448SSheldon Hearnparticularly dangerous from a security perspective. 33271207448SSheldon HearnWhenever it is possible, 33307657474SMark Johnston.Fn mkstemp , 33465ba8dffSJilles Tjoelker.Fn mkostemp 33507657474SMark Johnstonor 33607657474SMark Johnston.Fn mkostempsat 33707657474SMark Johnstonshould be used instead, since they do not have the race condition. 338dc3f2db7SKris KennawayIf 339dc3f2db7SKris Kennaway.Fn mkstemp 340dc3f2db7SKris Kennawaycannot be used, the filename created by 341dc3f2db7SKris Kennaway.Fn mktemp 342dc3f2db7SKris Kennawayshould be created using the 343dc3f2db7SKris Kennaway.Dv O_EXCL 344dc3f2db7SKris Kennawayflag to 345dc3f2db7SKris Kennaway.Xr open 2 346dc3f2db7SKris Kennawayand the return status of the call should be tested for failure. 347dc3f2db7SKris KennawayThis will ensure that the program does not continue blindly 348dc3f2db7SKris Kennawayin the event that an attacker has already created the file 349dc3f2db7SKris Kennawaywith the intention of manipulating or reading its contents. 350