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