1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2024-10-06 02:41:11 +01:00

common/linux/device.py: don't require sudo if already root user

On generic_linux devices, one might ssh as the root user, in which
case there is no need to use sudo.

In addition, some root filesystems may not have sudo (e.g. minimal
buildroot/busybox).

This patch attempts to detect the root user using 'id -u' and, if it
detects the root user, avoids the use of sudo for running commands as
well.

Cc: Lisa Nguyen <lisa.nguyen@linaro.org>
Signed-off-by: Kevin Hilman <khilman@linaro.org>
(setrofim: modified to only test once)
This commit is contained in:
Kevin Hilman 2015-03-20 22:56:17 +00:00 committed by Sergei Trofimov
parent d4ee737e50
commit bf12d18457

View File

@ -136,6 +136,7 @@ class BaseLinuxDevice(Device): # pylint: disable=abstract-method
self._is_ready = False self._is_ready = False
self._just_rebooted = False self._just_rebooted = False
self._is_rooted = None self._is_rooted = None
self._is_root_user = False
self._available_frequencies = {} self._available_frequencies = {}
self._available_governors = {} self._available_governors = {}
self._available_governor_tunables = {} self._available_governor_tunables = {}
@ -757,6 +758,16 @@ class LinuxDevice(BaseLinuxDevice):
@property @property
def is_rooted(self): def is_rooted(self):
if self._is_rooted is None: if self._is_rooted is None:
# First check if the user is root
try:
self.execute('test $(id -u) = 0')
self._is_root_user = True
self._is_rooted = True
return self._is_rooted
except DeviceError:
self._is_root_user = False
# Otherwise, check if the user has sudo rights
try: try:
self.execute('ls /', as_root=True) self.execute('ls /', as_root=True)
self._is_rooted = True self._is_rooted = True
@ -852,6 +863,9 @@ class LinuxDevice(BaseLinuxDevice):
raise DeviceError('Cannot execute in background with as_root=True unless user is root.') raise DeviceError('Cannot execute in background with as_root=True unless user is root.')
return self.shell.background(command) return self.shell.background(command)
else: else:
# If we're already the root user, don't bother with sudo
if self._is_root_user:
as_root = False
return self.shell.execute(command, timeout, check_exit_code, as_root, strip_colors) return self.shell.execute(command, timeout, check_exit_code, as_root, strip_colors)
def kick_off(self, command): def kick_off(self, command):