blob: 9fb03acce876c0a5314e6b9d007154d5e8b5907b [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 Hughes7ade61c2017-04-11 13:38:36 -070057#include "private/ErrnoRestorer.h"
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070058#include "private/bionic_futex.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070059#include "private/bionic_lock.h"
Elliott Hughes8eac9af2014-05-09 19:12:08 -070060#include "private/bionic_macros.h"
Dimitry Ivanov581b9f62017-01-09 11:05:52 -080061#include "private/bionic_sdk_version.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070062#include "private/libc_logging.h"
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000063
Elliott Hughesf8562c52017-01-26 16:48:57 -080064static constexpr int PROP_FILENAME_MAX = 1024;
65
66static constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250;
67static constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab;
68
69static constexpr size_t PA_SIZE = 128 * 1024;
70
Elliott Hughes9160ed92017-01-30 17:54:57 -080071#define SERIAL_DIRTY(serial) ((serial)&1)
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000072#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
Dimitry Ivanov489f58b2017-01-24 18:39:04 +000073
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000074static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
75static const char* kServiceVersionPropertyName = "ro.property_service.version";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000076
77/*
78 * Properties are stored in a hybrid trie/binary tree structure.
79 * Each property's name is delimited at '.' characters, and the tokens are put
80 * into a trie structure. Siblings at each level of the trie are stored in a
81 * binary tree. For instance, "ro.secure"="1" could be stored as follows:
82 *
83 * +-----+ children +----+ children +--------+
84 * | |-------------->| ro |-------------->| secure |
85 * +-----+ +----+ +--------+
86 * / \ / |
87 * left / \ right left / | prop +===========+
88 * v v v +-------->| ro.secure |
89 * +-----+ +-----+ +-----+ +-----------+
90 * | net | | sys | | com | | 1 |
91 * +-----+ +-----+ +-----+ +===========+
92 */
93
94// Represents a node in the trie.
95struct prop_bt {
Elliott Hughes9160ed92017-01-30 17:54:57 -080096 uint32_t namelen;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000097
Elliott Hughes9160ed92017-01-30 17:54:57 -080098 // The property trie is updated only by the init process (single threaded) which provides
99 // property service. And it can be read by multiple threads at the same time.
100 // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the
101 // left, right, children "pointers" in the trie node. To make sure readers who see the
102 // change of "pointers" can also notice the change of prop_bt structure contents pointed by
103 // the "pointers", we always use release-consume ordering pair when accessing these "pointers".
Yabin Cuib8ce4742015-02-10 21:35:56 -0800104
Elliott Hughes9160ed92017-01-30 17:54:57 -0800105 // prop "points" to prop_info structure if there is a propery associated with the trie node.
106 // Its situation is similar to the left, right, children "pointers". So we use
107 // atomic_uint_least32_t and release-consume ordering to protect it as well.
Yabin Cuib8ce4742015-02-10 21:35:56 -0800108
Elliott Hughes9160ed92017-01-30 17:54:57 -0800109 // We should also avoid rereading these fields redundantly, since not
110 // all processor implementations ensure that multiple loads from the
111 // same field are carried out in the right order.
112 atomic_uint_least32_t prop;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000113
Elliott Hughes9160ed92017-01-30 17:54:57 -0800114 atomic_uint_least32_t left;
115 atomic_uint_least32_t right;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000116
Elliott Hughes9160ed92017-01-30 17:54:57 -0800117 atomic_uint_least32_t children;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000118
Elliott Hughes9160ed92017-01-30 17:54:57 -0800119 char name[0];
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000120
Elliott Hughes9160ed92017-01-30 17:54:57 -0800121 prop_bt(const char* name, const uint32_t name_length) {
122 this->namelen = name_length;
123 memcpy(this->name, name, name_length);
124 this->name[name_length] = '\0';
125 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000126
Elliott Hughes9160ed92017-01-30 17:54:57 -0800127 private:
128 DISALLOW_COPY_AND_ASSIGN(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000129};
130
Tom Cherry926ebe12015-09-23 15:34:40 -0700131class prop_area {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800132 public:
133 prop_area(const uint32_t magic, const uint32_t version) : magic_(magic), version_(version) {
134 atomic_init(&serial_, 0);
135 memset(reserved_, 0, sizeof(reserved_));
136 // Allocate enough space for the root node.
137 bytes_used_ = sizeof(prop_bt);
138 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000139
Elliott Hughes9160ed92017-01-30 17:54:57 -0800140 const prop_info* find(const char* name);
141 bool add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000142
Elliott Hughes9160ed92017-01-30 17:54:57 -0800143 bool foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie);
Tom Cherry926ebe12015-09-23 15:34:40 -0700144
Elliott Hughes9160ed92017-01-30 17:54:57 -0800145 atomic_uint_least32_t* serial() {
146 return &serial_;
147 }
148 uint32_t magic() const {
149 return magic_;
150 }
151 uint32_t version() const {
152 return version_;
153 }
Tom Cherry926ebe12015-09-23 15:34:40 -0700154
Elliott Hughes9160ed92017-01-30 17:54:57 -0800155 private:
156 void* allocate_obj(const size_t size, uint_least32_t* const off);
157 prop_bt* new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off);
158 prop_info* new_prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen,
159 uint_least32_t* const off);
160 void* to_prop_obj(uint_least32_t off);
161 prop_bt* to_prop_bt(atomic_uint_least32_t* off_p);
162 prop_info* to_prop_info(atomic_uint_least32_t* off_p);
Tom Cherry926ebe12015-09-23 15:34:40 -0700163
Elliott Hughes9160ed92017-01-30 17:54:57 -0800164 prop_bt* root_node();
Tom Cherry926ebe12015-09-23 15:34:40 -0700165
Elliott Hughes9160ed92017-01-30 17:54:57 -0800166 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 -0700167
Elliott Hughes9160ed92017-01-30 17:54:57 -0800168 const prop_info* find_property(prop_bt* const trie, const char* name, uint32_t namelen,
169 const char* value, uint32_t valuelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700170
Elliott Hughes9160ed92017-01-30 17:54:57 -0800171 bool foreach_property(prop_bt* const trie, void (*propfn)(const prop_info* pi, void* cookie),
172 void* cookie);
Tom Cherry926ebe12015-09-23 15:34:40 -0700173
Elliott Hughes9160ed92017-01-30 17:54:57 -0800174 uint32_t bytes_used_;
175 atomic_uint_least32_t serial_;
176 uint32_t magic_;
177 uint32_t version_;
178 uint32_t reserved_[28];
179 char data_[0];
Tom Cherry926ebe12015-09-23 15:34:40 -0700180
Elliott Hughes9160ed92017-01-30 17:54:57 -0800181 DISALLOW_COPY_AND_ASSIGN(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000182};
183
184struct prop_info {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800185 atomic_uint_least32_t serial;
186 // we need to keep this buffer around because the property
187 // value can be modified whereas name is constant.
188 char value[PROP_VALUE_MAX];
189 char name[0];
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000190
Elliott Hughes9160ed92017-01-30 17:54:57 -0800191 prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen) {
192 memcpy(this->name, name, namelen);
193 this->name[namelen] = '\0';
194 atomic_init(&this->serial, valuelen << 24);
195 memcpy(this->value, value, valuelen);
196 this->value[valuelen] = '\0';
197 }
198
199 private:
200 DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000201};
202
Elliott Hughesf8562c52017-01-26 16:48:57 -0800203// 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 -0800204prop_area* __system_property_area__ = nullptr;
205
Tom Cherry49a309f2015-09-23 16:09:47 -0700206static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000207static size_t pa_data_size;
208static size_t pa_size;
Tom Cherryb4171692015-12-09 15:48:15 -0800209static bool initialized = false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000210
Tom Cherry49a309f2015-09-23 16:09:47 -0700211static prop_area* map_prop_area_rw(const char* filename, const char* context,
212 bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800213 /* dev is a tmpfs that we can use to carve a shared workspace
214 * out of, so let's do that...
215 */
216 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000217
Elliott Hughes9160ed92017-01-30 17:54:57 -0800218 if (fd < 0) {
219 if (errno == EACCES) {
220 /* for consistency with the case where the process has already
221 * mapped the page in and segfaults when trying to write to it
222 */
223 abort();
Tom Cherry49a309f2015-09-23 16:09:47 -0700224 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800225 return nullptr;
226 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700227
Elliott Hughes9160ed92017-01-30 17:54:57 -0800228 if (context) {
229 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
230 __libc_format_log(ANDROID_LOG_ERROR, "libc",
231 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
232 /*
233 * fsetxattr() will fail during system properties tests due to selinux policy.
234 * We do not want to create a custom policy for the tester, so we will continue in
235 * this function but set a flag that an error has occurred.
236 * Init, which is the only daemon that should ever call this function will abort
237 * when this error occurs.
238 * Otherwise, the tester will ignore it and continue, albeit without any selinux
239 * property separation.
240 */
241 if (fsetxattr_failed) {
242 *fsetxattr_failed = true;
243 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000244 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800245 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000246
Elliott Hughes9160ed92017-01-30 17:54:57 -0800247 if (ftruncate(fd, PA_SIZE) < 0) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000248 close(fd);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800249 return nullptr;
250 }
251
252 pa_size = PA_SIZE;
253 pa_data_size = pa_size - sizeof(prop_area);
254
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800255 void* const memory_area = mmap(nullptr, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800256 if (memory_area == MAP_FAILED) {
257 close(fd);
258 return nullptr;
259 }
260
261 prop_area* pa = new (memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
262
263 close(fd);
264 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000265}
266
Tom Cherry49a309f2015-09-23 16:09:47 -0700267static prop_area* map_fd_ro(const int fd) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800268 struct stat fd_stat;
269 if (fstat(fd, &fd_stat) < 0) {
270 return nullptr;
271 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000272
Elliott Hughes9160ed92017-01-30 17:54:57 -0800273 if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0) ||
274 ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) ||
275 (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area)))) {
276 return nullptr;
277 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000278
Elliott Hughes9160ed92017-01-30 17:54:57 -0800279 pa_size = fd_stat.st_size;
280 pa_data_size = pa_size - sizeof(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000281
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800282 void* const map_result = mmap(nullptr, pa_size, PROT_READ, MAP_SHARED, fd, 0);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800283 if (map_result == MAP_FAILED) {
284 return nullptr;
285 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000286
Elliott Hughes9160ed92017-01-30 17:54:57 -0800287 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
288 if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) {
289 munmap(pa, pa_size);
290 return nullptr;
291 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000292
Elliott Hughes9160ed92017-01-30 17:54:57 -0800293 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000294}
295
Elliott Hughesf8562c52017-01-26 16:48:57 -0800296static prop_area* map_prop_area(const char* filename) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800297 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
298 if (fd == -1) return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000299
Elliott Hughes9160ed92017-01-30 17:54:57 -0800300 prop_area* map_result = map_fd_ro(fd);
301 close(fd);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000302
Elliott Hughes9160ed92017-01-30 17:54:57 -0800303 return map_result;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000304}
305
Elliott Hughes9160ed92017-01-30 17:54:57 -0800306void* prop_area::allocate_obj(const size_t size, uint_least32_t* const off) {
307 const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t));
308 if (bytes_used_ + aligned > pa_data_size) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800309 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800310 }
311
312 *off = bytes_used_;
313 bytes_used_ += aligned;
314 return data_ + *off;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000315}
316
Elliott Hughes9160ed92017-01-30 17:54:57 -0800317prop_bt* prop_area::new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off) {
318 uint_least32_t new_offset;
319 void* const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800320 if (p != nullptr) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800321 prop_bt* bt = new (p) prop_bt(name, namelen);
322 *off = new_offset;
323 return bt;
324 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000325
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800326 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000327}
328
Elliott Hughes9160ed92017-01-30 17:54:57 -0800329prop_info* prop_area::new_prop_info(const char* name, uint32_t namelen, const char* value,
330 uint32_t valuelen, uint_least32_t* const off) {
331 uint_least32_t new_offset;
332 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800333 if (p != nullptr) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800334 prop_info* info = new (p) prop_info(name, namelen, value, valuelen);
335 *off = new_offset;
336 return info;
337 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000338
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800339 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000340}
341
Elliott Hughes9160ed92017-01-30 17:54:57 -0800342void* prop_area::to_prop_obj(uint_least32_t off) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800343 if (off > pa_data_size) return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800344
345 return (data_ + off);
346}
347
348inline prop_bt* prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800349 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
350 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
351}
352
Elliott Hughes9160ed92017-01-30 17:54:57 -0800353inline prop_info* prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800354 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
355 return reinterpret_cast<prop_info*>(to_prop_obj(off));
356}
357
Elliott Hughes9160ed92017-01-30 17:54:57 -0800358inline prop_bt* prop_area::root_node() {
359 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000360}
361
Elliott Hughes9160ed92017-01-30 17:54:57 -0800362static int cmp_prop_name(const char* one, uint32_t one_len, const char* two, uint32_t two_len) {
363 if (one_len < two_len)
364 return -1;
365 else if (one_len > two_len)
366 return 1;
367 else
368 return strncmp(one, two, one_len);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000369}
370
Elliott Hughes9160ed92017-01-30 17:54:57 -0800371prop_bt* prop_area::find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen,
372 bool alloc_if_needed) {
373 prop_bt* current = bt;
374 while (true) {
375 if (!current) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800376 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000377 }
378
Elliott Hughes9160ed92017-01-30 17:54:57 -0800379 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
380 if (ret == 0) {
381 return current;
382 }
383
384 if (ret < 0) {
385 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
386 if (left_offset != 0) {
387 current = to_prop_bt(&current->left);
388 } else {
389 if (!alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800390 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800391 }
392
Yabin Cuib8ce4742015-02-10 21:35:56 -0800393 uint_least32_t new_offset;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800394 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
395 if (new_bt) {
396 atomic_store_explicit(&current->left, new_offset, memory_order_release);
397 }
398 return new_bt;
399 }
400 } else {
401 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
402 if (right_offset != 0) {
403 current = to_prop_bt(&current->right);
404 } else {
405 if (!alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800406 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000407 }
408
Elliott Hughes9160ed92017-01-30 17:54:57 -0800409 uint_least32_t new_offset;
410 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
411 if (new_bt) {
412 atomic_store_explicit(&current->right, new_offset, memory_order_release);
413 }
414 return new_bt;
415 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000416 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800417 }
418}
419
420const prop_info* prop_area::find_property(prop_bt* const trie, const char* name, uint32_t namelen,
421 const char* value, uint32_t valuelen,
422 bool alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800423 if (!trie) return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800424
425 const char* remaining_name = name;
426 prop_bt* current = trie;
427 while (true) {
428 const char* sep = strchr(remaining_name, '.');
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800429 const bool want_subtree = (sep != nullptr);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800430 const uint32_t substr_size = (want_subtree) ? sep - remaining_name : strlen(remaining_name);
431
432 if (!substr_size) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800433 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800434 }
435
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800436 prop_bt* root = nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800437 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
438 if (children_offset != 0) {
439 root = to_prop_bt(&current->children);
440 } else if (alloc_if_needed) {
441 uint_least32_t new_offset;
442 root = new_prop_bt(remaining_name, substr_size, &new_offset);
443 if (root) {
444 atomic_store_explicit(&current->children, new_offset, memory_order_release);
445 }
446 }
447
448 if (!root) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800449 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800450 }
451
452 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
453 if (!current) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800454 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800455 }
456
457 if (!want_subtree) break;
458
459 remaining_name = sep + 1;
460 }
461
462 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
463 if (prop_offset != 0) {
464 return to_prop_info(&current->prop);
465 } else if (alloc_if_needed) {
466 uint_least32_t new_offset;
467 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
468 if (new_info) {
469 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
470 }
471
472 return new_info;
473 } else {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800474 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800475 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000476}
477
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000478class PropertyServiceConnection {
479 public:
480 PropertyServiceConnection() : last_error_(0) {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800481 socket_ = ::socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
482 if (socket_ == -1) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000483 last_error_ = errno;
484 return;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000485 }
486
487 const size_t namelen = strlen(property_service_socket);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000488 sockaddr_un addr;
489 memset(&addr, 0, sizeof(addr));
490 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
491 addr.sun_family = AF_LOCAL;
492 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000493
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800494 if (TEMP_FAILURE_RETRY(connect(socket_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
Tom Cherrybe4232b2017-04-14 13:45:20 -0700495 last_error_ = errno;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800496 close(socket_);
497 socket_ = -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000498 }
499 }
500
501 bool IsValid() {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800502 return socket_ != -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000503 }
504
505 int GetLastError() {
506 return last_error_;
507 }
508
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000509 bool RecvInt32(int32_t* value) {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800510 int result = TEMP_FAILURE_RETRY(recv(socket_, value, sizeof(*value), MSG_WAITALL));
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000511 return CheckSendRecvResult(result, sizeof(*value));
512 }
513
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800514 int socket() {
515 return socket_;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000516 }
517
518 ~PropertyServiceConnection() {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800519 if (socket_ != -1) {
520 close(socket_);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000521 }
522 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800523
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000524 private:
525 bool CheckSendRecvResult(int result, int expected_len) {
526 if (result == -1) {
527 last_error_ = errno;
528 } else if (result != expected_len) {
529 last_error_ = -1;
530 } else {
531 last_error_ = 0;
532 }
533
534 return last_error_ == 0;
535 }
536
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800537 int socket_;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000538 int last_error_;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800539
540 friend class SocketWriter;
541};
542
543class SocketWriter {
544 public:
545 explicit SocketWriter(PropertyServiceConnection* connection)
546 : connection_(connection), iov_index_(0), uint_buf_index_(0)
547 {}
548
549 SocketWriter& WriteUint32(uint32_t value) {
550 CHECK(uint_buf_index_ < kUintBufSize);
551 CHECK(iov_index_ < kIovSize);
552 uint32_t* ptr = uint_buf_ + uint_buf_index_;
553 uint_buf_[uint_buf_index_++] = value;
554 iov_[iov_index_].iov_base = ptr;
555 iov_[iov_index_].iov_len = sizeof(*ptr);
556 ++iov_index_;
557 return *this;
558 }
559
560 SocketWriter& WriteString(const char* value) {
561 uint32_t valuelen = strlen(value);
562 WriteUint32(valuelen);
563 if (valuelen == 0) {
564 return *this;
565 }
566
567 CHECK(iov_index_ < kIovSize);
568 iov_[iov_index_].iov_base = const_cast<char*>(value);
569 iov_[iov_index_].iov_len = valuelen;
570 ++iov_index_;
571
572 return *this;
573 }
574
575 bool Send() {
576 if (!connection_->IsValid()) {
577 return false;
578 }
579
580 if (writev(connection_->socket(), iov_, iov_index_) == -1) {
581 connection_->last_error_ = errno;
582 return false;
583 }
584
585 iov_index_ = uint_buf_index_ = 0;
586 return true;
587 }
588
589 private:
590 static constexpr size_t kUintBufSize = 8;
591 static constexpr size_t kIovSize = 8;
592
593 PropertyServiceConnection* connection_;
594 iovec iov_[kIovSize];
595 size_t iov_index_;
596 uint32_t uint_buf_[kUintBufSize];
597 size_t uint_buf_index_;
598
599 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketWriter);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000600};
601
Elliott Hughesf8562c52017-01-26 16:48:57 -0800602struct prop_msg {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800603 unsigned cmd;
604 char name[PROP_NAME_MAX];
605 char value[PROP_VALUE_MAX];
Elliott Hughesf8562c52017-01-26 16:48:57 -0800606};
607
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000608static int send_prop_msg(const prop_msg* msg) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800609 PropertyServiceConnection connection;
610 if (!connection.IsValid()) {
611 return connection.GetLastError();
612 }
613
614 int result = -1;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800615 int s = connection.socket();
Elliott Hughes9160ed92017-01-30 17:54:57 -0800616
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800617 const int num_bytes = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
Elliott Hughes9160ed92017-01-30 17:54:57 -0800618 if (num_bytes == sizeof(prop_msg)) {
619 // We successfully wrote to the property server but now we
620 // wait for the property server to finish its work. It
621 // acknowledges its completion by closing the socket so we
622 // poll here (on nothing), waiting for the socket to close.
623 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
624 // once the socket closes. Out of paranoia we cap our poll
625 // at 250 ms.
626 pollfd pollfds[1];
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800627 pollfds[0].fd = s;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800628 pollfds[0].events = 0;
629 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
630 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
631 result = 0;
632 } else {
633 // Ignore the timeout and treat it like a success anyway.
634 // The init process is single-threaded and its property
635 // service is sometimes slow to respond (perhaps it's off
636 // starting a child process or something) and thus this
637 // times out and the caller thinks it failed, even though
638 // it's still getting around to it. So we fake it here,
639 // mostly for ctl.* properties, but we do try and wait 250
640 // ms so callers who do read-after-write can reliably see
641 // what they've written. Most of the time.
642 // TODO: fix the system properties design.
643 __libc_format_log(ANDROID_LOG_WARN, "libc",
644 "Property service has timed out while trying to set \"%s\" to \"%s\"",
645 msg->name, msg->value);
646 result = 0;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000647 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800648 }
Dimitry Ivanov489f58b2017-01-24 18:39:04 +0000649
Elliott Hughes9160ed92017-01-30 17:54:57 -0800650 return result;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000651}
652
Elliott Hughes9160ed92017-01-30 17:54:57 -0800653bool prop_area::foreach_property(prop_bt* const trie,
654 void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
655 if (!trie) return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000656
Elliott Hughes9160ed92017-01-30 17:54:57 -0800657 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
658 if (left_offset != 0) {
659 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
660 if (err < 0) return false;
661 }
662 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
663 if (prop_offset != 0) {
664 prop_info* info = to_prop_info(&trie->prop);
665 if (!info) return false;
666 propfn(info, cookie);
667 }
668 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
669 if (children_offset != 0) {
670 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
671 if (err < 0) return false;
672 }
673 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
674 if (right_offset != 0) {
675 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
676 if (err < 0) return false;
677 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000678
Elliott Hughes9160ed92017-01-30 17:54:57 -0800679 return true;
Tom Cherry926ebe12015-09-23 15:34:40 -0700680}
681
Elliott Hughes9160ed92017-01-30 17:54:57 -0800682const prop_info* prop_area::find(const char* name) {
683 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
Tom Cherry926ebe12015-09-23 15:34:40 -0700684}
685
Elliott Hughes9160ed92017-01-30 17:54:57 -0800686bool prop_area::add(const char* name, unsigned int namelen, const char* value,
687 unsigned int valuelen) {
688 return find_property(root_node(), name, namelen, value, valuelen, true);
Tom Cherry926ebe12015-09-23 15:34:40 -0700689}
690
Elliott Hughes9160ed92017-01-30 17:54:57 -0800691bool prop_area::foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
692 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000693}
694
Tom Cherryb4171692015-12-09 15:48:15 -0800695class context_node {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800696 public:
697 context_node(context_node* next, const char* context, prop_area* pa)
698 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
699 lock_.init(false);
700 }
701 ~context_node() {
702 unmap();
703 free(context_);
704 }
705 bool open(bool access_rw, bool* fsetxattr_failed);
706 bool check_access_and_open();
707 void reset_access();
Tom Cherryb4171692015-12-09 15:48:15 -0800708
Elliott Hughes9160ed92017-01-30 17:54:57 -0800709 const char* context() const {
710 return context_;
711 }
712 prop_area* pa() {
713 return pa_;
714 }
Tom Cherryb4171692015-12-09 15:48:15 -0800715
Elliott Hughes9160ed92017-01-30 17:54:57 -0800716 context_node* next;
Tom Cherryb4171692015-12-09 15:48:15 -0800717
Elliott Hughes9160ed92017-01-30 17:54:57 -0800718 private:
719 bool check_access();
720 void unmap();
Tom Cherryb4171692015-12-09 15:48:15 -0800721
Elliott Hughes9160ed92017-01-30 17:54:57 -0800722 Lock lock_;
723 char* context_;
724 prop_area* pa_;
725 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700726};
727
728struct prefix_node {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800729 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
730 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
731 }
732 ~prefix_node() {
733 free(prefix);
734 }
735 char* prefix;
736 const size_t prefix_len;
737 context_node* context;
738 struct prefix_node* next;
Tom Cherry49a309f2015-09-23 16:09:47 -0700739};
740
741template <typename List, typename... Args>
742static inline void list_add(List** list, Args... args) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800743 *list = new List(*list, args...);
Tom Cherry49a309f2015-09-23 16:09:47 -0700744}
745
746static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800747 size_t prefix_len = strlen(prefix);
Tom Cherry49a309f2015-09-23 16:09:47 -0700748
Elliott Hughes9160ed92017-01-30 17:54:57 -0800749 auto next_list = list;
Tom Cherry49a309f2015-09-23 16:09:47 -0700750
Elliott Hughes9160ed92017-01-30 17:54:57 -0800751 while (*next_list) {
752 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
753 list_add(next_list, prefix, context);
754 return;
Tom Cherry49a309f2015-09-23 16:09:47 -0700755 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800756 next_list = &(*next_list)->next;
757 }
758 list_add(next_list, prefix, context);
Tom Cherry49a309f2015-09-23 16:09:47 -0700759}
760
761template <typename List, typename Func>
762static void list_foreach(List* list, Func func) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800763 while (list) {
764 func(list);
765 list = list->next;
766 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700767}
768
769template <typename List, typename Func>
770static List* list_find(List* list, Func func) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800771 while (list) {
772 if (func(list)) {
773 return list;
Tom Cherry49a309f2015-09-23 16:09:47 -0700774 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800775 list = list->next;
776 }
777 return nullptr;
Tom Cherry49a309f2015-09-23 16:09:47 -0700778}
779
780template <typename List>
781static void list_free(List** list) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800782 while (*list) {
783 auto old_list = *list;
784 *list = old_list->next;
785 delete old_list;
786 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700787}
788
789static prefix_node* prefixes = nullptr;
790static context_node* contexts = nullptr;
791
792/*
793 * pthread_mutex_lock() calls into system_properties in the case of contention.
794 * This creates a risk of dead lock if any system_properties functions
795 * use pthread locks after system_property initialization.
796 *
797 * For this reason, the below three functions use a bionic Lock and static
798 * allocation of memory for each filename.
799 */
800
Tom Cherryb4171692015-12-09 15:48:15 -0800801bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800802 lock_.lock();
803 if (pa_) {
Tom Cherryb4171692015-12-09 15:48:15 -0800804 lock_.unlock();
Elliott Hughes9160ed92017-01-30 17:54:57 -0800805 return true;
806 }
807
808 char filename[PROP_FILENAME_MAX];
809 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", property_filename, context_);
810 if (len < 0 || len > PROP_FILENAME_MAX) {
811 lock_.unlock();
812 return false;
813 }
814
815 if (access_rw) {
816 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
817 } else {
818 pa_ = map_prop_area(filename);
819 }
820 lock_.unlock();
821 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700822}
823
Tom Cherryb4171692015-12-09 15:48:15 -0800824bool context_node::check_access_and_open() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800825 if (!pa_ && !no_access_) {
826 if (!check_access() || !open(false, nullptr)) {
827 no_access_ = true;
Tom Cherryb4171692015-12-09 15:48:15 -0800828 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800829 }
830 return pa_;
Tom Cherryb4171692015-12-09 15:48:15 -0800831}
832
833void context_node::reset_access() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800834 if (!check_access()) {
835 unmap();
836 no_access_ = true;
837 } else {
838 no_access_ = false;
839 }
Tom Cherryb4171692015-12-09 15:48:15 -0800840}
841
842bool context_node::check_access() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800843 char filename[PROP_FILENAME_MAX];
844 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", property_filename, context_);
845 if (len < 0 || len > PROP_FILENAME_MAX) {
846 return false;
847 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700848
Elliott Hughes9160ed92017-01-30 17:54:57 -0800849 return access(filename, R_OK) == 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700850}
851
Tom Cherryb4171692015-12-09 15:48:15 -0800852void context_node::unmap() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800853 if (!pa_) {
854 return;
855 }
Tom Cherryb4171692015-12-09 15:48:15 -0800856
Elliott Hughes9160ed92017-01-30 17:54:57 -0800857 munmap(pa_, pa_size);
858 if (pa_ == __system_property_area__) {
859 __system_property_area__ = nullptr;
860 }
861 pa_ = nullptr;
Tom Cherryb4171692015-12-09 15:48:15 -0800862}
863
Tom Cherry49a309f2015-09-23 16:09:47 -0700864static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800865 char filename[PROP_FILENAME_MAX];
866 int len =
867 __libc_format_buffer(filename, sizeof(filename), "%s/properties_serial", property_filename);
868 if (len < 0 || len > PROP_FILENAME_MAX) {
869 __system_property_area__ = nullptr;
870 return false;
871 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700872
Elliott Hughes9160ed92017-01-30 17:54:57 -0800873 if (access_rw) {
874 __system_property_area__ =
875 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
876 } else {
877 __system_property_area__ = map_prop_area(filename);
878 }
879 return __system_property_area__;
Tom Cherry49a309f2015-09-23 16:09:47 -0700880}
881
882static prop_area* get_prop_area_for_name(const char* name) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800883 auto entry = list_find(prefixes, [name](prefix_node* l) {
884 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
885 });
886 if (!entry) {
887 return nullptr;
888 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700889
Elliott Hughes9160ed92017-01-30 17:54:57 -0800890 auto cnode = entry->context;
891 if (!cnode->pa()) {
892 /*
893 * We explicitly do not check no_access_ in this case because unlike the
894 * case of foreach(), we want to generate an selinux audit for each
895 * non-permitted property access in this function.
896 */
897 cnode->open(false, nullptr);
898 }
899 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700900}
901
902/*
903 * The below two functions are duplicated from label_support.c in libselinux.
904 * TODO: Find a location suitable for these functions such that both libc and
905 * libselinux can share a common source file.
906 */
907
908/*
909 * The read_spec_entries and read_spec_entry functions may be used to
910 * replace sscanf to read entries from spec files. The file and
911 * property services now use these.
912 */
913
914/* Read an entry from a spec file (e.g. file_contexts) */
Elliott Hughes9160ed92017-01-30 17:54:57 -0800915static inline int read_spec_entry(char** entry, char** ptr, int* len) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800916 *entry = nullptr;
917 char* tmp_buf = nullptr;
Tom Cherry49a309f2015-09-23 16:09:47 -0700918
Elliott Hughes9160ed92017-01-30 17:54:57 -0800919 while (isspace(**ptr) && **ptr != '\0') (*ptr)++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700920
Elliott Hughes9160ed92017-01-30 17:54:57 -0800921 tmp_buf = *ptr;
922 *len = 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700923
Elliott Hughes9160ed92017-01-30 17:54:57 -0800924 while (!isspace(**ptr) && **ptr != '\0') {
925 (*ptr)++;
926 (*len)++;
927 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700928
Elliott Hughes9160ed92017-01-30 17:54:57 -0800929 if (*len) {
930 *entry = strndup(tmp_buf, *len);
931 if (!*entry) return -1;
932 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700933
Elliott Hughes9160ed92017-01-30 17:54:57 -0800934 return 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700935}
936
937/*
938 * line_buf - Buffer containing the spec entries .
939 * num_args - The number of spec parameter entries to process.
940 * ... - A 'char **spec_entry' for each parameter.
941 * returns - The number of items processed.
942 *
943 * This function calls read_spec_entry() to do the actual string processing.
944 */
Elliott Hughes9160ed92017-01-30 17:54:57 -0800945static int read_spec_entries(char* line_buf, int num_args, ...) {
946 char **spec_entry, *buf_p;
947 int len, rc, items, entry_len = 0;
948 va_list ap;
Tom Cherry49a309f2015-09-23 16:09:47 -0700949
Elliott Hughes9160ed92017-01-30 17:54:57 -0800950 len = strlen(line_buf);
951 if (line_buf[len - 1] == '\n')
952 line_buf[len - 1] = '\0';
953 else
954 /* Handle case if line not \n terminated by bumping
955 * the len for the check below (as the line is NUL
956 * terminated by getline(3)) */
957 len++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700958
Elliott Hughes9160ed92017-01-30 17:54:57 -0800959 buf_p = line_buf;
960 while (isspace(*buf_p)) buf_p++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700961
Elliott Hughes9160ed92017-01-30 17:54:57 -0800962 /* Skip comment lines and empty lines. */
963 if (*buf_p == '#' || *buf_p == '\0') return 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700964
Elliott Hughes9160ed92017-01-30 17:54:57 -0800965 /* Process the spec file entries */
966 va_start(ap, num_args);
Tom Cherry49a309f2015-09-23 16:09:47 -0700967
Elliott Hughes9160ed92017-01-30 17:54:57 -0800968 items = 0;
969 while (items < num_args) {
970 spec_entry = va_arg(ap, char**);
Tom Cherry49a309f2015-09-23 16:09:47 -0700971
Elliott Hughes9160ed92017-01-30 17:54:57 -0800972 if (len - 1 == buf_p - line_buf) {
973 va_end(ap);
974 return items;
Tom Cherry49a309f2015-09-23 16:09:47 -0700975 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800976
977 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
978 if (rc < 0) {
979 va_end(ap);
980 return rc;
981 }
982 if (entry_len) items++;
983 }
984 va_end(ap);
985 return items;
Tom Cherry49a309f2015-09-23 16:09:47 -0700986}
987
Elliott Hughes9160ed92017-01-30 17:54:57 -0800988static bool initialize_properties_from_file(const char* filename) {
989 FILE* file = fopen(filename, "re");
990 if (!file) {
991 return false;
992 }
993
994 char* buffer = nullptr;
995 size_t line_len;
996 char* prop_prefix = nullptr;
997 char* context = nullptr;
998
999 while (getline(&buffer, &line_len, file) > 0) {
1000 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1001 if (items <= 0) {
1002 continue;
1003 }
1004 if (items == 1) {
1005 free(prop_prefix);
1006 continue;
1007 }
1008 /*
1009 * init uses ctl.* properties as an IPC mechanism and does not write them
1010 * to a property file, therefore we do not need to create property files
1011 * to store them.
1012 */
1013 if (!strncmp(prop_prefix, "ctl.", 4)) {
1014 free(prop_prefix);
1015 free(context);
1016 continue;
Tom Cherry49a309f2015-09-23 16:09:47 -07001017 }
1018
Elliott Hughes9160ed92017-01-30 17:54:57 -08001019 auto old_context =
1020 list_find(contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
1021 if (old_context) {
1022 list_add_after_len(&prefixes, prop_prefix, old_context);
1023 } else {
1024 list_add(&contexts, context, nullptr);
1025 list_add_after_len(&prefixes, prop_prefix, contexts);
Tom Cherry49a309f2015-09-23 16:09:47 -07001026 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001027 free(prop_prefix);
1028 free(context);
1029 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001030
Elliott Hughes9160ed92017-01-30 17:54:57 -08001031 free(buffer);
1032 fclose(file);
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001033
Elliott Hughes9160ed92017-01-30 17:54:57 -08001034 return true;
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001035}
1036
1037static bool initialize_properties() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001038 // If we do find /property_contexts, then this is being
1039 // run as part of the OTA updater on older release that had
1040 // /property_contexts - b/34370523
1041 if (initialize_properties_from_file("/property_contexts")) {
Tom Cherry49a309f2015-09-23 16:09:47 -07001042 return true;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001043 }
1044
Alex Klyubin176cf1f2017-03-08 13:16:03 -08001045 // Use property_contexts from /system & /vendor, fall back to those from /
1046 if (access("/system/etc/selinux/plat_property_contexts", R_OK) != -1) {
1047 if (!initialize_properties_from_file("/system/etc/selinux/plat_property_contexts")) {
1048 return false;
1049 }
1050 if (!initialize_properties_from_file("/vendor/etc/selinux/nonplat_property_contexts")) {
1051 return false;
1052 }
1053 } else {
1054 if (!initialize_properties_from_file("/plat_property_contexts")) {
1055 return false;
1056 }
1057 if (!initialize_properties_from_file("/nonplat_property_contexts")) {
1058 return false;
1059 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001060 }
1061
Elliott Hughes9160ed92017-01-30 17:54:57 -08001062 return true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001063}
1064
1065static bool is_dir(const char* pathname) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001066 struct stat info;
1067 if (stat(pathname, &info) == -1) {
1068 return false;
1069 }
1070 return S_ISDIR(info.st_mode);
Tom Cherry49a309f2015-09-23 16:09:47 -07001071}
1072
Tom Cherryb4171692015-12-09 15:48:15 -08001073static void free_and_unmap_contexts() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001074 list_free(&prefixes);
1075 list_free(&contexts);
1076 if (__system_property_area__) {
1077 munmap(__system_property_area__, pa_size);
1078 __system_property_area__ = nullptr;
1079 }
Tom Cherryb4171692015-12-09 15:48:15 -08001080}
1081
Elliott Hughes9160ed92017-01-30 17:54:57 -08001082int __system_properties_init() {
Elliott Hughes7ade61c2017-04-11 13:38:36 -07001083 // This is called from __libc_init_common, and should leave errno at 0 (http://b/37248982).
1084 ErrnoRestorer errno_restorer;
1085
Elliott Hughes9160ed92017-01-30 17:54:57 -08001086 if (initialized) {
1087 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001088 return 0;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001089 }
1090 if (is_dir(property_filename)) {
Tom Cherry49a309f2015-09-23 16:09:47 -07001091 if (!initialize_properties()) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001092 return -1;
Tom Cherry49a309f2015-09-23 16:09:47 -07001093 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001094 if (!map_system_property_area(false, nullptr)) {
1095 free_and_unmap_contexts();
1096 return -1;
Tom Cherry49a309f2015-09-23 16:09:47 -07001097 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001098 } else {
1099 __system_property_area__ = map_prop_area(property_filename);
Tom Cherry6ed51c02015-12-04 11:34:42 -08001100 if (!__system_property_area__) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001101 return -1;
Tom Cherry6ed51c02015-12-04 11:34:42 -08001102 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001103 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1104 list_add_after_len(&prefixes, "*", contexts);
1105 }
1106 initialized = true;
1107 return 0;
1108}
Tom Cherry6ed51c02015-12-04 11:34:42 -08001109
Elliott Hughes9160ed92017-01-30 17:54:57 -08001110int __system_property_set_filename(const char* filename) {
1111 size_t len = strlen(filename);
1112 if (len >= sizeof(property_filename)) return -1;
1113
1114 strcpy(property_filename, filename);
1115 return 0;
1116}
1117
1118int __system_property_area_init() {
1119 free_and_unmap_contexts();
1120 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
1121 if (!initialize_properties()) {
1122 return -1;
1123 }
1124 bool open_failed = false;
1125 bool fsetxattr_failed = false;
1126 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1127 if (!l->open(true, &fsetxattr_failed)) {
1128 open_failed = true;
Tom Cherry926ebe12015-09-23 15:34:40 -07001129 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001130 });
1131 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1132 free_and_unmap_contexts();
1133 return -1;
1134 }
1135 initialized = true;
1136 return fsetxattr_failed ? -2 : 0;
1137}
Tom Cherry926ebe12015-09-23 15:34:40 -07001138
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001139uint32_t __system_property_area_serial() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001140 prop_area* pa = __system_property_area__;
1141 if (!pa) {
1142 return -1;
1143 }
1144 // Make sure this read fulfilled before __system_property_serial
1145 return atomic_load_explicit(pa->serial(), memory_order_acquire);
1146}
1147
1148const prop_info* __system_property_find(const char* name) {
1149 if (!__system_property_area__) {
1150 return nullptr;
1151 }
1152
1153 prop_area* pa = get_prop_area_for_name(name);
1154 if (!pa) {
1155 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
1156 return nullptr;
1157 }
1158
1159 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001160}
1161
Hans Boehm1e8587a2014-08-19 14:07:55 -07001162// The C11 standard doesn't allow atomic loads from const fields,
1163// though C++11 does. Fudge it until standards get straightened out.
Elliott Hughes9160ed92017-01-30 17:54:57 -08001164static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, memory_order mo) {
1165 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1166 return atomic_load_explicit(non_const_s, mo);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001167}
1168
Elliott Hughes9160ed92017-01-30 17:54:57 -08001169int __system_property_read(const prop_info* pi, char* name, char* value) {
1170 while (true) {
1171 uint32_t serial = __system_property_serial(pi); // acquire semantics
1172 size_t len = SERIAL_VALUE_LEN(serial);
1173 memcpy(value, pi->value, len + 1);
1174 // TODO: Fix the synchronization scheme here.
1175 // There is no fully supported way to implement this kind
1176 // of synchronization in C++11, since the memcpy races with
1177 // updates to pi, and the data being accessed is not atomic.
1178 // The following fence is unintuitive, but would be the
1179 // correct one if memcpy used memory_order_relaxed atomic accesses.
1180 // In practice it seems unlikely that the generated code would
1181 // would be any different, so this should be OK.
1182 atomic_thread_fence(memory_order_acquire);
1183 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1184 if (name != nullptr) {
1185 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1186 if (namelen >= PROP_NAME_MAX) {
1187 __libc_format_log(ANDROID_LOG_ERROR, "libc",
1188 "The property name length for \"%s\" is >= %d;"
1189 " please use __system_property_read_callback"
1190 " to read this property. (the name is truncated to \"%s\")",
1191 pi->name, PROP_NAME_MAX - 1, name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001192 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001193 }
1194 return len;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001195 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001196 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001197}
1198
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001199void __system_property_read_callback(const prop_info* pi,
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001200 void (*callback)(void* cookie,
1201 const char* name,
1202 const char* value,
1203 uint32_t serial),
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001204 void* cookie) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001205 while (true) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001206 uint32_t serial = __system_property_serial(pi); // acquire semantics
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001207 size_t len = SERIAL_VALUE_LEN(serial);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001208 char value_buf[len + 1];
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001209
1210 memcpy(value_buf, pi->value, len);
1211 value_buf[len] = '\0';
1212
1213 // TODO: see todo in __system_property_read function
1214 atomic_thread_fence(memory_order_acquire);
1215 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001216 callback(cookie, pi->name, value_buf, serial);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001217 return;
1218 }
1219 }
1220}
1221
Elliott Hughes9160ed92017-01-30 17:54:57 -08001222int __system_property_get(const char* name, char* value) {
1223 const prop_info* pi = __system_property_find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001224
Elliott Hughes9160ed92017-01-30 17:54:57 -08001225 if (pi != 0) {
1226 return __system_property_read(pi, nullptr, value);
1227 } else {
1228 value[0] = 0;
1229 return 0;
1230 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001231}
1232
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001233static constexpr uint32_t kProtocolVersion1 = 1;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001234static constexpr uint32_t kProtocolVersion2 = 2; // current
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001235
1236static atomic_uint_least32_t g_propservice_protocol_version = 0;
1237
1238static void detect_protocol_version() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001239 char value[PROP_VALUE_MAX];
1240 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1241 g_propservice_protocol_version = kProtocolVersion1;
1242 __libc_format_log(ANDROID_LOG_WARN, "libc",
1243 "Using old property service protocol (\"%s\" is not set)",
1244 kServiceVersionPropertyName);
1245 } else {
1246 uint32_t version = static_cast<uint32_t>(atoll(value));
1247 if (version >= kProtocolVersion2) {
1248 g_propservice_protocol_version = kProtocolVersion2;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001249 } else {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001250 __libc_format_log(ANDROID_LOG_WARN, "libc",
1251 "Using old property service protocol (\"%s\"=\"%s\")",
1252 kServiceVersionPropertyName, value);
1253 g_propservice_protocol_version = kProtocolVersion1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001254 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001255 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001256}
1257
1258int __system_property_set(const char* key, const char* value) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001259 if (key == nullptr) return -1;
1260 if (value == nullptr) value = "";
1261 if (strlen(value) >= PROP_VALUE_MAX) return -1;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001262
Elliott Hughes9160ed92017-01-30 17:54:57 -08001263 if (g_propservice_protocol_version == 0) {
1264 detect_protocol_version();
1265 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001266
Elliott Hughes9160ed92017-01-30 17:54:57 -08001267 if (g_propservice_protocol_version == kProtocolVersion1) {
1268 // Old protocol does not support long names
1269 if (strlen(key) >= PROP_NAME_MAX) return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001270
Elliott Hughes9160ed92017-01-30 17:54:57 -08001271 prop_msg msg;
1272 memset(&msg, 0, sizeof msg);
1273 msg.cmd = PROP_MSG_SETPROP;
1274 strlcpy(msg.name, key, sizeof msg.name);
1275 strlcpy(msg.value, value, sizeof msg.value);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001276
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001277 return send_prop_msg(&msg);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001278 } else {
1279 // Use proper protocol
1280 PropertyServiceConnection connection;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001281 if (!connection.IsValid()) {
1282 errno = connection.GetLastError();
1283 __libc_format_log(ANDROID_LOG_WARN,
1284 "libc",
1285 "Unable to set property \"%s\" to \"%s\": connection failed; errno=%d (%s)",
1286 key,
1287 value,
1288 errno,
1289 strerror(errno));
1290 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001291 }
1292
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001293 SocketWriter writer(&connection);
1294 if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) {
1295 errno = connection.GetLastError();
1296 __libc_format_log(ANDROID_LOG_WARN,
1297 "libc",
1298 "Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)",
1299 key,
1300 value,
1301 errno,
1302 strerror(errno));
1303 return -1;
1304 }
1305
1306 int result = -1;
1307 if (!connection.RecvInt32(&result)) {
1308 errno = connection.GetLastError();
1309 __libc_format_log(ANDROID_LOG_WARN,
1310 "libc",
1311 "Unable to set property \"%s\" to \"%s\": recv failed; errno=%d (%s)",
1312 key,
1313 value,
1314 errno,
1315 strerror(errno));
1316 return -1;
1317 }
1318
1319 if (result != PROP_SUCCESS) {
1320 __libc_format_log(ANDROID_LOG_WARN,
1321 "libc",
1322 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1323 key,
1324 value,
1325 result);
1326 return -1;
1327 }
1328
1329 return 0;
1330 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001331}
1332
Elliott Hughes9160ed92017-01-30 17:54:57 -08001333int __system_property_update(prop_info* pi, const char* value, unsigned int len) {
1334 if (len >= PROP_VALUE_MAX) {
1335 return -1;
1336 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001337
Elliott Hughes9160ed92017-01-30 17:54:57 -08001338 prop_area* pa = __system_property_area__;
Tom Cherry6ed51c02015-12-04 11:34:42 -08001339
Elliott Hughes9160ed92017-01-30 17:54:57 -08001340 if (!pa) {
1341 return -1;
1342 }
Tom Cherry6ed51c02015-12-04 11:34:42 -08001343
Elliott Hughes9160ed92017-01-30 17:54:57 -08001344 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1345 serial |= 1;
1346 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1347 // The memcpy call here also races. Again pretend it
1348 // used memory_order_relaxed atomics, and use the analogous
1349 // counterintuitive fence.
1350 atomic_thread_fence(memory_order_release);
1351 strlcpy(pi->value, value, len + 1);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001352
Elliott Hughes9160ed92017-01-30 17:54:57 -08001353 atomic_store_explicit(&pi->serial, (len << 24) | ((serial + 1) & 0xffffff), memory_order_release);
1354 __futex_wake(&pi->serial, INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001355
Elliott Hughes9160ed92017-01-30 17:54:57 -08001356 atomic_store_explicit(pa->serial(), atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
1357 memory_order_release);
1358 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001359
Elliott Hughes9160ed92017-01-30 17:54:57 -08001360 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001361}
jiaguo879d3302014-03-13 17:39:58 +08001362
Elliott Hughes9160ed92017-01-30 17:54:57 -08001363int __system_property_add(const char* name, unsigned int namelen, const char* value,
1364 unsigned int valuelen) {
1365 if (valuelen >= PROP_VALUE_MAX) {
1366 return -1;
1367 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001368
Elliott Hughes9160ed92017-01-30 17:54:57 -08001369 if (namelen < 1) {
1370 return -1;
1371 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001372
Elliott Hughes9160ed92017-01-30 17:54:57 -08001373 if (!__system_property_area__) {
1374 return -1;
1375 }
Tom Cherry6ed51c02015-12-04 11:34:42 -08001376
Elliott Hughes9160ed92017-01-30 17:54:57 -08001377 prop_area* pa = get_prop_area_for_name(name);
Tom Cherry49a309f2015-09-23 16:09:47 -07001378
Elliott Hughes9160ed92017-01-30 17:54:57 -08001379 if (!pa) {
1380 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
1381 return -1;
1382 }
Tom Cherry926ebe12015-09-23 15:34:40 -07001383
Elliott Hughes9160ed92017-01-30 17:54:57 -08001384 bool ret = pa->add(name, namelen, value, valuelen);
1385 if (!ret) {
1386 return -1;
1387 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001388
Elliott Hughes9160ed92017-01-30 17:54:57 -08001389 // There is only a single mutator, but we want to make sure that
1390 // updates are visible to a reader waiting for the update.
1391 atomic_store_explicit(
1392 __system_property_area__->serial(),
1393 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
1394 memory_order_release);
1395 __futex_wake(__system_property_area__->serial(), INT32_MAX);
1396 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001397}
1398
Hans Boehm30214b92014-07-31 15:53:22 -07001399// Wait for non-locked serial, and retrieve it with acquire semantics.
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001400uint32_t __system_property_serial(const prop_info* pi) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001401 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
1402 while (SERIAL_DIRTY(serial)) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001403 __futex_wait(const_cast<_Atomic(uint_least32_t)*>(&pi->serial), serial, nullptr);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001404 serial = load_const_atomic(&pi->serial, memory_order_acquire);
1405 }
1406 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001407}
1408
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001409uint32_t __system_property_wait_any(uint32_t old_serial) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001410 uint32_t new_serial;
Elliott Hughes40c885a2017-02-16 17:13:04 -08001411 __system_property_wait(nullptr, old_serial, &new_serial, nullptr);
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001412 return new_serial;
1413}
Elliott Hughes9160ed92017-01-30 17:54:57 -08001414
Elliott Hughes40c885a2017-02-16 17:13:04 -08001415bool __system_property_wait(const prop_info* pi,
1416 uint32_t old_serial,
1417 uint32_t* new_serial_ptr,
1418 const timespec* relative_timeout) {
1419 // Are we waiting on the global serial or a specific serial?
1420 atomic_uint_least32_t* serial_ptr;
1421 if (pi == nullptr) {
1422 if (__system_property_area__ == nullptr) return -1;
1423 serial_ptr = __system_property_area__->serial();
1424 } else {
1425 serial_ptr = const_cast<atomic_uint_least32_t*>(&pi->serial);
1426 }
1427
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001428 uint32_t new_serial;
1429 do {
Elliott Hughes40c885a2017-02-16 17:13:04 -08001430 int rc;
1431 if ((rc = __futex_wait(serial_ptr, old_serial, relative_timeout)) != 0 && rc == -ETIMEDOUT) {
1432 return false;
1433 }
1434 new_serial = load_const_atomic(serial_ptr, memory_order_acquire);
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001435 } while (new_serial == old_serial);
Elliott Hughes40c885a2017-02-16 17:13:04 -08001436
1437 *new_serial_ptr = new_serial;
1438 return true;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001439}
1440
1441const prop_info* __system_property_find_nth(unsigned n) {
Elliott Hughes438e0192017-04-17 14:53:07 -07001442 struct find_nth {
1443 const uint32_t sought;
1444 uint32_t current;
1445 const prop_info* result;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001446
Elliott Hughes438e0192017-04-17 14:53:07 -07001447 explicit find_nth(uint32_t n) : sought(n), current(0), result(nullptr) {}
1448 static void fn(const prop_info* pi, void* ptr) {
1449 find_nth* self = reinterpret_cast<find_nth*>(ptr);
1450 if (self->current++ == self->sought) self->result = pi;
1451 }
1452 } state(n);
1453 __system_property_foreach(find_nth::fn, &state);
1454 return state.result;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001455}
1456
1457int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
1458 if (!__system_property_area__) {
1459 return -1;
1460 }
1461
1462 list_foreach(contexts, [propfn, cookie](context_node* l) {
1463 if (l->check_access_and_open()) {
Elliott Hughes438e0192017-04-17 14:53:07 -07001464 l->pa()->foreach(propfn, cookie);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001465 }
1466 });
1467 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001468}