b9dfdf9c129693a38071931c3bc76717d286ee11
[util-vserver.git] / scripts / vshelper
1 #! /usr/lib/util-vserver/sigexec /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 (the file '$UTIL_VSERVER_VARS' would be expected); 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
58                     local sync_dir
59                     vshelper.initStopSync sync_dir
60
61                     spawn $_VSERVER --defaulttty "$VSERVER" restart &
62
63                     disown %%
64                     vshelper.waitForStopSync "$sync_dir"
65                     ;;
66                 (halt|poweroff)
67                     logging $"Stopping vserver '$VSERVER'"
68
69                     local sync_dir
70                     vshelper.initStopSync sync_dir
71
72                     spawn $_VSERVER --defaulttty "$VSERVER" stop &
73
74                     disown %%
75                     vshelper.waitForStopSync "$sync_dir"
76                     ;;
77                 (swsusp)
78                     ## TODO: any senseful action here? Perhaps shutdown scheduler for it?
79                     exit 0
80                     ;;
81                 (restart2)      ;;
82                 (*)
83                     warning $"Unknown action '$ACTION' for vserver '$VSERVER'"
84                     exit 1
85             esac
86             ;;
87
88         (async)
89             spawn killContext "$XID"
90             ;;
91             
92         (sync)
93             local f=${METHOD_ARGS[0]}
94             test -n "$f" ||
95                 panic $"Insufficent arguments for method '$method' and vserver '$VSERVER'"
96
97             test -p "$f" ||
98                 panic $"File '$f' which is required for synchronisation of vserver '$VSERVER' is not a pipe"
99
100             spawn killContext "$XID"
101             echo "$ACTION" >"$f"
102             ;;
103
104         (*)
105             local script
106             findObject -x script "$__CONFDIR"/.defaults/apps/vshelper-methods/"$method" "$__PKGLIBDIR"/vshelper-methods/"$method" ''
107             
108             test -n '$script' || {
109                 warning $"No handler for internal method '$method' found"
110                 exit 1
111             }
112
113             export VSERVER
114             execute "$script" "${ARGS[@]}"
115     esac
116 }
117     
118 function doDefaultMethod
119 {
120     local handler
121
122     vshelper.getHandler handler "$VSERVER" "$ACTION" || {
123         warning $"No handler configured for action '$ACTION' on vserver '$VSERVER'"
124         exit 1
125     }
126
127     case "$handler" in
128         (/*)    execute "$handler" "${ARGS[@]}";;
129         (:*)    doInternalMethod "${handler##:}" "$@";;
130     esac
131 }
132
133 #===========
134
135 test "$1" != '--version' || showVersion
136 test "$1" != '--help'    || showHelp
137
138 if ! tty -s; then
139     findObject -e _VS_LOGFILE "$__CONFDIR"/.defaults/apps/vshelper/logfile /dev/null
140     findObject -e _VS_ERRFILE "$__CONFDIR"/.defaults/apps/vshelper/logfile /dev/null
141
142     exec   </dev/null
143     exec  >>$_VS_LOGFILE
144     exec 2>>$_VS_ERRFILE
145 fi
146
147 logging "$(date): vshelper $*"
148     
149 test "$#" -ge 2 ||
150     panic $"vshelper called with missing arguments; try '--help' for more information"
151
152     
153 vshelper.isEnabled || exit 0
154 ! vshelper.isDebug || set -x
155     
156 set -eu
157
158 if $_VSERVER_INFO - FEATURE vshelper0; then
159     declare -r XID=$1
160     declare -r ACTION=$2
161 elif $_VSERVER_INFO - FEATURE vshelper; then
162     declare -r ACTION=$1
163     declare -r XID=$2
164 else
165     panic $"vshelper functionality not supported by kernel"
166 fi
167
168 findObject -x delegate "$__CONFDIR"/.defaults/apps/vshelper-delegate/"$ACTION" "$__PKGLIBDIR"/vshelper-delegate/"$ACTION" ''
169 test -e "$delegate" && exec -a "$ACTION" "$delegate" "$@"
170
171 this_xid=$($_VSERVER_INFO - XID)
172 pxid=
173 responsible_xid=$XID
174 while true; do
175     pxid=$($_VSERVER_INFO "$responsible_xid" PXID) || break
176     test "$pxid" -ne "$this_xid"                   || break
177     responsible_xid=$pxid
178 done
179
180 vserver_id=$($_VSERVER_INFO "$responsible_xid" ID) ||
181     panic $"No responsible vserver found for xid '$responsible_xid' ($XID); aborting..."
182     
183 test "$XID" = "$responsible_xid" || {
184     logging "Giving 'vshelper' task for '$XID' to parent vserver '$vserver_id' ($responsible_xid)"
185     execute $_VSERVER "$vserver_id" exec $_VSHELPER "$@"
186 }
187
188 #===========
189
190 ARGS=( "$@" )
191
192 declare -a state
193 getFileArray state "$__VSHELPERSTATEDIR/$XID" && test ${#state[@]} -ge 2 || {
194     logging "'vshelper' not configured for xid '$XID'"
195     exit 0
196 }
197
198 declare -r VSERVER=${state[0]}
199
200 cfg_xid=$($_VSERVER_INFO "${state[0]}" CONTEXT false) ||
201     panic $"Configured vserver '$VSERVER' does not seem to run; aborting..."
202
203 test "$cfg_xid" = "$XID" ||
204     panic $"Configured ($cfg_xid) and actual ($XID) xid for vserver '$VSERVER' are mismatching; aborting..."
205
206 cur_xid=$($_VSERVER_INFO "$VSERVER" CONTEXT false) ||
207     panic $"Vserver '$VSERVER' with '$XID' does not seem to run; strange..."
208
209 test "$cur_xid" = "$XID" ||
210     panic $"Expected ($XID) and actual ($cur_xid) xid for vserver '$VSERVER' are mismatching; strange..."
211
212
213 method=${state[1]}
214 if test "${#state[@]}" -gt 2; then
215     unset state[0] state[1]
216     declare -ra METHOD_ARGS=( "${state[@]}" )
217 else
218     declare -ra METHOD_ARGS=()
219 fi
220
221 case "$method" in
222     (default)           doDefaultMethod "$@";;
223     (sync|async)        doInternalMethod "$method" "$@";;
224     (*)                 panic $"Unknown method '$method' used by vserver '$VSERVER'";;
225 esac