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