1 # This is a shell function library sourced by some Open vSwitch scripts.
2 # It is not intended to be invoked on its own.
4 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
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:
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 ## ----------------- ##
19 ## configure options ##
20 ## ----------------- ##
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
31 # /etc/openvswitch or /var/lib/openvswitch
32 if test X"$OVS_DBDIR" != X; then
34 elif test X"$OVS_SYSCONFDIR" != X; then
35 dbdir=$OVS_SYSCONFDIR/openvswitch
41 echo "$@" >> "${logdir}/ovs-ctl.log"
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" "$@"
53 # In case of the command 'status', we should return the exit status
54 # of ovs-ctl. It is also useful to document the o/p in ovs-ctl.log.
55 display=`"${datadir}/scripts/ovs-ctl" "$@" 2>&1`
57 echo "${display}" | tee -a "${logdir}/ovs-ctl.log"
61 echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
62 "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
71 LC_ALL=C; export LC_ALL
77 # Use the system's own implementations if it has any.
78 if test -e /etc/init.d/functions; then
79 . /etc/init.d/functions
80 elif test -e /etc/rc.d/init.d/functions; then
81 . /etc/rc.d/init.d/functions
82 elif test -e /lib/lsb/init-functions; then
83 . /lib/lsb/init-functions
86 # Implement missing functions (e.g. OpenSUSE lacks 'action').
87 if type log_success_msg >/dev/null 2>&1; then :; else
92 if type log_failure_msg >/dev/null 2>&1; then :; else
94 printf '%s ... failed!\n' "$*"
97 if type log_warning_msg >/dev/null 2>&1; then :; else
99 printf '%s ... (warning).\n' "$*"
102 if type action >/dev/null 2>&1; then :; else
108 if test $rc = 0; then
109 log_success_msg "$STRING"
111 log_failure_msg "$STRING"
122 # This is better than "kill -0" because it doesn't require permission to
123 # send a signal (so daemon_status in particular works as non-root).
134 # drop core files in a sensible place
135 test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
140 test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
141 set "$@" --log-file="$logdir/$daemon.log"
143 # pidfile and monitoring
144 test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
145 set "$@" --pidfile="$rundir/$daemon.pid"
146 set "$@" --detach --monitor
151 if (valgrind --version) > /dev/null 2>&1; then
152 set valgrind -q --leak-check=full --time-stamp=yes \
153 --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
155 log_failure_msg "valgrind not installed, running $daemon without it"
159 if (strace -V) > /dev/null 2>&1; then
160 strace="strace -tt -T -s 256 -ff"
161 if (strace -DV) > /dev/null 2>&1; then
163 set $strace -D -o "$logdir/$daemon.strace.log" "$@"
167 log_failure_msg "strace not installed, running $daemon without it"
171 set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
176 log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
181 if test X"$priority" != X; then
182 set nice -n "$priority" "$@"
185 action "Starting $daemon" "$@"
187 if test X"$strace" != X; then
188 # Strace doesn't have the -D option so we attach after the fact.
189 setsid $strace -o "$logdir/$daemon.strace.log" \
190 -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
195 if test -e "$rundir/$1.pid"; then
196 if pid=`cat "$rundir/$1.pid"`; then
197 for action in TERM .1 .25 .65 1 1 1 1 KILL 1 1 1 2 10 15 30 FAIL; do
198 if pid_exists "$pid" >/dev/null 2>&1; then :; else
203 action "Killing $1 ($pid)" kill $pid
206 action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
209 log_failure_msg "Killing $1 ($pid) failed"
219 log_success_msg "$1 is not running"
223 pidfile=$rundir/$1.pid
224 if test -e "$pidfile"; then
225 if pid=`cat "$pidfile"`; then
226 if pid_exists "$pid"; then
227 echo "$1 is running with pid $pid"
230 echo "Pidfile for $1 ($pidfile) is stale"
233 echo "Pidfile for $1 ($pidfile) exists but cannot be read"
236 echo "$1 is not running"
241 daemon_is_running () {
242 pidfile=$rundir/$1.pid
243 test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"