git: c0e249d32c78 - main - bsdinstall: avoid conflicts with fd 3

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Thu, 21 Sep 2023 07:51:56 UTC
The branch main has been updated by corvink:

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

commit c0e249d32c780ee8240fe8b3b8144078a8eec41f
Author:     Lars Kellogg-Stedman <lars@oddbit.com>
AuthorDate: 2023-08-15 15:44:02 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-09-21 06:41:48 +0000

    bsdinstall: avoid conflicts with fd 3
    
    Throughout the bsdinstall script fd 3 is used by f_dprintf (set through
    $TERMINAL_STDOUT_PASSTHRU). In several places in the bsdinstalls scripts,
    we use fd 3 to juggle stdout when calling out to other tools, which can
    cause the installer to fail with a "Bad file descriptor" error when
    f_dprintf attempts to use it.
    
    This commit replaces all constructs like this:
    
        exec 3>&1
        SOME_VARIABLE=$(some command 2>&1 1>&3)
        exec 3>&-
    
    With:
    
        exec 5>&1
        SOME_VARIABLE=$(some command 2>&1 1>&5)
        exec 5>&-
    
    PR:                     273148
    Reviewed by:            corvink
    Fixes:                  1f7746d81f53447ac15cc99395bb714d4dd0a4da ("bsdinstall: stop messing with file descriptors")
    MFC after:              1 week
---
 usr.sbin/bsdinstall/scripts/auto              | 22 +++++++++++-----------
 usr.sbin/bsdinstall/scripts/fetchmissingdists |  6 +++---
 usr.sbin/bsdinstall/scripts/hardening         |  6 +++---
 usr.sbin/bsdinstall/scripts/jail              | 16 ++++++++--------
 usr.sbin/bsdinstall/scripts/mirrorselect      | 12 ++++++------
 usr.sbin/bsdinstall/scripts/netconfig         | 12 ++++++------
 usr.sbin/bsdinstall/scripts/netconfig_ipv4    |  6 +++---
 usr.sbin/bsdinstall/scripts/netconfig_ipv6    |  6 +++---
 usr.sbin/bsdinstall/scripts/script            | 13 ++++++-------
 usr.sbin/bsdinstall/scripts/services          |  6 +++---
 usr.sbin/bsdinstall/scripts/time              | 12 ++++++------
 11 files changed, 58 insertions(+), 59 deletions(-)

