#!/bin/sh set -e # options for daemons: # name init.d/script user ps name for pgrep -f pidfile, relative to /var/run num-fds last-fd-name options=' clamd clamav-daemon clamav /usr/sbin/clamd clamav/clamd.pid 5 clamav.log amavis amavis.amavisd-new amavis amavisd \\(master\\) amavis/amavisd.pid 5 socket ' # note: pgrep -f takes a regexp, and this is shell expanded once, hence \\ start () { local daemon IFSOLD name script user psname pidfile num fdname daemon="$1" IFSOLD="$IFS" IFS=" " # tab read name script user psname pidfile num fdname <<-EOPTS $(echo "$options" | sed 's/ */ /g' | grep ^$daemon) EOPTS IFS="$IFSOLD" /etc/init.d/$script start wait_for_fds "$daemon" } stop () { local daemon IFSOLD name script user psname pidfile num fdname daemon="$1" n=10 IFSOLD="$IFS" IFS=" " # tab read name script user psname pidfile num fdname <<-EOPTS $(echo "$options" | sed 's/ */ /g' | grep ^$daemon) EOPTS IFS="$IFSOLD" /etc/init.d/$script stop pkill -u $user -f "$psname" > /dev/null || true while pgrep -u $user -f "$psname" > /dev/null && [ "$n" -gt 0 ] do sleep 1 n=$(($n-1)) done pkill -9 -u $user -f "$psname" > /dev/null || true #pkill -9 -u $user -x "$daemon" if pgrep -u $user -f "$psname" > /dev/null; then # still there? return 1 fi } wait_for_fds () { # wait until process shows some I/O readiness :) local name IFSOLD num sleep maxtry script user psname pidfile fdname name="$1" [ -z "$name" ] && return 1 IFSOLD="$IFS" IFS=" " # tab read name script user psname pidfile num fdname <<-EOPTS $(echo "$options" | sed 's/ */ /g' | grep ^$name) EOPTS IFS="$IFSOLD" num=${num:-4} sleep=${sleep:-1} maxtry=${maxtry:-90} if [ -n "$pidfile" ]; then pidfile=/var/run/$pidfile findpid="[ -f $pidfile ] && cat $pidfile || true" else findpid="pgrep -u $user -f \"$psname\" -P 1 | head -1" fi # loop the loop the loop try=1 while /bin/true do sleep $sleep # 1st, give it a chance to run pid=`eval $findpid` # 2nd: find it if [ ! -z "$pid" ]; then count=`ls -1 /proc/$pid/fd 2>/dev/null| wc -l` # 3rd: count all it's worth [ "$count" -ge "$num" ] && ls -l /proc/$pid/fd | grep -q $fdname \ && return # success -- release fi try=$(($try+1)) [ "0$try" -ge "0$maxtry" ] && return 1 # no luck this time done } # if we're called as amavisd-cn or amavis with start argument, # act like one; otherwise, pass the call down case "$(basename $0)" in amavisd-cn) arg="i$1" ;; amavis) if [ "$1" = start ]; then arg="i$1" else arg="$1" fi ;; *) arg="$1" ;; esac # If there's no diversion, play possum [ -x /etc/init.d/amavis.amavisd-new ] || exit 0 mta=postfix case "$arg" in start|stop|restart|reload|force-reload|debug) /etc/init.d/amavis.amavisd-new "$arg" ;; istart) start clamd start amavis /etc/init.d/$mta start ;; istop) /etc/init.d/$mta stop stop amavis stop clamd ;; irestart|ireload|iforce-reload) $0 stop sleep 2 $0 start ;; *) echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2 exit 1 ;; esac exit 0