1#!/bin/sh
2#
3# ms-check
4#
5# checks to see if the mailscanner process is running. starts the process
6# unless it has been manually stopped.
7#
8# author: 	Jerry Benton <mailscanner@mailborder.com>
9#			28 APR 2016
10#
11#   This program is free software; you can redistribute it and/or modify
12#   it under the terms of the GNU General Public License as published by
13#   the Free Software Foundation; either version 2 of the License, or
14#   (at your option) any later version.
15#
16#   This program is distributed in the hope that it will be useful,
17#   but WITHOUT ANY WARRANTY; without even the implied warranty of
18#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19#   GNU General Public License for more details.
20#
21#   You should have received a copy of the GNU General Public License
22#   along with this program; if not, write to the Free Software
23#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24#
25#      https://www.mailscanner.info
26#
27
28PATH=$PATH:/usr/sbin:/usr/bin:/bin:/sbin
29export PATH
30NAME=MailScanner
31DAEMON=/usr/sbin/MailScanner
32QUICKPEEK=/usr/sbin/ms-peek
33run_mailscanner=0
34ramdisk_sync=0
35ms_conf=/etc/MailScanner/MailScanner.conf
36ms_core=/usr/share/MailScanner
37ms_lib=/usr/lib/MailScanner
38ramdisk_store=/var/spool/MailScanner/ramdisk_store
39stopped_lockfile=/var/lock/subsys/MailScanner.off
40HOSTNAME=$(hostname)
41export HOSTNAME
42
43# enable logging of non-critical notices to the maillog: yes/no
44VERBOSE=yes
45
46# basic config file
47if [ -f /etc/MailScanner/defaults ] ; then
48	. /etc/MailScanner/defaults
49else
50	echo "Aborted: missing configuration file /etc/MailScanner/defaults"
51    exit 1
52fi
53
54PIDFILE=`${QUICKPEEK} PIDFile ${ms_conf}`
55
56if [ -z $PIDFILE ]; then
57	PIDFILE=/var/run/MailScanner.pid
58fi
59
60# Exit if the MailScanner executable is not installed
61[ -x $DAEMON ] || exit 0
62
63# if /var/lock/subsys is missing
64[ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
65
66# Don't start if MailScanner is not configured
67if [ $run_mailscanner = 0 ]; then
68	exit 0
69fi
70
71# check if a PID file exists
72if [ -f $PIDFILE ] ; then
73	# get the PID
74	PID=$(head -n 1 $PIDFILE)
75	# check to see if running and belongs to mailscanner
76	ps wwp $PID|grep -iq '[M]ailScanner' > /dev/null 2>&1
77
78	# get the return
79	RETVAL="$?"
80
81else
82	RETVAL=9
83fi
84
85# if 0 it is already running
86if [ $RETVAL -eq 0 ]; then
87	# already running
88	exit 0
89else
90
91	# if the stopped lock file is present exit
92	if [ -f $stopped_lockfile ] ; then
93		[ "$VERBOSE" != no ] && logger -i -p mail.notice "ms-check: not starting - lock file found"
94		exit 0
95	fi
96
97	# remove the PID file if preset
98	if [ -f $PIDFILE ] ; then
99		rm -f $PIDFILE
100	fi
101
102	# kill any rogue processes
103	kill -15 $(ps axww | grep '[M]ailScanner' | awk '{print $1}') > /dev/null 2>&1
104	# wait until they're gone.
105	while (ps axww | grep -q '[M]ailScanner'); do
106	    sleep 1
107	done
108
109	# log the start
110	[ "$VERBOSE" != no ] && logger -i -p mail.notice "ms-check: starting mailscanner"
111
112	# start mailscanner
113	$DAEMON $ms_conf
114
115	# set run file
116	if [ ! -f /var/lock/subsys/MailScanner ] ; then
117		touch /var/lock/subsys/MailScanner
118	fi
119
120	exit 0
121
122fi
123