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