diff --git a/usr.sbin/bsdinstall/scripts/auto b/usr.sbin/bsdinstall/scripts/auto
index fd5b634696ae..95084f1bdcc3 100755
--- a/usr.sbin/bsdinstall/scripts/auto
+++ b/usr.sbin/bsdinstall/scripts/auto
@@ -159,13 +159,13 @@ if [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
 	DISTMENU="$(echo ${DISTMENU} | sed -E 's/\.txz//g')"
 
 	if [ -n "$DISTMENU" ]; then
-		exec 3>&1
+		exec 5>&1
 		EXTRA_DISTS=$( eval dialog \
 		    --backtitle \"$OSNAME Installer\" \
 		    --title \"Distribution Select\" --nocancel --separate-output \
 		    --checklist \"Choose optional system components to install:\" \
 		    0 0 0 $DISTMENU \
-		2>&1 1>&3 )
+		2>&1 1>&5 )
 		for dist in $EXTRA_DISTS; do
 			export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
 		done
@@ -301,13 +301,13 @@ case $CURARCH in
 		;;
 esac
 
-exec 3>&1
+exec 5>&1
 PARTMODE=`echo $PMODES | xargs dialog --backtitle "$OSNAME Installer" \
 	--title "Partitioning" \
 	--item-help \
 	--menu "How would you like to partition your disk?" \
-	0 0 0 2>&1 1>&3` || exit 1
-exec 3>&-
+	0 0 0 2>&1 1>&5` || exit 1
+exec 5>&-
 
 case "$PARTMODE" in
 "$msg_auto_zfs")	# ZFS
@@ -340,10 +340,10 @@ esac
 [ -f /usr/libexec/bsdinstall/local.pre-fetch ] && f_dprintf "Running local.pre-fetch" && sh /usr/libexec/bsdinstall/local.pre-fetch "$BSDINSTALL_CHROOT"
 
 if [ -n "$FETCH_DISTRIBUTIONS" ]; then
-	exec 3>&1
-	export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&3)
+	exec 5>&1
+	export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&5)
 	FETCH_RESULT=$?
-	exec 3>&-
+	exec 5>&-
 
 	[ $FETCH_RESULT -ne 0 ] && error "Could not fetch remote distributions"
 fi
@@ -371,7 +371,7 @@ fi
     bsdinstall adduser
 
 finalconfig() {
-	exec 3>&1
+	exec 5>&1
 	REVISIT=$(dialog --backtitle "$OSNAME Installer" \
 	    --title "Final Configuration" --no-cancel --menu \
 	    "Setup of your $OSNAME system is nearly complete. You can now modify your configuration choices. After this screen, you will have an opportunity to make more complex changes using a shell." 0 0 0 \
@@ -383,8 +383,8 @@ finalconfig() {
 		"Services" "Set daemons to run on startup" \
 		"System Hardening" "Set security options" \
 		"Time Zone" "Set system timezone" \
-		"Handbook" "Install $OSNAME Handbook (requires network)" 2>&1 1>&3)
-	exec 3>&-
+		"Handbook" "Install $OSNAME Handbook (requires network)" 2>&1 1>&5)
+	exec 5>&-
 
 	case "$REVISIT" in
 	"Add User")
diff --git a/usr.sbin/bsdinstall/scripts/fetchmissingdists b/usr.sbin/bsdinstall/scripts/fetchmissingdists
index a1ce9e4e0ca5..d37acc96b92c 100644
--- a/usr.sbin/bsdinstall/scripts/fetchmissingdists
+++ b/usr.sbin/bsdinstall/scripts/fetchmissingdists
@@ -61,10 +61,10 @@ BSDINSTALL_FETCHDEST="$BSDINSTALL_CHROOT/usr/freebsd-dist"
 mkdir -p "$BSDINSTALL_FETCHDEST" || error "Could not create directory $BSDINSTALL_FETCHDEST"
 
 if [ -z "$BSDINSTALL_DISTSITE" ]; then
-	exec 3>&1
-	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&3)
+	exec 5>&1
+	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&5)
 	MIRROR_BUTTON=$?
-	exec 3>&-
+	exec 5>&-
 	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
 	export BSDINSTALL_DISTSITE
 fi
diff --git a/usr.sbin/bsdinstall/scripts/hardening b/usr.sbin/bsdinstall/scripts/hardening
index b9e259ff1a13..a77fcdeaf540 100755
--- a/usr.sbin/bsdinstall/scripts/hardening
+++ b/usr.sbin/bsdinstall/scripts/hardening
@@ -34,7 +34,7 @@ echo -n > $BSDINSTALL_TMPETC/rc.conf.hardening
 echo -n > $BSDINSTALL_TMPETC/sysctl.conf.hardening
 echo -n > $BSDINSTALL_TMPBOOT/loader.conf.hardening
 
-exec 3>&1
+exec 5>&1
 FEATURES=$( bsddialog --backtitle "$OSNAME Installer" \
     --title "System Hardening" --nocancel --separate-output \
     --checklist "Choose system security hardening options:" \
@@ -49,9 +49,9 @@ FEATURES=$( bsddialog --backtitle "$OSNAME Installer" \
 	"7 disable_syslogd" "Disable opening Syslogd network socket (disables remote logging)" ${disable_syslogd:-off} \
 	"8 secure_console" "Enable console password prompt" ${secure_console:-off} \
 	"9 disable_ddtrace" "Disallow DTrace destructive-mode" ${disable_ddtrace:-off} \
-2>&1 1>&3 )
+2>&1 1>&5 )
 retval=$?
-exec 3>&-
+exec 5>&-
 
 if [ $retval -ne $BSDDIALOG_OK ]; then
 	exit 1
diff --git a/usr.sbin/bsdinstall/scripts/jail b/usr.sbin/bsdinstall/scripts/jail
index e5822087e97b..ab095f44ba39 100755
--- a/usr.sbin/bsdinstall/scripts/jail
+++ b/usr.sbin/bsdinstall/scripts/jail
@@ -66,10 +66,10 @@ fi
 test ! -d $BSDINSTALL_DISTDIR && mkdir -p $BSDINSTALL_DISTDIR
 
 if [ ! -f $BSDINSTALL_DISTDIR/MANIFEST -a -z "$BSDINSTALL_DISTSITE" ]; then
-	exec 3>&1
-	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&3)
+	exec 5>&1
+	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&5)
 	MIRROR_BUTTON=$?
-	exec 3>&-
+	exec 5>&-
 	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
 	export BSDINSTALL_DISTSITE
 	fetch -o $BSDINSTALL_DISTDIR/MANIFEST $BSDINSTALL_DISTSITE/MANIFEST || error "Could not download $BSDINSTALL_DISTSITE/MANIFEST"
@@ -81,13 +81,13 @@ if [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
 
     if [ ! "$nonInteractive" == "YES" ]
     then
-	    exec 3>&1
+	    exec 5>&1
 	    EXTRA_DISTS=$(echo $DISTMENU | xargs -o bsddialog \
 	        --backtitle "$OSNAME Installer" \
 	        --title "Distribution Select" --no-cancel --separate-output \
 	        --checklist "Choose optional system components to install:" \
 	        0 0 0 \
-	    2>&1 1>&3)
+	    2>&1 1>&5)
 	    for dist in $EXTRA_DISTS; do
 	    	export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
 	    done
@@ -103,10 +103,10 @@ done
 FETCH_DISTRIBUTIONS=`echo $FETCH_DISTRIBUTIONS`	# Trim white space
 
 if [ -n "$FETCH_DISTRIBUTIONS" -a -z "$BSDINSTALL_DISTSITE" ]; then
-	exec 3>&1
-	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&3)
+	exec 5>&1
+	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&5)
 	MIRROR_BUTTON=$?
-	exec 3>&-
+	exec 5>&-
 	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
 	export BSDINSTALL_DISTSITE
 fi
diff --git a/usr.sbin/bsdinstall/scripts/mirrorselect b/usr.sbin/bsdinstall/scripts/mirrorselect
index 82a726c009b6..901f816206ba 100755
--- a/usr.sbin/bsdinstall/scripts/mirrorselect
+++ b/usr.sbin/bsdinstall/scripts/mirrorselect
@@ -35,7 +35,7 @@ BSDCFG_SHARE="/usr/share/bsdconfig"
 : ${BSDDIALOG_ESC=5}
 : ${BSDDIALOG_ERROR=255}
 
-exec 3>&1
+exec 5>&1
 MIRROR=`bsddialog --backtitle "$OSNAME Installer" \
     --title "Mirror Selection" --extra-button --extra-label "Other" \
     --menu "Please select the best suitable site for you or \"other\" if you want to specify a different choice. The \"Main Site\" directs users to the nearest project managed mirror via GeoDNS (they carry the full range of possible distributions and support both IPv4 and IPv6). All other sites are known as \"Community Mirrors\"; not every site listed here carries more than the base distribution kits. Select a site!" \
@@ -88,9 +88,9 @@ MIRROR=`bsddialog --backtitle "$OSNAME Installer" \
 	ftp://ftp5.us.freebsd.org 	"USA #5 - IPv6"\
 	ftp://ftp11.us.freebsd.org 	"USA #11 - IPv6"\
 	ftp://ftp14.us.freebsd.org 	"USA #14"\
-    2>&1 1>&3`
+    2>&1 1>&5`
 MIRROR_BUTTON=$?
-exec 3>&-
+exec 5>&-
 
 _UNAME_R=`uname -r`
 _UNAME_R=${_UNAME_R%-p*}
@@ -113,13 +113,13 @@ $BSDDIALOG_ERROR | $BSDDIALOG_CANCEL | $BSDDIALOG_ESC)
 $BSDDIALOG_OK)
 	;;
 $BSDDIALOG_EXTRA)
-	exec 3>&1
+	exec 5>&1
 	BSDINSTALL_DISTSITE=`bsddialog --backtitle "$OSNAME Installer" \
 	    --title "Mirror Selection" \
 	    --inputbox "Please enter the URL to an alternate $OSNAME mirror:" \
-	    0 74 "$BSDINSTALL_DISTSITE" 2>&1 1>&3`
+	    0 74 "$BSDINSTALL_DISTSITE" 2>&1 1>&5`
 	MIRROR_BUTTON=$?
-	exec 3>&-
+	exec 5>&-
 	test $MIRROR_BUTTON -eq $BSDDIALOG_OK || exec $0 $@
 	;;
 esac
diff --git a/usr.sbin/bsdinstall/scripts/netconfig b/usr.sbin/bsdinstall/scripts/netconfig
index bdbbd42b3328..99c129ab61f5 100755
--- a/usr.sbin/bsdinstall/scripts/netconfig
+++ b/usr.sbin/bsdinstall/scripts/netconfig
@@ -71,10 +71,10 @@ if [ -z "$INTERFACES" ]; then
 	exit 1
 fi
 
-exec 3>&1
-INTERFACE=`echo $BSDDIALOG_ITEMS | xargs -o bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' --menu 'Please select a network interface to configure:' 0 0 0 2>&1 1>&3`
+exec 5>&1
+INTERFACE=`echo $BSDDIALOG_ITEMS | xargs -o bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' --menu 'Please select a network interface to configure:' 0 0 0 2>&1 1>&5`
 if [ $? -eq $BSDDIALOG_CANCEL ]; then exit 1; fi
-exec 3>&-
+exec 5>&-
 
 : > $BSDINSTALL_TMPETC/._rc.conf.net
 
@@ -182,13 +182,13 @@ else
 	exit 0
 fi
 
-exec 3>&1
+exec 5>&1
 RESOLV=$(echo "${RESOLV}" | xargs -o bsddialog --backtitle "$OSNAME Installer" \
 	--title 'Network Configuration' \
 	--mixedform 'Resolver Configuration' 0 0 0 \
-2>&1 1>&3)
+2>&1 1>&5)
 if [ $? -eq $BSDDIALOG_CANCEL ]; then exec $0; fi
