I use a SuSE 6.1 system (greatly modified)

but I needed to modify the autofs script (included) - I have extensively
tested it and have gotten it to work well for me.

I also had to slightly modify autofs-4.0.0pre9/daemon/automount.c so that
errors would show up better.
My modifications are as follows - move become-daemon from here -line 1300

///////////////

  if ( argc < 2 ) {
    usage();
    exit(1);
  }

  become_daemon();

  path    = argv[0];
  map     = argv[1];
  mapargv = (const char **) &argv[2];
  mapargc = argc-2;

///////////

to here - line 1322

///////////
      syslog(LOG_DEBUG, "Map argv[%d] = %s", i, mapargv[i]);
  }

  if ( (mapfmt = strchr(map,',')) )
    *(mapfmt++) = '\0';

  if ( !(ap.lookup = open_lookup(map, "", mapfmt, mapargc,
mapargv)) )
    cleanup_exit(1);

  become_daemon();

  /* Signals which are only used in ST_READY state */
  sigemptyset(&ready_sigs);
  sigaddset(&ready_sigs, SIGUSR1);
  sigaddset(&ready_sigs, SIGUSR2);
///////////

This ensured that the command line options were properly checked before
daemonising

regs Mr E_T

#! /bin/bash 
# 
# $Id: //depot/autofs-4.0/samples/rc.autofs.in
# $ 
# 
# rc file for automount using a Sun-style "master map". 
# We first look for a local /etc/auto.master, then a YP 
# map with that name 
# 
# On most distributions, this file should be called: 
# /etc/rc.d/init.d/autofs or /etc/init.d/autofs 
#

# For Redhat-ish systems
#
# chkconfig: 345 15 85
# description: Automounts filesystems on demand

# This is used in the Debian distribution to determine the proper
# location for the S- and K-links to this init file.
# The following value is extracted by debstd to figure out how to
# generate the postinst script. Edit the field to change the way the
# script is registered through update-rc.d (see the manpage for
# update-rc.d!)
#
# Modified for SuSE 6.1 by D. H. McKay
# copy to /etc/rc.d/autofs
# ln -s /etc/rc.d/autofs /usr/sbin/autofs
# ln -s /etc/rc.d/autofs /etc/rc.d/rc2.d/K26autofs
# ln -s /etc/rc.d/autofs /etc/rc.d/rc2.d/S14autofs
# ln -s /etc/rc.d/autofs /etc/rc.d/rc3.d/K26autofs
# ln -s /etc/rc.d/autofs /etc/rc.d/rc3.d/S14autofs
# insert AUTO_FS=yes into /etc/rc.config
# the sytem will now load autofs on boot
#
FLAGS="defaults 21"

#
# Location of the automount daemon and the init directory
#
DAEMON=/usr/sbin/automount
initdir=/etc/rc.d/init.d

#
# Determine which kind of configuration we're using
#
system=unknown
if [ -f /etc/debian_version ]; then
    system=debian
elif [ -f /etc/redhat-release ]; then
    system=redhat
elif [ -f /etc/SuSE-release ]; then
    system=SuSE
    initdir=/etc/rc.d
else
    echo "$0: Unknown system, please port and contact [EMAIL PROTECTED]" 1>&2
    exit 1
fi

if [ $system = redhat ]; then
    . $initdir/functions
elif [ $system = SuSE ]; then
    . /etc/rc.config
fi

test -e $DAEMON || exit 0
thisscript="$0"
if [ ! -f "$thisscript" ]; then
    echo "$0: Cannot find myself" 1>&2
    exit 1
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

#
# We can add local options here
# e.g. localoptions='rsize=8192,wsize=8192'
#
localoptions=''

#
# Daemon options
# e.g. --timeout 60
#
daemonoptions=''

