debian: Create ovs-monitor-ipsec rundir if it doesn't exist
[sliver-openvswitch.git] / debian / openvswitch-ipsec.init
1 #!/bin/sh
2 #
3 # Copyright (c) 2007, 2009 Javier Fernandez-Sanguino <jfs@debian.org>
4 #
5 # This is free software; you may redistribute it and/or modify
6 # it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2,
8 # or (at your option) any later version.
9 #
10 # This is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License with
16 # the Debian operating system, in /usr/share/common-licenses/GPL;  if
17 # not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
19 #
20 ### BEGIN INIT INFO
21 # Provides:          openvswitch-ipsec
22 # Required-Start:    $network $local_fs $remote_fs
23 # Required-Stop:     $remote_fs
24 # Default-Start:     2 3 4 5
25 # Default-Stop:      0 1 6
26 # Short-Description: Open vSwitch GRE-over-IPsec daemon
27 ### END INIT INFO
28
29 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
30
31 DAEMON=/usr/share/openvswitch/scripts/ovs-monitor-ipsec # Daemon's location
32 NAME=ovs-monitor-ipsec          # Introduce the short server's name here
33 LOGDIR=/var/log/openvswitch     # Log directory to use
34
35 PIDFILE=/var/run/openvswitch/$NAME.pid 
36
37 test -x $DAEMON || exit 0
38
39 . /lib/lsb/init-functions
40
41 DODTIME=10              # Time to wait for the server to die, in seconds
42                         # If this value is set too low you might not
43                         # let some servers to die gracefully and
44                         # 'restart' will not work
45                         
46 set -e
47
48 running_pid() {
49 # Check if a given process pid's cmdline matches a given name
50     pid=$1
51     name=$2
52     [ -z "$pid" ] && return 1 
53     [ ! -d /proc/$pid ] &&  return 1
54     cmd=`cat /proc/$pid/cmdline | tr "\000" " "|cut -d " " -f 2`
55     # Is this the expected server
56     [ "$cmd" != "$name" ] &&  return 1
57     return 0
58 }
59
60 running() {
61 # Check if the process is running looking at /proc
62 # (works for all users)
63
64     # No pidfile, probably no daemon present
65     [ ! -f "$PIDFILE" ] && return 1
66     pid=`cat $PIDFILE`
67     running_pid $pid $DAEMON || return 1
68     return 0
69 }
70
71 start_server() {
72     if [ ! -d /var/run/openvswitch ]; then
73         install -d -m 755 -o root -g root /var/run/openvswitch
74     fi
75
76     PYTHONPATH=/usr/share/openvswitch/python \
77            /usr/share/openvswitch/scripts/ovs-monitor-ipsec \
78            --pidfile-name=$PIDFILE --detach --monitor \
79            unix:/var/run/openvswitch/db.sock
80
81     return 0
82 }
83
84 stop_server() {
85     if [ -e $PIDFILE ]; then
86         kill `cat $PIDFILE`
87     fi
88
89     return 0
90 }
91
92 force_stop() {
93 # Force the process to die killing it manually
94     [ ! -e "$PIDFILE" ] && return
95     if running ; then
96         kill -15 $pid
97         # Is it really dead?
98         sleep "$DODTIME"
99         if running ; then
100             kill -9 $pid
101             sleep "$DODTIME"
102             if running ; then
103                 echo "Cannot kill $NAME (pid=$pid)!"
104                 exit 1
105             fi
106         fi
107     fi
108     rm -f $PIDFILE
109 }
110
111
112 case "$1" in
113   start)
114         log_daemon_msg "Starting $NAME"
115         # Check if it's running first
116         if running ;  then
117             log_progress_msg "apparently already running"
118             log_end_msg 0
119             exit 0
120         fi
121         if start_server && running ;  then
122             # It's ok, the server started and is running
123             log_end_msg 0
124         else
125             # Either we could not start it or it is not running
126             # after we did
127             # NOTE: Some servers might die some time after they start,
128             # this code does not try to detect this and might give
129             # a false positive (use 'status' for that)
130             log_end_msg 1
131         fi
132         ;;
133   stop)
134         log_daemon_msg "Stopping $NAME"
135         if running ; then
136             # Only stop the server if we see it running
137             stop_server
138             log_end_msg $?
139         else
140             # If it's not running don't do anything
141             log_progress_msg "apparently not running"
142             log_end_msg 0
143             exit 0
144         fi
145         ;;
146   force-stop)
147         # First try to stop gracefully the program
148         $0 stop
149         if running; then
150             # If it's still running try to kill it more forcefully
151             log_daemon_msg "Stopping (force) $NAME"
152             force_stop
153             log_end_msg $?
154         fi
155         ;;
156   restart|force-reload)
157         log_daemon_msg "Restarting $NAME"
158         stop_server
159         # Wait some sensible amount, some server need this
160         [ -n "$DODTIME" ] && sleep $DODTIME
161         start_server
162         running
163         log_end_msg $?
164         ;;
165   status)
166         log_daemon_msg "Checking status of $NAME"
167         if running ;  then
168             log_progress_msg "running"
169             log_end_msg 0
170         else
171             log_progress_msg "apparently not running"
172             log_end_msg 1
173             exit 1
174         fi
175         ;;
176   # Use this if the daemon cannot reload
177   reload)
178         log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
179         log_warning_msg "cannot re-read the config file (use restart)."
180         ;;
181   *)
182         N=/etc/init.d/openvswitch-ipsec
183         echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
184         exit 1
185         ;;
186 esac
187
188 exit 0