Debian Codegraph @

debian/init-d-script

raw
1 #!/bin/sh
2 # See init-d-script(5) for instructions on how to use this library.
3 #=============================================================================
4
5 # Shift arguments early to fix #826214
6 __init_d_script_name="$1"
7 shift
8
9 # Define LSB log_* functions.
10 # Depend on sysvinit-utils (>= 3.05-1) to ensure that this file is present
11 # and status_of_proc is working.
12 . /lib/lsb/init-functions
13
14 # PATH should only include /usr/* if it runs after the mountnfs.sh
15 # script. Scripts running before mountnfs.sh should remove the /usr/*
16 # entries.
17 PATH=/usr/sbin:/usr/bin:/sbin:/bin
18 export PATH
19
20 is_call_implemented() {
21 PATH= command -V $1 >/dev/null 2>&1
22 }
23
24 do_usage() {
25 if is_call_implemented do_reload; then
26 echo "Usage: $SCRIPTNAME {start|stop|status|reload|restart|try-restart|force-reload}" >&2
27 else
28 echo "Usage: $SCRIPTNAME {start|stop|status|restart|try-restart|force-reload}" >&2
29 fi
30 }
31
32 call() {
33 cmd="$1"
34 shift
35 if is_call_implemented ${cmd}_override; then
36 ${cmd}_override "$@"
37 else
38 ${cmd} "$@"
39 fi
40 }
41
42 #
43 # Support for oneshot services
44 #
45 oneshot() {
46 [ "$TYPE" = oneshot ] || return
47 case $1 in
48 start-pre) [ -f /run/$NAME.oneshot ] && exit ;;
49 start-post) touch /run/$NAME.oneshot ;;
50 stop-pre) [ ! -f /run/$NAME.oneshot ] && exit ;;
51 stop-post) rm -f /run/$NAME.oneshot ;;
52 status)
53 if [ -f /run/$NAME.oneshot ]; then
54 log_success_msg "$NAME (oneshot) has been run"
55 else
56 log_failure_msg "$NAME (oneshot) has not been run"
57 fi
58 ;;
59 *) echo "ERROR: invalid oneshot request: $1" >&2 ;;
60 esac
61 }
62
63 #
64 # Function that starts the daemon/service
65 #
66
67 do_start_cmd() {
68 ${SETPRIV_ARGS:+setpriv $SETPRIV_ARGS} start-stop-daemon --start --quiet \
69 ${PIDFILE:+--pidfile "$PIDFILE"} \
70 ${COMMAND_NAME:+--name "$COMMAND_NAME"} \
71 ${DAEMON:+--exec "$DAEMON"} $START_ARGS -- $DAEMON_ARGS
72 }
73
74 do_start() {
75 oneshot start-pre
76 if is_call_implemented do_start_prepare; then
77 call do_start_prepare
78 fi
79 log_daemon_msg "Starting $DESC" "$NAME"
80 call do_start_cmd
81 retval=$?
82 if [ "$retval" = 0 ]; then
83 oneshot start-post
84 fi
85 if [ "$retval" = 1 ] && [ no != "$VERBOSE" ]; then
86 log_progress_msg "is already running"
87 fi
88 retval=$((retval > 1))
89 vlog_end_msg $retval
90 if is_call_implemented do_start_cleanup; then
91 call do_start_cleanup
92 fi
93 return $retval
94 }
95
96 #
97 # Function that stops the daemon/service
98 #
99
100 do_stop_cmd() {
101 start-stop-daemon --stop --quiet \
102 --retry=CONT/0/TERM/30/KILL/5 \
103 ${PIDFILE:+--pidfile "$PIDFILE"} \
104 ${COMMAND_NAME:+--name "$COMMAND_NAME"} \
105 ${DAEMON:+--exec "$DAEMON"} $STOP_ARGS
106 RETVAL="$?"
107 [ "$RETVAL" = 2 ] && return 2
108 # Wait for children to finish too if this is a daemon that forks
109 # and if the daemon is only ever run from this initscript.
110 # If the above conditions are not satisfied then add some other code
111 # that waits for the process to drop all resources that could be
112 # needed by services started subsequently. A last resort is to
113 # sleep for some time.
114 if [ -n "$DAEMON" ]; then
115 start-stop-daemon --stop --quiet --oknodo \
116 --retry=0/30/KILL/5 --exec "$DAEMON" $STOP_ARGS
117 [ "$?" = 2 ] && return 2
118 fi
119 # Many daemons don't delete their pidfiles when they exit.
120 rm -f $PIDFILE
121 return $RETVAL
122 }
123
124 do_stop() {
125 oneshot stop-pre
126 if is_call_implemented do_stop_prepare; then
127 call do_stop_prepare
128 fi
129 vlog_daemon_msg "Stopping $DESC" "$NAME"
130 call do_stop_cmd
131 retval=$?
132 # Oneshot stop is unconditional
133 oneshot stop-post
134 if [ "$retval" = 1 ] && [ no != "$VERBOSE" ] &&
135 [ "$TYPE" != oneshot ]; then
136 log_progress_msg "is not running"
137 fi
138 retval=$((retval > 1))
139 vlog_end_msg $retval
140 if is_call_implemented do_stop_cleanup; then
141 call do_stop_cleanup
142 fi
143 return $retval
144 }
145
146 do_restart() {
147 if is_call_implemented do_restart_prepare; then
148 call do_restart_prepare
149 fi
150 vlog_daemon_msg "Restarting $DESC" "$NAME"
151 call do_stop_cmd
152 retval=$(($? > 1))
153 if [ $retval -eq 0 ]; then
154 call do_start_cmd
155 retval=$(($? > 1))
156 fi
157 vlog_end_msg $retval
158 if is_call_implemented do_restart_cleanup; then
159 call do_restart_cleanup
160 fi
161 return $retval
162 }
163
164 # Wrapper for do_reload_cmd
165 do_reload_wrapper() {
166 if is_call_implemented do_reload_prepare; then
167 call do_reload_prepare
168 fi
169 vlog_daemon_msg "Reloading $DESC configuration files" "$NAME"
170 call do_reload_cmd
171 retval=$(($? != 0))
172 vlog_end_msg $retval
173 if is_call_implemented do_reload_cleanup; then
174 call do_reload_cleanup
175 fi
176 return $retval
177 }
178
179 # To enable this function, RELOAD_SIGNAL should be set to the kill signal
180 do_reload_signal() {
181 start-stop-daemon --stop --signal $RELOAD_SIGNAL --quiet \
182 ${PIDFILE:+--pidfile "$PIDFILE"} \
183 ${COMMAND_NAME:+--name "$COMMAND_NAME"} \
184 ${DAEMON:+--exec "$DAEMON"} $RELOAD_ARGS
185 }
186
187 # Deprecated: use RELOAD_SIGNAL instead for newer scripts
188 # Enable this using
189 # alias do_reload=do_reload_sigusr1
190 do_reload_sigusr1() {
191 RELOAD_SIGNAL=1
192 do_reload_cmd() { do_reload_signal; }
193 do_reload_wrapper
194 }
195
196 do_status() {
197 if [ "$TYPE" = oneshot ]; then
198 oneshot status
199 else
200 status_of_proc ${PIDFILE:+-p "$PIDFILE"} "${DAEMON:-none}" "$NAME"
201 fi
202 }
203
204 if [ "$DEBUG" = "true" ]; then
205 set -x
206 fi
207
208 # Unset configuration variables to make sure that if variable is not assigned a
209 # value in init script, it does not use one from environment. See #822918.
210 unset DAEMON DAEMON_ARGS DESC NAME COMMAND_NAME PIDFILE \
211 RELOAD_ARGS RELOAD_SIGNAL START_ARGS STOP_ARGS SETPRIV_ARGS TYPE
212
213 SCRIPTNAME="$__init_d_script_name"
214 scriptbasename=${__init_d_script_name##*/}
215 if [ "$scriptbasename" != "init-d-script" ]; then
216 . "$__init_d_script_name"
217 else
218 exit 0
219 fi
220
221 : ${NAME:=${DAEMON##*/}}
222 : ${DESC:=$NAME}
223 : ${COMMAND_NAME:=${NAME}}
224
225 # Do not use pid file if $PIDFILE is 'none'. Otherwise, generate from
226 # $NAME or use the value provided by the init.d script.
227 if [ none = "$PIDFILE" ]; then
228 PIDFILE=
229 elif [ -z "$PIDFILE" ]; then
230 PIDFILE=/var/run/$NAME.pid
231 fi
232
233 # Read configuration variable file if it is present
234 [ -r "/etc/default/${NAME}" ] && . "/etc/default/${NAME}"
235
236 # Exit if the package is not installed
237 if [ none != "$DAEMON" ] && [ ! -x "$DAEMON" ]; then
238 exit 0
239 fi
240
241 # Do not use DAEMON or COMMAND_NAME if they are set to 'none'.
242 [ none = "$DAEMON" ] && DAEMON=
243 [ none = "$COMMAND_NAME" ] && COMMAND_NAME=
244
245 # Load the VERBOSE setting and other rcS variables
246 . /lib/init/vars.sh
247 if [ -t 0 ]; then # Be verbose when called from a terminal
248 VERBOSE=yes
249 fi
250
251 # Setup do_reload if not already defined
252 if ! is_call_implemented do_reload; then
253 if ! is_call_implemented do_reload_cmd && [ -n "$RELOAD_SIGNAL" ]; then
254 do_reload_cmd() { do_reload_signal; }
255 fi
256 if is_call_implemented do_reload_cmd; then
257 alias do_reload=do_reload_wrapper
258 fi
259 fi
260
261 # Check if setpriv is available
262 if [ "$SETPRIV_ARGS" ]; then
263 if ! PATH=/bin:/usr/bin command -v setpriv >/dev/null 2>&1; then
264 echo "WARNING: setpriv not available, ignoring SETPRIV_ARGS" >&2
265 unset SETPRIV_ARGS
266 fi
267 fi
268
269 case "$1" in
270 start)
271 call do_start
272 ;;
273 stop)
274 call do_stop
275 ;;
276 status)
277 call do_status
278 ;;
279 reload)
280 if is_call_implemented do_reload; then
281 do_reload
282 else
283 call do_usage
284 exit 3
285 fi
286 ;;
287 force-reload)
288 if is_call_implemented do_force_reload; then
289 call do_force_reload
290 elif is_call_implemented do_reload; then
291 do_reload
292 else
293 call do_restart
294 fi
295 ;;
296 restart)
297 call do_restart
298 ;;
299 try-restart)
300 vlog_daemon_msg "Trying to restart $DESC" "$NAME"
301 if (call do_status) >/dev/null; then
302 (call do_restart) >/dev/null
303 else
304 [ no != "$VERBOSE" ] && log_progress_msg "is not running"
305 true
306 fi
307 vlog_end_msg $?
308 ;;
309 '')
310 call do_usage
311 exit 3
312 ;;
313 *)
314 if is_call_implemented do_unknown; then
315 call do_unknown "$1"
316 exit 3
317 else
318 call do_usage
319 exit 3
320 fi
321 ;;
322 esac
323 exit $? # See https://bugs.debian.org/822753#53