File dansguardian-squid_DG-init.sh of Package dansguardian

#! /bin/sh
# Copyright (c) 1996, 1997, 1998 S.u.S.E. GmbH
# Copyright (c) 1998, 1999, 2000, 2001 SuSE GmbH
# Copyright (c) 2002 SuSE Linux AG
#
# Author: Frank Bodammer, Peter Poeml, Klaus Singvogel <feedback@suse.de>
# Modified extensively to add dansguardian info by Don Vosburg <don@vosburgs.org>
#
# init.d/squid_DG
#
### BEGIN INIT INFO
# Provides:			squid_DG
# Required-Start:		$local_fs $remote_fs $network $named $time
# X-UnitedLinux-Should-Start:	apache
# Required-Stop:		$local_fs $remote_fs $network
# X-UnitedLinux-Should-Stop:	
# Default-Start:		3 5
# Default-Stop:			0 1 2 6
# Short-Description:		Squid web cache, DansGuardian content filtering
# Description:			Start the Squid web cache, providing
#				HTTP, FTP and other proxy services
#				along with dansguardian content filter
### END INIT INFO


SQUID_BIN=/usr/sbin/squid
SQUID_PID=/var/run/squid.pid
SQUID_CONF=/etc/squid/squid.conf
CONFFILELOCATION=/etc/dansguardian/
BINARYLOCATION=/usr/sbin/
PIDDIR=/var/run/


if [ ! -x $SQUID_BIN ] ; then
	echo -n "WWW-proxy squid not installed ! "
	exit 5
fi

. /etc/rc.status
RC_OPTIONS='-v'
rc_reset

ulimit -n 4096

# determine which one is the cache_swap directory
CACHE_SWAP=`perl -n -e \
 '/^cache_dir\s+\S+\s+(.*)\s+\d+\s+\d+\s+\d+/ && print "\$1 "' \
  /etc/squid/squid.conf`
[ -z "$CACHE_SWAP" ] && CACHE_SWAP=/var/cache/squid

# File includes changes by Thomas Jarosch
function wait_for_pid()
{
    local PID=$1
    local RET=0
    
    if [ $PID -eq 0 ] ; then
        return $RET
    fi
    
    # give 60 secs then KILL
    local COUNTDOWN=60

    while [ -d /proc/${PID} ] && [ $COUNTDOWN -gt 0 ] ; do
        sleep 1
        COUNTDOWN=$[$COUNTDOWN-1]
    done

    if [ -d /proc/${PID} ]; then
        COMMAND=`ps h -o command ${PID}`
        logger "dansguardian: timeout waiting for PID ${PID}: ${COMMAND}; sending SIGKILL"
        kill -KILL $PID >/dev/null 2>&1
        RET=1
    fi
    
    return $RET
}

case "$1" in
    start)
        echo -n "Starting WWW-proxy squid "
	checkproc $SQUID_BIN

        if [ $? -eq 0 ] ; then
	   echo -n "- Warning: squid already running ! "
	   rc_failed
           rc_status -v
	else
	   [ -e $SQUID_PID ] && echo -n "- Warning: $SQUID_PID exists ! "
	   for adir in $CACHE_SWAP ; do
	     if [ ! -d $adir/00 ]; then # create missing cache directories
	        umask 027             # prevent users reading any cache data
	        echo -n " ($adir)"
	        $SQUID_BIN -z -F > /dev/null 2>&1
	     fi
	     if [ ! -d $adir/00 ]; then
	        echo " - Could not create cache_dir ! "
	        rc_failed
		rc_status -v
		rc_exit
	     fi
	   done
	   sleep 2
	   rc_status $RC_OPTIONS
	fi
	startproc -l /var/log/squid/rcsquid.log $SQUID_BIN -sYD
	echo -n "Starting dansguardian "
	if [ -f ${BINARYLOCATION}dansguardian ] &&
           [ -f ${CONFFILELOCATION}dansguardian.conf ]; then
                if ${BINARYLOCATION}dansguardian 2> /dev/null; then
                 rc_status $RC_OPTIONS
                 [ -d /var/lock/subsys ] && touch /var/lock/subsys/dansguardian
                 else
                 echo -n "- Warning: dansguardian already running ! "
                 rc_failed 
		 rc_status -v
		 rc_exit
                fi
         fi
	;;
    stop)
	echo -n "Shutting down dansguardian "
        WAITPID=0
        if [ -f ${PIDDIR}dansguardian.pid ] ; then
            WAITPID=`cat ${PIDDIR}dansguardian.pid`
        fi
        if ${BINARYLOCATION}dansguardian -q 2> /dev/null; then
                if wait_for_pid $WAITPID ; then
                  rc_status $RC_OPTIONS 
                else
                 rc_failed
		 rc_status -v
		 rc_exit  
                fi
                /bin/rm -f ${PIDDIR}dansguardian.pid
                /bin/rm -f /tmp/.dguardianipc
                [ -d /var/lock/subsys ] && /bin/rm -f /var/lock/subsys/dansguardian
        else
	   echo -n "- Warning: dansguardian not running ! "
	   rc_failed 7
           rc_status -v
        fi
	echo -n "Shutting down WWW-proxy squid "
	if checkproc $SQUID_BIN ; then
	   $SQUID_BIN -k shutdown
	   sleep 2
	   if [ -e $SQUID_PID ] ; then 
	      echo -n "- wait a minute "
	      i=60
	      while [ -e $SQUID_PID ] && [ $i -gt 0 ] ; do
	         sleep 2
	         i=$[$i-1]
	         echo -n "."
	         [ $i -eq 41 ] && echo
	      done
	   fi
	   if checkproc $SQUID_BIN ; then
	      killproc -TERM $SQUID_BIN
	      echo -n " Warning: squid killed !"
	   fi
	else
	   echo -n "- Warning: squid not running ! "
	   rc_failed 7
	fi
	rc_status -v
	;;
    try-restart)
	$0 status >/dev/null && $0 restart
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	$0 stop
	$0 start
	$0 reload
	rc_status
	;;
    reload)
        echo -n "Shutting down dansguardian: "
        WAITPID=0
        if [ -f ${PIDDIR}dansguardian.pid ] ; then
            WAITPID=`cat ${PIDDIR}dansguardian.pid`
        fi
        if ${BINARYLOCATION}dansguardian -q 2> /dev/null; then
                if wait_for_pid $WAITPID ; then
                  rc_status $RC_OPTIONS 
                else
                 rc_failed
		 rc_status -v
                fi
                /bin/rm -f ${PIDDIR}dansguardian.pid
                /bin/rm -f /tmp/.dguardianipc
                [ -d /var/lock/subsys ] && /bin/rm -f /var/lock/subsys/dansguardian
        else
                rc_failed
		rc_status -v
        fi
        echo -n "Reloading WWW-proxy squid "
	if checkproc $SQUID_BIN ; then
	   $SQUID_BIN -k rotate
	   sleep 2
	   $SQUID_BIN -k reconfigure
	   rc_status -v
	else
	   echo -n "- Warning: squid not running ! "
	   rc_failed 7
           rc_status -v
           rc_exit
	fi
	if [ -f ${BINARYLOCATION}dansguardian ] &&
           [ -f ${CONFFILELOCATION}dansguardian.conf ]; then
                echo -n "Starting dansguardian: "
                if ${BINARYLOCATION}dansguardian 2> /dev/null; then
                 rc_status $RC_OPTIONS
                 [ -d /var/lock/subsys ] && touch /var/lock/subsys/dansguardian
                 else
                 rc_failed
		 rc_status -v
		 rc_exit
                fi
         fi
	;;
    status)
	echo -n "Checking for dansguardian "
        checkproc ${BINARYLOCATION}dansguardian
        rc_status -v
        echo -n "Checking for WWW-proxy squid "
        checkproc $SQUID_BIN
        rc_status -v
        ;;
    probe)
	test $SQUID_CONF -nt $SQUID_PID && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
openSUSE Build Service is sponsored by