Make the location of the database separately configurable.
[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 dbdir=${OVS_DBDIR-'@DBDIR@'}                    # /etc/openvswitch
26                                                 # or /var/lib/openvswitch
27 sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'}     # /etc
28 etcdir=$sysconfdir/openvswitch                  # /etc/openvswitch
29 datadir=${OVS_PKGDATADIR-'@pkgdatadir@'}        # /usr/share/openvswitch
30 bindir=${OVS_BINDIR-'@bindir@'}                 # /usr/bin
31 sbindir=${OVS_SBINDIR-'@sbindir@'}              # /usr/sbin
32
33 VERSION='@VERSION@'
34
35 LC_ALL=C; export LC_ALL
36
37 ## ------------- ##
38 ## LSB functions ##
39 ## ------------- ##
40
41 # Use the system's own implementations if it has any.
42 if test -e /etc/init.d/functions; then
43     . /etc/init.d/functions
44 elif test -e /etc/rc.d/init.d/functions; then
45     . /etc/rc.d/init.d/functions
46 elif test -e /lib/lsb/init-functions; then
47     . /lib/lsb/init-functions
48 fi
49
50 # Implement missing functions (e.g. OpenSUSE lacks 'action').
51 if type log_success_msg >/dev/null 2>&1; then :; else
52     log_success_msg () {
53         printf '%s.\n' "$*"
54     }
55 fi
56 if type log_failure_msg >/dev/null 2>&1; then :; else
57     log_failure_msg () {
58         printf '%s ... failed!\n' "$*"
59     }
60 fi
61 if type log_warning_msg >/dev/null 2>&1; then :; else
62     log_warning_msg () {
63         printf '%s ... (warning).\n' "$*"
64     }
65 fi
66 if type action >/dev/null 2>&1; then :; else
67     action () {
68        STRING=$1
69        shift
70        "$@"
71        rc=$?
72        if test $rc = 0; then
73             log_success_msg "$STRING"
74        else
75             log_failure_msg "$STRING"
76        fi
77        return $rc
78     }
79 fi
80
81 ## ------- ##
82 ## Daemons ##
83 ## ------- ##
84
85 pid_exists () {
86     # This is better than "kill -0" because it doesn't require permission to
87     # send a signal (so daemon_status in particular works as non-root).
88     test -d /proc/"$1"
89 }
90
91 start_daemon () {
92     priority=$1
93     wrapper=$2
94     shift; shift
95     daemon=$1
96     strace=""
97
98     # drop core files in a sensible place
99     test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
100     set "$@" --no-chdir
101     cd "$DAEMON_CWD"
102
103     # log file
104     test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
105     set "$@" --log-file="$logdir/$daemon.log"
106
107     # pidfile and monitoring
108     test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
109     set "$@" --pidfile="$rundir/$daemon.pid"
110     set "$@" --detach --monitor
111
112     # wrapper
113     case $wrapper in
114         valgrind)
115             if (valgrind --version) > /dev/null 2>&1; then
116                 set valgrind -q --leak-check=full --time-stamp=yes \
117                     --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
118             else
119                 log_failure_msg "valgrind not installed, running $daemon without it"
120             fi
121             ;;
122         strace)
123             if (strace -V) > /dev/null 2>&1; then
124                 strace="strace -tt -T -s 256 -ff"
125                 if (strace -DV) > /dev/null 2>&1; then
126                     # Has the -D option.
127                     set $strace -D -o "$logdir/$daemon.strace.log" "$@"
128                     strace=""
129                 fi
130             else
131                 log_failure_msg "strace not installed, running $daemon without it"
132             fi
133             ;;
134         '')
135             ;;
136         *)
137             log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
138             ;;
139     esac
140
141     # priority
142     if test X"$priority" != X; then
143         set nice -n "$priority" "$@"
144     fi
145
146     action "Starting $daemon" "$@"
147
148     if test X"$strace" != X; then
149         # Strace doesn't have the -D option so we attach after the fact.
150         setsid $strace -o "$logdir/$daemon.strace.log" \
151             -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
152     fi
153 }
154
155 DAEMON_CWD=/
156 stop_daemon () {
157     if test -e "$rundir/$1.pid"; then
158         if pid=`cat "$rundir/$1.pid"`; then
159             for action in TERM .1 .25 .65 1 1 1 1 KILL 1 1 1 1 FAIL; do
160                 case $action in
161                     TERM)
162                         action "Killing $1 ($pid)" kill $pid
163                         ;;
164                     KILL)
165                         action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
166                         ;;
167                     FAIL)
168                         log_failure_msg "Killing $1 ($pid) failed"
169                         return 1
170                         ;;
171                     *)
172                         if pid_exists $pid >/dev/null 2>&1; then
173                             sleep $action
174                         else
175                             return 0
176                         fi
177                         ;;
178                 esac
179             done
180         fi
181     fi
182     log_success_msg "$1 is not running"
183 }
184
185 daemon_status () {
186     pidfile=$rundir/$1.pid
187     if test -e "$pidfile"; then
188         if pid=`cat "$pidfile"`; then
189             if pid_exists "$pid"; then
190                 echo "$1 is running with pid $pid"
191                 return 0
192             else
193                 echo "Pidfile for $1 ($pidfile) is stale"
194             fi
195         else
196             echo "Pidfile for $1 ($pidfile) exists but cannot be read"
197         fi
198     else
199         echo "$1 is not running"
200     fi
201     return 1
202 }
203
204 daemon_is_running () {
205     pidfile=$rundir/$1.pid
206     test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
207 } >/dev/null 2>&1