blob: 06173a8fc9997c5307f7dd944e43e885c55465b6 [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 */
Tom Cherry49a309f2015-09-23 16:09:47 -070028#include <ctype.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000029#include <errno.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000030#include <fcntl.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070031#include <poll.h>
32#include <stdatomic.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000033#include <stdbool.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070034#include <stddef.h>
35#include <stdint.h>
36#include <stdio.h>
37#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>
Tom Cherry49a309f2015-09-23 16:09:47 -070049#include <sys/un.h>
50#include <sys/xattr.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000051
52#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
53#include <sys/_system_properties.h>
54#include <sys/system_properties.h>
55
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070056#include "private/bionic_futex.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070057#include "private/bionic_lock.h"
Elliott Hughes8eac9af2014-05-09 19:12:08 -070058#include "private/bionic_macros.h"
Dimitry Ivanov581b9f62017-01-09 11:05:52 -080059#include "private/bionic_sdk_version.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070060#include "private/libc_logging.h"
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000061
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000062static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
63
64
65/*
66 * Properties are stored in a hybrid trie/binary tree structure.
67 * Each property's name is delimited at '.' characters, and the tokens are put
68 * into a trie structure. Siblings at each level of the trie are stored in a
69 * binary tree. For instance, "ro.secure"="1" could be stored as follows:
70 *
71 * +-----+ children +----+ children +--------+
72 * | |-------------->| ro |-------------->| secure |
73 * +-----+ +----+ +--------+
74 * / \ / |
75 * left / \ right left / | prop +===========+
76 * v v v +-------->| ro.secure |
77 * +-----+ +-----+ +-----+ +-----------+
78 * | net | | sys | | com | | 1 |
79 * +-----+ +-----+ +-----+ +===========+
80 */
81
82// Represents a node in the trie.
83struct prop_bt {
84 uint8_t namelen;
85 uint8_t reserved[3];
86
Yabin Cuib8ce4742015-02-10 21:35:56 -080087 // The property trie is updated only by the init process (single threaded) which provides
88 // property service. And it can be read by multiple threads at the same time.
89 // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the
90 // left, right, children "pointers" in the trie node. To make sure readers who see the
91 // change of "pointers" can also notice the change of prop_bt structure contents pointed by
92 // the "pointers", we always use release-consume ordering pair when accessing these "pointers".
93
94 // prop "points" to prop_info structure if there is a propery associated with the trie node.
95 // Its situation is similar to the left, right, children "pointers". So we use
96 // atomic_uint_least32_t and release-consume ordering to protect it as well.
97
Hans Boehm30214b92014-07-31 15:53:22 -070098 // We should also avoid rereading these fields redundantly, since not
99 // all processor implementations ensure that multiple loads from the
100 // same field are carried out in the right order.
Yabin Cuib8ce4742015-02-10 21:35:56 -0800101 atomic_uint_least32_t prop;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000102
Yabin Cuib8ce4742015-02-10 21:35:56 -0800103 atomic_uint_least32_t left;
104 atomic_uint_least32_t right;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000105
Yabin Cuib8ce4742015-02-10 21:35:56 -0800106 atomic_uint_least32_t children;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000107
108 char name[0];
109
110 prop_bt(const char *name, const uint8_t name_length) {
111 this->namelen = name_length;
112 memcpy(this->name, name, name_length);
113 this->name[name_length] = '\0';
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000114 }
115
116private:
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700117 DISALLOW_COPY_AND_ASSIGN(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000118};
119
Tom Cherry926ebe12015-09-23 15:34:40 -0700120class prop_area {
121public:
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000122
123 prop_area(const uint32_t magic, const uint32_t version) :
Tom Cherry926ebe12015-09-23 15:34:40 -0700124 magic_(magic), version_(version) {
125 atomic_init(&serial_, 0);
126 memset(reserved_, 0, sizeof(reserved_));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000127 // Allocate enough space for the root node.
Tom Cherry926ebe12015-09-23 15:34:40 -0700128 bytes_used_ = sizeof(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000129 }
130
Tom Cherry926ebe12015-09-23 15:34:40 -0700131 const prop_info *find(const char *name);
132 bool add(const char *name, unsigned int namelen,
133 const char *value, unsigned int valuelen);
134
135 bool foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie);
136
137 atomic_uint_least32_t *serial() { return &serial_; }
138 uint32_t magic() const { return magic_; }
139 uint32_t version() const { return version_; }
140
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000141private:
Tom Cherry926ebe12015-09-23 15:34:40 -0700142 void *allocate_obj(const size_t size, uint_least32_t *const off);
143 prop_bt *new_prop_bt(const char *name, uint8_t namelen, uint_least32_t *const off);
144 prop_info *new_prop_info(const char *name, uint8_t namelen,
145 const char *value, uint8_t valuelen,
146 uint_least32_t *const off);
147 void *to_prop_obj(uint_least32_t off);
148 prop_bt *to_prop_bt(atomic_uint_least32_t *off_p);
149 prop_info *to_prop_info(atomic_uint_least32_t *off_p);
150
151 prop_bt *root_node();
152
153 prop_bt *find_prop_bt(prop_bt *const bt, const char *name,
154 uint8_t namelen, bool alloc_if_needed);
155
156 const prop_info *find_property(prop_bt *const trie, const char *name,
157 uint8_t namelen, const char *value,
158 uint8_t valuelen, bool alloc_if_needed);
159
160 bool foreach_property(prop_bt *const trie,
161 void (*propfn)(const prop_info *pi, void *cookie),
162 void *cookie);
163
164 uint32_t bytes_used_;
165 atomic_uint_least32_t serial_;
166 uint32_t magic_;
167 uint32_t version_;
168 uint32_t reserved_[28];
169 char data_[0];
170
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700171 DISALLOW_COPY_AND_ASSIGN(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000172};
173
174struct prop_info {
Hans Boehm30214b92014-07-31 15:53:22 -0700175 atomic_uint_least32_t serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000176 char value[PROP_VALUE_MAX];
177 char name[0];
178
179 prop_info(const char *name, const uint8_t namelen, const char *value,
180 const uint8_t valuelen) {
181 memcpy(this->name, name, namelen);
182 this->name[namelen] = '\0';
Hans Boehm30214b92014-07-31 15:53:22 -0700183 atomic_init(&this->serial, valuelen << 24);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000184 memcpy(this->value, value, valuelen);
185 this->value[valuelen] = '\0';
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000186 }
187private:
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700188 DISALLOW_COPY_AND_ASSIGN(prop_info);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000189};
190
191struct find_nth_cookie {
192 uint32_t count;
193 const uint32_t n;
194 const prop_info *pi;
195
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700196 explicit find_nth_cookie(uint32_t n) : count(0), n(n), pi(NULL) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000197 }
198};
199
Tom Cherry49a309f2015-09-23 16:09:47 -0700200static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000201static bool compat_mode = false;
202static size_t pa_data_size;
203static size_t pa_size;
Tom Cherryb4171692015-12-09 15:48:15 -0800204static bool initialized = false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000205
206// NOTE: This isn't static because system_properties_compat.c
207// requires it.
208prop_area *__system_property_area__ = NULL;
209
210static int get_fd_from_env(void)
211{
212 // This environment variable consistes of two decimal integer
213 // values separated by a ",". The first value is a file descriptor
214 // and the second is the size of the system properties area. The
215 // size is currently unused.
216 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
217
218 if (!env) {
219 return -1;
220 }
221
222 return atoi(env);
223}
224
Tom Cherry49a309f2015-09-23 16:09:47 -0700225static prop_area* map_prop_area_rw(const char* filename, const char* context,
226 bool* fsetxattr_failed) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000227 /* dev is a tmpfs that we can use to carve a shared workspace
228 * out of, so let's do that...
229 */
Tom Cherry49a309f2015-09-23 16:09:47 -0700230 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000231
232 if (fd < 0) {
233 if (errno == EACCES) {
234 /* for consistency with the case where the process has already
235 * mapped the page in and segfaults when trying to write to it
236 */
237 abort();
238 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700239 return nullptr;
240 }
241
242 if (context) {
243 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
244 __libc_format_log(ANDROID_LOG_ERROR, "libc",
245 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
246 /*
247 * fsetxattr() will fail during system properties tests due to selinux policy.
248 * We do not want to create a custom policy for the tester, so we will continue in
249 * this function but set a flag that an error has occurred.
250 * Init, which is the only daemon that should ever call this function will abort
251 * when this error occurs.
252 * Otherwise, the tester will ignore it and continue, albeit without any selinux
253 * property separation.
254 */
255 if (fsetxattr_failed) {
256 *fsetxattr_failed = true;
257 }
258 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000259 }
260
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000261 if (ftruncate(fd, PA_SIZE) < 0) {
262 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700263 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000264 }
265
266 pa_size = PA_SIZE;
267 pa_data_size = pa_size - sizeof(prop_area);
268 compat_mode = false;
269
270 void *const memory_area = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
271 if (memory_area == MAP_FAILED) {
272 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700273 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000274 }
275
276 prop_area *pa = new(memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
277
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000278 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700279 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000280}
281
Tom Cherry49a309f2015-09-23 16:09:47 -0700282static prop_area* map_fd_ro(const int fd) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000283 struct stat fd_stat;
284 if (fstat(fd, &fd_stat) < 0) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700285 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000286 }
287
288 if ((fd_stat.st_uid != 0)
289 || (fd_stat.st_gid != 0)
290 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
Narayan Kamath37e95702014-02-24 11:05:02 +0000291 || (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area))) ) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700292 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000293 }
294
295 pa_size = fd_stat.st_size;
296 pa_data_size = pa_size - sizeof(prop_area);
297
298 void* const map_result = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0);
299 if (map_result == MAP_FAILED) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700300 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000301 }
302
303 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
Tom Cherry926ebe12015-09-23 15:34:40 -0700304 if ((pa->magic() != PROP_AREA_MAGIC) ||
305 (pa->version() != PROP_AREA_VERSION &&
306 pa->version() != PROP_AREA_VERSION_COMPAT)) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000307 munmap(pa, pa_size);
Tom Cherry49a309f2015-09-23 16:09:47 -0700308 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000309 }
310
Tom Cherry926ebe12015-09-23 15:34:40 -0700311 if (pa->version() == PROP_AREA_VERSION_COMPAT) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000312 compat_mode = true;
313 }
314
Tom Cherry49a309f2015-09-23 16:09:47 -0700315 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000316}
317
Tom Cherry49a309f2015-09-23 16:09:47 -0700318static prop_area* map_prop_area(const char* filename, bool is_legacy) {
319 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000320 bool close_fd = true;
Tom Cherry49a309f2015-09-23 16:09:47 -0700321 if (fd == -1 && errno == ENOENT && is_legacy) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000322 /*
323 * For backwards compatibility, if the file doesn't
324 * exist, we use the environment to get the file descriptor.
325 * For security reasons, we only use this backup if the kernel
326 * returns ENOENT. We don't want to use the backup if the kernel
327 * returns other errors such as ENOMEM or ENFILE, since it
328 * might be possible for an external program to trigger this
329 * condition.
Tom Cherry49a309f2015-09-23 16:09:47 -0700330 * Only do this for the legacy prop file, secured prop files
331 * do not have a backup
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000332 */
333 fd = get_fd_from_env();
334 close_fd = false;
335 }
336
337 if (fd < 0) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700338 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000339 }
340
Tom Cherry49a309f2015-09-23 16:09:47 -0700341 prop_area* map_result = map_fd_ro(fd);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000342 if (close_fd) {
343 close(fd);
344 }
345
346 return map_result;
347}
348
Tom Cherry926ebe12015-09-23 15:34:40 -0700349void *prop_area::allocate_obj(const size_t size, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000350{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800351 const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t));
Tom Cherry926ebe12015-09-23 15:34:40 -0700352 if (bytes_used_ + aligned > pa_data_size) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000353 return NULL;
354 }
355
Tom Cherry926ebe12015-09-23 15:34:40 -0700356 *off = bytes_used_;
357 bytes_used_ += aligned;
358 return data_ + *off;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000359}
360
Tom Cherry926ebe12015-09-23 15:34:40 -0700361prop_bt *prop_area::new_prop_bt(const char *name, uint8_t namelen, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000362{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800363 uint_least32_t new_offset;
364 void *const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
365 if (p != NULL) {
366 prop_bt* bt = new(p) prop_bt(name, namelen);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000367 *off = new_offset;
368 return bt;
369 }
370
371 return NULL;
372}
373
Tom Cherry926ebe12015-09-23 15:34:40 -0700374prop_info *prop_area::new_prop_info(const char *name, uint8_t namelen,
Yabin Cuib8ce4742015-02-10 21:35:56 -0800375 const char *value, uint8_t valuelen, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000376{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800377 uint_least32_t new_offset;
378 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
379 if (p != NULL) {
380 prop_info* info = new(p) prop_info(name, namelen, value, valuelen);
381 *off = new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000382 return info;
383 }
384
385 return NULL;
386}
387
Tom Cherry926ebe12015-09-23 15:34:40 -0700388void *prop_area::to_prop_obj(uint_least32_t off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000389{
390 if (off > pa_data_size)
391 return NULL;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000392
Tom Cherry926ebe12015-09-23 15:34:40 -0700393 return (data_ + off);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000394}
395
Tom Cherry926ebe12015-09-23 15:34:40 -0700396inline prop_bt *prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800397 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
398 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
399}
400
Tom Cherry926ebe12015-09-23 15:34:40 -0700401inline prop_info *prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800402 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
403 return reinterpret_cast<prop_info*>(to_prop_obj(off));
404}
405
Tom Cherry926ebe12015-09-23 15:34:40 -0700406inline prop_bt *prop_area::root_node()
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000407{
408 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
409}
410
411static int cmp_prop_name(const char *one, uint8_t one_len, const char *two,
412 uint8_t two_len)
413{
414 if (one_len < two_len)
415 return -1;
416 else if (one_len > two_len)
417 return 1;
418 else
419 return strncmp(one, two, one_len);
420}
421
Tom Cherry926ebe12015-09-23 15:34:40 -0700422prop_bt *prop_area::find_prop_bt(prop_bt *const bt, const char *name,
423 uint8_t namelen, bool alloc_if_needed)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000424{
425
426 prop_bt* current = bt;
427 while (true) {
428 if (!current) {
429 return NULL;
430 }
431
432 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
433 if (ret == 0) {
434 return current;
435 }
436
437 if (ret < 0) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800438 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
439 if (left_offset != 0) {
440 current = to_prop_bt(&current->left);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000441 } else {
442 if (!alloc_if_needed) {
443 return NULL;
444 }
445
Yabin Cuib8ce4742015-02-10 21:35:56 -0800446 uint_least32_t new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000447 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
448 if (new_bt) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800449 atomic_store_explicit(&current->left, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000450 }
451 return new_bt;
452 }
453 } else {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800454 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
455 if (right_offset != 0) {
456 current = to_prop_bt(&current->right);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000457 } else {
458 if (!alloc_if_needed) {
459 return NULL;
460 }
461
Yabin Cuib8ce4742015-02-10 21:35:56 -0800462 uint_least32_t new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000463 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
464 if (new_bt) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800465 atomic_store_explicit(&current->right, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000466 }
467 return new_bt;
468 }
469 }
470 }
471}
472
Tom Cherry926ebe12015-09-23 15:34:40 -0700473const prop_info *prop_area::find_property(prop_bt *const trie, const char *name,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000474 uint8_t namelen, const char *value, uint8_t valuelen,
475 bool alloc_if_needed)
476{
477 if (!trie) return NULL;
478
479 const char *remaining_name = name;
480 prop_bt* current = trie;
481 while (true) {
482 const char *sep = strchr(remaining_name, '.');
483 const bool want_subtree = (sep != NULL);
484 const uint8_t substr_size = (want_subtree) ?
485 sep - remaining_name : strlen(remaining_name);
486
487 if (!substr_size) {
488 return NULL;
489 }
490
491 prop_bt* root = NULL;
Yabin Cuib8ce4742015-02-10 21:35:56 -0800492 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
493 if (children_offset != 0) {
494 root = to_prop_bt(&current->children);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000495 } else if (alloc_if_needed) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800496 uint_least32_t new_offset;
497 root = new_prop_bt(remaining_name, substr_size, &new_offset);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000498 if (root) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800499 atomic_store_explicit(&current->children, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000500 }
501 }
502
503 if (!root) {
504 return NULL;
505 }
506
507 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
508 if (!current) {
509 return NULL;
510 }
511
512 if (!want_subtree)
513 break;
514
515 remaining_name = sep + 1;
516 }
517
Yabin Cuib8ce4742015-02-10 21:35:56 -0800518 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
519 if (prop_offset != 0) {
520 return to_prop_info(&current->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000521 } else if (alloc_if_needed) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800522 uint_least32_t new_offset;
523 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000524 if (new_info) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800525 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000526 }
527
528 return new_info;
529 } else {
530 return NULL;
531 }
532}
533
534static int send_prop_msg(const prop_msg *msg)
535{
Elliott Hughes0dc39f92014-09-22 17:43:09 -0700536 const int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
537 if (fd == -1) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000538 return -1;
539 }
540
541 const size_t namelen = strlen(property_service_socket);
542
543 sockaddr_un addr;
544 memset(&addr, 0, sizeof(addr));
545 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
546 addr.sun_family = AF_LOCAL;
547 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
548 if (TEMP_FAILURE_RETRY(connect(fd, reinterpret_cast<sockaddr*>(&addr), alen)) < 0) {
549 close(fd);
550 return -1;
551 }
552
553 const int num_bytes = TEMP_FAILURE_RETRY(send(fd, msg, sizeof(prop_msg), 0));
554
555 int result = -1;
556 if (num_bytes == sizeof(prop_msg)) {
557 // We successfully wrote to the property server but now we
558 // wait for the property server to finish its work. It
559 // acknowledges its completion by closing the socket so we
560 // poll here (on nothing), waiting for the socket to close.
561 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
562 // once the socket closes. Out of paranoia we cap our poll
563 // at 250 ms.
564 pollfd pollfds[1];
565 pollfds[0].fd = fd;
566 pollfds[0].events = 0;
567 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
568 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
569 result = 0;
570 } else {
571 // Ignore the timeout and treat it like a success anyway.
572 // The init process is single-threaded and its property
573 // service is sometimes slow to respond (perhaps it's off
574 // starting a child process or something) and thus this
575 // times out and the caller thinks it failed, even though
576 // it's still getting around to it. So we fake it here,
577 // mostly for ctl.* properties, but we do try and wait 250
578 // ms so callers who do read-after-write can reliably see
579 // what they've written. Most of the time.
580 // TODO: fix the system properties design.
581 result = 0;
582 }
583 }
584
585 close(fd);
586 return result;
587}
588
589static void find_nth_fn(const prop_info *pi, void *ptr)
590{
591 find_nth_cookie *cookie = reinterpret_cast<find_nth_cookie*>(ptr);
592
593 if (cookie->n == cookie->count)
594 cookie->pi = pi;
595
596 cookie->count++;
597}
598
Tom Cherry926ebe12015-09-23 15:34:40 -0700599bool prop_area::foreach_property(prop_bt *const trie,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000600 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
601{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000602 if (!trie)
Tom Cherry926ebe12015-09-23 15:34:40 -0700603 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000604
Yabin Cuib8ce4742015-02-10 21:35:56 -0800605 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
606 if (left_offset != 0) {
607 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000608 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700609 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000610 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800611 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
612 if (prop_offset != 0) {
613 prop_info *info = to_prop_info(&trie->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000614 if (!info)
Tom Cherry926ebe12015-09-23 15:34:40 -0700615 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000616 propfn(info, cookie);
617 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800618 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
619 if (children_offset != 0) {
620 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000621 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700622 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000623 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800624 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
625 if (right_offset != 0) {
626 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000627 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700628 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000629 }
630
Tom Cherry926ebe12015-09-23 15:34:40 -0700631 return true;
632}
633
634const prop_info *prop_area::find(const char *name) {
635 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
636}
637
638bool prop_area::add(const char *name, unsigned int namelen,
639 const char *value, unsigned int valuelen) {
640 return find_property(root_node(), name, namelen, value, valuelen, true);
641}
642
643bool prop_area::foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
644 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000645}
646
Tom Cherryb4171692015-12-09 15:48:15 -0800647class context_node {
648public:
649 context_node(context_node* next, const char* context, prop_area* pa)
650 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
651 lock_.init(false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700652 }
653 ~context_node() {
Tom Cherryb4171692015-12-09 15:48:15 -0800654 unmap();
655 free(context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700656 }
Tom Cherryb4171692015-12-09 15:48:15 -0800657 bool open(bool access_rw, bool* fsetxattr_failed);
658 bool check_access_and_open();
659 void reset_access();
660
661 const char* context() const { return context_; }
662 prop_area* pa() { return pa_; }
663
664 context_node* next;
665
666private:
667 bool check_access();
668 void unmap();
669
670 Lock lock_;
671 char* context_;
672 prop_area* pa_;
673 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700674};
675
676struct prefix_node {
677 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
678 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
679 }
680 ~prefix_node() {
681 free(prefix);
682 }
683 char* prefix;
684 const size_t prefix_len;
685 context_node* context;
686 struct prefix_node* next;
687};
688
689template <typename List, typename... Args>
690static inline void list_add(List** list, Args... args) {
691 *list = new List(*list, args...);
692}
693
694static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
695 size_t prefix_len = strlen(prefix);
696
697 auto next_list = list;
698
699 while (*next_list) {
700 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
701 list_add(next_list, prefix, context);
702 return;
703 }
704 next_list = &(*next_list)->next;
705 }
706 list_add(next_list, prefix, context);
707}
708
709template <typename List, typename Func>
710static void list_foreach(List* list, Func func) {
711 while (list) {
712 func(list);
713 list = list->next;
714 }
715}
716
717template <typename List, typename Func>
718static List* list_find(List* list, Func func) {
719 while (list) {
720 if (func(list)) {
721 return list;
722 }
723 list = list->next;
724 }
725 return nullptr;
726}
727
728template <typename List>
729static void list_free(List** list) {
730 while (*list) {
731 auto old_list = *list;
732 *list = old_list->next;
733 delete old_list;
734 }
735}
736
737static prefix_node* prefixes = nullptr;
738static context_node* contexts = nullptr;
739
740/*
741 * pthread_mutex_lock() calls into system_properties in the case of contention.
742 * This creates a risk of dead lock if any system_properties functions
743 * use pthread locks after system_property initialization.
744 *
745 * For this reason, the below three functions use a bionic Lock and static
746 * allocation of memory for each filename.
747 */
748
Tom Cherryb4171692015-12-09 15:48:15 -0800749bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
750 lock_.lock();
751 if (pa_) {
752 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700753 return true;
754 }
755
756 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800757 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
758 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700759 if (len < 0 || len > PROP_FILENAME_MAX) {
Tom Cherryb4171692015-12-09 15:48:15 -0800760 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700761 return false;
762 }
763
764 if (access_rw) {
Tom Cherryb4171692015-12-09 15:48:15 -0800765 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
Tom Cherry49a309f2015-09-23 16:09:47 -0700766 } else {
Tom Cherryb4171692015-12-09 15:48:15 -0800767 pa_ = map_prop_area(filename, false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700768 }
Tom Cherryb4171692015-12-09 15:48:15 -0800769 lock_.unlock();
770 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700771}
772
Tom Cherryb4171692015-12-09 15:48:15 -0800773bool context_node::check_access_and_open() {
774 if (!pa_ && !no_access_) {
775 if (!check_access() || !open(false, nullptr)) {
776 no_access_ = true;
777 }
778 }
779 return pa_;
780}
781
782void context_node::reset_access() {
783 if (!check_access()) {
784 unmap();
785 no_access_ = true;
786 } else {
787 no_access_ = false;
788 }
789}
790
791bool context_node::check_access() {
Tom Cherry49a309f2015-09-23 16:09:47 -0700792 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800793 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
794 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700795 if (len < 0 || len > PROP_FILENAME_MAX) {
796 return false;
797 }
798
799 return access(filename, R_OK) == 0;
800}
801
Tom Cherryb4171692015-12-09 15:48:15 -0800802void context_node::unmap() {
803 if (!pa_) {
804 return;
805 }
806
807 munmap(pa_, pa_size);
808 if (pa_ == __system_property_area__) {
809 __system_property_area__ = nullptr;
810 }
811 pa_ = nullptr;
812}
813
Tom Cherry49a309f2015-09-23 16:09:47 -0700814static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
815 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800816 int len = __libc_format_buffer(filename, sizeof(filename),
817 "%s/properties_serial", property_filename);
Tom Cherry49a309f2015-09-23 16:09:47 -0700818 if (len < 0 || len > PROP_FILENAME_MAX) {
819 __system_property_area__ = nullptr;
820 return false;
821 }
822
823 if (access_rw) {
824 __system_property_area__ =
825 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
826 } else {
827 __system_property_area__ = map_prop_area(filename, false);
828 }
829 return __system_property_area__;
830}
831
832static prop_area* get_prop_area_for_name(const char* name) {
Tom Cherry845e24a2015-12-03 15:38:52 -0800833 auto entry = list_find(prefixes, [name](prefix_node* l) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700834 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
835 });
836 if (!entry) {
837 return nullptr;
838 }
839
840 auto cnode = entry->context;
Tom Cherryb4171692015-12-09 15:48:15 -0800841 if (!cnode->pa()) {
842 /*
843 * We explicitly do not check no_access_ in this case because unlike the
844 * case of foreach(), we want to generate an selinux audit for each
845 * non-permitted property access in this function.
846 */
847 cnode->open(false, nullptr);
Tom Cherry49a309f2015-09-23 16:09:47 -0700848 }
Tom Cherryb4171692015-12-09 15:48:15 -0800849 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700850}
851
852/*
853 * The below two functions are duplicated from label_support.c in libselinux.
854 * TODO: Find a location suitable for these functions such that both libc and
855 * libselinux can share a common source file.
856 */
857
858/*
859 * The read_spec_entries and read_spec_entry functions may be used to
860 * replace sscanf to read entries from spec files. The file and
861 * property services now use these.
862 */
863
864/* Read an entry from a spec file (e.g. file_contexts) */
865static inline int read_spec_entry(char **entry, char **ptr, int *len)
866{
867 *entry = NULL;
868 char *tmp_buf = NULL;
869
870 while (isspace(**ptr) && **ptr != '\0')
871 (*ptr)++;
872
873 tmp_buf = *ptr;
874 *len = 0;
875
876 while (!isspace(**ptr) && **ptr != '\0') {
877 (*ptr)++;
878 (*len)++;
879 }
880
881 if (*len) {
882 *entry = strndup(tmp_buf, *len);
883 if (!*entry)
884 return -1;
885 }
886
887 return 0;
888}
889
890/*
891 * line_buf - Buffer containing the spec entries .
892 * num_args - The number of spec parameter entries to process.
893 * ... - A 'char **spec_entry' for each parameter.
894 * returns - The number of items processed.
895 *
896 * This function calls read_spec_entry() to do the actual string processing.
897 */
898static int read_spec_entries(char *line_buf, int num_args, ...)
899{
900 char **spec_entry, *buf_p;
901 int len, rc, items, entry_len = 0;
902 va_list ap;
903
904 len = strlen(line_buf);
905 if (line_buf[len - 1] == '\n')
906 line_buf[len - 1] = '\0';
907 else
908 /* Handle case if line not \n terminated by bumping
909 * the len for the check below (as the line is NUL
910 * terminated by getline(3)) */
911 len++;
912
913 buf_p = line_buf;
914 while (isspace(*buf_p))
915 buf_p++;
916
917 /* Skip comment lines and empty lines. */
918 if (*buf_p == '#' || *buf_p == '\0')
919 return 0;
920
921 /* Process the spec file entries */
922 va_start(ap, num_args);
923
924 items = 0;
925 while (items < num_args) {
926 spec_entry = va_arg(ap, char **);
927
928 if (len - 1 == buf_p - line_buf) {
929 va_end(ap);
930 return items;
931 }
932
933 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
934 if (rc < 0) {
935 va_end(ap);
936 return rc;
937 }
938 if (entry_len)
939 items++;
940 }
941 va_end(ap);
942 return items;
943}
944
945static bool initialize_properties() {
Tom Cherry49a309f2015-09-23 16:09:47 -0700946 FILE* file = fopen("/property_contexts", "re");
947
948 if (!file) {
949 return false;
950 }
951
952 char* buffer = nullptr;
953 size_t line_len;
954 char* prop_prefix = nullptr;
955 char* context = nullptr;
956
957 while (getline(&buffer, &line_len, file) > 0) {
958 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
959 if (items <= 0) {
960 continue;
961 }
962 if (items == 1) {
963 free(prop_prefix);
964 continue;
965 }
Tom Cherry21eadee2015-12-04 15:53:25 -0800966 /*
967 * init uses ctl.* properties as an IPC mechanism and does not write them
968 * to a property file, therefore we do not need to create property files
969 * to store them.
970 */
971 if (!strncmp(prop_prefix, "ctl.", 4)) {
972 free(prop_prefix);
973 free(context);
974 continue;
975 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700976
Tom Cherry845e24a2015-12-03 15:38:52 -0800977 auto old_context = list_find(
Tom Cherryb4171692015-12-09 15:48:15 -0800978 contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
Tom Cherry49a309f2015-09-23 16:09:47 -0700979 if (old_context) {
980 list_add_after_len(&prefixes, prop_prefix, old_context);
981 } else {
982 list_add(&contexts, context, nullptr);
983 list_add_after_len(&prefixes, prop_prefix, contexts);
984 }
985 free(prop_prefix);
986 free(context);
987 }
988
989 free(buffer);
990 fclose(file);
991 return true;
992}
993
994static bool is_dir(const char* pathname) {
995 struct stat info;
996 if (stat(pathname, &info) == -1) {
997 return false;
998 }
999 return S_ISDIR(info.st_mode);
1000}
1001
Tom Cherryb4171692015-12-09 15:48:15 -08001002static void free_and_unmap_contexts() {
1003 list_free(&prefixes);
1004 list_free(&contexts);
1005 if (__system_property_area__) {
1006 munmap(__system_property_area__, pa_size);
1007 __system_property_area__ = nullptr;
1008 }
1009}
1010
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001011int __system_properties_init()
1012{
Tom Cherryb4171692015-12-09 15:48:15 -08001013 if (initialized) {
1014 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
1015 return 0;
1016 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001017 if (is_dir(property_filename)) {
1018 if (!initialize_properties()) {
1019 return -1;
1020 }
1021 if (!map_system_property_area(false, nullptr)) {
Tom Cherryb4171692015-12-09 15:48:15 -08001022 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001023 return -1;
1024 }
1025 } else {
1026 __system_property_area__ = map_prop_area(property_filename, true);
1027 if (!__system_property_area__) {
1028 return -1;
1029 }
1030 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1031 list_add_after_len(&prefixes, "*", contexts);
1032 }
Tom Cherryb4171692015-12-09 15:48:15 -08001033 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001034 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001035}
1036
1037int __system_property_set_filename(const char *filename)
1038{
1039 size_t len = strlen(filename);
1040 if (len >= sizeof(property_filename))
1041 return -1;
1042
1043 strcpy(property_filename, filename);
1044 return 0;
1045}
1046
1047int __system_property_area_init()
1048{
Tom Cherryb4171692015-12-09 15:48:15 -08001049 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001050 mkdir(property_filename, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1051 if (!initialize_properties()) {
1052 return -1;
1053 }
Tom Cherryb4171692015-12-09 15:48:15 -08001054 bool open_failed = false;
Tom Cherry49a309f2015-09-23 16:09:47 -07001055 bool fsetxattr_failed = false;
Tom Cherryb4171692015-12-09 15:48:15 -08001056 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1057 if (!l->open(true, &fsetxattr_failed)) {
1058 open_failed = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001059 }
1060 });
Tom Cherryb4171692015-12-09 15:48:15 -08001061 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1062 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001063 return -1;
1064 }
Tom Cherryb4171692015-12-09 15:48:15 -08001065 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001066 return fsetxattr_failed ? -2 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001067}
1068
Mark Salyzynbfd65272015-04-24 09:31:32 -07001069unsigned int __system_property_area_serial()
1070{
1071 prop_area *pa = __system_property_area__;
1072 if (!pa) {
1073 return -1;
1074 }
1075 // Make sure this read fulfilled before __system_property_serial
Tom Cherry926ebe12015-09-23 15:34:40 -07001076 return atomic_load_explicit(pa->serial(), memory_order_acquire);
Mark Salyzynbfd65272015-04-24 09:31:32 -07001077}
1078
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001079const prop_info *__system_property_find(const char *name)
1080{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001081 if (!__system_property_area__) {
1082 return nullptr;
1083 }
1084
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001085 if (__predict_false(compat_mode)) {
1086 return __system_property_find_compat(name);
1087 }
Tom Cherry926ebe12015-09-23 15:34:40 -07001088
Tom Cherry49a309f2015-09-23 16:09:47 -07001089 prop_area* pa = get_prop_area_for_name(name);
1090 if (!pa) {
1091 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001092 return nullptr;
1093 }
1094
Tom Cherry49a309f2015-09-23 16:09:47 -07001095 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001096}
1097
Hans Boehm1e8587a2014-08-19 14:07:55 -07001098// The C11 standard doesn't allow atomic loads from const fields,
1099// though C++11 does. Fudge it until standards get straightened out.
1100static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s,
1101 memory_order mo) {
1102 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1103 return atomic_load_explicit(non_const_s, mo);
1104}
1105
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001106int __system_property_read(const prop_info *pi, char *name, char *value)
1107{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001108 if (__predict_false(compat_mode)) {
1109 return __system_property_read_compat(pi, name, value);
1110 }
1111
jiaguo879d3302014-03-13 17:39:58 +08001112 while (true) {
Hans Boehm30214b92014-07-31 15:53:22 -07001113 uint32_t serial = __system_property_serial(pi); // acquire semantics
jiaguo879d3302014-03-13 17:39:58 +08001114 size_t len = SERIAL_VALUE_LEN(serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001115 memcpy(value, pi->value, len + 1);
Hans Boehm30214b92014-07-31 15:53:22 -07001116 // TODO: Fix the synchronization scheme here.
1117 // There is no fully supported way to implement this kind
1118 // of synchronization in C++11, since the memcpy races with
1119 // updates to pi, and the data being accessed is not atomic.
1120 // The following fence is unintuitive, but would be the
1121 // correct one if memcpy used memory_order_relaxed atomic accesses.
1122 // In practice it seems unlikely that the generated code would
1123 // would be any different, so this should be OK.
1124 atomic_thread_fence(memory_order_acquire);
1125 if (serial ==
Hans Boehm1e8587a2014-08-19 14:07:55 -07001126 load_const_atomic(&(pi->serial), memory_order_relaxed)) {
jiaguo879d3302014-03-13 17:39:58 +08001127 if (name != 0) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001128 strcpy(name, pi->name);
1129 }
1130 return len;
1131 }
1132 }
1133}
1134
1135int __system_property_get(const char *name, char *value)
1136{
1137 const prop_info *pi = __system_property_find(name);
1138
1139 if (pi != 0) {
1140 return __system_property_read(pi, 0, value);
1141 } else {
1142 value[0] = 0;
1143 return 0;
1144 }
1145}
1146
1147int __system_property_set(const char *key, const char *value)
1148{
1149 if (key == 0) return -1;
1150 if (value == 0) value = "";
1151 if (strlen(key) >= PROP_NAME_MAX) return -1;
1152 if (strlen(value) >= PROP_VALUE_MAX) return -1;
1153
1154 prop_msg msg;
1155 memset(&msg, 0, sizeof msg);
1156 msg.cmd = PROP_MSG_SETPROP;
1157 strlcpy(msg.name, key, sizeof msg.name);
1158 strlcpy(msg.value, value, sizeof msg.value);
1159
1160 const int err = send_prop_msg(&msg);
1161 if (err < 0) {
1162 return err;
1163 }
1164
1165 return 0;
1166}
1167
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001168int __system_property_update(prop_info *pi, const char *value, unsigned int len)
1169{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001170 if (len >= PROP_VALUE_MAX)
1171 return -1;
1172
Tom Cherry6ed51c02015-12-04 11:34:42 -08001173 prop_area* pa = __system_property_area__;
1174
1175 if (!pa) {
1176 return -1;
1177 }
1178
Hans Boehm30214b92014-07-31 15:53:22 -07001179 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1180 serial |= 1;
1181 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1182 // The memcpy call here also races. Again pretend it
1183 // used memory_order_relaxed atomics, and use the analogous
1184 // counterintuitive fence.
1185 atomic_thread_fence(memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001186 memcpy(pi->value, value, len + 1);
Hans Boehm30214b92014-07-31 15:53:22 -07001187 atomic_store_explicit(
1188 &pi->serial,
1189 (len << 24) | ((serial + 1) & 0xffffff),
1190 memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001191 __futex_wake(&pi->serial, INT32_MAX);
1192
Hans Boehm30214b92014-07-31 15:53:22 -07001193 atomic_store_explicit(
Tom Cherry926ebe12015-09-23 15:34:40 -07001194 pa->serial(),
1195 atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001196 memory_order_release);
Tom Cherry926ebe12015-09-23 15:34:40 -07001197 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001198
1199 return 0;
1200}
jiaguo879d3302014-03-13 17:39:58 +08001201
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001202int __system_property_add(const char *name, unsigned int namelen,
1203 const char *value, unsigned int valuelen)
1204{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001205 if (namelen >= PROP_NAME_MAX)
1206 return -1;
1207 if (valuelen >= PROP_VALUE_MAX)
1208 return -1;
1209 if (namelen < 1)
1210 return -1;
1211
Tom Cherry6ed51c02015-12-04 11:34:42 -08001212 if (!__system_property_area__) {
1213 return -1;
1214 }
1215
Tom Cherry49a309f2015-09-23 16:09:47 -07001216 prop_area* pa = get_prop_area_for_name(name);
1217
1218 if (!pa) {
1219 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001220 return -1;
1221 }
1222
Tom Cherry49a309f2015-09-23 16:09:47 -07001223 bool ret = pa->add(name, namelen, value, valuelen);
Tom Cherry926ebe12015-09-23 15:34:40 -07001224 if (!ret)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001225 return -1;
1226
Hans Boehm30214b92014-07-31 15:53:22 -07001227 // There is only a single mutator, but we want to make sure that
1228 // updates are visible to a reader waiting for the update.
1229 atomic_store_explicit(
Tom Cherry49a309f2015-09-23 16:09:47 -07001230 __system_property_area__->serial(),
1231 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001232 memory_order_release);
Tom Cherry49a309f2015-09-23 16:09:47 -07001233 __futex_wake(__system_property_area__->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001234 return 0;
1235}
1236
Hans Boehm30214b92014-07-31 15:53:22 -07001237// Wait for non-locked serial, and retrieve it with acquire semantics.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001238unsigned int __system_property_serial(const prop_info *pi)
1239{
Hans Boehm1e8587a2014-08-19 14:07:55 -07001240 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001241 while (SERIAL_DIRTY(serial)) {
Hans Boehm30214b92014-07-31 15:53:22 -07001242 __futex_wait(const_cast<volatile void *>(
1243 reinterpret_cast<const void *>(&pi->serial)),
1244 serial, NULL);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001245 serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001246 }
1247 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001248}
1249
1250unsigned int __system_property_wait_any(unsigned int serial)
1251{
1252 prop_area *pa = __system_property_area__;
Hans Boehm30214b92014-07-31 15:53:22 -07001253 uint32_t my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001254
Tom Cherry6ed51c02015-12-04 11:34:42 -08001255 if (!pa) {
1256 return 0;
1257 }
1258
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001259 do {
Tom Cherry926ebe12015-09-23 15:34:40 -07001260 __futex_wait(pa->serial(), serial, NULL);
1261 my_serial = atomic_load_explicit(pa->serial(), memory_order_acquire);
Hans Boehm30214b92014-07-31 15:53:22 -07001262 } while (my_serial == serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001263
Hans Boehm30214b92014-07-31 15:53:22 -07001264 return my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001265}
1266
1267const prop_info *__system_property_find_nth(unsigned n)
1268{
Dimitry Ivanov581b9f62017-01-09 11:05:52 -08001269 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
1270 __libc_fatal("__system_property_find_nth is not supported since Android O,"
1271 " please use __system_property_foreach instead.");
1272 }
1273
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001274 find_nth_cookie cookie(n);
1275
1276 const int err = __system_property_foreach(find_nth_fn, &cookie);
1277 if (err < 0) {
1278 return NULL;
1279 }
1280
1281 return cookie.pi;
1282}
1283
1284int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
1285 void *cookie)
1286{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001287 if (!__system_property_area__) {
1288 return -1;
1289 }
1290
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001291 if (__predict_false(compat_mode)) {
1292 return __system_property_foreach_compat(propfn, cookie);
1293 }
1294
Tom Cherry845e24a2015-12-03 15:38:52 -08001295 list_foreach(contexts, [propfn, cookie](context_node* l) {
Tom Cherryb4171692015-12-09 15:48:15 -08001296 if (l->check_access_and_open()) {
1297 l->pa()->foreach(propfn, cookie);
Tom Cherry49a309f2015-09-23 16:09:47 -07001298 }
1299 });
1300 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001301}