1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # VirtualBox generic init script.
|
---|
4 | #
|
---|
5 | # Copyright (C) 2012 Oracle Corporation
|
---|
6 | #
|
---|
7 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
8 | # available from http://www.virtualbox.org. This file is free software;
|
---|
9 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
10 | # General Public License (GPL) as published by the Free Software
|
---|
11 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
12 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
13 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
14 | #
|
---|
15 |
|
---|
16 | ### BEGIN INIT INFO
|
---|
17 | # Required-Start: $local_fs
|
---|
18 | # Should-Start: $syslog
|
---|
19 | # Required-Stop: $local_fs
|
---|
20 | # Should-Stop: $syslog
|
---|
21 | # Default-Start: 2 3 4 5
|
---|
22 | # Default-Stop: 0 1 6
|
---|
23 | # Short-Description: %DESCRIPTION%
|
---|
24 | ### END INIT INFO
|
---|
25 |
|
---|
26 | ## @todo We should really replace the daemon starting, stopping and checking
|
---|
27 | # code with a tool of our own written in C, which we could always use
|
---|
28 | # instead of the LSB functions.
|
---|
29 |
|
---|
30 | cr="
|
---|
31 | "
|
---|
32 | tab=" "
|
---|
33 | IFS=" ${cr}${tab}"
|
---|
34 | 'unset' -f unalias
|
---|
35 | 'unalias' -a
|
---|
36 | unset -f command
|
---|
37 | PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH
|
---|
38 |
|
---|
39 | ## A generic service script which can be used, after substituting some place-
|
---|
40 | # holders with service-specific values, to run most services on LSB, System V
|
---|
41 | # or BSD-compatible service management systems. As we control both the service
|
---|
42 | # code and the init script we try to push as much as possible of the logic into
|
---|
43 | # the service and out of the very system-dependent service configuration
|
---|
44 | # scripts and files. See the help text of the "install_service.sh" helper
|
---|
45 | # script for more details.
|
---|
46 | #
|
---|
47 | # Furthermore, to simplify deployment, we will install all init scripts using
|
---|
48 | # this generic template manually during the post install phase or at run time
|
---|
49 | # using LSB functions if they are available (as they should be on most common
|
---|
50 | # modern distributions) or manually placing the file in the appropriate
|
---|
51 | # directory and creating symbolic links on System V or writing to rc.local on
|
---|
52 | # BSD-compatible systems. Systems requiring different treatment will be added
|
---|
53 | # here when we add support for them, but we will try to keep everything as
|
---|
54 | # generic as we can.
|
---|
55 | #
|
---|
56 | # In general, we try to behave as natively as we reasonably can on the most
|
---|
57 | # important target systems we support and to work well enough on as many others
|
---|
58 | # as possible, but in particular without trying to look perfectly native.
|
---|
59 | #
|
---|
60 | # To use this template as an init script, replace the following text sequences
|
---|
61 | # (wrapped in percent characters) with the values you need:
|
---|
62 | # COMMAND: Path to the service binary or script, with all required escaping for
|
---|
63 | # characters which are special in shell scripts.
|
---|
64 | # ARGUMENTS: The arguments to pass to the binary when starting the service,
|
---|
65 | # with all required escaping for characters which are special in shell scripts.
|
---|
66 | # SERVICE_NAME: The name of the service, using ASCII characters 33 to 126 only.
|
---|
67 | # DESCRIPTION: Short description of the service, suitable for use in texts like
|
---|
68 | # "DESCRIPTION successfully started", using Utf-8 characters 32 to 126 and 128
|
---|
69 | # and upwards.
|
---|
70 |
|
---|
71 | ## Time out in seconds when shutting down the service.
|
---|
72 | SHUT_DOWN_TIME_OUT=5
|
---|
73 | ## If this is set to an empty value then the LSB init functions will not be
|
---|
74 | # used. This is intended for testing the fallback commands.
|
---|
75 | LSB_FUNCTIONS="/lib/lsb/init-functions"
|
---|
76 |
|
---|
77 | # Silently exit if the package was uninstalled but not purged.
|
---|
78 | test -r %COMMAND% || exit 0
|
---|
79 |
|
---|
80 | ## The function definition at the start of every non-trivial shell script!
|
---|
81 | abort()
|
---|
82 | {
|
---|
83 | log_failure_msg "$*"
|
---|
84 | exit 1
|
---|
85 | }
|
---|
86 |
|
---|
87 | ## Exit successfully.
|
---|
88 | do_success()
|
---|
89 | {
|
---|
90 | log_success_msg "%DESCRIPTION% successfully started."
|
---|
91 | exit 0
|
---|
92 | }
|
---|
93 |
|
---|
94 | ## Set the error message.
|
---|
95 | set_error()
|
---|
96 | {
|
---|
97 | test -z "${error}" && error="${1}"
|
---|
98 | }
|
---|
99 |
|
---|
100 | # Gentoo/OpenRC perculiarity.
|
---|
101 | if test "x${0}" = "x/sbin/rc" || test "x${0}" = "xrc"; then
|
---|
102 | shift
|
---|
103 | fi
|
---|
104 |
|
---|
105 | # Process arguments.
|
---|
106 | action=""
|
---|
107 | error=""
|
---|
108 | prefix="/var"
|
---|
109 | while test x"${#}" != "x0"; do
|
---|
110 | case "${1}" in
|
---|
111 | --lsb-functions)
|
---|
112 | test x"${#}" = "x1" &&
|
---|
113 | set_error "${1}: missing argument."
|
---|
114 | LSB_FUNCTIONS="${2}"
|
---|
115 | shift 2;;
|
---|
116 | --prefix)
|
---|
117 | test x"${#}" = "x1" &&
|
---|
118 | set_error "${1}: missing argument."
|
---|
119 | prefix="${2}"
|
---|
120 | shift 2;;
|
---|
121 | --help)
|
---|
122 | cat << EOF
|
---|
123 | Usage:
|
---|
124 |
|
---|
125 | ${0} {start|stop|restart|status} [<options>]
|
---|
126 |
|
---|
127 | start|stop|restart|status
|
---|
128 | Start/stop/restart/report status for the service.
|
---|
129 |
|
---|
130 | Options:
|
---|
131 |
|
---|
132 | --lsb-functions <script>
|
---|
133 | Take the standard LSB init functions from <script> instead of from the
|
---|
134 | normal location, or use our own versions if <script> is an empty string.
|
---|
135 |
|
---|
136 | --prefix <folder>
|
---|
137 | Use the folder <folder> for storing variable data instead of "/var". The
|
---|
138 | child folder "run" must exist.
|
---|
139 | EOF
|
---|
140 | exit 0;;
|
---|
141 | start|stop|restart|force-reload|condrestart|try-restart|reload|status)
|
---|
142 | test -z "${action}" ||
|
---|
143 | set_error "More than one action requested."
|
---|
144 | action="${1}"
|
---|
145 | shift;;
|
---|
146 | *)
|
---|
147 | set_error "Unknown option \"${1}\". Try \"${0} --help\" for more information."
|
---|
148 | shift;;
|
---|
149 | esac
|
---|
150 | done
|
---|
151 |
|
---|
152 | ## Set Redhat and Fedora lock directory
|
---|
153 | LOCK_FOLDER="${prefix}/lock/subsys/"
|
---|
154 | LOCK_FILE="${LOCK_FOLDER}/%SERVICE_NAME%"
|
---|
155 |
|
---|
156 | # Use LSB functions if available. Success and failure messages default to just
|
---|
157 | # "echo" if the LSB functions are not available, so call these functions with
|
---|
158 | # messages which clearly read as success or failure messages.
|
---|
159 | test -n "${LSB_FUNCTIONS}" && test -f "${LSB_FUNCTIONS}" &&
|
---|
160 | . "${LSB_FUNCTIONS}"
|
---|
161 |
|
---|
162 | type log_success_msg >/dev/null 2>&1 ||
|
---|
163 | log_success_msg()
|
---|
164 | {
|
---|
165 | cat << EOF
|
---|
166 | ${*}
|
---|
167 | EOF
|
---|
168 | }
|
---|
169 |
|
---|
170 | type log_failure_msg >/dev/null 2>&1 ||
|
---|
171 | log_failure_msg()
|
---|
172 | {
|
---|
173 | cat << EOF
|
---|
174 | ${*}
|
---|
175 | EOF
|
---|
176 | }
|
---|
177 |
|
---|
178 | ## Get the LSB standard PID-file name for a binary.
|
---|
179 | pidfilename()
|
---|
180 | {
|
---|
181 | echo "${prefix}/run/${1##*/}.pid"
|
---|
182 | }
|
---|
183 |
|
---|
184 | ## Get the PID-file for a process like the LSB functions do ( "-p" or by name).
|
---|
185 | pidfileofproc()
|
---|
186 | {
|
---|
187 | if test x"${1}" = "x-p"; then
|
---|
188 | echo "${2}"
|
---|
189 | else
|
---|
190 | pidfilename "${1}"
|
---|
191 | fi
|
---|
192 | }
|
---|
193 |
|
---|
194 | ## Read the pids from an LSB PID-file, checking that they are positive numbers.
|
---|
195 | pidsfromfile()
|
---|
196 | {
|
---|
197 | pids=""
|
---|
198 | test -r "${1}" &&
|
---|
199 | read -r pids < "${1}" 2>/dev/null
|
---|
200 | for i in $pids; do
|
---|
201 | test 1 -le "${i}" || return 1
|
---|
202 | done
|
---|
203 | echo "${pids}"
|
---|
204 | }
|
---|
205 |
|
---|
206 | ## Check whether the binary $1 with the pids $2... is running.
|
---|
207 | procrunning()
|
---|
208 | {
|
---|
209 | binary="${1}"
|
---|
210 | shift
|
---|
211 | ps -p "${@}" -f 2>/dev/null | grep "${binary}" >/dev/null
|
---|
212 | }
|
---|
213 |
|
---|
214 | type pidofproc >/dev/null 2>&1 ||
|
---|
215 | pidofproc()
|
---|
216 | {
|
---|
217 | pidfile="`pidfileofproc "${@}"`"
|
---|
218 | test "x${1}" = "x-p" && shift 2
|
---|
219 | pids="`pidsfromfile "${pidfile}"`"
|
---|
220 | procrunning "${1}" ${pids} && echo "${pids}"
|
---|
221 | }
|
---|
222 |
|
---|
223 | type killproc >/dev/null 2>&1 ||
|
---|
224 | killproc()
|
---|
225 | {
|
---|
226 | pidfile="`pidfileofproc "${@}"`"
|
---|
227 | test "x${1}" = "x-p" && shift 2
|
---|
228 | pids="`pidsfromfile "${pidfile}"`"
|
---|
229 | if test -n "${2}"; then
|
---|
230 | procrunning "${1}" ${pids} || return 1
|
---|
231 | kill "${2}" ${pids}
|
---|
232 | return 0
|
---|
233 | else
|
---|
234 | rm -f "${pidfile}"
|
---|
235 | procrunning "${1}" ${pids} || return 0
|
---|
236 | kill "${pids}"
|
---|
237 | # Short busy wait for the process to terminate.
|
---|
238 | stamp="`times`"
|
---|
239 | while test x"${stamp}" = x"`times`"; do
|
---|
240 | procrunning "${1}" ${pids} || return 0
|
---|
241 | done
|
---|
242 | # Slow sleeping wait if it is still running.
|
---|
243 | for high in "" 1 2 3 4 5 6 7 8 9; do
|
---|
244 | for time in ${high}0 ${high}1 ${high}2 ${high}3 ${high}4 ${high}5 ${high}6 ${high}7 ${high}8 ${high}9; do
|
---|
245 | sleep 1
|
---|
246 | procrunning "${1}" ${pids} || return 0
|
---|
247 | if test "${time}" = "${SHUT_DOWN_TIME_OUT}"; then
|
---|
248 | kill -9 "${pid}"
|
---|
249 | return 0
|
---|
250 | fi
|
---|
251 | done
|
---|
252 | done
|
---|
253 | return 0
|
---|
254 | fi
|
---|
255 | }
|
---|
256 |
|
---|
257 | start()
|
---|
258 | {
|
---|
259 | test -d "${LOCK_FOLDER}" && touch "${LOCK_FILE}"
|
---|
260 | test -n "`pidofproc %COMMAND%`" && exit 0
|
---|
261 | %COMMAND% %ARGUMENTS% >/dev/null 2>&1 &
|
---|
262 | pid="$!"
|
---|
263 | pidfile="`pidfilename %COMMAND%`"
|
---|
264 | echo "${pid}" > "${pidfile}"
|
---|
265 | do_success
|
---|
266 | }
|
---|
267 |
|
---|
268 | stop()
|
---|
269 | {
|
---|
270 | killproc %COMMAND% || abort "%DESCRIPTION% failed to stop!"
|
---|
271 | rm -f "${LOCK_FILE}"
|
---|
272 | log_success_msg "%DESCRIPTION% successfully stopped."
|
---|
273 | return 0
|
---|
274 | }
|
---|
275 |
|
---|
276 | status()
|
---|
277 | {
|
---|
278 | pid="`pidofproc %COMMAND%`"
|
---|
279 | test -n "${pid}" &&
|
---|
280 | {
|
---|
281 | echo "%SERVICE_NAME% running, process ${pid}"
|
---|
282 | exit 0
|
---|
283 | }
|
---|
284 | test -f "`pidfilename %COMMAND%`" &&
|
---|
285 | {
|
---|
286 | echo "%SERVICE_NAME% not running but PID-file present."
|
---|
287 | exit 1
|
---|
288 | }
|
---|
289 | test -f "${LOCK_FILE}" &&
|
---|
290 | {
|
---|
291 | echo "%SERVICE_NAME% not running but lock file present."
|
---|
292 | exit 2
|
---|
293 | }
|
---|
294 | echo "%SERVICE_NAME% not running."
|
---|
295 | exit 3
|
---|
296 | }
|
---|
297 |
|
---|
298 | test -z "${error}" || abort "${error}"
|
---|
299 |
|
---|
300 | case "${action}" in
|
---|
301 | start)
|
---|
302 | start;;
|
---|
303 | stop)
|
---|
304 | stop;;
|
---|
305 | restart|force-reload)
|
---|
306 | start
|
---|
307 | stop;;
|
---|
308 | condrestart|try-restart)
|
---|
309 | status || exit 0
|
---|
310 | stop
|
---|
311 | start;;
|
---|
312 | reload)
|
---|
313 | ;;
|
---|
314 | status)
|
---|
315 | status;;
|
---|
316 | esac
|
---|