[hipl-dev] [Branch ~hipl-core/hipl/trunk] Rev 5955: Check whether daemon is running in openwrt init scripts.

  • From: noreply@xxxxxxxxxxxxx
  • To: HIPL core team <hipl-dev@xxxxxxxxxxxxx>
  • Date: Wed, 08 Jun 2011 16:56:13 -0000

------------------------------------------------------------
revno: 5955
committer: David Martin <david.martin.mailbox@xxxxxxxxxxxxxx>
branch nick: hipl_init-scripts
timestamp: Wed 2011-06-08 18:40:25 +0200
message:
  Check whether daemon is running in openwrt init scripts.
              
  Both hipd and hipfw create a PID file with their process id. Check against
  it to determine whether we actually have to start or stop the daemon.
  In case the daemon is already running or stopped print a notice respectively.
modified:
  packaging/openwrt/hipl/files/hipd.init
  packaging/openwrt/hipl/files/hipfw.init


--
lp:hipl
https://code.launchpad.net/~hipl-core/hipl/trunk

Your team HIPL core team is subscribed to branch lp:hipl.
To unsubscribe from this branch go to 
https://code.launchpad.net/~hipl-core/hipl/trunk/+edit-subscription
=== modified file 'packaging/openwrt/hipl/files/hipd.init'
--- packaging/openwrt/hipl/files/hipd.init      2011-05-27 12:52:21 +0000
+++ packaging/openwrt/hipl/files/hipd.init      2011-06-08 16:40:25 +0000
@@ -5,23 +5,45 @@
 STOP=65
 
 HIPD_OPTS="-bkN"
+PID_FILE="/var/lock/hipd.lock"
+
+# check if daemon is already running
+is_running() {
+    if test -f $PID_FILE; then
+        pgrep hipd | grep -q -f $PID_FILE
+        return $?
+    else
+        return 1
+    fi
+}
+
+stop_daemon() {
+    PID=$(cat $PID_FILE)
+    kill $PID >/dev/null
+}
 
 start() {
-    echo -n "Starting hipd"
-    hipd ${HIPD_OPTS}
+    if is_running; then
+       echo -n "hipd is already running"
+    else
+       echo -n "Starting hipd"
+       hipd ${HIPD_OPTS}
+    fi
     echo "."
 }
 
 restart() {
-    echo -n "Restarting hipd"
-    # if hipd is running, give it 5 seconds to exit gracefully
-    killall hipd 2>/dev/null && env sleep 5
-    hipd ${HIPD_OPTS}
-    echo "."
+    echo "Restarting hipd."
+    is_running && echo "Stopping hipd." && stop_daemon && sleep 5
+    start
 }
 
 stop() {
-    echo -n "Stopping hipd"
-    killall hipd 2>/dev/null
+    if is_running; then
+        echo -n "Stopping hipd"
+        stop_daemon
+    else
+        echo -n "hipd is not running"
+    fi
     echo "."
 }

=== modified file 'packaging/openwrt/hipl/files/hipfw.init'
--- packaging/openwrt/hipl/files/hipfw.init     2011-05-27 12:52:21 +0000
+++ packaging/openwrt/hipl/files/hipfw.init     2011-06-08 16:40:25 +0000
@@ -5,23 +5,45 @@
 STOP=65
 
 HIPFW_START='hipfw -kb'
+PID_FILE="/var/lock/hip_firewall.lock"
+
+# check if daemon is already running
+is_running() {
+    if test -f $PID_FILE; then
+        pgrep hipfw | grep -q -f $PID_FILE
+        return $?
+    else
+        return 1
+    fi
+}
+
+stop_daemon() {
+    PID=$(cat $PID_FILE)
+    kill $PID >/dev/null
+}
 
 start() {
-    echo -n "Starting hipfw"
-    ${HIPFW_START}
+    if is_running; then
+        echo -n "hipfw is already running"
+    else
+        echo -n "Starting hipfw"
+        ${HIPFW_START}
+    fi
     echo "."
 }
 
 restart() {
-    echo -n "Restarting hipfw"
-    # if hipfw is running, give it 2 seconds to exit gracefully
-    killall hipfw 2>/dev/null && env sleep 2
-    ${HIPFW_START}
-    echo "."
+    echo "Restarting hipfw."
+    is_running && echo "Stopping hipfw." && stop_daemon && sleep 2
+    start
 }
 
 stop() {
-    echo -n "Stopping hipfw"
-    killall hipfw 2>/dev/null
+    if is_running; then
+        echo -n "Stopping hipfw"
+        stop_daemon
+    else
+        echo -n "hipfw is not running"
+    fi
     echo "."
 }

Other related posts: