#!/bin/sh #---------------------------------------------------------------------------- # /etc/init.d/inet_shlib - shell library of the inet package # # Copyright (c) 2001-2005 Ansgar Püster # # Creation: 18.10.2003 ap # Last Update: $Id$ # # 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. #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib # # Message mode # detailed print detailed messages # MMODE='' # # This library is used by # # /etc/init.d/sshd # /etc/init.d/xinetd # /etc/init.d/pure-ftpd # /var/install/config.d/inet.sh # # # verify_pidfile # ============== # # Parameters: $1 pidfile name (relativ to /var/run) # $2 expected process name # # Returns: 0 pidfile exists, process valid # 1 pidfile does not exist or was deleted # 2 pidfile exists but process is curious # verify_pidfile() { path=/var/run pidfile=$path/$1 pname=$2 # ps only shows the first 15 chracters of true command name (%c) short_pname=${pname:0:15} if [ -e $pidfile ]; then pid=`cat $path/$1` # check if process is valid kill -0 $pid > /dev/null 2>&1 rc=$? if [ $rc = 0 ]; then # check process name ps --no-headers -eo "%p %c" | grep "^ *$pid " | grep $short_pname > /dev/null 2>&1 rc=$? if [ $rc = 0 ]; then return 0 else mecho -error "curious process running with pid $pid" ps ax | grep "^ *$pid " mecho -error "process '$pname' using pidfile '$pidfile' was expected" return 2 fi else mecho -error "pidfile $pidfile contains erroneous pid $pid, pidfile deleted" rm $pidfile return 1 fi fi return 1 } # # check_start # =========== # # Parameters: $1 pidfile name (relativ to /var/run) # $2 expected process name # $3 maximum waittime for start # $4 sleeptime # # Returns: 0 process did start # 1 process did not start # check_start() { [ $# -lt 4 ] && return 2 PIDFILE=$1 PROCESS=$2 # ps only shows the first 15 chracters of true command name (%c) SHORT_PROCESS=${PROCESS:0:15} WAITTIME=$3 SLEEPTIME=$4 RTIME=$WAITTIME path=/var/run pidfile=$path/$PIDFILE while [ $RTIME -gt 0 ] do # if [ -f $pidfile ]; then if [ -s $pidfile ]; then pid=`cat $pidfile` > /dev/null 2>&1 # check if process is valid kill -0 $pid > /dev/null 2>&1 if [ $? = 0 ]; then # check process ps --no-headers -eo "%c %p" | grep "^$SHORT_PROCESS " | grep $pid > /dev/null 2>&1 rc=$? if [ $rc = 0 ]; then [ "$MMODE" = 'detailed' ] && mecho "process '$PROCESS' (PID $pid) using pidfile '$pidfile' started" return 0 else mecho -error "curious process running with pid $pid" ps ax | grep "^ *$PROCESS " mecho -error "process '$PROCESS' using pidfile '$pidfile' was expected" return 1 fi else mecho -error "pidfile '$pidfile' contains PID $pid, but process $PROCESS does not exists" return 1 fi else sleep $SLEEPTIME RTIME=`expr $RTIME - $SLEEPTIME` fi done mecho -error "process '$PROCESS' did not start within $WAITTIME seconds" return 1 } # # kill_and_wait # ============= # # Parameters: $1 pidfile name (relativ to /var/run) # $2 expected process name # $3 maximum waittime for each signal # except KILL # $4 sleeptime # $5 command (kill / killall) # $6.. signals (TERM, INT, HUP, KILL) # # Returns: 0 process terminated # 1 process did not terminate # 2 wrong parameter # kill_and_wait() { [ $# -lt 6 ] && return 2 PIDFILE=$1 PROCESS=$2 WAITTIME=$3 SLEEPTIME=$4 KILLCMD=$5 shift;shift;shift;shift;shift SIGNALLIST=$* path=/var/run pidfile=$path/$PIDFILE pid=`cat $pidfile` > /dev/null 2>&1 [ "$MMODE" = 'detailed' ] && mecho "Waiting for process $PROCESS (PID $pid) to terminate ..." for SIGNAL in $SIGNALLIST do # terminate loop, if no valid process or no pidfile # check if process is valid kill -0 $pid > /dev/null 2>&1 if [ ! $? = 0 ]; then # process was killed rm -f $pidfile continue fi [ ! -f $pidfile ] && continue RTIME=$WAITTIME case $KILLCMD in kill) kill -$SIGNAL $pid;; killall) killall -$SIGNAL $PROCESS;; *) return 2;; esac # SIGNAL=KILL wait 1 seconds [ $SIGNAL = 'KILL' ] && sleep 1 # check if process is still valid kill -0 $pid > /dev/null 2>&1 if [ ! $? = 0 ]; then # process was killed rm -f $pidfile continue fi # wait for normal termination if [ ! $SIGNAL = 'KILL' ]; then while [ "$RTIME" -gt 0 ] do RTIME=`expr $RTIME - $SLEEPTIME` if [ ! -f $pidfile ]; then RTIME=0 else sleep $SLEEPTIME fi done fi done # check if process is still valid if [ -f $pidfile ]; then mecho -error "Waittime expired, but process $PROCESS did not terminate" return 1 else [ "$MMODE" = 'detailed' ] && mecho "process $PROCESS (PID $pid) terminated" return 0 fi }