Debian Codegraph @

script/invoke-rc.d

raw
1 #!/bin/sh
2 # vim: ft=sh
3 #
4 # invoke-rc.d.sysvinit - Executes initscript actions
5 #
6 # SysVinit /etc/rc?.d version for Debian's sysvinit package
7 #
8 # Copyright (C) 2000,2001 Henrique de Moraes Holschuh <[email protected]>
9 #
10 # This program is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by the Free
12 # Software Foundation; either version 2 of the License, or (at your option)
13 # any later version.
14 #
15 # This program is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 # for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
23
24 # Constants
25 RUNLEVELHELPER=/sbin/runlevel
26 POLICYHELPER=$DPKG_ROOT/usr/sbin/policy-rc.d
27 INITDPREFIX=/etc/init.d/
28 RCDPREFIX=/etc/rc
29
30 # Options
31 BEQUIET=
32 MODE=
33 ACTION=
34 FALLBACK=
35 NOFALLBACK=
36 FORCE=
37 RETRY=
38 RETURNFAILURE=
39 RC=
40 is_systemd=
41 is_openrc=
42 SKIP_SYSTEMD_NATIVE=
43
44 # Shell options
45 set +e
46
47 dohelp () {
48 #
49 # outputs help and usage
50 #
51 cat <<EOF
52
53 invoke-rc.d, Debian/SysVinit (/etc/rc?.d) initscript subsystem.
54 Copyright (c) 2000,2001 Henrique de Moraes Holschuh <[email protected]>
55
56 Usage:
57 invoke-rc.d [options] <basename> <action> [extra parameters]
58
59 basename - Initscript ID, as per update-rc.d(8)
60 action - Initscript action. Known actions are:
61 start, [force-]stop, [try-]restart,
62 [force-]reload, status
63 WARNING: not all initscripts implement all of the above actions.
64
65 extra parameters are passed as is to the initscript, following
66 the action (first initscript parameter).
67
68 Options:
69 --quiet
70 Quiet mode, no error messages are generated.
71 --force
72 Try to run the initscript regardless of policy and subsystem
73 non-fatal errors.
74 --try-anyway
75 Try to run init script even if a non-fatal error is found.
76 --disclose-deny
77 Return status code 101 instead of status code 0 if
78 initscript action is denied by local policy rules or
79 runlevel constrains.
80 --query
81 Returns one of status codes 100-106, does not run
82 the initscript. Implies --disclose-deny and --no-fallback.
83 --no-fallback
84 Ignores any fallback action requests by the policy layer.
85 Warning: this is usually a very *bad* idea for any actions
86 other than "start".
87 --skip-systemd-native
88 Exits before doing anything if a systemd environment is detected
89 and the requested service is a native systemd unit.
90 This is useful for maintainer scripts that want to defer systemd
91 actions to deb-systemd-invoke
92 --help
93 Outputs help message to stdout
94
95 EOF
96 }
97
98 printerror () {
99 #
100 # prints an error message
101 # $* - error message
102 #
103 if test x${BEQUIET} = x ; then
104 echo `basename $0`: "$*" >&2
105 fi
106 }
107
108 formataction () {
109 #
110 # formats a list in $* into $printaction
111 # for human-friendly printing to stderr
112 # and sets $naction to action or actions
113 #
114 printaction=`echo $* | sed 's/ /, /g'`
115 if test $# -eq 1 ; then
116 naction=action
117 else
118 naction=actions
119 fi
120 }
121
122 querypolicy () {
123 #
124 # queries policy database
125 # returns: $RC = 104 - ok, run
126 # $RC = 101 - ok, do not run
127 # other - exit with status $RC, maybe run if $RETRY
128 # initial status of $RC is taken into account.
129 #
130
131 policyaction="${ACTION}"
132 if test x${RC} = "x101" ; then
133 if test "${ACTION}" = "start" || test "${ACTION}" = "restart" || test "${ACTION}" = "try-restart"; then
134 policyaction="(${ACTION})"
135 fi
136 fi
137
138 if test "x${POLICYHELPER}" != x && test -x "${POLICYHELPER}" ; then
139 FALLBACK=`${POLICYHELPER} ${BEQUIET} ${INITSCRIPTID} "${policyaction}" ${RL}`
140 RC=$?
141 formataction ${ACTION}
142 case ${RC} in
143 0) RC=104
144 ;;
145 1) RC=105
146 ;;
147 101) if test x${FORCE} != x ; then
148 printerror Overriding policy-rc.d denied execution of ${printaction}.
149 RC=104
150 else
151 printerror policy-rc.d denied execution of ${printaction}.
152 fi
153 ;;
154 esac
155 if test x${MODE} != xquery ; then
156 case ${RC} in
157 105) printerror policy-rc.d query returned \"behaviour undefined\",
158 printerror assuming \"${printaction}\" is allowed.
159 RC=104
160 ;;
161 106) formataction ${FALLBACK}
162 if test x${FORCE} = x ; then
163 if test x${NOFALLBACK} = x ; then
164 ACTION="${FALLBACK}"
165 printerror executing ${naction} \"${printaction}\" instead due to policy-rc.d request.
166 RC=104
167 else
168 printerror ignoring policy-rc.d fallback request: ${printaction}.
169 RC=101
170 fi
171 else
172 printerror ignoring policy-rc.d fallback request: ${printaction}.
173 RC=104
174 fi
175 ;;
176 esac
177 fi
178 case ${RC} in
179 100|101|102|103|104|105|106) ;;
180 *) printerror WARNING: policy-rc.d returned unexpected error status ${RC}, 102 used instead.
181 RC=102
182 ;;
183 esac
184 else
185 if test ! -e "/sbin/init" ; then
186 if test x${FORCE} != x ; then
187 printerror "WARNING: No init system and policy-rc.d missing, but force specified so proceeding."
188 else
189 printerror "WARNING: No init system and policy-rc.d missing! Defaulting to block."
190 RC=101
191 fi
192 fi
193
194 if test x${RC} = x ; then
195 RC=104
196 fi
197 fi
198 return
199 }
200
201 verifyparameter () {
202 #
203 # Verifies if $1 is not null, and $# = 1
204 #
205 if test $# -eq 0 ; then
206 printerror syntax error: invalid empty parameter
207 exit 103
208 elif test $# -ne 1 ; then
209 printerror syntax error: embedded blanks are not allowed in \"$*\"
210 exit 103
211 fi
212 return
213 }
214
215 ##
216 ## main
217 ##
218
219 ## Verifies command line arguments
220
221 if test $# -eq 0 ; then
222 printerror syntax error: missing required parameter, --help assumed
223 dohelp
224 exit 103
225 fi
226
227 state=I
228 while test $# -gt 0 && test ${state} != III ; do
229 case "$1" in
230 --help) dohelp
231 exit 0
232 ;;
233 --quiet) BEQUIET=--quiet
234 ;;
235 --force) FORCE=yes
236 RETRY=yes
237 ;;
238 --try-anyway)
239 RETRY=yes
240 ;;
241 --disclose-deny)
242 RETURNFAILURE=yes
243 ;;
244 --query) MODE=query
245 RETURNFAILURE=yes
246 ;;
247 --no-fallback)
248 NOFALLBACK=yes
249 ;;
250 --skip-systemd-native) SKIP_SYSTEMD_NATIVE=yes
251 ;;
252 --*) printerror syntax error: unknown option \"$1\"
253 exit 103
254 ;;
255 *) case ${state} in
256 I) verifyparameter $1
257 INITSCRIPTID=$1
258 ;;
259 II) verifyparameter $1
260 ACTION=$1
261 ;;
262 esac
263 state=${state}I
264 ;;
265 esac
266 shift
267 done
268
269 if test ${state} != III ; then
270 printerror syntax error: missing required parameter
271 exit 103
272 fi
273
274 #NOTE: It may not be obvious, but "$@" from this point on must expand
275 #to the extra initscript parameters, except inside functions.
276
277 if test -d /run/systemd/system ; then
278 is_systemd=1
279 UNIT="${INITSCRIPTID%.sh}.service"
280 elif test -f /run/openrc/softlevel ; then
281 is_openrc=1
282 elif test ! -f "${INITDPREFIX}${INITSCRIPTID}" ; then
283 ## Verifies if the given initscript ID is known
284 ## For sysvinit, this error is critical
285 printerror unknown initscript, ${INITDPREFIX}${INITSCRIPTID} not found.
286 fi
287
288 ## Queries sysvinit for the current runlevel
289 if [ ! -x ${RUNLEVELHELPER} ] || ! RL=`${RUNLEVELHELPER}`; then
290 if [ -n "$is_systemd" ] && systemctl is-active --quiet sysinit.target; then
291 # under systemd, the [2345] runlevels are only set upon reaching them;
292 # if we are past sysinit.target (roughly equivalent to rcS), consider
293 # this as runlevel 5 (this is only being used for validating rcN.d
294 # symlinks, so the precise value does not matter much)
295 RL=5
296 else
297 printerror "could not determine current runlevel"
298 # this usually fails in schroots etc., ignore failure (#823611)
299 RL=
300 fi
301 fi
302 # strip off previous runlevel
303 RL=${RL#* }
304
305 ## Running ${RUNLEVELHELPER} to get current runlevel do not work in
306 ## the boot runlevel (scripts in /etc/rcS.d/), as /var/run/utmp
307 ## contains runlevel 0 or 6 (written at shutdown) at that point.
308 if test x${RL} = x0 || test x${RL} = x6 ; then
309 if ps -fp 1 | grep -q 'init boot' ; then
310 RL=S
311 fi
312 fi
313
314 ## Handles shutdown sequences VERY safely
315 ## i.e.: forget about policy, and do all we can to run the script.
316 ## BTW, why the heck are we being run in a shutdown runlevel?!
317 if test x${RL} = x0 || test x${RL} = x6 ; then
318 FORCE=yes
319 RETRY=yes
320 POLICYHELPER=
321 BEQUIET=
322 printerror "-----------------------------------------------------"
323 printerror "WARNING: 'invoke-rc.d ${INITSCRIPTID} ${ACTION}' called"
324 printerror "during shutdown sequence."
325 printerror "enabling safe mode: initscript policy layer disabled"
326 printerror "-----------------------------------------------------"
327 fi
328
329 ## Verifies the existance of proper S??initscriptID and K??initscriptID
330 ## *links* in the proper /etc/rc?.d/ directory
331 verifyrclink () {
332 #
333 # verifies if parameters are non-dangling symlinks
334 # all parameters are verified
335 #
336 doexit=
337 while test $# -gt 0 ; do
338 if test ! -L "$1" ; then
339 printerror not a symlink: $1
340 doexit=102
341 fi
342 if test ! -f "$1" ; then
343 printerror dangling symlink: $1
344 doexit=102
345 fi
346 shift
347 done
348 if test x${doexit} != x && test x${RETRY} = x; then
349 exit ${doexit}
350 fi
351 return 0
352 }
353
354 testexec () {
355 #
356 # returns true if any of the parameters is
357 # executable (after following links)
358 #
359 while test $# -gt 0 ; do
360 if test -x "$1" ; then
361 return 0
362 fi
363 shift
364 done
365 return 1
366 }
367
368 RC=
369
370 ###
371 ### LOCAL POLICY: Enforce that the script/unit is enabled. For SysV init
372 ### scripts, this needs a start entry in either runlevel S or current runlevel
373 ### to allow start or restart.
374 if [ -n "$is_systemd" ]; then
375 case ${ACTION} in
376 start|restart|try-restart)
377 # If a package ships both init script and systemd service file, the
378 # systemd unit will not be enabled by the time invoke-rc.d is called
379 # (with current debhelper sequence). This would make systemctl is-enabled
380 # report the wrong status, and then the service would not be started.
381 # This check cannot be removed as long as we support not passing --skip-systemd-native
382
383 if systemctl --quiet is-enabled "${UNIT}" 2>/dev/null || \
384 ls ${RCDPREFIX}[S2345].d/S[0-9][0-9]${INITSCRIPTID} >/dev/null 2>&1; then
385 RC=104
386 elif systemctl --quiet is-active "${UNIT}" 2>/dev/null; then
387 RC=104
388 else
389 RC=101
390 fi
391 ;;
392 esac
393 else
394 # we do handle multiple links per runlevel
395 # but we don't handle embedded blanks in link names :-(
396 if test x${RL} != x ; then
397 SLINK=`ls -d -Q ${RCDPREFIX}${RL}.d/S[0-9][0-9]${INITSCRIPTID} 2>/dev/null | xargs`
398 KLINK=`ls -d -Q ${RCDPREFIX}${RL}.d/K[0-9][0-9]${INITSCRIPTID} 2>/dev/null | xargs`
399 SSLINK=`ls -d -Q ${RCDPREFIX}S.d/S[0-9][0-9]${INITSCRIPTID} 2>/dev/null | xargs`
400
401 verifyrclink ${SLINK} ${KLINK} ${SSLINK}
402 fi
403
404 case ${ACTION} in
405 start|restart|try-restart)
406 if testexec ${SLINK} ; then
407 RC=104
408 elif testexec ${KLINK} ; then
409 RC=101
410 elif testexec ${SSLINK} ; then
411 RC=104
412 else
413 RC=101
414 fi
415 ;;
416 esac
417 fi
418
419 # test if /etc/init.d/initscript is actually executable
420 _executable=
421 if [ -n "$is_systemd" ]; then
422 _executable=1
423 elif testexec "${INITDPREFIX}${INITSCRIPTID}"; then
424 _executable=1
425 fi
426 if [ "$_executable" = "1" ]; then
427 if test x${RC} = x && test x${MODE} = xquery ; then
428 RC=105
429 fi
430
431 # call policy layer
432 querypolicy
433 case ${RC} in
434 101|104)
435 ;;
436 *) if test x${MODE} != xquery ; then
437 printerror policy-rc.d returned error status ${RC}
438 if test x${RETRY} = x ; then
439 exit ${RC}
440 else
441 RC=102
442 fi
443 fi
444 ;;
445 esac
446 else
447 ###
448 ### LOCAL INITSCRIPT POLICY: non-executable initscript; deny exec.
449 ### (this is common sense, actually :^P )
450 ###
451 RC=101
452 fi
453
454 ## Handles --query
455 if test x${MODE} = xquery ; then
456 exit ${RC}
457 fi
458
459
460 setechoactions () {
461 if test $# -gt 1 ; then
462 echoaction=true
463 else
464 echoaction=
465 fi
466 }
467 getnextaction () {
468 saction=$1
469 shift
470 ACTION="$@"
471 }
472
473 ## Executes initscript
474 ## note that $ACTION is a space-separated list of actions
475 ## to be attempted in order until one suceeds.
476 if test x${FORCE} != x || test ${RC} -eq 104 ; then
477 if [ -n "$is_systemd" ] || testexec "${INITDPREFIX}${INITSCRIPTID}" ; then
478 RC=102
479 setechoactions ${ACTION}
480 while test ! -z "${ACTION}" ; do
481 getnextaction ${ACTION}
482 if test ! -z ${echoaction} ; then
483 printerror executing initscript action \"${saction}\"...
484 fi
485
486 if [ -n "$is_systemd" ]; then
487 if [ "$SKIP_SYSTEMD_NATIVE" = yes ] ; then
488 case $(systemctl show --value --property SourcePath "${UNIT}") in
489 /etc/init.d/*)
490 ;;
491 *)
492 # We were asked to skip native systemd units, and this one was not generated by the sysv generator
493 # exit cleanly
494 exit 0
495 ;;
496 esac
497
498 fi
499 _state=$(systemctl -p LoadState show "${UNIT}" 2>/dev/null)
500
501 case $saction in
502 start|restart|try-restart)
503 [ "$_state" != "LoadState=masked" ] || exit 0
504 systemctl $sctl_args "${saction}" "${UNIT}" && exit 0
505 ;;
506 stop|status)
507 systemctl $sctl_args "${saction}" "${UNIT}" && exit 0
508 ;;
509 reload)
510 [ "$_state" != "LoadState=masked" ] || exit 0
511 _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
512 # Don't block on reload requests during bootup and shutdown
513 # from units/hooks and simply schedule the task.
514 if ! systemctl --quiet is-system-running; then
515 sctl_args="--no-block"
516 fi
517 if [ "$_canreload" = "CanReload=no" ]; then
518 "${INITDPREFIX}${INITSCRIPTID}" "${saction}" "$@" && exit 0
519 else
520 systemctl $sctl_args reload "${UNIT}" && exit 0
521 fi
522 ;;
523 force-stop)
524 systemctl --signal=KILL kill "${UNIT}" && exit 0
525 ;;
526 force-reload)
527 [ "$_state" != "LoadState=masked" ] || exit 0
528 _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
529 if [ "$_canreload" = "CanReload=no" ]; then
530 systemctl $sctl_args restart "${UNIT}" && exit 0
531 else
532 systemctl $sctl_args reload "${UNIT}" && exit 0
533 fi
534 ;;
535 *)
536 # We try to run non-standard actions by running
537 # the init script directly.
538 "${INITDPREFIX}${INITSCRIPTID}" "${saction}" "$@" && exit 0
539 ;;
540 esac
541 elif [ -n "$is_openrc" ]; then
542 rc-service "${INITSCRIPTID}" "${saction}" && exit 0
543 else
544 "${INITDPREFIX}${INITSCRIPTID}" "${saction}" "$@" && exit 0
545 fi
546 RC=$?
547
548 if test ! -z "${ACTION}" ; then
549 printerror action \"${saction}\" failed, trying next action...
550 fi
551 done
552 printerror initscript ${INITSCRIPTID}, action \"${saction}\" failed.
553 if [ -n "$is_systemd" ] && [ "$saction" = start -o "$saction" = restart -o "$saction" = "try-restart" ]; then
554 systemctl status --full --no-pager "${UNIT}" || true
555 fi
556 exit ${RC}
557 fi
558 exit 102
559 fi
560
561 ## Handles --disclose-deny and denied "status" action (bug #381497)
562 if test ${RC} -eq 101 && test x${RETURNFAILURE} = x ; then
563 if test "x${ACTION%% *}" = "xstatus"; then
564 printerror emulating initscript action \"status\", returning \"unknown\"
565 RC=4
566 else
567 RC=0
568 fi
569 else
570 formataction ${ACTION}
571 printerror initscript ${naction} \"${printaction}\" not executed.
572 fi
573
574 exit ${RC}