#
# This function will build a list of automount commands to execute in
# order to activate all the mount points. It is used to figure out
# the difference of automount points in case of a reload
#
function getmounts()
{
#
# Check for local maps to be loaded
#
if [ -f /etc/auto.master ]
then
    cat /etc/auto.master | sed -e '/^#/d' -e '/^$/d'| (
        while read dir map options
        do
            if [ ! -z "$dir" -a ! -z "$map" \
                        -a x`echo "$map" | cut -c1` != 'x-' ]
            then
                map=`echo "/etc/$map" | sed -e 's:^/etc//:/:'`
                options=`echo "$options" | sed -e 's/\(^\|[ \t]\)-/\1/g'`
                if [ -x $map ]; then
                    echo "$DAEMON $daemonoptions $dir program $map $options 
$localoptions"
                elif [ -f $map ]; then
                    echo "$DAEMON $daemonoptions $dir file $map $options 
$localoptions"
                else
                    echo "$DAEMON $daemonoptions $dir yp `basename $map` $options 
$localoptions"
                fi
            fi
        done
    )
fi

#
# Check for YellowPage maps to be loaded
#
if [ -e /usr/bin/ypcat ] && [ `ypcat -k auto.master 2>/dev/null | wc -l` -gt 0 ]
then
    ypcat -k auto.master | (
        while read dir map options
        do
            if [ ! -z "$dir" -a ! -z "$map" \
                        -a x`echo "$map" | cut -c1` != 'x-' ]
            then
                map=`echo "$map" | sed -e 's/^auto_/auto./'`
                if echo $options | grep -- '-t' >/dev/null 2>&1 ; then
                    
mountoptions="--timeout $(echo $options | \
                      sed 's/^.*-t\(imeout\)*[ \t]*\([0-9][0-9]*\).*$/\2/g')"
                fi
                options=`echo "$options" | sed -e '
                  s/--*t\(imeout\)*[ \t]*[0-9][0-9]*//g
                  s/\(^\|[ \t]\)-/\1/g'`
                echo "$DAEMON $daemonoptions $mountoptions $dir yp $map $options 
$localoptions"
            fi
        done
    )
fi
}

#
# Status lister.
#
function status()
{
        echo "Configured Mount Points:"
        echo "------------------------"
        getmounts
        echo ""
        echo "Active Mount Points:"
        echo "--------------------"
        ps axw|grep "[0-9]:[0-9][0-9] [a-zA-Z/]*automount " | (
                while read pid tt stat time command; do echo $command; done
        )
}

# return true if at least one pid is alive
function alive()
{
    if [ -z "$*" ]; then
        return 1
    fi
    for i in $*; do
        if kill -0 $i 2> /dev/null; then
            return 0
        fi
    done

    return 1
}

#
# SuSE start/stop function.
#
function SuSE()
{

base=${0##*/}
link=${base#*[SK][0-9][0-9]}
test $link = $base && AUTOFS=yes
test "$AUTO_FS" = yes || exit 0
#
# See how we were called.
#
export return=$rc_done
case "$1" in
  start)
        # Check if the automounter is already running?
        if [ ! -f /var/lock/subsys/autofs ]; then
            echo -n "Starting automounter:"
            getmounts | (
              while read newmount
                do
                  ret=0;
                  $newmount || ret=1
                done
                return $ret
                )
            if [ $? -eq 1 ]
              then
                return=$rc_failed
              fi
            if alive `/sbin/pidof $DAEMON`
              then
                touch /var/lock/subsys/autofs
              else
                return=$rc_failed
              fi
            echo -e "$return"
        fi
        ;;
  stop)
        echo -n "Shutting down automounter:"
        pids=$(/sbin/pidof $DAEMON)
        kill -TERM $pids 2> /dev/null && sleep 1
        count=1
        while alive $pids; do
            sleep 5
            count=$(expr $count + 1)
            if [ $count -gt 5 ]; then
                return=$rc_failed
                break;
            fi
        pids=$(/sbin/pidof $DAEMON)
        kill -TERM $pids 2> /dev/null && sleep 1
        done
        if ! alive $pids
          then
            rm -f /var/lock/subsys/autofs
          fi
        echo -e "$return"
        ;;
  restart)
        $0 stop && $0 start || return=$rc_failed
        ;;
  reload)
        if [ ! -f /var/lock/subsys/autofs ]; then
                $0 start || return=$rc_failed
                exit 0
        fi
        echo -n "reloading automount"
        TMP1=`mktemp /tmp/autofs.XXXXXX` || return=$rc_failed
        TMP2=`mktemp /tmp/autofs.XXXXXX` || return=$rc_failed
        if [ -f $TMP1 ] && [ -f $TMP2 ]
          then
            getmounts >$TMP1
            ps axw|grep "[0-9]:[0-9][0-9] $DAEMON " | (
                while read pid tt stat time command args; do
                        echo "$command $args" >>$TMP2
                        if ! grep -q "^$command[' ']*$args" $TMP1; then
                                while kill -USR2 $pid > /dev/null 2>&1 ; do
                                        sleep 3
                                done
                        fi
                done
                )
                (       ret=0;
                        while read command args; do
                        if ! grep -q "^${command}[' ']*${args}" $TMP2; then
                                $command $args || ret=1
                        fi
                        done 
                return $ret ) < $TMP1
                if [ $? -eq 1 ]
                then
                  return=$rc_failed
                fi
                rm -f $TMP1 $TMP2
        fi
        echo -e "$return"
        ;;
  status)
        status
        if ! alive `/sbin/pidof $DAEMON`
          then
            echo -e "\nautomounter is Down"
            rm -f /var/lock/subsys/autofs
          fi
        ;;
  *)
        echo "Usage: $initdir/autofs {start|stop|restart|reload|status}"
        exit 1
