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