git: 32a579e4fc69 - main - rc.subr(8): introduce ${name}_offcmd

From: Eugene Grosbein <eugen_at_FreeBSD.org>
Date: Sun, 02 Jun 2024 19:29:41 UTC
The branch main has been updated by eugen:

URL: https://cgit.FreeBSD.org/src/commit/?id=32a579e4fc69a65e8901111ad5f65ec56a97dfab

commit 32a579e4fc69a65e8901111ad5f65ec56a97dfab
Author:     Eugene Grosbein <eugen@FreeBSD.org>
AuthorDate: 2024-06-02 19:13:42 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2024-06-02 19:29:22 +0000

    rc.subr(8): introduce ${name}_offcmd
    
    New variable ${name}_offcmd may be used to supply commands
    executed if named service is not enabled. Previously start_precmd
    could be used for such a task but now rc.subr(8) does not call it
    if a service is not enabled.
    
    Fix devd startup script to use it instead of start_precmd.
    
    PR:             279198
    MFC after:      2 weeks
    Reported by:    Dmitry S. Lukhtionov
    Tested by:      Dmitry S. Lukhtionov
---
 libexec/rc/rc.d/devd     |  7 +++----
 libexec/rc/rc.subr       | 25 +++++++++++++++++++++----
 share/man/man8/rc.subr.8 |  4 +++-
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/libexec/rc/rc.d/devd b/libexec/rc/rc.d/devd
index 47326662339c..98f2068c2075 100755
--- a/libexec/rc/rc.d/devd
+++ b/libexec/rc/rc.d/devd
@@ -14,7 +14,8 @@ desc="Device state change daemon"
 rcvar="devd_enable"
 command="/sbin/${name}"
 
-start_precmd=${name}_prestart
+devd_offcmd=devd_off
+start_precmd=find_pidfile
 stop_precmd=find_pidfile
 
 find_pidfile()
@@ -26,10 +27,8 @@ find_pidfile()
 	fi
 }
 
-devd_prestart()
+devd_off()
 {
-	find_pidfile
-
 	# If devd is disabled, turn it off in the kernel to avoid unnecessary
 	# memory usage.
 	if ! checkyesno ${rcvar}; then
diff --git a/libexec/rc/rc.subr b/libexec/rc/rc.subr
index 2fd4796b569f..9eead30790e2 100644
--- a/libexec/rc/rc.subr
+++ b/libexec/rc/rc.subr
@@ -982,6 +982,8 @@ startmsg()
 #
 #	${name}_limits	n	limits(1) to apply to ${command}.
 #
+#	${name}_offcmd	n	If set, run if a service is not enabled.
+#
 #	${rc_arg}_cmd	n	If set, use this as the method when invoked;
 #				Otherwise, use default command (see below)
 #
@@ -1270,13 +1272,13 @@ run_rc_command()
 		    -a "$rc_arg" != "describe" -a "$rc_arg" != "status" ] ||
 		    [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then
 			if ! checkyesno ${rcvar}; then
-				if [ -n "${rc_quiet}" ]; then
-					return 0
-				fi
+			    [ "$rc_arg" = "start" ] && _run_rc_offcmd
+			    if [ -z "${rc_quiet}" ]; then
 				echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
 				echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
 				echo "instead of '${rc_arg}'."
-				return 0
+			    fi
+			    return 0
 			fi
 		fi
 
@@ -1643,11 +1645,26 @@ $_cpusetcmd $command $rc_flags $command_args"
 #
 #	name	       R/W
 #	------------------
+#	_offcmd		R
 #	_precmd		R
 #	_postcmd	R
 #	_return		W
 #	_setup		R
 #
+_run_rc_offcmd()
+{
+	eval _offcmd=\$${name}_offcmd
+	if [ -n "$_offcmd" ]; then
+		if [ -n "$_env" ]; then
+			eval "export -- $_env"
+		fi
+		debug "run_rc_command: ${rc_arg}_offcmd: $_offcmd $rc_extra_args"
+		eval "$_offcmd $rc_extra_args"
+		_return=$?
+	fi
+	return 0
+}
+
 _run_rc_precmd()
 {
 	check_required_before "$rc_arg" || return 1
diff --git a/share/man/man8/rc.subr.8 b/share/man/man8/rc.subr.8
index 8f7b72e96dc5..4e3fa6771bf4 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 May 28, 2024
+.Dd June 2, 2024
 .Dt RC.SUBR 8
 .Os
 .Sh NAME
@@ -773,6 +773,8 @@ Login class to use with
 .Va ${name}_limits .
 Defaults to
 .Dq Li daemon .
+.It Va ${name}_offcmd
+Shell commands to run if a service is not enabled.
 .It Va ${name}_oomprotect
 .Xr protect 1
 .Va command