blob: a4faf85acb056db460a0473e996c91983b71aed1 [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.
Elliott Hughesf8562c52017-01-26 16:48:57 -0800212prop_area* __system_property_area__ = nullptr;
213
Tom Cherry49a309f2015-09-23 16:09:47 -0700214static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000215static size_t pa_data_size;
216static size_t pa_size;
Tom Cherryb4171692015-12-09 15:48:15 -0800217static bool initialized = false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000218
Tom Cherry49a309f2015-09-23 16:09:47 -0700219static prop_area* map_prop_area_rw(const char* filename, const char* context,
220 bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800221 /* dev is a tmpfs that we can use to carve a shared workspace
222 * out of, so let's do that...
223 */
224 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000225
Elliott Hughes9160ed92017-01-30 17:54:57 -0800226 if (fd < 0) {
227 if (errno == EACCES) {
228 /* for consistency with the case where the process has already
229 * mapped the page in and segfaults when trying to write to it
230 */
231 abort();
Tom Cherry49a309f2015-09-23 16:09:47 -0700232 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800233 return nullptr;
234 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700235
Elliott Hughes9160ed92017-01-30 17:54:57 -0800236 if (context) {
237 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
238 __libc_format_log(ANDROID_LOG_ERROR, "libc",
239 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
240 /*
241 * fsetxattr() will fail during system properties tests due to selinux policy.
242 * We do not want to create a custom policy for the tester, so we will continue in
243 * this function but set a flag that an error has occurred.
244 * Init, which is the only daemon that should ever call this function will abort
245 * when this error occurs.
246 * Otherwise, the tester will ignore it and continue, albeit without any selinux
247 * property separation.
248 */
249 if (fsetxattr_failed) {
250 *fsetxattr_failed = true;
251 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000252 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800253 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000254
Elliott Hughes9160ed92017-01-30 17:54:57 -0800255 if (ftruncate(fd, PA_SIZE) < 0) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000256 close(fd);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800257 return nullptr;
258 }
259
260 pa_size = PA_SIZE;
261 pa_data_size = pa_size - sizeof(prop_area);
262
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800263 void* const memory_area = mmap(nullptr, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800264 if (memory_area == MAP_FAILED) {
265 close(fd);
266 return nullptr;
267 }
268
269 prop_area* pa = new (memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
270
271 close(fd);
272 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000273}
274
Tom Cherry49a309f2015-09-23 16:09:47 -0700275static prop_area* map_fd_ro(const int fd) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800276 struct stat fd_stat;
277 if (fstat(fd, &fd_stat) < 0) {
278 return nullptr;
279 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000280
Elliott Hughes9160ed92017-01-30 17:54:57 -0800281 if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0) ||
282 ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) ||
283 (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area)))) {
284 return nullptr;
285 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000286
Elliott Hughes9160ed92017-01-30 17:54:57 -0800287 pa_size = fd_stat.st_size;
288 pa_data_size = pa_size - sizeof(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000289
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800290 void* const map_result = mmap(nullptr, pa_size, PROT_READ, MAP_SHARED, fd, 0);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800291 if (map_result == MAP_FAILED) {
292 return nullptr;
293 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000294
Elliott Hughes9160ed92017-01-30 17:54:57 -0800295 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
296 if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) {
297 munmap(pa, pa_size);
298 return nullptr;
299 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000300
Elliott Hughes9160ed92017-01-30 17:54:57 -0800301 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000302}
303
Elliott Hughesf8562c52017-01-26 16:48:57 -0800304static prop_area* map_prop_area(const char* filename) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800305 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
306 if (fd == -1) return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000307
Elliott Hughes9160ed92017-01-30 17:54:57 -0800308 prop_area* map_result = map_fd_ro(fd);
309 close(fd);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000310
Elliott Hughes9160ed92017-01-30 17:54:57 -0800311 return map_result;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000312}
313
Elliott Hughes9160ed92017-01-30 17:54:57 -0800314void* prop_area::allocate_obj(const size_t size, uint_least32_t* const off) {
315 const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t));
316 if (bytes_used_ + aligned > pa_data_size) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800317 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800318 }
319
320 *off = bytes_used_;
321 bytes_used_ += aligned;
322 return data_ + *off;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000323}
324
Elliott Hughes9160ed92017-01-30 17:54:57 -0800325prop_bt* prop_area::new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off) {
326 uint_least32_t new_offset;
327 void* const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800328 if (p != nullptr) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800329 prop_bt* bt = new (p) prop_bt(name, namelen);
330 *off = new_offset;
331 return bt;
332 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000333
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800334 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000335}
336
Elliott Hughes9160ed92017-01-30 17:54:57 -0800337prop_info* prop_area::new_prop_info(const char* name, uint32_t namelen, const char* value,
338 uint32_t valuelen, uint_least32_t* const off) {
339 uint_least32_t new_offset;
340 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800341 if (p != nullptr) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800342 prop_info* info = new (p) prop_info(name, namelen, value, valuelen);
343 *off = new_offset;
344 return info;
345 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000346
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800347 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000348}
349
Elliott Hughes9160ed92017-01-30 17:54:57 -0800350void* prop_area::to_prop_obj(uint_least32_t off) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800351 if (off > pa_data_size) return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800352
353 return (data_ + off);
354}
355
356inline prop_bt* prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800357 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
358 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
359}
360
Elliott Hughes9160ed92017-01-30 17:54:57 -0800361inline prop_info* prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800362 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
363 return reinterpret_cast<prop_info*>(to_prop_obj(off));
364}
365
Elliott Hughes9160ed92017-01-30 17:54:57 -0800366inline prop_bt* prop_area::root_node() {
367 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000368}
369
Elliott Hughes9160ed92017-01-30 17:54:57 -0800370static int cmp_prop_name(const char* one, uint32_t one_len, const char* two, uint32_t two_len) {
371 if (one_len < two_len)
372 return -1;
373 else if (one_len > two_len)
374 return 1;
375 else
376 return strncmp(one, two, one_len);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000377}
378
Elliott Hughes9160ed92017-01-30 17:54:57 -0800379prop_bt* prop_area::find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen,
380 bool alloc_if_needed) {
381 prop_bt* current = bt;
382 while (true) {
383 if (!current) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800384 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000385 }
386
Elliott Hughes9160ed92017-01-30 17:54:57 -0800387 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
388 if (ret == 0) {
389 return current;
390 }
391
392 if (ret < 0) {
393 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
394 if (left_offset != 0) {
395 current = to_prop_bt(&current->left);
396 } else {
397 if (!alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800398 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800399 }
400
Yabin Cuib8ce4742015-02-10 21:35:56 -0800401 uint_least32_t new_offset;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800402 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
403 if (new_bt) {
404 atomic_store_explicit(&current->left, new_offset, memory_order_release);
405 }
406 return new_bt;
407 }
408 } else {
409 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
410 if (right_offset != 0) {
411 current = to_prop_bt(&current->right);
412 } else {
413 if (!alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800414 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000415 }
416
Elliott Hughes9160ed92017-01-30 17:54:57 -0800417 uint_least32_t new_offset;
418 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
419 if (new_bt) {
420 atomic_store_explicit(&current->right, new_offset, memory_order_release);
421 }
422 return new_bt;
423 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000424 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800425 }
426}
427
428const prop_info* prop_area::find_property(prop_bt* const trie, const char* name, uint32_t namelen,
429 const char* value, uint32_t valuelen,
430 bool alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800431 if (!trie) return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800432
433 const char* remaining_name = name;
434 prop_bt* current = trie;
435 while (true) {
436 const char* sep = strchr(remaining_name, '.');
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800437 const bool want_subtree = (sep != nullptr);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800438 const uint32_t substr_size = (want_subtree) ? sep - remaining_name : strlen(remaining_name);
439
440 if (!substr_size) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800441 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800442 }
443
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800444 prop_bt* root = nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800445 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
446 if (children_offset != 0) {
447 root = to_prop_bt(&current->children);
448 } else if (alloc_if_needed) {
449 uint_least32_t new_offset;
450 root = new_prop_bt(remaining_name, substr_size, &new_offset);
451 if (root) {
452 atomic_store_explicit(&current->children, new_offset, memory_order_release);
453 }
454 }
455
456 if (!root) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800457 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800458 }
459
460 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
461 if (!current) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800462 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800463 }
464
465 if (!want_subtree) break;
466
467 remaining_name = sep + 1;
468 }
469
470 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
471 if (prop_offset != 0) {
472 return to_prop_info(&current->prop);
473 } else if (alloc_if_needed) {
474 uint_least32_t new_offset;
475 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
476 if (new_info) {
477 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
478 }
479
480 return new_info;
481 } else {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800482 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800483 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000484}
485
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000486class PropertyServiceConnection {
487 public:
488 PropertyServiceConnection() : last_error_(0) {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800489 socket_ = ::socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
490 if (socket_ == -1) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000491 last_error_ = errno;
492 return;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000493 }
494
495 const size_t namelen = strlen(property_service_socket);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000496 sockaddr_un addr;
497 memset(&addr, 0, sizeof(addr));
498 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
499 addr.sun_family = AF_LOCAL;
500 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000501
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800502 if (TEMP_FAILURE_RETRY(connect(socket_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
503 close(socket_);
504 socket_ = -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000505 last_error_ = errno;
506 }
507 }
508
509 bool IsValid() {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800510 return socket_ != -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000511 }
512
513 int GetLastError() {
514 return last_error_;
515 }
516
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000517 bool RecvInt32(int32_t* value) {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800518 int result = TEMP_FAILURE_RETRY(recv(socket_, value, sizeof(*value), MSG_WAITALL));
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000519 return CheckSendRecvResult(result, sizeof(*value));
520 }
521
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800522 int socket() {
523 return socket_;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000524 }
525
526 ~PropertyServiceConnection() {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800527 if (socket_ != -1) {
528 close(socket_);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000529 }
530 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800531
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000532 private:
533 bool CheckSendRecvResult(int result, int expected_len) {
534 if (result == -1) {
535 last_error_ = errno;
536 } else if (result != expected_len) {
537 last_error_ = -1;
538 } else {
539 last_error_ = 0;
540 }
541
542 return last_error_ == 0;
543 }
544
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800545 int socket_;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000546 int last_error_;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800547
548 friend class SocketWriter;
549};
550
551class SocketWriter {
552 public:
553 explicit SocketWriter(PropertyServiceConnection* connection)
554 : connection_(connection), iov_index_(0), uint_buf_index_(0)
555 {}
556
557 SocketWriter& WriteUint32(uint32_t value) {
558 CHECK(uint_buf_index_ < kUintBufSize);
559 CHECK(iov_index_ < kIovSize);
560 uint32_t* ptr = uint_buf_ + uint_buf_index_;
561 uint_buf_[uint_buf_index_++] = value;
562 iov_[iov_index_].iov_base = ptr;
563 iov_[iov_index_].iov_len = sizeof(*ptr);
564 ++iov_index_;
565 return *this;
566 }
567
568 SocketWriter& WriteString(const char* value) {
569 uint32_t valuelen = strlen(value);
570 WriteUint32(valuelen);
571 if (valuelen == 0) {
572 return *this;
573 }
574
575 CHECK(iov_index_ < kIovSize);
576 iov_[iov_index_].iov_base = const_cast<char*>(value);
577 iov_[iov_index_].iov_len = valuelen;
578 ++iov_index_;
579
580 return *this;
581 }
582
583 bool Send() {
584 if (!connection_->IsValid()) {
585 return false;
586 }
587
588 if (writev(connection_->socket(), iov_, iov_index_) == -1) {
589 connection_->last_error_ = errno;
590 return false;
591 }
592
593 iov_index_ = uint_buf_index_ = 0;
594 return true;
595 }
596
597 private:
598 static constexpr size_t kUintBufSize = 8;
599 static constexpr size_t kIovSize = 8;
600
601 PropertyServiceConnection* connection_;
602 iovec iov_[kIovSize];
603 size_t iov_index_;
604 uint32_t uint_buf_[kUintBufSize];
605 size_t uint_buf_index_;
606
607 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketWriter);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000608};
609
Elliott Hughesf8562c52017-01-26 16:48:57 -0800610struct prop_msg {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800611 unsigned cmd;
612 char name[PROP_NAME_MAX];
613 char value[PROP_VALUE_MAX];
Elliott Hughesf8562c52017-01-26 16:48:57 -0800614};
615
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000616static int send_prop_msg(const prop_msg* msg) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800617 PropertyServiceConnection connection;
618 if (!connection.IsValid()) {
619 return connection.GetLastError();
620 }
621
622 int result = -1;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800623 int s = connection.socket();
Elliott Hughes9160ed92017-01-30 17:54:57 -0800624
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800625 const int num_bytes = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
Elliott Hughes9160ed92017-01-30 17:54:57 -0800626 if (num_bytes == sizeof(prop_msg)) {
627 // We successfully wrote to the property server but now we
628 // wait for the property server to finish its work. It
629 // acknowledges its completion by closing the socket so we
630 // poll here (on nothing), waiting for the socket to close.
631 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
632 // once the socket closes. Out of paranoia we cap our poll
633 // at 250 ms.
634 pollfd pollfds[1];
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800635 pollfds[0].fd = s;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800636 pollfds[0].events = 0;
637 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
638 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
639 result = 0;
640 } else {
641 // Ignore the timeout and treat it like a success anyway.
642 // The init process is single-threaded and its property
643 // service is sometimes slow to respond (perhaps it's off
644 // starting a child process or something) and thus this
645 // times out and the caller thinks it failed, even though
646 // it's still getting around to it. So we fake it here,
647 // mostly for ctl.* properties, but we do try and wait 250
648 // ms so callers who do read-after-write can reliably see
649 // what they've written. Most of the time.
650 // TODO: fix the system properties design.
651 __libc_format_log(ANDROID_LOG_WARN, "libc",
652 "Property service has timed out while trying to set \"%s\" to \"%s\"",
653 msg->name, msg->value);
654 result = 0;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000655 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800656 }
Dimitry Ivanov489f58b2017-01-24 18:39:04 +0000657
Elliott Hughes9160ed92017-01-30 17:54:57 -0800658 return result;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000659}
660
Elliott Hughes9160ed92017-01-30 17:54:57 -0800661static void find_nth_fn(const prop_info* pi, void* ptr) {
662 find_nth_cookie* cookie = reinterpret_cast<find_nth_cookie*>(ptr);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000663
Elliott Hughes9160ed92017-01-30 17:54:57 -0800664 if (cookie->n == cookie->count) cookie->pi = pi;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000665
Elliott Hughes9160ed92017-01-30 17:54:57 -0800666 cookie->count++;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000667}
668
Elliott Hughes9160ed92017-01-30 17:54:57 -0800669bool prop_area::foreach_property(prop_bt* const trie,
670 void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
671 if (!trie) return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000672
Elliott Hughes9160ed92017-01-30 17:54:57 -0800673 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
674 if (left_offset != 0) {
675 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
676 if (err < 0) return false;
677 }
678 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
679 if (prop_offset != 0) {
680 prop_info* info = to_prop_info(&trie->prop);
681 if (!info) return false;
682 propfn(info, cookie);
683 }
684 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
685 if (children_offset != 0) {
686 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
687 if (err < 0) return false;
688 }
689 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
690 if (right_offset != 0) {
691 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
692 if (err < 0) return false;
693 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000694
Elliott Hughes9160ed92017-01-30 17:54:57 -0800695 return true;
Tom Cherry926ebe12015-09-23 15:34:40 -0700696}
697
Elliott Hughes9160ed92017-01-30 17:54:57 -0800698const prop_info* prop_area::find(const char* name) {
699 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
Tom Cherry926ebe12015-09-23 15:34:40 -0700700}
701
Elliott Hughes9160ed92017-01-30 17:54:57 -0800702bool prop_area::add(const char* name, unsigned int namelen, const char* value,
703 unsigned int valuelen) {
704 return find_property(root_node(), name, namelen, value, valuelen, true);
Tom Cherry926ebe12015-09-23 15:34:40 -0700705}
706
Elliott Hughes9160ed92017-01-30 17:54:57 -0800707bool prop_area::foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
708 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000709}
710
Tom Cherryb4171692015-12-09 15:48:15 -0800711class context_node {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800712 public:
713 context_node(context_node* next, const char* context, prop_area* pa)
714 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
715 lock_.init(false);
716 }
717 ~context_node() {
718 unmap();
719 free(context_);
720 }
721 bool open(bool access_rw, bool* fsetxattr_failed);
722 bool check_access_and_open();
723 void reset_access();
Tom Cherryb4171692015-12-09 15:48:15 -0800724
Elliott Hughes9160ed92017-01-30 17:54:57 -0800725 const char* context() const {
726 return context_;
727 }
728 prop_area* pa() {
729 return pa_;
730 }
Tom Cherryb4171692015-12-09 15:48:15 -0800731
Elliott Hughes9160ed92017-01-30 17:54:57 -0800732 context_node* next;
Tom Cherryb4171692015-12-09 15:48:15 -0800733
Elliott Hughes9160ed92017-01-30 17:54:57 -0800734 private:
735 bool check_access();
736 void unmap();
Tom Cherryb4171692015-12-09 15:48:15 -0800737
Elliott Hughes9160ed92017-01-30 17:54:57 -0800738 Lock lock_;
739 char* context_;
740 prop_area* pa_;
741 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700742};
743
744struct prefix_node {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800745 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
746 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
747 }
748 ~prefix_node() {
749 free(prefix);
750 }
751 char* prefix;
752 const size_t prefix_len;
753 context_node* context;
754 struct prefix_node* next;
Tom Cherry49a309f2015-09-23 16:09:47 -0700755};
756
757template <typename List, typename... Args>
758static inline void list_add(List** list, Args... args) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800759 *list = new List(*list, args...);
Tom Cherry49a309f2015-09-23 16:09:47 -0700760}
761
762static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800763 size_t prefix_len = strlen(prefix);
Tom Cherry49a309f2015-09-23 16:09:47 -0700764
Elliott Hughes9160ed92017-01-30 17:54:57 -0800765 auto next_list = list;
Tom Cherry49a309f2015-09-23 16:09:47 -0700766
Elliott Hughes9160ed92017-01-30 17:54:57 -0800767 while (*next_list) {
768 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
769 list_add(next_list, prefix, context);
770 return;
Tom Cherry49a309f2015-09-23 16:09:47 -0700771 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800772 next_list = &(*next_list)->next;
773 }
774 list_add(next_list, prefix, context);
Tom Cherry49a309f2015-09-23 16:09:47 -0700775}
776
777template <typename List, typename Func>
778static void list_foreach(List* list, Func func) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800779 while (list) {
780 func(list);
781 list = list->next;
782 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700783}
784
785template <typename List, typename Func>
786static List* list_find(List* list, Func func) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800787 while (list) {
788 if (func(list)) {
789 return list;
Tom Cherry49a309f2015-09-23 16:09:47 -0700790 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800791 list = list->next;
792 }
793 return nullptr;
Tom Cherry49a309f2015-09-23 16:09:47 -0700794}
795
796template <typename List>
797static void list_free(List** list) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800798 while (*list) {
799 auto old_list = *list;
800 *list = old_list->next;
801 delete old_list;
802 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700803}
804
805static prefix_node* prefixes = nullptr;
806static context_node* contexts = nullptr;
807
808/*
809 * pthread_mutex_lock() calls into system_properties in the case of contention.
810 * This creates a risk of dead lock if any system_properties functions
811 * use pthread locks after system_property initialization.
812 *
813 * For this reason, the below three functions use a bionic Lock and static
814 * allocation of memory for each filename.
815 */
816
Tom Cherryb4171692015-12-09 15:48:15 -0800817bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800818 lock_.lock();
819 if (pa_) {
Tom Cherryb4171692015-12-09 15:48:15 -0800820 lock_.unlock();
Elliott Hughes9160ed92017-01-30 17:54:57 -0800821 return true;
822 }
823
824 char filename[PROP_FILENAME_MAX];
825 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", property_filename, context_);
826 if (len < 0 || len > PROP_FILENAME_MAX) {
827 lock_.unlock();
828 return false;
829 }
830
831 if (access_rw) {
832 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
833 } else {
834 pa_ = map_prop_area(filename);
835 }
836 lock_.unlock();
837 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700838}
839
Tom Cherryb4171692015-12-09 15:48:15 -0800840bool context_node::check_access_and_open() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800841 if (!pa_ && !no_access_) {
842 if (!check_access() || !open(false, nullptr)) {
843 no_access_ = true;
Tom Cherryb4171692015-12-09 15:48:15 -0800844 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800845 }
846 return pa_;
Tom Cherryb4171692015-12-09 15:48:15 -0800847}
848
849void context_node::reset_access() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800850 if (!check_access()) {
851 unmap();
852 no_access_ = true;
853 } else {
854 no_access_ = false;
855 }
Tom Cherryb4171692015-12-09 15:48:15 -0800856}
857
858bool context_node::check_access() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800859 char filename[PROP_FILENAME_MAX];
860 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", property_filename, context_);
861 if (len < 0 || len > PROP_FILENAME_MAX) {
862 return false;
863 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700864
Elliott Hughes9160ed92017-01-30 17:54:57 -0800865 return access(filename, R_OK) == 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700866}
867
Tom Cherryb4171692015-12-09 15:48:15 -0800868void context_node::unmap() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800869 if (!pa_) {
870 return;
871 }
Tom Cherryb4171692015-12-09 15:48:15 -0800872
Elliott Hughes9160ed92017-01-30 17:54:57 -0800873 munmap(pa_, pa_size);
874 if (pa_ == __system_property_area__) {
875 __system_property_area__ = nullptr;
876 }
877 pa_ = nullptr;
Tom Cherryb4171692015-12-09 15:48:15 -0800878}
879
Tom Cherry49a309f2015-09-23 16:09:47 -0700880static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800881 char filename[PROP_FILENAME_MAX];
882 int len =
883 __libc_format_buffer(filename, sizeof(filename), "%s/properties_serial", property_filename);
884 if (len < 0 || len > PROP_FILENAME_MAX) {
885 __system_property_area__ = nullptr;
886 return false;
887 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700888
Elliott Hughes9160ed92017-01-30 17:54:57 -0800889 if (access_rw) {
890 __system_property_area__ =
891 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
892 } else {
893 __system_property_area__ = map_prop_area(filename);
894 }
895 return __system_property_area__;
Tom Cherry49a309f2015-09-23 16:09:47 -0700896}
897
898static prop_area* get_prop_area_for_name(const char* name) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800899 auto entry = list_find(prefixes, [name](prefix_node* l) {
900 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
901 });
902 if (!entry) {
903 return nullptr;
904 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700905
Elliott Hughes9160ed92017-01-30 17:54:57 -0800906 auto cnode = entry->context;
907 if (!cnode->pa()) {
908 /*
909 * We explicitly do not check no_access_ in this case because unlike the
910 * case of foreach(), we want to generate an selinux audit for each
911 * non-permitted property access in this function.
912 */
913 cnode->open(false, nullptr);
914 }
915 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700916}
917
918/*
919 * The below two functions are duplicated from label_support.c in libselinux.
920 * TODO: Find a location suitable for these functions such that both libc and
921 * libselinux can share a common source file.
922 */
923
924/*
925 * The read_spec_entries and read_spec_entry functions may be used to
926 * replace sscanf to read entries from spec files. The file and
927 * property services now use these.
928 */
929
930/* Read an entry from a spec file (e.g. file_contexts) */
Elliott Hughes9160ed92017-01-30 17:54:57 -0800931static inline int read_spec_entry(char** entry, char** ptr, int* len) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800932 *entry = nullptr;
933 char* tmp_buf = nullptr;
Tom Cherry49a309f2015-09-23 16:09:47 -0700934
Elliott Hughes9160ed92017-01-30 17:54:57 -0800935 while (isspace(**ptr) && **ptr != '\0') (*ptr)++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700936
Elliott Hughes9160ed92017-01-30 17:54:57 -0800937 tmp_buf = *ptr;
938 *len = 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700939
Elliott Hughes9160ed92017-01-30 17:54:57 -0800940 while (!isspace(**ptr) && **ptr != '\0') {
941 (*ptr)++;
942 (*len)++;
943 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700944
Elliott Hughes9160ed92017-01-30 17:54:57 -0800945 if (*len) {
946 *entry = strndup(tmp_buf, *len);
947 if (!*entry) return -1;
948 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700949
Elliott Hughes9160ed92017-01-30 17:54:57 -0800950 return 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700951}
952
953/*
954 * line_buf - Buffer containing the spec entries .
955 * num_args - The number of spec parameter entries to process.
956 * ... - A 'char **spec_entry' for each parameter.
957 * returns - The number of items processed.
958 *
959 * This function calls read_spec_entry() to do the actual string processing.
960 */
Elliott Hughes9160ed92017-01-30 17:54:57 -0800961static int read_spec_entries(char* line_buf, int num_args, ...) {
962 char **spec_entry, *buf_p;
963 int len, rc, items, entry_len = 0;
964 va_list ap;
Tom Cherry49a309f2015-09-23 16:09:47 -0700965
Elliott Hughes9160ed92017-01-30 17:54:57 -0800966 len = strlen(line_buf);
967 if (line_buf[len - 1] == '\n')
968 line_buf[len - 1] = '\0';
969 else
970 /* Handle case if line not \n terminated by bumping
971 * the len for the check below (as the line is NUL
972 * terminated by getline(3)) */
973 len++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700974
Elliott Hughes9160ed92017-01-30 17:54:57 -0800975 buf_p = line_buf;
976 while (isspace(*buf_p)) buf_p++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700977
Elliott Hughes9160ed92017-01-30 17:54:57 -0800978 /* Skip comment lines and empty lines. */
979 if (*buf_p == '#' || *buf_p == '\0') return 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700980
Elliott Hughes9160ed92017-01-30 17:54:57 -0800981 /* Process the spec file entries */
982 va_start(ap, num_args);
Tom Cherry49a309f2015-09-23 16:09:47 -0700983
Elliott Hughes9160ed92017-01-30 17:54:57 -0800984 items = 0;
985 while (items < num_args) {
986 spec_entry = va_arg(ap, char**);
Tom Cherry49a309f2015-09-23 16:09:47 -0700987
Elliott Hughes9160ed92017-01-30 17:54:57 -0800988 if (len - 1 == buf_p - line_buf) {
989 va_end(ap);
990 return items;
Tom Cherry49a309f2015-09-23 16:09:47 -0700991 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800992
993 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
994 if (rc < 0) {
995 va_end(ap);
996 return rc;
997 }
998 if (entry_len) items++;
999 }
1000 va_end(ap);
1001 return items;
Tom Cherry49a309f2015-09-23 16:09:47 -07001002}
1003
Elliott Hughes9160ed92017-01-30 17:54:57 -08001004static bool initialize_properties_from_file(const char* filename) {
1005 FILE* file = fopen(filename, "re");
1006 if (!file) {
1007 return false;
1008 }
1009
1010 char* buffer = nullptr;
1011 size_t line_len;
1012 char* prop_prefix = nullptr;
1013 char* context = nullptr;
1014
1015 while (getline(&buffer, &line_len, file) > 0) {
1016 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1017 if (items <= 0) {
1018 continue;
1019 }
1020 if (items == 1) {
1021 free(prop_prefix);
1022 continue;
1023 }
1024 /*
1025 * init uses ctl.* properties as an IPC mechanism and does not write them
1026 * to a property file, therefore we do not need to create property files
1027 * to store them.
1028 */
1029 if (!strncmp(prop_prefix, "ctl.", 4)) {
1030 free(prop_prefix);
1031 free(context);
1032 continue;
Tom Cherry49a309f2015-09-23 16:09:47 -07001033 }
1034
Elliott Hughes9160ed92017-01-30 17:54:57 -08001035 auto old_context =
1036 list_find(contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
1037 if (old_context) {
1038 list_add_after_len(&prefixes, prop_prefix, old_context);
1039 } else {
1040 list_add(&contexts, context, nullptr);
1041 list_add_after_len(&prefixes, prop_prefix, contexts);
Tom Cherry49a309f2015-09-23 16:09:47 -07001042 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001043 free(prop_prefix);
1044 free(context);
1045 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001046
Elliott Hughes9160ed92017-01-30 17:54:57 -08001047 free(buffer);
1048 fclose(file);
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001049
Elliott Hughes9160ed92017-01-30 17:54:57 -08001050 return true;
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001051}
1052
1053static bool initialize_properties() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001054 // If we do find /property_contexts, then this is being
1055 // run as part of the OTA updater on older release that had
1056 // /property_contexts - b/34370523
1057 if (initialize_properties_from_file("/property_contexts")) {
Tom Cherry49a309f2015-09-23 16:09:47 -07001058 return true;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001059 }
1060
Alex Klyubin176cf1f2017-03-08 13:16:03 -08001061 // Use property_contexts from /system & /vendor, fall back to those from /
1062 if (access("/system/etc/selinux/plat_property_contexts", R_OK) != -1) {
1063 if (!initialize_properties_from_file("/system/etc/selinux/plat_property_contexts")) {
1064 return false;
1065 }
1066 if (!initialize_properties_from_file("/vendor/etc/selinux/nonplat_property_contexts")) {
1067 return false;
1068 }
1069 } else {
1070 if (!initialize_properties_from_file("/plat_property_contexts")) {
1071 return false;
1072 }
1073 if (!initialize_properties_from_file("/nonplat_property_contexts")) {
1074 return false;
1075 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001076 }
1077
Elliott Hughes9160ed92017-01-30 17:54:57 -08001078 return true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001079}
1080
1081static bool is_dir(const char* pathname) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001082 struct stat info;
1083 if (stat(pathname, &info) == -1) {
1084 return false;
1085 }
1086 return S_ISDIR(info.st_mode);
Tom Cherry49a309f2015-09-23 16:09:47 -07001087}
1088
Tom Cherryb4171692015-12-09 15:48:15 -08001089static void free_and_unmap_contexts() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001090 list_free(&prefixes);
1091 list_free(&contexts);
1092 if (__system_property_area__) {
1093 munmap(__system_property_area__, pa_size);
1094 __system_property_area__ = nullptr;
1095 }
Tom Cherryb4171692015-12-09 15:48:15 -08001096}
1097
Elliott Hughes9160ed92017-01-30 17:54:57 -08001098int __system_properties_init() {
1099 if (initialized) {
1100 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001101 return 0;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001102 }
1103 if (is_dir(property_filename)) {
Tom Cherry49a309f2015-09-23 16:09:47 -07001104 if (!initialize_properties()) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001105 return -1;
Tom Cherry49a309f2015-09-23 16:09:47 -07001106 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001107 if (!map_system_property_area(false, nullptr)) {
1108 free_and_unmap_contexts();
1109 return -1;
Tom Cherry49a309f2015-09-23 16:09:47 -07001110 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001111 } else {
1112 __system_property_area__ = map_prop_area(property_filename);
Tom Cherry6ed51c02015-12-04 11:34:42 -08001113 if (!__system_property_area__) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001114 return -1;
Tom Cherry6ed51c02015-12-04 11:34:42 -08001115 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001116 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1117 list_add_after_len(&prefixes, "*", contexts);
1118 }
1119 initialized = true;
1120 return 0;
1121}
Tom Cherry6ed51c02015-12-04 11:34:42 -08001122
Elliott Hughes9160ed92017-01-30 17:54:57 -08001123int __system_property_set_filename(const char* filename) {
1124 size_t len = strlen(filename);
1125 if (len >= sizeof(property_filename)) return -1;
1126
1127 strcpy(property_filename, filename);
1128 return 0;
1129}
1130
1131int __system_property_area_init() {
1132 free_and_unmap_contexts();
1133 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
1134 if (!initialize_properties()) {
1135 return -1;
1136 }
1137 bool open_failed = false;
1138 bool fsetxattr_failed = false;
1139 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1140 if (!l->open(true, &fsetxattr_failed)) {
1141 open_failed = true;
Tom Cherry926ebe12015-09-23 15:34:40 -07001142 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001143 });
1144 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1145 free_and_unmap_contexts();
1146 return -1;
1147 }
1148 initialized = true;
1149 return fsetxattr_failed ? -2 : 0;
1150}
Tom Cherry926ebe12015-09-23 15:34:40 -07001151
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001152uint32_t __system_property_area_serial() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001153 prop_area* pa = __system_property_area__;
1154 if (!pa) {
1155 return -1;
1156 }
1157 // Make sure this read fulfilled before __system_property_serial
1158 return atomic_load_explicit(pa->serial(), memory_order_acquire);
1159}
1160
1161const prop_info* __system_property_find(const char* name) {
1162 if (!__system_property_area__) {
1163 return nullptr;
1164 }
1165
1166 prop_area* pa = get_prop_area_for_name(name);
1167 if (!pa) {
1168 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
1169 return nullptr;
1170 }
1171
1172 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001173}
1174
Hans Boehm1e8587a2014-08-19 14:07:55 -07001175// The C11 standard doesn't allow atomic loads from const fields,
1176// though C++11 does. Fudge it until standards get straightened out.
Elliott Hughes9160ed92017-01-30 17:54:57 -08001177static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, memory_order mo) {
1178 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1179 return atomic_load_explicit(non_const_s, mo);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001180}
1181
Elliott Hughes9160ed92017-01-30 17:54:57 -08001182int __system_property_read(const prop_info* pi, char* name, char* value) {
1183 while (true) {
1184 uint32_t serial = __system_property_serial(pi); // acquire semantics
1185 size_t len = SERIAL_VALUE_LEN(serial);
1186 memcpy(value, pi->value, len + 1);
1187 // TODO: Fix the synchronization scheme here.
1188 // There is no fully supported way to implement this kind
1189 // of synchronization in C++11, since the memcpy races with
1190 // updates to pi, and the data being accessed is not atomic.
1191 // The following fence is unintuitive, but would be the
1192 // correct one if memcpy used memory_order_relaxed atomic accesses.
1193 // In practice it seems unlikely that the generated code would
1194 // would be any different, so this should be OK.
1195 atomic_thread_fence(memory_order_acquire);
1196 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1197 if (name != nullptr) {
1198 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1199 if (namelen >= PROP_NAME_MAX) {
1200 __libc_format_log(ANDROID_LOG_ERROR, "libc",
1201 "The property name length for \"%s\" is >= %d;"
1202 " please use __system_property_read_callback"
1203 " to read this property. (the name is truncated to \"%s\")",
1204 pi->name, PROP_NAME_MAX - 1, name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001205 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001206 }
1207 return len;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001208 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001209 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001210}
1211
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001212void __system_property_read_callback(const prop_info* pi,
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001213 void (*callback)(void* cookie,
1214 const char* name,
1215 const char* value,
1216 uint32_t serial),
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001217 void* cookie) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001218 while (true) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001219 uint32_t serial = __system_property_serial(pi); // acquire semantics
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001220 size_t len = SERIAL_VALUE_LEN(serial);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001221 char value_buf[len + 1];
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001222
1223 memcpy(value_buf, pi->value, len);
1224 value_buf[len] = '\0';
1225
1226 // TODO: see todo in __system_property_read function
1227 atomic_thread_fence(memory_order_acquire);
1228 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001229 callback(cookie, pi->name, value_buf, serial);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001230 return;
1231 }
1232 }
1233}
1234
Elliott Hughes9160ed92017-01-30 17:54:57 -08001235int __system_property_get(const char* name, char* value) {
1236 const prop_info* pi = __system_property_find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001237
Elliott Hughes9160ed92017-01-30 17:54:57 -08001238 if (pi != 0) {
1239 return __system_property_read(pi, nullptr, value);
1240 } else {
1241 value[0] = 0;
1242 return 0;
1243 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001244}
1245
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001246static constexpr uint32_t kProtocolVersion1 = 1;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001247static constexpr uint32_t kProtocolVersion2 = 2; // current
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001248
1249static atomic_uint_least32_t g_propservice_protocol_version = 0;
1250
1251static void detect_protocol_version() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001252 char value[PROP_VALUE_MAX];
1253 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1254 g_propservice_protocol_version = kProtocolVersion1;
1255 __libc_format_log(ANDROID_LOG_WARN, "libc",
1256 "Using old property service protocol (\"%s\" is not set)",
1257 kServiceVersionPropertyName);
1258 } else {
1259 uint32_t version = static_cast<uint32_t>(atoll(value));
1260 if (version >= kProtocolVersion2) {
1261 g_propservice_protocol_version = kProtocolVersion2;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001262 } else {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001263 __libc_format_log(ANDROID_LOG_WARN, "libc",
1264 "Using old property service protocol (\"%s\"=\"%s\")",
1265 kServiceVersionPropertyName, value);
1266 g_propservice_protocol_version = kProtocolVersion1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001267 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001268 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001269}
1270
1271int __system_property_set(const char* key, const char* value) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001272 if (key == nullptr) return -1;
1273 if (value == nullptr) value = "";
1274 if (strlen(value) >= PROP_VALUE_MAX) return -1;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001275
Elliott Hughes9160ed92017-01-30 17:54:57 -08001276 if (g_propservice_protocol_version == 0) {
1277 detect_protocol_version();
1278 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001279
Elliott Hughes9160ed92017-01-30 17:54:57 -08001280 if (g_propservice_protocol_version == kProtocolVersion1) {
1281 // Old protocol does not support long names
1282 if (strlen(key) >= PROP_NAME_MAX) return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001283
Elliott Hughes9160ed92017-01-30 17:54:57 -08001284 prop_msg msg;
1285 memset(&msg, 0, sizeof msg);
1286 msg.cmd = PROP_MSG_SETPROP;
1287 strlcpy(msg.name, key, sizeof msg.name);
1288 strlcpy(msg.value, value, sizeof msg.value);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001289
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001290 return send_prop_msg(&msg);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001291 } else {
1292 // Use proper protocol
1293 PropertyServiceConnection connection;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001294 if (!connection.IsValid()) {
1295 errno = connection.GetLastError();
1296 __libc_format_log(ANDROID_LOG_WARN,
1297 "libc",
1298 "Unable to set property \"%s\" to \"%s\": connection failed; errno=%d (%s)",
1299 key,
1300 value,
1301 errno,
1302 strerror(errno));
1303 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001304 }
1305
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001306 SocketWriter writer(&connection);
1307 if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) {
1308 errno = connection.GetLastError();
1309 __libc_format_log(ANDROID_LOG_WARN,
1310 "libc",
1311 "Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)",
1312 key,
1313 value,
1314 errno,
1315 strerror(errno));
1316 return -1;
1317 }
1318
1319 int result = -1;
1320 if (!connection.RecvInt32(&result)) {
1321 errno = connection.GetLastError();
1322 __libc_format_log(ANDROID_LOG_WARN,
1323 "libc",
1324 "Unable to set property \"%s\" to \"%s\": recv failed; errno=%d (%s)",
1325 key,
1326 value,
1327 errno,
1328 strerror(errno));
1329 return -1;
1330 }
1331
1332 if (result != PROP_SUCCESS) {
1333 __libc_format_log(ANDROID_LOG_WARN,
1334 "libc",
1335 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1336 key,
1337 value,
1338 result);
1339 return -1;
1340 }
1341
1342 return 0;
1343 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001344}
1345
Elliott Hughes9160ed92017-01-30 17:54:57 -08001346int __system_property_update(prop_info* pi, const char* value, unsigned int len) {
1347 if (len >= PROP_VALUE_MAX) {
1348 return -1;
1349 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001350
Elliott Hughes9160ed92017-01-30 17:54:57 -08001351 prop_area* pa = __system_property_area__;
Tom Cherry6ed51c02015-12-04 11:34:42 -08001352
Elliott Hughes9160ed92017-01-30 17:54:57 -08001353 if (!pa) {
1354 return -1;
1355 }
Tom Cherry6ed51c02015-12-04 11:34:42 -08001356
Elliott Hughes9160ed92017-01-30 17:54:57 -08001357 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1358 serial |= 1;
1359 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1360 // The memcpy call here also races. Again pretend it
1361 // used memory_order_relaxed atomics, and use the analogous
1362 // counterintuitive fence.
1363 atomic_thread_fence(memory_order_release);
1364 strlcpy(pi->value, value, len + 1);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001365
Elliott Hughes9160ed92017-01-30 17:54:57 -08001366 atomic_store_explicit(&pi->serial, (len << 24) | ((serial + 1) & 0xffffff), memory_order_release);
1367 __futex_wake(&pi->serial, INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001368
Elliott Hughes9160ed92017-01-30 17:54:57 -08001369 atomic_store_explicit(pa->serial(), atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
1370 memory_order_release);
1371 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001372
Elliott Hughes9160ed92017-01-30 17:54:57 -08001373 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001374}
jiaguo879d3302014-03-13 17:39:58 +08001375
Elliott Hughes9160ed92017-01-30 17:54:57 -08001376int __system_property_add(const char* name, unsigned int namelen, const char* value,
1377 unsigned int valuelen) {
1378 if (valuelen >= PROP_VALUE_MAX) {
1379 return -1;
1380 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001381
Elliott Hughes9160ed92017-01-30 17:54:57 -08001382 if (namelen < 1) {
1383 return -1;
1384 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001385
Elliott Hughes9160ed92017-01-30 17:54:57 -08001386 if (!__system_property_area__) {
1387 return -1;
1388 }
Tom Cherry6ed51c02015-12-04 11:34:42 -08001389
Elliott Hughes9160ed92017-01-30 17:54:57 -08001390 prop_area* pa = get_prop_area_for_name(name);
Tom Cherry49a309f2015-09-23 16:09:47 -07001391
Elliott Hughes9160ed92017-01-30 17:54:57 -08001392 if (!pa) {
1393 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
1394 return -1;
1395 }
Tom Cherry926ebe12015-09-23 15:34:40 -07001396
Elliott Hughes9160ed92017-01-30 17:54:57 -08001397 bool ret = pa->add(name, namelen, value, valuelen);
1398 if (!ret) {
1399 return -1;
1400 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001401
Elliott Hughes9160ed92017-01-30 17:54:57 -08001402 // There is only a single mutator, but we want to make sure that
1403 // updates are visible to a reader waiting for the update.
1404 atomic_store_explicit(
1405 __system_property_area__->serial(),
1406 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
1407 memory_order_release);
1408 __futex_wake(__system_property_area__->serial(), INT32_MAX);
1409 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001410}
1411
Hans Boehm30214b92014-07-31 15:53:22 -07001412// Wait for non-locked serial, and retrieve it with acquire semantics.
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001413uint32_t __system_property_serial(const prop_info* pi) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001414 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
1415 while (SERIAL_DIRTY(serial)) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001416 __futex_wait(const_cast<_Atomic(uint_least32_t)*>(&pi->serial), serial, nullptr);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001417 serial = load_const_atomic(&pi->serial, memory_order_acquire);
1418 }
1419 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001420}
1421
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001422uint32_t __system_property_wait_any(uint32_t old_serial) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001423 uint32_t new_serial;
Elliott Hughes40c885a2017-02-16 17:13:04 -08001424 __system_property_wait(nullptr, old_serial, &new_serial, nullptr);
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001425 return new_serial;
1426}
Elliott Hughes9160ed92017-01-30 17:54:57 -08001427
Elliott Hughes40c885a2017-02-16 17:13:04 -08001428bool __system_property_wait(const prop_info* pi,
1429 uint32_t old_serial,
1430 uint32_t* new_serial_ptr,
1431 const timespec* relative_timeout) {
1432 // Are we waiting on the global serial or a specific serial?
1433 atomic_uint_least32_t* serial_ptr;
1434 if (pi == nullptr) {
1435 if (__system_property_area__ == nullptr) return -1;
1436 serial_ptr = __system_property_area__->serial();
1437 } else {
1438 serial_ptr = const_cast<atomic_uint_least32_t*>(&pi->serial);
1439 }
1440
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001441 uint32_t new_serial;
1442 do {
Elliott Hughes40c885a2017-02-16 17:13:04 -08001443 int rc;
1444 if ((rc = __futex_wait(serial_ptr, old_serial, relative_timeout)) != 0 && rc == -ETIMEDOUT) {
1445 return false;
1446 }
1447 new_serial = load_const_atomic(serial_ptr, memory_order_acquire);
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001448 } while (new_serial == old_serial);
Elliott Hughes40c885a2017-02-16 17:13:04 -08001449
1450 *new_serial_ptr = new_serial;
1451 return true;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001452}
1453
1454const prop_info* __system_property_find_nth(unsigned n) {
1455 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
1456 __libc_fatal(
1457 "__system_property_find_nth is not supported since Android O,"
1458 " please use __system_property_foreach instead.");
1459 }
1460
1461 find_nth_cookie cookie(n);
1462
1463 const int err = __system_property_foreach(find_nth_fn, &cookie);
1464 if (err < 0) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -08001465 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001466 }
1467
1468 return cookie.pi;
1469}
1470
1471int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
1472 if (!__system_property_area__) {
1473 return -1;
1474 }
1475
1476 list_foreach(contexts, [propfn, cookie](context_node* l) {
1477 if (l->check_access_and_open()) {
1478 l->pa()->foreach (propfn, cookie);
1479 }
1480 });
1481 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001482}