git: 318d0db5fe8a - main - rc.subr: boottrace annotations

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Tue, 22 Feb 2022 00:17:23 UTC
The branch main has been updated by mhorne:

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

commit 318d0db5fe8a25321321d27dfcbe42d36996876c
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2021-01-17 22:56:59 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2022-02-22 00:16:12 +0000

    rc.subr: boottrace annotations
    
    When enabled, have the framework use the boottrace(8) utility to execute
    each rc script, generating trace entries for the entire suite of
    scripts.
    
    Reviewed by:    0mp (slightly earlier version)
    Sponsored by:   NetApp, Inc.
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D31930
---
 libexec/rc/rc.subr | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/libexec/rc/rc.subr b/libexec/rc/rc.subr
index 7144c3cbfce9..76f878c11e29 100644
--- a/libexec/rc/rc.subr
+++ b/libexec/rc/rc.subr
@@ -1411,7 +1411,9 @@ run_rc_script()
 		;;
 	*)				# run in subshell
 		if [ -x $_file ]; then
-			if [ -n "$rc_fast_and_loose" ]; then
+			if [ -n "$rc_boottrace" ]; then
+				boottrace_fn "$_file" "$_arg"
+			elif [ -n "$rc_fast_and_loose" ]; then
 				set $_arg; . $_file
 			else
 				( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3
@@ -1424,6 +1426,26 @@ run_rc_script()
 	esac
 }
 
+boottrace_fn()
+{
+	local _file _arg
+	_file=$1
+	_arg=$2
+
+	if [ -n "$rc_fast_and_loose" ]; then
+		boottrace_sysctl "$_file start"
+		set $_arg; . $_file
+		boottrace_sysctl "$_file done"
+	else
+		$boottrace_cmd "$_file" "$_arg"
+	fi
+}
+
+boottrace_sysctl()
+{
+	${SYSCTL} kern.boottrace.boottrace="$1"
+}
+
 #
 # load_rc_config [service]
 #	Source in the configuration file(s) for a given service.
@@ -2195,3 +2217,8 @@ _echoonce()
 if kenv -q rc.debug > /dev/null ; then
 	rc_debug=YES
 fi
+
+boottrace_cmd=`command -v boottrace`
+if [ -n "$boottrace_cmd" ] && [ "`${SYSCTL_N} -q kern.boottrace.enabled`" = "1" ]; then
+	rc_boottrace=YES
+fi