Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 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 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 5 | #include "update_engine/policy_manager/real_shill_provider.h" |
| 6 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame^] | 10 | #include <base/strings/stringprintf.h> |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
| 12 | |
| 13 | #include "update_engine/policy_manager/generic_variables.h" |
| 14 | #include "update_engine/utils.h" |
| 15 | |
| 16 | using std::string; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // Looks up a key in a hash table and returns the string inside of the returned |
| 21 | // GValue. |
| 22 | const char* GetStrProperty(GHashTable* hash_table, const char* key) { |
| 23 | auto gval = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table, key)); |
| 24 | return (gval ? g_value_get_string(gval) : NULL); |
| 25 | } |
| 26 | |
| 27 | }; // namespace |
| 28 | |
| 29 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 30 | namespace chromeos_policy_manager { |
| 31 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 32 | // ShillConnector methods. |
| 33 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 34 | const ShillConnector::ConnStrToType ShillConnector::shill_conn_str_to_type[] = { |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 35 | {shill::kTypeEthernet, ConnectionType::kEthernet}, |
| 36 | {shill::kTypeWifi, ConnectionType::kWifi}, |
| 37 | {shill::kTypeWimax, ConnectionType::kWimax}, |
| 38 | {shill::kTypeBluetooth, ConnectionType::kBluetooth}, |
| 39 | {shill::kTypeCellular, ConnectionType::kCellular}, |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 40 | }; |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 41 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 42 | ShillConnector::~ShillConnector() { |
| 43 | if (!is_init_) |
| 44 | return; |
| 45 | |
| 46 | // Detach signal handler, free manager proxy. |
| 47 | dbus_->ProxyDisconnectSignal(manager_proxy_, shill::kMonitorPropertyChanged, |
| 48 | G_CALLBACK(signal_handler_), signal_data_); |
| 49 | dbus_->ProxyUnref(manager_proxy_); |
| 50 | } |
| 51 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 52 | bool ShillConnector::Init() { |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 53 | if (is_init_) |
| 54 | return true; |
| 55 | |
| 56 | // Obtain a DBus connection. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 57 | GError* error = NULL; |
| 58 | connection_ = dbus_->BusGet(DBUS_BUS_SYSTEM, &error); |
| 59 | if (!connection_) { |
| 60 | LOG(ERROR) << "Failed to initialize DBus connection: " |
| 61 | << chromeos_update_engine::utils::GetAndFreeGError(&error); |
| 62 | return false; |
| 63 | } |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 64 | |
| 65 | // Allocate a shill manager proxy. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 66 | manager_proxy_ = GetProxy(shill::kFlimflamServicePath, |
| 67 | shill::kFlimflamManagerInterface); |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 68 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 69 | // Subscribe to the manager's PropertyChanged signal. |
| 70 | dbus_->ProxyAddSignal_2(manager_proxy_, shill::kMonitorPropertyChanged, |
| 71 | G_TYPE_STRING, G_TYPE_VALUE); |
| 72 | dbus_->ProxyConnectSignal(manager_proxy_, shill::kMonitorPropertyChanged, |
| 73 | G_CALLBACK(signal_handler_), signal_data_, NULL); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 74 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 75 | return is_init_ = true; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool ShillConnector::GetConnectionType(const string& service_path, |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 79 | ConnectionType* conn_type_p) { |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 80 | // Obtain a proxy for the service path. |
| 81 | DBusGProxy* service_proxy = GetProxy(service_path.c_str(), |
| 82 | shill::kFlimflamServiceInterface); |
| 83 | |
| 84 | GHashTable* hash_table = NULL; |
| 85 | bool success = false; |
| 86 | bool is_vpn = false; |
| 87 | if (GetProperties(service_proxy, &hash_table)) { |
| 88 | const char* type_str = GetStrProperty(hash_table, shill::kTypeProperty); |
| 89 | if (type_str && !strcmp(type_str, shill::kTypeVPN)) { |
| 90 | is_vpn = true; |
| 91 | type_str = GetStrProperty(hash_table, shill::kPhysicalTechnologyProperty); |
| 92 | } |
| 93 | if (type_str) { |
| 94 | success = true; |
| 95 | *conn_type_p = ParseConnType(type_str); |
| 96 | } |
| 97 | g_hash_table_unref(hash_table); |
| 98 | } |
| 99 | |
| 100 | if (!success) { |
| 101 | LOG(ERROR) << "Could not find type of " |
| 102 | << (is_vpn ? "physical connection underlying VPN " : "") |
| 103 | << "connection (" << service_path << ")"; |
| 104 | } |
| 105 | |
| 106 | dbus_->ProxyUnref(service_proxy); |
| 107 | return success; |
| 108 | } |
| 109 | |
| 110 | DBusGProxy* ShillConnector::GetProxy(const char* path, const char* interface) { |
| 111 | return dbus_->ProxyNewForName(connection_, shill::kFlimflamServiceName, |
| 112 | path, interface); |
| 113 | } |
| 114 | |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 115 | ConnectionType ShillConnector::ParseConnType(const char* str) { |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 116 | for (unsigned i = 0; i < arraysize(shill_conn_str_to_type); i++) |
| 117 | if (!strcmp(str, shill_conn_str_to_type[i].str)) |
| 118 | return shill_conn_str_to_type[i].type; |
| 119 | |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 120 | return ConnectionType::kUnknown; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 123 | bool ShillConnector::GetProperties(DBusGProxy* proxy, GHashTable** result_p) { |
| 124 | GError* error = NULL; |
| 125 | if (!dbus_->ProxyCall_0_1(proxy, shill::kGetPropertiesFunction, &error, |
| 126 | result_p)) { |
| 127 | LOG(ERROR) << "Calling shill via DBus proxy failed: " |
| 128 | << chromeos_update_engine::utils::GetAndFreeGError(&error); |
| 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 134 | |
| 135 | // A variable returning the curent connection type. |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 136 | class ConnTypeVariable : public Variable<ConnectionType> { |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 137 | public: |
| 138 | ConnTypeVariable(const string& name, ShillConnector* connector, |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 139 | RealShillProvider* provider) |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 140 | : Variable<ConnectionType>(name, kVariableModePoll), |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 141 | connector_(connector), provider_(provider) {} |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 142 | |
| 143 | protected: |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 144 | // TODO(garnold) Shift to a non-blocking version, respect the timeout. |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 145 | virtual const ConnectionType* GetValue(base::TimeDelta /* timeout */, |
| 146 | string* errmsg) { |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 147 | if (!(provider_->is_connected_)) { |
| 148 | if (errmsg) |
| 149 | *errmsg = "No connection detected"; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 150 | return NULL; |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 151 | } |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 152 | |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 153 | ConnectionType conn_type; |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 154 | if (provider_->is_conn_type_valid_) { |
| 155 | conn_type = provider_->conn_type_; |
| 156 | } else { |
| 157 | if (!connector_->GetConnectionType(provider_->default_service_path_, |
| 158 | &conn_type)) { |
| 159 | if (errmsg) |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame^] | 160 | *errmsg = base::StringPrintf( |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 161 | "Could not retrieve type of default connection (%s)", |
| 162 | provider_->default_service_path_.c_str()); |
| 163 | return NULL; |
| 164 | } |
| 165 | provider_->is_conn_type_valid_ = true; |
| 166 | } |
| 167 | |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 168 | return new ConnectionType(conn_type); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | private: |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 172 | // The DBus connector. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 173 | ShillConnector* connector_; |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 174 | |
| 175 | // The shill provider object (we need to read/update some internal members). |
| 176 | RealShillProvider* provider_; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 177 | |
| 178 | DISALLOW_COPY_AND_ASSIGN(ConnTypeVariable); |
| 179 | }; |
| 180 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 181 | |
| 182 | // RealShillProvider methods. |
| 183 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 184 | bool RealShillProvider::DoInit() { |
| 185 | // Initialize a DBus connection and obtain the shill manager proxy. |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 186 | connector_.reset(new ShillConnector(dbus_, HandlePropertyChangedStatic, |
| 187 | this)); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 188 | if (!connector_->Init()) |
| 189 | return false; |
| 190 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 191 | // Read initial connection status. |
| 192 | GHashTable* hash_table = NULL; |
| 193 | if (!connector_->GetManagerProperties(&hash_table)) |
| 194 | return false; |
| 195 | GValue* value = reinterpret_cast<GValue*>( |
| 196 | g_hash_table_lookup(hash_table, shill::kDefaultServiceProperty)); |
| 197 | bool success = ProcessDefaultService(value); |
| 198 | g_hash_table_unref(hash_table); |
| 199 | if (!success) |
| 200 | return false; |
| 201 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 202 | // Initialize variables. |
Alex Deymo | 540d942 | 2014-02-27 11:17:31 -0800 | [diff] [blame] | 203 | set_var_is_connected( |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 204 | new CopyVariable<bool>("is_connected", kVariableModePoll, is_connected_)); |
Alex Deymo | 540d942 | 2014-02-27 11:17:31 -0800 | [diff] [blame] | 205 | set_var_conn_type( |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 206 | new ConnTypeVariable("conn_type", connector_.get(), this)); |
Alex Deymo | 540d942 | 2014-02-27 11:17:31 -0800 | [diff] [blame] | 207 | set_var_conn_last_changed( |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 208 | new CopyVariable<base::Time>("conn_last_changed", kVariableModePoll, |
| 209 | conn_last_changed_)); |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 213 | bool RealShillProvider::ProcessDefaultService(GValue* value) { |
| 214 | // Decode the string from the boxed value. |
| 215 | const char* default_service_path_str = NULL; |
| 216 | if (!(value && (default_service_path_str = g_value_get_string(value)))) |
| 217 | return false; |
| 218 | |
| 219 | // Update the connection status. |
| 220 | is_connected_ = strcmp(default_service_path_str, "/"); |
| 221 | if (default_service_path_ != default_service_path_str) |
| 222 | conn_last_changed_ = clock_->GetWallclockTime(); |
| 223 | default_service_path_ = default_service_path_str; |
| 224 | is_conn_type_valid_ = false; |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | void RealShillProvider::HandlePropertyChanged(DBusGProxy* proxy, |
| 229 | const char* name, GValue* value) { |
| 230 | if (!strcmp(name, shill::kDefaultServiceProperty)) |
| 231 | ProcessDefaultService(value); |
| 232 | } |
| 233 | |
| 234 | void RealShillProvider::HandlePropertyChangedStatic(DBusGProxy* proxy, |
| 235 | const char* name, |
| 236 | GValue* value, |
| 237 | void* data) { |
| 238 | auto obj = reinterpret_cast<RealShillProvider*>(data); |
| 239 | obj->HandlePropertyChanged(proxy, name, value); |
| 240 | } |
| 241 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 242 | } // namespace chromeos_policy_manager |