Skip to content

Commit

Permalink
UC-647 Improve init scripts http://track.sipfoundry.org/browse/UC-647
Browse files Browse the repository at this point in the history
- output of scripts ends with a newline
- when stop is called processes are now killed by name
- to see if a process was launched successfully we :
    -look for the pid of the new spawned process in the process table
    for a maximum amount of time
    -check to see if the process is listening on a port (netstat is used)
- before printing echo_succsess or echo_failure return codes are analyzed

Added 4 new functions in the sipx-utils.sh.in file
- checkRunningInstance
    - returns 0 if processes related with a regex and belonging to a user
    are found in the process table
    - return1 1 otherwise

- checkRunningInstanceTimeout
    - similar to checkRunningInstance
    - searches if process exists for a maximum amount of time by checking
    the process table periodically

-checkIfListening
  - using netstat check if the process related with the regex is listening
  on any port
- killAnyOrphans
   - in a loop search for processes that are related with a specified regex
   and belong to a specified user and kill them

modifications made to functions:
- start ()
    - no fork :
        - kill all related processes (killAnyOrphans)
        - launch process
    - daemon :
        - check to see if there is another instance of the process running
        (checkRunningInstance);if there is one exit with failure else launch
        process
        - check if process was launched (checkRunningInstanceTimeout)
        - check if listening on a port  (checkIfListening)
- stop ()
    - kill all related processes instead of killing by the pids found in the
    pidfile
- status
    - search for all related processes
    - for the java related scripts to see if the process is running the
    process table is checked
- condrestart
    - check in the process table if there is another instance of the same
    process running before restarting
  • Loading branch information
LaurentiuGhita committed Aug 13, 2013
1 parent d74d848 commit bd62ae4
Show file tree
Hide file tree
Showing 20 changed files with 909 additions and 580 deletions.
115 changes: 61 additions & 54 deletions sipXbridge/bin/sipxbridge.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,57 @@

prog=sipxbridge
pidfile="@SIPX_RUNDIR@/sipxbridge.pid"
regex="Dprocname=sipxbridge"

[ -e @SIPX_CONFDIR@/sysconfig/$prog ] && . @SIPX_CONFDIR@/sysconfig/$prog

checkRunningInstance() {
if [ -f "${pidfile}" ]; then
pid=`cat ${pidfile}`
if checkpid $pid 2>&1; then
echo "Process $prog is already running with pid $pid"
exit 1
fi
fi
}

start() {
checkRunningInstance

JavaCmd=`@SIPX_BINDIR@/sipx-config --java`
Dependencies=`@SIPX_BINDIR@/java-dep -d @SIPX_JAVADIR@/sipXcommons @bridge_PKGS@`
export CLASSPATH=`echo @SIPX_CONFDIR@/sipxbridge @SIPX_JAVADIR@/sipXbridge/*.jar ${Dependencies} | sed -e 's/ /:/g'`

# procname is there so cfengine can find it
Command="$JavaCmd \
${SIPXBRIDGE_OPTS} \
-Dprocname=sipxbridge \
-Dconf.dir=@SIPX_CONFDIR@ \
-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger \
-Djavax.net.ssl.keyStore=@SIPX_CONFDIR@/ssl/ssl.keystore \
-Djavax.net.ssl.keyStorePassword=changeit \
-Dsipxbridge.command=start \
org.sipfoundry.sipxbridge.Gateway \
$Args"
if [ -n "${NoFork}" ] ; then
runuser -s /bin/bash @SIPXPBXUSER@ -c "${Command}"
else
echo -n $"Starting sipxbridge: "
runuser -s /bin/bash @SIPXPBXUSER@ -c "${Command}" >/dev/null 2>&1 &
echo $! > ${pidfile}
echo_success
fi
JavaCmd=`@SIPX_BINDIR@/sipx-config --java`
Dependencies=`@SIPX_BINDIR@/java-dep -d @SIPX_JAVADIR@/sipXcommons @bridge_PKGS@`
export CLASSPATH=`echo @SIPX_CONFDIR@/sipxbridge @SIPX_JAVADIR@/sipXbridge/*.jar ${Dependencies} | sed -e 's/ /:/g'`

# procname is there so cfengine can find it
Command="$JavaCmd \
${SIPXBRIDGE_OPTS} \
-Dprocname=sipxbridge \
-Dconf.dir=@SIPX_CONFDIR@ \
-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger \
-Djavax.net.ssl.keyStore=@SIPX_CONFDIR@/ssl/ssl.keystore \
-Djavax.net.ssl.keyStorePassword=changeit \
-Dsipxbridge.command=start \
org.sipfoundry.sipxbridge.Gateway \
$Args"
if [ -n "${NoFork}" ] ; then
killAnyOrphans @SIPXPBXUSER@ $regex
runuser -s /bin/bash @SIPXPBXUSER@ -c "${Command}"
else
checkRunningInstance @SIPXPBXUSER@ $regex > /dev/null
Status=$?
[ $Status -eq 0 ] && { echo -n "$prog $runningError"; echo_failure; echo; exit $ERUNNING; }

echo -n $"Starting $prog: "
runuser -s /bin/bash @SIPXPBXUSER@ -c "${Command}" >/dev/null 2>&1 &
echo $! > ${pidfile}

#check if process was spawned
checkRunningInstanceTimeout @SIPXPBXUSER@ $regex
Status=$?
[ $Status -eq 0 ] || { echo_failure; echo; exit $ESRCH; }
pid=`ps -u @SIPXPBXUSER@ -o pid,args | grep $regex | grep -v "grep" | awk '{print $1 }'`
checkIfListening $pid
Status=$?
[ $Status -eq 0 ] || { echo_failure; echo; exit $ENOSCKT; }
[ $Status -eq 0 ] && { echo_success; echo; }
fi
}

