Handle errors in pkexec and sudo

This commit is contained in:
the-sane 2022-10-31 14:54:17 -04:00
parent 677a789ab5
commit 8f86d37b88

View File

@ -224,6 +224,39 @@ debug_print() {
esac esac
} }
# Try to execute a supplied command as root
# Expects one string argument
try_exec() {
# This function expects one string arguments
if [ "$#" -lt 1 ]; then
printf "\nScript error: The try_exec() function expects an argument. Aborting.\n"
read -n 1 -s -p "Press any key..."
exit 0
fi
retval=0
# Use pollkit's pkexec for gui authentication with a fallback to sudo
if [ -x "$(command -v pkexec)" ]; then
pkexec sh -c "$1"
# Check the return value
if [ "$?" -eq "126" ] || [ "$?" -eq "127" ]; then
# User cancel or error
retval=1
fi
else
sudo sh -c "$1"
# Check the return value
if [ "$?" -eq "1" ]; then
# Error
retval=1
fi
fi
return "$retval"
}
# Display a message to the user. # Display a message to the user.
# Expects the first argument to indicate the message type, followed by # Expects the first argument to indicate the message type, followed by
# a string of arguments that will be passed to zenity or echoed to the user. # a string of arguments that will be passed to zenity or echoed to the user.
@ -695,11 +728,11 @@ mapcount_set() {
if [ -d "/etc/sysctl.d" ]; then if [ -d "/etc/sysctl.d" ]; then
# Newer versions of sysctl # Newer versions of sysctl
preflight_actions+=('printf "\n# Added by LUG-Helper:\nvm.max_map_count = 16777216\n" > /etc/sysctl.d/20-starcitizen-max_map_count.conf && sysctl --system') preflight_actions+=('printf "\n# Added by LUG-Helper:\nvm.max_map_count = 16777216\n" > /etc/sysctl.d/20-starcitizen-max_map_count.conf && sysctl --system')
preflight_results+=("The vm.max_map_count configuration has been appended to:\n/etc/sysctl.d/20-starcitizen-max_map_count.conf") preflight_results+=("The vm.max_map_count configuration has been added to:\n/etc/sysctl.d/20-starcitizen-max_map_count.conf")
else else
# Older versions of sysctl # Older versions of sysctl
preflight_actions+=('printf "\n# Added by LUG-Helper:\nvm.max_map_count = 16777216" >> /etc/sysctl.conf && sysctl -p') preflight_actions+=('printf "\n# Added by LUG-Helper:\nvm.max_map_count = 16777216" >> /etc/sysctl.conf && sysctl -p')
preflight_results+=("The vm.max_map_count configuration has been appended to:\n/etc/sysctl.conf") preflight_results+=("The vm.max_map_count configuration has been added to:\n/etc/sysctl.conf")
fi fi
# Verify that the setting took effect # Verify that the setting took effect
@ -919,11 +952,11 @@ preflight_check() {
# Execute the actions set by the functions # Execute the actions set by the functions
if [ ! -z "$preflight_actions_string" ]; then if [ ! -z "$preflight_actions_string" ]; then
# Use pollkit's pkexec for gui with a fallback to sudo # Try to execute the actions as root
if [ -x "$(command -v pkexec)" ]; then try_exec "$preflight_actions_string"
pkexec sh -c "$preflight_actions_string" if [ "$?" -eq 1 ]; then
else message info "Authentication failed or there was an error.\nSee terminal for more information.\n\nReturning to main menu."
sudo sh -c "$preflight_actions_string" return 0
fi fi
fi fi
@ -1705,12 +1738,12 @@ eac_workaround() {
if message question "$eac_title\n\nThe following entry will be added to /etc/hosts:\n$eac_hosts_formatted\n\nThe following directory will be deleted:\n$eac_dir_formatted\n\n\nTo revert these changes, delete the above line from\n/etc/hosts and relaunch the game\n\nDo you want to proceed?"; then if message question "$eac_title\n\nThe following entry will be added to /etc/hosts:\n$eac_hosts_formatted\n\nThe following directory will be deleted:\n$eac_dir_formatted\n\n\nTo revert these changes, delete the above line from\n/etc/hosts and relaunch the game\n\nDo you want to proceed?"; then
debug_print continue "Editing hosts file..." debug_print continue "Editing hosts file..."
# Use pollkit's pkexec for gui with a fallback to sudo # Try to modify /etc/hosts as root
if [ -x "$(command -v pkexec)" ]; then try_exec "printf '\n$eac_hosts #Star Citizen EAC workaround\n' >> /etc/hosts"
pkexec sh -c "echo $eac_hosts '#Star Citizen EAC workaround' >> /etc/hosts" if [ "$?" -eq 1 ]; then
else message info "Something went wrong. Unable to modify /etc/hosts.\n\nReturning to main menu."
sudo sh -c "echo $eac_hosts '#Star Citizen EAC workaround' >> /etc/hosts" return 0
fi fi
# Delete the EAC directory if it exists # Delete the EAC directory if it exists