xref: /freebsd/lib/libc/stdio/mktemp.3 (revision ae07a5805b1906f29e786f415d67bef334557bd3)
1.\" Copyright (c) 1989, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd July 07, 2025
29.Dt MKTEMP 3
30.Os
31.Sh NAME
32.Nm mktemp
33.Nd make temporary file name (unique)
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In stdlib.h
38.Ft char *
39.Fn mktemp "char *template"
40.Ft int
41.Fn mkstemp "char *template"
42.Ft int
43.Fn mkostemp "char *template" "int oflags"
44.Ft int
45.Fn mkostemps "char *template" "int suffixlen" "int oflags"
46.Ft int
47.Fn mkostempsat "int dfd" "char *template" "int suffixlen" "int oflags"
48.Ft char *
49.Fn mkdtemp "char *template"
50.In unistd.h
51.Ft int
52.Fn mkstemps "char *template" "int suffixlen"
53.Sh DESCRIPTION
54The
55.Fn mktemp
56function
57takes the given file name template and overwrites a portion of it
58to create a file name.
59This file name is guaranteed not to exist at the time of function invocation
60and is suitable for use
61by the application.
62The template may be any file name with some number of
63.Ql X Ns s
64appended
65to it, for example
66.Pa /tmp/temp.XXXXXXXXXX .
67The trailing
68.Ql X Ns s
69are replaced with a
70unique alphanumeric combination.
71The number of unique file names
72.Fn mktemp
73can return depends on the number of
74.Ql X Ns s
75provided; six
76.Ql X Ns s
77will
78result in
79.Fn mktemp
80selecting one of 56800235584 (62 ** 6) possible temporary file names.
81.Pp
82The
83.Fn mkstemp
84function
85makes the same replacement to the template and creates the template file,
86mode 0600, returning a file descriptor opened for reading and writing.
87This avoids the race between testing for a file's existence and opening it
88for use.
89.Pp
90The
91.Fn mkostemp
92function
93is like
94.Fn mkstemp
95but allows specifying additional
96.Xr open 2
97flags (defined in
98.In fcntl.h ) .
99The permitted flags are
100.Dv O_APPEND ,
101.Dv O_DIRECT ,
102.Dv O_SHLOCK ,
103.Dv O_EXLOCK ,
104.Dv O_SYNC ,
105.Dv O_CLOEXEC
106and
107.Dv O_CLOFORK .
108.Pp
109The
110.Fn mkstemps
111and
112.Fn mkostemps
113functions act the same as
114.Fn mkstemp
115and
116.Fn mkostemp
117respectively,
118except they permit a suffix to exist in the template.
119The template should be of the form
120.Pa /tmp/tmpXXXXXXXXXXsuffix .
121The
122.Fn mkstemps
123and
124.Fn mkostemps
125function
126are told the length of the suffix string.
127.Pp
128The
129.Fn mkostempsat
130function acts the same as
131.Fn mkostemps
132but takes an additional directory descriptor as a parameter.
133The temporary file is created relative to the corresponding
134directory, or to the current working directory if the special
135value
136.Dv AT_FDCWD
137is specified.
138If the template path is an absolute path, the
139.Fa dfd
140parameter is ignored and the behavior is identical to
141.Fn mkostemps .
142.Pp
143The
144.Fn mkdtemp
145function makes the same replacement to the template as in
146.Fn mktemp
147and creates the template directory, mode 0700.
148.Sh RETURN VALUES
149The
150.Fn mktemp
151and
152.Fn mkdtemp
153functions return a pointer to the template on success and
154.Dv NULL
155on failure.
156The
157.Fn mkstemp ,
158.Fn mkostemp
159.Fn mkstemps
160and
161.Fn mkostemps
162functions
163return \-1 if no suitable file could be created.
164If either call fails an error code is placed in the global variable
165.Va errno .
166.Sh ERRORS
167The
168.Fn mkstemp ,
169.Fn mkostemp ,
170.Fn mkstemps ,
171.Fn mkostemps
172and
173.Fn mkdtemp
174functions
175may set
176.Va errno
177to one of the following values:
178.Bl -tag -width Er
179.It Bq Er ENOTDIR
180The pathname portion of the template is not an existing directory.
181.El
182.Pp
183The
184.Fn mkostemp
185and
186.Fn mkostemps
187functions
188may also set
189.Va errno
190to the following value:
191.Bl -tag -width Er
192.It Bq Er EINVAL
193The
194.Fa oflags
195argument is invalid.
196.El
197.Pp
198The
199.Fn mkstemp ,
200.Fn mkostemp ,
201.Fn mkstemps ,
202.Fn mkostemps
203and
204.Fn mkdtemp
205functions
206may also set
207.Va errno
208to any value specified by the
209.Xr stat 2
210function.
211.Pp
212The
213.Fn mkstemp ,
214.Fn mkostemp ,
215.Fn mkstemps
216and
217.Fn mkostemps
218functions
219may also set
220.Va errno
221to any value specified by the
222.Xr open 2
223function.
224.Pp
225The
226.Fn mkdtemp
227function
228may also set
229.Va errno
230to any value specified by the
231.Xr mkdir 2
232function.
233.Sh NOTES
234A common problem that results in a core dump is that the programmer
235passes in a read-only string to
236.Fn mktemp ,
237.Fn mkstemp ,
238.Fn mkstemps
239or
240.Fn mkdtemp .
241This is common with programs that were developed before
242.St -isoC
243compilers were common.
244For example, calling
245.Fn mkstemp
246with an argument of
247.Qq /tmp/tempfile.XXXXXXXXXX
248will result in a core dump due to
249.Fn mkstemp
250attempting to modify the string constant that was given.
251.Pp
252The
253.Fn mkdtemp ,
254.Fn mkstemp
255and
256.Fn mktemp
257function prototypes are also available from
258.In unistd.h .
259.Sh SEE ALSO
260.Xr chmod 2 ,
261.Xr getpid 2 ,
262.Xr mkdir 2 ,
263.Xr open 2 ,
264.Xr stat 2
265.Sh STANDARDS
266The
267.Fn mkstemp
268and
269.Fn mkdtemp
270functions are expected to conform to
271.St -p1003.1-2008 .
272The
273.Fn mktemp
274function is expected to conform to
275.St -p1003.1-2001
276and is not specified by
277.St -p1003.1-2008 .
278The
279.Fn mkostemp
280function conforms to
281.St -p1003.1-2024 .
282The
283.Fn mkstemps ,
284.Fn mkostemps
285and
286.Fn mkostempsat
287functions do not conform to any standard.
288.Sh HISTORY
289A
290.Fn mktemp
291function appeared in
292.At v7 .
293The
294.Fn mkstemp
295function appeared in
296.Bx 4.4 .
297The
298.Fn mkdtemp
299function first appeared in
300.Ox 2.2 ,
301and later in
302.Fx 3.2 .
303The
304.Fn mkstemps
305function first appeared in
306.Ox 2.4 ,
307and later in
308.Fx 3.4 .
309The
310.Fn mkostemp
311and
312.Fn mkostemps
313functions appeared in
314.Fx 10.0 .
315The
316.Fn mkostempsat
317function appeared in
318.Fx 13.0 .
319.Sh BUGS
320This family of functions produces filenames which can be guessed,
321though the risk is minimized when large numbers of
322.Ql X Ns s
323are used to
324increase the number of possible temporary filenames.
325This makes the race in
326.Fn mktemp ,
327between testing for a file's existence (in the
328.Fn mktemp
329function call)
330and opening it for use
331(later in the user application)
332particularly dangerous from a security perspective.
333Whenever it is possible,
334.Fn mkstemp ,
335.Fn mkostemp
336or
337.Fn mkostempsat
338should be used instead, since they do not have the race condition.
339If
340.Fn mkstemp
341cannot be used, the filename created by
342.Fn mktemp
343should be created using the
344.Dv O_EXCL
345flag to
346.Xr open 2
347and the return status of the call should be tested for failure.
348This will ensure that the program does not continue blindly
349in the event that an attacker has already created the file
350with the intention of manipulating or reading its contents.
351