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