]> git.saurik.com Git - apple/libc.git/blame - stdio/FreeBSD/mktemp.3
Libc-1244.1.7.tar.gz
[apple/libc.git] / stdio / FreeBSD / mktemp.3
CommitLineData
5b2abdfb
A
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.
974e3884 12.\" 3. Neither the name of the University nor the names of its contributors
5b2abdfb
A
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.\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93
5b2abdfb 29.\"
974e3884 30.Dd December 18, 2015
5b2abdfb
A
31.Dt MKTEMP 3
32.Os
33.Sh NAME
974e3884 34.Nm mktemp ,
ad3c9f2a 35.Nm mkstemp ,
974e3884 36.Nm mkdtemp ,
b061a43b 37.Nm mkdtempat_np ,
ad3c9f2a 38.Nm mkstemps ,
b061a43b 39.Nm mkstempsat_np ,
974e3884 40.Nm mkostemp ,
b061a43b
A
41.Nm mkostemps ,
42.Nm mkostempsat_np
5b2abdfb
A
43.Nd make temporary file name (unique)
44.Sh LIBRARY
45.Lb libc
46.Sh SYNOPSIS
47.In unistd.h
48.Ft char *
974e3884
A
49.Fo mktemp
50.Fa "char *template"
51.Fc
52.Ft int
53.Fo mkstemp
54.Fa "char *template"
55.Fc
56.Ft char *
ad3c9f2a
A
57.Fo mkdtemp
58.Fa "char *template"
59.Fc
b061a43b
A
60.Ft char *
61.Fo mkdtempat_np
62.Fa "int dfd"
63.Fa "char *template"
64.Fc
5b2abdfb 65.Ft int
ad3c9f2a
A
66.Fo mkstemps
67.Fa "char *template"
68.Fa "int suffixlen"
69.Fc
5b2abdfb 70.Ft int
b061a43b
A
71.Fo mkstempsat_np
72.Fa "int dfd"
73.Fa "char *template"
74.Fa "int suffixlen"
75.Fc
76.Ft int
974e3884 77.Fo mkostemp
ad3c9f2a 78.Fa "char *template"
974e3884 79.Fa "int oflags"
ad3c9f2a 80.Fc
974e3884
A
81.Ft int
82.Fo mkostemps
ad3c9f2a 83.Fa "char *template"
974e3884
A
84.Fa "int suffixlen"
85.Fa "int oflags"
ad3c9f2a 86.Fc
b061a43b
A
87.Ft int
88.Fo mkostempsat_np
89.Fa "int dfd"
90.Fa "char *template"
91.Fa "int suffixlen"
92.Fa "int oflags"
93.Fc
5b2abdfb
A
94.Sh DESCRIPTION
95The
96.Fn mktemp
97function
98takes the given file name template and overwrites a portion of it
99to create a file name.
100This file name is guaranteed not to exist at the time of function invocation
101and is suitable for use
102by the application.
103The template may be any file name with some number of
104.Ql X Ns s
105appended
106to it, for example
107.Pa /tmp/temp.XXXXXX .
108The trailing
109.Ql X Ns s
110are replaced with a
111unique alphanumeric combination.
112The number of unique file names
113.Fn mktemp
114can return depends on the number of
115.Ql X Ns s
116provided; six
117.Ql X Ns s
118will
119result in
120.Fn mktemp
121selecting one of 56800235584 (62 ** 6) possible temporary file names.
122.Pp
123The
124.Fn mkstemp
125function
126makes the same replacement to the template and creates the template file,
127mode 0600, returning a file descriptor opened for reading and writing.
128This avoids the race between testing for a file's existence and opening it
129for use.
130.Pp
131The
974e3884
A
132.Fn mkdtemp
133function makes the same replacement to the template as in
134.Fn mktemp
135and creates the template directory, mode 0700.
136.Pp
137The
138.Fn mkostemp
139function
140is like
141.Fn mkstemp
142but allows specifying additional
143.Xr open 2
144flags (defined in
145.In fcntl.h ) .
146The permitted flags are
147.Dv O_APPEND ,
148.Dv O_SHLOCK ,
149.Dv O_EXLOCK
150and
151.Dv O_CLOEXEC .
152.Pp
153The
5b2abdfb 154.Fn mkstemps
974e3884
A
155and
156.Fn mkostemps
157functions act the same as
158.Fn mkstemp
159and
160.Fn mkostemp
161respectively,
162except they permit a suffix to exist in the template.
5b2abdfb
A
163The template should be of the form
164.Pa /tmp/tmpXXXXXXsuffix .
9385eb3d 165The
5b2abdfb 166.Fn mkstemps
974e3884
A
167and
168.Fn mkostemps
169functions
170are told the length of the suffix string.
b061a43b
A
171.Pp
172The
173.Fn mkdtempat_np ,
174.Fn mkstempsat_np ,
175and
176.Fn mkostempsat_np
177functions
178act the same as
179.Fn mkdtemp ,
180.Fn mkstemps ,
181and
182.Fn mkostemps
183respectively,
184except in the case where the
185.Ar path
186specifies a relative path.
187In this case, path is treated as relative to the directory associated with the file descriptor
188.Ar fd
189instead of the current working directory.
5b2abdfb
A
190.Sh RETURN VALUES
191The
192.Fn mktemp
193and
194.Fn mkdtemp
195functions return a pointer to the template on success and
196.Dv NULL
197on failure.
198The
974e3884
A
199.Fn mkstemp ,
200.Fn mkostemp ,
5b2abdfb 201.Fn mkstemps
974e3884
A
202and
203.Fn mkostemps
5b2abdfb
A
204functions
205return \-1 if no suitable file could be created.
206If either call fails an error code is placed in the global variable
207.Va errno .
208.Sh ERRORS
209The
210.Fn mkstemp ,
974e3884
A
211.Fn mkostemp ,
212.Fn mkstemps ,
213.Fn mkostemps ,
5b2abdfb
A
214and
215.Fn mkdtemp
216functions
217may set
218.Va errno
219to one of the following values:
220.Bl -tag -width Er
221.It Bq Er ENOTDIR
222The pathname portion of the template is not an existing directory.
223.El
224.Pp
225The
974e3884
A
226.Fn mkostemp
227and
228.Fn mkostemps
229functions
230may also set
231.Va errno
232to the following value:
233.Bl -tag -width Er
234.It Bq Er EINVAL
235The
236.Fa oflags
237argument is invalid.
238.El
239.Pp
240The
5b2abdfb 241.Fn mkstemp ,
974e3884 242.Fn mkostemp ,
ad3c9f2a 243.Fn mkstemps ,
974e3884 244.Fn mkostemps
5b2abdfb
A
245and
246.Fn mkdtemp
247functions
248may also set
249.Va errno
250to any value specified by the
251.Xr stat 2
252function.
253.Pp
254The
974e3884
A
255.Fn mkstemp ,
256.Fn mkostemp ,
5b2abdfb 257.Fn mkstemps
974e3884
A
258and
259.Fn mkostemps
5b2abdfb
A
260functions
261may also set
262.Va errno
263to any value specified by the
264.Xr open 2
265function.
266.Pp
267The
268.Fn mkdtemp
269function
270may also set
271.Va errno
272to any value specified by the
273.Xr mkdir 2
274function.
275.Sh NOTES
276A common problem that results in a core dump is that the programmer
277passes in a read-only string to
278.Fn mktemp ,
279.Fn mkstemp ,
974e3884 280.Fn mkstemps
5b2abdfb
A
281or
282.Fn mkdtemp .
283This is common with programs that were developed before
284.St -isoC
285compilers were common.
286For example, calling
287.Fn mkstemp
288with an argument of
289.Qq /tmp/tempfile.XXXXXX
290will result in a core dump due to
291.Fn mkstemp
292attempting to modify the string constant that was given.
ad3c9f2a
A
293.Sh LEGACY SYNOPSIS
294.Fd #include <unistd.h>
295.Pp
296The include file
297.In unistd.h
298is necessary and sufficient for all functions.
1f2f436a
A
299.Sh SEE ALSO
300.Xr chmod 2 ,
301.Xr getpid 2 ,
302.Xr mkdir 2 ,
303.Xr open 2 ,
ad3c9f2a
A
304.Xr stat 2 ,
305.Xr compat 5
974e3884
A
306.Sh STANDARDS
307The
308.Fn mkstemp
309and
310.Fn mkdtemp
311functions are expected to conform to
312.St -p1003.1-2008 .
313The
314.Fn mktemp
315function is expected to conform to
316.St -p1003.1-2001
317and is not specified by
318.St -p1003.1-2008 .
319The
320.Fn mkostemp ,
321.Fn mkstemps
322and
323.Fn mkostemps
324functions do not conform to any standard.
1f2f436a
A
325.Sh HISTORY
326A
327.Fn mktemp
328function appeared in
329.At v7 .
330The
331.Fn mkstemp
332function appeared in
333.Bx 4.4 .
334The
335.Fn mkdtemp
336function first appeared in
337.Ox 2.2 ,
338and later in
339.Fx 3.2 .
340The
341.Fn mkstemps
342function first appeared in
343.Ox 2.4 ,
344and later in
345.Fx 3.4 .
974e3884
A
346The
347.Fn mkostemp
348and
349.Fn mkostemps
350functions appeared in OS X 10.12.
5b2abdfb
A
351.Sh BUGS
352This family of functions produces filenames which can be guessed,
353though the risk is minimized when large numbers of
354.Ql X Ns s
355are used to
356increase the number of possible temporary filenames.
357This makes the race in
358.Fn mktemp ,
359between testing for a file's existence (in the
360.Fn mktemp
361function call)
362and opening it for use
363(later in the user application)
364particularly dangerous from a security perspective.
365Whenever it is possible,
366.Fn mkstemp
974e3884
A
367or
368.Fn mkostemp
5b2abdfb
A
369should be used instead, since it does not have the race condition.
370If
371.Fn mkstemp
372cannot be used, the filename created by
373.Fn mktemp
374should be created using the
375.Dv O_EXCL
376flag to
377.Xr open 2
378and the return status of the call should be tested for failure.
379This will ensure that the program does not continue blindly
380in the event that an attacker has already created the file
381with the intention of manipulating or reading its contents.