stop() {
echo -n $"Stopping sipxbridge: "
killproc -p "${pidfile}" "sipxbridge"
echo -n $"Stopping $prog: "
killAnyOrphans @SIPXPBXUSER@ $regex
checkRunningInstance @SIPXPBXUSER@ $regex > /dev/null
Status=$?
echo
[ $Status -eq 0 ] && rm -f "${pidfile}"
[ $Status -eq 0 ] && { echo_failure; echo; exit $ERUNNING; }
[ $Status -eq 0 ] || { echo_success; echo; }
}

restart() {
Expand All @@ -75,19 +79,19 @@ restart() {
}

configtest() {
Status=0
# check validity of xml files
! test -e @SIPX_CONFDIR@/peeridentities.xml \
|| @bindir@/sipx-validate-xml @SIPX_CONFDIR@/peeridentities.xml
Status=$(($Status+$?))

# Check that the log file is writable.
logfile="@SIPX_LOGDIR@/sipxbridge.log"
if [ -e $logfile -a ! -w $logfile ]
then
echo "Log file '$logfile' exists but is not writable by user '@SIPXPBXUSER@'." >&2
Status=1
fi
Status=0
# check validity of xml files
! test -e @SIPX_CONFDIR@/peeridentities.xml \
|| @bindir@/sipx-validate-xml @SIPX_CONFDIR@/peeridentities.xml
Status=$(($Status+$?))

# Check that the log file is writable.
logfile="@SIPX_LOGDIR@/sipxbridge.log"
if [ -e $logfile -a ! -w $logfile ]
then
echo "Log file '$logfile' exists but is not writable by user '@SIPXPBXUSER@'." >&2
Status=1
fi
}

case "$1" in
Expand All @@ -105,10 +109,13 @@ case "$1" in
restart
;;
condrestart)
[ -f ${pidfile} ] && restart || :
checkRunningInstance @SIPXPBXUSER@ $regex > /dev/null
Status=$?
[ $Status -eq 0 ] && restart || :
;;
status)
status -p ${pidfile} sipxbridge
checkRunningInstance @SIPXPBXUSER@ $regex $prog
Status=$?
;;
configtest)
configtest
Expand Down
73 changes: 46 additions & 27 deletions sipXcdr/bin/sipxcdr.in
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ fi
resolverArgs="--daemon --confdir ${ConfDir} --logdir ${LogDir}"
Database=SIPXCDR
Today=`date`
regex="${CallResolverMain}"
prog="sipxcdr"

PsqlCmd="${Psql} --tuples-only --no-align --username ${PgUser} --dbname ${Database}"