esac
}

#
# Redhat start/stop function.
#
function redhat()
{

#
# See how we were called.
#
case "$1" in
  start)
        # Check if the automounter is already running?
        if [ ! -f /var/lock/subsys/autofs ]; then
            echo 'Starting automounter: '
            getmounts | sh
            touch /var/lock/subsys/autofs
        fi
        ;;
  stop)
        pids=$(/sbin/pidof $DAEMON)
        kill -TERM $pids 2> /dev/null && sleep 1
        count=1
        while alive $pids; do
            sleep 5
            count=$(expr $count + 1)
            if [ $count -gt 5 ]; then
                echo "Giving up on automounter"
                break;
            fi
            echo "Automounter not stopped yet: retrying... (attempt $count)"
        done
        if [ $count -gt 1 -a $count -le 10 ]; then
            echo "Automounter stopped"
        fi
        rm -f /var/lock/subsys/autofs
        ;;
  reload|restart)
        if [ ! -f /var/lock/subsys/autofs ]; then
                echo "Automounter not running"
                exit 1
        fi
        echo "Checking for changes to /etc/auto.master ...."
        TMP1=`mktemp /tmp/autofs.XXXXXX` || { echo "could not make temp file" >& 2; 
exit 1; }
        TMP2=`mktemp /tmp/autofs.XXXXXX` || { echo "could not make temp file" >& 2; 
exit 1; }
        getmounts >$TMP1
        ps ax|grep "[0-9]:[0-9][0-9] $DAEMON " | (
            while read pid tt stat time command; do
                echo "$command" >>$TMP2
                if ! grep -q "^$command" $TMP2; then
                        while kill -USR2 $pid; do
                            sleep 3
                        done
                        echo "Stop $command"
                fi
            done
        )
        ( while read x; do
                if ! grep -q "^$x" $TMP2; then
                        $x
                        echo "Start $x"
                fi
        done ) < $TMP1
        rm -f $TMP1 $TMP2
        ;;
  status)
        status
        ;;
  *)
        echo "Usage: $initdir/autofs {start|stop|restart|reload|status}"
        exit 1
esac
}

#
# Debian start/stop functions.
#
function debian()
{
#
# See how we were called.
#
case "$1" in
    start)
        echo -n 'Starting automounter:'
        getmounts | while read cmd mnt rest
        do
                echo -n " $mnt"
                pidfile=/var/run/autofs`echo $mnt | sed 's/\//./'`.pid
                start-stop-daemon --start --pidfile $pidfile --quiet \
                        --exec $DAEMON $daemonoptions -- $mnt $rest
        done
        echo "."
        ;;
    stop)
        echo 'Stopping automounter.'
        start-stop-daemon --stop --quiet --signal USR2 --exec $DAEMON
        ;;
    reload|restart)
        echo "Reloading automounter: checking for changes ... "
        TMP=/var/run/autofs.tmp
        getmounts >$TMP
        for i in /var/run/autofs.*.pid
        do
                pid=`head -n 1 $i 2>/dev/null`
                [ "$pid" = "" ] && continue
                command=`tail +2 $i`
                if ! grep -q "^$command" $TMP
                then
                        echo "Stopping automounter: $command"
                        kill -USR2 $pid
                fi
        done
        rm -f $TMP
        $thisscript start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $initdir/autofs {start|stop|restart|reload|status}" >&2
        exit 1
        ;;
esac
}

if [ $system = debian ]; then
        debian "$@"
elif [ $system = redhat ]; then
        redhat "$@"
elif [ $system = SuSE ]; then
        SuSE "$@"
fi

exit 0

Reply via email to