diff: avoid leaving directories in /tmp

Fergus Henderson fjh at cs.mu.OZ.AU
Sun Jun 28 17:42:08 AEST 1998


On 26-Mar-1998, I wrote:
> [Tyson wrote:] 
> > +		old_umask=`umask`
> > +		umask 022
> > +		mmake_tmpdir=/tmp/mmake$$
> > +		tmp=$mmake_tmpdir/mmake
> > +		if ! mkdir $mmake_tmpdir ; then
> > +			echo "Unable to create temporary makefile" 1>&2
> > +			exit 1
> > +		fi
> > +		umask $old_umask
> > +		trap 'status=$?; rm -rf $mmake_tmpdir; exit $status' 0 1 2 3 13 15
> 
> It would be slightly better to swap the order of the `umask' and `trap'
> statements.  There is a very small timing window where if someone
> hits control-C between the mkdir and the trap, the temp file and directory
> will not be removed.  I don't see any simple way of fixing that, and it is
> a very minor problem, but you should make the timing window as small
> as possible.

One of the nightly tests on murlibobo failed last night, due to
the mkdir failing because the directory already existed.
This in turn was probably due to this script's failure to remove
the directory when interrupted during this timing window.

Fortunately after a bit more thought I think there is a simple way
of fixing it.  The problem with moving the `trap' to before the mkdir
was that if the directory *did* exist, then it might be a bit
dangerous to do a `rm -rf' on it, in case it contained useful files.
But it should be OK to put a `trap' before the mkdir so long
as it just does a `rmdir'.

Hence the following fix.

--------------------

scripts/mmake.in:
scripts/ml.in:
	Ensure that the scripts clean up properly if interrupted --
	avoid a small timing window during which an interrupt signal
	could cause the scripts to leave temporary directories lying
	around in /tmp.

Index: scripts/ml.in
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/ml.in,v
retrieving revision 1.43
diff -u -r1.43 ml.in
--- ml.in	1998/06/09 02:16:28	1.43
+++ ml.in	1998/06/28 07:40:32
@@ -439,6 +439,7 @@
 	umask 022
 	ML_TMPDIR=/tmp/ml$$
 	PIPE=$ML_TMPDIR/pipe
+	trap 'rmdir $ML_TMPDIR >/dev/null 2>&1; exit 1' 1 2 3 13 15 
 	if mkdir $ML_TMPDIR ; then
 		true
 	else
Index: scripts/mmake.in
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/mmake.in,v
retrieving revision 1.24
diff -u -r1.24 mmake.in
--- mmake.in	1998/05/29 21:30:20	1.24
+++ mmake.in	1998/06/28 07:30:04
@@ -160,13 +160,16 @@
 			umask 022
 			mmake_tmpdir=/tmp/mmake$$
 			tmp=$mmake_tmpdir/mmake
+			trap 'rmdir $mmake_tmpdir >/dev/null 2>&1; exit 1' \
+				1 2 3 13 15
 			if mkdir $mmake_tmpdir ; then
 				true
 			else
 				echo "Unable to create temporary makefile" 1>&2
 				exit 1
 			fi
-			trap 'status=$?; rm -rf $mmake_tmpdir; exit $status' 0 1 2 3 13 15
+			trap 'status=$?; rm -rf $mmake_tmpdir; exit $status' \
+				0 1 2 3 13 15
 			umask $old_umask
 			;;
 		*)
-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the developers mailing list