blob: da71f09e81bd409b291e6413231f112713f487c8 [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
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000062#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
Dimitry Ivanov489f58b2017-01-24 18:39:04 +000063
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000064static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
65static const char* kServiceVersionPropertyName = "ro.property_service.version";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000066
67/*
68 * Properties are stored in a hybrid trie/binary tree structure.
69 * Each property's name is delimited at '.' characters, and the tokens are put
70 * into a trie structure. Siblings at each level of the trie are stored in a
71 * binary tree. For instance, "ro.secure"="1" could be stored as follows:
72 *
73 * +-----+ children +----+ children +--------+
74 * | |-------------->| ro |-------------->| secure |
75 * +-----+ +----+ +--------+
76 * / \ / |
77 * left / \ right left / | prop +===========+
78 * v v v +-------->| ro.secure |
79 * +-----+ +-----+ +-----+ +-----------+
80 * | net | | sys | | com | | 1 |
81 * +-----+ +-----+ +-----+ +===========+
82 */
83
84// Represents a node in the trie.
85struct prop_bt {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000086 uint32_t namelen;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000087
Yabin Cuib8ce4742015-02-10 21:35:56 -080088 // The property trie is updated only by the init process (single threaded) which provides
89 // property service. And it can be read by multiple threads at the same time.
90 // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the
91 // left, right, children "pointers" in the trie node. To make sure readers who see the
92 // change of "pointers" can also notice the change of prop_bt structure contents pointed by
93 // the "pointers", we always use release-consume ordering pair when accessing these "pointers".
94
95 // prop "points" to prop_info structure if there is a propery associated with the trie node.
96 // Its situation is similar to the left, right, children "pointers". So we use
97 // atomic_uint_least32_t and release-consume ordering to protect it as well.
98
Hans Boehm30214b92014-07-31 15:53:22 -070099 // We should also avoid rereading these fields redundantly, since not
100 // all processor implementations ensure that multiple loads from the
101 // same field are carried out in the right order.
Yabin Cuib8ce4742015-02-10 21:35:56 -0800102 atomic_uint_least32_t prop;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000103
Yabin Cuib8ce4742015-02-10 21:35:56 -0800104 atomic_uint_least32_t left;
105 atomic_uint_least32_t right;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000106
Yabin Cuib8ce4742015-02-10 21:35:56 -0800107 atomic_uint_least32_t children;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000108
109 char name[0];
110
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000111 prop_bt(const char *name, const uint32_t name_length) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000112 this->namelen = name_length;
113 memcpy(this->name, name, name_length);
114 this->name[name_length] = '\0';
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000115 }
116
117private:
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700118 DISALLOW_COPY_AND_ASSIGN(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000119};
120
Tom Cherry926ebe12015-09-23 15:34:40 -0700121class prop_area {
122public:
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000123
124 prop_area(const uint32_t magic, const uint32_t version) :
Tom Cherry926ebe12015-09-23 15:34:40 -0700125 magic_(magic), version_(version) {
126 atomic_init(&serial_, 0);
127 memset(reserved_, 0, sizeof(reserved_));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000128 // Allocate enough space for the root node.
Tom Cherry926ebe12015-09-23 15:34:40 -0700129 bytes_used_ = sizeof(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000130 }
131
Tom Cherry926ebe12015-09-23 15:34:40 -0700132 const prop_info *find(const char *name);
133 bool add(const char *name, unsigned int namelen,
134 const char *value, unsigned int valuelen);
135
136 bool foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie);
137
138 atomic_uint_least32_t *serial() { return &serial_; }
139 uint32_t magic() const { return magic_; }
140 uint32_t version() const { return version_; }
141
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000142private:
Tom Cherry926ebe12015-09-23 15:34:40 -0700143 void *allocate_obj(const size_t size, uint_least32_t *const off);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000144 prop_bt *new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off);
145 prop_info *new_prop_info(const char *name, uint32_t namelen,
146 const char *value, uint32_t valuelen,
Tom Cherry926ebe12015-09-23 15:34:40 -0700147 uint_least32_t *const off);
148 void *to_prop_obj(uint_least32_t off);
149 prop_bt *to_prop_bt(atomic_uint_least32_t *off_p);
150 prop_info *to_prop_info(atomic_uint_least32_t *off_p);
151
152 prop_bt *root_node();
153
154 prop_bt *find_prop_bt(prop_bt *const bt, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000155 uint32_t namelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700156
157 const prop_info *find_property(prop_bt *const trie, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000158 uint32_t namelen, const char *value,
159 uint32_t valuelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700160
161 bool foreach_property(prop_bt *const trie,
162 void (*propfn)(const prop_info *pi, void *cookie),
163 void *cookie);
164
165 uint32_t bytes_used_;
166 atomic_uint_least32_t serial_;
167 uint32_t magic_;
168 uint32_t version_;
169 uint32_t reserved_[28];
170 char data_[0];
171
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700172 DISALLOW_COPY_AND_ASSIGN(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000173};
174
175struct prop_info {
Hans Boehm30214b92014-07-31 15:53:22 -0700176 atomic_uint_least32_t serial;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000177 // we need to keep this buffer around because the property
178 // value can be modified whereas name is constant.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000179 char value[PROP_VALUE_MAX];
180 char name[0];
181
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000182 prop_info(const char *name, uint32_t namelen, const char *value, uint32_t valuelen) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000183 memcpy(this->name, name, namelen);
184 this->name[namelen] = '\0';
Hans Boehm30214b92014-07-31 15:53:22 -0700185 atomic_init(&this->serial, valuelen << 24);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000186 memcpy(this->value, value, valuelen);
187 this->value[valuelen] = '\0';
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000188 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000189 private:
190 DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000191};
192
193struct find_nth_cookie {
194 uint32_t count;
195 const uint32_t n;
196 const prop_info *pi;
197
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700198 explicit find_nth_cookie(uint32_t n) : count(0), n(n), pi(NULL) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000199 }
200};
201
Tom Cherry49a309f2015-09-23 16:09:47 -0700202static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000203static bool compat_mode = false;
204static size_t pa_data_size;
205static size_t pa_size;
Tom Cherryb4171692015-12-09 15:48:15 -0800206static bool initialized = false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000207
208// NOTE: This isn't static because system_properties_compat.c
209// requires it.
210prop_area *__system_property_area__ = NULL;
211
212static int get_fd_from_env(void)
213{
214 // This environment variable consistes of two decimal integer
215 // values separated by a ",". The first value is a file descriptor
216 // and the second is the size of the system properties area. The
217 // size is currently unused.
218 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
219
220 if (!env) {
221 return -1;
222 }
223
224 return atoi(env);
225}
226
Tom Cherry49a309f2015-09-23 16:09:47 -0700227static prop_area* map_prop_area_rw(const char* filename, const char* context,
228 bool* fsetxattr_failed) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000229 /* dev is a tmpfs that we can use to carve a shared workspace
230 * out of, so let's do that...
231 */
Tom Cherry49a309f2015-09-23 16:09:47 -0700232 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000233
234 if (fd < 0) {
235 if (errno == EACCES) {
236 /* for consistency with the case where the process has already
237 * mapped the page in and segfaults when trying to write to it
238 */
239 abort();
240 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700241 return nullptr;
242 }
243
244 if (context) {
245 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
246 __libc_format_log(ANDROID_LOG_ERROR, "libc",
247 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
248 /*
249 * fsetxattr() will fail during system properties tests due to selinux policy.
250 * We do not want to create a custom policy for the tester, so we will continue in
251 * this function but set a flag that an error has occurred.
252 * Init, which is the only daemon that should ever call this function will abort
253 * when this error occurs.
254 * Otherwise, the tester will ignore it and continue, albeit without any selinux
255 * property separation.
256 */
257 if (fsetxattr_failed) {
258 *fsetxattr_failed = true;
259 }
260 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000261 }
262
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000263 if (ftruncate(fd, PA_SIZE) < 0) {
264 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700265 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000266 }
267
268 pa_size = PA_SIZE;
269 pa_data_size = pa_size - sizeof(prop_area);
270 compat_mode = false;
271
272 void *const memory_area = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
273 if (memory_area == MAP_FAILED) {
274 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700275 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000276 }
277
278 prop_area *pa = new(memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
279
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000280 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700281 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000282}
283
Tom Cherry49a309f2015-09-23 16:09:47 -0700284static prop_area* map_fd_ro(const int fd) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000285 struct stat fd_stat;
286 if (fstat(fd, &fd_stat) < 0) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700287 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000288 }
289
290 if ((fd_stat.st_uid != 0)
291 || (fd_stat.st_gid != 0)
292 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
Narayan Kamath37e95702014-02-24 11:05:02 +0000293 || (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area))) ) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700294 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000295 }
296
297 pa_size = fd_stat.st_size;
298 pa_data_size = pa_size - sizeof(prop_area);
299
300 void* const map_result = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0);
301 if (map_result == MAP_FAILED) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700302 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000303 }
304
305 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
Tom Cherry926ebe12015-09-23 15:34:40 -0700306 if ((pa->magic() != PROP_AREA_MAGIC) ||
307 (pa->version() != PROP_AREA_VERSION &&
308 pa->version() != PROP_AREA_VERSION_COMPAT)) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000309 munmap(pa, pa_size);
Tom Cherry49a309f2015-09-23 16:09:47 -0700310 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000311 }
312
Tom Cherry926ebe12015-09-23 15:34:40 -0700313 if (pa->version() == PROP_AREA_VERSION_COMPAT) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000314 compat_mode = true;
315 }
316
Tom Cherry49a309f2015-09-23 16:09:47 -0700317 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000318}
319
Tom Cherry49a309f2015-09-23 16:09:47 -0700320static prop_area* map_prop_area(const char* filename, bool is_legacy) {
321 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000322 bool close_fd = true;
Tom Cherry49a309f2015-09-23 16:09:47 -0700323 if (fd == -1 && errno == ENOENT && is_legacy) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000324 /*
325 * For backwards compatibility, if the file doesn't
326 * exist, we use the environment to get the file descriptor.
327 * For security reasons, we only use this backup if the kernel
328 * returns ENOENT. We don't want to use the backup if the kernel
329 * returns other errors such as ENOMEM or ENFILE, since it
330 * might be possible for an external program to trigger this
331 * condition.
Tom Cherry49a309f2015-09-23 16:09:47 -0700332 * Only do this for the legacy prop file, secured prop files
333 * do not have a backup
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000334 */
335 fd = get_fd_from_env();
336 close_fd = false;
337 }
338
339 if (fd < 0) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700340 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000341 }
342
Tom Cherry49a309f2015-09-23 16:09:47 -0700343 prop_area* map_result = map_fd_ro(fd);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000344 if (close_fd) {
345 close(fd);
346 }
347
348 return map_result;
349}
350
Tom Cherry926ebe12015-09-23 15:34:40 -0700351void *prop_area::allocate_obj(const size_t size, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000352{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800353 const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t));
Tom Cherry926ebe12015-09-23 15:34:40 -0700354 if (bytes_used_ + aligned > pa_data_size) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000355 return NULL;
356 }
357
Tom Cherry926ebe12015-09-23 15:34:40 -0700358 *off = bytes_used_;
359 bytes_used_ += aligned;
360 return data_ + *off;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000361}
362
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000363prop_bt *prop_area::new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000364{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800365 uint_least32_t new_offset;
366 void *const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
367 if (p != NULL) {
368 prop_bt* bt = new(p) prop_bt(name, namelen);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000369 *off = new_offset;
370 return bt;
371 }
372
373 return NULL;
374}
375
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000376prop_info *prop_area::new_prop_info(const char *name, uint32_t namelen,
377 const char *value, uint32_t valuelen, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000378{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800379 uint_least32_t new_offset;
380 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
381 if (p != NULL) {
382 prop_info* info = new(p) prop_info(name, namelen, value, valuelen);
383 *off = new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000384 return info;
385 }
386
387 return NULL;
388}
389
Tom Cherry926ebe12015-09-23 15:34:40 -0700390void *prop_area::to_prop_obj(uint_least32_t off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000391{
392 if (off > pa_data_size)
393 return NULL;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000394
Tom Cherry926ebe12015-09-23 15:34:40 -0700395 return (data_ + off);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000396}
397
Tom Cherry926ebe12015-09-23 15:34:40 -0700398inline prop_bt *prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800399 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
400 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
401}
402
Tom Cherry926ebe12015-09-23 15:34:40 -0700403inline prop_info *prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800404 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
405 return reinterpret_cast<prop_info*>(to_prop_obj(off));
406}
407
Tom Cherry926ebe12015-09-23 15:34:40 -0700408inline prop_bt *prop_area::root_node()
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000409{
410 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
411}
412
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000413static int cmp_prop_name(const char *one, uint32_t one_len, const char *two,
414 uint32_t two_len)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000415{
416 if (one_len < two_len)
417 return -1;
418 else if (one_len > two_len)
419 return 1;
420 else
421 return strncmp(one, two, one_len);
422}
423
Tom Cherry926ebe12015-09-23 15:34:40 -0700424prop_bt *prop_area::find_prop_bt(prop_bt *const bt, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000425 uint32_t namelen, bool alloc_if_needed)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000426{
427
428 prop_bt* current = bt;
429 while (true) {
430 if (!current) {
431 return NULL;
432 }
433
434 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
435 if (ret == 0) {
436 return current;
437 }
438
439 if (ret < 0) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800440 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
441 if (left_offset != 0) {
442 current = to_prop_bt(&current->left);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000443 } else {
444 if (!alloc_if_needed) {
445 return NULL;
446 }
447
Yabin Cuib8ce4742015-02-10 21:35:56 -0800448 uint_least32_t new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000449 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
450 if (new_bt) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800451 atomic_store_explicit(&current->left, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000452 }
453 return new_bt;
454 }
455 } else {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800456 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
457 if (right_offset != 0) {
458 current = to_prop_bt(&current->right);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000459 } else {
460 if (!alloc_if_needed) {
461 return NULL;
462 }
463
Yabin Cuib8ce4742015-02-10 21:35:56 -0800464 uint_least32_t new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000465 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
466 if (new_bt) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800467 atomic_store_explicit(&current->right, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000468 }
469 return new_bt;
470 }
471 }
472 }
473}
474
Tom Cherry926ebe12015-09-23 15:34:40 -0700475const prop_info *prop_area::find_property(prop_bt *const trie, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000476 uint32_t namelen, const char *value, uint32_t valuelen,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000477 bool alloc_if_needed)
478{
479 if (!trie) return NULL;
480
481 const char *remaining_name = name;
482 prop_bt* current = trie;
483 while (true) {
484 const char *sep = strchr(remaining_name, '.');
485 const bool want_subtree = (sep != NULL);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000486 const uint32_t substr_size = (want_subtree) ?
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000487 sep - remaining_name : strlen(remaining_name);
488
489 if (!substr_size) {
490 return NULL;
491 }
492
493 prop_bt* root = NULL;
Yabin Cuib8ce4742015-02-10 21:35:56 -0800494 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
495 if (children_offset != 0) {
496 root = to_prop_bt(&current->children);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000497 } else if (alloc_if_needed) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800498 uint_least32_t new_offset;
499 root = new_prop_bt(remaining_name, substr_size, &new_offset);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000500 if (root) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800501 atomic_store_explicit(&current->children, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000502 }
503 }
504
505 if (!root) {
506 return NULL;
507 }
508
509 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
510 if (!current) {
511 return NULL;
512 }
513
514 if (!want_subtree)
515 break;
516
517 remaining_name = sep + 1;
518 }
519
Yabin Cuib8ce4742015-02-10 21:35:56 -0800520 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
521 if (prop_offset != 0) {
522 return to_prop_info(&current->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000523 } else if (alloc_if_needed) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800524 uint_least32_t new_offset;
525 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000526 if (new_info) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800527 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000528 }
529
530 return new_info;
531 } else {
532 return NULL;
533 }
534}
535
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000536class PropertyServiceConnection {
537 public:
538 PropertyServiceConnection() : last_error_(0) {
539 fd_ = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
540 if (fd_ == -1) {
541 last_error_ = errno;
542 return;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000543 }
544
545 const size_t namelen = strlen(property_service_socket);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000546 sockaddr_un addr;
547 memset(&addr, 0, sizeof(addr));
548 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
549 addr.sun_family = AF_LOCAL;
550 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000551
552 if (TEMP_FAILURE_RETRY(connect(fd_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
553 close(fd_);
554 fd_ = -1;
555 last_error_ = errno;
556 }
557 }
558
559 bool IsValid() {
560 return fd_ != -1;
561 }
562
563 int GetLastError() {
564 return last_error_;
565 }
566
567 bool SendUint32(uint32_t value) {
568 int result = TEMP_FAILURE_RETRY(send(fd_, &value, sizeof(value), 0));
569 return CheckSendRecvResult(result, sizeof(value));
570 }
571
572 bool SendString(const char* value) {
573 uint32_t valuelen = strlen(value);
574 if (!SendUint32(valuelen)) {
575 return false;
Dimitry Ivanov5c1ce272015-12-03 11:26:38 -0800576 }
Dimitry Ivanov5c1ce272015-12-03 11:26:38 -0800577
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000578 int result = TEMP_FAILURE_RETRY(send(fd_, value, valuelen, 0));
579 return CheckSendRecvResult(result, valuelen);
580 }
581
582 bool RecvInt32(int32_t* value) {
583 int result = TEMP_FAILURE_RETRY(recv(fd_, value, sizeof(*value), MSG_WAITALL));
584 return CheckSendRecvResult(result, sizeof(*value));
585 }
586
587 int GetFd() {
588 return fd_;
589 }
590
591 ~PropertyServiceConnection() {
592 if (fd_ != -1) {
593 close(fd_);
594 }
595 }
596 private:
597 bool CheckSendRecvResult(int result, int expected_len) {
598 if (result == -1) {
599 last_error_ = errno;
600 } else if (result != expected_len) {
601 last_error_ = -1;
602 } else {
603 last_error_ = 0;
604 }
605
606 return last_error_ == 0;
607 }
608
609 int fd_;
610 int last_error_;
611};
612
613static int send_prop_msg(const prop_msg* msg) {
614 PropertyServiceConnection connection;
615 if (!connection.IsValid()) {
616 return connection.GetLastError();
617 }
Dimitry Ivanov489f58b2017-01-24 18:39:04 +0000618
619 int result = -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000620 int fd = connection.GetFd();
621
622 const int num_bytes = TEMP_FAILURE_RETRY(send(fd, msg, sizeof(prop_msg), 0));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000623 if (num_bytes == sizeof(prop_msg)) {
624 // We successfully wrote to the property server but now we
625 // wait for the property server to finish its work. It
626 // acknowledges its completion by closing the socket so we
627 // poll here (on nothing), waiting for the socket to close.
628 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
629 // once the socket closes. Out of paranoia we cap our poll
630 // at 250 ms.
631 pollfd pollfds[1];
632 pollfds[0].fd = fd;
633 pollfds[0].events = 0;
634 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
635 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
636 result = 0;
637 } else {
638 // Ignore the timeout and treat it like a success anyway.
639 // The init process is single-threaded and its property
640 // service is sometimes slow to respond (perhaps it's off
641 // starting a child process or something) and thus this
642 // times out and the caller thinks it failed, even though
643 // it's still getting around to it. So we fake it here,
644 // mostly for ctl.* properties, but we do try and wait 250
645 // ms so callers who do read-after-write can reliably see
646 // what they've written. Most of the time.
647 // TODO: fix the system properties design.
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000648 __libc_format_log(ANDROID_LOG_WARN, "libc",
649 "Property service has timed out while trying to set \"%s\" to \"%s\"",
650 msg->name, msg->value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000651 result = 0;
652 }
653 }
654
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000655 return result;
656}
657
658static void find_nth_fn(const prop_info *pi, void *ptr)
659{
660 find_nth_cookie *cookie = reinterpret_cast<find_nth_cookie*>(ptr);
661
662 if (cookie->n == cookie->count)
663 cookie->pi = pi;
664
665 cookie->count++;
666}
667
Tom Cherry926ebe12015-09-23 15:34:40 -0700668bool prop_area::foreach_property(prop_bt *const trie,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000669 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
670{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000671 if (!trie)
Tom Cherry926ebe12015-09-23 15:34:40 -0700672 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000673
Yabin Cuib8ce4742015-02-10 21:35:56 -0800674 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
675 if (left_offset != 0) {
676 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000677 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700678 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000679 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800680 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
681 if (prop_offset != 0) {
682 prop_info *info = to_prop_info(&trie->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000683 if (!info)
Tom Cherry926ebe12015-09-23 15:34:40 -0700684 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000685 propfn(info, cookie);
686 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800687 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
688 if (children_offset != 0) {
689 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000690 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700691 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000692 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800693 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
694 if (right_offset != 0) {
695 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000696 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700697 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000698 }
699
Tom Cherry926ebe12015-09-23 15:34:40 -0700700 return true;
701}
702
703const prop_info *prop_area::find(const char *name) {
704 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
705}
706
707bool prop_area::add(const char *name, unsigned int namelen,
708 const char *value, unsigned int valuelen) {
709 return find_property(root_node(), name, namelen, value, valuelen, true);
710}
711
712bool prop_area::foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
713 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000714}
715
Tom Cherryb4171692015-12-09 15:48:15 -0800716class context_node {
717public:
718 context_node(context_node* next, const char* context, prop_area* pa)
719 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
720 lock_.init(false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700721 }
722 ~context_node() {
Tom Cherryb4171692015-12-09 15:48:15 -0800723 unmap();
724 free(context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700725 }
Tom Cherryb4171692015-12-09 15:48:15 -0800726 bool open(bool access_rw, bool* fsetxattr_failed);
727 bool check_access_and_open();
728 void reset_access();
729
730 const char* context() const { return context_; }
731 prop_area* pa() { return pa_; }
732
733 context_node* next;
734
735private:
736 bool check_access();
737 void unmap();
738
739 Lock lock_;
740 char* context_;
741 prop_area* pa_;
742 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700743};
744
745struct prefix_node {
746 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
747 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
748 }
749 ~prefix_node() {
750 free(prefix);
751 }
752 char* prefix;
753 const size_t prefix_len;
754 context_node* context;
755 struct prefix_node* next;
756};
757
758template <typename List, typename... Args>
759static inline void list_add(List** list, Args... args) {
760 *list = new List(*list, args...);
761}
762
763static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
764 size_t prefix_len = strlen(prefix);
765
766 auto next_list = list;
767
768 while (*next_list) {
769 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
770 list_add(next_list, prefix, context);
771 return;
772 }
773 next_list = &(*next_list)->next;
774 }
775 list_add(next_list, prefix, context);
776}
777
778template <typename List, typename Func>
779static void list_foreach(List* list, Func func) {
780 while (list) {
781 func(list);
782 list = list->next;
783 }
784}
785
786template <typename List, typename Func>
787static List* list_find(List* list, Func func) {
788 while (list) {
789 if (func(list)) {
790 return list;
791 }
792 list = list->next;
793 }
794 return nullptr;
795}
796
797template <typename List>
798static void list_free(List** list) {
799 while (*list) {
800 auto old_list = *list;
801 *list = old_list->next;
802 delete old_list;
803 }
804}
805
806static prefix_node* prefixes = nullptr;
807static context_node* contexts = nullptr;
808
809/*
810 * pthread_mutex_lock() calls into system_properties in the case of contention.
811 * This creates a risk of dead lock if any system_properties functions
812 * use pthread locks after system_property initialization.
813 *
814 * For this reason, the below three functions use a bionic Lock and static
815 * allocation of memory for each filename.
816 */
817
Tom Cherryb4171692015-12-09 15:48:15 -0800818bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
819 lock_.lock();
820 if (pa_) {
821 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700822 return true;
823 }
824
825 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800826 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
827 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700828 if (len < 0 || len > PROP_FILENAME_MAX) {
Tom Cherryb4171692015-12-09 15:48:15 -0800829 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700830 return false;
831 }
832
833 if (access_rw) {
Tom Cherryb4171692015-12-09 15:48:15 -0800834 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
Tom Cherry49a309f2015-09-23 16:09:47 -0700835 } else {
Tom Cherryb4171692015-12-09 15:48:15 -0800836 pa_ = map_prop_area(filename, false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700837 }
Tom Cherryb4171692015-12-09 15:48:15 -0800838 lock_.unlock();
839 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700840}
841
Tom Cherryb4171692015-12-09 15:48:15 -0800842bool context_node::check_access_and_open() {
843 if (!pa_ && !no_access_) {
844 if (!check_access() || !open(false, nullptr)) {
845 no_access_ = true;
846 }
847 }
848 return pa_;
849}
850
851void context_node::reset_access() {
852 if (!check_access()) {
853 unmap();
854 no_access_ = true;
855 } else {
856 no_access_ = false;
857 }
858}
859
860bool context_node::check_access() {
Tom Cherry49a309f2015-09-23 16:09:47 -0700861 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800862 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
863 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700864 if (len < 0 || len > PROP_FILENAME_MAX) {
865 return false;
866 }
867
868 return access(filename, R_OK) == 0;
869}
870
Tom Cherryb4171692015-12-09 15:48:15 -0800871void context_node::unmap() {
872 if (!pa_) {
873 return;
874 }
875
876 munmap(pa_, pa_size);
877 if (pa_ == __system_property_area__) {
878 __system_property_area__ = nullptr;
879 }
880 pa_ = nullptr;
881}
882
Tom Cherry49a309f2015-09-23 16:09:47 -0700883static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
884 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800885 int len = __libc_format_buffer(filename, sizeof(filename),
886 "%s/properties_serial", property_filename);
Tom Cherry49a309f2015-09-23 16:09:47 -0700887 if (len < 0 || len > PROP_FILENAME_MAX) {
888 __system_property_area__ = nullptr;
889 return false;
890 }
891
892 if (access_rw) {
893 __system_property_area__ =
894 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
895 } else {
896 __system_property_area__ = map_prop_area(filename, false);
897 }
898 return __system_property_area__;
899}
900
901static prop_area* get_prop_area_for_name(const char* name) {
Tom Cherry845e24a2015-12-03 15:38:52 -0800902 auto entry = list_find(prefixes, [name](prefix_node* l) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700903 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
904 });
905 if (!entry) {
906 return nullptr;
907 }
908
909 auto cnode = entry->context;
Tom Cherryb4171692015-12-09 15:48:15 -0800910 if (!cnode->pa()) {
911 /*
912 * We explicitly do not check no_access_ in this case because unlike the
913 * case of foreach(), we want to generate an selinux audit for each
914 * non-permitted property access in this function.
915 */
916 cnode->open(false, nullptr);
Tom Cherry49a309f2015-09-23 16:09:47 -0700917 }
Tom Cherryb4171692015-12-09 15:48:15 -0800918 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700919}
920
921/*
922 * The below two functions are duplicated from label_support.c in libselinux.
923 * TODO: Find a location suitable for these functions such that both libc and
924 * libselinux can share a common source file.
925 */
926
927/*
928 * The read_spec_entries and read_spec_entry functions may be used to
929 * replace sscanf to read entries from spec files. The file and
930 * property services now use these.
931 */
932
933/* Read an entry from a spec file (e.g. file_contexts) */
934static inline int read_spec_entry(char **entry, char **ptr, int *len)
935{
936 *entry = NULL;
937 char *tmp_buf = NULL;
938
939 while (isspace(**ptr) && **ptr != '\0')
940 (*ptr)++;
941
942 tmp_buf = *ptr;
943 *len = 0;
944
945 while (!isspace(**ptr) && **ptr != '\0') {
946 (*ptr)++;
947 (*len)++;
948 }
949
950 if (*len) {
951 *entry = strndup(tmp_buf, *len);
952 if (!*entry)
953 return -1;
954 }
955
956 return 0;
957}
958
959/*
960 * line_buf - Buffer containing the spec entries .
961 * num_args - The number of spec parameter entries to process.
962 * ... - A 'char **spec_entry' for each parameter.
963 * returns - The number of items processed.
964 *
965 * This function calls read_spec_entry() to do the actual string processing.
966 */
967static int read_spec_entries(char *line_buf, int num_args, ...)
968{
969 char **spec_entry, *buf_p;
970 int len, rc, items, entry_len = 0;
971 va_list ap;
972
973 len = strlen(line_buf);
974 if (line_buf[len - 1] == '\n')
975 line_buf[len - 1] = '\0';
976 else
977 /* Handle case if line not \n terminated by bumping
978 * the len for the check below (as the line is NUL
979 * terminated by getline(3)) */
980 len++;
981
982 buf_p = line_buf;
983 while (isspace(*buf_p))
984 buf_p++;
985
986 /* Skip comment lines and empty lines. */
987 if (*buf_p == '#' || *buf_p == '\0')
988 return 0;
989
990 /* Process the spec file entries */
991 va_start(ap, num_args);
992
993 items = 0;
994 while (items < num_args) {
995 spec_entry = va_arg(ap, char **);
996
997 if (len - 1 == buf_p - line_buf) {
998 va_end(ap);
999 return items;
1000 }
1001
1002 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
1003 if (rc < 0) {
1004 va_end(ap);
1005 return rc;
1006 }
1007 if (entry_len)
1008 items++;
1009 }
1010 va_end(ap);
1011 return items;
1012}
1013
Jorim Jaggie2f5ea12017-01-18 15:37:18 +00001014static bool initialize_properties() {
1015 FILE* file = fopen("/property_contexts", "re");
Tom Cherry49a309f2015-09-23 16:09:47 -07001016
1017 if (!file) {
1018 return false;
1019 }
1020
1021 char* buffer = nullptr;
1022 size_t line_len;
1023 char* prop_prefix = nullptr;
1024 char* context = nullptr;
1025
1026 while (getline(&buffer, &line_len, file) > 0) {
1027 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1028 if (items <= 0) {
1029 continue;
1030 }
1031 if (items == 1) {
1032 free(prop_prefix);
1033 continue;
1034 }
Tom Cherry21eadee2015-12-04 15:53:25 -08001035 /*
1036 * init uses ctl.* properties as an IPC mechanism and does not write them
1037 * to a property file, therefore we do not need to create property files
1038 * to store them.
1039 */
1040 if (!strncmp(prop_prefix, "ctl.", 4)) {
1041 free(prop_prefix);
1042 free(context);
1043 continue;
1044 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001045
Tom Cherry845e24a2015-12-03 15:38:52 -08001046 auto old_context = list_find(
Tom Cherryb4171692015-12-09 15:48:15 -08001047 contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001048 if (old_context) {
1049 list_add_after_len(&prefixes, prop_prefix, old_context);
1050 } else {
1051 list_add(&contexts, context, nullptr);
1052 list_add_after_len(&prefixes, prop_prefix, contexts);
1053 }
1054 free(prop_prefix);
1055 free(context);
1056 }
1057
1058 free(buffer);
1059 fclose(file);
1060 return true;
1061}
1062
1063static bool is_dir(const char* pathname) {
1064 struct stat info;
1065 if (stat(pathname, &info) == -1) {
1066 return false;
1067 }
1068 return S_ISDIR(info.st_mode);
1069}
1070
Tom Cherryb4171692015-12-09 15:48:15 -08001071static void free_and_unmap_contexts() {
1072 list_free(&prefixes);
1073 list_free(&contexts);
1074 if (__system_property_area__) {
1075 munmap(__system_property_area__, pa_size);
1076 __system_property_area__ = nullptr;
1077 }
1078}
1079
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001080int __system_properties_init()
1081{
Tom Cherryb4171692015-12-09 15:48:15 -08001082 if (initialized) {
1083 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
1084 return 0;
1085 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001086 if (is_dir(property_filename)) {
1087 if (!initialize_properties()) {
1088 return -1;
1089 }
1090 if (!map_system_property_area(false, nullptr)) {
Tom Cherryb4171692015-12-09 15:48:15 -08001091 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001092 return -1;
1093 }
1094 } else {
1095 __system_property_area__ = map_prop_area(property_filename, true);
1096 if (!__system_property_area__) {
1097 return -1;
1098 }
1099 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1100 list_add_after_len(&prefixes, "*", contexts);
1101 }
Tom Cherryb4171692015-12-09 15:48:15 -08001102 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001103 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001104}
1105
1106int __system_property_set_filename(const char *filename)
1107{
1108 size_t len = strlen(filename);
1109 if (len >= sizeof(property_filename))
1110 return -1;
1111
1112 strcpy(property_filename, filename);
1113 return 0;
1114}
1115
1116int __system_property_area_init()
1117{
Tom Cherryb4171692015-12-09 15:48:15 -08001118 free_and_unmap_contexts();
Nick Kralevichbb59d472017-01-06 12:04:29 -08001119 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
Tom Cherry49a309f2015-09-23 16:09:47 -07001120 if (!initialize_properties()) {
1121 return -1;
1122 }
Tom Cherryb4171692015-12-09 15:48:15 -08001123 bool open_failed = false;
Tom Cherry49a309f2015-09-23 16:09:47 -07001124 bool fsetxattr_failed = false;
Tom Cherryb4171692015-12-09 15:48:15 -08001125 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1126 if (!l->open(true, &fsetxattr_failed)) {
1127 open_failed = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001128 }
1129 });
Tom Cherryb4171692015-12-09 15:48:15 -08001130 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1131 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001132 return -1;
1133 }
Tom Cherryb4171692015-12-09 15:48:15 -08001134 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001135 return fsetxattr_failed ? -2 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001136}
1137
Mark Salyzynbfd65272015-04-24 09:31:32 -07001138unsigned int __system_property_area_serial()
1139{
1140 prop_area *pa = __system_property_area__;
1141 if (!pa) {
1142 return -1;
1143 }
1144 // Make sure this read fulfilled before __system_property_serial
Tom Cherry926ebe12015-09-23 15:34:40 -07001145 return atomic_load_explicit(pa->serial(), memory_order_acquire);
Mark Salyzynbfd65272015-04-24 09:31:32 -07001146}
1147
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001148const prop_info *__system_property_find(const char *name)
1149{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001150 if (!__system_property_area__) {
1151 return nullptr;
1152 }
1153
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001154 if (__predict_false(compat_mode)) {
1155 return __system_property_find_compat(name);
1156 }
Tom Cherry926ebe12015-09-23 15:34:40 -07001157
Tom Cherry49a309f2015-09-23 16:09:47 -07001158 prop_area* pa = get_prop_area_for_name(name);
1159 if (!pa) {
1160 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001161 return nullptr;
1162 }
1163
Tom Cherry49a309f2015-09-23 16:09:47 -07001164 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001165}
1166
Hans Boehm1e8587a2014-08-19 14:07:55 -07001167// The C11 standard doesn't allow atomic loads from const fields,
1168// though C++11 does. Fudge it until standards get straightened out.
1169static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s,
1170 memory_order mo) {
1171 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1172 return atomic_load_explicit(non_const_s, mo);
1173}
1174
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001175int __system_property_read(const prop_info *pi, char *name, char *value)
1176{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001177 if (__predict_false(compat_mode)) {
1178 return __system_property_read_compat(pi, name, value);
1179 }
1180
jiaguo879d3302014-03-13 17:39:58 +08001181 while (true) {
Hans Boehm30214b92014-07-31 15:53:22 -07001182 uint32_t serial = __system_property_serial(pi); // acquire semantics
jiaguo879d3302014-03-13 17:39:58 +08001183 size_t len = SERIAL_VALUE_LEN(serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001184 memcpy(value, pi->value, len + 1);
Hans Boehm30214b92014-07-31 15:53:22 -07001185 // TODO: Fix the synchronization scheme here.
1186 // There is no fully supported way to implement this kind
1187 // of synchronization in C++11, since the memcpy races with
1188 // updates to pi, and the data being accessed is not atomic.
1189 // The following fence is unintuitive, but would be the
1190 // correct one if memcpy used memory_order_relaxed atomic accesses.
1191 // In practice it seems unlikely that the generated code would
1192 // would be any different, so this should be OK.
1193 atomic_thread_fence(memory_order_acquire);
1194 if (serial ==
Hans Boehm1e8587a2014-08-19 14:07:55 -07001195 load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001196 if (name != nullptr) {
1197 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1198 if(namelen >= PROP_NAME_MAX) {
1199 __libc_format_log(ANDROID_LOG_ERROR, "libc",
1200 "The property name length for \"%s\" is >= %d;"
1201 " please use __system_property_read_callback"
1202 " to read this property. (the name is truncated to \"%s\")",
1203 pi->name, PROP_NAME_MAX - 1, name);
1204 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001205 }
1206 return len;
1207 }
1208 }
1209}
1210
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001211void __system_property_read_callback(const prop_info* pi,
1212 void (*callback)(void* cookie, const char* name, const char* value),
1213 void* cookie) {
1214 // TODO (dimitry): do we need compat mode for this function?
1215 if (__predict_false(compat_mode)) {
1216 char value_buf[PROP_VALUE_MAX];
1217 __system_property_read_compat(pi, nullptr, value_buf);
1218 callback(cookie, pi->name, value_buf);
1219 return;
1220 }
1221
1222 while (true) {
1223 uint32_t serial = __system_property_serial(pi); // acquire semantics
1224 size_t len = SERIAL_VALUE_LEN(serial);
1225 char value_buf[len+1];
1226
1227 memcpy(value_buf, pi->value, len);
1228 value_buf[len] = '\0';
1229
1230 // TODO: see todo in __system_property_read function
1231 atomic_thread_fence(memory_order_acquire);
1232 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1233 callback(cookie, pi->name, value_buf);
1234 return;
1235 }
1236 }
1237}
1238
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001239int __system_property_get(const char *name, char *value)
1240{
1241 const prop_info *pi = __system_property_find(name);
1242
1243 if (pi != 0) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001244 return __system_property_read(pi, nullptr, value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001245 } else {
1246 value[0] = 0;
1247 return 0;
1248 }
1249}
1250
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001251static constexpr uint32_t kProtocolVersion1 = 1;
1252static constexpr uint32_t kProtocolVersion2 = 2; // current
1253
1254static atomic_uint_least32_t g_propservice_protocol_version = 0;
1255
1256static void detect_protocol_version() {
1257 char value[PROP_VALUE_MAX];
1258 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1259 g_propservice_protocol_version = kProtocolVersion1;
1260 __libc_format_log(ANDROID_LOG_WARN, "libc",
1261 "Using old property service protocol (\"%s\" is not set)",
1262 kServiceVersionPropertyName);
1263 } else {
1264 uint32_t version = static_cast<uint32_t>(atoll(value));
1265 if (version >= kProtocolVersion2) {
1266 g_propservice_protocol_version = kProtocolVersion2;
1267 } else {
1268 __libc_format_log(ANDROID_LOG_WARN, "libc",
1269 "Using old property service protocol (\"%s\"=\"%s\")",
1270 kServiceVersionPropertyName, value);
1271 g_propservice_protocol_version = kProtocolVersion1;
1272 }
1273 }
1274}
1275
1276int __system_property_set(const char* key, const char* value) {
1277 if (key == nullptr) return -1;
1278 if (value == nullptr) value = "";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001279 if (strlen(value) >= PROP_VALUE_MAX) return -1;
1280
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001281 if (g_propservice_protocol_version == 0) {
1282 detect_protocol_version();
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001283 }
1284
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001285 int result = -1;
1286 if (g_propservice_protocol_version == kProtocolVersion1) {
1287 // Old protocol does not support long names
1288 if (strlen(key) >= PROP_NAME_MAX) return -1;
1289
1290 prop_msg msg;
1291 memset(&msg, 0, sizeof msg);
1292 msg.cmd = PROP_MSG_SETPROP;
1293 strlcpy(msg.name, key, sizeof msg.name);
1294 strlcpy(msg.value, value, sizeof msg.value);
1295
1296 result = send_prop_msg(&msg);
1297 } else {
1298 // Use proper protocol
1299 PropertyServiceConnection connection;
1300 if (connection.IsValid() &&
1301 connection.SendUint32(PROP_MSG_SETPROP2) &&
1302 connection.SendString(key) &&
1303 connection.SendString(value) &&
1304 connection.RecvInt32(&result)) {
1305 if (result != PROP_SUCCESS) {
1306 __libc_format_log(ANDROID_LOG_WARN, "libc",
1307 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1308 key, value, result);
1309 }
1310 } else {
1311 result = connection.GetLastError();
1312 __libc_format_log(ANDROID_LOG_WARN, "libc",
1313 "Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)",
1314 key, value, result, strerror(result));
1315 }
1316 }
1317
1318 return result != 0 ? -1 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001319}
1320
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001321int __system_property_update(prop_info *pi, const char *value, unsigned int len)
1322{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001323 if (len >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001324 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001325 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001326
Tom Cherry6ed51c02015-12-04 11:34:42 -08001327 prop_area* pa = __system_property_area__;
1328
1329 if (!pa) {
1330 return -1;
1331 }
1332
Hans Boehm30214b92014-07-31 15:53:22 -07001333 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1334 serial |= 1;
1335 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1336 // The memcpy call here also races. Again pretend it
1337 // used memory_order_relaxed atomics, and use the analogous
1338 // counterintuitive fence.
1339 atomic_thread_fence(memory_order_release);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001340 strlcpy(pi->value, value, len + 1);
1341
Hans Boehm30214b92014-07-31 15:53:22 -07001342 atomic_store_explicit(
1343 &pi->serial,
1344 (len << 24) | ((serial + 1) & 0xffffff),
1345 memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001346 __futex_wake(&pi->serial, INT32_MAX);
1347
Hans Boehm30214b92014-07-31 15:53:22 -07001348 atomic_store_explicit(
Tom Cherry926ebe12015-09-23 15:34:40 -07001349 pa->serial(),
1350 atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001351 memory_order_release);
Tom Cherry926ebe12015-09-23 15:34:40 -07001352 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001353
1354 return 0;
1355}
jiaguo879d3302014-03-13 17:39:58 +08001356
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001357int __system_property_add(const char *name, unsigned int namelen,
1358 const char *value, unsigned int valuelen)
1359{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001360 if (valuelen >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001361 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001362 }
1363
1364 if (namelen < 1) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001365 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001366 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001367
Tom Cherry6ed51c02015-12-04 11:34:42 -08001368 if (!__system_property_area__) {
1369 return -1;
1370 }
1371
Tom Cherry49a309f2015-09-23 16:09:47 -07001372 prop_area* pa = get_prop_area_for_name(name);
1373
1374 if (!pa) {
1375 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001376 return -1;
1377 }
1378
Tom Cherry49a309f2015-09-23 16:09:47 -07001379 bool ret = pa->add(name, namelen, value, valuelen);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001380 if (!ret) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001381 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001382 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001383
Hans Boehm30214b92014-07-31 15:53:22 -07001384 // There is only a single mutator, but we want to make sure that
1385 // updates are visible to a reader waiting for the update.
1386 atomic_store_explicit(
Tom Cherry49a309f2015-09-23 16:09:47 -07001387 __system_property_area__->serial(),
1388 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001389 memory_order_release);
Tom Cherry49a309f2015-09-23 16:09:47 -07001390 __futex_wake(__system_property_area__->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001391 return 0;
1392}
1393
Hans Boehm30214b92014-07-31 15:53:22 -07001394// Wait for non-locked serial, and retrieve it with acquire semantics.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001395unsigned int __system_property_serial(const prop_info *pi)
1396{
Hans Boehm1e8587a2014-08-19 14:07:55 -07001397 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001398 while (SERIAL_DIRTY(serial)) {
Hans Boehm30214b92014-07-31 15:53:22 -07001399 __futex_wait(const_cast<volatile void *>(
1400 reinterpret_cast<const void *>(&pi->serial)),
1401 serial, NULL);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001402 serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001403 }
1404 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001405}
1406
1407unsigned int __system_property_wait_any(unsigned int serial)
1408{
1409 prop_area *pa = __system_property_area__;
Hans Boehm30214b92014-07-31 15:53:22 -07001410 uint32_t my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001411
Tom Cherry6ed51c02015-12-04 11:34:42 -08001412 if (!pa) {
1413 return 0;
1414 }
1415
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001416 do {
Tom Cherry926ebe12015-09-23 15:34:40 -07001417 __futex_wait(pa->serial(), serial, NULL);
1418 my_serial = atomic_load_explicit(pa->serial(), memory_order_acquire);
Hans Boehm30214b92014-07-31 15:53:22 -07001419 } while (my_serial == serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001420
Hans Boehm30214b92014-07-31 15:53:22 -07001421 return my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001422}
1423
1424const prop_info *__system_property_find_nth(unsigned n)
1425{
Dimitry Ivanov581b9f62017-01-09 11:05:52 -08001426 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
1427 __libc_fatal("__system_property_find_nth is not supported since Android O,"
1428 " please use __system_property_foreach instead.");
1429 }
1430
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001431 find_nth_cookie cookie(n);
1432
1433 const int err = __system_property_foreach(find_nth_fn, &cookie);
1434 if (err < 0) {
1435 return NULL;
1436 }
1437
1438 return cookie.pi;
1439}
1440
1441int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
1442 void *cookie)
1443{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001444 if (!__system_property_area__) {
1445 return -1;
1446 }
1447
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001448 if (__predict_false(compat_mode)) {
1449 return __system_property_foreach_compat(propfn, cookie);
1450 }
1451
Tom Cherry845e24a2015-12-03 15:38:52 -08001452 list_foreach(contexts, [propfn, cookie](context_node* l) {
Tom Cherryb4171692015-12-09 15:48:15 -08001453 if (l->check_access_and_open()) {
1454 l->pa()->foreach(propfn, cookie);
Tom Cherry49a309f2015-09-23 16:09:47 -07001455 }
1456 });
1457 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001458}