Skip to contentSkip to conversation

configure: Put tempfiles in subdir so we can clean up libtool files

PATCH In review Peter Maydell 3 participants 5 messages May 6, 2014 – May 29, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 configure: Put tempfiles in subdir so we can clean up libtool files

When libtool support was added to configure, the new temporary files were left out of the list of files cleaned up on exit; this results in a lot of stale .lo files being left around in /tmp. Worse, libtool creates a /tmp/.libs directory which we can't easily clean up.

Put all our temporary files in a single temporary directory created via mktemp -d, so we can easily clean it up. This has the bonus result that we no longer use $RANDOM (which silently expands to the empty string if your shell is not bash, and so is pretty useless).

Note that because we now use mktemp's tempdir-finding logic rather than handrolling it, we no longer honour TEMPDIR (only TMPDIR).

---
I don't know why we were looking at TEMPDIR; that code was in there from the initial commit by Fabrice back in 2003...

 configure | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
configure +10 -11
index 870c939..84c600f 100755
@@ -2,26 +2,25 @@
 #
 # qemu configure script (c) 2003 Fabrice Bellard
 #
-# set temporary file name
-if test ! -z "$TMPDIR" ; then
-    TMPDIR1="${TMPDIR}"
-elif test ! -z "$TEMPDIR" ; then
-    TMPDIR1="${TEMPDIR}"
-else
-    TMPDIR1="/tmp"
+
+TMPDIR1=$(mktemp -t -d)
+if [ $? -ne 0 ]; then
+    echo "ERROR: failed to create temporary directory"
+    exit 1
 fi
 
-TMPC="${TMPDIR1}/qemu-conf-${RANDOM}-$$-${RANDOM}.c"
-TMPB="qemu-conf-${RANDOM}-$$-${RANDOM}"
+TMPB="qemu-conf"
+TMPC="${TMPDIR1}/${TMPB}.c"
 TMPO="${TMPDIR1}/${TMPB}.o"
 TMPCXX="${TMPDIR1}/${TMPB}.cxx"
 TMPL="${TMPDIR1}/${TMPB}.lo"
 TMPA="${TMPDIR1}/lib${TMPB}.la"
-TMPE="${TMPDIR1}/qemu-conf-${RANDOM}-$$-${RANDOM}.exe"
+TMPE="${TMPDIR1}/${TMPB}.exe"
 
 # NB: do not call "exit" in the trap handler; this is buggy with some shells;
 # see <[email protected]>
-trap "rm -f $TMPC $TMPO $TMPCXX $TMPE" EXIT INT QUIT TERM
+trap "rm -rf ${TMPDIR1}" EXIT INT QUIT TERM
+
 rm -f config.log
 
 # Print a helpful header at the top of config.log
Signature · 1 line
1.9.2
#1 lore raw
Quoted 19 lines · Peter Maydell

When libtool support was added to configure, the new temporary files were left out of the list of files cleaned up on exit; this results in a lot of stale .lo files being left around in /tmp. Worse, libtool creates a /tmp/.libs directory which we can't easily clean up.

Put all our temporary files in a single temporary directory created via mktemp -d, so we can easily clean it up. This has the bonus result that we no longer use $RANDOM (which silently expands to the empty string if your shell is not bash, and so is pretty useless).

Note that because we now use mktemp's tempdir-finding logic rather than handrolling it, we no longer honour TEMPDIR (only TMPDIR).

---
I don't know why we were looking at TEMPDIR; that code was in there from the initial commit by Fabrice back in 2003...

configure line 6

Quoted 2 lines
configure +2 -0
+
+TMPDIR1=$(mktemp -t -d)

mktemp is not POSIX. BSD mktemp lacks -t:

http://www.freebsd.org/cgi/man.cgi?query=mktemp&apropos=0&sektion=1&manpath=Red+Hat+Linux%2Fi386+9&format=html

and there are probably systems that lack mktemp(1) altogether. You'll need to come up with a more portable alternative.

Here's what autoconf recommends (modify to fit...):

# Create a (secure) tmp directory for tmp files.

