debian: force-reload-kmod while package upgrading.
[sliver-openvswitch.git] / utilities / ovs-lib.in
1 # This is a shell function library sourced by some Open vSwitch scripts.
2 # It is not intended to be invoked on its own.
3
4 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at:
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 ## ----------------- ##
19 ## configure options ##
20 ## ----------------- ##
21
22 # All of these should be substituted by the Makefile at build time.
23 logdir=${OVS_LOGDIR-'@LOGDIR@'}                 # /var/log/openvswitch
24 rundir=${OVS_RUNDIR-'@RUNDIR@'}                 # /var/run/openvswitch
25 sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'}     # /etc
26 etcdir=$sysconfdir/openvswitch                  # /etc/openvswitch
27 datadir=${OVS_PKGDATADIR-'@pkgdatadir@'}        # /usr/share/openvswitch
28 bindir=${OVS_BINDIR-'@bindir@'}                 # /usr/bin
29 sbindir=${OVS_SBINDIR-'@sbindir@'}              # /usr/sbin
30
31 # /etc/openvswitch or /var/lib/openvswitch
32 if test X"$OVS_DBDIR" != X; then
33     dbdir=$OVS_DBDIR
34 elif test X"$OVS_SYSCONFDIR" != X; then
35     dbdir=$OVS_SYSCONFDIR/openvswitch
36 else
37     dbdir='@DBDIR@'
38 fi
39
40 ovs_ctl_log () {
41     echo "$@" >> "${logdir}/ovs-ctl.log"
42 }
43
44 ovs_ctl () {
45     case "$@" in
46         *"=strace"*)
47             # In case of running the daemon with strace, piping the o/p causes
48             # the script to block (strace probably does not close the inherited
49             # pipe). So, do not log the o/p to ovs-ctl.log.
50             "${datadir}/scripts/ovs-ctl" "$@"
51         ;;
52         *)
53             echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
54             "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
55         ;;
56     esac
57 }
58
59 VERSION='@VERSION@'
60
61 DAEMON_CWD=/
62
63 LC_ALL=C; export LC_ALL
64
65 ## ------------- ##
66 ## LSB functions ##
67 ## ------------- ##
68
69 # Use the system's own implementations if it has any.
70 if test -e /etc/init.d/functions; then
71     . /etc/init.d/functions
72 elif test -e /etc/rc.d/init.d/functions; then
73     . /etc/rc.d/init.d/functions
74 elif test -e /lib/lsb/init-functions; then
75     . /lib/lsb/init-functions
76 fi
77
78 # Implement missing functions (e.g. OpenSUSE lacks 'action').
79 if type log_success_msg >/dev/null 2>&1; then :; else
80     log_success_msg () {
81         printf '%s.\n' "$*"
82     }
83 fi
84 if type log_failure_msg >/dev/null 2>&1; then :; else
85     log_failure_msg () {
86         printf '%s ... failed!\n' "$*"
87     }
88 fi
89 if type log_warning_msg >/dev/null 2>&1; then :; else
90     log_warning_msg () {
91         printf '%s ... (warning).\n' "$*"
92     }
93 fi
94 if type action >/dev/null 2>&1; then :; else
95     action () {
96        STRING=$1
97        shift
98        "$@"
99        rc=$?
100        if test $rc = 0; then
101             log_success_msg "$STRING"
102        else
103             log_failure_msg "$STRING"
104        fi
105        return $rc
106     }
107 fi
108
109 ## ------- ##
110 ## Daemons ##
111 ## ------- ##
112
113 pid_exists () {
114     # This is better than "kill -0" because it doesn't require permission to
115     # send a signal (so daemon_status in particular works as non-root).
116     test -d /proc/"$1"
117 }
118
119 start_daemon () {
120     priority=$1
121     wrapper=$2
122     shift; shift
123     daemon=$1
124     strace=""
125
126     # drop core files in a sensible place
127     test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
128     set "$@" --no-chdir
129     cd "$DAEMON_CWD"
130
131     # log file
132     test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
133     set "$@" --log-file="$logdir/$daemon.log"
134
135     # pidfile and monitoring
136     test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
137     set "$@" --pidfile="$rundir/$daemon.pid"
138     set "$@" --detach --monitor
139
140     # wrapper
141     case $wrapper in
142         valgrind)
143             if (valgrind --version) > /dev/null 2>&1; then
144                 set valgrind -q --leak-check=full --time-stamp=yes \
145                     --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
146             else
147                 log_failure_msg "valgrind not installed, running $daemon without it"
148             fi
149             ;;
150         strace)
151             if (strace -V) > /dev/null 2>&1; then
152                 strace="strace -tt -T -s 256 -ff"
153                 if (strace -DV) > /dev/null 2>&1; then
154                     # Has the -D option.
155                     set $strace -D -o "$logdir/$daemon.strace.log" "$@"
156                     strace=""
157                 fi
158             else
159                 log_failure_msg "strace not installed, running $daemon without it"
160             fi
161             ;;
162         glibc)
163             set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
164             ;;
165         '')
166             ;;
167         *)
168             log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
169             ;;
170     esac
171
172     # priority
173     if test X"$priority" != X; then
174         set nice -n "$priority" "$@"
175     fi
176
177     action "Starting $daemon" "$@"
178
179     if test X"$strace" != X; then
180         # Strace doesn't have the -D option so we attach after the fact.
181         setsid $strace -o "$logdir/$daemon.strace.log" \
182             -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
183     fi
184 }
185
186 stop_daemon () {
187     if test -e "$rundir/$1.pid"; then
188         if pid=`cat "$rundir/$1.pid"`; then
189             for action in TERM .1 .25 .65 1 1 1 1 KILL 1 1 1 2 10 15 30 FAIL; do
190                 if pid_exists "$pid" >/dev/null 2>&1; then :; else
191                     return 0
192                 fi
193                 case $action in
194                     TERM)
195                         action "Killing $1 ($pid)" kill $pid
196                         ;;
197                     KILL)
198                         action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
199                         ;;
200                     FAIL)
201                         log_failure_msg "Killing $1 ($pid) failed"
202                         return 1
203                         ;;
204                     *)
205                         sleep $action
206                         ;;
207                 esac
208             done
209         fi
210     fi
211     log_success_msg "$1 is not running"
212 }
213
214 daemon_status () {
215     pidfile=$rundir/$1.pid
216     if test -e "$pidfile"; then
217         if pid=`cat "$pidfile"`; then
218             if pid_exists "$pid"; then
219                 echo "$1 is running with pid $pid"
220                 return 0
221             else
222                 echo "Pidfile for $1 ($pidfile) is stale"
223             fi
224         else
225             echo "Pidfile for $1 ($pidfile) exists but cannot be read"
226         fi
227     else
228         echo "$1 is not running"
229     fi
230     return 1
231 }
232
233 daemon_is_running () {
234     pidfile=$rundir/$1.pid
235     test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
236 } >/dev/null 2>&1