-exec 3>&-
+exec 5>&-
 
 echo ${RESOLV} | tr ' ' '\n' | \
 awk '
diff --git a/usr.sbin/bsdinstall/scripts/netconfig_ipv4 b/usr.sbin/bsdinstall/scripts/netconfig_ipv4
index 7be4a59194c5..3da1803cd0d1 100755
--- a/usr.sbin/bsdinstall/scripts/netconfig_ipv4
+++ b/usr.sbin/bsdinstall/scripts/netconfig_ipv4
@@ -71,14 +71,14 @@ IP_ADDRESS=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $2); }'`
 NETMASK=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $4); }'`
 ROUTER=`netstat -rn -f inet | awk '/default/ {printf("%s\n", $2);}'`
 
-exec 3>&1
+exec 5>&1
 IF_CONFIG=$(bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' --form 'Static Network Interface Configuration' 0 0 0 \
 	'IP Address' 1 1 "$IP_ADDRESS" 1 20 16 0 \
 	'Subnet Mask' 2 1 "$NETMASK" 2 20 16 0 \
 	'Default Router' 3 1 "$ROUTER" 3 20 16 0 \
-2>&1 1>&3)
+2>&1 1>&5)
 if [ $? -eq $BSDDIALOG_CANCEL ]; then exit 1; fi
