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