2014-11-10 10:33:31 +03:00
|
|
|
.\" $NetBSD: mktemp.1,v 1.22 2014/11/10 07:33:31 snj Exp $
|
1999-09-05 00:36:15 +04:00
|
|
|
.\" From: $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
|
1999-09-03 00:46:17 +04:00
|
|
|
.\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $
|
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 1989, 1991, 1993
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
.\" 3. Neither the name of the University nor the names of its contributors
|
1999-09-03 00:46:17 +04:00
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
|
|
|
|
.\"
|
2012-11-04 14:10:03 +04:00
|
|
|
.Dd November 4, 2012
|
1999-09-03 00:46:17 +04:00
|
|
|
.Dt MKTEMP 1
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm mktemp
|
|
|
|
.Nd make temporary file name (unique)
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Nm mktemp
|
2006-09-19 23:00:17 +04:00
|
|
|
.Op Fl dqu
|
2009-08-16 00:02:28 +04:00
|
|
|
.Op Fl p Ar tmpdir
|
2006-09-19 18:22:51 +04:00
|
|
|
.Bro
|
2006-09-19 23:01:44 +04:00
|
|
|
.Fl t Ar prefix
|
2006-09-19 18:22:51 +04:00
|
|
|
.No |
|
|
|
|
.Ar template ...
|
|
|
|
.Brc
|
1999-09-03 00:46:17 +04:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Nm
|
2012-11-04 14:10:03 +04:00
|
|
|
utility
|
|
|
|
is provided to allow shell scripts to safely use temporary files.
|
|
|
|
It creates temporary files or directories using unique names,
|
|
|
|
and prints the names.
|
|
|
|
.Pp
|
|
|
|
The name of each temporary file or directory is derived from a
|
|
|
|
template that includes several trailing
|
|
|
|
.Ql X
|
|
|
|
characters, such as
|
|
|
|
.Pa /tmp/prefix.XXXX .
|
1999-09-03 00:46:17 +04:00
|
|
|
The trailing
|
2012-11-04 14:10:03 +04:00
|
|
|
.Ql X
|
|
|
|
characters in the template are replaced by unique values derived from
|
|
|
|
the current process number and additional letters or numbers.
|
|
|
|
Any
|
|
|
|
.Ql X
|
|
|
|
characters other than at the end of the template are taken literally.
|
1999-09-03 00:46:17 +04:00
|
|
|
The number of unique file names
|
|
|
|
.Nm
|
2012-11-04 14:10:03 +04:00
|
|
|
can return depends on the number of trailing
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ql X Ns s
|
2012-11-04 14:10:03 +04:00
|
|
|
in the template; six
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ql X Ns s
|
2006-09-19 23:00:17 +04:00
|
|
|
will result in
|
1999-09-03 00:46:17 +04:00
|
|
|
.Nm
|
|
|
|
testing roughly 26 ** 6 combinations.
|
|
|
|
.Pp
|
2012-11-04 14:10:03 +04:00
|
|
|
The templates used to create the unique names are derived from the
|
|
|
|
.Fl t Ar prefix
|
|
|
|
option, or the
|
|
|
|
.Ar template
|
|
|
|
arguments, possibly modified by other options.
|
|
|
|
Any number of temporary files or directories may be created
|
|
|
|
in a single invocation using multiple
|
|
|
|
.Ar template
|
|
|
|
arguments.
|
|
|
|
It is possible to specify both a
|
|
|
|
.Fl t Ar prefix
|
|
|
|
option and one or more
|
|
|
|
.Ar template
|
|
|
|
arguments,
|
|
|
|
but this is not usually done.
|
|
|
|
.Pp
|
|
|
|
If neither a
|
|
|
|
.Fl t Ar prefix
|
|
|
|
option, nor any
|
|
|
|
.Ar template
|
|
|
|
arguments are specified, then the default is equivalent to
|
|
|
|
.Fl t Li mktemp .
|
|
|
|
.Pp
|
1999-09-03 00:46:17 +04:00
|
|
|
If
|
|
|
|
.Nm
|
|
|
|
can successfully generate a unique file name, the file
|
|
|
|
is created with mode 0600 (unless the
|
|
|
|
.Fl u
|
2006-09-19 23:00:17 +04:00
|
|
|
flag is given) and the filename is printed to standard output.
|
2012-11-04 14:10:03 +04:00
|
|
|
.Sh OPTIONS
|
|
|
|
The available options are as follows:
|
|
|
|
.Bl -tag -width indent
|
|
|
|
.It Fl d
|
|
|
|
Make a directory instead of a file.
|
|
|
|
.It Fl p Ar tmpdir
|
|
|
|
Specifies a directory in which temporary files should be created.
|
|
|
|
If this option is specified, then it applies to all temporary files,
|
|
|
|
including those created as a result of a
|
|
|
|
.Fl t Ar prefix
|
|
|
|
option, and those created as a result of a
|
|
|
|
.Ar template
|
|
|
|
argument.
|
1999-09-03 00:46:17 +04:00
|
|
|
.Pp
|
|
|
|
If the
|
2009-08-16 00:02:28 +04:00
|
|
|
.Fl p Ar tmpdir
|
2012-11-04 14:10:03 +04:00
|
|
|
option is not specified, then
|
|
|
|
temporary files created as a result of a
|
|
|
|
.Fl t Ar prefix
|
|
|
|
option will use a default temporary directory
|
|
|
|
(as described under the
|
|
|
|
.Fl t
|
|
|
|
option),
|
|
|
|
but temporary files created as a result of a
|
2006-01-13 00:39:45 +03:00
|
|
|
.Ar template
|
2012-11-04 14:10:03 +04:00
|
|
|
argument will not use a default temporary directory
|
|
|
|
(so they will be created relative to the current working directory, if the
|
2006-09-19 18:22:51 +04:00
|
|
|
.Ar template
|
2012-11-04 14:10:03 +04:00
|
|
|
does not begin with
|
|
|
|
.Ql \&/ ) .
|
|
|
|
.It Fl t Ar prefix
|
|
|
|
Generate a template using an appropriate directory name, followed by the
|
|
|
|
supplied
|
|
|
|
.Ar prefix ,
|
|
|
|
followed by
|
|
|
|
.Ql \&.XXXXXXXX .
|
|
|
|
Any
|
|
|
|
.Ql X
|
|
|
|
characters in the supplied
|
|
|
|
.Ar prefix
|
|
|
|
are taken literally, but the trailing
|
|
|
|
.Ql X
|
|
|
|
characters in the appended
|
|
|
|
.Ql \&.XXXXXXXX
|
|
|
|
are replaced by unique values.
|
2006-09-19 18:22:51 +04:00
|
|
|
.Pp
|
2012-11-04 14:10:03 +04:00
|
|
|
The directory name used for the template generated by the
|
|
|
|
.Fl t Ar prefix
|
|
|
|
option is taken from the
|
|
|
|
.Fl p Ar tmpdir
|
|
|
|
option, or from the
|
|
|
|
.Ev TMPDIR
|
|
|
|
environment variable, or
|
|
|
|
.Pa /tmp
|
|
|
|
as a default.
|
1999-09-03 00:46:17 +04:00
|
|
|
.Pp
|
2012-11-04 14:10:03 +04:00
|
|
|
If one or more
|
|
|
|
.Ar template
|
|
|
|
arguments are used in addition to the
|
|
|
|
.Fl t Ar prefix
|
|
|
|
option, then the
|
|
|
|
.Ar prefix
|
|
|
|
does not apply to the
|
|
|
|
.Ar template
|
|
|
|
arguments.
|
1999-09-03 00:46:17 +04:00
|
|
|
.It Fl q
|
2002-09-30 15:08:56 +04:00
|
|
|
Fail silently if an error occurs.
|
|
|
|
This is useful if
|
1999-09-03 00:46:17 +04:00
|
|
|
a script does not want error output to go to standard error.
|
|
|
|
.It Fl u
|
|
|
|
Operate in
|
|
|
|
.Dq unsafe
|
2002-09-30 15:08:56 +04:00
|
|
|
mode.
|
|
|
|
The temp file will be unlinked before
|
1999-09-03 00:46:17 +04:00
|
|
|
.Nm
|
2002-09-30 15:08:56 +04:00
|
|
|
exits.
|
|
|
|
This is slightly better than
|
1999-09-03 00:46:17 +04:00
|
|
|
.Xr mktemp 3
|
2002-09-30 15:08:56 +04:00
|
|
|
but still introduces a race condition.
|
2006-09-19 23:00:17 +04:00
|
|
|
Use of this option is not encouraged.
|
1999-09-03 00:46:17 +04:00
|
|
|
.El
|
2012-11-04 14:10:03 +04:00
|
|
|
.Sh NOTES
|
|
|
|
.Nm
|
|
|
|
takes care to create the files or directories in a way that is
|
|
|
|
safe from race conditions (provided the
|
|
|
|
.Fl u
|
|
|
|
option is not used).
|
|
|
|
.Pp
|
|
|
|
Traditionally, without
|
|
|
|
.Nm ,
|
|
|
|
many shell scripts created temporary files
|
|
|
|
using the name of the program with
|
|
|
|
the pid as a suffix.
|
|
|
|
This kind of naming scheme is predictable and creates a race condition that
|
|
|
|
allows an attacker to subvert the program by
|
|
|
|
creating a different file, directory, or symbolic link
|
|
|
|
under the same name.
|
|
|
|
A safer, though still inferior, approach
|
2014-11-10 10:33:31 +03:00
|
|
|
is to make a temporary directory using the same naming scheme
|
2012-11-04 14:10:03 +04:00
|
|
|
While this does allow one to guarantee that a temporary file will
|
|
|
|
not be subverted, it still allows a simple denial of service attack.
|
|
|
|
For these reasons it is recommended that
|
|
|
|
.Nm
|
2014-11-10 10:33:31 +03:00
|
|
|
be used instead of simpler schemes.
|
2012-11-04 14:10:03 +04:00
|
|
|
.Pp
|
|
|
|
Care should be taken to ensure that it is appropriate to use an
|
|
|
|
environment variable potentially supplied by the user.
|
2000-09-04 11:35:15 +04:00
|
|
|
.Sh EXIT STATUS
|
1999-09-03 00:46:17 +04:00
|
|
|
The
|
|
|
|
.Nm
|
2006-09-19 23:00:17 +04:00
|
|
|
utility exits with a value of 0 on success, and 1 on any failure.
|
1999-09-03 00:46:17 +04:00
|
|
|
.Sh EXAMPLES
|
|
|
|
The following
|
|
|
|
.Xr sh 1
|
|
|
|
fragment illustrates a simple use of
|
|
|
|
.Nm
|
|
|
|
where the script should quit if it cannot get a safe
|
|
|
|
temporary file.
|
|
|
|
.Bd -literal -offset indent
|
2004-04-02 14:44:22 +04:00
|
|
|
TMPFILE=`mktemp /tmp/${0##*/}.XXXXXX` || exit 1
|
2002-02-08 04:36:18 +03:00
|
|
|
echo "program output" \*[Gt]\*[Gt] $TMPFILE
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
To allow the use of $TMPDIR:
|
|
|
|
.Bd -literal -offset indent
|
2004-04-02 14:44:22 +04:00
|
|
|
TMPFILE=`mktemp -t ${0##*/}` || exit 1
|
2002-02-08 04:36:18 +03:00
|
|
|
echo "program output" \*[Gt]\*[Gt] $TMPFILE
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
In this case, we want the script to catch the error itself.
|
|
|
|
.Bd -literal -offset indent
|
2004-04-02 14:44:22 +04:00
|
|
|
TMPFILE=`mktemp -q /tmp/${0##*/}.XXXXXX`
|
1999-09-03 00:46:17 +04:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "$0: Can't create temp file, exiting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
.Ed
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr mkdtemp 3 ,
|
|
|
|
.Xr mkstemp 3 ,
|
|
|
|
.Xr mktemp 3 ,
|
|
|
|
.Xr environ 7
|
|
|
|
.Sh HISTORY
|
1999-09-05 00:36:15 +04:00
|
|
|
The
|
1999-09-03 00:46:17 +04:00
|
|
|
.Nm
|
|
|
|
utility appeared in
|
1999-09-05 00:36:15 +04:00
|
|
|
.Nx 1.5 .
|
2014-11-10 10:33:31 +03:00
|
|
|
It was imported from
|
2006-01-13 00:39:45 +03:00
|
|
|
.Fx ,
|
2014-11-10 10:33:31 +03:00
|
|
|
and the idea and the manual page were taken from
|
2006-01-13 00:39:45 +03:00
|
|
|
.Ox .
|