-exec 3>&-
+exec 5>&-
 
 echo $INTERFACE $IF_CONFIG |
     awk -v prefix="$IFCONFIG_PREFIX" '{
diff --git a/usr.sbin/bsdinstall/scripts/netconfig_ipv6 b/usr.sbin/bsdinstall/scripts/netconfig_ipv6
index f2d864c539c4..00ef8791de4b 100755
--- a/usr.sbin/bsdinstall/scripts/netconfig_ipv6
+++ b/usr.sbin/bsdinstall/scripts/netconfig_ipv6
@@ -107,13 +107,13 @@ END {
 	printf "\"Default Router\" %d 1 \"%s\" %d 16 50 50 0 ", n, dfr, n;
 }'`
 
-exec 3>&1
+exec 5>&1
 IF_CONFIG=$(echo ${ADDRS} | xargs -o bsddialog --backtitle "$OSNAME Installer" \
 	--title 'Network Configuration' \
 	--mixedform 'Static IPv6 Network Interface Configuration' 0 0 0 \
-2>&1 1>&3)
+2>&1 1>&5)
 if [ $? -eq $BSDDIALOG_CANCEL ]; then exit 1; fi
-exec 3>&-
+exec 5>&-
 
 echo ${IF_CONFIG} | tr ' ' '\n' | \
 awk -v iface="${INTERFACE}" '
