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
This commit is contained in:
Alexander Leidinger 2022-11-30 19:31:41 +01:00
parent f19ae3633b
commit 194e059bb8
2 changed files with 26 additions and 9 deletions

View file

@ -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

View file

@ -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