xref: /freebsd/lib/libc/stdio/mktemp.3 (revision 076574744c20de3296e8f9f9e0f7ef946a78a431)
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.\"
2858f0484fSRodney W. Grimes.\"     @(#)mktemp.3	8.1 (Berkeley) 6/4/93
297f3dea24SPeter Wemm.\" $FreeBSD$
3058f0484fSRodney W. Grimes.\"
31*07657474SMark Johnston.Dd July 29, 2019
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"
49*07657474SMark Johnston.Ft int
50*07657474SMark Johnston.Fn mkostempsat "int dfd" "char *template" "int suffixlen" "int oflags"
512f253e75SWarner Losh.Ft char *
522f253e75SWarner Losh.Fn mkdtemp "char *template"
53fc0bd00fSJilles Tjoelker.In unistd.h
54fc0bd00fSJilles Tjoelker.Ft int
55fc0bd00fSJilles Tjoelker.Fn mkstemps "char *template" "int suffixlen"
5658f0484fSRodney W. Grimes.Sh DESCRIPTION
5758f0484fSRodney W. GrimesThe
5858f0484fSRodney W. Grimes.Fn mktemp
5958f0484fSRodney W. Grimesfunction
6058f0484fSRodney W. Grimestakes the given file name template and overwrites a portion of it
6158f0484fSRodney W. Grimesto create a file name.
62dc3f2db7SKris KennawayThis file name is guaranteed not to exist at the time of function invocation
63dc3f2db7SKris Kennawayand is suitable for use
6458f0484fSRodney W. Grimesby the application.
6558f0484fSRodney W. GrimesThe template may be any file name with some number of
6658f0484fSRodney W. Grimes.Ql X Ns s
6758f0484fSRodney W. Grimesappended
6858f0484fSRodney W. Grimesto it, for example
69dc3f2db7SKris Kennaway.Pa /tmp/temp.XXXXXX .
7058f0484fSRodney W. GrimesThe trailing
7158f0484fSRodney W. Grimes.Ql X Ns s
72dc3f2db7SKris Kennawayare replaced with a
73dc3f2db7SKris Kennawayunique alphanumeric combination.
7458f0484fSRodney W. GrimesThe number of unique file names
7558f0484fSRodney W. Grimes.Fn mktemp
7658f0484fSRodney W. Grimescan return depends on the number of
7758f0484fSRodney W. Grimes.Ql X Ns s
7858f0484fSRodney W. Grimesprovided; six
7958f0484fSRodney W. Grimes.Ql X Ns s
8058f0484fSRodney W. Grimeswill
8158f0484fSRodney W. Grimesresult in
8258f0484fSRodney W. Grimes.Fn mktemp
83dc3f2db7SKris Kennawayselecting one of 56800235584 (62 ** 6) possible temporary file names.
8458f0484fSRodney W. Grimes.Pp
8558f0484fSRodney W. GrimesThe
8658f0484fSRodney W. Grimes.Fn mkstemp
8758f0484fSRodney W. Grimesfunction
8858f0484fSRodney W. Grimesmakes the same replacement to the template and creates the template file,
8958f0484fSRodney W. Grimesmode 0600, returning a file descriptor opened for reading and writing.
9058f0484fSRodney W. GrimesThis avoids the race between testing for a file's existence and opening it
9158f0484fSRodney W. Grimesfor use.
922f253e75SWarner Losh.Pp
932f253e75SWarner LoshThe
9465ba8dffSJilles Tjoelker.Fn mkostemp
9565ba8dffSJilles Tjoelkerfunction
9665ba8dffSJilles Tjoelkeris like
9765ba8dffSJilles Tjoelker.Fn mkstemp
9865ba8dffSJilles Tjoelkerbut allows specifying additional
9965ba8dffSJilles Tjoelker.Xr open 2
10065ba8dffSJilles Tjoelkerflags (defined in
10165ba8dffSJilles Tjoelker.In fcntl.h ) .
10265ba8dffSJilles TjoelkerThe permitted flags are
10365ba8dffSJilles Tjoelker.Dv O_APPEND ,
10465ba8dffSJilles Tjoelker.Dv O_DIRECT ,
10565ba8dffSJilles Tjoelker.Dv O_SHLOCK ,
10665ba8dffSJilles Tjoelker.Dv O_EXLOCK ,
10765ba8dffSJilles Tjoelker.Dv O_SYNC
10865ba8dffSJilles Tjoelkerand
10965ba8dffSJilles Tjoelker.Dv O_CLOEXEC .
11065ba8dffSJilles Tjoelker.Pp
11165ba8dffSJilles TjoelkerThe
1122ee87840SWarner Losh.Fn mkstemps
11365ba8dffSJilles Tjoelkerand
11465ba8dffSJilles Tjoelker.Fn mkostemps
11565ba8dffSJilles Tjoelkerfunctions act the same as
11665ba8dffSJilles Tjoelker.Fn mkstemp
11765ba8dffSJilles Tjoelkerand
11865ba8dffSJilles Tjoelker.Fn mkostemp
11965ba8dffSJilles Tjoelkerrespectively,
12065ba8dffSJilles Tjoelkerexcept they permit a suffix to exist in the template.
12126ba999eSWarner LoshThe template should be of the form
1222ee87840SWarner Losh.Pa /tmp/tmpXXXXXXsuffix .
1231fae73b1SRuslan ErmilovThe
1242ee87840SWarner Losh.Fn mkstemps
12565ba8dffSJilles Tjoelkerand
12665ba8dffSJilles Tjoelker.Fn mkostemps
1271fae73b1SRuslan Ermilovfunction
12865ba8dffSJilles Tjoelkerare told the length of the suffix string.
1292ee87840SWarner Losh.Pp
1302ee87840SWarner LoshThe
131*07657474SMark Johnston.Fn mkostempsat
132*07657474SMark Johnstonfunction acts the same as
133*07657474SMark Johnston.Fn mkostemps
134*07657474SMark Johnstonbut takes an additional directory descriptor as a parameter.
135*07657474SMark JohnstonThe temporary file is created relative to the corresponding
136*07657474SMark Johnstondirectory, or to the current working directory if the special
137*07657474SMark Johnstonvalue
138*07657474SMark Johnston.Dv AT_FDCWD
139*07657474SMark Johnstonis specified.
140*07657474SMark JohnstonIf the template path is an absolute path, the
141*07657474SMark Johnston.Fa dfd
142*07657474SMark Johnstonparameter is ignored and the behavior is identical to
143*07657474SMark Johnston.Fn mkostemps .
144*07657474SMark Johnston.Pp
145*07657474SMark JohnstonThe
1462f253e75SWarner Losh.Fn mkdtemp
1472f253e75SWarner Loshfunction makes the same replacement to the template as in
148facc6767SRuslan Ermilov.Fn mktemp
1492f253e75SWarner Loshand creates the template directory, mode 0700.
15058f0484fSRodney W. Grimes.Sh RETURN VALUES
15158f0484fSRodney W. GrimesThe
15258f0484fSRodney W. Grimes.Fn mktemp
1532f253e75SWarner Loshand
1542f253e75SWarner Losh.Fn mkdtemp
1552f253e75SWarner Loshfunctions return a pointer to the template on success and
15658f0484fSRodney W. Grimes.Dv NULL
15758f0484fSRodney W. Grimeson failure.
15858f0484fSRodney W. GrimesThe
15965ba8dffSJilles Tjoelker.Fn mkstemp ,
16065ba8dffSJilles Tjoelker.Fn mkostemp
16126ba999eSWarner Losh.Fn mkstemps
16265ba8dffSJilles Tjoelkerand
16365ba8dffSJilles Tjoelker.Fn mkostemps
16426ba999eSWarner Loshfunctions
16526ba999eSWarner Loshreturn \-1 if no suitable file could be created.
16658f0484fSRodney W. GrimesIf either call fails an error code is placed in the global variable
16758f0484fSRodney W. Grimes.Va errno .
16858f0484fSRodney W. Grimes.Sh ERRORS
16958f0484fSRodney W. GrimesThe
17026ba999eSWarner Losh.Fn mkstemp ,
17165ba8dffSJilles Tjoelker.Fn mkostemp ,
17265ba8dffSJilles Tjoelker.Fn mkstemps ,
17365ba8dffSJilles Tjoelker.Fn mkostemps
1742f253e75SWarner Loshand
1752f253e75SWarner Losh.Fn mkdtemp
17658f0484fSRodney W. Grimesfunctions
17758f0484fSRodney W. Grimesmay set
17858f0484fSRodney W. Grimes.Va errno
17958f0484fSRodney W. Grimesto one of the following values:
18003fc6303SAlexey Zelkin.Bl -tag -width Er
18158f0484fSRodney W. Grimes.It Bq Er ENOTDIR
18258f0484fSRodney W. GrimesThe pathname portion of the template is not an existing directory.
18358f0484fSRodney W. Grimes.El
18458f0484fSRodney W. Grimes.Pp
18558f0484fSRodney W. GrimesThe
18665ba8dffSJilles Tjoelker.Fn mkostemp
18765ba8dffSJilles Tjoelkerand
18865ba8dffSJilles Tjoelker.Fn mkostemps
18965ba8dffSJilles Tjoelkerfunctions
19065ba8dffSJilles Tjoelkermay also set
19165ba8dffSJilles Tjoelker.Va errno
19265ba8dffSJilles Tjoelkerto the following value:
19365ba8dffSJilles Tjoelker.Bl -tag -width Er
19465ba8dffSJilles Tjoelker.It Bq Er EINVAL
19565ba8dffSJilles TjoelkerThe
19665ba8dffSJilles Tjoelker.Fa oflags
19765ba8dffSJilles Tjoelkerargument is invalid.
19865ba8dffSJilles Tjoelker.El
19965ba8dffSJilles Tjoelker.Pp
20065ba8dffSJilles TjoelkerThe
20126ba999eSWarner Losh.Fn mkstemp ,
20265ba8dffSJilles Tjoelker.Fn mkostemp ,
20365ba8dffSJilles Tjoelker.Fn mkstemps ,
20465ba8dffSJilles Tjoelker.Fn mkostemps
2052f253e75SWarner Loshand
2062f253e75SWarner Losh.Fn mkdtemp
20758f0484fSRodney W. Grimesfunctions
20858f0484fSRodney W. Grimesmay also set
20958f0484fSRodney W. Grimes.Va errno
21058f0484fSRodney W. Grimesto any value specified by the
21158f0484fSRodney W. Grimes.Xr stat 2
21258f0484fSRodney W. Grimesfunction.
21358f0484fSRodney W. Grimes.Pp
21458f0484fSRodney W. GrimesThe
21565ba8dffSJilles Tjoelker.Fn mkstemp ,
21665ba8dffSJilles Tjoelker.Fn mkostemp ,
21726ba999eSWarner Losh.Fn mkstemps
21865ba8dffSJilles Tjoelkerand
21965ba8dffSJilles Tjoelker.Fn mkostemps
22026ba999eSWarner Loshfunctions
22158f0484fSRodney W. Grimesmay also set
22258f0484fSRodney W. Grimes.Va errno
22358f0484fSRodney W. Grimesto any value specified by the
22458f0484fSRodney W. Grimes.Xr open 2
22558f0484fSRodney W. Grimesfunction.
2262f253e75SWarner Losh.Pp
2272f253e75SWarner LoshThe
2282f253e75SWarner Losh.Fn mkdtemp
2292f253e75SWarner Loshfunction
2302f253e75SWarner Loshmay also set
2312f253e75SWarner Losh.Va errno
2322f253e75SWarner Loshto any value specified by the
2332f253e75SWarner Losh.Xr mkdir 2
2342f253e75SWarner Loshfunction.
235d428a98dSMike Pritchard.Sh NOTES
236d428a98dSMike PritchardA common problem that results in a core dump is that the programmer
237d428a98dSMike Pritchardpasses in a read-only string to
2382f253e75SWarner Losh.Fn mktemp ,
23926ba999eSWarner Losh.Fn mkstemp ,
24026ba999eSWarner Losh.Fn mkstemps
241d428a98dSMike Pritchardor
2422f253e75SWarner Losh.Fn mkdtemp .
243d428a98dSMike PritchardThis is common with programs that were developed before
244588a200cSRuslan Ermilov.St -isoC
245417349c4SMike Pritchardcompilers were common.
246d428a98dSMike PritchardFor example, calling
247d428a98dSMike Pritchard.Fn mkstemp
248d428a98dSMike Pritchardwith an argument of
249d428a98dSMike Pritchard.Qq /tmp/tempfile.XXXXXX
250d428a98dSMike Pritchardwill result in a core dump due to
251d428a98dSMike Pritchard.Fn mkstemp
252d428a98dSMike Pritchardattempting to modify the string constant that was given.
253fc0bd00fSJilles Tjoelker.Pp
254fc0bd00fSJilles TjoelkerThe
255fc0bd00fSJilles Tjoelker.Fn mkdtemp ,
256fc0bd00fSJilles Tjoelker.Fn mkstemp
257fc0bd00fSJilles Tjoelkerand
258fc0bd00fSJilles Tjoelker.Fn mktemp
259fc0bd00fSJilles Tjoelkerfunction prototypes are also available from
260fc0bd00fSJilles Tjoelker.In unistd.h .
26124a0682cSRuslan Ermilov.Sh SEE ALSO
26224a0682cSRuslan Ermilov.Xr chmod 2 ,
26324a0682cSRuslan Ermilov.Xr getpid 2 ,
26424a0682cSRuslan Ermilov.Xr mkdir 2 ,
26524a0682cSRuslan Ermilov.Xr open 2 ,
26624a0682cSRuslan Ermilov.Xr stat 2
267fc0bd00fSJilles Tjoelker.Sh STANDARDS
268fc0bd00fSJilles TjoelkerThe
269fc0bd00fSJilles Tjoelker.Fn mkstemp
270fc0bd00fSJilles Tjoelkerand
271fc0bd00fSJilles Tjoelker.Fn mkdtemp
272fc0bd00fSJilles Tjoelkerfunctions are expected to conform to
273fc0bd00fSJilles Tjoelker.St -p1003.1-2008 .
274fc0bd00fSJilles TjoelkerThe
275fc0bd00fSJilles Tjoelker.Fn mktemp
276fc0bd00fSJilles Tjoelkerfunction is expected to conform to
277fc0bd00fSJilles Tjoelker.St -p1003.1-2001
278fc0bd00fSJilles Tjoelkerand is not specified by
279fc0bd00fSJilles Tjoelker.St -p1003.1-2008 .
280fc0bd00fSJilles TjoelkerThe
28165ba8dffSJilles Tjoelker.Fn mkostemp ,
282*07657474SMark Johnston.Fn mkstemps ,
28365ba8dffSJilles Tjoelker.Fn mkostemps
284*07657474SMark Johnstonand
285*07657474SMark 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 .
314*07657474SMark JohnstonThe
315*07657474SMark Johnston.Fn mkostempsat
316*07657474SMark Johnstonfunction appeared in
317*07657474SMark 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,
333*07657474SMark Johnston.Fn mkstemp ,
33465ba8dffSJilles Tjoelker.Fn mkostemp
335*07657474SMark Johnstonor
336*07657474SMark Johnston.Fn mkostempsat
337*07657474SMark 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