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