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