1#
2# Copyright (c) 2005 XenSource Ltd.
3#
4# This library is free software; you can redistribute it and/or
5# modify it under the terms of version 2.1 of the GNU Lesser General Public
6# License as published by the Free Software Foundation.
7#
8# This library is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11# Lesser General Public License for more details.
12#
13# You should have received a copy of the GNU Lesser General Public
14# License along with this library; If not, see <http://www.gnu.org/licenses/>.
15#
16
17dir=$(dirname "$0")
18. "$dir/hotplugpath.sh"
19. "$dir/logging.sh"
20. "$dir/xen-script-common.sh"
21. "$dir/locking.sh"
22
23exec 2>>@XEN_LOG_DIR@/xen-hotplug.log
24
25export PATH="${bindir}:${sbindir}:${LIBEXEC_BIN}:/sbin:/bin:/usr/bin:/usr/sbin:$PATH"
26export LD_LIBRARY_PATH="${libdir}${LD_LIBRARY_PATH+:}$LD_LIBRARY_PATH"
27export LANG="POSIX"
28unset $(set | grep ^LC_ | cut -d= -f1)
29
30fatal() {
31  _xenstore_write "$XENBUS_PATH/hotplug-error" "$*" \
32                  "$XENBUS_PATH/hotplug-status" error
33  log err "$@"
34  exit 1
35}
36
37success() {
38  # Tell DevController that backend is "connected"
39  xenstore_write "$XENBUS_PATH/hotplug-status" connected
40}
41
42do_or_die() {
43  "$@" || fatal "$@ failed"
44}
45
46do_without_error() {
47  "$@" 2>/dev/null || log debug "$@ failed"
48}
49
50sigerr() {
51  fatal "$0 failed; error detected."
52}
53
54trap sigerr ERR
55
56
57##
58# xenstore_read <path>+
59#
60# Read each of the given paths, returning each result on a separate line, or
61# exit this script if any of the paths is missing.
62#
63xenstore_read() {
64  local v=$(xenstore-read "$@" || true)
65  [ "$v" != "" ] || fatal "xenstore-read $@ failed."
66  echo "$v"
67}
68
69
70##
71# xenstore_read_default <path> <default>
72#
73# Read the given path, returning the value there or the given default if the
74# path is not present.
75#
76xenstore_read_default() {
77  xenstore-read "$1" 2>/dev/null || echo "$2"
78}
79
80
81##
82# _xenstore_write (<path> <value>)+
83#
84# Write each of the key/value pairs to the store.
85#
86_xenstore_write() {
87  log debug "Writing $@ to xenstore."
88  xenstore-write "$@"
89}
90
91##
92# xenstore_write (<path> <value>)+
93#
94# Write each of the key/value pairs to the store, and exit this script if any
95# such writing fails.
96#
97xenstore_write() {
98  _xenstore_write "$@" || fatal "Writing $@ to xenstore failed."
99}
100
101##
102# call_hooks <devtype> <hook>
103#
104# Execute each hook in the <hook> directory.
105#
106call_hooks() {
107  for f in @XEN_SCRIPT_DIR@/${1}-${2}.d/*.hook; do
108    if [ -x "$f" ]; then . "$f"; fi
109  done
110}
111
112log debug "$@" "XENBUS_PATH=$XENBUS_PATH"
113