blob: ec1d18fa4e1ffa11e592607793e7212736185d42 [file] [log] [blame]
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001/*
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 */
Elliott Hughesf8562c52017-01-26 16:48:57 -080028
Tom Cherry49a309f2015-09-23 16:09:47 -070029#include <ctype.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000030#include <errno.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000031#include <fcntl.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070032#include <poll.h>
33#include <stdatomic.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000034#include <stdbool.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070035#include <stddef.h>
36#include <stdint.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070037#include <stdlib.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000038#include <string.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070039#include <unistd.h>
40#include <new>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000041
Tom Cherry49a309f2015-09-23 16:09:47 -070042#include <linux/xattr.h>
43#include <netinet/in.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000044#include <sys/mman.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000045#include <sys/select.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070046#include <sys/socket.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000047#include <sys/stat.h>
48#include <sys/types.h>
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -080049#include <sys/uio.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070050#include <sys/un.h>
51#include <sys/xattr.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000052
53#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
54#include <sys/_system_properties.h>
55#include <sys/system_properties.h>
56
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070057#include "private/bionic_futex.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070058#include "private/bionic_lock.h"
Elliott Hughes8eac9af2014-05-09 19:12:08 -070059#include "private/bionic_macros.h"
Dimitry Ivanov581b9f62017-01-09 11:05:52 -080060#include "private/bionic_sdk_version.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070061#include "private/libc_logging.h"
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000062
Elliott Hughesf8562c52017-01-26 16:48:57 -080063static constexpr int PROP_FILENAME_MAX = 1024;
64
65static constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250;
66static constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab;
67
68static constexpr size_t PA_SIZE = 128 * 1024;
69
Elliott Hughes9160ed92017-01-30 17:54:57 -080070#define SERIAL_DIRTY(serial) ((serial)&1)
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000071#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
Dimitry Ivanov489f58b2017-01-24 18:39:04 +000072
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000073static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
74static const char* kServiceVersionPropertyName = "ro.property_service.version";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000075
76/*
77 * Properties are stored in a hybrid trie/binary tree structure.
78 * Each property's name is delimited at '.' characters, and the tokens are put
79 * into a trie structure. Siblings at each level of the trie are stored in a
80 * binary tree. For instance, "ro.secure"="1" could be stored as follows:
81 *
82 * +-----+ children +----+ children +--------+
83 * | |-------------->| ro |-------------->| secure |
84 * +-----+ +----+ +--------+
85 * / \ / |
86 * left / \ right left / | prop +===========+
87 * v v v +-------->| ro.secure |
88 * +-----+ +-----+ +-----+ +-----------+
89 * | net | | sys | | com | | 1 |
90 * +-----+ +-----+ +-----+ +===========+
91 */
92
93// Represents a node in the trie.
94struct prop_bt {
Elliott Hughes9160ed92017-01-30 17:54:57 -080095 uint32_t namelen;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000096
Elliott Hughes9160ed92017-01-30 17:54:57 -080097 // The property trie is updated only by the init process (single threaded) which provides
98 // property service. And it can be read by multiple threads at the same time.
99 // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the
100 // left, right, children "pointers" in the trie node. To make sure readers who see the
101 // change of "pointers" can also notice the change of prop_bt structure contents pointed by
102 // the "pointers", we always use release-consume ordering pair when accessing these "pointers".
Yabin Cuib8ce4742015-02-10 21:35:56 -0800103
Elliott Hughes9160ed92017-01-30 17:54:57 -0800104 // prop "points" to prop_info structure if there is a propery associated with the trie node.
105 // Its situation is similar to the left, right, children "pointers". So we use
106 // atomic_uint_least32_t and release-consume ordering to protect it as well.
Yabin Cuib8ce4742015-02-10 21:35:56 -0800107
Elliott Hughes9160ed92017-01-30 17:54:57 -0800108 // We should also avoid rereading these fields redundantly, since not
109 // all processor implementations ensure that multiple loads from the
110 // same field are carried out in the right order.
111 atomic_uint_least32_t prop;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000112
Elliott Hughes9160ed92017-01-30 17:54:57 -0800113 atomic_uint_least32_t left;
114 atomic_uint_least32_t right;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000115
Elliott Hughes9160ed92017-01-30 17:54:57 -0800116 atomic_uint_least32_t children;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000117
Elliott Hughes9160ed92017-01-30 17:54:57 -0800118 char name[0];
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000119
Elliott Hughes9160ed92017-01-30 17:54:57 -0800120 prop_bt(const char* name, const uint32_t name_length) {
121 this->namelen = name_length;
122 memcpy(this->name, name, name_length);
123 this->name[name_length] = '\0';
124 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000125
Elliott Hughes9160ed92017-01-30 17:54:57 -0800126 private:
127 DISALLOW_COPY_AND_ASSIGN(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000128};
129
Tom Cherry926ebe12015-09-23 15:34:40 -0700130class prop_area {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800131 public:
132 prop_area(const uint32_t magic, const uint32_t version) : magic_(magic), version_(version) {
133 atomic_init(&serial_, 0);
134 memset(reserved_, 0, sizeof(reserved_));
135 // Allocate enough space for the root node.
136 bytes_used_ = sizeof(prop_bt);
137 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000138
Elliott Hughes9160ed92017-01-30 17:54:57 -0800139 const prop_info* find(const char* name);
140 bool add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000141
Elliott Hughes9160ed92017-01-30 17:54:57 -0800142 bool foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie);
Tom Cherry926ebe12015-09-23 15:34:40 -0700143
Elliott Hughes9160ed92017-01-30 17:54:57 -0800144 atomic_uint_least32_t* serial() {
145 return &serial_;
146 }
147 uint32_t magic() const {
148 return magic_;
149 }
150 uint32_t version() const {
151 return version_;
152 }
Tom Cherry926ebe12015-09-23 15:34:40 -0700153
Elliott Hughes9160ed92017-01-30 17:54:57 -0800154 private:
155 void* allocate_obj(const size_t size, uint_least32_t* const off);
156 prop_bt* new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off);
157 prop_info* new_prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen,
158 uint_least32_t* const off);
159 void* to_prop_obj(uint_least32_t off);
160 prop_bt* to_prop_bt(atomic_uint_least32_t* off_p);
161 prop_info* to_prop_info(atomic_uint_least32_t* off_p);
Tom Cherry926ebe12015-09-23 15:34:40 -0700162
Elliott Hughes9160ed92017-01-30 17:54:57 -0800163 prop_bt* root_node();
Tom Cherry926ebe12015-09-23 15:34:40 -0700164
Elliott Hughes9160ed92017-01-30 17:54:57 -0800165 prop_bt* find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700166
Elliott Hughes9160ed92017-01-30 17:54:57 -0800167 const prop_info* find_property(prop_bt* const trie, const char* name, uint32_t namelen,
168 const char* value, uint32_t valuelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700169
Elliott Hughes9160ed92017-01-30 17:54:57 -0800170 bool foreach_property(prop_bt* const trie, void (*propfn)(const prop_info* pi, void* cookie),
171 void* cookie);
Tom Cherry926ebe12015-09-23 15:34:40 -0700172
Elliott Hughes9160ed92017-01-30 17:54:57 -0800173 uint32_t bytes_used_;
174 atomic_uint_least32_t serial_;
175 uint32_t magic_;
176 uint32_t version_;
177 uint32_t reserved_[28];
178 char data_[0];
Tom Cherry926ebe12015-09-23 15:34:40 -0700179
Elliott Hughes9160ed92017-01-30 17:54:57 -0800180 DISALLOW_COPY_AND_ASSIGN(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000181};
182
183struct prop_info {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800184 atomic_uint_least32_t serial;
185 // we need to keep this buffer around because the property
186 // value can be modified whereas name is constant.
187 char value[PROP_VALUE_MAX];
188 char name[0];
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000189
Elliott Hughes9160ed92017-01-30 17:54:57 -0800190 prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen) {
191 memcpy(this->name, name, namelen);
192 this->name[namelen] = '\0';
193 atomic_init(&this->serial, valuelen << 24);
194 memcpy(this->value, value, valuelen);
195 this->value[valuelen] = '\0';
196 }
197
198 private:
199 DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000200};
201
202struct find_nth_cookie {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800203 uint32_t count;
204 const uint32_t n;
205 const prop_info* pi;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000206
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800207 explicit find_nth_cookie(uint32_t n) : count(0), n(n), pi(nullptr) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800208 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000209};
210
Elliott Hughesf8562c52017-01-26 16:48:57 -0800211// This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol.
212// It's also used in a libnativehelper test.
213prop_area* __system_property_area__ = nullptr;
214
Tom Cherry49a309f2015-09-23 16:09:47 -0700215static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000216static size_t pa_data_size;
217static size_t pa_size;
Tom Cherryb4171692015-12-09 15:48:15 -0800218static bool initialized = false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000219
Tom Cherry49a309f2015-09-23 16:09:47 -0700220static prop_area* map_prop_area_rw(const char* filename, const char* context,
221 bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800222 /* dev is a tmpfs that we can use to carve a shared workspace
223 * out of, so let's do that...
224 */
225 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000226
Elliott Hughes9160ed92017-01-30 17:54:57 -0800227 if (fd < 0) {
228 if (errno == EACCES) {
229 /* for consistency with the case where the process has already
230 * mapped the page in and segfaults when trying to write to it
231 */
232 abort();
Tom Cherry49a309f2015-09-23 16:09:47 -0700233 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800234 return nullptr;
235 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700236
Elliott Hughes9160ed92017-01-30 17:54:57 -0800237 if (context) {
238 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
239 __libc_format_log(ANDROID_LOG_ERROR, "libc",
240 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
241 /*
242 * fsetxattr() will fail during system properties tests due to selinux policy.
243 * We do not want to create a custom policy for the tester, so we will continue in
244 * this function but set a flag that an error has occurred.
245 * Init, which is the only daemon that should ever call this function will abort
246 * when this error occurs.
247 * Otherwise, the tester will ignore it and continue, albeit without any selinux
248 * property separation.
249 */
250 if (fsetxattr_failed) {
251 *fsetxattr_failed = true;
252 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000253 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800254 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000255
Elliott Hughes9160ed92017-01-30 17:54:57 -0800256 if (ftruncate(fd, PA_SIZE) < 0) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000257 close(fd);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800258 return nullptr;
259 }
260
261 pa_size = PA_SIZE;
262 pa_data_size = pa_size - sizeof(prop_area);
263
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800264 void* const memory_area = mmap(nullptr, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800265 if (memory_area == MAP_FAILED) {
266 close(fd);
267 return nullptr;
268 }
269
270 prop_area* pa = new (memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
271
272 close(fd);
273 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000274}
275
Tom Cherry49a309f2015-09-23 16:09:47 -0700276static prop_area* map_fd_ro(const int fd) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800277 struct stat fd_stat;
278 if (fstat(fd, &fd_stat) < 0) {
279 return nullptr;
280 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000281
Elliott Hughes9160ed92017-01-30 17:54:57 -0800282 if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0) ||
283 ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) ||
284 (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area)))) {
285 return nullptr;
286 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000287
Elliott Hughes9160ed92017-01-30 17:54:57 -0800288 pa_size = fd_stat.st_size;
289 pa_data_size = pa_size - sizeof(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000290
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800291 void* const map_result = mmap(nullptr, pa_size, PROT_READ, MAP_SHARED, fd, 0);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800292 if (map_result == MAP_FAILED) {
293 return nullptr;
294 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000295
Elliott Hughes9160ed92017-01-30 17:54:57 -0800296 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
297 if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) {
298 munmap(pa, pa_size);
299 return nullptr;
300 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000301
Elliott Hughes9160ed92017-01-30 17:54:57 -0800302 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000303}
304
Elliott Hughesf8562c52017-01-26 16:48:57 -0800305static prop_area* map_prop_area(const char* filename) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800306 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
307 if (fd == -1) return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000308
Elliott Hughes9160ed92017-01-30 17:54:57 -0800309 prop_area* map_result = map_fd_ro(fd);
310 close(fd);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000311
Elliott Hughes9160ed92017-01-30 17:54:57 -0800312 return map_result;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000313}
314
Elliott Hughes9160ed92017-01-30 17:54:57 -0800315void* prop_area::allocate_obj(const size_t size, uint_least32_t* const off) {
316 const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t));
317 if (bytes_used_ + aligned > pa_data_size) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800318 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800319 }
320
321 *off = bytes_used_;
322 bytes_used_ += aligned;
323 return data_ + *off;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000324}
325
Elliott Hughes9160ed92017-01-30 17:54:57 -0800326prop_bt* prop_area::new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off) {
327 uint_least32_t new_offset;
328 void* const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800329 if (p != nullptr) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800330 prop_bt* bt = new (p) prop_bt(name, namelen);
331 *off = new_offset;
332 return bt;
333 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000334
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800335 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000336}
337
Elliott Hughes9160ed92017-01-30 17:54:57 -0800338prop_info* prop_area::new_prop_info(const char* name, uint32_t namelen, const char* value,
339 uint32_t valuelen, uint_least32_t* const off) {
340 uint_least32_t new_offset;
341 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800342 if (p != nullptr) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800343 prop_info* info = new (p) prop_info(name, namelen, value, valuelen);
344 *off = new_offset;
345 return info;
346 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000347
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800348 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000349}
350
Elliott Hughes9160ed92017-01-30 17:54:57 -0800351void* prop_area::to_prop_obj(uint_least32_t off) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800352 if (off > pa_data_size) return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800353
354 return (data_ + off);
355}
356
357inline prop_bt* prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800358 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
359 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
360}
361
Elliott Hughes9160ed92017-01-30 17:54:57 -0800362inline prop_info* prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800363 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
364 return reinterpret_cast<prop_info*>(to_prop_obj(off));
365}
366
Elliott Hughes9160ed92017-01-30 17:54:57 -0800367inline prop_bt* prop_area::root_node() {
368 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000369}
370
Elliott Hughes9160ed92017-01-30 17:54:57 -0800371static int cmp_prop_name(const char* one, uint32_t one_len, const char* two, uint32_t two_len) {
372 if (one_len < two_len)
373 return -1;
374 else if (one_len > two_len)
375 return 1;
376 else
377 return strncmp(one, two, one_len);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000378}
379
Elliott Hughes9160ed92017-01-30 17:54:57 -0800380prop_bt* prop_area::find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen,
381 bool alloc_if_needed) {
382 prop_bt* current = bt;
383 while (true) {
384 if (!current) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800385 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000386 }
387
Elliott Hughes9160ed92017-01-30 17:54:57 -0800388 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
389 if (ret == 0) {
390 return current;
391 }
392
393 if (ret < 0) {
394 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
395 if (left_offset != 0) {
396 current = to_prop_bt(&current->left);
397 } else {
398 if (!alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800399 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800400 }
401
Yabin Cuib8ce4742015-02-10 21:35:56 -0800402 uint_least32_t new_offset;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800403 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
404 if (new_bt) {
405 atomic_store_explicit(&current->left, new_offset, memory_order_release);
406 }
407 return new_bt;
408 }
409 } else {
410 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
411 if (right_offset != 0) {
412 current = to_prop_bt(&current->right);
413 } else {
414 if (!alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800415 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000416 }
417
Elliott Hughes9160ed92017-01-30 17:54:57 -0800418 uint_least32_t new_offset;
419 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
420 if (new_bt) {
421 atomic_store_explicit(&current->right, new_offset, memory_order_release);
422 }
423 return new_bt;
424 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000425 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800426 }
427}
428
429const prop_info* prop_area::find_property(prop_bt* const trie, const char* name, uint32_t namelen,
430 const char* value, uint32_t valuelen,
431 bool alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800432 if (!trie) return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800433
434 const char* remaining_name = name;
435 prop_bt* current = trie;
436 while (true) {
437 const char* sep = strchr(remaining_name, '.');
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800438 const bool want_subtree = (sep != nullptr);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800439 const uint32_t substr_size = (want_subtree) ? sep - remaining_name : strlen(remaining_name);
440
441 if (!substr_size) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800442 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800443 }
444
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800445 prop_bt* root = nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800446 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
447 if (children_offset != 0) {
448 root = to_prop_bt(&current->children);
449 } else if (alloc_if_needed) {
450 uint_least32_t new_offset;
451 root = new_prop_bt(remaining_name, substr_size, &new_offset);
452 if (root) {
453 atomic_store_explicit(&current->children, new_offset, memory_order_release);
454 }
455 }
456
457 if (!root) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800458 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800459 }
460
461 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
462 if (!current) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800463 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800464 }
465
466 if (!want_subtree) break;
467
468 remaining_name = sep + 1;
469 }
470
471 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
472 if (prop_offset != 0) {
473 return to_prop_info(&current->prop);
474 } else if (alloc_if_needed) {
475 uint_least32_t new_offset;
476 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
477 if (new_info) {
478 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
479 }
480
481 return new_info;
482 } else {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800483 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800484 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000485}
486
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000487class PropertyServiceConnection {
488 public:
489 PropertyServiceConnection() : last_error_(0) {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800490 socket_ = ::socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
491 if (socket_ == -1) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000492 last_error_ = errno;
493 return;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000494 }
495
496 const size_t namelen = strlen(property_service_socket);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000497 sockaddr_un addr;
498 memset(&addr, 0, sizeof(addr));
499 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
500 addr.sun_family = AF_LOCAL;
501 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000502
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800503 if (TEMP_FAILURE_RETRY(connect(socket_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
504 close(socket_);
505 socket_ = -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000506 last_error_ = errno;
507 }
508 }
509
510 bool IsValid() {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800511 return socket_ != -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000512 }
513
514 int GetLastError() {
515 return last_error_;
516 }
517
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000518 bool RecvInt32(int32_t* value) {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800519 int result = TEMP_FAILURE_RETRY(recv(socket_, value, sizeof(*value), MSG_WAITALL));
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000520 return CheckSendRecvResult(result, sizeof(*value));
521 }
522
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800523 int socket() {
524 return socket_;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000525 }
526
527 ~PropertyServiceConnection() {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800528 if (socket_ != -1) {
529 close(socket_);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000530 }
531 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800532
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000533 private:
534 bool CheckSendRecvResult(int result, int expected_len) {
535 if (result == -1) {
536 last_error_ = errno;
537 } else if (result != expected_len) {
538 last_error_ = -1;
539 } else {
540 last_error_ = 0;
541 }
542
543 return last_error_ == 0;
544 }
545
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800546 int socket_;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000547 int last_error_;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800548
549 friend class SocketWriter;
550};
551
552class SocketWriter {
553 public:
554 explicit SocketWriter(PropertyServiceConnection* connection)
555 : connection_(connection), iov_index_(0), uint_buf_index_(0)
556 {}
557
558 SocketWriter& WriteUint32(uint32_t value) {
559 CHECK(uint_buf_index_ < kUintBufSize);
560 CHECK(iov_index_ < kIovSize);
561 uint32_t* ptr = uint_buf_ + uint_buf_index_;
562 uint_buf_[uint_buf_index_++] = value;
563 iov_[iov_index_].iov_base = ptr;
564 iov_[iov_index_].iov_len = sizeof(*ptr);
565 ++iov_index_;
566 return *this;
567 }
568
569 SocketWriter& WriteString(const char* value) {
570 uint32_t valuelen = strlen(value);
571 WriteUint32(valuelen);
572 if (valuelen == 0) {
573 return *this;
574 }
575
576 CHECK(iov_index_ < kIovSize);
577 iov_[iov_index_].iov_base = const_cast<char*>(value);
578 iov_[iov_index_].iov_len = valuelen;
579 ++iov_index_;
580
581 return *this;
582 }
583
584 bool Send() {
585 if (!connection_->IsValid()) {
586 return false;
587 }
588
589 if (writev(connection_->socket(), iov_, iov_index_) == -1) {
590 connection_->last_error_ = errno;
591 return false;
592 }
593
594 iov_index_ = uint_buf_index_ = 0;
595 return true;
596 }
597
598 private:
599 static constexpr size_t kUintBufSize = 8;
600 static constexpr size_t kIovSize = 8;
601
602 PropertyServiceConnection* connection_;
603 iovec iov_[kIovSize];
604 size_t iov_index_;
605 uint32_t uint_buf_[kUintBufSize];
606 size_t uint_buf_index_;
607
608 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketWriter);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000609};
610
Elliott Hughesf8562c52017-01-26 16:48:57 -0800611struct prop_msg {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800612 unsigned cmd;
613 char name[PROP_NAME_MAX];
614 char value[PROP_VALUE_MAX];
Elliott Hughesf8562c52017-01-26 16:48:57 -0800615};
616
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000617static int send_prop_msg(const prop_msg* msg) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800618 PropertyServiceConnection connection;
619 if (!connection.IsValid()) {
620 return connection.GetLastError();
621 }
622
623 int result = -1;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800624 int s = connection.socket();
Elliott Hughes9160ed92017-01-30 17:54:57 -0800625
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800626 const int num_bytes = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
Elliott Hughes9160ed92017-01-30 17:54:57 -0800627 if (num_bytes == sizeof(prop_msg)) {
628 // We successfully wrote to the property server but now we
629 // wait for the property server to finish its work. It
630 // acknowledges its completion by closing the socket so we
631 // poll here (on nothing), waiting for the socket to close.
632 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
633 // once the socket closes. Out of paranoia we cap our poll
634 // at 250 ms.
635 pollfd pollfds[1];
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800636 pollfds[0].fd = s;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800637 pollfds[0].events = 0;
638 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
639 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
640 result = 0;
641 } else {
642 // Ignore the timeout and treat it like a success anyway.
643 // The init process is single-threaded and its property
644 // service is sometimes slow to respond (perhaps it's off
645 // starting a child process or something) and thus this
646 // times out and the caller thinks it failed, even though
647 // it's still getting around to it. So we fake it here,
648 // mostly for ctl.* properties, but we do try and wait 250
649 // ms so callers who do read-after-write can reliably see
650 // what they've written. Most of the time.
651 // TODO: fix the system properties design.
652 __libc_format_log(ANDROID_LOG_WARN, "libc",
653 "Property service has timed out while trying to set \"%s\" to \"%s\"",
654 msg->name, msg->value);
655 result = 0;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000656 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800657 }
Dimitry Ivanov489f58b2017-01-24 18:39:04 +0000658
Elliott Hughes9160ed92017-01-30 17:54:57 -0800659 return result;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000660}
661
Elliott Hughes9160ed92017-01-30 17:54:57 -0800662static void find_nth_fn(const prop_info* pi, void* ptr) {
663 find_nth_cookie* cookie = reinterpret_cast<find_nth_cookie*>(ptr);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000664
Elliott Hughes9160ed92017-01-30 17:54:57 -0800665 if (cookie->n == cookie->count) cookie->pi = pi;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000666
Elliott Hughes9160ed92017-01-30 17:54:57 -0800667 cookie->count++;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000668}
669
Elliott Hughes9160ed92017-01-30 17:54:57 -0800670bool prop_area::foreach_property(prop_bt* const trie,
671 void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
672 if (!trie) return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000673
Elliott Hughes9160ed92017-01-30 17:54:57 -0800674 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
675 if (left_offset != 0) {
676 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
677 if (err < 0) return false;
678 }
679 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
680 if (prop_offset != 0) {
681 prop_info* info = to_prop_info(&trie->prop);
682 if (!info) return false;
683 propfn(info, cookie);
684 }
685 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
686 if (children_offset != 0) {
687 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
688 if (err < 0) return false;
689 }
690 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
691 if (right_offset != 0) {
692 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
693 if (err < 0) return false;
694 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000695
Elliott Hughes9160ed92017-01-30 17:54:57 -0800696 return true;
Tom Cherry926ebe12015-09-23 15:34:40 -0700697}
698
Elliott Hughes9160ed92017-01-30 17:54:57 -0800699const prop_info* prop_area::find(const char* name) {
700 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
Tom Cherry926ebe12015-09-23 15:34:40 -0700701}
702
Elliott Hughes9160ed92017-01-30 17:54:57 -0800703bool prop_area::add(const char* name, unsigned int namelen, const char* value,
704 unsigned int valuelen) {
705 return find_property(root_node(), name, namelen, value, valuelen, true);
Tom Cherry926ebe12015-09-23 15:34:40 -0700706}
707
Elliott Hughes9160ed92017-01-30 17:54:57 -0800708bool prop_area::foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
709 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000710}
711
Tom Cherryb4171692015-12-09 15:48:15 -0800712class context_node {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800713 public:
714 context_node(context_node* next, const char* context, prop_area* pa)
715 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
716 lock_.init(false);
717 }
718 ~context_node() {
719 unmap();
720 free(context_);
721 }
722 bool open(bool access_rw, bool* fsetxattr_failed);
723 bool check_access_and_open();
724 void reset_access();
Tom Cherryb4171692015-12-09 15:48:15 -0800725
Elliott Hughes9160ed92017-01-30 17:54:57 -0800726 const char* context() const {
727 return context_;
728 }
729 prop_area* pa() {
730 return pa_;
731 }
Tom Cherryb4171692015-12-09 15:48:15 -0800732
Elliott Hughes9160ed92017-01-30 17:54:57 -0800733 context_node* next;
Tom Cherryb4171692015-12-09 15:48:15 -0800734
Elliott Hughes9160ed92017-01-30 17:54:57 -0800735 private:
736 bool check_access();
737 void unmap();
Tom Cherryb4171692015-12-09 15:48:15 -0800738
Elliott Hughes9160ed92017-01-30 17:54:57 -0800739 Lock lock_;
740 char* context_;
741 prop_area* pa_;
742 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700743};
744
745struct prefix_node {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800746 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
747 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
748 }
749 ~prefix_node() {
750 free(prefix);
751 }
752 char* prefix;
753 const size_t prefix_len;
754 context_node* context;
755 struct prefix_node* next;
Tom Cherry49a309f2015-09-23 16:09:47 -0700756};
757
758template <typename List, typename... Args>
759static inline void list_add(List** list, Args... args) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800760 *list = new List(*list, args...);
Tom Cherry49a309f2015-09-23 16:09:47 -0700761}
762
763static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800764 size_t prefix_len = strlen(prefix);
Tom Cherry49a309f2015-09-23 16:09:47 -0700765
Elliott Hughes9160ed92017-01-30 17:54:57 -0800766 auto next_list = list;
Tom Cherry49a309f2015-09-23 16:09:47 -0700767
Elliott Hughes9160ed92017-01-30 17:54:57 -0800768 while (*next_list) {
769 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
770 list_add(next_list, prefix, context);
771 return;
Tom Cherry49a309f2015-09-23 16:09:47 -0700772 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800773 next_list = &(*next_list)->next;
774 }
775 list_add(next_list, prefix, context);
Tom Cherry49a309f2015-09-23 16:09:47 -0700776}
777
778template <typename List, typename Func>
779static void list_foreach(List* list, Func func) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800780 while (list) {
781 func(list);
782 list = list->next;
783 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700784}
785
786template <typename List, typename Func>
787static List* list_find(List* list, Func func) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800788 while (list) {
789 if (func(list)) {
790 return list;
Tom Cherry49a309f2015-09-23 16:09:47 -0700791 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800792 list = list->next;
793 }
794 return nullptr;
Tom Cherry49a309f2015-09-23 16:09:47 -0700795}
796
797template <typename List>
798static void list_free(List** list) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800799 while (*list) {
800 auto old_list = *list;
801 *list = old_list->next;
802 delete old_list;
803 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700804}
805
806static prefix_node* prefixes = nullptr;
807static context_node* contexts = nullptr;
808
809/*
810 * pthread_mutex_lock() calls into system_properties in the case of contention.
811 * This creates a risk of dead lock if any system_properties functions
812 * use pthread locks after system_property initialization.
813 *
814 * For this reason, the below three functions use a bionic Lock and static
815 * allocation of memory for each filename.
816 */
817
Tom Cherryb4171692015-12-09 15:48:15 -0800818bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800819 lock_.lock();
820 if (pa_) {
Tom Cherryb4171692015-12-09 15:48:15 -0800821 lock_.unlock();
Elliott Hughes9160ed92017-01-30 17:54:57 -0800822 return true;
823 }
824
825 char filename[PROP_FILENAME_MAX];
826 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", property_filename, context_);
827 if (len < 0 || len > PROP_FILENAME_MAX) {
828 lock_.unlock();
829 return false;
830 }
831
832 if (access_rw) {
833 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
834 } else {
835 pa_ = map_prop_area(filename);
836 }
837 lock_.unlock();
838 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700839}
840
Tom Cherryb4171692015-12-09 15:48:15 -0800841bool context_node::check_access_and_open() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800842 if (!pa_ && !no_access_) {
843 if (!check_access() || !open(false, nullptr)) {
844 no_access_ = true;
Tom Cherryb4171692015-12-09 15:48:15 -0800845 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800846 }
847 return pa_;
Tom Cherryb4171692015-12-09 15:48:15 -0800848}
849
850void context_node::reset_access() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800851 if (!check_access()) {
852 unmap();
853 no_access_ = true;
854 } else {
855 no_access_ = false;
856 }
Tom Cherryb4171692015-12-09 15:48:15 -0800857}
858
859bool context_node::check_access() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800860 char filename[PROP_FILENAME_MAX];
861 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", property_filename, context_);
862 if (len < 0 || len > PROP_FILENAME_MAX) {
863 return false;
864 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700865
Elliott Hughes9160ed92017-01-30 17:54:57 -0800866 return access(filename, R_OK) == 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700867}
868
Tom Cherryb4171692015-12-09 15:48:15 -0800869void context_node::unmap() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800870 if (!pa_) {
871 return;
872 }
Tom Cherryb4171692015-12-09 15:48:15 -0800873
Elliott Hughes9160ed92017-01-30 17:54:57 -0800874 munmap(pa_, pa_size);
875 if (pa_ == __system_property_area__) {
876 __system_property_area__ = nullptr;
877 }
878 pa_ = nullptr;
Tom Cherryb4171692015-12-09 15:48:15 -0800879}
880
Tom Cherry49a309f2015-09-23 16:09:47 -0700881static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800882 char filename[PROP_FILENAME_MAX];
883 int len =
884 __libc_format_buffer(filename, sizeof(filename), "%s/properties_serial", property_filename);
885 if (len < 0 || len > PROP_FILENAME_MAX) {
886 __system_property_area__ = nullptr;
887 return false;
888 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700889
Elliott Hughes9160ed92017-01-30 17:54:57 -0800890 if (access_rw) {
891 __system_property_area__ =
892 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
893 } else {
894 __system_property_area__ = map_prop_area(filename);
895 }
896 return __system_property_area__;
Tom Cherry49a309f2015-09-23 16:09:47 -0700897}
898
899static prop_area* get_prop_area_for_name(const char* name) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800900 auto entry = list_find(prefixes, [name](prefix_node* l) {
901 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
902 });
903 if (!entry) {
904 return nullptr;
905 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700906
Elliott Hughes9160ed92017-01-30 17:54:57 -0800907 auto cnode = entry->context;
908 if (!cnode->pa()) {
909 /*
910 * We explicitly do not check no_access_ in this case because unlike the
911 * case of foreach(), we want to generate an selinux audit for each
912 * non-permitted property access in this function.
913 */
914 cnode->open(false, nullptr);
915 }
916 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700917}
918
919/*
920 * The below two functions are duplicated from label_support.c in libselinux.
921 * TODO: Find a location suitable for these functions such that both libc and
922 * libselinux can share a common source file.
923 */
924
925/*
926 * The read_spec_entries and read_spec_entry functions may be used to
927 * replace sscanf to read entries from spec files. The file and
928 * property services now use these.
929 */
930
931/* Read an entry from a spec file (e.g. file_contexts) */
Elliott Hughes9160ed92017-01-30 17:54:57 -0800932static inline int read_spec_entry(char** entry, char** ptr, int* len) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800933 *entry = nullptr;
934 char* tmp_buf = nullptr;
Tom Cherry49a309f2015-09-23 16:09:47 -0700935
Elliott Hughes9160ed92017-01-30 17:54:57 -0800936 while (isspace(**ptr) && **ptr != '\0') (*ptr)++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700937
Elliott Hughes9160ed92017-01-30 17:54:57 -0800938 tmp_buf = *ptr;
939 *len = 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700940
Elliott Hughes9160ed92017-01-30 17:54:57 -0800941 while (!isspace(**ptr) && **ptr != '\0') {
942 (*ptr)++;
943 (*len)++;
944 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700945
Elliott Hughes9160ed92017-01-30 17:54:57 -0800946 if (*len) {
947 *entry = strndup(tmp_buf, *len);
948 if (!*entry) return -1;
949 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700950
Elliott Hughes9160ed92017-01-30 17:54:57 -0800951 return 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700952}
953
954/*
955 * line_buf - Buffer containing the spec entries .
956 * num_args - The number of spec parameter entries to process.
957 * ... - A 'char **spec_entry' for each parameter.
958 * returns - The number of items processed.
959 *
960 * This function calls read_spec_entry() to do the actual string processing.
961 */
Elliott Hughes9160ed92017-01-30 17:54:57 -0800962static int read_spec_entries(char* line_buf, int num_args, ...) {
963 char **spec_entry, *buf_p;
964 int len, rc, items, entry_len = 0;
965 va_list ap;
Tom Cherry49a309f2015-09-23 16:09:47 -0700966
Elliott Hughes9160ed92017-01-30 17:54:57 -0800967 len = strlen(line_buf);
968 if (line_buf[len - 1] == '\n')
969 line_buf[len - 1] = '\0';
970 else
971 /* Handle case if line not \n terminated by bumping
972 * the len for the check below (as the line is NUL
973 * terminated by getline(3)) */
974 len++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700975
Elliott Hughes9160ed92017-01-30 17:54:57 -0800976 buf_p = line_buf;
977 while (isspace(*buf_p)) buf_p++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700978
Elliott Hughes9160ed92017-01-30 17:54:57 -0800979 /* Skip comment lines and empty lines. */
980 if (*buf_p == '#' || *buf_p == '\0') return 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700981
Elliott Hughes9160ed92017-01-30 17:54:57 -0800982 /* Process the spec file entries */
983 va_start(ap, num_args);
Tom Cherry49a309f2015-09-23 16:09:47 -0700984
Elliott Hughes9160ed92017-01-30 17:54:57 -0800985 items = 0;
986 while (items < num_args) {
987 spec_entry = va_arg(ap, char**);
Tom Cherry49a309f2015-09-23 16:09:47 -0700988
Elliott Hughes9160ed92017-01-30 17:54:57 -0800989 if (len - 1 == buf_p - line_buf) {
990 va_end(ap);
991 return items;
Tom Cherry49a309f2015-09-23 16:09:47 -0700992 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800993
994 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
995 if (rc < 0) {
996 va_end(ap);
997 return rc;
998 }
999 if (entry_len) items++;
1000 }
1001 va_end(ap);
1002 return items;
Tom Cherry49a309f2015-09-23 16:09:47 -07001003}
1004
Elliott Hughes9160ed92017-01-30 17:54:57 -08001005static bool initialize_properties_from_file(const char* filename) {
1006 FILE* file = fopen(filename, "re");
1007 if (!file) {
1008 return false;
1009 }
1010
1011 char* buffer = nullptr;
1012 size_t line_len;
1013 char* prop_prefix = nullptr;
1014 char* context = nullptr;
1015
1016 while (getline(&buffer, &line_len, file) > 0) {
1017 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1018 if (items <= 0) {
1019 continue;
1020 }
1021 if (items == 1) {
1022 free(prop_prefix);
1023 continue;
1024 }
1025 /*
1026 * init uses ctl.* properties as an IPC mechanism and does not write them
1027 * to a property file, therefore we do not need to create property files
1028 * to store them.
1029 */
1030 if (!strncmp(prop_prefix, "ctl.", 4)) {
1031 free(prop_prefix);
1032 free(context);
1033 continue;
Tom Cherry49a309f2015-09-23 16:09:47 -07001034 }
1035
Elliott Hughes9160ed92017-01-30 17:54:57 -08001036 auto old_context =
1037 list_find(contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
1038 if (old_context) {
1039 list_add_after_len(&prefixes, prop_prefix, old_context);
1040 } else {
1041 list_add(&contexts, context, nullptr);
1042 list_add_after_len(&prefixes, prop_prefix, contexts);
Tom Cherry49a309f2015-09-23 16:09:47 -07001043 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001044 free(prop_prefix);
1045 free(context);
1046 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001047
Elliott Hughes9160ed92017-01-30 17:54:57 -08001048 free(buffer);
1049 fclose(file);
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001050
Elliott Hughes9160ed92017-01-30 17:54:57 -08001051 return true;
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001052}
1053
1054static bool initialize_properties() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001055 // If we do find /property_contexts, then this is being
1056 // run as part of the OTA updater on older release that had
1057 // /property_contexts - b/34370523
1058 if (initialize_properties_from_file("/property_contexts")) {
Tom Cherry49a309f2015-09-23 16:09:47 -07001059 return true;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001060 }
1061
1062 // TODO: Change path to /system/property_contexts after b/27805372
1063 if (!initialize_properties_from_file("/plat_property_contexts")) {
1064 return false;
1065 }
1066
1067 // TODO: Change path to /vendor/property_contexts after b/27805372
1068 // device-specific property context is optional, so load if it exists.
1069 initialize_properties_from_file("/nonplat_property_contexts");
1070
1071 return true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001072}
1073
1074static bool is_dir(const char* pathname) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001075 struct stat info;
1076 if (stat(pathname, &info) == -1) {
1077 return false;
1078 }
1079 return S_ISDIR(info.st_mode);
Tom Cherry49a309f2015-09-23 16:09:47 -07001080}
1081
Tom Cherryb4171692015-12-09 15:48:15 -08001082static void free_and_unmap_contexts() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001083 list_free(&prefixes);
1084 list_free(&contexts);
1085 if (__system_property_area__) {
1086 munmap(__system_property_area__, pa_size);
1087 __system_property_area__ = nullptr;
1088 }
Tom Cherryb4171692015-12-09 15:48:15 -08001089}
1090
Elliott Hughes9160ed92017-01-30 17:54:57 -08001091int __system_properties_init() {
1092 if (initialized) {
1093 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001094 return 0;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001095 }
1096 if (is_dir(property_filename)) {
Tom Cherry49a309f2015-09-23 16:09:47 -07001097 if (!initialize_properties()) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001098 return -1;
Tom Cherry49a309f2015-09-23 16:09:47 -07001099 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001100 if (!map_system_property_area(false, nullptr)) {
1101 free_and_unmap_contexts();
1102 return -1;
Tom Cherry49a309f2015-09-23 16:09:47 -07001103 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001104 } else {
1105 __system_property_area__ = map_prop_area(property_filename);
Tom Cherry6ed51c02015-12-04 11:34:42 -08001106 if (!__system_property_area__) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001107 return -1;
Tom Cherry6ed51c02015-12-04 11:34:42 -08001108 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001109 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1110 list_add_after_len(&prefixes, "*", contexts);
1111 }
1112 initialized = true;
1113 return 0;
1114}
Tom Cherry6ed51c02015-12-04 11:34:42 -08001115
Elliott Hughes9160ed92017-01-30 17:54:57 -08001116int __system_property_set_filename(const char* filename) {
1117 size_t len = strlen(filename);
1118 if (len >= sizeof(property_filename)) return -1;
1119
1120 strcpy(property_filename, filename);
1121 return 0;
1122}
1123
1124int __system_property_area_init() {
1125 free_and_unmap_contexts();
1126 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
1127 if (!initialize_properties()) {
1128 return -1;
1129 }
1130 bool open_failed = false;
1131 bool fsetxattr_failed = false;
1132 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1133 if (!l->open(true, &fsetxattr_failed)) {
1134 open_failed = true;
Tom Cherry926ebe12015-09-23 15:34:40 -07001135 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001136 });
1137 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1138 free_and_unmap_contexts();
1139 return -1;
1140 }
1141 initialized = true;
1142 return fsetxattr_failed ? -2 : 0;
1143}
Tom Cherry926ebe12015-09-23 15:34:40 -07001144
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001145uint32_t __system_property_area_serial() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001146 prop_area* pa = __system_property_area__;
1147 if (!pa) {
1148 return -1;
1149 }
1150 // Make sure this read fulfilled before __system_property_serial
1151 return atomic_load_explicit(pa->serial(), memory_order_acquire);
1152}
1153
1154const prop_info* __system_property_find(const char* name) {
1155 if (!__system_property_area__) {
1156 return nullptr;
1157 }
1158
1159 prop_area* pa = get_prop_area_for_name(name);
1160 if (!pa) {
1161 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
1162 return nullptr;
1163 }
1164
1165 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001166}
1167
Hans Boehm1e8587a2014-08-19 14:07:55 -07001168// The C11 standard doesn't allow atomic loads from const fields,
1169// though C++11 does. Fudge it until standards get straightened out.
Elliott Hughes9160ed92017-01-30 17:54:57 -08001170static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, memory_order mo) {
1171 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1172 return atomic_load_explicit(non_const_s, mo);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001173}
1174
Elliott Hughes9160ed92017-01-30 17:54:57 -08001175int __system_property_read(const prop_info* pi, char* name, char* value) {
1176 while (true) {
1177 uint32_t serial = __system_property_serial(pi); // acquire semantics
1178 size_t len = SERIAL_VALUE_LEN(serial);
1179 memcpy(value, pi->value, len + 1);
1180 // TODO: Fix the synchronization scheme here.
1181 // There is no fully supported way to implement this kind
1182 // of synchronization in C++11, since the memcpy races with
1183 // updates to pi, and the data being accessed is not atomic.
1184 // The following fence is unintuitive, but would be the
1185 // correct one if memcpy used memory_order_relaxed atomic accesses.
1186 // In practice it seems unlikely that the generated code would
1187 // would be any different, so this should be OK.
1188 atomic_thread_fence(memory_order_acquire);
1189 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1190 if (name != nullptr) {
1191 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1192 if (namelen >= PROP_NAME_MAX) {
1193 __libc_format_log(ANDROID_LOG_ERROR, "libc",
1194 "The property name length for \"%s\" is >= %d;"
1195 " please use __system_property_read_callback"
1196 " to read this property. (the name is truncated to \"%s\")",
1197 pi->name, PROP_NAME_MAX - 1, name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001198 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001199 }
1200 return len;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001201 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001202 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001203}
1204
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001205void __system_property_read_callback(const prop_info* pi,
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001206 void (*callback)(void* cookie,
1207 const char* name,
1208 const char* value,
1209 uint32_t serial),
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001210 void* cookie) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001211 while (true) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001212 uint32_t serial = __system_property_serial(pi); // acquire semantics
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001213 size_t len = SERIAL_VALUE_LEN(serial);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001214 char value_buf[len + 1];
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001215
1216 memcpy(value_buf, pi->value, len);
1217 value_buf[len] = '\0';
1218
1219 // TODO: see todo in __system_property_read function
1220 atomic_thread_fence(memory_order_acquire);
1221 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001222 callback(cookie, pi->name, value_buf, serial);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001223 return;
1224 }
1225 }
1226}
1227
Elliott Hughes9160ed92017-01-30 17:54:57 -08001228int __system_property_get(const char* name, char* value) {
1229 const prop_info* pi = __system_property_find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001230
Elliott Hughes9160ed92017-01-30 17:54:57 -08001231 if (pi != 0) {
1232 return __system_property_read(pi, nullptr, value);
1233 } else {
1234 value[0] = 0;
1235 return 0;
1236 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001237}
1238
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001239static constexpr uint32_t kProtocolVersion1 = 1;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001240static constexpr uint32_t kProtocolVersion2 = 2; // current
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001241
1242static atomic_uint_least32_t g_propservice_protocol_version = 0;
1243
1244static void detect_protocol_version() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001245 char value[PROP_VALUE_MAX];
1246 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1247 g_propservice_protocol_version = kProtocolVersion1;
1248 __libc_format_log(ANDROID_LOG_WARN, "libc",
1249 "Using old property service protocol (\"%s\" is not set)",
1250 kServiceVersionPropertyName);
1251 } else {
1252 uint32_t version = static_cast<uint32_t>(atoll(value));
1253 if (version >= kProtocolVersion2) {
1254 g_propservice_protocol_version = kProtocolVersion2;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001255 } else {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001256 __libc_format_log(ANDROID_LOG_WARN, "libc",
1257 "Using old property service protocol (\"%s\"=\"%s\")",
1258 kServiceVersionPropertyName, value);
1259 g_propservice_protocol_version = kProtocolVersion1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001260 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001261 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001262}
1263
1264int __system_property_set(const char* key, const char* value) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001265 if (key == nullptr) return -1;
1266 if (value == nullptr) value = "";
1267 if (strlen(value) >= PROP_VALUE_MAX) return -1;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001268
Elliott Hughes9160ed92017-01-30 17:54:57 -08001269 if (g_propservice_protocol_version == 0) {
1270 detect_protocol_version();
1271 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001272
Elliott Hughes9160ed92017-01-30 17:54:57 -08001273 if (g_propservice_protocol_version == kProtocolVersion1) {
1274 // Old protocol does not support long names
1275 if (strlen(key) >= PROP_NAME_MAX) return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001276
Elliott Hughes9160ed92017-01-30 17:54:57 -08001277 prop_msg msg;
1278 memset(&msg, 0, sizeof msg);
1279 msg.cmd = PROP_MSG_SETPROP;
1280 strlcpy(msg.name, key, sizeof msg.name);
1281 strlcpy(msg.value, value, sizeof msg.value);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001282
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001283 return send_prop_msg(&msg);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001284 } else {
1285 // Use proper protocol
1286 PropertyServiceConnection connection;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001287 if (!connection.IsValid()) {
1288 errno = connection.GetLastError();
1289 __libc_format_log(ANDROID_LOG_WARN,
1290 "libc",
1291 "Unable to set property \"%s\" to \"%s\": connection failed; errno=%d (%s)",
1292 key,
1293 value,
1294 errno,
1295 strerror(errno));
1296 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001297 }
1298
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001299 SocketWriter writer(&connection);
1300 if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) {
1301 errno = connection.GetLastError();
1302 __libc_format_log(ANDROID_LOG_WARN,
1303 "libc",
1304 "Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)",
1305 key,
1306 value,
1307 errno,
1308 strerror(errno));
1309 return -1;
1310 }
1311
1312 int result = -1;
1313 if (!connection.RecvInt32(&result)) {
1314 errno = connection.GetLastError();
1315 __libc_format_log(ANDROID_LOG_WARN,
1316 "libc",
1317 "Unable to set property \"%s\" to \"%s\": recv failed; errno=%d (%s)",
1318 key,
1319 value,
1320 errno,
1321 strerror(errno));
1322 return -1;
1323 }
1324
1325 if (result != PROP_SUCCESS) {
1326 __libc_format_log(ANDROID_LOG_WARN,
1327 "libc",
1328 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1329 key,
1330 value,
1331 result);
1332 return -1;
1333 }
1334
1335 return 0;
1336 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001337}
1338
Elliott Hughes9160ed92017-01-30 17:54:57 -08001339int __system_property_update(prop_info* pi, const char* value, unsigned int len) {
1340 if (len >= PROP_VALUE_MAX) {
1341 return -1;
1342 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001343
Elliott Hughes9160ed92017-01-30 17:54:57 -08001344 prop_area* pa = __system_property_area__;
Tom Cherry6ed51c02015-12-04 11:34:42 -08001345
Elliott Hughes9160ed92017-01-30 17:54:57 -08001346 if (!pa) {
1347 return -1;
1348 }
Tom Cherry6ed51c02015-12-04 11:34:42 -08001349
Elliott Hughes9160ed92017-01-30 17:54:57 -08001350 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1351 serial |= 1;
1352 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1353 // The memcpy call here also races. Again pretend it
1354 // used memory_order_relaxed atomics, and use the analogous
1355 // counterintuitive fence.
1356 atomic_thread_fence(memory_order_release);
1357 strlcpy(pi->value, value, len + 1);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001358
Elliott Hughes9160ed92017-01-30 17:54:57 -08001359 atomic_store_explicit(&pi->serial, (len << 24) | ((serial + 1) & 0xffffff), memory_order_release);
1360 __futex_wake(&pi->serial, INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001361
Elliott Hughes9160ed92017-01-30 17:54:57 -08001362 atomic_store_explicit(pa->serial(), atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
1363 memory_order_release);
1364 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001365
Elliott Hughes9160ed92017-01-30 17:54:57 -08001366 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001367}
jiaguo879d3302014-03-13 17:39:58 +08001368
Elliott Hughes9160ed92017-01-30 17:54:57 -08001369int __system_property_add(const char* name, unsigned int namelen, const char* value,
1370 unsigned int valuelen) {
1371 if (valuelen >= PROP_VALUE_MAX) {
1372 return -1;
1373 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001374
Elliott Hughes9160ed92017-01-30 17:54:57 -08001375 if (namelen < 1) {
1376 return -1;
1377 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001378
Elliott Hughes9160ed92017-01-30 17:54:57 -08001379 if (!__system_property_area__) {
1380 return -1;
1381 }
Tom Cherry6ed51c02015-12-04 11:34:42 -08001382
Elliott Hughes9160ed92017-01-30 17:54:57 -08001383 prop_area* pa = get_prop_area_for_name(name);
Tom Cherry49a309f2015-09-23 16:09:47 -07001384
Elliott Hughes9160ed92017-01-30 17:54:57 -08001385 if (!pa) {
1386 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
1387 return -1;
1388 }
Tom Cherry926ebe12015-09-23 15:34:40 -07001389
Elliott Hughes9160ed92017-01-30 17:54:57 -08001390 bool ret = pa->add(name, namelen, value, valuelen);
1391 if (!ret) {
1392 return -1;
1393 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001394
Elliott Hughes9160ed92017-01-30 17:54:57 -08001395 // There is only a single mutator, but we want to make sure that
1396 // updates are visible to a reader waiting for the update.
1397 atomic_store_explicit(
1398 __system_property_area__->serial(),
1399 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
1400 memory_order_release);
1401 __futex_wake(__system_property_area__->serial(), INT32_MAX);
1402 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001403}
1404
Hans Boehm30214b92014-07-31 15:53:22 -07001405// Wait for non-locked serial, and retrieve it with acquire semantics.
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001406uint32_t __system_property_serial(const prop_info* pi) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001407 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
1408 while (SERIAL_DIRTY(serial)) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001409 __futex_wait(const_cast<_Atomic(uint_least32_t)*>(&pi->serial), serial, nullptr);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001410 serial = load_const_atomic(&pi->serial, memory_order_acquire);
1411 }
1412 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001413}
1414
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001415uint32_t __system_property_wait_any(uint32_t old_serial) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001416 uint32_t new_serial;
Elliott Hughes40c885a2017-02-16 17:13:04 -08001417 __system_property_wait(nullptr, old_serial, &new_serial, nullptr);
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001418 return new_serial;
1419}
Elliott Hughes9160ed92017-01-30 17:54:57 -08001420
Elliott Hughes40c885a2017-02-16 17:13:04 -08001421bool __system_property_wait(const prop_info* pi,
1422 uint32_t old_serial,
1423 uint32_t* new_serial_ptr,
1424 const timespec* relative_timeout) {
1425 // Are we waiting on the global serial or a specific serial?
1426 atomic_uint_least32_t* serial_ptr;
1427 if (pi == nullptr) {
1428 if (__system_property_area__ == nullptr) return -1;
1429 serial_ptr = __system_property_area__->serial();
1430 } else {
1431 serial_ptr = const_cast<atomic_uint_least32_t*>(&pi->serial);
1432 }
1433
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001434 uint32_t new_serial;
1435 do {
Elliott Hughes40c885a2017-02-16 17:13:04 -08001436 int rc;
1437 if ((rc = __futex_wait(serial_ptr, old_serial, relative_timeout)) != 0 && rc == -ETIMEDOUT) {
1438 return false;
1439 }
1440 new_serial = load_const_atomic(serial_ptr, memory_order_acquire);
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001441 } while (new_serial == old_serial);
Elliott Hughes40c885a2017-02-16 17:13:04 -08001442
1443 *new_serial_ptr = new_serial;
1444 return true;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001445}
1446
1447const prop_info* __system_property_find_nth(unsigned n) {
1448 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
1449 __libc_fatal(
1450 "__system_property_find_nth is not supported since Android O,"
1451 " please use __system_property_foreach instead.");
1452 }
1453
1454 find_nth_cookie cookie(n);
1455
1456 const int err = __system_property_foreach(find_nth_fn, &cookie);
1457 if (err < 0) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -08001458 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001459 }
1460
1461 return cookie.pi;
1462}
1463
1464int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
1465 if (!__system_property_area__) {
1466 return -1;
1467 }
1468
1469 list_foreach(contexts, [propfn, cookie](context_node* l) {
1470 if (l->check_access_and_open()) {
1471 l->pa()->foreach (propfn, cookie);
1472 }
1473 });
1474 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001475}