diff mbox

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

Message ID 1399382220-14874-1-git-send-email-peter.maydell@linaro.org
State New
Headers show

Commit Message

Peter Maydell May 6, 2014, 1:17 p.m. UTC
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).

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
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(-)

Comments

Eric Blake May 6, 2014, 2:36 p.m. UTC | #1
On 05/06/2014 07:17 AM, Peter Maydell wrote:
> 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).
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> I don't know why we were looking at TEMPDIR; that code was
> in there from the initial commit by Fabrice back in 2003...
> 

> +
> +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.
Peter Maydell May 6, 2014, 2:53 p.m. UTC | #2
On 6 May 2014 15:36, Eric Blake <eblake@redhat.com> wrote:
> 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.

> 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

Yuck.

> 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
Eric Blake May 6, 2014, 3:43 p.m. UTC | #3
On 05/06/2014 08:53 AM, Peter Maydell wrote:

>> # 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.
> 
>> 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.

> 
> 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.

> 
> 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.
Ed Maste May 29, 2014, 2:33 a.m. UTC | #4
On 6 May 2014 10:36, Eric Blake <eblake@redhat.com> wrote:
>
> 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.
diff mbox

Patch

diff --git a/configure b/configure
index 870c939..84c600f 100755
--- a/configure
+++ b/configure
@@ -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 <1285349658-3122-1-git-send-email-loic.minier@linaro.org>
-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