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