diff --git a/usr.sbin/bsdinstall/scripts/script b/usr.sbin/bsdinstall/scripts/script
index 1c617835c274..ae1c6b3011fa 100755
--- a/usr.sbin/bsdinstall/scripts/script
+++ b/usr.sbin/bsdinstall/scripts/script
@@ -114,13 +114,12 @@ fi
 bsdinstall mount
 
 # Fetch missing distribution files, if any
-(
-	exec 3>&1
-	export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&3)
-	FETCH_RESULT=$?
-	exec 3>&-
-	return $FETCH_RESULT
-) || error "Could not fetch remote distributions"
+exec 5>&1
+export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&5)
+FETCH_RESULT=$?
+exec 5>&-
+
+[ $FETCH_RESULT -ne 0 ] && error "Could not fetch remote distributions"
 
 # Unpack distributions
 bsdinstall checksum
diff --git a/usr.sbin/bsdinstall/scripts/services b/usr.sbin/bsdinstall/scripts/services
index 025aaa444cd1..93282effbb3f 100755
--- a/usr.sbin/bsdinstall/scripts/services
+++ b/usr.sbin/bsdinstall/scripts/services
@@ -40,7 +40,7 @@ fi
 
 echo -n > $BSDINSTALL_TMPETC/rc.conf.services
 
-exec 3>&1
+exec 5>&1
 DAEMONS=$( bsddialog --backtitle "$OSNAME Installer" \
     --title "System Configuration" --no-cancel --separate-output \
     --checklist "Choose the services you would like to be started at boot:" \
@@ -55,9 +55,9 @@ DAEMONS=$( bsddialog --backtitle "$OSNAME Installer" \
 	powerd	"Adjust CPU frequency dynamically if supported" \
 		${powerd_enable:-off} \
 	dumpdev "Enable kernel crash dumps to /var/crash" ${dumpdev:-on} \
-2>&1 1>&3 )
+2>&1 1>&5 )
 retval=$?
-exec 3>&-
+exec 5>&-
 
 if [ $retval -ne $BSDDIALOG_OK ]; then
 	exit 1
diff --git a/usr.sbin/bsdinstall/scripts/time b/usr.sbin/bsdinstall/scripts/time
index bba58d291cbd..1d66fa4d5fcf 100755
--- a/usr.sbin/bsdinstall/scripts/time
+++ b/usr.sbin/bsdinstall/scripts/time
@@ -37,7 +37,7 @@ TZ="${BSDINSTALL_CHROOT}/etc/localtime"
 export TZ
 
 # Set date
-exec 3>&1
+exec 5>&1
 DATE=$(bsddialog --backtitle "$OSNAME Installer" \
 	--title 'Time & Date' \
 	--ok-label 'Set Date' \
@@ -45,11 +45,11 @@ DATE=$(bsddialog --backtitle "$OSNAME Installer" \
 	--default-no \
 	--date-format '%Y%m%d%H%M.%S' \
 	--datebox '' 0 40 \
-2>&1 1>&3) && date $DATE
-exec 3>&-
+2>&1 1>&5) && date $DATE
+exec 5>&-
 
 # Set time
-exec 3>&1
+exec 5>&1
 TIME=$(bsddialog --backtitle "$OSNAME Installer" \
 	--title 'Time & Date' \
 	--ok-label 'Set Time' \
@@ -57,8 +57,8 @@ TIME=$(bsddialog --backtitle "$OSNAME Installer" \
 	--default-no \
 	--time-format '%H%M.%S' \
 	--timebox '' 0 40 \
-2>&1 1>&3) && date $TIME
-exec 3>&-
+2>&1 1>&5) && date $TIME
+exec 5>&-
 
 # Switch back
 if [ -n "$saved_TZ" ]; then