From 45e47679fef0bf91013d52e32a488caaaa355d8b Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Tue, 26 Mar 2024 14:55:44 +0100 Subject: [PATCH] rc: also run NAME_setup on NAME_reload Reload is used for service reconfiguration as well and lacks a NAME_prepend-like mechanism so it makes sense to extend the NAME_reload hook into this action. precmd may use configuration checks and blocks setup from doing its designated work (e.g. nginx). In moving the invoke of the setup script in front allows us to provide custom scripts for config file generation and fixing prior to precmd checking configuration integrity. Also introduce _run_rc_setup to separate the launcher from the main one. Let it run correctly in the case of restart_precmd and block further execution as would be the case in start due to the internal plumbing of restart being split into calling stop and start afterwards. PR: https://reviews.freebsd.org/D36259 --- libexec/rc/rc.subr | 69 ++++++++++++++++++++++++++-------------- share/man/man8/rc.subr.8 | 13 ++++++-- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/libexec/rc/rc.subr b/libexec/rc/rc.subr index 5140f2a1add..c2a77ef00fc 100644 --- a/libexec/rc/rc.subr +++ b/libexec/rc/rc.subr @@ -810,7 +810,8 @@ startmsg() # # ${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". # @@ -1072,9 +1073,9 @@ run_rc_command() return 1 fi - # if there's a custom ${XXX_cmd}, - # run that instead of the default - # + # if there's a custom ${XXX_cmd}, + # run that instead of the default + # eval _cmd=\$${rc_arg}_cmd \ _precmd=\$${rc_arg}_precmd \ _postcmd=\$${rc_arg}_postcmd @@ -1083,6 +1084,11 @@ run_rc_command() if [ -n "$_env" ]; then eval "export -- $_env" 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_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1 _run_rc_postcmd @@ -1143,13 +1149,15 @@ run_rc_command() return 1 fi + _run_rc_setup || warn "failed to setup ${name}" + if ! _run_rc_precmd; then warn "failed precmd routine for ${name}" return 1 fi - # setup the full command to run - # + # setup the full command to run + # startmsg "Starting ${name}." if [ -n "$_chroot" ]; then _cd= @@ -1180,24 +1188,18 @@ $_cpusetcmd $command $rc_flags $command_args" 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" - # run the full command - # + # run the full command + # if ! _run_rc_doit "$_doit"; then warn "failed to start ${name}" return 1 fi - # finally, run postcmd - # + # finally, run postcmd + # _run_rc_postcmd ;; @@ -1210,14 +1212,14 @@ $_cpusetcmd $command $rc_flags $command_args" _run_rc_precmd || return 1 - # send the signal to stop - # + # send the signal to stop + # echo "Stopping ${name}." _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") _run_rc_doit "$_doit" || return 1 - # wait for the command to exit, - # and run postcmd. + # wait for the command to exit, + # and run postcmd. wait_for_pids $rc_pid _run_rc_postcmd @@ -1229,6 +1231,8 @@ $_cpusetcmd $command $rc_flags $command_args" return 1 fi + _run_rc_setup || warn "failed to setup ${name}" + _run_rc_precmd || return 1 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") @@ -1238,9 +1242,11 @@ $_cpusetcmd $command $rc_flags $command_args" ;; restart) - # prevent restart being called more - # than once by any given script - # + _run_rc_setup || warn "failed to setup ${name}" + + # prevent restart being called more + # than once by any given script + # if ${_rc_restart_done:-false}; then return 0 fi @@ -1354,6 +1360,7 @@ $_cpusetcmd $command $rc_flags $command_args" # _precmd R # _postcmd R # _return W +# _setup R # _run_rc_offcmd() { @@ -1399,6 +1406,20 @@ _run_rc_postcmd() 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 + return 0 +} + _run_rc_doit() { local _m diff --git a/share/man/man8/rc.subr.8 b/share/man/man8/rc.subr.8 index 844ba035669..c44a6b18369 100644 --- a/share/man/man8/rc.subr.8 +++ b/share/man/man8/rc.subr.8 @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd September 22, 2024 +.Dd November 21, 2023 .Dt RC.SUBR 8 .Os .Sh NAME @@ -686,8 +686,15 @@ This is a generic version of or .Va ${name}_nice . .It Va ${name}_setup -Command to be run prior to -.Va command . +Optional command to be run during +.Cm start , +.Cm restart , +and +.Cm reload +prior to the respective +.Ar argument Ns Va _precmd . +If the command fails for any reason it will output a warning, +but execution will continue. .It Ar argument Ns Va _cmd Shell commands which override the default method for .Ar argument .