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