utilities: install ovs-lib.sh as data not a script
[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 Nicira Networks, 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 case @BUILDNR@ in
33     [1-9]*) BUILDNR='+build@BUILDNR@' ;;
34     *) BUILDNR= ;;
35 esac
36
37 LC_ALL=C; export LC_ALL
38
39 ## ------------- ##
40 ## LSB functions ##
41 ## ------------- ##
42
43 # Use the system's own implementations if it has any.
44 if test -e /etc/init.d/functions; then
45     . /etc/init.d/functions
46 elif test -e /etc/rc.d/init.d/functions; then
47     . /etc/rc.d/init.d/functions
48 elif test -e /lib/lsb/init-functions; then
49     . /lib/lsb/init-functions
50 fi
51
52 # Implement missing functions (e.g. OpenSUSE lacks 'action').
53 if type log_success_msg >/dev/null 2>&1; then :; else
54     log_success_msg () {
55         printf '%s.\n' "$*"
56     }
57 fi
58 if type log_failure_msg >/dev/null 2>&1; then :; else
59     log_failure_msg () {
60         printf '%s ... failed!\n' "$*"
61     }
62 fi
63 if type log_warning_msg >/dev/null 2>&1; then :; else
64     log_warning_msg () {
65         printf '%s ... (warning).\n' "$*"
66     }
67 fi
68 if type action >/dev/null 2>&1; then :; else
69     action () {
70        STRING=$1
71        shift
72        "$@"
73        rc=$?
74        if test $rc = 0; then
75             log_success_msg "$STRING"
76        else
77             log_failure_msg "$STRING"
78        fi
79        return $rc
80     }
81 fi
82
83 ## ------- ##
84 ## Daemons ##
85 ## ------- ##
86
87 pid_exists () {
88     # This is better than "kill -0" because it doesn't require permission to
89     # send a signal (so daemon_status in particular works as non-root).
90     test -d /proc/"$1"
91 }
92
93 start_daemon () {
94     priority=$1
95     shift
96     daemon=$1
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     # priority
113     if test X"$priority" != X; then
114         set nice -n "$priority" "$@"
115     fi
116
117     action "Starting $daemon" "$@"
118 }
119
120 DAEMON_CWD=/
121 stop_daemon () {
122     if test -e "$rundir/$1.pid"; then
123         if pid=`cat "$rundir/$1.pid"`; then
124             for action in TERM .1 .25 .65 1 1 1 1 KILL 1 1 1 1 FAIL; do
125                 case $action in
126                     TERM)
127                         action "Killing $1 ($pid)" kill $pid
128                         ;;
129                     KILL)
130                         action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
131                         ;;
132                     FAIL)
133                         log_failure_msg "Killing $1 ($pid) failed"
134                         return 1
135                         ;;
136                     *)
137                         if pid_exists $pid >/dev/null 2>&1; then
138                             sleep $action
139                         else
140                             return 0
141                         fi
142                         ;;
143                 esac
144             done
145         fi
146     fi
147     log_success_msg "$1 is not running"
148 }
149
150 daemon_status () {
151     pidfile=$rundir/$1.pid
152     if test -e "$pidfile"; then
153         if pid=`cat "$pidfile"`; then
154             if pid_exists "$pid"; then
155                 echo "$1 is running with pid $pid"
156                 return 0
157             else
158                 echo "Pidfile for $1 ($pidfile) is stale"
159             fi
160         else
161             echo "Pidfile for $1 ($pidfile) exists but cannot be read"
162         fi
163     else
164         echo "$1 is not running"
165     fi
166     return 1
167 }
168
169 daemon_is_running () {
170     pidfile=$rundir/$1.pid
171     test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
172 } >/dev/null 2>&1