Expand Down Expand Up @@ -419,37 +421,52 @@ onSetup() {

# Start CDR call resolver
start() {
onSetup
# Check database version - don't run if it's wrong
echo $$ > ${PidFile}
databasePatch
if ! databaseVersionCheck; then
exit 1
fi
if [ -n "${NoFork}" ] ; then
runuser -s /bin/bash @SIPXPBXUSER@ -c "${RubyCmd} ${CallResolverMain} ${resolverArgs}"
else
echo -n $"Starting sipxcdr: "
runuser -s /bin/bash @SIPXPBXUSER@ -c "${RubyCmd} ${CallResolverMain} ${resolverArgs}" >/dev/null 2>&1 &
echo $! > ${PidFile}
echo_success
fi
onSetup
# Check database version - don't run if it's wrong
echo $$ > ${PidFile}
databasePatch
if ! databaseVersionCheck; then
exit 1
fi
if [ -n "${NoFork}" ] ; then
killAnyOrphans @SIPXPBXUSER@ $regex
runuser -s /bin/bash @SIPXPBXUSER@ -c "${RubyCmd} ${CallResolverMain} ${resolverArgs}"
else
checkRunningInstance @SIPXPBXUSER@ $regex > /dev/null
Status=$?
[ $Status -eq 0 ] && { echo -n "$prog $runningError"; echo_failure; echo; exit $ERUNNING; }

echo -n $"Starting ${prog}: "
runuser -s /bin/bash @SIPXPBXUSER@ -c "${RubyCmd} ${CallResolverMain} ${resolverArgs}" >/dev/null 2>&1 &
echo $! > ${PidFile}

#check if process was spawned
checkRunningInstanceTimeout @SIPXPBXUSER@ $regex
Status=$?
[ $Status -eq 0 ] && { echo_success; echo; exit $ESRCH; }
pid=`ps -u @SIPXPBXUSER@ -o pid,args | grep $regex | grep -v "grep" | awk '{print $1 }'`
checkIfListening $pid
Status=$?
[ $Status -eq 0 ] || { echo_failure; echo; exit $ENOSCKT; }
[ $Status -eq 0 ] && { echo_success; echo; }
fi
}

stop() {
echo -n $"Stopping sipxcdr: "
killproc -p "${PidFile}" "sipxcdr"
echo -n $"Stopping $prog: "
killAnyOrphans @SIPXPBXUSER@ $regex
checkRunningInstance @SIPXPBXUSER@ $regex > /dev/null
Status=$?
echo
[ $Status -eq 0 ] && rm -f "${PidFile}"
[ $Status -eq 0 ] && { echo_failure; echo; exit $ERUNNING; }
[ $Status -eq 0 ] || { echo_success; echo; }
}

restart() {
stop
# sleep for no good reason. Can be disconcerting if stop/start
# switch is too fast IMHO
sleep 1
start
stop
# sleep for no good reason. Can be disconcerting if stop/start
# switch is too fast IMHO
sleep 1
start
}

case "$1" in
Expand All @@ -467,10 +484,13 @@ case "$1" in
restart
;;
condrestart)
[ -f ${pidfile} ] && restart || :
checkRunningInstance @SIPXPBXUSER@ $regex > /dev/null
Status=$?
[ $Status -eq 0 ] && restart || :
;;
status)
status -p ${PidFile} sipxcdr
checkRunningInstance @SIPXPBXUSER@ $regex $prog
Status=$?
;;
configtest)
configCheck
Expand All @@ -493,4 +513,3 @@ case "$1" in
esac

exit $Status

87 changes: 87 additions & 0 deletions sipXcommserverLib/bin/sipx-utils.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
### Utility script functions common to server startup scripts
###

EALREADY=114 #Operation already in progress
ENOENT=2 #No such file or directory
ENOEXEC=8 #Exec format error
ESRCH=3 #No such process
ERUNNING=1 #Process still running
ENOSCKT=4 #No listening socket


# Set up correctly depending on the distribution
if [ -f /etc/redhat-release ]
then
Expand Down Expand Up @@ -459,3 +467,82 @@ EOF
fi
return $selinux_status
}

##
## functions for process management
##
maxTimeout=10
sleepTime=0.5
runningError="is already running"
execError="Provided file is not executable"
invalidFileError="Invalid file "
noPortError="No listening port found"

#check if process is in the process table
checkRunningInstance () {
user=$1
regex=$2
program=$3
procs=`ps -u $1 -o pid,args | grep $regex | grep -v "grep" | awk '{print $1 }' | tr -s '\n' " "`
[ "${procs}" ] && { echo "$program (pid $procs) is running"; return 0; }
[ "${procs}" ] || { echo "$program is stopped"; return $ESRCH; }
}

#check if process is in the process table for a maximum amount of time
checkRunningInstanceTimeout () {
user=$1
regex=$2
[ $3 ] && timeout=$3
[ $3 ] || timeout=$maxTimeout

for((i=0; i<$timeout; i++ )); do
checkRunningInstance $user $regex > /dev/null
Status=$?
[ $Status -eq 0 ] && return 0
sleep $sleepTime
done

return $ESRCH
}

#check if process is listening on a port for a maximum amount of time
checkIfListening() {
regex=$1
[ $2 ] && timeout=$2
[ $2 ] || timeout=$maxTimeout
for((i=0; i<$timeout; i++)); do
result=`netstat -tulpn | awk '$6 == "LISTEN"' | grep $regex | awk '{ print $4 }'`
[ "${result}" ] && return 0;
sleep $sleepTime
done
return $ENOSCKT
}

#kill any process that belongs to a specified user and related to a regex
killAnyOrphans () {
user=$1
regex=$2
Status=0
[ $3 ] && timeout=$3
[ $3 ] || timeout=$maxTimeout

#TERM first, then KILL if not dead
procs=`ps -u $1 -o pid,args | grep $regex | grep -v "grep" | awk '{print $1 }'`
for pid in $procs; do kill -TERM $pid; done
for((i=0; i<$timeout; i++)); do
checkRunningInstance $user $regex > /dev/null
Status=$?
[ $Status -eq $ESRCH ] && break;
sleep $sleepTime
done

checkRunningInstance $user $regex > /dev/null
Status=$?
[ $Status -eq 0 ] && { procs=`ps -u $1 -o pid,args | grep $regex | grep -v "grep" | awk '{print $1 }'`; \
for pid in $procs; do kill -KILL $pid; done ; \
}

checkRunningInstance $user $regex > /dev/null
Status=$?
[ $Status -eq 0 ] && { echo_failure; echo; }
}
Loading

0 comments on commit bd62ae4

Please sign in to comment.