git: 194e059bb803 - main - service: Add -E option to set environment variables before starting a service.

From: Alexander Leidinger <netchild_at_FreeBSD.org>
Date: Mon, 19 Jun 2023 17:46:05 UTC
The branch main has been updated by netchild:

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

commit 194e059bb80334e6f4f791a186015b20d7f6f4b8
Author:     Alexander Leidinger <netchild@FreeBSD.org>
AuthorDate: 2022-11-30 18:31:41 +0000
Commit:     Alexander Leidinger <netchild@FreeBSD.org>
CommitDate: 2023-06-19 17:45:54 +0000

    service: Add -E option to set environment variables before starting a service.
    
    This allows for quicker testing/debugging of rc scripts and is a pre-req
    for automatic service jails.
    
    Differential Revision:  https://reviews.freebsd.org/D40369
    Reviewed by:            se
---
 usr.sbin/service/service.8  | 12 ++++++++++++
 usr.sbin/service/service.sh | 23 ++++++++++++++---------
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/usr.sbin/service/service.8 b/usr.sbin/service/service.8
index 9902ae3c8574..c2be0e0af037 100644
--- a/usr.sbin/service/service.8
+++ b/usr.sbin/service/service.8
@@ -48,6 +48,7 @@
 .Nm
 .Op Fl j Ar jail
 .Op Fl v
+.Op Fl E Ar var=value
 .Ar script
 .Ar command
 .Sh DESCRIPTION
@@ -67,6 +68,13 @@ the scripts using various criteria.
 .Pp
 The options are as follows:
 .Bl -tag -width F1
+.It Fl E Ar var=value
+Set the environment variable
+.Ar var
+to the specified
+.Ar value
+before starting the script.
+This option can be used multiple times.
 .It Fl e
 List services that are enabled.
 The list of scripts to check is compiled using
@@ -117,6 +125,9 @@ to
 which is how they are set in
 .Pa /etc/rc
 at boot time.
+If the
+.Fl E
+option is used, the corresponding variable is set accordingly.
 .Sh EXIT STATUS
 .Ex -std
 .Sh EXAMPLES
@@ -126,6 +137,7 @@ command:
 .Bd -literal -offset -ident
 service named status
 service -j dns named status
+service -E LC_ALL=C.UTF-8 named start
 service -rv
 .Ed
 .Pp
diff --git a/usr.sbin/service/service.sh b/usr.sbin/service/service.sh
index f056c43d3c98..5f232955bdff 100755
--- a/usr.sbin/service/service.sh
+++ b/usr.sbin/service/service.sh
@@ -37,21 +37,23 @@ usage () {
 	echo "${0##*/} [-j <jail name or id>] -e"
 	echo "${0##*/} [-j <jail name or id>] -R"
 	echo "${0##*/} [-j <jail name or id>] [-v] -l | -r"
-	echo "${0##*/} [-j <jail name or id>] [-v] <rc.d script> start|stop|etc."
+	echo "${0##*/} [-j <jail name or id>] [-v] [-E var=value] <rc.d script> start|stop|etc."
 	echo "${0##*/} -h"
 	echo ''
-	echo "-j	Perform actions within the named jail"
-	echo '-e	Show services that are enabled'
-	echo "-R	Stop and start enabled $local_startup services"
-	echo "-l	List all scripts in /etc/rc.d and $local_startup"
-	echo '-r	Show the results of boot time rcorder'
-	echo '-v	Verbose'
+	echo "-j		Perform actions within the named jail"
+	echo "-E n=val	Set variable n to val before executing the rc.d script"
+	echo '-e		Show services that are enabled'
+	echo "-R		Stop and start enabled $local_startup services"
+	echo "-l		List all scripts in /etc/rc.d and $local_startup"
+	echo '-r		Show the results of boot time rcorder'
+	echo '-v		Verbose'
 	echo ''
 }
 
-while getopts 'j:ehlrRv' COMMAND_LINE_ARGUMENT ; do
+while getopts 'j:E:ehlrRv' COMMAND_LINE_ARGUMENT ; do
 	case "${COMMAND_LINE_ARGUMENT}" in
 	j)	JAIL="${OPTARG}" ;;
+	E)	VARS="${VARS} ${OPTARG}" ;;
 	e)	ENABLED=eopt ;;
 	h)	usage ; exit 0 ;;
 	l)	LIST=lopt ;;
@@ -72,6 +74,9 @@ if [ -n "${JAIL}" ]; then
 	[ -n "${RCORDER}" ] && args="${args} -r"
 	[ -n "${RESTART}" ] && args="${args} -R"
 	[ -n "${VERBOSE}" ] && args="${args} -v"
+	for var in ${VARS}; do
+		args="${args} -E ${var}"
+	done
 
 	# Call jexec(8) with the rebuild args and any positional args that
 	# were left in $@
@@ -171,7 +176,7 @@ cd /
 for dir in /etc/rc.d $local_startup; do
 	if [ -x "$dir/$script" ]; then
 		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
-		exec env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin "$dir/$script" "$@"
+		exec env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin ${VARS} "$dir/$script" "$@"
 	fi
 done