#!/bin/sh
#
#   MailScanner - SMTP E-Mail Virus Scanner
#   Copyright (C) 2001  Julian Field
#
#   $Id: check_mailscanner.linux,v 1.3 2002/01/15 10:05:32 jkf Exp $
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   The author, Julian Field, can be contacted by email at
#      Jules@JulianField.net
#   or by paper mail at
#      Julian Field
#      Dept of Electronics & Computer Science
#      University of Southampton
#      Southampton
#      SO17 1BJ
#      United Kingdom
#

# Check that the virus scanner is still running.
# Re-start it if necessary.
# This can also be used from the init script to start it in the first
# place.

process=mailscanner
virusdir=/usr/sbin
config=/etc/mailscanner/mailscanner.conf

# get the pids of running mailscanner(s)
pid=`/bin/ps axww |
     /bin/grep '[ ]'$virusdir/$process |
     /usr/bin/awk '{ print $1 }'`

# if any of them are defunct, get rid of them
if [ "$pid" != "" ]; then
  for i in $pid; do
    status=`/bin/ps ww $i | /bin/grep defunct`
    if [ "$status" != "" ]; then
      kill -9 $i
    fi
  done
fi

# get the pids of non-defunct mailscanner(s)
pid=`/bin/ps axww |
     /bin/grep '[ ]'$virusdir/$process |
     /usr/bin/awk '{ print $1 }'`

if [ "$pid" = "" ]; then
  # Restart it
  PATH=${virusdir}:$PATH
  echo Starting virus scanner...
  $process $config
else
  echo Running with pid $pid
fi

