blob: 93184a32292cefb1f980ed28e5bdeaf8d8719808 [file] [log] [blame]
Tom Cherryfd44b9f2017-11-08 14:01:00 -08001/*
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 Cherry8d366a82017-11-30 15:41:32 -080049#include <new>
50
Tom Cherryfd44b9f2017-11-08 14:01:00 -080051#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 Cherry79b724c2017-11-17 17:14:05 -080062#include "contexts_serialized.h"
Tom Cherryfd44b9f2017-11-08 14:01:00 -080063#include "contexts_split.h"
64#include "prop_area.h"
65#include "prop_info.h"
Tom Cherryf76bbf52017-11-08 14:01:00 -080066#include "property_filename.h"
Tom Cherryfd44b9f2017-11-08 14:01:00 -080067
68// We don't want to use new or malloc in properties (b/31659220), and since these classes are
Tom Cherry8d366a82017-11-30 15:41:32 -080069// 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.
72static union ContextsUnion {
73 ContextsUnion() {}
74 ~ContextsUnion() {}
Tom Cherry79b724c2017-11-17 17:14:05 -080075 ContextsSerialized contexts_serialized;
Tom Cherry8d366a82017-11-30 15:41:32 -080076 ContextsSplit contexts_split;
77 ContextsPreSplit contexts_pre_split;
78} contexts_union;
Tom Cherryfd44b9f2017-11-08 14:01:00 -080079static Contexts* contexts = nullptr;
80
81#define SERIAL_DIRTY(serial) ((serial)&1)
82#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
83
84static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
85static 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 Cherryf76bbf52017-11-08 14:01:00 -080088// It is set to nullptr and never modified.
Tom Cherryfd44b9f2017-11-08 14:01:00 -080089__BIONIC_WEAK_VARIABLE_FOR_NATIVE_BRIDGE
90prop_area* __system_property_area__ = nullptr;
91
92char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Tom Cherryfd44b9f2017-11-08 14:01:00 -080093
94class 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
159class 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
218struct prop_msg {
219 unsigned cmd;
220 char name[PROP_NAME_MAX];
221 char value[PROP_VALUE_MAX];
222};
223
224static 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
269static 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
278int __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 Cherrye149f3c2017-12-13 02:48:23 -0800288 new (&contexts_union.contexts_split) ContextsSplit();
289 if (!contexts_union.contexts_split.Initialize(false)) {
290 return -1;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800291 }
Tom Cherrye149f3c2017-12-13 02:48:23 -0800292 contexts = &contexts_union.contexts_split;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800293 } else {
Tom Cherry8d366a82017-11-30 15:41:32 -0800294 new (&contexts_union.contexts_pre_split) ContextsPreSplit();
295 if (!contexts_union.contexts_pre_split.Initialize(false)) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800296 return -1;
297 }
Tom Cherry8d366a82017-11-30 15:41:32 -0800298 contexts = &contexts_union.contexts_pre_split;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800299 }
300 return 0;
301}
302
303__BIONIC_WEAK_FOR_NATIVE_BRIDGE
304int __system_property_set_filename(const char* filename) {
305 size_t len = strlen(filename);
306 if (len >= sizeof(property_filename)) return -1;
307
308 strcpy(property_filename, filename);
309 return 0;
310}
311
312__BIONIC_WEAK_FOR_NATIVE_BRIDGE
313int __system_property_area_init() {
314 if (contexts != nullptr) {
315 contexts->FreeAndUnmap();
316 }
317 // We set this unconditionally as we want tests to continue on regardless of if this failed
318 // and property_service will abort on an error condition, so no harm done.
Tom Cherrye149f3c2017-12-13 02:48:23 -0800319 new (&contexts_union.contexts_split) ContextsSplit;
320 contexts = &contexts_union.contexts_split;
321 if (!contexts_union.contexts_split.Initialize(true)) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800322 return -1;
323 }
324 return 0;
325}
326
327__BIONIC_WEAK_FOR_NATIVE_BRIDGE
328uint32_t __system_property_area_serial() {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800329 if (contexts == nullptr) {
330 return -1;
331 }
332
333 prop_area* pa = contexts->GetSerialPropArea();
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800334 if (!pa) {
335 return -1;
336 }
Tom Cherryf76bbf52017-11-08 14:01:00 -0800337
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800338 // Make sure this read fulfilled before __system_property_serial
339 return atomic_load_explicit(pa->serial(), memory_order_acquire);
340}
341
342__BIONIC_WEAK_FOR_NATIVE_BRIDGE
343const prop_info* __system_property_find(const char* name) {
344 if (contexts == nullptr) {
345 return nullptr;
346 }
347
348 prop_area* pa = contexts->GetPropAreaForName(name);
349 if (!pa) {
350 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
351 return nullptr;
352 }
353
354 return pa->find(name);
355}
356
357static bool is_read_only(const char* name) {
358 return strncmp(name, "ro.", 3) == 0;
359}
360
361__BIONIC_WEAK_FOR_NATIVE_BRIDGE
362int __system_property_read(const prop_info* pi, char* name, char* value) {
363 while (true) {
364 uint32_t serial = __system_property_serial(pi); // acquire semantics
365 size_t len = SERIAL_VALUE_LEN(serial);
366 memcpy(value, pi->value, len + 1);
367 // TODO: Fix the synchronization scheme here.
368 // There is no fully supported way to implement this kind
369 // of synchronization in C++11, since the memcpy races with
370 // updates to pi, and the data being accessed is not atomic.
371 // The following fence is unintuitive, but would be the
372 // correct one if memcpy used memory_order_relaxed atomic accesses.
373 // In practice it seems unlikely that the generated code would
374 // would be any different, so this should be OK.
375 atomic_thread_fence(memory_order_acquire);
376 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
377 if (name != nullptr) {
378 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
379 if (namelen >= PROP_NAME_MAX) {
380 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
381 "The property name length for \"%s\" is >= %d;"
382 " please use __system_property_read_callback"
383 " to read this property. (the name is truncated to \"%s\")",
384 pi->name, PROP_NAME_MAX - 1, name);
385 }
386 }
387 if (is_read_only(pi->name) && pi->is_long()) {
388 async_safe_format_log(
389 ANDROID_LOG_ERROR, "libc",
390 "The property \"%s\" has a value with length %zu that is too large for"
391 " __system_property_get()/__system_property_read(); use"
392 " __system_property_read_callback() instead.",
393 pi->name, strlen(pi->long_value()));
394 }
395 return len;
396 }
397 }
398}
399
400__BIONIC_WEAK_FOR_NATIVE_BRIDGE
401void __system_property_read_callback(const prop_info* pi,
402 void (*callback)(void* cookie, const char* name,
403 const char* value, uint32_t serial),
404 void* cookie) {
405 // Read only properties don't need to copy the value to a temporary buffer, since it can never
406 // change.
407 if (is_read_only(pi->name)) {
408 uint32_t serial = __system_property_serial(pi);
409 if (pi->is_long()) {
410 callback(cookie, pi->name, pi->long_value(), serial);
411 } else {
412 callback(cookie, pi->name, pi->value, serial);
413 }
414 return;
415 }
416
417 while (true) {
418 uint32_t serial = __system_property_serial(pi); // acquire semantics
419 size_t len = SERIAL_VALUE_LEN(serial);
420 char value_buf[len + 1];
421
422 memcpy(value_buf, pi->value, len);
423 value_buf[len] = '\0';
424
425 // TODO: see todo in __system_property_read function
426 atomic_thread_fence(memory_order_acquire);
427 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
428 callback(cookie, pi->name, value_buf, serial);
429 return;
430 }
431 }
432}
433
434__BIONIC_WEAK_FOR_NATIVE_BRIDGE
435int __system_property_get(const char* name, char* value) {
436 const prop_info* pi = __system_property_find(name);
437
438 if (pi != 0) {
439 return __system_property_read(pi, nullptr, value);
440 } else {
441 value[0] = 0;
442 return 0;
443 }
444}
445
446static constexpr uint32_t kProtocolVersion1 = 1;
447static constexpr uint32_t kProtocolVersion2 = 2; // current
448
449static atomic_uint_least32_t g_propservice_protocol_version = 0;
450
451static void detect_protocol_version() {
452 char value[PROP_VALUE_MAX];
453 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
454 g_propservice_protocol_version = kProtocolVersion1;
455 async_safe_format_log(ANDROID_LOG_WARN, "libc",
456 "Using old property service protocol (\"%s\" is not set)",
457 kServiceVersionPropertyName);
458 } else {
459 uint32_t version = static_cast<uint32_t>(atoll(value));
460 if (version >= kProtocolVersion2) {
461 g_propservice_protocol_version = kProtocolVersion2;
462 } else {
463 async_safe_format_log(ANDROID_LOG_WARN, "libc",
464 "Using old property service protocol (\"%s\"=\"%s\")",
465 kServiceVersionPropertyName, value);
466 g_propservice_protocol_version = kProtocolVersion1;
467 }
468 }
469}
470
471__BIONIC_WEAK_FOR_NATIVE_BRIDGE
472int __system_property_set(const char* key, const char* value) {
473 if (key == nullptr) return -1;
474 if (value == nullptr) value = "";
475
476 if (g_propservice_protocol_version == 0) {
477 detect_protocol_version();
478 }
479
480 if (g_propservice_protocol_version == kProtocolVersion1) {
481 // Old protocol does not support long names or values
482 if (strlen(key) >= PROP_NAME_MAX) return -1;
483 if (strlen(value) >= PROP_VALUE_MAX) return -1;
484
485 prop_msg msg;
486 memset(&msg, 0, sizeof msg);
487 msg.cmd = PROP_MSG_SETPROP;
488 strlcpy(msg.name, key, sizeof msg.name);
489 strlcpy(msg.value, value, sizeof msg.value);
490
491 return send_prop_msg(&msg);
492 } else {
493 // New protocol only allows long values for ro. properties only.
494 if (strlen(value) >= PROP_VALUE_MAX && !is_read_only(key)) return -1;
495 // Use proper protocol
496 PropertyServiceConnection connection;
497 if (!connection.IsValid()) {
498 errno = connection.GetLastError();
499 async_safe_format_log(
500 ANDROID_LOG_WARN, "libc",
501 "Unable to set property \"%s\" to \"%s\": connection failed; errno=%d (%s)", key, value,
502 errno, strerror(errno));
503 return -1;
504 }
505
506 SocketWriter writer(&connection);
507 if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) {
508 errno = connection.GetLastError();
509 async_safe_format_log(ANDROID_LOG_WARN, "libc",
510 "Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)",
511 key, value, errno, strerror(errno));
512 return -1;
513 }
514
515 int result = -1;
516 if (!connection.RecvInt32(&result)) {
517 errno = connection.GetLastError();
518 async_safe_format_log(ANDROID_LOG_WARN, "libc",
519 "Unable to set property \"%s\" to \"%s\": recv failed; errno=%d (%s)",
520 key, value, errno, strerror(errno));
521 return -1;
522 }
523
524 if (result != PROP_SUCCESS) {
525 async_safe_format_log(ANDROID_LOG_WARN, "libc",
526 "Unable to set property \"%s\" to \"%s\": error code: 0x%x", key, value,
527 result);
528 return -1;
529 }
530
531 return 0;
532 }
533}
534
535__BIONIC_WEAK_FOR_NATIVE_BRIDGE
536int __system_property_update(prop_info* pi, const char* value, unsigned int len) {
537 if (len >= PROP_VALUE_MAX) {
538 return -1;
539 }
540
Tom Cherryf76bbf52017-11-08 14:01:00 -0800541 if (contexts == nullptr) {
542 return -1;
543 }
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800544
Tom Cherryf76bbf52017-11-08 14:01:00 -0800545 prop_area* pa = contexts->GetSerialPropArea();
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800546 if (!pa) {
547 return -1;
548 }
549
550 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
551 serial |= 1;
552 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
553 // The memcpy call here also races. Again pretend it
554 // used memory_order_relaxed atomics, and use the analogous
555 // counterintuitive fence.
556 atomic_thread_fence(memory_order_release);
557 strlcpy(pi->value, value, len + 1);
558
559 atomic_store_explicit(&pi->serial, (len << 24) | ((serial + 1) & 0xffffff), memory_order_release);
560 __futex_wake(&pi->serial, INT32_MAX);
561
562 atomic_store_explicit(pa->serial(), atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
563 memory_order_release);
564 __futex_wake(pa->serial(), INT32_MAX);
565
566 return 0;
567}
568
569__BIONIC_WEAK_FOR_NATIVE_BRIDGE
570int __system_property_add(const char* name, unsigned int namelen, const char* value,
571 unsigned int valuelen) {
572 if (valuelen >= PROP_VALUE_MAX && !is_read_only(name)) {
573 return -1;
574 }
575
576 if (namelen < 1) {
577 return -1;
578 }
579
Tom Cherryf76bbf52017-11-08 14:01:00 -0800580 if (contexts == nullptr) {
581 return -1;
582 }
583
584 prop_area* serial_pa = contexts->GetSerialPropArea();
585 if (serial_pa == nullptr) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800586 return -1;
587 }
588
589 prop_area* pa = contexts->GetPropAreaForName(name);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800590 if (!pa) {
591 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
592 return -1;
593 }
594
595 bool ret = pa->add(name, namelen, value, valuelen);
596 if (!ret) {
597 return -1;
598 }
599
600 // There is only a single mutator, but we want to make sure that
601 // updates are visible to a reader waiting for the update.
Tom Cherryf76bbf52017-11-08 14:01:00 -0800602 atomic_store_explicit(serial_pa->serial(),
603 atomic_load_explicit(serial_pa->serial(), memory_order_relaxed) + 1,
604 memory_order_release);
605 __futex_wake(serial_pa->serial(), INT32_MAX);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800606 return 0;
607}
608
609// Wait for non-locked serial, and retrieve it with acquire semantics.
610__BIONIC_WEAK_FOR_NATIVE_BRIDGE
611uint32_t __system_property_serial(const prop_info* pi) {
612 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
613 while (SERIAL_DIRTY(serial)) {
614 __futex_wait(const_cast<_Atomic(uint_least32_t)*>(&pi->serial), serial, nullptr);
615 serial = load_const_atomic(&pi->serial, memory_order_acquire);
616 }
617 return serial;
618}
619
620__BIONIC_WEAK_FOR_NATIVE_BRIDGE
621uint32_t __system_property_wait_any(uint32_t old_serial) {
622 uint32_t new_serial;
623 __system_property_wait(nullptr, old_serial, &new_serial, nullptr);
624 return new_serial;
625}
626
627__BIONIC_WEAK_FOR_NATIVE_BRIDGE
628bool __system_property_wait(const prop_info* pi, uint32_t old_serial, uint32_t* new_serial_ptr,
629 const timespec* relative_timeout) {
630 // Are we waiting on the global serial or a specific serial?
631 atomic_uint_least32_t* serial_ptr;
632 if (pi == nullptr) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800633 if (contexts == nullptr) {
634 return -1;
635 }
636
637 prop_area* serial_pa = contexts->GetSerialPropArea();
638 if (serial_pa == nullptr) {
639 return -1;
640 }
641
642 serial_ptr = serial_pa->serial();
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800643 } else {
644 serial_ptr = const_cast<atomic_uint_least32_t*>(&pi->serial);
645 }
646
647 uint32_t new_serial;
648 do {
649 int rc;
650 if ((rc = __futex_wait(serial_ptr, old_serial, relative_timeout)) != 0 && rc == -ETIMEDOUT) {
651 return false;
652 }
653 new_serial = load_const_atomic(serial_ptr, memory_order_acquire);
654 } while (new_serial == old_serial);
655
656 *new_serial_ptr = new_serial;
657 return true;
658}
659
660__BIONIC_WEAK_FOR_NATIVE_BRIDGE
661const prop_info* __system_property_find_nth(unsigned n) {
662 struct find_nth {
663 const uint32_t sought;
664 uint32_t current;
665 const prop_info* result;
666
667 explicit find_nth(uint32_t n) : sought(n), current(0), result(nullptr) {
668 }
669 static void fn(const prop_info* pi, void* ptr) {
670 find_nth* self = reinterpret_cast<find_nth*>(ptr);
671 if (self->current++ == self->sought) self->result = pi;
672 }
673 } state(n);
674 __system_property_foreach(find_nth::fn, &state);
675 return state.result;
676}
677
678__BIONIC_WEAK_FOR_NATIVE_BRIDGE
679int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
680 if (contexts == nullptr) {
681 return -1;
682 }
683
684 contexts->ForEach(propfn, cookie);
685
686 return 0;
687}