From 03611dc63e3e44d0504f0d05c3129fbc0b2d1510 Mon Sep 17 00:00:00 2001 From: Sébastien Luttringer Date: Wed, 8 Jan 2014 01:15:37 +0100 Subject: Switch to root when started as regular user In collaborative builder machine, these scripts are often allowed to become root via sudo. This patch avoid to prefix them by sudo each time or call su. Signed-off-by: Pierre Schmitz --- lib/common.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib') diff --git a/lib/common.sh b/lib/common.sh index c9ff1b8..b885080 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.' +} -- cgit v1.2.3-70-g09d2