0b6f42ffcaf770556ede3c7e3efb6832b62eaafe
[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 stdintoexitstatus () {
45     read exitstatus
46     return $exitstatus
47 }
48
49 ovs_ctl () {
50     case "$@" in
51         *"=strace"*)
52             # In case of running the daemon with strace, piping the o/p causes
53             # the script to block (strace probably does not close the inherited
54             # pipe). So, do not log the o/p to ovs-ctl.log.
55             "${datadir}/scripts/ovs-ctl" "$@"
56         ;;
57         *)
58             # Tee ovs-ctl output to ovs-ctl.log and yield ovs-ctl's exit
59             # status. See (line wrapped)
60             # http://unix.stackexchange.com/questions/14270/\
61             # get-exit-status-of-process-thats-piped-to-another/70675#70675
62             echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
63             ( ( ( ( ("${datadir}/scripts/ovs-ctl" "$@"  2>&1 ; echo $? >&3) \
64             | tee -a "${logdir}/ovs-ctl.log") >&4) 3>&1) | stdintoexitstatus) \
65             4>&1
66         ;;
67     esac
68 }
69
70 VERSION='@VERSION@'
71
72 DAEMON_CWD=/
73
74 LC_ALL=C; export LC_ALL
75
76 ## ------------- ##
77 ## LSB functions ##
78 ## ------------- ##
79
80 # Use the system's own implementations if it has any.
81 if test -e /etc/init.d/functions; then
82     . /etc/init.d/functions
83 elif test -e /etc/rc.d/init.d/functions; then
84     . /etc/rc.d/init.d/functions
85 elif test -e /lib/lsb/init-functions; then
86     . /lib/lsb/init-functions
87 fi
88
89 # Implement missing functions (e.g. OpenSUSE lacks 'action').
90 if type log_success_msg >/dev/null 2>&1; then :; else
91     log_success_msg () {
92         printf '%s.\n' "$*"
93     }
94 fi
95 if type log_failure_msg >/dev/null 2>&1; then :; else
96     log_failure_msg () {
97         printf '%s ... failed!\n' "$*"
98     }
99 fi
100 if type log_warning_msg >/dev/null 2>&1; then :; else
101     log_warning_msg () {
102         printf '%s ... (warning).\n' "$*"
103     }
104 fi
105 if type action >/dev/null 2>&1; then :; else
106     action () {
107        STRING=$1
108        shift
109        "$@"
110        rc=$?
111        if test $rc = 0; then
112             log_success_msg "$STRING"
113        else
114             log_failure_msg "$STRING"
115        fi
116        return $rc
117     }
118 fi
119
120 ## ------- ##
121 ## Daemons ##
122 ## ------- ##
123
124 pid_exists () {
125     # This is better than "kill -0" because it doesn't require permission to
126     # send a signal (so daemon_status in particular works as non-root).
127     test -d /proc/"$1"
128 }
129
130 start_daemon () {
131     priority=$1
132     wrapper=$2
133     shift; shift
134     daemon=$1
135     strace=""
136
137     # drop core files in a sensible place
138     test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
139     set "$@" --no-chdir
140     cd "$DAEMON_CWD"
141
142     # log file
143     test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
144     set "$@" --log-file="$logdir/$daemon.log"
145
146     # pidfile and monitoring
147     test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
148     set "$@" --pidfile="$rundir/$daemon.pid"
149     set "$@" --detach --monitor
150
151     # wrapper
152     case $wrapper in
153         valgrind)
154             if (valgrind --version) > /dev/null 2>&1; then
155                 set valgrind -q --leak-check=full --time-stamp=yes \
156                     --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
157             else
158                 log_failure_msg "valgrind not installed, running $daemon without it"
159             fi
160             ;;
161         strace)
162             if (strace -V) > /dev/null 2>&1; then
163                 strace="strace -tt -T -s 256 -ff"
164                 if (strace -DV) > /dev/null 2>&1; then
165                     # Has the -D option.
166                     set $strace -D -o "$logdir/$daemon.strace.log" "$@"
167                     strace=""
168                 fi
169             else
170                 log_failure_msg "strace not installed, running $daemon without it"
171             fi
172             ;;
173         glibc)
174             set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
175             ;;
176         '')
177             ;;
178         *)
179             log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
180             ;;
181     esac
182
183     # priority
184     if test X"$priority" != X; then
185         set nice -n "$priority" "$@"
186     fi
187
188     action "Starting $daemon" "$@"
189
190     if test X"$strace" != X; then
191         # Strace doesn't have the -D option so we attach after the fact.
192         setsid $strace -o "$logdir/$daemon.strace.log" \
193             -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
194     fi
195 }
196
197 stop_daemon () {
198     if test -e "$rundir/$1.pid"; then
199         if pid=`cat "$rundir/$1.pid"`; then
200             for action in TERM .1 .25 .65 1 1 1 1 KILL 1 1 1 2 10 15 30 FAIL; do
201                 if pid_exists "$pid" >/dev/null 2>&1; then :; else
202                     return 0
203                 fi
204                 case $action in
205                     TERM)
206                         action "Killing $1 ($pid)" kill $pid
207                         ;;
208                     KILL)
209                         action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
210                         ;;
211                     FAIL)
212                         log_failure_msg "Killing $1 ($pid) failed"
213                         return 1
214                         ;;
215                     *)
216                         sleep $action
217                         ;;
218                 esac
219             done
220         fi
221     fi
222     log_success_msg "$1 is not running"
223 }
224
225 daemon_status () {
226     pidfile=$rundir/$1.pid
227     if test -e "$pidfile"; then
228         if pid=`cat "$pidfile"`; then
229             if pid_exists "$pid"; then
230                 echo "$1 is running with pid $pid"
231                 return 0
232             else
233                 echo "Pidfile for $1 ($pidfile) is stale"
234             fi
235         else
236             echo "Pidfile for $1 ($pidfile) exists but cannot be read"
237         fi
238     else
239         echo "$1 is not running"
240     fi
241     return 1
242 }
243
244 daemon_is_running () {
245     pidfile=$rundir/$1.pid
246     test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
247 } >/dev/null 2>&1