Changeset View
Changeset View
Standalone View
Standalone View
libexec/rc/rc.subr
Show First 20 Lines • Show All 804 Lines • ▼ Show 20 Lines | |||||
# Requires /usr to be mounted. | # Requires /usr to be mounted. | ||||
# | # | ||||
# ${name}_groups n Comma separated list of supplementary groups | # ${name}_groups n Comma separated list of supplementary groups | ||||
# to run the chrooted ${command} with. | # to run the chrooted ${command} with. | ||||
# Requires /usr to be mounted. | # Requires /usr to be mounted. | ||||
# | # | ||||
# ${name}_prepend n Command added before ${command}. | # ${name}_prepend n Command added before ${command}. | ||||
# | # | ||||
# ${name}_setup n Command executed before ${command}. | # ${name}_setup n Command executed during start, restart and | ||||
# reload before ${rc_arg}_precmd is run. | |||||
# | # | ||||
# ${name}_login_class n Login class to use, else "daemon". | # ${name}_login_class n Login class to use, else "daemon". | ||||
# | # | ||||
# ${name}_limits n limits(1) to apply to ${command}. | # ${name}_limits n limits(1) to apply to ${command}. | ||||
# | # | ||||
# ${rc_arg}_cmd n If set, use this as the method when invoked; | # ${rc_arg}_cmd n If set, use this as the method when invoked; | ||||
# Otherwise, use default command (see below) | # Otherwise, use default command (see below) | ||||
# | # | ||||
▲ Show 20 Lines • Show All 242 Lines • ▼ Show 20 Lines | for _elem in $_keywords; do | ||||
if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then | if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then | ||||
if [ -z "$rc_quiet" ]; then | if [ -z "$rc_quiet" ]; then | ||||
echo 1>&2 "${name} already running? " \ | echo 1>&2 "${name} already running? " \ | ||||
"(pid=$rc_pid)." | "(pid=$rc_pid)." | ||||
fi | fi | ||||
return 1 | return 1 | ||||
fi | fi | ||||
# if there's a custom ${XXX_cmd}, | # if there's a custom ${XXX_cmd}, | ||||
# run that instead of the default | # run that instead of the default | ||||
# | # | ||||
eval _cmd=\$${rc_arg}_cmd \ | eval _cmd=\$${rc_arg}_cmd \ | ||||
_precmd=\$${rc_arg}_precmd \ | _precmd=\$${rc_arg}_precmd \ | ||||
_postcmd=\$${rc_arg}_postcmd | _postcmd=\$${rc_arg}_postcmd | ||||
if [ -n "$_cmd" ]; then | if [ -n "$_cmd" ]; then | ||||
if [ -n "$_env" ]; then | if [ -n "$_env" ]; then | ||||
eval "export -- $_env" | eval "export -- $_env" | ||||
fi | fi | ||||
# service can redefine all so check for valid targets | |||||
if [ "$rc_arg" = 'start' -o "$rc_arg" = 'restart' -o \ | |||||
"$rc_arg" = 'reload' ]; then | |||||
_run_rc_setup || warn "failed to setup ${name}" | |||||
fi | |||||
_run_rc_precmd || return 1 | _run_rc_precmd || return 1 | ||||
_run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1 | _run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1 | ||||
_run_rc_postcmd | _run_rc_postcmd | ||||
return $_return | return $_return | ||||
fi | fi | ||||
case "$rc_arg" in # default operations... | case "$rc_arg" in # default operations... | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | status) | ||||
;; | ;; | ||||
start) | start) | ||||
if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then | if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then | ||||
warn "run_rc_command: cannot run $command" | warn "run_rc_command: cannot run $command" | ||||
return 1 | return 1 | ||||
fi | fi | ||||
_run_rc_setup || warn "failed to setup ${name}" | |||||
if ! _run_rc_precmd; then | if ! _run_rc_precmd; then | ||||
warn "failed precmd routine for ${name}" | warn "failed precmd routine for ${name}" | ||||
return 1 | return 1 | ||||
fi | fi | ||||
# setup the full command to run | # setup the full command to run | ||||
# | # | ||||
startmsg "Starting ${name}." | startmsg "Starting ${name}." | ||||
if [ -n "$_chroot" ]; then | if [ -n "$_chroot" ]; then | ||||
_cd= | _cd= | ||||
_doit="\ | _doit="\ | ||||
${_nice:+nice -n $_nice }\ | ${_nice:+nice -n $_nice }\ | ||||
$_cpusetcmd \ | $_cpusetcmd \ | ||||
${_fib:+setfib -F $_fib }\ | ${_fib:+setfib -F $_fib }\ | ||||
${_env:+env $_env }\ | ${_env:+env $_env }\ | ||||
Show All 14 Lines | $_cpusetcmd $command $rc_flags $command_args" | ||||
fi | fi | ||||
_doit="nice -n $_nice $_doit" | _doit="nice -n $_nice $_doit" | ||||
fi | fi | ||||
if [ -n "$_prepend" ]; then | if [ -n "$_prepend" ]; then | ||||
_doit="$_prepend $_doit" | _doit="$_prepend $_doit" | ||||
fi | fi | ||||
fi | fi | ||||
if [ -n "$_setup" ]; then | |||||
if ! _run_rc_doit "$_setup"; then | |||||
warn "failed to setup ${name}" | |||||
fi | |||||
fi | |||||
# Prepend default limits | # Prepend default limits | ||||
_doit="$_cd limits -C $_login_class $_limits $_doit" | _doit="$_cd limits -C $_login_class $_limits $_doit" | ||||
# run the full command | # run the full command | ||||
# | # | ||||
if ! _run_rc_doit "$_doit"; then | if ! _run_rc_doit "$_doit"; then | ||||
warn "failed to start ${name}" | warn "failed to start ${name}" | ||||
return 1 | return 1 | ||||
fi | fi | ||||
# finally, run postcmd | # finally, run postcmd | ||||
# | # | ||||
_run_rc_postcmd | _run_rc_postcmd | ||||
;; | ;; | ||||
stop) | stop) | ||||
if [ -z "$rc_pid" ]; then | if [ -z "$rc_pid" ]; then | ||||
[ -n "$rc_fast" ] && return 0 | [ -n "$rc_fast" ] && return 0 | ||||
_run_rc_notrunning | _run_rc_notrunning | ||||
return 1 | return 1 | ||||
fi | fi | ||||
_run_rc_precmd || return 1 | _run_rc_precmd || return 1 | ||||
# send the signal to stop | # send the signal to stop | ||||
# | # | ||||
echo "Stopping ${name}." | echo "Stopping ${name}." | ||||
_doit=$(_run_rc_killcmd "${sig_stop:-TERM}") | _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") | ||||
_run_rc_doit "$_doit" || return 1 | _run_rc_doit "$_doit" || return 1 | ||||
# wait for the command to exit, | # wait for the command to exit, | ||||
# and run postcmd. | # and run postcmd. | ||||
wait_for_pids $rc_pid | wait_for_pids $rc_pid | ||||
_run_rc_postcmd | _run_rc_postcmd | ||||
;; | ;; | ||||
reload) | reload) | ||||
if [ -z "$rc_pid" ]; then | if [ -z "$rc_pid" ]; then | ||||
_run_rc_notrunning | _run_rc_notrunning | ||||
return 1 | return 1 | ||||
fi | fi | ||||
_run_rc_setup || warn "failed to setup ${name}" | |||||
_run_rc_precmd || return 1 | _run_rc_precmd || return 1 | ||||
_doit=$(_run_rc_killcmd "${sig_reload:-HUP}") | _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") | ||||
_run_rc_doit "$_doit" || return 1 | _run_rc_doit "$_doit" || return 1 | ||||
_run_rc_postcmd | _run_rc_postcmd | ||||
;; | ;; | ||||
restart) | restart) | ||||
_run_rc_setup || warn "failed to setup ${name}" | |||||
# prevent restart being called more | # prevent restart being called more | ||||
# than once by any given script | # than once by any given script | ||||
# | # | ||||
if ${_rc_restart_done:-false}; then | if ${_rc_restart_done:-false}; then | ||||
return 0 | return 0 | ||||
fi | fi | ||||
_rc_restart_done=true | _rc_restart_done=true | ||||
_run_rc_precmd || return 1 | _run_rc_precmd || return 1 | ||||
# run those in a subshell to keep global variables | # run those in a subshell to keep global variables | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | |||||
# Helper functions for run_rc_command: common code. | # Helper functions for run_rc_command: common code. | ||||
# They use such global variables besides the exported rc_* ones: | # They use such global variables besides the exported rc_* ones: | ||||
# | # | ||||
# name R/W | # name R/W | ||||
# ------------------ | # ------------------ | ||||
# _precmd R | # _precmd R | ||||
# _postcmd R | # _postcmd R | ||||
# _return W | # _return W | ||||
# _setup R | |||||
# | # | ||||
_run_rc_precmd() | _run_rc_precmd() | ||||
{ | { | ||||
check_required_before "$rc_arg" || return 1 | check_required_before "$rc_arg" || return 1 | ||||
if [ -n "$_precmd" ]; then | if [ -n "$_precmd" ]; then | ||||
debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" | debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" | ||||
eval "$_precmd $rc_extra_args" | eval "$_precmd $rc_extra_args" | ||||
Show All 11 Lines | |||||
} | } | ||||
_run_rc_postcmd() | _run_rc_postcmd() | ||||
{ | { | ||||
if [ -n "$_postcmd" ]; then | if [ -n "$_postcmd" ]; then | ||||
debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" | debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" | ||||
eval "$_postcmd $rc_extra_args" | eval "$_postcmd $rc_extra_args" | ||||
_return=$? | _return=$? | ||||
fi | |||||
return 0 | |||||
} | |||||
_run_rc_setup() | |||||
{ | |||||
# prevent multiple execution on restart => stop/start split | |||||
if ! ${_rc_restart_done:-false} && [ -n "$_setup" ]; then | |||||
debug "run_rc_command: ${rc_arg}_setup: $_setup" | |||||
eval "$_setup" | |||||
_return=$? | |||||
if [ $_return -ne 0 ]; then | |||||
return 1 | |||||
fi | |||||
fi | fi | ||||
return 0 | return 0 | ||||
} | } | ||||
_run_rc_doit() | _run_rc_doit() | ||||
{ | { | ||||
local _m | local _m | ||||
▲ Show 20 Lines • Show All 901 Lines • Show Last 20 Lines |