diff --git a/libexec/rc/rc.subr b/libexec/rc/rc.subr --- a/libexec/rc/rc.subr +++ b/libexec/rc/rc.subr @@ -806,7 +806,8 @@ # # ${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 # # ${name}_login_class n Login class to use, else "daemon". # @@ -1047,9 +1048,9 @@ 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 @@ -1058,6 +1059,11 @@ 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 "$_cmd $rc_extra_args" || return 1 _run_rc_postcmd @@ -1118,13 +1124,15 @@ 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= @@ -1154,24 +1162,18 @@ 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 ;; @@ -1203,6 +1205,8 @@ return 1 fi + _run_rc_setup || warn "failed to setup ${name}" + _run_rc_precmd || return 1 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") @@ -1327,6 +1331,7 @@ # _precmd R # _postcmd R # _return W +# _setup R # _run_rc_precmd() { @@ -1358,6 +1363,19 @@ return 0 } +_run_rc_setup() +{ + if [ -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 --- a/share/man/man8/rc.subr.8 +++ b/share/man/man8/rc.subr.8 @@ -29,7 +29,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 16, 2022 +.Dd December 14, 2022 .Dt RC.SUBR 8 .Os .Sh NAME @@ -686,8 +686,24 @@ 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. +Please note that +.Cm restart +is implicit through +.Cm start +so a failure of +.Ar restart Ns Va _precmd +will prevent its execution unless +.Ar restart Ns Va _cmd +was defined. .It Ar argument Ns Va _cmd Shell commands which override the default method for .Ar argument .