[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bourne shell assistance needed for Bug #422909



Hi folks,

Bug #422909 has resulted from a broken assumption in how the shell
would handle errors, and I'm unsure exactly how to do this correctly.
Any assistance would be greatly appreciated.  Given that this can in
some circumstances result in severe dataloss, I'd very much like to
get this right.

The actual problematic code is here:
http://svn.debian.org/wsvn/buildd-tools/trunk/schroot/bin/schroot/setup/10mount?op=file&rev=0&sc=0  :

# Unmount all filesystem under specified location
# $1: mount base location
do_umount_all()
{
    "$LIBEXEC_DIR/schroot-listmounts" -m "$1" |
    while read mountloc; do
	if [ "$AUTH_VERBOSITY" = "verbose" ]; then
	    echo "Unmounting $mountloc"
	fi
	umount "$mountloc" || exit 1
    done || exit 1
}

The problem here is that if schroot-listmounts segfaults (the trigger
in this case) or returns an error, the script continues without any
indication of the fact, despite "set -e" being in effect.  In this bug
this results in a failure to umount a set of mounted filesystems, and
then an "rm -rf" taking out all the data further down the line.

A small testcase to demonstrate:

int
main (void)
{
  char *s = "segfault me";
  *s = 'p';
  return 0;
}
#!/bin/sh

set -e

    # This SEGV goes unnoticed.
    ./segfault |
    while read line; do
	    echo "Line: $line"
    done || exit 3

    # This one is noticed.
    ./segfault

true
How can I rewrite the while loop securely (and preferably avoiding
tmpfiles), such that any bad exit status or failure will result in
immediate termination of the script with an error status?


Sorry if this is too basic for -devel, but I need to be sure it's
correct.


Many thanks,
Roger

-- 
  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please GPG sign your mail.

Attachment: pgpuA_IlcCh0i.pgp
Description: PGP signature


Reply to: