mirror of
				https://github.com/ARM-software/devlib.git
				synced 2025-11-03 23:41:21 +00:00 
			
		
		
		
	AdbConnection: Add adb rooting to the connection to allow tracking
Add a method to `AdbConnection` to control whether whether adb is connected as root. This allows for the connection to track whether it is connected as root for a particular device across all instances of a connection.
This commit is contained in:
		@@ -237,6 +237,8 @@ class AdbConnection(object):
 | 
				
			|||||||
    # maintains the count of parallel active connections to a device, so that
 | 
					    # maintains the count of parallel active connections to a device, so that
 | 
				
			||||||
    # adb disconnect is not invoked untill all connections are closed
 | 
					    # adb disconnect is not invoked untill all connections are closed
 | 
				
			||||||
    active_connections = defaultdict(int)
 | 
					    active_connections = defaultdict(int)
 | 
				
			||||||
 | 
					    # Track connected as root status per device
 | 
				
			||||||
 | 
					    _connected_as_root = defaultdict(lambda: None)
 | 
				
			||||||
    default_timeout = 10
 | 
					    default_timeout = 10
 | 
				
			||||||
    ls_command = 'ls'
 | 
					    ls_command = 'ls'
 | 
				
			||||||
    su_cmd = 'su -c {}'
 | 
					    su_cmd = 'su -c {}'
 | 
				
			||||||
@@ -245,6 +247,18 @@ class AdbConnection(object):
 | 
				
			|||||||
    def name(self):
 | 
					    def name(self):
 | 
				
			||||||
        return self.device
 | 
					        return self.device
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @property
 | 
				
			||||||
 | 
					    def connected_as_root(self):
 | 
				
			||||||
 | 
					        if self._connected_as_root[self.device] is None:
 | 
				
			||||||
 | 
					                result = self.execute('id')
 | 
				
			||||||
 | 
					                self._connected_as_root[self.device] = 'uid=0(' in result
 | 
				
			||||||
 | 
					        return self._connected_as_root[self.device]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @connected_as_root.setter
 | 
				
			||||||
 | 
					    def connected_as_root(self, state):
 | 
				
			||||||
 | 
					        self._connected_as_root[self.device] = state
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # pylint: disable=unused-argument
 | 
					    # pylint: disable=unused-argument
 | 
				
			||||||
    def __init__(self, device=None, timeout=None, platform=None, adb_server=None):
 | 
					    def __init__(self, device=None, timeout=None, platform=None, adb_server=None):
 | 
				
			||||||
        self.timeout = timeout if timeout is not None else self.default_timeout
 | 
					        self.timeout = timeout if timeout is not None else self.default_timeout
 | 
				
			||||||
@@ -307,6 +321,13 @@ class AdbConnection(object):
 | 
				
			|||||||
        # before the next one can be issued.
 | 
					        # before the next one can be issued.
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def adb_root(self, enable=True):
 | 
				
			||||||
 | 
					        cmd = 'root' if enable else 'unroot'
 | 
				
			||||||
 | 
					        output = adb_command(self.device, cmd, timeout=30)
 | 
				
			||||||
 | 
					        if 'cannot run as root in production builds' in output:
 | 
				
			||||||
 | 
					            raise TargetStableError(output)
 | 
				
			||||||
 | 
					        AdbConnection._connected_as_root[self.device] = enable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Again, we need to handle boards where the default output format from ls is
 | 
					    # Again, we need to handle boards where the default output format from ls is
 | 
				
			||||||
    # single column *and* boards where the default output is multi-column.
 | 
					    # single column *and* boards where the default output is multi-column.
 | 
				
			||||||
    # We need to do this purely because the '-1' option causes errors on older
 | 
					    # We need to do this purely because the '-1' option causes errors on older
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user