dnschneid/crouton — pull request #201 by drinkcat (2013-06-08 08:43:27+00:00)
Title: Fix traps in main.sh

Traps do not work correctly in `main.sh` (nor in `prepare` probably, I will fix that too if you think this solution is ok).

There are 2 problems, first, `main.sh` does not exit on interrupt:

```
$ sudo sh -x -e installer/main.sh -r precise -n precisetest -t core
[snip]
+ echo Downloading latest debootstrap...
Downloading latest debootstrap...
+ d=http://anonscm.debian.org/gitweb/?p=d-i/debootstrap.git;a=snapshot;h=HEAD;sf=tgz
+ tar -C /tmp/main.sh.EBb --strip-components=1 -zx
+ wget -O- --no-verbose --timeout=60 -t2 http://anonscm.debian.org/gitweb/?p=d-i/debootstrap.git;a=snapshot;h=HEAD;sf=tgz
********* here I press Ctrl-C: the trap INT is executed... ***********
^C+ umount -f /tmp/main.sh.EBb
+ rm -rf /tmp/main.sh.EBb
+ sh -e installer/../host-bin/unmount-chroot -y -c /usr/local/chroots precisetest
+ stty echo
********* ... but the script still continues ***********
+ echo Download from Debian gitweb failed. Trying latest release...
Download from Debian gitweb failed. Trying latest release...
+ d=http://ftp.debian.org/debian/pool/main/d/debootstrap/
+ wget -O- --no-verbose --timeout=60 -t2 http://ftp.debian.org/debian/pool/main/d/debootstrap/
+ sed -ne s ^.*\(debootstrap_[0-9.]*.tar.gz\).*$ \1 p
+ tail -n 1
2013-06-08 13:22:17 URL:http://ftp.debian.org/debian/pool/main/d/debootstrap/ [4356/4356] -> "-" [1]
+ f=debootstrap_1.0.52.tar.gz
+ [ -z debootstrap_1.0.52.tar.gz ]
+ v=1.0.52.tar.gz
+ v=1.0.52
+ echo Downloading debootstrap version 1.0.52...
Downloading debootstrap version 1.0.52...
+ wget -O- --no-verbose --timeout=60 -t2 http://ftp.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.52.tar.gz
+ tar -C /tmp/main.sh.EBb --strip-components=1 -zx
Cannot write to `-' (Broken pipe).
+ error 1 Failed to download debootstrap.
+ local ecode=1
+ shift
+ echo Failed to download debootstrap.
Failed to download debootstrap.
+ exit 1
********* Now the trap for EXIT (0) finally runs (because of `-e`) ***********
+ umount -f /tmp/main.sh.EBb
umount2: No such file or directory
umount: /tmp/main.sh.EBb: not found
```

Second problem. In some cases the trap handler is executed twice:

```
$ sudo sh -x -e installer/main.sh -r precise -n precisetest -t core
[snip]
+ echo Downloading bootstrap files...
Downloading bootstrap files...
+ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/opt/bin:/tmp/main.sh.WrN DEBOOTSTRAP_DIR=/tmp/main.sh.WrN /tmp/main.sh.WrN/debootstrap --foreign --arch=armhf precise /tmp/main.sh.WrN/precise-armhf http://ports.ubuntu.com/ubuntu-ports/
I: Retrieving Release
********* here I press Ctrl-C: debootstrap catches it, and exits ***********
^CE: Interrupt caught ... exiting
********* The trap for EXIT (0) runs ***********
+ umount -f /tmp/main.sh.WrN
********* The trap for INT then runs (I think that what is happening is that
sh receives both signals, starts handling the first one (EXIT), and only
handles the second one (INT) after the next command is run) ***********
+ umount -f /tmp/main.sh.WrN
umount2: Invalid argument
umount: /tmp/main.sh.WrN: not mounted
```

I fix these 2 problems by:
- Disabling all traps at the beginning of the trap handler, to make we do not execute clean-up commands twice.
- Forcing `exit 2` at the end of the handler