git: 66c75fa63aff - main - freebsd-update: Fix the pkgbase check

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 08 Aug 2025 13:00:45 UTC
The branch main has been updated by markj:

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

commit 66c75fa63aff40e9c587345b2cc6b8148e396de8
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-08-06 20:36:05 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-08-08 12:51:54 +0000

    freebsd-update: Fix the pkgbase check
    
    Even on a pkgbase system, it should be possible to use freebsd-update -j
    to upgrade a non-pkgbase jail, at least for the time being.  However,
    the check_pkgbase() call came before get_params, so BASEDIR was always
    set to /.
    
    Make check_pkgbase() a pure function and call it after get_params().
    While here, use pkg -r ${BASEDIR} instead of pkg -c ${BASEDIR} since the
    latter requires root privileges.  freebsd-update is supposed to be run
    as root, but it doesn't actually check this that I can see, so let's not
    make that assumption here since it affects the result of the function
    (i.e., pkg -c ${BASEDIR} always fails as a non-root user).
    
    Reviewed by:    des
    Fixes:          856e158dc4aa ("freebsd-update: improve pkgbase check")
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D51770
---
 usr.sbin/freebsd-update/freebsd-update.sh | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/usr.sbin/freebsd-update/freebsd-update.sh b/usr.sbin/freebsd-update/freebsd-update.sh
index ccd98a883dca..c388e76644dc 100644
--- a/usr.sbin/freebsd-update/freebsd-update.sh
+++ b/usr.sbin/freebsd-update/freebsd-update.sh
@@ -1099,24 +1099,19 @@ IDS_check_params () {
 	fetch_setup_verboselevel
 }
 
-# Packaged base and freebsd-update are incompatible.  Exit with an error if
-# packaged base is in use.
+# Return 0 if the system is managed using pkgbase, 1 otherwise.
 check_pkgbase()
 {
 	# Packaged base requires that pkg is bootstrapped.
-	if ! pkg -c ${BASEDIR} -N >/dev/null 2>/dev/null; then
-		return
+	if ! pkg -r ${BASEDIR} -N >/dev/null 2>/dev/null; then
+		return 1
 	fi
 	# uname(1) is used by pkg to determine ABI, so it should exist.
 	# If it comes from a package then this system uses packaged base.
-	if ! pkg -c ${BASEDIR} which /usr/bin/uname >/dev/null; then
-		return
+	if ! pkg -r ${BASEDIR} which /usr/bin/uname >/dev/null; then
+		return 1
 	fi
-	cat <<EOF
-freebsd-update is incompatible with the use of packaged base.  Please see
-https://wiki.freebsd.org/PkgBase for more information.
-EOF
-	exit 1
+	return 0
 }
 
 #### Core functionality -- the actual work gets done here
@@ -3615,10 +3610,18 @@ export LC_ALL=C
 # Clear environment variables that may affect operation of tools that we use.
 unset GREP_OPTIONS
 
+# Parse command line options and the configuration file.
+get_params "$@"
+
 # Disallow use with packaged base.
-check_pkgbase
+if check_pkgbase; then
+	cat <<EOF
+freebsd-update is incompatible with the use of packaged base.  Please see
+https://wiki.freebsd.org/PkgBase for more information.
+EOF
+	exit 1
+fi
 
-get_params $@
 for COMMAND in ${COMMANDS}; do
 	cmd_${COMMAND}
 done