initial checkin
[util-vserver.git] / util-vserver / scripts / vshelper
1 #! /bin/bash
2
3 # Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 #  
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
8 #  
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #  
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 ## Usage: vshelper <xid> <action> <args>
19
20 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
21 test -e "$UTIL_VSERVER_VARS" || {
22     echo "Can not find util-vserver installation; aborting..." >&2
23     exit 1
24 }
25 . "$UTIL_VSERVER_VARS"
26 . "$_LIB_FUNCTIONS"
27
28 function showHelp
29 {
30     echo $"\
31 Usage: vshelper <xid> <event> <args>*
32
33 Report bugs to <$PACKAGE_BUGREPORT>."
34     exit 0
35 }
36
37 function showVersion
38 {
39     echo $"\
40 vshelper $PACKAGE_VERSION -- userspace reboot helper
41 This program is part of $PACKAGE_STRING
42
43 Copyright (C) 2004 Enrico Scholz
44 This program is free software; you may redistribute it under the terms of
45 the GNU General Public License.  This program has absolutely no warranty."
46     exit 0
47 }
48
49 function doInternalMethod
50 {
51     local method=$1
52     case "$method" in
53         (restart)
54             case "$ACTION" in
55                 (restart*)
56                     logging $"Restarting vserver '$VSERVER'"
57                     execute $_VSERVER "$VSERVER" restart
58                     ;;
59                 (halt|poweroff)
60                     logging $"Stopping vserver '$VSERVER'"
61                     execute $_VSERVER "$VSERVER" stop
62                     ;;
63                 (swsusp)
64                     ## TODO: any senseful action here? Perhaps shutdown scheduler for it?
65                     exit 0
66                 (*)
67                     warning $"Unknown action '$ACTION' for vserver '$VSERVER'"
68                     exit 1
69             esac
70             ;;
71             
72         (sync)
73             local f=${METHOD_ARGS[0]}
74             test "$f" ||
75                 panic $"Insufficent arguments for method '$method' and vserver '$VSERVER'"
76
77             test -p "$f" ||
78                 panic $"File '$f' which is required for synchronisation of vserver '$VSERVER' is not a pipe"
79
80             echo "$ACTION" >"$f"
81             ;;
82
83         (*)
84             local script
85             findObject -x script "$CONFDIR"/.defaults/apps/vshelper-methods/"$method" "$PKGLIBDIR"/vshelper-methods/"$method" ''
86             
87             test '$script' || {
88                 warning $"No handler for internal method '$method' found"
89                 exit 1
90             }
91
92             export VSERVER
93             execute "$script" "${ARGS[@]}"
94     esac
95 }
96     
97 function doDefaultMethod
98 {
99     local handler
100
101     vshelper.getHandler handler "$VSERVER" "$ACTION" || {
102         warning $"No handler configured for action '$ACTION' on vserver '$VSERVER'"
103         exit 1
104     }
105
106     case "$handler" in
107         (/*)    execute "$handler" "${ARGS[@]}";;
108         (:*)    doInternalMethod "${handler##:}" "$@";;
109     esac
110 }
111
112 #===========
113
114
115 test "$1" != '--version' || showVersion
116 test "$1" != '--help'    || showHelp
117 test "$#" -ge 2 ||
118     panic $"vshelper called with missing arguments; try '--help' for more information"
119
120 vshelper.isEnabled || exit 0
121 logging "$(date): vshelper $*"
122     
123 set -eu
124
125 declare -r XID=$1
126 declare -r ACTION=$2
127
128
129 this_xid=$($_VSERVER_INFO - XID)
130 pxid=
131 while true; do
132     pxid=$($_VSERVER_INFO "$XID" PXID) || break
133     test "$pxid" -ne "$this_xid"       || break
134     xid=$pxid
135 done
136
137 vserver_id=$($_VSERVER_INFO "$XID" ID) ||
138     panic $"No responsible vserver found for xid '$1' ($XID); aborting..."
139     
140 test "$1" = "$XID" || {
141     logging "Giving 'vshelper' task for '$1' to parent vserver '$vserver_id' ($XID)"
142     execute $_VSERVER "$vserver_id" exec $_VSHELPER "$@"
143 }
144
145 #===========
146
147 ARGS=( "$@" )
148
149 declare -a state
150 getFileArray state "$VSHELPERSTATEDIR/$XID" && test ${#state[@]} -ge 2 || {
151     logging "'vshelper' not configured for xid '$XID'"
152     exit 0
153 }
154
155 declare -r VSERVER=${state[0]}
156
157 cfg_xid=$($_VSERVER_INFO "${state[0]}" CONTEXT false) ||
158     panic $"Configured vserver '$VSERVER' does not seem to run; aborting..."
159
160 test "$cfg_xid" = "$XID" ||
161     panic $"Configured ($cfg_xid) and actual ($XID) xid for vserver '$VSERVER' are mismatching; aborting..."
162
163 cur_xid=$($_VSERVER_INFO "$VSERVER" CONTEXT false) ||
164     panic $"Vserver '$VSERVER' with '$XID' does not seem to run; strange..."
165
166 test "$cur_xid" = "$XID" ||
167     panic $"Expected ($XID) and actual ($cur_xid) xid for vserver '$VSERVER' are mismatching; strange..."
168
169
170 method=${state[1]}
171 if test "${#state[@]}" -gt 2; then
172     unset state[0] state[1]
173     declare -ra METHOD_ARGS=( "${state[@]}" )
174 else
175     declare -ra METHOD_ARGS=()
176 fi
177
178 case "$method" in
179     (default)   doDefaultMethod "$@";;
180     (sync)      doInternalMethod "$method" "$@";;
181     (*)         panic $"Unknown method '$method' used by vserver '$VSERVER'";;
182 esac