VirtualBox

source: vbox/trunk/src/VBox/Installer/linux/install_service/init_template.sh@ 44081

Last change on this file since 44081 was 44081, checked in by vboxsync, 12 years ago

Installer/linux: src/VBox/Installer/linux/scripts/VBoxHeadlessXOrg.sh and install_service.sh fixes.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
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
30cr="
31"
32tab=" "
33IFS=" ${cr}${tab}"
34'unset' -f unalias
35'unalias' -a
36unset -f command
37PATH=/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.
72SHUT_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.
75LSB_FUNCTIONS="/lib/lsb/init-functions"
76
77# Silently exit if the package was uninstalled but not purged.
78test -r %COMMAND% || exit 0
79
80## The function definition at the start of every non-trivial shell script!
81abort()
82{
83 log_failure_msg "$*"
84 exit 1
85}
86
87## Exit successfully.
88do_success()
89{
90 log_success_msg "%DESCRIPTION% successfully started."
91 exit 0
92}
93
94## Set the error message.
95set_error()
96{
97 test -z "${error}" && error="${1}"
98}
99
100# Gentoo/OpenRC perculiarity.
101if test "x${0}" = "x/sbin/rc" || test "x${0}" = "xrc"; then
102 shift
103fi
104
105# Process arguments.
106action=""
107error=""
108prefix="/var"
109while 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
123Usage:
124
125 ${0} {start|stop|restart|status} [<options>]
126
127 start|stop|restart|status
128 Start/stop/restart/report status for the service.
129
130Options:
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.
139EOF
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
150done
151
152## Set Redhat and Fedora lock directory
153LOCK_FOLDER="${prefix}/lock/subsys/"
154LOCK_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.
159test -n "${LSB_FUNCTIONS}" && test -f "${LSB_FUNCTIONS}" &&
160 . "${LSB_FUNCTIONS}"
161
162type log_success_msg >/dev/null 2>&1 ||
163 log_success_msg()
164 {
165 cat << EOF
166${*}
167EOF
168 }
169
170type log_failure_msg >/dev/null 2>&1 ||
171 log_failure_msg()
172 {
173 cat << EOF
174${*}
175EOF
176 }
177
178## Get the LSB standard PID-file name for a binary.
179pidfilename()
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).
185pidfileofproc()
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.
195pidsfromfile()
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.
207procrunning()
208{
209 binary="${1}"
210 shift
211 ps -p "${@}" -f 2>/dev/null | grep "${binary}" >/dev/null
212}
213
214# We prefer our own implementations of pidofproc and killproc over falling back
215# to distribution ones with unknown quirks.
216# type pidofproc >/dev/null 2>&1 ||
217 pidofproc()
218 {
219 pidfile="`pidfileofproc "${@}"`"
220 test "x${1}" = "x-p" && shift 2
221 pids="`pidsfromfile "${pidfile}"`"
222 procrunning "${1}" ${pids} && echo "${pids}"
223 }
224
225# type killproc >/dev/null 2>&1 ||
226 killproc()
227 {
228 pidfile="`pidfileofproc "${@}"`"
229 test "x${1}" = "x-p" && shift 2
230 pids="`pidsfromfile "${pidfile}"`"
231 if test -n "${2}"; then
232 procrunning "${1}" ${pids} || return 1
233 kill "${2}" ${pids}
234 return 0
235 else
236 rm -f "${pidfile}"
237 procrunning "${1}" ${pids} || return 0
238 kill "${pids}"
239 # Short busy wait for the process to terminate.
240 stamp="`times`"
241 while test x"${stamp}" = x"`times`"; do
242 procrunning "${1}" ${pids} || return 0
243 done
244 # Slow sleeping wait if it is still running.
245 for high in "" 1 2 3 4 5 6 7 8 9; do
246 for time in ${high}0 ${high}1 ${high}2 ${high}3 ${high}4 ${high}5 ${high}6 ${high}7 ${high}8 ${high}9; do
247 sleep 1
248 procrunning "${1}" ${pids} || return 0
249 if test "${time}" = "${SHUT_DOWN_TIME_OUT}"; then
250 kill -9 "${pid}"
251 return 0
252 fi
253 done
254 done
255 return 0
256 fi
257 }
258
259start()
260{
261 test -d "${LOCK_FOLDER}" && touch "${LOCK_FILE}"
262 test -n "`pidofproc %COMMAND%`" && exit 0
263 %COMMAND% %ARGUMENTS% >/dev/null 2>&1 &
264 pid="$!"
265 pidfile="`pidfilename %COMMAND%`"
266 echo "${pid}" > "${pidfile}"
267 do_success
268}
269
270stop()
271{
272 killproc %COMMAND% || abort "%DESCRIPTION% failed to stop!"
273 rm -f "${LOCK_FILE}"
274 log_success_msg "%DESCRIPTION% successfully stopped."
275 return 0
276}
277
278status()
279{
280 pid="`pidofproc %COMMAND%`"
281 test -n "${pid}" &&
282 {
283 echo "%SERVICE_NAME% running, process ${pid}"
284 exit 0
285 }
286 test -f "`pidfilename %COMMAND%`" &&
287 {
288 echo "%SERVICE_NAME% not running but PID-file present."
289 exit 1
290 }
291 test -f "${LOCK_FILE}" &&
292 {
293 echo "%SERVICE_NAME% not running but lock file present."
294 exit 2
295 }
296 echo "%SERVICE_NAME% not running."
297 exit 3
298}
299
300test -z "${error}" || abort "${error}"
301
302case "${action}" in
303start)
304 start;;
305stop)
306 stop;;
307restart|force-reload)
308 start
309 stop;;
310condrestart|try-restart)
311 status || exit 0
312 stop
313 start;;
314reload)
315 ;;
316status)
317 status;;
318esac
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette