git: efb1717d21c2 - stable/14 - rc.subr(8): MFC: introduce ${name}_offcmd

From: Eugene Grosbein <eugen_at_FreeBSD.org>
Date: Mon, 17 Jun 2024 04:59:12 UTC
The branch stable/14 has been updated by eugen:

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

commit efb1717d21c2cc488955914cb4fe9f3683b9cbe5
Author:     Eugene Grosbein <eugen@FreeBSD.org>
AuthorDate: 2024-06-02 19:13:42 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2024-06-17 04:57:53 +0000

    rc.subr(8): MFC: 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
    Reported by:    Dmitry S. Lukhtionov
    Tested by:      Dmitry S. Lukhtionov
    
    (cherry picked from commit 32a579e4fc69a65e8901111ad5f65ec56a97dfab)
    (cherry picked from commit c2db3a0c7d31116028b38b426a9b139d26cbc7e5)
---
 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 43fb9d5928dd..fe19abd817b4 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 332718dc6f2c..a662b878860b 100644
--- a/libexec/rc/rc.subr
+++ b/libexec/rc/rc.subr
@@ -816,6 +816,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)
 #
@@ -1051,13 +1053,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
 
@@ -1347,10 +1349,25 @@ $_cpusetcmd $command $rc_flags $command_args"
 #
 #	name	       R/W
 #	------------------
+#	_offcmd		R
 #	_precmd		R
 #	_postcmd	R
 #	_return		W
 #
+_run_rc_offcmd()
+{
+	eval _offcmd=\$${name}_offcmd
+	if [ -n "$_offcmd" ]; then
+		if [ -n "$_env" ]; then
+			eval "export -- $_env"
+		fi
+		debug "run_rc_command: ${name}_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 3e3ff8c8d1e1..3037840007a4 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 August 16, 2022
+.Dd June 2, 2024
 .Dt RC.SUBR 8
 .Os
 .Sh NAME
@@ -631,6 +631,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