blob: 96a4017a250e1b37cfe34c157667808bebadb008 [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 Ivanovcafd3552017-01-24 12:39:33 -0800578 // Trying to send even 0 bytes to closed socket may lead to
579 // broken pipe (http://b/34670529).
580 if (valuelen == 0) {
581 return true;
582 }
583
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000584 int result = TEMP_FAILURE_RETRY(send(fd_, value, valuelen, 0));
585 return CheckSendRecvResult(result, valuelen);
586 }
587
588 bool RecvInt32(int32_t* value) {
589 int result = TEMP_FAILURE_RETRY(recv(fd_, value, sizeof(*value), MSG_WAITALL));
590 return CheckSendRecvResult(result, sizeof(*value));
591 }
592
593 int GetFd() {
594 return fd_;
595 }
596
597 ~PropertyServiceConnection() {
598 if (fd_ != -1) {
599 close(fd_);
600 }
601 }
602 private:
603 bool CheckSendRecvResult(int result, int expected_len) {
604 if (result == -1) {
605 last_error_ = errno;
606 } else if (result != expected_len) {
607 last_error_ = -1;
608 } else {
609 last_error_ = 0;
610 }
611
612 return last_error_ == 0;
613 }
614
615 int fd_;
616 int last_error_;
617};
618
619static int send_prop_msg(const prop_msg* msg) {
620 PropertyServiceConnection connection;
621 if (!connection.IsValid()) {
622 return connection.GetLastError();
623 }
Dimitry Ivanov489f58b2017-01-24 18:39:04 +0000624
625 int result = -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000626 int fd = connection.GetFd();
627
628 const int num_bytes = TEMP_FAILURE_RETRY(send(fd, msg, sizeof(prop_msg), 0));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000629 if (num_bytes == sizeof(prop_msg)) {
630 // We successfully wrote to the property server but now we
631 // wait for the property server to finish its work. It
632 // acknowledges its completion by closing the socket so we
633 // poll here (on nothing), waiting for the socket to close.
634 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
635 // once the socket closes. Out of paranoia we cap our poll
636 // at 250 ms.
637 pollfd pollfds[1];
638 pollfds[0].fd = fd;
639 pollfds[0].events = 0;
640 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
641 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
642 result = 0;
643 } else {
644 // Ignore the timeout and treat it like a success anyway.
645 // The init process is single-threaded and its property
646 // service is sometimes slow to respond (perhaps it's off
647 // starting a child process or something) and thus this
648 // times out and the caller thinks it failed, even though
649 // it's still getting around to it. So we fake it here,
650 // mostly for ctl.* properties, but we do try and wait 250
651 // ms so callers who do read-after-write can reliably see
652 // what they've written. Most of the time.
653 // TODO: fix the system properties design.
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000654 __libc_format_log(ANDROID_LOG_WARN, "libc",
655 "Property service has timed out while trying to set \"%s\" to \"%s\"",
656 msg->name, msg->value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000657 result = 0;
658 }
659 }
660
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000661 return result;
662}
663
664static void find_nth_fn(const prop_info *pi, void *ptr)
665{
666 find_nth_cookie *cookie = reinterpret_cast<find_nth_cookie*>(ptr);
667
668 if (cookie->n == cookie->count)
669 cookie->pi = pi;
670
671 cookie->count++;
672}
673
Tom Cherry926ebe12015-09-23 15:34:40 -0700674bool prop_area::foreach_property(prop_bt *const trie,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000675 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
676{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000677 if (!trie)
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 left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
681 if (left_offset != 0) {
682 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000683 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700684 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000685 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800686 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
687 if (prop_offset != 0) {
688 prop_info *info = to_prop_info(&trie->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000689 if (!info)
Tom Cherry926ebe12015-09-23 15:34:40 -0700690 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000691 propfn(info, cookie);
692 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800693 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
694 if (children_offset != 0) {
695 const int err = foreach_property(to_prop_bt(&trie->children), 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 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800699 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
700 if (right_offset != 0) {
701 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000702 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700703 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000704 }
705
Tom Cherry926ebe12015-09-23 15:34:40 -0700706 return true;
707}
708
709const prop_info *prop_area::find(const char *name) {
710 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
711}
712
713bool prop_area::add(const char *name, unsigned int namelen,
714 const char *value, unsigned int valuelen) {
715 return find_property(root_node(), name, namelen, value, valuelen, true);
716}
717
718bool prop_area::foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
719 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000720}
721
Tom Cherryb4171692015-12-09 15:48:15 -0800722class context_node {
723public:
724 context_node(context_node* next, const char* context, prop_area* pa)
725 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
726 lock_.init(false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700727 }
728 ~context_node() {
Tom Cherryb4171692015-12-09 15:48:15 -0800729 unmap();
730 free(context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700731 }
Tom Cherryb4171692015-12-09 15:48:15 -0800732 bool open(bool access_rw, bool* fsetxattr_failed);
733 bool check_access_and_open();
734 void reset_access();
735
736 const char* context() const { return context_; }
737 prop_area* pa() { return pa_; }
738
739 context_node* next;
740
741private:
742 bool check_access();
743 void unmap();
744
745 Lock lock_;
746 char* context_;
747 prop_area* pa_;
748 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700749};
750
751struct prefix_node {
752 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
753 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
754 }
755 ~prefix_node() {
756 free(prefix);
757 }
758 char* prefix;
759 const size_t prefix_len;
760 context_node* context;
761 struct prefix_node* next;
762};
763
764template <typename List, typename... Args>
765static inline void list_add(List** list, Args... args) {
766 *list = new List(*list, args...);
767}
768
769static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
770 size_t prefix_len = strlen(prefix);
771
772 auto next_list = list;
773
774 while (*next_list) {
775 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
776 list_add(next_list, prefix, context);
777 return;
778 }
779 next_list = &(*next_list)->next;
780 }
781 list_add(next_list, prefix, context);
782}
783
784template <typename List, typename Func>
785static void list_foreach(List* list, Func func) {
786 while (list) {
787 func(list);
788 list = list->next;
789 }
790}
791
792template <typename List, typename Func>
793static List* list_find(List* list, Func func) {
794 while (list) {
795 if (func(list)) {
796 return list;
797 }
798 list = list->next;
799 }
800 return nullptr;
801}
802
803template <typename List>
804static void list_free(List** list) {
805 while (*list) {
806 auto old_list = *list;
807 *list = old_list->next;
808 delete old_list;
809 }
810}
811
812static prefix_node* prefixes = nullptr;
813static context_node* contexts = nullptr;
814
815/*
816 * pthread_mutex_lock() calls into system_properties in the case of contention.
817 * This creates a risk of dead lock if any system_properties functions
818 * use pthread locks after system_property initialization.
819 *
820 * For this reason, the below three functions use a bionic Lock and static
821 * allocation of memory for each filename.
822 */
823
Tom Cherryb4171692015-12-09 15:48:15 -0800824bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
825 lock_.lock();
826 if (pa_) {
827 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700828 return true;
829 }
830
831 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800832 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
833 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700834 if (len < 0 || len > PROP_FILENAME_MAX) {
Tom Cherryb4171692015-12-09 15:48:15 -0800835 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700836 return false;
837 }
838
839 if (access_rw) {
Tom Cherryb4171692015-12-09 15:48:15 -0800840 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
Tom Cherry49a309f2015-09-23 16:09:47 -0700841 } else {
Tom Cherryb4171692015-12-09 15:48:15 -0800842 pa_ = map_prop_area(filename, false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700843 }
Tom Cherryb4171692015-12-09 15:48:15 -0800844 lock_.unlock();
845 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700846}
847
Tom Cherryb4171692015-12-09 15:48:15 -0800848bool context_node::check_access_and_open() {
849 if (!pa_ && !no_access_) {
850 if (!check_access() || !open(false, nullptr)) {
851 no_access_ = true;
852 }
853 }
854 return pa_;
855}
856
857void context_node::reset_access() {
858 if (!check_access()) {
859 unmap();
860 no_access_ = true;
861 } else {
862 no_access_ = false;
863 }
864}
865
866bool context_node::check_access() {
Tom Cherry49a309f2015-09-23 16:09:47 -0700867 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800868 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
869 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700870 if (len < 0 || len > PROP_FILENAME_MAX) {
871 return false;
872 }
873
874 return access(filename, R_OK) == 0;
875}
876
Tom Cherryb4171692015-12-09 15:48:15 -0800877void context_node::unmap() {
878 if (!pa_) {
879 return;
880 }
881
882 munmap(pa_, pa_size);
883 if (pa_ == __system_property_area__) {
884 __system_property_area__ = nullptr;
885 }
886 pa_ = nullptr;
887}
888
Tom Cherry49a309f2015-09-23 16:09:47 -0700889static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
890 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800891 int len = __libc_format_buffer(filename, sizeof(filename),
892 "%s/properties_serial", property_filename);
Tom Cherry49a309f2015-09-23 16:09:47 -0700893 if (len < 0 || len > PROP_FILENAME_MAX) {
894 __system_property_area__ = nullptr;
895 return false;
896 }
897
898 if (access_rw) {
899 __system_property_area__ =
900 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
901 } else {
902 __system_property_area__ = map_prop_area(filename, false);
903 }
904 return __system_property_area__;
905}
906
907static prop_area* get_prop_area_for_name(const char* name) {
Tom Cherry845e24a2015-12-03 15:38:52 -0800908 auto entry = list_find(prefixes, [name](prefix_node* l) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700909 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
910 });
911 if (!entry) {
912 return nullptr;
913 }
914
915 auto cnode = entry->context;
Tom Cherryb4171692015-12-09 15:48:15 -0800916 if (!cnode->pa()) {
917 /*
918 * We explicitly do not check no_access_ in this case because unlike the
919 * case of foreach(), we want to generate an selinux audit for each
920 * non-permitted property access in this function.
921 */
922 cnode->open(false, nullptr);
Tom Cherry49a309f2015-09-23 16:09:47 -0700923 }
Tom Cherryb4171692015-12-09 15:48:15 -0800924 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700925}
926
927/*
928 * The below two functions are duplicated from label_support.c in libselinux.
929 * TODO: Find a location suitable for these functions such that both libc and
930 * libselinux can share a common source file.
931 */
932
933/*
934 * The read_spec_entries and read_spec_entry functions may be used to
935 * replace sscanf to read entries from spec files. The file and
936 * property services now use these.
937 */
938
939/* Read an entry from a spec file (e.g. file_contexts) */
940static inline int read_spec_entry(char **entry, char **ptr, int *len)
941{
942 *entry = NULL;
943 char *tmp_buf = NULL;
944
945 while (isspace(**ptr) && **ptr != '\0')
946 (*ptr)++;
947
948 tmp_buf = *ptr;
949 *len = 0;
950
951 while (!isspace(**ptr) && **ptr != '\0') {
952 (*ptr)++;
953 (*len)++;
954 }
955
956 if (*len) {
957 *entry = strndup(tmp_buf, *len);
958 if (!*entry)
959 return -1;
960 }
961
962 return 0;
963}
964
965/*
966 * line_buf - Buffer containing the spec entries .
967 * num_args - The number of spec parameter entries to process.
968 * ... - A 'char **spec_entry' for each parameter.
969 * returns - The number of items processed.
970 *
971 * This function calls read_spec_entry() to do the actual string processing.
972 */
973static int read_spec_entries(char *line_buf, int num_args, ...)
974{
975 char **spec_entry, *buf_p;
976 int len, rc, items, entry_len = 0;
977 va_list ap;
978
979 len = strlen(line_buf);
980 if (line_buf[len - 1] == '\n')
981 line_buf[len - 1] = '\0';
982 else
983 /* Handle case if line not \n terminated by bumping
984 * the len for the check below (as the line is NUL
985 * terminated by getline(3)) */
986 len++;
987
988 buf_p = line_buf;
989 while (isspace(*buf_p))
990 buf_p++;
991
992 /* Skip comment lines and empty lines. */
993 if (*buf_p == '#' || *buf_p == '\0')
994 return 0;
995
996 /* Process the spec file entries */
997 va_start(ap, num_args);
998
999 items = 0;
1000 while (items < num_args) {
1001 spec_entry = va_arg(ap, char **);
1002
1003 if (len - 1 == buf_p - line_buf) {
1004 va_end(ap);
1005 return items;
1006 }
1007
1008 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
1009 if (rc < 0) {
1010 va_end(ap);
1011 return rc;
1012 }
1013 if (entry_len)
1014 items++;
1015 }
1016 va_end(ap);
1017 return items;
1018}
1019
Jorim Jaggie2f5ea12017-01-18 15:37:18 +00001020static bool initialize_properties() {
1021 FILE* file = fopen("/property_contexts", "re");
Tom Cherry49a309f2015-09-23 16:09:47 -07001022
1023 if (!file) {
1024 return false;
1025 }
1026
1027 char* buffer = nullptr;
1028 size_t line_len;
1029 char* prop_prefix = nullptr;
1030 char* context = nullptr;
1031
1032 while (getline(&buffer, &line_len, file) > 0) {
1033 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1034 if (items <= 0) {
1035 continue;
1036 }
1037 if (items == 1) {
1038 free(prop_prefix);
1039 continue;
1040 }
Tom Cherry21eadee2015-12-04 15:53:25 -08001041 /*
1042 * init uses ctl.* properties as an IPC mechanism and does not write them
1043 * to a property file, therefore we do not need to create property files
1044 * to store them.
1045 */
1046 if (!strncmp(prop_prefix, "ctl.", 4)) {
1047 free(prop_prefix);
1048 free(context);
1049 continue;
1050 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001051
Tom Cherry845e24a2015-12-03 15:38:52 -08001052 auto old_context = list_find(
Tom Cherryb4171692015-12-09 15:48:15 -08001053 contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001054 if (old_context) {
1055 list_add_after_len(&prefixes, prop_prefix, old_context);
1056 } else {
1057 list_add(&contexts, context, nullptr);
1058 list_add_after_len(&prefixes, prop_prefix, contexts);
1059 }
1060 free(prop_prefix);
1061 free(context);
1062 }
1063
1064 free(buffer);
1065 fclose(file);
1066 return true;
1067}
1068
1069static bool is_dir(const char* pathname) {
1070 struct stat info;
1071 if (stat(pathname, &info) == -1) {
1072 return false;
1073 }
1074 return S_ISDIR(info.st_mode);
1075}
1076
Tom Cherryb4171692015-12-09 15:48:15 -08001077static void free_and_unmap_contexts() {
1078 list_free(&prefixes);
1079 list_free(&contexts);
1080 if (__system_property_area__) {
1081 munmap(__system_property_area__, pa_size);
1082 __system_property_area__ = nullptr;
1083 }
1084}
1085
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001086int __system_properties_init()
1087{
Tom Cherryb4171692015-12-09 15:48:15 -08001088 if (initialized) {
1089 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
1090 return 0;
1091 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001092 if (is_dir(property_filename)) {
1093 if (!initialize_properties()) {
1094 return -1;
1095 }
1096 if (!map_system_property_area(false, nullptr)) {
Tom Cherryb4171692015-12-09 15:48:15 -08001097 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001098 return -1;
1099 }
1100 } else {
1101 __system_property_area__ = map_prop_area(property_filename, true);
1102 if (!__system_property_area__) {
1103 return -1;
1104 }
1105 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1106 list_add_after_len(&prefixes, "*", contexts);
1107 }
Tom Cherryb4171692015-12-09 15:48:15 -08001108 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001109 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001110}
1111
1112int __system_property_set_filename(const char *filename)
1113{
1114 size_t len = strlen(filename);
1115 if (len >= sizeof(property_filename))
1116 return -1;
1117
1118 strcpy(property_filename, filename);
1119 return 0;
1120}
1121
1122int __system_property_area_init()
1123{
Tom Cherryb4171692015-12-09 15:48:15 -08001124 free_and_unmap_contexts();
Nick Kralevichbb59d472017-01-06 12:04:29 -08001125 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
Tom Cherry49a309f2015-09-23 16:09:47 -07001126 if (!initialize_properties()) {
1127 return -1;
1128 }
Tom Cherryb4171692015-12-09 15:48:15 -08001129 bool open_failed = false;
Tom Cherry49a309f2015-09-23 16:09:47 -07001130 bool fsetxattr_failed = false;
Tom Cherryb4171692015-12-09 15:48:15 -08001131 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1132 if (!l->open(true, &fsetxattr_failed)) {
1133 open_failed = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001134 }
1135 });
Tom Cherryb4171692015-12-09 15:48:15 -08001136 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1137 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001138 return -1;
1139 }
Tom Cherryb4171692015-12-09 15:48:15 -08001140 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001141 return fsetxattr_failed ? -2 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001142}
1143
Mark Salyzynbfd65272015-04-24 09:31:32 -07001144unsigned int __system_property_area_serial()
1145{
1146 prop_area *pa = __system_property_area__;
1147 if (!pa) {
1148 return -1;
1149 }
1150 // Make sure this read fulfilled before __system_property_serial
Tom Cherry926ebe12015-09-23 15:34:40 -07001151 return atomic_load_explicit(pa->serial(), memory_order_acquire);
Mark Salyzynbfd65272015-04-24 09:31:32 -07001152}
1153
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001154const prop_info *__system_property_find(const char *name)
1155{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001156 if (!__system_property_area__) {
1157 return nullptr;
1158 }
1159
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001160 if (__predict_false(compat_mode)) {
1161 return __system_property_find_compat(name);
1162 }
Tom Cherry926ebe12015-09-23 15:34:40 -07001163
Tom Cherry49a309f2015-09-23 16:09:47 -07001164 prop_area* pa = get_prop_area_for_name(name);
1165 if (!pa) {
1166 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001167 return nullptr;
1168 }
1169
Tom Cherry49a309f2015-09-23 16:09:47 -07001170 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001171}
1172
Hans Boehm1e8587a2014-08-19 14:07:55 -07001173// The C11 standard doesn't allow atomic loads from const fields,
1174// though C++11 does. Fudge it until standards get straightened out.
1175static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s,
1176 memory_order mo) {
1177 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1178 return atomic_load_explicit(non_const_s, mo);
1179}
1180
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001181int __system_property_read(const prop_info *pi, char *name, char *value)
1182{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001183 if (__predict_false(compat_mode)) {
1184 return __system_property_read_compat(pi, name, value);
1185 }
1186
jiaguo879d3302014-03-13 17:39:58 +08001187 while (true) {
Hans Boehm30214b92014-07-31 15:53:22 -07001188 uint32_t serial = __system_property_serial(pi); // acquire semantics
jiaguo879d3302014-03-13 17:39:58 +08001189 size_t len = SERIAL_VALUE_LEN(serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001190 memcpy(value, pi->value, len + 1);
Hans Boehm30214b92014-07-31 15:53:22 -07001191 // TODO: Fix the synchronization scheme here.
1192 // There is no fully supported way to implement this kind
1193 // of synchronization in C++11, since the memcpy races with
1194 // updates to pi, and the data being accessed is not atomic.
1195 // The following fence is unintuitive, but would be the
1196 // correct one if memcpy used memory_order_relaxed atomic accesses.
1197 // In practice it seems unlikely that the generated code would
1198 // would be any different, so this should be OK.
1199 atomic_thread_fence(memory_order_acquire);
1200 if (serial ==
Hans Boehm1e8587a2014-08-19 14:07:55 -07001201 load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001202 if (name != nullptr) {
1203 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1204 if(namelen >= PROP_NAME_MAX) {
1205 __libc_format_log(ANDROID_LOG_ERROR, "libc",
1206 "The property name length for \"%s\" is >= %d;"
1207 " please use __system_property_read_callback"
1208 " to read this property. (the name is truncated to \"%s\")",
1209 pi->name, PROP_NAME_MAX - 1, name);
1210 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001211 }
1212 return len;
1213 }
1214 }
1215}
1216
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001217void __system_property_read_callback(const prop_info* pi,
1218 void (*callback)(void* cookie, const char* name, const char* value),
1219 void* cookie) {
1220 // TODO (dimitry): do we need compat mode for this function?
1221 if (__predict_false(compat_mode)) {
1222 char value_buf[PROP_VALUE_MAX];
1223 __system_property_read_compat(pi, nullptr, value_buf);
1224 callback(cookie, pi->name, value_buf);
1225 return;
1226 }
1227
1228 while (true) {
1229 uint32_t serial = __system_property_serial(pi); // acquire semantics
1230 size_t len = SERIAL_VALUE_LEN(serial);
1231 char value_buf[len+1];
1232
1233 memcpy(value_buf, pi->value, len);
1234 value_buf[len] = '\0';
1235
1236 // TODO: see todo in __system_property_read function
1237 atomic_thread_fence(memory_order_acquire);
1238 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1239 callback(cookie, pi->name, value_buf);
1240 return;
1241 }
1242 }
1243}
1244
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001245int __system_property_get(const char *name, char *value)
1246{
1247 const prop_info *pi = __system_property_find(name);
1248
1249 if (pi != 0) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001250 return __system_property_read(pi, nullptr, value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001251 } else {
1252 value[0] = 0;
1253 return 0;
1254 }
1255}
1256
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001257static constexpr uint32_t kProtocolVersion1 = 1;
1258static constexpr uint32_t kProtocolVersion2 = 2; // current
1259
1260static atomic_uint_least32_t g_propservice_protocol_version = 0;
1261
1262static void detect_protocol_version() {
1263 char value[PROP_VALUE_MAX];
1264 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1265 g_propservice_protocol_version = kProtocolVersion1;
1266 __libc_format_log(ANDROID_LOG_WARN, "libc",
1267 "Using old property service protocol (\"%s\" is not set)",
1268 kServiceVersionPropertyName);
1269 } else {
1270 uint32_t version = static_cast<uint32_t>(atoll(value));
1271 if (version >= kProtocolVersion2) {
1272 g_propservice_protocol_version = kProtocolVersion2;
1273 } else {
1274 __libc_format_log(ANDROID_LOG_WARN, "libc",
1275 "Using old property service protocol (\"%s\"=\"%s\")",
1276 kServiceVersionPropertyName, value);
1277 g_propservice_protocol_version = kProtocolVersion1;
1278 }
1279 }
1280}
1281
1282int __system_property_set(const char* key, const char* value) {
1283 if (key == nullptr) return -1;
1284 if (value == nullptr) value = "";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001285 if (strlen(value) >= PROP_VALUE_MAX) return -1;
1286
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001287 if (g_propservice_protocol_version == 0) {
1288 detect_protocol_version();
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001289 }
1290
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001291 int result = -1;
1292 if (g_propservice_protocol_version == kProtocolVersion1) {
1293 // Old protocol does not support long names
1294 if (strlen(key) >= PROP_NAME_MAX) return -1;
1295
1296 prop_msg msg;
1297 memset(&msg, 0, sizeof msg);
1298 msg.cmd = PROP_MSG_SETPROP;
1299 strlcpy(msg.name, key, sizeof msg.name);
1300 strlcpy(msg.value, value, sizeof msg.value);
1301
1302 result = send_prop_msg(&msg);
1303 } else {
1304 // Use proper protocol
1305 PropertyServiceConnection connection;
1306 if (connection.IsValid() &&
1307 connection.SendUint32(PROP_MSG_SETPROP2) &&
1308 connection.SendString(key) &&
1309 connection.SendString(value) &&
1310 connection.RecvInt32(&result)) {
1311 if (result != PROP_SUCCESS) {
1312 __libc_format_log(ANDROID_LOG_WARN, "libc",
1313 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1314 key, value, result);
1315 }
1316 } else {
1317 result = connection.GetLastError();
1318 __libc_format_log(ANDROID_LOG_WARN, "libc",
1319 "Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)",
1320 key, value, result, strerror(result));
1321 }
1322 }
1323
1324 return result != 0 ? -1 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001325}
1326
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001327int __system_property_update(prop_info *pi, const char *value, unsigned int len)
1328{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001329 if (len >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001330 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001331 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001332
Tom Cherry6ed51c02015-12-04 11:34:42 -08001333 prop_area* pa = __system_property_area__;
1334
1335 if (!pa) {
1336 return -1;
1337 }
1338
Hans Boehm30214b92014-07-31 15:53:22 -07001339 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1340 serial |= 1;
1341 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1342 // The memcpy call here also races. Again pretend it
1343 // used memory_order_relaxed atomics, and use the analogous
1344 // counterintuitive fence.
1345 atomic_thread_fence(memory_order_release);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001346 strlcpy(pi->value, value, len + 1);
1347
Hans Boehm30214b92014-07-31 15:53:22 -07001348 atomic_store_explicit(
1349 &pi->serial,
1350 (len << 24) | ((serial + 1) & 0xffffff),
1351 memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001352 __futex_wake(&pi->serial, INT32_MAX);
1353
Hans Boehm30214b92014-07-31 15:53:22 -07001354 atomic_store_explicit(
Tom Cherry926ebe12015-09-23 15:34:40 -07001355 pa->serial(),
1356 atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001357 memory_order_release);
Tom Cherry926ebe12015-09-23 15:34:40 -07001358 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001359
1360 return 0;
1361}
jiaguo879d3302014-03-13 17:39:58 +08001362
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001363int __system_property_add(const char *name, unsigned int namelen,
1364 const char *value, unsigned int valuelen)
1365{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001366 if (valuelen >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001367 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001368 }
1369
1370 if (namelen < 1) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001371 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001372 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001373
Tom Cherry6ed51c02015-12-04 11:34:42 -08001374 if (!__system_property_area__) {
1375 return -1;
1376 }
1377
Tom Cherry49a309f2015-09-23 16:09:47 -07001378 prop_area* pa = get_prop_area_for_name(name);
1379
1380 if (!pa) {
1381 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001382 return -1;
1383 }
1384
Tom Cherry49a309f2015-09-23 16:09:47 -07001385 bool ret = pa->add(name, namelen, value, valuelen);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001386 if (!ret) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001387 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001388 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001389
Hans Boehm30214b92014-07-31 15:53:22 -07001390 // There is only a single mutator, but we want to make sure that
1391 // updates are visible to a reader waiting for the update.
1392 atomic_store_explicit(
Tom Cherry49a309f2015-09-23 16:09:47 -07001393 __system_property_area__->serial(),
1394 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001395 memory_order_release);
Tom Cherry49a309f2015-09-23 16:09:47 -07001396 __futex_wake(__system_property_area__->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001397 return 0;
1398}
1399
Hans Boehm30214b92014-07-31 15:53:22 -07001400// Wait for non-locked serial, and retrieve it with acquire semantics.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001401unsigned int __system_property_serial(const prop_info *pi)
1402{
Hans Boehm1e8587a2014-08-19 14:07:55 -07001403 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001404 while (SERIAL_DIRTY(serial)) {
Hans Boehm30214b92014-07-31 15:53:22 -07001405 __futex_wait(const_cast<volatile void *>(
1406 reinterpret_cast<const void *>(&pi->serial)),
1407 serial, NULL);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001408 serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001409 }
1410 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001411}
1412
1413unsigned int __system_property_wait_any(unsigned int serial)
1414{
1415 prop_area *pa = __system_property_area__;
Hans Boehm30214b92014-07-31 15:53:22 -07001416 uint32_t my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001417
Tom Cherry6ed51c02015-12-04 11:34:42 -08001418 if (!pa) {
1419 return 0;
1420 }
1421
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001422 do {
Tom Cherry926ebe12015-09-23 15:34:40 -07001423 __futex_wait(pa->serial(), serial, NULL);
1424 my_serial = atomic_load_explicit(pa->serial(), memory_order_acquire);
Hans Boehm30214b92014-07-31 15:53:22 -07001425 } while (my_serial == serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001426
Hans Boehm30214b92014-07-31 15:53:22 -07001427 return my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001428}
1429
1430const prop_info *__system_property_find_nth(unsigned n)
1431{
Dimitry Ivanov581b9f62017-01-09 11:05:52 -08001432 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
1433 __libc_fatal("__system_property_find_nth is not supported since Android O,"
1434 " please use __system_property_foreach instead.");
1435 }
1436
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001437 find_nth_cookie cookie(n);
1438
1439 const int err = __system_property_foreach(find_nth_fn, &cookie);
1440 if (err < 0) {
1441 return NULL;
1442 }
1443
1444 return cookie.pi;
1445}
1446
1447int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
1448 void *cookie)
1449{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001450 if (!__system_property_area__) {
1451 return -1;
1452 }
1453
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001454 if (__predict_false(compat_mode)) {
1455 return __system_property_foreach_compat(propfn, cookie);
1456 }
1457
Tom Cherry845e24a2015-12-03 15:38:52 -08001458 list_foreach(contexts, [propfn, cookie](context_node* l) {
Tom Cherryb4171692015-12-09 15:48:15 -08001459 if (l->check_access_and_open()) {
1460 l->pa()->foreach(propfn, cookie);
Tom Cherry49a309f2015-09-23 16:09:47 -07001461 }
1462 });
1463 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001464}