{
tmp=(umask 077 && mktemp -d "./confXXXXXX") 2>/dev/null && test -d "$tmp"
} ||
{
tmp=./conf$$-$RANDOM
(umask 077 && mkdir "$tmp")
} || as_fn_error $? "cannot create a temporary directory in ." "$LINENO" 5 ac_tmp=$tmp

The use of $$ and $RANDOM is safe (even on shells that lack $RANDOM) because of the fact that mkdir is atomic and the umask is correctly set prior to the mkdir.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 3 lines · Eric Blake

mktemp is not POSIX. BSD mktemp lacks -t:

http://www.freebsd.org/cgi/man.cgi?query=mktemp&apropos=0&sektion=1&manpath=Red+Hat+Linux%2Fi386+9&format=html

Sigh.

Quoted 7 lines

and there are probably systems that lack mktemp(1) altogether. You'll need to come up with a more portable alternative.

Here's what autoconf recommends (modify to fit...):

# Create a (secure) tmp directory for tmp files.

Quoted 9 lines

{
tmp=(umask 077 && mktemp -d "./confXXXXXX") 2>/dev/null && test -d "$tmp"
} ||
{
tmp=./conf$$-$RANDOM
(umask 077 && mkdir "$tmp")
} || as_fn_error $? "cannot create a temporary directory in ." "$LINENO" 5 ac_tmp=$tmp

Yuck.

Quoted 3 lines

The use of $$ and $RANDOM is safe (even on shells that lack $RANDOM) because of the fact that mkdir is atomic and the umask is correctly set prior to the mkdir.

I dislike the use of $RANDOM, because it means we behave inconsistently. If it's OK for $RANDOM to expand to "" then we should just not use it at all, because that's OK and the same everywhere.

Similarly, if it's OK not to use mktemp on some systems, we should use the same non-mktemp code everywhere.

We could sidestep this rubbish by not trying to put our temp files in /tmp/, and instead just put them in the build directory (ie ./conf-temps/ or something similar, which we blow away and recreate every time).

thanks
-- PMM

#3 lore raw
Quoted 14 lines · Peter Maydell
Quoted 11 lines

# Create a (secure) tmp directory for tmp files.

{
tmp=(umask 077 && mktemp -d "./confXXXXXX") 2>/dev/null && test -d "$tmp"
} ||
{
tmp=./conf$$-$RANDOM
(umask 077 && mkdir "$tmp")
} || as_fn_error $? "cannot create a temporary directory in ." "$LINENO" 5 ac_tmp=$tmp

Yuck.

Quoted 8 lines
Quoted 3 lines

The use of $$ and $RANDOM is safe (even on shells that lack $RANDOM) because of the fact that mkdir is atomic and the umask is correctly set prior to the mkdir.

I dislike the use of $RANDOM, because it means we behave inconsistently. If it's OK for $RANDOM to expand to "" then we should just not use it at all, because that's OK and the same everywhere.

It's okay for $RANDOM to expand to "" in the fallback code, for the platforms that lack mktemp(1); most developers are on a platform that have mktemp. The use of $RANDOM makes it harder for an attacker to pre-create a competing file by the same name, but does not add any security; so omitting $RANDOM for the fallback path doesn't hurt if you are that bothered by seeing it present in a dash script.

Quoted 3 lines

Similarly, if it's OK not to use mktemp on some systems, we should use the same non-mktemp code everywhere.

The fallback is not ideal, but tolerable. It's still better to try and use mktemp where it exists.

Quoted 5 lines

We could sidestep this rubbish by not trying to put our temp files in /tmp/, and instead just put them in the build directory (ie ./conf-temps/ or something similar, which we blow away and recreate every time).

Yes, using a different location for temporary files and avoiding /tmp might also work.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw
In reply to Eric Blake
Quoted 4 lines · Eric Blake

mktemp is not POSIX. BSD mktemp lacks -t:

http://www.freebsd.org/cgi/man.cgi?query=mktemp&apropos=0&sektion=1&manpath=Red+Hat+Linux%2Fi386+9&format=html

You managed to link to a mktemp man page for some old Red Hat version there (note the URL); FreeBSD's mktemp man page (for the 9.2 release) is here:

http://www.freebsd.org/cgi/man.cgi?query=mktemp&apropos=0&sektion=1&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html

which supports -t, but with a different meaning compared to contemporary Linux.