#!/bin/sh
# $Id: t-restart-0.sh,v 1.13 2007/09/29 02:10:07 ca Exp $
# Copyright (c) 2004 Sendmail, Inc. and its suppliers.
#	All rights reserved.
#
# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#
# ----------------------------------------
# test restart functionality of mcp
# ----------------------------------------

test -s stop && exit 1

# set for verbose mode to -V...
V=""
ERRS=0
OUT=mcp.out
PIDS=pids
CONF=restart.conf
PIDF1="t-restart1.pid"
PIDF2="t-restart2.pid"
L1=restart.log
L2=restart2.log
R1="restarts1"
R2="restarts2"
VERBOSE=false
SD=`dirname $0`
. ${SD}/../check2/user.sh
if test $# -ge 1
then
  if test "X"$1 = "X-v"
  then
    VERBOSE=true;
  fi
fi

rm -f ${PIDS} ${OUT} ${PIDF1} ${PIDF2} ${L1} ${L2}

cat > ${CONF} <<EOF
restart {
	start_action = wait;
	min_processes = 1;
	max_processes = 1;
	user = ${USER};
	restart_dependencies = {restart2};
	path = ./t-restart;
	arguments = "t-restart -p ${PIDF1} -f ${R1} -n 3 -VV";
}
restart2 {
	start_action = wait;
	min_processes = 1;
	max_processes = 1;
	user = ${USER};
	path = ./t-restart;
	arguments = "t-restart -p ${PIDF2} -f ${R2} -n 2 -VV";
}
EOF

echo 0 > ${R1}
echo 0 > ${R2}
./mcp -D -t -dddd ${CONF} > ${OUT} 2>&1 &
echo $! >> ${PIDS}

i=0
while test ! -s ${PIDF1}
do
  sleep 3
  i=`expr ${i} + 1 `
  if test ${i} -ge 4
  then
    echo "$0: ERROR: missing ${PIDF1}"
    ERRS=`expr ${ERRS} + 1 `
    break
  fi
done

CNT=2
if test -s ${PIDF1}
then
  PIDR=`cat ${PIDF1}`
  kill -HUP ${PIDR}
  sleep 5
  for F in ${R1} ${R2}
  do
    if test -s ${F}
    then
      C=`cat ${F}`
      if test ${C} -ne ${CNT}
      then
        ${VERBOSE} && echo "counter ${F} wrong: got ${C}, expected ${CNT}"
        ERRS=`expr ${ERRS} + 1 `
      fi
    else
      ${VERBOSE} && echo "missing ${F}"
      ERRS=`expr ${ERRS} + 1 `
    fi
  done
fi

for i in `cat ${PIDS}`
do
  kill ${i}
done

exit ${ERRS}


syntax highlighted by Code2HTML, v. 0.9.1