Skip to content

Commit 37eef46

Browse files
committed
android/adb: Enable fall back for su command
Commit 89c40fb switched from using `echo CMD | su` to `su -c CMD` however not all modern versions of `su` support this format. Automatically try and detect if the new style is supported when connecting and if not, fall back to the old implementation.
1 parent 88f8c9e commit 37eef46

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

devlib/utils/android.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class AdbConnection(object):
239239
active_connections = defaultdict(int)
240240
default_timeout = 10
241241
ls_command = 'ls'
242+
su_cmd = 'su -c {}'
242243

243244
@property
244245
def name(self):
@@ -264,6 +265,7 @@ def _setup_ls(self):
264265
self.ls_command = 'ls'
265266
logger.debug("ls command is set to {}".format(self.ls_command))
266267

268+
267269
# pylint: disable=unused-argument
268270
def __init__(self, device=None, timeout=None, platform=None, adb_server=None):
269271
self.timeout = timeout if timeout is not None else self.default_timeout
@@ -274,6 +276,7 @@ def __init__(self, device=None, timeout=None, platform=None, adb_server=None):
274276
adb_connect(self.device)
275277
AdbConnection.active_connections[self.device] += 1
276278
self._setup_ls()
279+
self._setup_su()
277280

278281
def push(self, source, dest, timeout=None):
279282
if timeout is None:
@@ -303,7 +306,7 @@ def execute(self, command, timeout=None, check_exit_code=False,
303306
as_root=False, strip_colors=True, will_succeed=False):
304307
try:
305308
return adb_shell(self.device, command, timeout, check_exit_code,
306-
as_root, adb_server=self.adb_server)
309+
as_root, adb_server=self.adb_server, su_cmd=self.su_cmd)
307310
except TargetStableError as e:
308311
if will_succeed:
309312
raise TargetTransientError(e)
@@ -326,6 +329,18 @@ def cancel_running_command(self):
326329
pass
327330

328331

332+
def _setup_su(self):
333+
try:
334+
# Try the new style of invoking `su`
335+
self.execute('ls', timeout=self.timeout, as_root=True,
336+
check_exit_code=True)
337+
# If failure assume either old style or unrooted. Here we will assume
338+
# old style and root status will be verified later.
339+
except (TargetStableError, TargetTransientError, TimeoutError):
340+
self.su_cmd = 'echo {} | su'
341+
logger.debug("su command is set to {}".format(quote(self.su_cmd)))
342+
343+
329344
def fastboot_command(command, timeout=None, device=None):
330345
_check_env()
331346
target = '-s {}'.format(quote(device)) if device else ''
@@ -423,15 +438,15 @@ def _ping(device):
423438

424439
# pylint: disable=too-many-locals
425440
def adb_shell(device, command, timeout=None, check_exit_code=False,
426-
as_root=False, adb_server=None): # NOQA
441+
as_root=False, adb_server=None, su_cmd='su -c {}'): # NOQA
427442
_check_env()
428443
parts = ['adb']
429444
if adb_server is not None:
430445
parts += ['-H', adb_server]
431446
if device is not None:
432447
parts += ['-s', device]
433448
parts += ['shell',
434-
command if not as_root else 'su -c {}'.format(quote(command))]
449+
command if not as_root else su_cmd.format(quote(command))]
435450

436451
logger.debug(' '.join(quote(part) for part in parts))
437452
# On older combinations of ADB/Android versions, the adb host command always

0 commit comments

Comments
 (0)