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:		856e158dc4 ("freebsd-update: improve pkgbase check")
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D51770
This commit is contained in:
Mark Johnston 2025-08-06 20:36:05 +00:00
parent f4744b8acb
commit 66c75fa63a

View file

@ -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
# Disallow use with packaged base.
check_pkgbase
# Parse command line options and the configuration file.
get_params "$@"
# Disallow use with packaged base.
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