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