Improve pkexec fallback

This commit is contained in:
the-sane 2024-09-22 14:25:47 -04:00
parent df7d8e1dff
commit b10ac91952

View File

@ -258,33 +258,35 @@ try_exec() {
exit 0 exit 0
fi fi
retval=0
# Use pollkit's pkexec for gui authentication with a fallback to sudo # Use pollkit's pkexec for gui authentication with a fallback to sudo
if [ -x "$(command -v pkexec)" ]; then if [ -x "$(command -v pkexec)" ]; then
pkexec sh -c "$1" pkexec sh -c "$1"
# Check the exit status # Check the exit status
statuscode="$?" if [ "$?" -eq 126 ] || [ "$?" -eq 127 ]; then
if [ "$statuscode" -eq 126 ] || [ "$statuscode" -eq 127 ]; then
# User cancel or error # User cancel or error
retval=1 debug_print continue "pkexec returned an error. Falling back to sudo..."
else
# Successful execution, return here
return 0
fi fi
elif [ -x "$(command -v sudo)" ]; then fi
# Fall back to sudo if pkexec is unavailable or returned an error
if [ -x "$(command -v sudo)" ]; then
sudo sh -c "$1" sudo sh -c "$1"
# Check the exit status # Check the exit status
statuscode="$?" if [ "$?" -eq 1 ]; then
if [ "$statuscode" -eq 1 ]; then
# Error # Error
retval=1 return 1
fi fi
else else
# We don't know how to perform this operation with elevated privileges # We don't know how to perform this operation with elevated privileges
printf "\nNeither Polkit nor sudo appear to be installed. Unable to execute the command with the required privileges.\n" printf "\nNeither Polkit nor sudo appear to be installed. Unable to execute the command with the required privileges.\n"
retval=1 return 1
fi fi
return "$retval" return 0
} }
# Echo a formatted debug message to the terminal and optionally exit # Echo a formatted debug message to the terminal and optionally exit