From nobody Wed May 29 17:28:25 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VqGZ15LBPz5Mt5L; Wed, 29 May 2024 17:28:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VqGZ14mwbz4p1B; Wed, 29 May 2024 17:28:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1717003705; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7t7o+ILjcXU+VOY7hdCxbzhYrRBDho5X9FBvM4PPEbA=; b=qFnopvnXFOwYDI0nk19E8h23RqI5DLNyFiJHCuB4G/szR3o2yvEZYHPY/q06CZllnneM2f F9bd3slzuWnkcHqaYQ/3JOhUIo9hYv2+dp2hX9VjgKYoa9/nvYxfjUnAvATuctZ4Hsjbpb rMVZi1txG5tsv83VBuoGv36/chSuRoIstUtAUBZh8o9dzh8scrVrMdEFk/5dxUC8FI1tpN PsoP87Ka17mIwEMIzsTlir6O2fp6Xl/F8Jar+cCJxEaAtnc8s1AoG+7Bq04kloGK3r//NY Zg5GggL/cpQJUknpm7SMhdaiUHtaH4vcU4g5dXYbqkvOHsFSyDnSJgy1cZ6ldw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1717003705; a=rsa-sha256; cv=none; b=D1CazFB6uymjX/QBo/eUF0oN4yi3a+eKlePzhV4LGIhoXOqlrXdJvnLmvBPTeF3naKWg8j OpfdIMezUgqTK3RSGODtFKvgElYyVMydu2SPm29q7wI6aOuFMjGOLYrN10WCfKpqnDDPNY t4QU+xV2OPmevhX0hNabA6XBfDQGonMJFdJW8n7xIaWqiITtW3GAbwBD3gM6b3Yph8VL/D kNKLHchTidpaFbQco8CZhVaeAwi4353gOWA0N61M1/2H+88WR35Un4tquS/g4kdNR+hXhi 6JJ+LD6xuyDL93BLow3VSxfnYKaIKZbrSJkbnrFCCs5LdhCXrQLsNXUMzfpQTw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1717003705; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7t7o+ILjcXU+VOY7hdCxbzhYrRBDho5X9FBvM4PPEbA=; b=G6QfbccMWbtdFVrHIwymiKAXK2neU1/VVBfPJKku+PATNXw8bgYmrJL5idr6P8V2GfQoZb vRDesMHajcHEJAi1MgO4ctw2IVRCdxARu/ksgJBUzqVREnTY7lR0Su1uEyExf+/CfC56hW OP6Jsg9o8r8dFjz2dF1t03G8HV+kU5BQQkGocysYV3/uVBxYCDhhc+K97OC01YTeZbFKp9 67KxYDZK7UzlQVY+9xB63Pelr8tngU5uzIbNDDTfEKGbYiMLiVVYYXFq2M60C6KWWSommT unYS91PesCCxIWY3c+W/+u2Y5iBDGorNbs76zWdQIGosj6w2dLVDTocOzqL4tg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VqGZ14Mr1z15BT; Wed, 29 May 2024 17:28:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 44THSPSE086497; Wed, 29 May 2024 17:28:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 44THSPmO086494; Wed, 29 May 2024 17:28:25 GMT (envelope-from git) Date: Wed, 29 May 2024 17:28:25 GMT Message-Id: <202405291728.44THSPmO086494@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 11333dd580b7 - main - rc: improve NAME_setup handling List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 11333dd580b704fb4ff0836e1d1a0634099b0cf6 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=11333dd580b704fb4ff0836e1d1a0634099b0cf6 commit 11333dd580b704fb4ff0836e1d1a0634099b0cf6 Author: Franco Fichtner AuthorDate: 2024-05-24 16:38:56 +0000 Commit: Warner Losh CommitDate: 2024-05-29 17:23:46 +0000 rc: improve NAME_setup handling 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. Differential-Revsiion: https://reviews.freebsd.org/D36259 Signed-off-by: Franco Fichtner Reviewed by: imp, oshogbo Pull Request: https://github.com/freebsd/freebsd-src/pull/1258 --- libexec/rc/rc.subr | 73 +++++++++++++++++++++++++++++++----------------- share/man/man8/rc.subr.8 | 13 +++++++-- 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/libexec/rc/rc.subr b/libexec/rc/rc.subr index 23651cbf8e58..1b0fdc837228 100644 --- a/libexec/rc/rc.subr +++ b/libexec/rc/rc.subr @@ -975,7 +975,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". # @@ -1287,9 +1288,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 @@ -1301,6 +1302,14 @@ run_rc_command() fi if [ "${_rc_svcj}" != jailing ]; then + # service can redefine all so + # check for valid setup target + 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 fi if ! checkyesno ${name}_svcj; then @@ -1400,6 +1409,8 @@ run_rc_command() fi if [ "${_rc_svcj}" != jailing ]; then + _run_rc_setup || warn "failed to setup ${name}" + if ! _run_rc_precmd; then warn "failed precmd routine for ${name}" return 1 @@ -1416,8 +1427,8 @@ run_rc_command() fi fi - # setup the full command to run - # + # setup the full command to run + # startmsg "Starting ${name}." if [ -n "$_chroot" ]; then _cd= @@ -1448,16 +1459,9 @@ $_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" - local _really_run_it=true if checkyesno ${name}_svcj; then if [ "${_rc_svcj}" != jailing ]; then @@ -1466,8 +1470,8 @@ $_cpusetcmd $command $rc_flags $command_args" fi if [ "$_really_run_it" = true ]; then - # run the full command - # + # run the full command + # if ! _run_rc_doit "$_doit"; then warn "failed to start ${name}" return 1 @@ -1475,8 +1479,8 @@ $_cpusetcmd $command $rc_flags $command_args" fi if [ "${_rc_svcj}" != jailing ]; then - # finally, run postcmd - # + # finally, run postcmd + # _run_rc_postcmd fi ;; @@ -1490,14 +1494,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 if checkyesno ${name}_svcj; then @@ -1514,6 +1518,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}") @@ -1523,9 +1529,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 @@ -1638,6 +1646,7 @@ $_cpusetcmd $command $rc_flags $command_args" # _precmd R # _postcmd R # _return W +# _setup R # _run_rc_precmd() { @@ -1669,6 +1678,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 0906ecb2e787..8f7b72e96dc5 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 February 10, 2024 +.Dd May 28, 2024 .Dt RC.SUBR 8 .Os .Sh NAME @@ -826,8 +826,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 .