1# Copyright 2015 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import collections 6import dbus 7import logging 8import pipes 9import re 10import shlex 11 12from autotest_lib.client.bin import utils 13from autotest_lib.client.common_lib import error 14 15 16# Represents the result of a dbus-send call. |sender| refers to the temporary 17# bus name of dbus-send, |responder| to the remote process, and |response| 18# contains the parsed response. 19DBusSendResult = collections.namedtuple('DBusSendResult', ['sender', 20 'responder', 21 'response']) 22# Used internally. 23DictEntry = collections.namedtuple('DictEntry', ['key', 'value']) 24 25 26def _build_token_stream(headerless_dbus_send_output): 27 """A tokenizer for dbus-send output. 28 29 The output is basically just like splitting on whitespace, except that 30 strings are kept together by " characters. 31 32 @param headerless_dbus_send_output: list of lines of dbus-send output 33 without the meta-information prefix. 34 @return list of tokens in dbus-send output. 35 """ 36 return shlex.split(' '.join(headerless_dbus_send_output)) 37 38 39def _parse_value(token_stream): 40 """Turn a stream of tokens from dbus-send output into native python types. 41 42 @param token_stream: output from _build_token_stream() above. 43 44 """ 45 # Assumes properly tokenized output (strings with spaces handled). 46 # Assumes tokens are pre-stripped 47 token_type = token_stream.pop(0) 48 if token_type == 'variant': 49 token_type = token_stream.pop(0) 50 if token_type == 'object': 51 token_type = token_stream.pop(0) # Should be 'path' 52 token_value = token_stream.pop(0) 53 INT_TYPES = ('int16', 'uint16', 'int32', 'uint32', 54 'int64', 'uint64', 'byte') 55 if token_type in INT_TYPES: 56 return int(token_value) 57 if token_type == 'string' or token_type == 'path': 58 return token_value # shlex removed surrounding " chars. 59 if token_type == 'boolean': 60 return token_value == 'true' 61 if token_type == 'double': 62 return float(token_value) 63 if token_type == 'array': 64 values = [] 65 while token_stream[0] != ']': 66 values.append(_parse_value(token_stream)) 67 token_stream.pop(0) 68 if values and all([isinstance(x, DictEntry) for x in values]): 69 values = dict(values) 70 return values 71 if token_type == 'dict': 72 assert token_value == 'entry(' 73 key = _parse_value(token_stream) 74 value = _parse_value(token_stream) 75 assert token_stream.pop(0) == ')' 76 return DictEntry(key=key, value=value) 77 raise error.TestError('Unhandled DBus type found: %s' % token_type) 78 79 80def _parse_dbus_send_output(dbus_send_stdout): 81 """Turn dbus-send output into usable Python types. 82 83 This looks like: 84 85 localhost ~ # dbus-send --system --dest=org.chromium.flimflam \ 86 --print-reply --reply-timeout=2000 / \ 87 org.chromium.flimflam.Manager.GetProperties 88 method return time=1490931987.170070 sender=org.chromium.flimflam -> \ 89 destination=:1.37 serial=6 reply_serial=2 90 array [ 91 dict entry( 92 string "ActiveProfile" 93 variant string "/profile/default" 94 ) 95 dict entry( 96 string "ArpGateway" 97 variant boolean true 98 ) 99 ... 100 ] 101 102 @param dbus_send_output: string stdout from dbus-send 103 @return a DBusSendResult. 104 105 """ 106 lines = dbus_send_stdout.strip().splitlines() 107 # The first line contains meta-information about the response 108 header = lines[0] 109 lines = lines[1:] 110 dbus_address_pattern = r'[:\d\\.]+|[a-zA-Z.]+' 111 # The header may or may not have a time= field. 112 match = re.match(r'method return (time=[\d\\.]+ )?sender=(%s) -> ' 113 r'destination=(%s) serial=\d+ reply_serial=\d+' % 114 (dbus_address_pattern, dbus_address_pattern), header) 115 116 if match is None: 117 raise error.TestError('Could not parse dbus-send header: %s' % header) 118 119 sender = match.group(2) 120 responder = match.group(3) 121 token_stream = _build_token_stream(lines) 122 ret_val = _parse_value(token_stream) 123 # Note that DBus permits multiple response values, and this is not handled. 124 logging.debug('Got DBus response: %r', ret_val) 125 return DBusSendResult(sender=sender, responder=responder, response=ret_val) 126 127 128def _dbus2string(raw_arg): 129 """Turn a dbus.* type object into a string that dbus-send expects. 130 131 @param raw_dbus dbus.* type object to stringify. 132 @return string suitable for dbus-send. 133 134 """ 135 int_map = { 136 dbus.Int16: 'int16:', 137 dbus.Int32: 'int32:', 138 dbus.Int64: 'int64:', 139 dbus.UInt16: 'uint16:', 140 dbus.UInt32: 'uint32:', 141 dbus.UInt64: 'uint64:', 142 dbus.Double: 'double:', 143 dbus.Byte: 'byte:', 144 } 145 146 if isinstance(raw_arg, dbus.String): 147 return pipes.quote('string:%s' % raw_arg.replace('"', r'\"')) 148 149 if isinstance(raw_arg, dbus.Boolean): 150 if raw_arg: 151 return 'boolean:true' 152 else: 153 return 'boolean:false' 154 155 for prim_type, prefix in int_map.iteritems(): 156 if isinstance(raw_arg, prim_type): 157 return prefix + str(raw_arg) 158 159 raise error.TestError('No support for serializing %r' % raw_arg) 160 161 162def _build_arg_string(raw_args): 163 """Construct a string of arguments to a DBus method as dbus-send expects. 164 165 @param raw_args list of dbus.* type objects to seriallize. 166 @return string suitable for dbus-send. 167 168 """ 169 return ' '.join([_dbus2string(arg) for arg in raw_args]) 170 171 172def dbus_send(bus_name, interface, object_path, method_name, args=None, 173 host=None, timeout_seconds=2, tolerate_failures=False, user=None): 174 """Call dbus-send without arguments. 175 176 @param bus_name: string identifier of DBus connection to send a message to. 177 @param interface: string DBus interface of object to call method on. 178 @param object_path: string DBus path of remote object to call method on. 179 @param method_name: string name of method to call. 180 @param args: optional list of arguments. Arguments must be of types 181 from the python dbus module. 182 @param host: An optional host object if running against a remote host. 183 @param timeout_seconds: number of seconds to wait for a response. 184 @param tolerate_failures: boolean True to ignore problems receiving a 185 response. 186 @param user: An option argument to run dbus-send as a given user. 187 188 """ 189 run = utils.run if host is None else host.run 190 cmd = ('dbus-send --system --print-reply --reply-timeout=%d --dest=%s ' 191 '%s %s.%s' % (int(timeout_seconds * 1000), bus_name, 192 object_path, interface, method_name)) 193 194 if user is not None: 195 cmd = ('sudo -u %s %s' % (user, cmd)) 196 if args is not None: 197 cmd = cmd + ' ' + _build_arg_string(args) 198 result = run(cmd, ignore_status=tolerate_failures) 199 if result.exit_status != 0: 200 logging.debug('%r', result.stdout) 201 return None 202 return _parse_dbus_send_output(result.stdout) 203 204 205def get_property(bus_name, interface, object_path, property_name, host=None): 206 """A helpful wrapper that extracts the value of a DBus property. 207 208 @param bus_name: string identifier of DBus connection to send a message to. 209 @param interface: string DBus interface exposing the property. 210 @param object_path: string DBus path of remote object to call method on. 211 @param property_name: string name of property to get. 212 @param host: An optional host object if running against a remote host. 213 214 """ 215 return dbus_send(bus_name, dbus.PROPERTIES_IFACE, object_path, 'Get', 216 args=[dbus.String(interface), dbus.String(property_name)], 217 host=host) 218