summaryrefslogtreecommitdiff
path: root/utilities/ovs-lib.in
blob: 7a15aaecf7cedba7c37149d5b9ecb08c8f629288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# This is a shell function library sourced by some Open vSwitch scripts.
# It is not intended to be invoked on its own.

# Copyright (C) 2009, 2010, 2011, 2012 Nicira Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

## ----------------- ##
## configure options ##
## ----------------- ##

# All of these should be substituted by the Makefile at build time.
logdir=${OVS_LOGDIR-'@LOGDIR@'}                 # /var/log/openvswitch
rundir=${OVS_RUNDIR-'@RUNDIR@'}                 # /var/run/openvswitch
sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'}     # /etc
etcdir=$sysconfdir/openvswitch                  # /etc/openvswitch
datadir=${OVS_PKGDATADIR-'@pkgdatadir@'}        # /usr/share/openvswitch
bindir=${OVS_BINDIR-'@bindir@'}                 # /usr/bin
sbindir=${OVS_SBINDIR-'@sbindir@'}              # /usr/sbin

VERSION='@VERSION@'

LC_ALL=C; export LC_ALL

## ------------- ##
## LSB functions ##
## ------------- ##

# Use the system's own implementations if it has any.
if test -e /etc/init.d/functions; then
    . /etc/init.d/functions
elif test -e /etc/rc.d/init.d/functions; then
    . /etc/rc.d/init.d/functions
elif test -e /lib/lsb/init-functions; then
    . /lib/lsb/init-functions
fi

# Implement missing functions (e.g. OpenSUSE lacks 'action').
if type log_success_msg >/dev/null 2>&1; then :; else
    log_success_msg () {
        printf '%s.\n' "$*"
    }
fi
if type log_failure_msg >/dev/null 2>&1; then :; else
    log_failure_msg () {
        printf '%s ... failed!\n' "$*"
    }
fi
if type log_warning_msg >/dev/null 2>&1; then :; else
    log_warning_msg () {
        printf '%s ... (warning).\n' "$*"
    }
fi
if type action >/dev/null 2>&1; then :; else
    action () {
       STRING=$1
       shift
       "$@"
       rc=$?
       if test $rc = 0; then
            log_success_msg "$STRING"
       else
            log_failure_msg "$STRING"
       fi
       return $rc
    }
fi

## ------- ##
## Daemons ##
## ------- ##

pid_exists () {
    # This is better than "kill -0" because it doesn't require permission to
    # send a signal (so daemon_status in particular works as non-root).
    test -d /proc/"$1"
}

start_daemon () {
    priority=$1
    shift
    daemon=$1

    # drop core files in a sensible place
    test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
    set "$@" --no-chdir
    cd "$DAEMON_CWD"

    # log file
    test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
    set "$@" --log-file="$logdir/$daemon.log"

    # pidfile and monitoring
    test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
    set "$@" --pidfile="$rundir/$daemon.pid"
    set "$@" --detach --monitor

    # priority
    if test X"$priority" != X; then
        set nice -n "$priority" "$@"
    fi

    action "Starting $daemon" "$@"
}

DAEMON_CWD=/
stop_daemon () {
    if test -e "$rundir/$1.pid"; then
        if pid=`cat "$rundir/$1.pid"`; then
            for action in TERM .1 .25 .65 1 1 1 1 KILL 1 1 1 1 FAIL; do
                case $action in
                    TERM)
                        action "Killing $1 ($pid)" kill $pid
                        ;;
                    KILL)
                        action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
                        ;;
                    FAIL)
                        log_failure_msg "Killing $1 ($pid) failed"
                        return 1
                        ;;
                    *)
                        if pid_exists $pid >/dev/null 2>&1; then
                            sleep $action
                        else
                            return 0
                        fi
                        ;;
                esac
            done
        fi
    fi
    log_success_msg "$1 is not running"
}

daemon_status () {
    pidfile=$rundir/$1.pid
    if test -e "$pidfile"; then
        if pid=`cat "$pidfile"`; then
            if pid_exists "$pid"; then
                echo "$1 is running with pid $pid"
                return 0
            else
                echo "Pidfile for $1 ($pidfile) is stale"
            fi
        else
            echo "Pidfile for $1 ($pidfile) exists but cannot be read"
        fi
    else
        echo "$1 is not running"
    fi
    return 1
}

daemon_is_running () {
    pidfile=$rundir/$1.pid
    test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
} >/dev/null 2>&1