blob: c610df2b3288dd3e2637ade2ddfae6cb9a848030 [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
Tom Cherrye275d6d2017-12-11 23:31:33 -080029#include "system_properties/system_properties.h"
30
Tom Cherryfd44b9f2017-11-08 14:01:00 -080031#include <errno.h>
Tom Cherryfd44b9f2017-11-08 14:01:00 -080032#include <stdatomic.h>
Tom Cherryfd44b9f2017-11-08 14:01:00 -080033#include <stdlib.h>
34#include <string.h>
Tom Cherryfd44b9f2017-11-08 14:01:00 -080035#include <sys/stat.h>
36#include <sys/types.h>
Tom Cherryfd44b9f2017-11-08 14:01:00 -080037#include <unistd.h>
38
Tom Cherry8d366a82017-11-30 15:41:32 -080039#include <new>
40
Tom Cherryfd44b9f2017-11-08 14:01:00 -080041#include <async_safe/log.h>
42
43#include "private/ErrnoRestorer.h"
Tom Cherryfd44b9f2017-11-08 14:01:00 -080044#include "private/bionic_futex.h"
Tom Cherryfd44b9f2017-11-08 14:01:00 -080045
Tom Cherrye275d6d2017-12-11 23:31:33 -080046#include "system_properties/context_node.h"
47#include "system_properties/prop_area.h"
48#include "system_properties/prop_info.h"
Tom Cherryfd44b9f2017-11-08 14:01:00 -080049
50#define SERIAL_DIRTY(serial) ((serial)&1)
51#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
52
Tom Cherryfd44b9f2017-11-08 14:01:00 -080053static bool is_dir(const char* pathname) {
54 struct stat info;
55 if (stat(pathname, &info) == -1) {
56 return false;
57 }
58 return S_ISDIR(info.st_mode);
59}
60
Tom Cherrye275d6d2017-12-11 23:31:33 -080061bool SystemProperties::Init(const char* filename) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -080062 // This is called from __libc_init_common, and should leave errno at 0 (http://b/37248982).
63 ErrnoRestorer errno_restorer;
64
Tom Cherrye275d6d2017-12-11 23:31:33 -080065 if (initialized_) {
66 contexts()->ResetAccess();
67 return true;
Tom Cherryfd44b9f2017-11-08 14:01:00 -080068 }
Tom Cherrye275d6d2017-12-11 23:31:33 -080069
70 if (strlen(filename) > PROP_FILENAME_MAX) {
71 return false;
72 }
73 strcpy(property_filename_, filename);
74
75 if (is_dir(property_filename_)) {
Tom Cherry8be995b2017-12-14 01:57:37 +000076 if (access("/dev/__properties__/property_info", R_OK) == 0) {
Tom Cherrye275d6d2017-12-11 23:31:33 -080077 new (&contexts_union_.contexts_serialized) ContextsSerialized();
78 if (!contexts_union_.contexts_serialized.Initialize(false, property_filename_, nullptr)) {
79 return false;
Tom Cherry8be995b2017-12-14 01:57:37 +000080 }
Tom Cherry8be995b2017-12-14 01:57:37 +000081 } else {
Tom Cherrye275d6d2017-12-11 23:31:33 -080082 new (&contexts_union_.contexts_split) ContextsSplit();
83 if (!contexts_union_.contexts_split.Initialize(false, property_filename_, nullptr)) {
84 return false;
Tom Cherry8be995b2017-12-14 01:57:37 +000085 }
Tom Cherryfd44b9f2017-11-08 14:01:00 -080086 }
Tom Cherryfd44b9f2017-11-08 14:01:00 -080087 } else {
Tom Cherrye275d6d2017-12-11 23:31:33 -080088 new (&contexts_union_.contexts_pre_split) ContextsPreSplit();
89 if (!contexts_union_.contexts_pre_split.Initialize(false, property_filename_, nullptr)) {
90 return false;
Tom Cherryfd44b9f2017-11-08 14:01:00 -080091 }
Tom Cherryfd44b9f2017-11-08 14:01:00 -080092 }
Tom Cherrye275d6d2017-12-11 23:31:33 -080093 initialized_ = true;
94 return true;
Tom Cherryfd44b9f2017-11-08 14:01:00 -080095}
96
Tom Cherrye275d6d2017-12-11 23:31:33 -080097bool SystemProperties::AreaInit(const char* filename, bool* fsetxattr_failed) {
98 if (strlen(filename) > PROP_FILENAME_MAX) {
99 return false;
100 }
101 strcpy(property_filename_, filename);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800102
Tom Cherrye275d6d2017-12-11 23:31:33 -0800103 new (&contexts_union_.contexts_serialized) ContextsSerialized();
104 if (!contexts_union_.contexts_serialized.Initialize(true, property_filename_, fsetxattr_failed)) {
105 return false;
106 }
107 initialized_ = true;
108 return true;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800109}
110
Tom Cherrye275d6d2017-12-11 23:31:33 -0800111uint32_t SystemProperties::AreaSerial() {
112 if (!initialized_) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800113 return -1;
114 }
115
Tom Cherrye275d6d2017-12-11 23:31:33 -0800116 prop_area* pa = contexts()->GetSerialPropArea();
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800117 if (!pa) {
118 return -1;
119 }
Tom Cherryf76bbf52017-11-08 14:01:00 -0800120
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800121 // Make sure this read fulfilled before __system_property_serial
122 return atomic_load_explicit(pa->serial(), memory_order_acquire);
123}
124
Tom Cherrye275d6d2017-12-11 23:31:33 -0800125const prop_info* SystemProperties::Find(const char* name) {
126 if (!initialized_) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800127 return nullptr;
128 }
129
Tom Cherrye275d6d2017-12-11 23:31:33 -0800130 prop_area* pa = contexts()->GetPropAreaForName(name);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800131 if (!pa) {
132 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
133 return nullptr;
134 }
135
136 return pa->find(name);
137}
138
139static bool is_read_only(const char* name) {
140 return strncmp(name, "ro.", 3) == 0;
141}
142
Tom Cherrye275d6d2017-12-11 23:31:33 -0800143int SystemProperties::Read(const prop_info* pi, char* name, char* value) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800144 while (true) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800145 uint32_t serial = Serial(pi); // acquire semantics
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800146 size_t len = SERIAL_VALUE_LEN(serial);
147 memcpy(value, pi->value, len + 1);
148 // TODO: Fix the synchronization scheme here.
149 // There is no fully supported way to implement this kind
150 // of synchronization in C++11, since the memcpy races with
151 // updates to pi, and the data being accessed is not atomic.
152 // The following fence is unintuitive, but would be the
153 // correct one if memcpy used memory_order_relaxed atomic accesses.
154 // In practice it seems unlikely that the generated code would
155 // would be any different, so this should be OK.
156 atomic_thread_fence(memory_order_acquire);
157 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
158 if (name != nullptr) {
159 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
160 if (namelen >= PROP_NAME_MAX) {
161 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
162 "The property name length for \"%s\" is >= %d;"
163 " please use __system_property_read_callback"
164 " to read this property. (the name is truncated to \"%s\")",
165 pi->name, PROP_NAME_MAX - 1, name);
166 }
167 }
168 if (is_read_only(pi->name) && pi->is_long()) {
169 async_safe_format_log(
170 ANDROID_LOG_ERROR, "libc",
171 "The property \"%s\" has a value with length %zu that is too large for"
172 " __system_property_get()/__system_property_read(); use"
173 " __system_property_read_callback() instead.",
174 pi->name, strlen(pi->long_value()));
175 }
176 return len;
177 }
178 }
179}
180
Tom Cherrye275d6d2017-12-11 23:31:33 -0800181void SystemProperties::ReadCallback(const prop_info* pi,
182 void (*callback)(void* cookie, const char* name,
183 const char* value, uint32_t serial),
184 void* cookie) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800185 // Read only properties don't need to copy the value to a temporary buffer, since it can never
186 // change.
187 if (is_read_only(pi->name)) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800188 uint32_t serial = Serial(pi);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800189 if (pi->is_long()) {
190 callback(cookie, pi->name, pi->long_value(), serial);
191 } else {
192 callback(cookie, pi->name, pi->value, serial);
193 }
194 return;
195 }
196
197 while (true) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800198 uint32_t serial = Serial(pi); // acquire semantics
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800199 size_t len = SERIAL_VALUE_LEN(serial);
200 char value_buf[len + 1];
201
202 memcpy(value_buf, pi->value, len);
203 value_buf[len] = '\0';
204
Tom Cherrye275d6d2017-12-11 23:31:33 -0800205 // TODO: see todo in Read function
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800206 atomic_thread_fence(memory_order_acquire);
207 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
208 callback(cookie, pi->name, value_buf, serial);
209 return;
210 }
211 }
212}
213
Tom Cherrye275d6d2017-12-11 23:31:33 -0800214int SystemProperties::Get(const char* name, char* value) {
215 const prop_info* pi = Find(name);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800216
217 if (pi != 0) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800218 return Read(pi, nullptr, value);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800219 } else {
220 value[0] = 0;
221 return 0;
222 }
223}
224
Tom Cherrye275d6d2017-12-11 23:31:33 -0800225int SystemProperties::Update(prop_info* pi, const char* value, unsigned int len) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800226 if (len >= PROP_VALUE_MAX) {
227 return -1;
228 }
229
Tom Cherrye275d6d2017-12-11 23:31:33 -0800230 if (!initialized_) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800231 return -1;
232 }
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800233
Tom Cherrye275d6d2017-12-11 23:31:33 -0800234 prop_area* pa = contexts()->GetSerialPropArea();
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800235 if (!pa) {
236 return -1;
237 }
238
239 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
240 serial |= 1;
241 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
242 // The memcpy call here also races. Again pretend it
243 // used memory_order_relaxed atomics, and use the analogous
244 // counterintuitive fence.
245 atomic_thread_fence(memory_order_release);
246 strlcpy(pi->value, value, len + 1);
247
248 atomic_store_explicit(&pi->serial, (len << 24) | ((serial + 1) & 0xffffff), memory_order_release);
249 __futex_wake(&pi->serial, INT32_MAX);
250
251 atomic_store_explicit(pa->serial(), atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
252 memory_order_release);
253 __futex_wake(pa->serial(), INT32_MAX);
254
255 return 0;
256}
257
Tom Cherrye275d6d2017-12-11 23:31:33 -0800258int SystemProperties::Add(const char* name, unsigned int namelen, const char* value,
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800259 unsigned int valuelen) {
260 if (valuelen >= PROP_VALUE_MAX && !is_read_only(name)) {
261 return -1;
262 }
263
264 if (namelen < 1) {
265 return -1;
266 }
267
Tom Cherrye275d6d2017-12-11 23:31:33 -0800268 if (!initialized_) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800269 return -1;
270 }
271
Tom Cherrye275d6d2017-12-11 23:31:33 -0800272 prop_area* serial_pa = contexts()->GetSerialPropArea();
Tom Cherryf76bbf52017-11-08 14:01:00 -0800273 if (serial_pa == nullptr) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800274 return -1;
275 }
276
Tom Cherrye275d6d2017-12-11 23:31:33 -0800277 prop_area* pa = contexts()->GetPropAreaForName(name);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800278 if (!pa) {
279 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
280 return -1;
281 }
282
283 bool ret = pa->add(name, namelen, value, valuelen);
284 if (!ret) {
285 return -1;
286 }
287
288 // There is only a single mutator, but we want to make sure that
289 // updates are visible to a reader waiting for the update.
Tom Cherryf76bbf52017-11-08 14:01:00 -0800290 atomic_store_explicit(serial_pa->serial(),
291 atomic_load_explicit(serial_pa->serial(), memory_order_relaxed) + 1,
292 memory_order_release);
293 __futex_wake(serial_pa->serial(), INT32_MAX);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800294 return 0;
295}
296
297// Wait for non-locked serial, and retrieve it with acquire semantics.
Tom Cherrye275d6d2017-12-11 23:31:33 -0800298uint32_t SystemProperties::Serial(const prop_info* pi) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800299 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
300 while (SERIAL_DIRTY(serial)) {
301 __futex_wait(const_cast<_Atomic(uint_least32_t)*>(&pi->serial), serial, nullptr);
302 serial = load_const_atomic(&pi->serial, memory_order_acquire);
303 }
304 return serial;
305}
306
Tom Cherrye275d6d2017-12-11 23:31:33 -0800307uint32_t SystemProperties::WaitAny(uint32_t old_serial) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800308 uint32_t new_serial;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800309 Wait(nullptr, old_serial, &new_serial, nullptr);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800310 return new_serial;
311}
312
Tom Cherrye275d6d2017-12-11 23:31:33 -0800313bool SystemProperties::Wait(const prop_info* pi, uint32_t old_serial, uint32_t* new_serial_ptr,
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800314 const timespec* relative_timeout) {
315 // Are we waiting on the global serial or a specific serial?
316 atomic_uint_least32_t* serial_ptr;
317 if (pi == nullptr) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800318 if (!initialized_) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800319 return -1;
320 }
321
Tom Cherrye275d6d2017-12-11 23:31:33 -0800322 prop_area* serial_pa = contexts()->GetSerialPropArea();
Tom Cherryf76bbf52017-11-08 14:01:00 -0800323 if (serial_pa == nullptr) {
324 return -1;
325 }
326
327 serial_ptr = serial_pa->serial();
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800328 } else {
329 serial_ptr = const_cast<atomic_uint_least32_t*>(&pi->serial);
330 }
331
332 uint32_t new_serial;
333 do {
334 int rc;
335 if ((rc = __futex_wait(serial_ptr, old_serial, relative_timeout)) != 0 && rc == -ETIMEDOUT) {
336 return false;
337 }
338 new_serial = load_const_atomic(serial_ptr, memory_order_acquire);
339 } while (new_serial == old_serial);
340
341 *new_serial_ptr = new_serial;
342 return true;
343}
344
Tom Cherrye275d6d2017-12-11 23:31:33 -0800345const prop_info* SystemProperties::FindNth(unsigned n) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800346 struct find_nth {
347 const uint32_t sought;
348 uint32_t current;
349 const prop_info* result;
350
351 explicit find_nth(uint32_t n) : sought(n), current(0), result(nullptr) {
352 }
353 static void fn(const prop_info* pi, void* ptr) {
354 find_nth* self = reinterpret_cast<find_nth*>(ptr);
355 if (self->current++ == self->sought) self->result = pi;
356 }
357 } state(n);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800358 Foreach(find_nth::fn, &state);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800359 return state.result;
360}
361
Tom Cherrye275d6d2017-12-11 23:31:33 -0800362int SystemProperties::Foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
363 if (!initialized_) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800364 return -1;
365 }
366
Tom Cherrye275d6d2017-12-11 23:31:33 -0800367 contexts()->ForEach(propfn, cookie);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800368
369 return 0;
370}