Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / debian / openvswitch-controller.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-controller
22 # Required-Start:    $network $local_fs $remote_fs
23 # Required-Stop:     $remote_fs
24 # Should-Start:      $named
25 # Should-Stop:       
26 # Default-Start:     2 3 4 5
27 # Default-Stop:      0 1 6
28 # Short-Description: Open vSwitch controller
29 ### END INIT INFO
30
31 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
32
33 DAEMON=/usr/sbin/ovs-controller # Introduce the server's location here
34 NAME=ovs-controller         # Introduce the short server's name here
35 DESC=ovs-controller         # Introduce a short description here
36 LOGDIR=/var/log/openvswitch # Log directory to use
37
38 PIDFILE=/var/run/openvswitch/$NAME.pid 
39
40 test -x $DAEMON || exit 0
41
42 . /lib/lsb/init-functions
43
44 # Default options, these can be overriden by the information
45 # at /etc/default/openvswitch-controller
46 DAEMON_OPTS=""          # Additional options given to the server 
47
48 DODTIME=10              # Time to wait for the server to die, in seconds
49                         # If this value is set too low you might not
50                         # let some servers to die gracefully and
51                         # 'restart' will not work
52                         
53 LOGFILE=$LOGDIR/$NAME.log  # Server logfile
54 #DAEMONUSER=            # User to run the daemons as. If this value
55                         # is set start-stop-daemon will chuid the server
56
57 # Include defaults if available
58 default=/etc/default/openvswitch-controller
59 if [ -f $default ] ; then
60     . $default
61 fi
62
63 # Check that the user exists (if we set a user)
64 # Does the user exist?
65 if [ -n "$DAEMONUSER" ] ; then
66     if getent passwd | grep -q "^$DAEMONUSER:"; then
67         # Obtain the uid and gid
68         DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
69         DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
70     else
71         log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
72         exit 1
73     fi
74 fi
75
76
77 set -e
78
79 running_pid() {
80 # Check if a given process pid's cmdline matches a given name
81     pid=$1
82     name=$2
83     [ -z "$pid" ] && return 1 
84     [ ! -d /proc/$pid ] &&  return 1
85     cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
86     # Is this the expected server
87     [ "$cmd" != "$name" ] &&  return 1
88     return 0
89 }
90
91 running() {
92 # Check if the process is running looking at /proc
93 # (works for all users)
94
95     # No pidfile, probably no daemon present
96     [ ! -f "$PIDFILE" ] && return 1
97     pid=`cat $PIDFILE`
98     running_pid $pid $DAEMON || return 1
99     return 0
100 }
101
102 start_server() {
103     if [ -z "$LISTEN" ]; then
104         echo "$default: No connection methods configured, controller disabled" >&2
105         exit 0
106     fi
107
108     SSL_OPTS=
109     case $LISTEN in
110         *ssl*)
111             : ${PRIVKEY:=/etc/openvswitch-controller/privkey.pem}
112             : ${CERT:=/etc/openvswitch-controller/cert.pem}
113             : ${CACERT:=/etc/openvswitch-controller/cacert.pem}
114             if test ! -e "$PRIVKEY" || test ! -e "$CERT" ||
115                 test ! -e "$CACERT"; then
116                 if test ! -e "$PRIVKEY"; then
117                     echo "$PRIVKEY: private key missing" >&2
118                 fi
119                 if test ! -e "$CERT"; then
120                     echo "$CERT: certificate for private key missing" >&2
121                 fi
122                 if test ! -e "$CACERT"; then
123                     echo "$CACERT: CA certificate missing" >&2
124                 fi
125                 exit 1
126             fi
127             SSL_OPTS="--private-key=$PRIVKEY --certificate=$CERT --ca-cert=$CACERT"
128             ;;
129     esac
130
131 # Start the process using the wrapper
132         if [ -z "$DAEMONUSER" ] ; then
133             start-stop-daemon --start --pidfile $PIDFILE \
134                         --exec $DAEMON -- --detach --pidfile=$PIDFILE \
135                         $LISTEN $DAEMON_OPTS $SSL_OPTS
136             errcode=$?
137         else
138 # if we are using a daemonuser then change the user id
139             start-stop-daemon --start --quiet --pidfile $PIDFILE \
140                         --chuid $DAEMONUSER --exec $DAEMON -- \
141                         --detach --pidfile=$PIDFILE $LISTEN $DAEMON_OPTS \
142                         $SSL_OPTS
143             errcode=$?
144         fi
145         return $errcode
146 }
147
148 stop_server() {
149 # Stop the process using the wrapper
150         if [ -z "$DAEMONUSER" ] ; then
151             start-stop-daemon --stop --quiet --pidfile $PIDFILE \
152                         --exec $DAEMON
153             errcode=$?
154         else
155 # if we are using a daemonuser then look for process that match
156             start-stop-daemon --stop --quiet --pidfile $PIDFILE \
157                         --user $DAEMONUSER --exec $DAEMON
158             errcode=$?
159         fi
160
161         return $errcode
162 }
163
164 reload_server() {
165     [ ! -f "$PIDFILE" ] && return 1
166     pid=`cat $PIDFILE` # This is the daemon's pid
167     # Send a SIGHUP
168     kill -1 $pid
169     return $?
170 }
171
172 force_stop() {
173 # Force the process to die killing it manually
174     [ ! -e "$PIDFILE" ] && return
175     if running ; then
176         kill -15 $pid
177         # Is it really dead?
178         sleep "$DODTIME"
179         if running ; then
180             kill -9 $pid
181             sleep "$DODTIME"
182             if running ; then
183                 echo "Cannot kill $NAME (pid=$pid)!"
184                 exit 1
185             fi
186         fi
187     fi
188     rm -f $PIDFILE
189 }
190
191
192 case "$1" in
193   start)
194         log_daemon_msg "Starting $DESC " "$NAME"
195         # Check if it's running first
196         if running ;  then
197             log_progress_msg "apparently already running"
198             log_end_msg 0
199             exit 0
200         fi
201         if start_server && running ;  then
202             # It's ok, the server started and is running
203             log_end_msg 0
204         else
205             # Either we could not start it or it is not running
206             # after we did
207             # NOTE: Some servers might die some time after they start,
208             # this code does not try to detect this and might give
209             # a false positive (use 'status' for that)
210             log_end_msg 1
211         fi
212         ;;
213   stop)
214         log_daemon_msg "Stopping $DESC" "$NAME"
215         if running ; then
216             # Only stop the server if we see it running
217             stop_server
218             log_end_msg $?
219         else
220             # If it's not running don't do anything
221             log_progress_msg "apparently not running"
222             log_end_msg 0
223             exit 0
224         fi
225         ;;
226   force-stop)
227         # First try to stop gracefully the program
228         $0 stop
229         if running; then
230             # If it's still running try to kill it more forcefully
231             log_daemon_msg "Stopping (force) $DESC" "$NAME"
232             force_stop
233             log_end_msg $?
234         fi
235         ;;
236   restart|force-reload)
237         log_daemon_msg "Restarting $DESC" "$NAME"
238         stop_server
239         # Wait some sensible amount, some server need this
240         [ -n "$DODTIME" ] && sleep $DODTIME
241         start_server
242         running
243         log_end_msg $?
244         ;;
245   status)
246
247         log_daemon_msg "Checking status of $DESC" "$NAME"
248         if running ;  then
249             log_progress_msg "running"
250             log_end_msg 0
251         else
252             log_progress_msg "apparently not running"
253             log_end_msg 1
254             exit 1
255         fi
256         ;;
257   # Use this if the daemon cannot reload
258   reload)
259         log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
260         log_warning_msg "cannot re-read the config file (use restart)."
261         ;;
262   *)
263         N=/etc/init.d/openvswitch-controller
264         echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
265         exit 1
266         ;;
267 esac
268
269 exit 0