Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <netinet/in.h> |
| 32 | #include <poll.h> |
| 33 | #include <stdatomic.h> |
| 34 | #include <stdint.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/select.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/uio.h> |
| 42 | #include <sys/un.h> |
| 43 | #include <unistd.h> |
| 44 | |
| 45 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 46 | #include <sys/_system_properties.h> |
| 47 | #include <sys/system_properties.h> |
| 48 | |
Tom Cherry | 8d366a8 | 2017-11-30 15:41:32 -0800 | [diff] [blame] | 49 | #include <new> |
| 50 | |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 51 | #include <async_safe/log.h> |
| 52 | |
| 53 | #include "private/ErrnoRestorer.h" |
| 54 | #include "private/bionic_defs.h" |
| 55 | #include "private/bionic_futex.h" |
| 56 | #include "private/bionic_macros.h" |
| 57 | #include "private/bionic_sdk_version.h" |
| 58 | |
| 59 | #include "context_node.h" |
| 60 | #include "contexts.h" |
| 61 | #include "contexts_pre_split.h" |
Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame^] | 62 | #include "contexts_serialized.h" |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 63 | #include "contexts_split.h" |
| 64 | #include "prop_area.h" |
| 65 | #include "prop_info.h" |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 66 | #include "property_filename.h" |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 67 | |
| 68 | // We don't want to use new or malloc in properties (b/31659220), and since these classes are |
Tom Cherry | 8d366a8 | 2017-11-30 15:41:32 -0800 | [diff] [blame] | 69 | // small enough and we place them in a static union. Note that system properties are initialized |
| 70 | // before static initializers are called, so using a Constructor here is an error. Even a |
| 71 | // Constructor that zero initializes a class will clobber the previous property initialization. |
| 72 | static union ContextsUnion { |
| 73 | ContextsUnion() {} |
| 74 | ~ContextsUnion() {} |
Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame^] | 75 | ContextsSerialized contexts_serialized; |
Tom Cherry | 8d366a8 | 2017-11-30 15:41:32 -0800 | [diff] [blame] | 76 | ContextsSplit contexts_split; |
| 77 | ContextsPreSplit contexts_pre_split; |
| 78 | } contexts_union; |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 79 | static Contexts* contexts = nullptr; |
| 80 | |
| 81 | #define SERIAL_DIRTY(serial) ((serial)&1) |
| 82 | #define SERIAL_VALUE_LEN(serial) ((serial) >> 24) |
| 83 | |
| 84 | static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME; |
| 85 | static const char* kServiceVersionPropertyName = "ro.property_service.version"; |
| 86 | |
| 87 | // This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol. |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 88 | // It is set to nullptr and never modified. |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 89 | __BIONIC_WEAK_VARIABLE_FOR_NATIVE_BRIDGE |
| 90 | prop_area* __system_property_area__ = nullptr; |
| 91 | |
| 92 | char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME; |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 93 | |
| 94 | class PropertyServiceConnection { |
| 95 | public: |
| 96 | PropertyServiceConnection() : last_error_(0) { |
| 97 | socket_ = ::socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 98 | if (socket_ == -1) { |
| 99 | last_error_ = errno; |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | const size_t namelen = strlen(property_service_socket); |
| 104 | sockaddr_un addr; |
| 105 | memset(&addr, 0, sizeof(addr)); |
| 106 | strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path)); |
| 107 | addr.sun_family = AF_LOCAL; |
| 108 | socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1; |
| 109 | |
| 110 | if (TEMP_FAILURE_RETRY(connect(socket_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) { |
| 111 | last_error_ = errno; |
| 112 | close(socket_); |
| 113 | socket_ = -1; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | bool IsValid() { |
| 118 | return socket_ != -1; |
| 119 | } |
| 120 | |
| 121 | int GetLastError() { |
| 122 | return last_error_; |
| 123 | } |
| 124 | |
| 125 | bool RecvInt32(int32_t* value) { |
| 126 | int result = TEMP_FAILURE_RETRY(recv(socket_, value, sizeof(*value), MSG_WAITALL)); |
| 127 | return CheckSendRecvResult(result, sizeof(*value)); |
| 128 | } |
| 129 | |
| 130 | int socket() { |
| 131 | return socket_; |
| 132 | } |
| 133 | |
| 134 | ~PropertyServiceConnection() { |
| 135 | if (socket_ != -1) { |
| 136 | close(socket_); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | bool CheckSendRecvResult(int result, int expected_len) { |
| 142 | if (result == -1) { |
| 143 | last_error_ = errno; |
| 144 | } else if (result != expected_len) { |
| 145 | last_error_ = -1; |
| 146 | } else { |
| 147 | last_error_ = 0; |
| 148 | } |
| 149 | |
| 150 | return last_error_ == 0; |
| 151 | } |
| 152 | |
| 153 | int socket_; |
| 154 | int last_error_; |
| 155 | |
| 156 | friend class SocketWriter; |
| 157 | }; |
| 158 | |
| 159 | class SocketWriter { |
| 160 | public: |
| 161 | explicit SocketWriter(PropertyServiceConnection* connection) |
| 162 | : connection_(connection), iov_index_(0), uint_buf_index_(0) { |
| 163 | } |
| 164 | |
| 165 | SocketWriter& WriteUint32(uint32_t value) { |
| 166 | CHECK(uint_buf_index_ < kUintBufSize); |
| 167 | CHECK(iov_index_ < kIovSize); |
| 168 | uint32_t* ptr = uint_buf_ + uint_buf_index_; |
| 169 | uint_buf_[uint_buf_index_++] = value; |
| 170 | iov_[iov_index_].iov_base = ptr; |
| 171 | iov_[iov_index_].iov_len = sizeof(*ptr); |
| 172 | ++iov_index_; |
| 173 | return *this; |
| 174 | } |
| 175 | |
| 176 | SocketWriter& WriteString(const char* value) { |
| 177 | uint32_t valuelen = strlen(value); |
| 178 | WriteUint32(valuelen); |
| 179 | if (valuelen == 0) { |
| 180 | return *this; |
| 181 | } |
| 182 | |
| 183 | CHECK(iov_index_ < kIovSize); |
| 184 | iov_[iov_index_].iov_base = const_cast<char*>(value); |
| 185 | iov_[iov_index_].iov_len = valuelen; |
| 186 | ++iov_index_; |
| 187 | |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | bool Send() { |
| 192 | if (!connection_->IsValid()) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | if (writev(connection_->socket(), iov_, iov_index_) == -1) { |
| 197 | connection_->last_error_ = errno; |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | iov_index_ = uint_buf_index_ = 0; |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | static constexpr size_t kUintBufSize = 8; |
| 207 | static constexpr size_t kIovSize = 8; |
| 208 | |
| 209 | PropertyServiceConnection* connection_; |
| 210 | iovec iov_[kIovSize]; |
| 211 | size_t iov_index_; |
| 212 | uint32_t uint_buf_[kUintBufSize]; |
| 213 | size_t uint_buf_index_; |
| 214 | |
| 215 | DISALLOW_IMPLICIT_CONSTRUCTORS(SocketWriter); |
| 216 | }; |
| 217 | |
| 218 | struct prop_msg { |
| 219 | unsigned cmd; |
| 220 | char name[PROP_NAME_MAX]; |
| 221 | char value[PROP_VALUE_MAX]; |
| 222 | }; |
| 223 | |
| 224 | static int send_prop_msg(const prop_msg* msg) { |
| 225 | PropertyServiceConnection connection; |
| 226 | if (!connection.IsValid()) { |
| 227 | return connection.GetLastError(); |
| 228 | } |
| 229 | |
| 230 | int result = -1; |
| 231 | int s = connection.socket(); |
| 232 | |
| 233 | const int num_bytes = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0)); |
| 234 | if (num_bytes == sizeof(prop_msg)) { |
| 235 | // We successfully wrote to the property server but now we |
| 236 | // wait for the property server to finish its work. It |
| 237 | // acknowledges its completion by closing the socket so we |
| 238 | // poll here (on nothing), waiting for the socket to close. |
| 239 | // If you 'adb shell setprop foo bar' you'll see the POLLHUP |
| 240 | // once the socket closes. Out of paranoia we cap our poll |
| 241 | // at 250 ms. |
| 242 | pollfd pollfds[1]; |
| 243 | pollfds[0].fd = s; |
| 244 | pollfds[0].events = 0; |
| 245 | const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */)); |
| 246 | if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) { |
| 247 | result = 0; |
| 248 | } else { |
| 249 | // Ignore the timeout and treat it like a success anyway. |
| 250 | // The init process is single-threaded and its property |
| 251 | // service is sometimes slow to respond (perhaps it's off |
| 252 | // starting a child process or something) and thus this |
| 253 | // times out and the caller thinks it failed, even though |
| 254 | // it's still getting around to it. So we fake it here, |
| 255 | // mostly for ctl.* properties, but we do try and wait 250 |
| 256 | // ms so callers who do read-after-write can reliably see |
| 257 | // what they've written. Most of the time. |
| 258 | // TODO: fix the system properties design. |
| 259 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 260 | "Property service has timed out while trying to set \"%s\" to \"%s\"", |
| 261 | msg->name, msg->value); |
| 262 | result = 0; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return result; |
| 267 | } |
| 268 | |
| 269 | static bool is_dir(const char* pathname) { |
| 270 | struct stat info; |
| 271 | if (stat(pathname, &info) == -1) { |
| 272 | return false; |
| 273 | } |
| 274 | return S_ISDIR(info.st_mode); |
| 275 | } |
| 276 | |
| 277 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 278 | int __system_properties_init() { |
| 279 | // This is called from __libc_init_common, and should leave errno at 0 (http://b/37248982). |
| 280 | ErrnoRestorer errno_restorer; |
| 281 | |
| 282 | if (contexts != nullptr) { |
| 283 | contexts->ResetAccess(); |
| 284 | return 0; |
| 285 | } |
| 286 | contexts = nullptr; |
| 287 | if (is_dir(property_filename)) { |
Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame^] | 288 | if (access("/dev/__properties__/property_info", R_OK) == 0) { |
| 289 | new (&contexts_union.contexts_serialized) ContextsSerialized(); |
| 290 | if (!contexts_union.contexts_serialized.Initialize(false)) { |
| 291 | return -1; |
| 292 | } |
| 293 | contexts = &contexts_union.contexts_serialized; |
| 294 | } else { |
| 295 | new (&contexts_union.contexts_split) ContextsSplit(); |
| 296 | if (!contexts_union.contexts_split.Initialize(false)) { |
| 297 | return -1; |
| 298 | } |
| 299 | contexts = &contexts_union.contexts_split; |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 300 | } |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 301 | } else { |
Tom Cherry | 8d366a8 | 2017-11-30 15:41:32 -0800 | [diff] [blame] | 302 | new (&contexts_union.contexts_pre_split) ContextsPreSplit(); |
| 303 | if (!contexts_union.contexts_pre_split.Initialize(false)) { |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 304 | return -1; |
| 305 | } |
Tom Cherry | 8d366a8 | 2017-11-30 15:41:32 -0800 | [diff] [blame] | 306 | contexts = &contexts_union.contexts_pre_split; |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 312 | int __system_property_set_filename(const char* filename) { |
| 313 | size_t len = strlen(filename); |
| 314 | if (len >= sizeof(property_filename)) return -1; |
| 315 | |
| 316 | strcpy(property_filename, filename); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 321 | int __system_property_area_init() { |
| 322 | if (contexts != nullptr) { |
| 323 | contexts->FreeAndUnmap(); |
| 324 | } |
| 325 | // We set this unconditionally as we want tests to continue on regardless of if this failed |
| 326 | // and property_service will abort on an error condition, so no harm done. |
Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame^] | 327 | new (&contexts_union.contexts_serialized) ContextsSerialized; |
| 328 | contexts = &contexts_union.contexts_serialized; |
| 329 | if (!contexts_union.contexts_serialized.Initialize(true)) { |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 330 | return -1; |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 336 | uint32_t __system_property_area_serial() { |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 337 | if (contexts == nullptr) { |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | prop_area* pa = contexts->GetSerialPropArea(); |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 342 | if (!pa) { |
| 343 | return -1; |
| 344 | } |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 345 | |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 346 | // Make sure this read fulfilled before __system_property_serial |
| 347 | return atomic_load_explicit(pa->serial(), memory_order_acquire); |
| 348 | } |
| 349 | |
| 350 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 351 | const prop_info* __system_property_find(const char* name) { |
| 352 | if (contexts == nullptr) { |
| 353 | return nullptr; |
| 354 | } |
| 355 | |
| 356 | prop_area* pa = contexts->GetPropAreaForName(name); |
| 357 | if (!pa) { |
| 358 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name); |
| 359 | return nullptr; |
| 360 | } |
| 361 | |
| 362 | return pa->find(name); |
| 363 | } |
| 364 | |
| 365 | static bool is_read_only(const char* name) { |
| 366 | return strncmp(name, "ro.", 3) == 0; |
| 367 | } |
| 368 | |
| 369 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 370 | int __system_property_read(const prop_info* pi, char* name, char* value) { |
| 371 | while (true) { |
| 372 | uint32_t serial = __system_property_serial(pi); // acquire semantics |
| 373 | size_t len = SERIAL_VALUE_LEN(serial); |
| 374 | memcpy(value, pi->value, len + 1); |
| 375 | // TODO: Fix the synchronization scheme here. |
| 376 | // There is no fully supported way to implement this kind |
| 377 | // of synchronization in C++11, since the memcpy races with |
| 378 | // updates to pi, and the data being accessed is not atomic. |
| 379 | // The following fence is unintuitive, but would be the |
| 380 | // correct one if memcpy used memory_order_relaxed atomic accesses. |
| 381 | // In practice it seems unlikely that the generated code would |
| 382 | // would be any different, so this should be OK. |
| 383 | atomic_thread_fence(memory_order_acquire); |
| 384 | if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) { |
| 385 | if (name != nullptr) { |
| 386 | size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX); |
| 387 | if (namelen >= PROP_NAME_MAX) { |
| 388 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", |
| 389 | "The property name length for \"%s\" is >= %d;" |
| 390 | " please use __system_property_read_callback" |
| 391 | " to read this property. (the name is truncated to \"%s\")", |
| 392 | pi->name, PROP_NAME_MAX - 1, name); |
| 393 | } |
| 394 | } |
| 395 | if (is_read_only(pi->name) && pi->is_long()) { |
| 396 | async_safe_format_log( |
| 397 | ANDROID_LOG_ERROR, "libc", |
| 398 | "The property \"%s\" has a value with length %zu that is too large for" |
| 399 | " __system_property_get()/__system_property_read(); use" |
| 400 | " __system_property_read_callback() instead.", |
| 401 | pi->name, strlen(pi->long_value())); |
| 402 | } |
| 403 | return len; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 409 | void __system_property_read_callback(const prop_info* pi, |
| 410 | void (*callback)(void* cookie, const char* name, |
| 411 | const char* value, uint32_t serial), |
| 412 | void* cookie) { |
| 413 | // Read only properties don't need to copy the value to a temporary buffer, since it can never |
| 414 | // change. |
| 415 | if (is_read_only(pi->name)) { |
| 416 | uint32_t serial = __system_property_serial(pi); |
| 417 | if (pi->is_long()) { |
| 418 | callback(cookie, pi->name, pi->long_value(), serial); |
| 419 | } else { |
| 420 | callback(cookie, pi->name, pi->value, serial); |
| 421 | } |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | while (true) { |
| 426 | uint32_t serial = __system_property_serial(pi); // acquire semantics |
| 427 | size_t len = SERIAL_VALUE_LEN(serial); |
| 428 | char value_buf[len + 1]; |
| 429 | |
| 430 | memcpy(value_buf, pi->value, len); |
| 431 | value_buf[len] = '\0'; |
| 432 | |
| 433 | // TODO: see todo in __system_property_read function |
| 434 | atomic_thread_fence(memory_order_acquire); |
| 435 | if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) { |
| 436 | callback(cookie, pi->name, value_buf, serial); |
| 437 | return; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 443 | int __system_property_get(const char* name, char* value) { |
| 444 | const prop_info* pi = __system_property_find(name); |
| 445 | |
| 446 | if (pi != 0) { |
| 447 | return __system_property_read(pi, nullptr, value); |
| 448 | } else { |
| 449 | value[0] = 0; |
| 450 | return 0; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static constexpr uint32_t kProtocolVersion1 = 1; |
| 455 | static constexpr uint32_t kProtocolVersion2 = 2; // current |
| 456 | |
| 457 | static atomic_uint_least32_t g_propservice_protocol_version = 0; |
| 458 | |
| 459 | static void detect_protocol_version() { |
| 460 | char value[PROP_VALUE_MAX]; |
| 461 | if (__system_property_get(kServiceVersionPropertyName, value) == 0) { |
| 462 | g_propservice_protocol_version = kProtocolVersion1; |
| 463 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 464 | "Using old property service protocol (\"%s\" is not set)", |
| 465 | kServiceVersionPropertyName); |
| 466 | } else { |
| 467 | uint32_t version = static_cast<uint32_t>(atoll(value)); |
| 468 | if (version >= kProtocolVersion2) { |
| 469 | g_propservice_protocol_version = kProtocolVersion2; |
| 470 | } else { |
| 471 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 472 | "Using old property service protocol (\"%s\"=\"%s\")", |
| 473 | kServiceVersionPropertyName, value); |
| 474 | g_propservice_protocol_version = kProtocolVersion1; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 480 | int __system_property_set(const char* key, const char* value) { |
| 481 | if (key == nullptr) return -1; |
| 482 | if (value == nullptr) value = ""; |
| 483 | |
| 484 | if (g_propservice_protocol_version == 0) { |
| 485 | detect_protocol_version(); |
| 486 | } |
| 487 | |
| 488 | if (g_propservice_protocol_version == kProtocolVersion1) { |
| 489 | // Old protocol does not support long names or values |
| 490 | if (strlen(key) >= PROP_NAME_MAX) return -1; |
| 491 | if (strlen(value) >= PROP_VALUE_MAX) return -1; |
| 492 | |
| 493 | prop_msg msg; |
| 494 | memset(&msg, 0, sizeof msg); |
| 495 | msg.cmd = PROP_MSG_SETPROP; |
| 496 | strlcpy(msg.name, key, sizeof msg.name); |
| 497 | strlcpy(msg.value, value, sizeof msg.value); |
| 498 | |
| 499 | return send_prop_msg(&msg); |
| 500 | } else { |
| 501 | // New protocol only allows long values for ro. properties only. |
| 502 | if (strlen(value) >= PROP_VALUE_MAX && !is_read_only(key)) return -1; |
| 503 | // Use proper protocol |
| 504 | PropertyServiceConnection connection; |
| 505 | if (!connection.IsValid()) { |
| 506 | errno = connection.GetLastError(); |
| 507 | async_safe_format_log( |
| 508 | ANDROID_LOG_WARN, "libc", |
| 509 | "Unable to set property \"%s\" to \"%s\": connection failed; errno=%d (%s)", key, value, |
| 510 | errno, strerror(errno)); |
| 511 | return -1; |
| 512 | } |
| 513 | |
| 514 | SocketWriter writer(&connection); |
| 515 | if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) { |
| 516 | errno = connection.GetLastError(); |
| 517 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 518 | "Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)", |
| 519 | key, value, errno, strerror(errno)); |
| 520 | return -1; |
| 521 | } |
| 522 | |
| 523 | int result = -1; |
| 524 | if (!connection.RecvInt32(&result)) { |
| 525 | errno = connection.GetLastError(); |
| 526 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 527 | "Unable to set property \"%s\" to \"%s\": recv failed; errno=%d (%s)", |
| 528 | key, value, errno, strerror(errno)); |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | if (result != PROP_SUCCESS) { |
| 533 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 534 | "Unable to set property \"%s\" to \"%s\": error code: 0x%x", key, value, |
| 535 | result); |
| 536 | return -1; |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 544 | int __system_property_update(prop_info* pi, const char* value, unsigned int len) { |
| 545 | if (len >= PROP_VALUE_MAX) { |
| 546 | return -1; |
| 547 | } |
| 548 | |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 549 | if (contexts == nullptr) { |
| 550 | return -1; |
| 551 | } |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 552 | |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 553 | prop_area* pa = contexts->GetSerialPropArea(); |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 554 | if (!pa) { |
| 555 | return -1; |
| 556 | } |
| 557 | |
| 558 | uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed); |
| 559 | serial |= 1; |
| 560 | atomic_store_explicit(&pi->serial, serial, memory_order_relaxed); |
| 561 | // The memcpy call here also races. Again pretend it |
| 562 | // used memory_order_relaxed atomics, and use the analogous |
| 563 | // counterintuitive fence. |
| 564 | atomic_thread_fence(memory_order_release); |
| 565 | strlcpy(pi->value, value, len + 1); |
| 566 | |
| 567 | atomic_store_explicit(&pi->serial, (len << 24) | ((serial + 1) & 0xffffff), memory_order_release); |
| 568 | __futex_wake(&pi->serial, INT32_MAX); |
| 569 | |
| 570 | atomic_store_explicit(pa->serial(), atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1, |
| 571 | memory_order_release); |
| 572 | __futex_wake(pa->serial(), INT32_MAX); |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 578 | int __system_property_add(const char* name, unsigned int namelen, const char* value, |
| 579 | unsigned int valuelen) { |
| 580 | if (valuelen >= PROP_VALUE_MAX && !is_read_only(name)) { |
| 581 | return -1; |
| 582 | } |
| 583 | |
| 584 | if (namelen < 1) { |
| 585 | return -1; |
| 586 | } |
| 587 | |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 588 | if (contexts == nullptr) { |
| 589 | return -1; |
| 590 | } |
| 591 | |
| 592 | prop_area* serial_pa = contexts->GetSerialPropArea(); |
| 593 | if (serial_pa == nullptr) { |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 594 | return -1; |
| 595 | } |
| 596 | |
| 597 | prop_area* pa = contexts->GetPropAreaForName(name); |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 598 | if (!pa) { |
| 599 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name); |
| 600 | return -1; |
| 601 | } |
| 602 | |
| 603 | bool ret = pa->add(name, namelen, value, valuelen); |
| 604 | if (!ret) { |
| 605 | return -1; |
| 606 | } |
| 607 | |
| 608 | // There is only a single mutator, but we want to make sure that |
| 609 | // updates are visible to a reader waiting for the update. |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 610 | atomic_store_explicit(serial_pa->serial(), |
| 611 | atomic_load_explicit(serial_pa->serial(), memory_order_relaxed) + 1, |
| 612 | memory_order_release); |
| 613 | __futex_wake(serial_pa->serial(), INT32_MAX); |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | // Wait for non-locked serial, and retrieve it with acquire semantics. |
| 618 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 619 | uint32_t __system_property_serial(const prop_info* pi) { |
| 620 | uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire); |
| 621 | while (SERIAL_DIRTY(serial)) { |
| 622 | __futex_wait(const_cast<_Atomic(uint_least32_t)*>(&pi->serial), serial, nullptr); |
| 623 | serial = load_const_atomic(&pi->serial, memory_order_acquire); |
| 624 | } |
| 625 | return serial; |
| 626 | } |
| 627 | |
| 628 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 629 | uint32_t __system_property_wait_any(uint32_t old_serial) { |
| 630 | uint32_t new_serial; |
| 631 | __system_property_wait(nullptr, old_serial, &new_serial, nullptr); |
| 632 | return new_serial; |
| 633 | } |
| 634 | |
| 635 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 636 | bool __system_property_wait(const prop_info* pi, uint32_t old_serial, uint32_t* new_serial_ptr, |
| 637 | const timespec* relative_timeout) { |
| 638 | // Are we waiting on the global serial or a specific serial? |
| 639 | atomic_uint_least32_t* serial_ptr; |
| 640 | if (pi == nullptr) { |
Tom Cherry | f76bbf5 | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 641 | if (contexts == nullptr) { |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | prop_area* serial_pa = contexts->GetSerialPropArea(); |
| 646 | if (serial_pa == nullptr) { |
| 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | serial_ptr = serial_pa->serial(); |
Tom Cherry | fd44b9f | 2017-11-08 14:01:00 -0800 | [diff] [blame] | 651 | } else { |
| 652 | serial_ptr = const_cast<atomic_uint_least32_t*>(&pi->serial); |
| 653 | } |
| 654 | |
| 655 | uint32_t new_serial; |
| 656 | do { |
| 657 | int rc; |
| 658 | if ((rc = __futex_wait(serial_ptr, old_serial, relative_timeout)) != 0 && rc == -ETIMEDOUT) { |
| 659 | return false; |
| 660 | } |
| 661 | new_serial = load_const_atomic(serial_ptr, memory_order_acquire); |
| 662 | } while (new_serial == old_serial); |
| 663 | |
| 664 | *new_serial_ptr = new_serial; |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 669 | const prop_info* __system_property_find_nth(unsigned n) { |
| 670 | struct find_nth { |
| 671 | const uint32_t sought; |
| 672 | uint32_t current; |
| 673 | const prop_info* result; |
| 674 | |
| 675 | explicit find_nth(uint32_t n) : sought(n), current(0), result(nullptr) { |
| 676 | } |
| 677 | static void fn(const prop_info* pi, void* ptr) { |
| 678 | find_nth* self = reinterpret_cast<find_nth*>(ptr); |
| 679 | if (self->current++ == self->sought) self->result = pi; |
| 680 | } |
| 681 | } state(n); |
| 682 | __system_property_foreach(find_nth::fn, &state); |
| 683 | return state.result; |
| 684 | } |
| 685 | |
| 686 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 687 | int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) { |
| 688 | if (contexts == nullptr) { |
| 689 | return -1; |
| 690 | } |
| 691 | |
| 692 | contexts->ForEach(propfn, cookie); |
| 693 | |
| 694 | return 0; |
| 695 | } |