diff --git a/arch-nspawn.in b/arch-nspawn.in index 6900382a1acc115c2cf5b6d93796c657588d5512..4436a0e32a9ab9bb6dd80e467b5e7503b4cbc348 100644 --- a/arch-nspawn.in +++ b/arch-nspawn.in @@ -37,8 +37,8 @@ while getopts 'hC:M:c:' arg; do done shift $(($OPTIND - 1)) -(( $EUID != 0 )) && die 'This script must be run as root.' (( $# < 1 )) && die 'You must specify a directory.' +check_root "$0" "$@" working_dir=$(readlink -f "$1") shift 1 diff --git a/archbuild.in b/archbuild.in index b1c96f97e430bbe39baa0b358134d77ad88442fd..ae2f511fcaa3a081511993b7fd3bc74e2ce1ff06 100644 --- a/archbuild.in +++ b/archbuild.in @@ -40,9 +40,7 @@ done # Pass all arguments after -- right to makepkg makechrootpkg_args+=("${@:$OPTIND}") -if (( EUID )); then - die 'This script must be run as root.' -fi +check_root "$0" "$@" if ${clean_first} || [[ ! -d "${chroots}/${repo}-${arch}" ]]; then msg "Creating chroot for [${repo}] (${arch})..." diff --git a/lib/common.sh b/lib/common.sh index c9ff1b80b9bb66d5b6703ec5631938ccf1884e4a..b885080e994a0a19a9c1a4ede5899af7060409a4 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -228,3 +228,16 @@ find_cached_package() { return 1 esac } + +## +# usage : check_root ("$0" "$@") +## +check_root() { + (( EUID == 0 )) && return + if type -P sudo >/dev/null; then + exec sudo -- "$@" + else + exec su root -c "$(printf '%q' "$@")" + fi + die 'This script must be run as root.' +} diff --git a/makechrootpkg.in b/makechrootpkg.in index 47c6ac9618a354e3c1cf7073794ad7786df73de2..97c7780822f0a31d359eb14a0dab343793f8c36b 100644 --- a/makechrootpkg.in +++ b/makechrootpkg.in @@ -81,7 +81,7 @@ while getopts 'hcur:I:l:nTD:d:' arg; do esac done -(( EUID != 0 )) && die 'This script must be run as root.' +check_root "$0" "$@" [[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.' diff --git a/mkarchroot.in b/mkarchroot.in index 7cdb27484c641adc43578b5431871c5c07ec3c7e..fb472bc0f827b52c982a950f030f884c65d1535b 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -35,9 +35,10 @@ while getopts 'hC:M:c:' arg; do done shift $(($OPTIND - 1)) -(( $EUID != 0 )) && die 'This script must be run as root.' (( $# < 2 )) && die 'You must specify a directory and one or more packages.' +check_root "$0" "$@" + working_dir="$(readlink -f $1)" shift 1