#!/bin/csh -f

# This was originally written to clear "type none" entries in /etc/mtab,
# but I have come across a new problem.  Sometimes, it seems the automount
# process handling a particular mountpoint dies, leaving an entry in
# /etc/mtab pointing at a non-existent PID.  This wedges things so that
# restarting automount doesn't fix things.  You then have to lazy umount
# that mountpoint and anything mounted there.  ;^(  This script now has
# a "-c" option to clear those errors. 

# daw; 7/11/08

# New problem:
#    ls /net/koko/vol/vol1/system/master/power/remote
# while, for instance,
#    ls /net/koko-60/vol/vol1/system/master/power/remote

# succeeds.  Turns out that while "mount" does not show it, there are
# a number of remains left in /proc/mounts looking like this:
#  -hosts /net/koko/vol/vol1/system/solaris/patchroot autofs rw,fd=6,pgrp=2279,timeout=300,minproto=5,maxproto=5,offset 0 0

# confusing things.  After automount is shut down, these "mounts" can
# be umounted.

# daw; 12/7/11

switch (`uname -s``uname -r`)
case Linux*:
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG = /bin/echo
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -c:
		set CLEAR	# clear dead automount PIDs
		shift
		breaksw
	case -net:
		set NET		# clear /net references from /proc/mounts
		shift
		breaksw
	default:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

if (! $?DEBUG) set DEBUG

cd /				# restart autofs from root

set BASE = `basename $0`
if (-e /etc/init.d/autofs) set COMMAND = /etc/init.d/autofs
if (! $?COMMAND && -e /usr/sbin/service) set COMMAND = "/usr/sbin/service autofs"
if (! $?COMMAND) exec echo ${BASE}: How do I restart automount\?

if ($?CLEAR) goto clear

set FILESYSTEMS = `mount | egrep ":|type none"|awk '{print $3}'`

if ($?VERBOSE) echo `date +%T` Lazy unmounting automounted filesystems

foreach FILESYSTEM ( $FILESYSTEMS )
    if (! -e /etc/fstab) exec echo where is /etc/fstab\?
    grep "[ 	]${FILESYSTEM}[ 	]" /etc/fstab > /dev/null
    if ($status) then		# if not in fstab, assume automounted
	if ($?VERBOSE) echo "   " `date +%T` $FILESYSTEM
	$DEBUG /bin/umount -l $FILESYSTEM
    endif
end

if ($?VERBOSE) echo `date +%T` Restarting autofs

$DEBUG $COMMAND restart

if ($?NET) then
    if ($?VERBOSE) echo `date +%T` Looking for /net references in /proc/mounts

#    set NETFSS = `grep "^-hosts /net[/ ]" /proc/mounts | sed -e 's;^-hosts ;;' -e 's; .*;;' | sort -r`
# Don't umount filesystems associated with current automount; 8/11/12
# and avoid "Word too long." by umounting one at a time; 8/11/12
    set AMPID = `/sbin/pidof automount`
#    set NETFSN = `grep "^-hosts /net[/ ]" /proc/mounts | grep -vw "$AMPID" | sed -e 's;^-hosts ;;' -e 's; .*;;' | wc -l`
    set NETFSN = `grep "^-hosts /net[/ ]" /proc/mounts | sed -e 's;^-hosts ;;' -e 's; .*;;' | wc -l`
#echo NETFSN = $NETFSN

    if ($NETFSN) then
	if ($?VERBOSE) echo `date +%T` Stopping autofs
	$DEBUG $COMMAND stop
    endif

    set NETFSN = `grep "^-hosts /net[/ ]" /proc/mounts | sed -e 's;^-hosts ;;' -e 's; .*;;' | wc -l`
#echo NETFSN = $NETFSN


    while ($NETFSN)
#      set NETFS = `grep "^-hosts /net[/ ]" /proc/mounts | grep -vw "$AMPID" | sed -e 's;^-hosts ;;' -e 's; .*;;' | sort -r | head -1`
      set NETFS = `grep "^-hosts /net[/ ]" /proc/mounts | sed -e 's;^-hosts ;;' -e 's; .*;;' | sort -r | head -1`

#      if ("$NETFSS" != "") then
#	if ($?VERBOSE) echo `date +%T` Stopping autofs
#	$DEBUG /etc/init.d/autofs stop
#	if ($?VERBOSE) echo `date +%T` Ummounting /net references
	if ($?VERBOSE) echo `date +%T` Ummounting $NETFS
#	foreach NETFS ( $NETFSS )
#	    if ($?VERBOSE) echo "   " `date +%T` $NETFS
	    $DEBUG /bin/umount $NETFS
#	    if ($status) then
#		if ($?VERBOSE) echo `date +%T` Restarting autofs to recover
#		$DEBUG /etc/init.d/autofs restart
#		if ($?VERBOSE) echo "   " `date +%T` Retrying $NETFS
#		$DEBUG /bin/umount $NETFS
#	    endif
#	end
#	if ($?VERBOSE) echo `date +%T` Restarting autofs again
#	$DEBUG /etc/init.d/autofs restart
#	if ($?VERBOSE) echo `date +%T` Starting autofs
#	$DEBUG /etc/init.d/autofs start
#      endif
#      set NETFSN = `grep "^-hosts /net[/ ]" /proc/mounts | grep -vw "$AMPID" | sed -e 's;^-hosts ;;' -e 's; .*;;' | wc -l`
      set ONETFSN = $NETFSN
      set NETFSN = `grep "^-hosts /net[/ ]" /proc/mounts | sed -e 's;^-hosts ;;' -e 's; .*;;' | wc -l`
#echo NETFSN = $NETFSN
      if ($NETFSN == $ONETFSN) then
	echo NETFSN "($NETFSN)" did not decrease
	grep "^-hosts /net[/ ]" /proc/mounts | sed -e 's;^-hosts ;;' -e 's; .*;;' | sed 's;.;    &;'
	break
      endif
    end
    if ($?VERBOSE) echo `date +%T` Starting autofs
    $DEBUG $COMMAND start

endif

goto done

clear:

set ARRAY = `/bin/mount | grep ^auto | sed -e 's;.*(pid;;' -e 's;).* on ; ;' -e 's; type .*;;'`

set BMPTS

while ( $#ARRAY )
#    echo $ARRAY[1] $ARRAY[2]
    /bin/ps $ARRAY[1] > /dev/null
    if ($status) then
	set BMPTS = ( $BMPTS $ARRAY[2] )
    endif
    shift ARRAY ; shift ARRAY
end

if ("$BMPTS" == "") goto done

echo `date +%T` Problem found with `echo $BMPTS | sed 's; ;, ;g'`

set MPTS

foreach BMPT ( $BMPTS )
    set MPTS = ( $MPTS `/bin/mount | grep " on ${BMPT}[ /]" | sed -e 's;.* on ;;' -e 's; .*;;' | sort -r` )
end

if ($?VERBOSE) echo `date +%T` Stopping autofs

$DEBUG $COMMAND stop

if ($?VERBOSE) echo `date +%T` Lazy unmounting $MPTS

foreach MPT ( $MPTS )
    if ($?VERBOSE) echo "   " `date +%T` $MPT
    $DEBUG /bin/umount -l $MPT
end

if ($?VERBOSE) echo `date +%T` Restarting autofs

$DEBUG $COMMAND start

done:

if ($?VERBOSE) echo `date +%T` Done
