From 52c70a8d8a2b2d28c3eb53281c80088f0aecdf17 Mon Sep 17 00:00:00 2001
From: Colin Campbell <colin.campbell@ptfs-europe.com>
Date: Thu, 1 May 2014 15:17:44 +0100
Subject: [PATCH] Bug 12169 Use pid file in sipserver shutdown

Change sample SIPConfig to request server writes a pid file
Use this in shutdown rather than the current open to error
method
Have also added to the Config parameters to ensure that the sipserver
runs as the correct user and sets its own session id. These are always
useful but makes it easier for users to run up the sipserver as root at
boot time similarly to apache, mysql etc
Added to the sample config where to locate other server parameter
documentation
Removed from the sample config the unedifying, unwanted and
purely historical http example
---
 etc/SIPconfig.xml        | 14 ++++++++------
 misc/bin/sip_shutdown.sh | 19 +++++--------------
 2 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/etc/SIPconfig.xml b/etc/SIPconfig.xml
index bd8d49e..e8d293a 100644
--- a/etc/SIPconfig.xml
+++ b/etc/SIPconfig.xml
@@ -4,7 +4,11 @@
 
 <!--
   Set Net::Server::PreFork runtime parameters 
+  See documentation for Net::Server
   syslog_ident will identify SIP2 Koha server entries in syslog
+  user and group select what user the service should run as
+  if started as root (e.g. in server startup ) the server will switch to
+  this user after binding to the socket it is listening on
   For OpenSolaris, add: syslog_logsock=stream
 -->
   <server-params
@@ -13,15 +17,13 @@
     log_file='Sys::Syslog'
     syslog_ident='koha_sip'
     syslog_facility='local6'
+    setsid="1"
+    user='koha'
+    group='koha'
+    pid_file='/var/run/sipserver.pid'
   />
   
   <listeners>
-<!-- vestigial HTTP, never implemented: just use the OPAC!
-	<service
-      port="0:8080/tcp"
-      transport="http"
-      protocol="NCIP/1.0" />
--->
     <service
       port="8023/tcp"
       transport="telnet"
diff --git a/misc/bin/sip_shutdown.sh b/misc/bin/sip_shutdown.sh
index 07abbce..3545df9 100755
--- a/misc/bin/sip_shutdown.sh
+++ b/misc/bin/sip_shutdown.sh
@@ -1,19 +1,10 @@
 #!/bin/bash
 
-. $HOME/.bash_profile
+#Terminates the session leader Sipserver which should terminate the children
+# The pidfile name is specified as a server parameter in the configuration
+# file
 
-# this is brittle: the primary server must have the lowest PPID
-# this is brittle: ps behavior is very platform-specific, only tested on Debian Etch
+PID_FILE=/var/run/sipserver.pid
 
-target="SIPServer";
-PROCPID=$(ps x -o pid,ppid,args --sort ppid | grep "$target" | grep -v grep | head -1 | awk '{print $1}');
+kill `cat $PID_FILE`
 
-if [ ! $PROCPID ] ; then
-    echo "No processes found for $target";
-    exit;
-fi
-
-echo "SIP Processes for this user ($USER):";
-ps x -o pid,ppid,args --sort ppid | grep "$target" | grep -v grep ;
-echo "Killing process #$PROCPID";
-kill $PROCPID;
-- 
1.9.0