blob: d5f48d203254d85221341625d8c028aab823d50d [file] [log] [blame]
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Elliott Hughesf8562c52017-01-26 16:48:57 -080028
Tom Cherry49a309f2015-09-23 16:09:47 -070029#include <ctype.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000030#include <errno.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000031#include <fcntl.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070032#include <poll.h>
33#include <stdatomic.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000034#include <stdbool.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070035#include <stddef.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <stdlib.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000039#include <string.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070040#include <unistd.h>
41#include <new>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000042
Tom Cherry49a309f2015-09-23 16:09:47 -070043#include <linux/xattr.h>
44#include <netinet/in.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000045#include <sys/mman.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000046#include <sys/select.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070047#include <sys/socket.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000048#include <sys/stat.h>
49#include <sys/types.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070050#include <sys/un.h>
51#include <sys/xattr.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000052
53#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
54#include <sys/_system_properties.h>
55#include <sys/system_properties.h>
56
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070057#include "private/bionic_futex.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070058#include "private/bionic_lock.h"
Elliott Hughes8eac9af2014-05-09 19:12:08 -070059#include "private/bionic_macros.h"
Dimitry Ivanov581b9f62017-01-09 11:05:52 -080060#include "private/bionic_sdk_version.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070061#include "private/libc_logging.h"
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000062
Elliott Hughesf8562c52017-01-26 16:48:57 -080063static constexpr int PROP_FILENAME_MAX = 1024;
64
65static constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250;
66static constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab;
67
68static constexpr size_t PA_SIZE = 128 * 1024;
69
70#define SERIAL_DIRTY(serial) ((serial) & 1)
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000071#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
Dimitry Ivanov489f58b2017-01-24 18:39:04 +000072
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000073static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
74static const char* kServiceVersionPropertyName = "ro.property_service.version";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000075
76/*
77 * Properties are stored in a hybrid trie/binary tree structure.
78 * Each property's name is delimited at '.' characters, and the tokens are put
79 * into a trie structure. Siblings at each level of the trie are stored in a
80 * binary tree. For instance, "ro.secure"="1" could be stored as follows:
81 *
82 * +-----+ children +----+ children +--------+
83 * | |-------------->| ro |-------------->| secure |
84 * +-----+ +----+ +--------+
85 * / \ / |
86 * left / \ right left / | prop +===========+
87 * v v v +-------->| ro.secure |
88 * +-----+ +-----+ +-----+ +-----------+
89 * | net | | sys | | com | | 1 |
90 * +-----+ +-----+ +-----+ +===========+
91 */
92
93// Represents a node in the trie.
94struct prop_bt {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000095 uint32_t namelen;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000096
Yabin Cuib8ce4742015-02-10 21:35:56 -080097 // The property trie is updated only by the init process (single threaded) which provides
98 // property service. And it can be read by multiple threads at the same time.
99 // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the
100 // left, right, children "pointers" in the trie node. To make sure readers who see the
101 // change of "pointers" can also notice the change of prop_bt structure contents pointed by
102 // the "pointers", we always use release-consume ordering pair when accessing these "pointers".
103
104 // prop "points" to prop_info structure if there is a propery associated with the trie node.
105 // Its situation is similar to the left, right, children "pointers". So we use
106 // atomic_uint_least32_t and release-consume ordering to protect it as well.
107
Hans Boehm30214b92014-07-31 15:53:22 -0700108 // We should also avoid rereading these fields redundantly, since not
109 // all processor implementations ensure that multiple loads from the
110 // same field are carried out in the right order.
Yabin Cuib8ce4742015-02-10 21:35:56 -0800111 atomic_uint_least32_t prop;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000112
Yabin Cuib8ce4742015-02-10 21:35:56 -0800113 atomic_uint_least32_t left;
114 atomic_uint_least32_t right;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000115
Yabin Cuib8ce4742015-02-10 21:35:56 -0800116 atomic_uint_least32_t children;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000117
118 char name[0];
119
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000120 prop_bt(const char *name, const uint32_t name_length) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000121 this->namelen = name_length;
122 memcpy(this->name, name, name_length);
123 this->name[name_length] = '\0';
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000124 }
125
126private:
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700127 DISALLOW_COPY_AND_ASSIGN(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000128};
129
Tom Cherry926ebe12015-09-23 15:34:40 -0700130class prop_area {
131public:
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000132
133 prop_area(const uint32_t magic, const uint32_t version) :
Tom Cherry926ebe12015-09-23 15:34:40 -0700134 magic_(magic), version_(version) {
135 atomic_init(&serial_, 0);
136 memset(reserved_, 0, sizeof(reserved_));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000137 // Allocate enough space for the root node.
Tom Cherry926ebe12015-09-23 15:34:40 -0700138 bytes_used_ = sizeof(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000139 }
140
Tom Cherry926ebe12015-09-23 15:34:40 -0700141 const prop_info *find(const char *name);
142 bool add(const char *name, unsigned int namelen,
143 const char *value, unsigned int valuelen);
144
145 bool foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie);
146
147 atomic_uint_least32_t *serial() { return &serial_; }
148 uint32_t magic() const { return magic_; }
149 uint32_t version() const { return version_; }
150
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000151private:
Tom Cherry926ebe12015-09-23 15:34:40 -0700152 void *allocate_obj(const size_t size, uint_least32_t *const off);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000153 prop_bt *new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off);
154 prop_info *new_prop_info(const char *name, uint32_t namelen,
155 const char *value, uint32_t valuelen,
Tom Cherry926ebe12015-09-23 15:34:40 -0700156 uint_least32_t *const off);
157 void *to_prop_obj(uint_least32_t off);
158 prop_bt *to_prop_bt(atomic_uint_least32_t *off_p);
159 prop_info *to_prop_info(atomic_uint_least32_t *off_p);
160
161 prop_bt *root_node();
162
163 prop_bt *find_prop_bt(prop_bt *const bt, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000164 uint32_t namelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700165
166 const prop_info *find_property(prop_bt *const trie, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000167 uint32_t namelen, const char *value,
168 uint32_t valuelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700169
170 bool foreach_property(prop_bt *const trie,
171 void (*propfn)(const prop_info *pi, void *cookie),
172 void *cookie);
173
174 uint32_t bytes_used_;
175 atomic_uint_least32_t serial_;
176 uint32_t magic_;
177 uint32_t version_;
178 uint32_t reserved_[28];
179 char data_[0];
180
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700181 DISALLOW_COPY_AND_ASSIGN(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000182};
183
184struct prop_info {
Hans Boehm30214b92014-07-31 15:53:22 -0700185 atomic_uint_least32_t serial;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000186 // we need to keep this buffer around because the property
187 // value can be modified whereas name is constant.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000188 char value[PROP_VALUE_MAX];
189 char name[0];
190
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000191 prop_info(const char *name, uint32_t namelen, const char *value, uint32_t valuelen) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000192 memcpy(this->name, name, namelen);
193 this->name[namelen] = '\0';
Hans Boehm30214b92014-07-31 15:53:22 -0700194 atomic_init(&this->serial, valuelen << 24);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000195 memcpy(this->value, value, valuelen);
196 this->value[valuelen] = '\0';
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000197 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000198 private:
199 DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000200};
201
202struct find_nth_cookie {
203 uint32_t count;
204 const uint32_t n;
205 const prop_info *pi;
206
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700207 explicit find_nth_cookie(uint32_t n) : count(0), n(n), pi(NULL) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000208 }
209};
210
Elliott Hughesf8562c52017-01-26 16:48:57 -0800211// This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol.
212// It's also used in a libnativehelper test.
213prop_area* __system_property_area__ = nullptr;
214
Tom Cherry49a309f2015-09-23 16:09:47 -0700215static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000216static size_t pa_data_size;
217static size_t pa_size;
Tom Cherryb4171692015-12-09 15:48:15 -0800218static bool initialized = false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000219
Tom Cherry49a309f2015-09-23 16:09:47 -0700220static prop_area* map_prop_area_rw(const char* filename, const char* context,
221 bool* fsetxattr_failed) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000222 /* dev is a tmpfs that we can use to carve a shared workspace
223 * out of, so let's do that...
224 */
Tom Cherry49a309f2015-09-23 16:09:47 -0700225 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000226
227 if (fd < 0) {
228 if (errno == EACCES) {
229 /* for consistency with the case where the process has already
230 * mapped the page in and segfaults when trying to write to it
231 */
232 abort();
233 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700234 return nullptr;
235 }
236
237 if (context) {
238 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
239 __libc_format_log(ANDROID_LOG_ERROR, "libc",
240 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
241 /*
242 * fsetxattr() will fail during system properties tests due to selinux policy.
243 * We do not want to create a custom policy for the tester, so we will continue in
244 * this function but set a flag that an error has occurred.
245 * Init, which is the only daemon that should ever call this function will abort
246 * when this error occurs.
247 * Otherwise, the tester will ignore it and continue, albeit without any selinux
248 * property separation.
249 */
250 if (fsetxattr_failed) {
251 *fsetxattr_failed = true;
252 }
253 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000254 }
255
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000256 if (ftruncate(fd, PA_SIZE) < 0) {
257 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700258 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000259 }
260
261 pa_size = PA_SIZE;
262 pa_data_size = pa_size - sizeof(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000263
264 void *const memory_area = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
265 if (memory_area == MAP_FAILED) {
266 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700267 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000268 }
269
270 prop_area *pa = new(memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
271
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000272 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700273 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000274}
275
Tom Cherry49a309f2015-09-23 16:09:47 -0700276static prop_area* map_fd_ro(const int fd) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000277 struct stat fd_stat;
278 if (fstat(fd, &fd_stat) < 0) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700279 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000280 }
281
282 if ((fd_stat.st_uid != 0)
283 || (fd_stat.st_gid != 0)
284 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
Narayan Kamath37e95702014-02-24 11:05:02 +0000285 || (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area))) ) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700286 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000287 }
288
289 pa_size = fd_stat.st_size;
290 pa_data_size = pa_size - sizeof(prop_area);
291
292 void* const map_result = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0);
293 if (map_result == MAP_FAILED) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700294 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000295 }
296
297 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
Elliott Hughesf8562c52017-01-26 16:48:57 -0800298 if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000299 munmap(pa, pa_size);
Tom Cherry49a309f2015-09-23 16:09:47 -0700300 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000301 }
302
Tom Cherry49a309f2015-09-23 16:09:47 -0700303 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000304}
305
Elliott Hughesf8562c52017-01-26 16:48:57 -0800306static prop_area* map_prop_area(const char* filename) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700307 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
Elliott Hughesf8562c52017-01-26 16:48:57 -0800308 if (fd == -1) return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000309
Tom Cherry49a309f2015-09-23 16:09:47 -0700310 prop_area* map_result = map_fd_ro(fd);
Elliott Hughesf8562c52017-01-26 16:48:57 -0800311 close(fd);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000312
313 return map_result;
314}
315
Tom Cherry926ebe12015-09-23 15:34:40 -0700316void *prop_area::allocate_obj(const size_t size, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000317{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800318 const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t));
Tom Cherry926ebe12015-09-23 15:34:40 -0700319 if (bytes_used_ + aligned > pa_data_size) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000320 return NULL;
321 }
322
Tom Cherry926ebe12015-09-23 15:34:40 -0700323 *off = bytes_used_;
324 bytes_used_ += aligned;
325 return data_ + *off;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000326}
327
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000328prop_bt *prop_area::new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000329{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800330 uint_least32_t new_offset;
331 void *const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
332 if (p != NULL) {
333 prop_bt* bt = new(p) prop_bt(name, namelen);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000334 *off = new_offset;
335 return bt;
336 }
337
338 return NULL;
339}
340
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000341prop_info *prop_area::new_prop_info(const char *name, uint32_t namelen,
342 const char *value, uint32_t valuelen, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000343{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800344 uint_least32_t new_offset;
345 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
346 if (p != NULL) {
347 prop_info* info = new(p) prop_info(name, namelen, value, valuelen);
348 *off = new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000349 return info;
350 }
351
352 return NULL;
353}
354
Tom Cherry926ebe12015-09-23 15:34:40 -0700355void *prop_area::to_prop_obj(uint_least32_t off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000356{
357 if (off > pa_data_size)
358 return NULL;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000359
Tom Cherry926ebe12015-09-23 15:34:40 -0700360 return (data_ + off);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000361}
362
Tom Cherry926ebe12015-09-23 15:34:40 -0700363inline prop_bt *prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800364 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
365 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
366}
367
Tom Cherry926ebe12015-09-23 15:34:40 -0700368inline prop_info *prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800369 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
370 return reinterpret_cast<prop_info*>(to_prop_obj(off));
371}
372
Tom Cherry926ebe12015-09-23 15:34:40 -0700373inline prop_bt *prop_area::root_node()
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000374{
375 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
376}
377
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000378static int cmp_prop_name(const char *one, uint32_t one_len, const char *two,
379 uint32_t two_len)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000380{
381 if (one_len < two_len)
382 return -1;
383 else if (one_len > two_len)
384 return 1;
385 else
386 return strncmp(one, two, one_len);
387}
388
Tom Cherry926ebe12015-09-23 15:34:40 -0700389prop_bt *prop_area::find_prop_bt(prop_bt *const bt, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000390 uint32_t namelen, bool alloc_if_needed)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000391{
392
393 prop_bt* current = bt;
394 while (true) {
395 if (!current) {
396 return NULL;
397 }
398
399 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
400 if (ret == 0) {
401 return current;
402 }
403
404 if (ret < 0) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800405 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
406 if (left_offset != 0) {
407 current = to_prop_bt(&current->left);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000408 } else {
409 if (!alloc_if_needed) {
410 return NULL;
411 }
412
Yabin Cuib8ce4742015-02-10 21:35:56 -0800413 uint_least32_t new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000414 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
415 if (new_bt) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800416 atomic_store_explicit(&current->left, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000417 }
418 return new_bt;
419 }
420 } else {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800421 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
422 if (right_offset != 0) {
423 current = to_prop_bt(&current->right);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000424 } else {
425 if (!alloc_if_needed) {
426 return NULL;
427 }
428
Yabin Cuib8ce4742015-02-10 21:35:56 -0800429 uint_least32_t new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000430 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
431 if (new_bt) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800432 atomic_store_explicit(&current->right, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000433 }
434 return new_bt;
435 }
436 }
437 }
438}
439
Tom Cherry926ebe12015-09-23 15:34:40 -0700440const prop_info *prop_area::find_property(prop_bt *const trie, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000441 uint32_t namelen, const char *value, uint32_t valuelen,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000442 bool alloc_if_needed)
443{
444 if (!trie) return NULL;
445
446 const char *remaining_name = name;
447 prop_bt* current = trie;
448 while (true) {
449 const char *sep = strchr(remaining_name, '.');
450 const bool want_subtree = (sep != NULL);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000451 const uint32_t substr_size = (want_subtree) ?
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000452 sep - remaining_name : strlen(remaining_name);
453
454 if (!substr_size) {
455 return NULL;
456 }
457
458 prop_bt* root = NULL;
Yabin Cuib8ce4742015-02-10 21:35:56 -0800459 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
460 if (children_offset != 0) {
461 root = to_prop_bt(&current->children);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000462 } else if (alloc_if_needed) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800463 uint_least32_t new_offset;
464 root = new_prop_bt(remaining_name, substr_size, &new_offset);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000465 if (root) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800466 atomic_store_explicit(&current->children, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000467 }
468 }
469
470 if (!root) {
471 return NULL;
472 }
473
474 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
475 if (!current) {
476 return NULL;
477 }
478
479 if (!want_subtree)
480 break;
481
482 remaining_name = sep + 1;
483 }
484
Yabin Cuib8ce4742015-02-10 21:35:56 -0800485 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
486 if (prop_offset != 0) {
487 return to_prop_info(&current->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000488 } else if (alloc_if_needed) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800489 uint_least32_t new_offset;
490 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000491 if (new_info) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800492 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000493 }
494
495 return new_info;
496 } else {
497 return NULL;
498 }
499}
500
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000501class PropertyServiceConnection {
502 public:
503 PropertyServiceConnection() : last_error_(0) {
504 fd_ = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
505 if (fd_ == -1) {
506 last_error_ = errno;
507 return;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000508 }
509
510 const size_t namelen = strlen(property_service_socket);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000511 sockaddr_un addr;
512 memset(&addr, 0, sizeof(addr));
513 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
514 addr.sun_family = AF_LOCAL;
515 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000516
517 if (TEMP_FAILURE_RETRY(connect(fd_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
518 close(fd_);
519 fd_ = -1;
520 last_error_ = errno;
521 }
522 }
523
524 bool IsValid() {
525 return fd_ != -1;
526 }
527
528 int GetLastError() {
529 return last_error_;
530 }
531
532 bool SendUint32(uint32_t value) {
533 int result = TEMP_FAILURE_RETRY(send(fd_, &value, sizeof(value), 0));
534 return CheckSendRecvResult(result, sizeof(value));
535 }
536
537 bool SendString(const char* value) {
538 uint32_t valuelen = strlen(value);
539 if (!SendUint32(valuelen)) {
540 return false;
Dimitry Ivanov5c1ce272015-12-03 11:26:38 -0800541 }
Dimitry Ivanov5c1ce272015-12-03 11:26:38 -0800542
Dimitry Ivanovcafd3552017-01-24 12:39:33 -0800543 // Trying to send even 0 bytes to closed socket may lead to
544 // broken pipe (http://b/34670529).
545 if (valuelen == 0) {
546 return true;
547 }
548
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000549 int result = TEMP_FAILURE_RETRY(send(fd_, value, valuelen, 0));
550 return CheckSendRecvResult(result, valuelen);
551 }
552
553 bool RecvInt32(int32_t* value) {
554 int result = TEMP_FAILURE_RETRY(recv(fd_, value, sizeof(*value), MSG_WAITALL));
555 return CheckSendRecvResult(result, sizeof(*value));
556 }
557
558 int GetFd() {
559 return fd_;
560 }
561
562 ~PropertyServiceConnection() {
563 if (fd_ != -1) {
564 close(fd_);
565 }
566 }
567 private:
568 bool CheckSendRecvResult(int result, int expected_len) {
569 if (result == -1) {
570 last_error_ = errno;
571 } else if (result != expected_len) {
572 last_error_ = -1;
573 } else {
574 last_error_ = 0;
575 }
576
577 return last_error_ == 0;
578 }
579
580 int fd_;
581 int last_error_;
582};
583
Elliott Hughesf8562c52017-01-26 16:48:57 -0800584struct prop_msg {
585 unsigned cmd;
586 char name[PROP_NAME_MAX];
587 char value[PROP_VALUE_MAX];
588};
589
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000590static int send_prop_msg(const prop_msg* msg) {
591 PropertyServiceConnection connection;
592 if (!connection.IsValid()) {
593 return connection.GetLastError();
594 }
Dimitry Ivanov489f58b2017-01-24 18:39:04 +0000595
596 int result = -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000597 int fd = connection.GetFd();
598
599 const int num_bytes = TEMP_FAILURE_RETRY(send(fd, msg, sizeof(prop_msg), 0));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000600 if (num_bytes == sizeof(prop_msg)) {
601 // We successfully wrote to the property server but now we
602 // wait for the property server to finish its work. It
603 // acknowledges its completion by closing the socket so we
604 // poll here (on nothing), waiting for the socket to close.
605 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
606 // once the socket closes. Out of paranoia we cap our poll
607 // at 250 ms.
608 pollfd pollfds[1];
609 pollfds[0].fd = fd;
610 pollfds[0].events = 0;
611 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
612 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
613 result = 0;
614 } else {
615 // Ignore the timeout and treat it like a success anyway.
616 // The init process is single-threaded and its property
617 // service is sometimes slow to respond (perhaps it's off
618 // starting a child process or something) and thus this
619 // times out and the caller thinks it failed, even though
620 // it's still getting around to it. So we fake it here,
621 // mostly for ctl.* properties, but we do try and wait 250
622 // ms so callers who do read-after-write can reliably see
623 // what they've written. Most of the time.
624 // TODO: fix the system properties design.
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000625 __libc_format_log(ANDROID_LOG_WARN, "libc",
626 "Property service has timed out while trying to set \"%s\" to \"%s\"",
627 msg->name, msg->value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000628 result = 0;
629 }
630 }
631
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000632 return result;
633}
634
635static void find_nth_fn(const prop_info *pi, void *ptr)
636{
637 find_nth_cookie *cookie = reinterpret_cast<find_nth_cookie*>(ptr);
638
639 if (cookie->n == cookie->count)
640 cookie->pi = pi;
641
642 cookie->count++;
643}
644
Tom Cherry926ebe12015-09-23 15:34:40 -0700645bool prop_area::foreach_property(prop_bt *const trie,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000646 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
647{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000648 if (!trie)
Tom Cherry926ebe12015-09-23 15:34:40 -0700649 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000650
Yabin Cuib8ce4742015-02-10 21:35:56 -0800651 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
652 if (left_offset != 0) {
653 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000654 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700655 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000656 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800657 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
658 if (prop_offset != 0) {
659 prop_info *info = to_prop_info(&trie->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000660 if (!info)
Tom Cherry926ebe12015-09-23 15:34:40 -0700661 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000662 propfn(info, cookie);
663 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800664 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
665 if (children_offset != 0) {
666 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000667 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700668 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000669 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800670 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
671 if (right_offset != 0) {
672 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000673 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700674 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000675 }
676
Tom Cherry926ebe12015-09-23 15:34:40 -0700677 return true;
678}
679
680const prop_info *prop_area::find(const char *name) {
681 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
682}
683
684bool prop_area::add(const char *name, unsigned int namelen,
685 const char *value, unsigned int valuelen) {
686 return find_property(root_node(), name, namelen, value, valuelen, true);
687}
688
689bool prop_area::foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
690 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000691}
692
Tom Cherryb4171692015-12-09 15:48:15 -0800693class context_node {
694public:
695 context_node(context_node* next, const char* context, prop_area* pa)
696 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
697 lock_.init(false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700698 }
699 ~context_node() {
Tom Cherryb4171692015-12-09 15:48:15 -0800700 unmap();
701 free(context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700702 }
Tom Cherryb4171692015-12-09 15:48:15 -0800703 bool open(bool access_rw, bool* fsetxattr_failed);
704 bool check_access_and_open();
705 void reset_access();
706
707 const char* context() const { return context_; }
708 prop_area* pa() { return pa_; }
709
710 context_node* next;
711
712private:
713 bool check_access();
714 void unmap();
715
716 Lock lock_;
717 char* context_;
718 prop_area* pa_;
719 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700720};
721
722struct prefix_node {
723 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
724 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
725 }
726 ~prefix_node() {
727 free(prefix);
728 }
729 char* prefix;
730 const size_t prefix_len;
731 context_node* context;
732 struct prefix_node* next;
733};
734
735template <typename List, typename... Args>
736static inline void list_add(List** list, Args... args) {
737 *list = new List(*list, args...);
738}
739
740static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
741 size_t prefix_len = strlen(prefix);
742
743 auto next_list = list;
744
745 while (*next_list) {
746 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
747 list_add(next_list, prefix, context);
748 return;
749 }
750 next_list = &(*next_list)->next;
751 }
752 list_add(next_list, prefix, context);
753}
754
755template <typename List, typename Func>
756static void list_foreach(List* list, Func func) {
757 while (list) {
758 func(list);
759 list = list->next;
760 }
761}
762
763template <typename List, typename Func>
764static List* list_find(List* list, Func func) {
765 while (list) {
766 if (func(list)) {
767 return list;
768 }
769 list = list->next;
770 }
771 return nullptr;
772}
773
774template <typename List>
775static void list_free(List** list) {
776 while (*list) {
777 auto old_list = *list;
778 *list = old_list->next;
779 delete old_list;
780 }
781}
782
783static prefix_node* prefixes = nullptr;
784static context_node* contexts = nullptr;
785
786/*
787 * pthread_mutex_lock() calls into system_properties in the case of contention.
788 * This creates a risk of dead lock if any system_properties functions
789 * use pthread locks after system_property initialization.
790 *
791 * For this reason, the below three functions use a bionic Lock and static
792 * allocation of memory for each filename.
793 */
794
Tom Cherryb4171692015-12-09 15:48:15 -0800795bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
796 lock_.lock();
797 if (pa_) {
798 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700799 return true;
800 }
801
802 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800803 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
804 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700805 if (len < 0 || len > PROP_FILENAME_MAX) {
Tom Cherryb4171692015-12-09 15:48:15 -0800806 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700807 return false;
808 }
809
810 if (access_rw) {
Tom Cherryb4171692015-12-09 15:48:15 -0800811 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
Tom Cherry49a309f2015-09-23 16:09:47 -0700812 } else {
Elliott Hughesf8562c52017-01-26 16:48:57 -0800813 pa_ = map_prop_area(filename);
Tom Cherry49a309f2015-09-23 16:09:47 -0700814 }
Tom Cherryb4171692015-12-09 15:48:15 -0800815 lock_.unlock();
816 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700817}
818
Tom Cherryb4171692015-12-09 15:48:15 -0800819bool context_node::check_access_and_open() {
820 if (!pa_ && !no_access_) {
821 if (!check_access() || !open(false, nullptr)) {
822 no_access_ = true;
823 }
824 }
825 return pa_;
826}
827
828void context_node::reset_access() {
829 if (!check_access()) {
830 unmap();
831 no_access_ = true;
832 } else {
833 no_access_ = false;
834 }
835}
836
837bool context_node::check_access() {
Tom Cherry49a309f2015-09-23 16:09:47 -0700838 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800839 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
840 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700841 if (len < 0 || len > PROP_FILENAME_MAX) {
842 return false;
843 }
844
845 return access(filename, R_OK) == 0;
846}
847
Tom Cherryb4171692015-12-09 15:48:15 -0800848void context_node::unmap() {
849 if (!pa_) {
850 return;
851 }
852
853 munmap(pa_, pa_size);
854 if (pa_ == __system_property_area__) {
855 __system_property_area__ = nullptr;
856 }
857 pa_ = nullptr;
858}
859
Tom Cherry49a309f2015-09-23 16:09:47 -0700860static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
861 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800862 int len = __libc_format_buffer(filename, sizeof(filename),
863 "%s/properties_serial", property_filename);
Tom Cherry49a309f2015-09-23 16:09:47 -0700864 if (len < 0 || len > PROP_FILENAME_MAX) {
865 __system_property_area__ = nullptr;
866 return false;
867 }
868
869 if (access_rw) {
870 __system_property_area__ =
871 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
872 } else {
Elliott Hughesf8562c52017-01-26 16:48:57 -0800873 __system_property_area__ = map_prop_area(filename);
Tom Cherry49a309f2015-09-23 16:09:47 -0700874 }
875 return __system_property_area__;
876}
877
878static prop_area* get_prop_area_for_name(const char* name) {
Tom Cherry845e24a2015-12-03 15:38:52 -0800879 auto entry = list_find(prefixes, [name](prefix_node* l) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700880 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
881 });
882 if (!entry) {
883 return nullptr;
884 }
885
886 auto cnode = entry->context;
Tom Cherryb4171692015-12-09 15:48:15 -0800887 if (!cnode->pa()) {
888 /*
889 * We explicitly do not check no_access_ in this case because unlike the
890 * case of foreach(), we want to generate an selinux audit for each
891 * non-permitted property access in this function.
892 */
893 cnode->open(false, nullptr);
Tom Cherry49a309f2015-09-23 16:09:47 -0700894 }
Tom Cherryb4171692015-12-09 15:48:15 -0800895 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700896}
897
898/*
899 * The below two functions are duplicated from label_support.c in libselinux.
900 * TODO: Find a location suitable for these functions such that both libc and
901 * libselinux can share a common source file.
902 */
903
904/*
905 * The read_spec_entries and read_spec_entry functions may be used to
906 * replace sscanf to read entries from spec files. The file and
907 * property services now use these.
908 */
909
910/* Read an entry from a spec file (e.g. file_contexts) */
911static inline int read_spec_entry(char **entry, char **ptr, int *len)
912{
913 *entry = NULL;
914 char *tmp_buf = NULL;
915
916 while (isspace(**ptr) && **ptr != '\0')
917 (*ptr)++;
918
919 tmp_buf = *ptr;
920 *len = 0;
921
922 while (!isspace(**ptr) && **ptr != '\0') {
923 (*ptr)++;
924 (*len)++;
925 }
926
927 if (*len) {
928 *entry = strndup(tmp_buf, *len);
929 if (!*entry)
930 return -1;
931 }
932
933 return 0;
934}
935
936/*
937 * line_buf - Buffer containing the spec entries .
938 * num_args - The number of spec parameter entries to process.
939 * ... - A 'char **spec_entry' for each parameter.
940 * returns - The number of items processed.
941 *
942 * This function calls read_spec_entry() to do the actual string processing.
943 */
944static int read_spec_entries(char *line_buf, int num_args, ...)
945{
946 char **spec_entry, *buf_p;
947 int len, rc, items, entry_len = 0;
948 va_list ap;
949
950 len = strlen(line_buf);
951 if (line_buf[len - 1] == '\n')
952 line_buf[len - 1] = '\0';
953 else
954 /* Handle case if line not \n terminated by bumping
955 * the len for the check below (as the line is NUL
956 * terminated by getline(3)) */
957 len++;
958
959 buf_p = line_buf;
960 while (isspace(*buf_p))
961 buf_p++;
962
963 /* Skip comment lines and empty lines. */
964 if (*buf_p == '#' || *buf_p == '\0')
965 return 0;
966
967 /* Process the spec file entries */
968 va_start(ap, num_args);
969
970 items = 0;
971 while (items < num_args) {
972 spec_entry = va_arg(ap, char **);
973
974 if (len - 1 == buf_p - line_buf) {
975 va_end(ap);
976 return items;
977 }
978
979 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
980 if (rc < 0) {
981 va_end(ap);
982 return rc;
983 }
984 if (entry_len)
985 items++;
986 }
987 va_end(ap);
988 return items;
989}
990
Jorim Jaggie2f5ea12017-01-18 15:37:18 +0000991static bool initialize_properties() {
992 FILE* file = fopen("/property_contexts", "re");
Tom Cherry49a309f2015-09-23 16:09:47 -0700993
994 if (!file) {
995 return false;
996 }
997
998 char* buffer = nullptr;
999 size_t line_len;
1000 char* prop_prefix = nullptr;
1001 char* context = nullptr;
1002
1003 while (getline(&buffer, &line_len, file) > 0) {
1004 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1005 if (items <= 0) {
1006 continue;
1007 }
1008 if (items == 1) {
1009 free(prop_prefix);
1010 continue;
1011 }
Tom Cherry21eadee2015-12-04 15:53:25 -08001012 /*
1013 * init uses ctl.* properties as an IPC mechanism and does not write them
1014 * to a property file, therefore we do not need to create property files
1015 * to store them.
1016 */
1017 if (!strncmp(prop_prefix, "ctl.", 4)) {
1018 free(prop_prefix);
1019 free(context);
1020 continue;
1021 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001022
Tom Cherry845e24a2015-12-03 15:38:52 -08001023 auto old_context = list_find(
Tom Cherryb4171692015-12-09 15:48:15 -08001024 contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001025 if (old_context) {
1026 list_add_after_len(&prefixes, prop_prefix, old_context);
1027 } else {
1028 list_add(&contexts, context, nullptr);
1029 list_add_after_len(&prefixes, prop_prefix, contexts);
1030 }
1031 free(prop_prefix);
1032 free(context);
1033 }
1034
1035 free(buffer);
1036 fclose(file);
1037 return true;
1038}
1039
1040static bool is_dir(const char* pathname) {
1041 struct stat info;
1042 if (stat(pathname, &info) == -1) {
1043 return false;
1044 }
1045 return S_ISDIR(info.st_mode);
1046}
1047
Tom Cherryb4171692015-12-09 15:48:15 -08001048static void free_and_unmap_contexts() {
1049 list_free(&prefixes);
1050 list_free(&contexts);
1051 if (__system_property_area__) {
1052 munmap(__system_property_area__, pa_size);
1053 __system_property_area__ = nullptr;
1054 }
1055}
1056
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001057int __system_properties_init()
1058{
Tom Cherryb4171692015-12-09 15:48:15 -08001059 if (initialized) {
1060 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
1061 return 0;
1062 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001063 if (is_dir(property_filename)) {
1064 if (!initialize_properties()) {
1065 return -1;
1066 }
1067 if (!map_system_property_area(false, nullptr)) {
Tom Cherryb4171692015-12-09 15:48:15 -08001068 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001069 return -1;
1070 }
1071 } else {
Elliott Hughesf8562c52017-01-26 16:48:57 -08001072 __system_property_area__ = map_prop_area(property_filename);
Tom Cherry49a309f2015-09-23 16:09:47 -07001073 if (!__system_property_area__) {
1074 return -1;
1075 }
1076 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1077 list_add_after_len(&prefixes, "*", contexts);
1078 }
Tom Cherryb4171692015-12-09 15:48:15 -08001079 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001080 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001081}
1082
1083int __system_property_set_filename(const char *filename)
1084{
1085 size_t len = strlen(filename);
1086 if (len >= sizeof(property_filename))
1087 return -1;
1088
1089 strcpy(property_filename, filename);
1090 return 0;
1091}
1092
1093int __system_property_area_init()
1094{
Tom Cherryb4171692015-12-09 15:48:15 -08001095 free_and_unmap_contexts();
Nick Kralevichbb59d472017-01-06 12:04:29 -08001096 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
Tom Cherry49a309f2015-09-23 16:09:47 -07001097 if (!initialize_properties()) {
1098 return -1;
1099 }
Tom Cherryb4171692015-12-09 15:48:15 -08001100 bool open_failed = false;
Tom Cherry49a309f2015-09-23 16:09:47 -07001101 bool fsetxattr_failed = false;
Tom Cherryb4171692015-12-09 15:48:15 -08001102 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1103 if (!l->open(true, &fsetxattr_failed)) {
1104 open_failed = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001105 }
1106 });
Tom Cherryb4171692015-12-09 15:48:15 -08001107 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1108 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001109 return -1;
1110 }
Tom Cherryb4171692015-12-09 15:48:15 -08001111 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001112 return fsetxattr_failed ? -2 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001113}
1114
Mark Salyzynbfd65272015-04-24 09:31:32 -07001115unsigned int __system_property_area_serial()
1116{
1117 prop_area *pa = __system_property_area__;
1118 if (!pa) {
1119 return -1;
1120 }
1121 // Make sure this read fulfilled before __system_property_serial
Tom Cherry926ebe12015-09-23 15:34:40 -07001122 return atomic_load_explicit(pa->serial(), memory_order_acquire);
Mark Salyzynbfd65272015-04-24 09:31:32 -07001123}
1124
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001125const prop_info *__system_property_find(const char *name)
1126{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001127 if (!__system_property_area__) {
1128 return nullptr;
1129 }
1130
Tom Cherry49a309f2015-09-23 16:09:47 -07001131 prop_area* pa = get_prop_area_for_name(name);
1132 if (!pa) {
1133 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001134 return nullptr;
1135 }
1136
Tom Cherry49a309f2015-09-23 16:09:47 -07001137 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001138}
1139
Hans Boehm1e8587a2014-08-19 14:07:55 -07001140// The C11 standard doesn't allow atomic loads from const fields,
1141// though C++11 does. Fudge it until standards get straightened out.
1142static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s,
1143 memory_order mo) {
1144 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1145 return atomic_load_explicit(non_const_s, mo);
1146}
1147
Elliott Hughesf8562c52017-01-26 16:48:57 -08001148int __system_property_read(const prop_info *pi, char *name, char *value) {
jiaguo879d3302014-03-13 17:39:58 +08001149 while (true) {
Hans Boehm30214b92014-07-31 15:53:22 -07001150 uint32_t serial = __system_property_serial(pi); // acquire semantics
jiaguo879d3302014-03-13 17:39:58 +08001151 size_t len = SERIAL_VALUE_LEN(serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001152 memcpy(value, pi->value, len + 1);
Hans Boehm30214b92014-07-31 15:53:22 -07001153 // TODO: Fix the synchronization scheme here.
1154 // There is no fully supported way to implement this kind
1155 // of synchronization in C++11, since the memcpy races with
1156 // updates to pi, and the data being accessed is not atomic.
1157 // The following fence is unintuitive, but would be the
1158 // correct one if memcpy used memory_order_relaxed atomic accesses.
1159 // In practice it seems unlikely that the generated code would
1160 // would be any different, so this should be OK.
1161 atomic_thread_fence(memory_order_acquire);
1162 if (serial ==
Hans Boehm1e8587a2014-08-19 14:07:55 -07001163 load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001164 if (name != nullptr) {
1165 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1166 if(namelen >= PROP_NAME_MAX) {
1167 __libc_format_log(ANDROID_LOG_ERROR, "libc",
1168 "The property name length for \"%s\" is >= %d;"
1169 " please use __system_property_read_callback"
1170 " to read this property. (the name is truncated to \"%s\")",
1171 pi->name, PROP_NAME_MAX - 1, name);
1172 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001173 }
1174 return len;
1175 }
1176 }
1177}
1178
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001179void __system_property_read_callback(const prop_info* pi,
1180 void (*callback)(void* cookie, const char* name, const char* value),
1181 void* cookie) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001182 while (true) {
1183 uint32_t serial = __system_property_serial(pi); // acquire semantics
1184 size_t len = SERIAL_VALUE_LEN(serial);
1185 char value_buf[len+1];
1186
1187 memcpy(value_buf, pi->value, len);
1188 value_buf[len] = '\0';
1189
1190 // TODO: see todo in __system_property_read function
1191 atomic_thread_fence(memory_order_acquire);
1192 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1193 callback(cookie, pi->name, value_buf);
1194 return;
1195 }
1196 }
1197}
1198
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001199int __system_property_get(const char *name, char *value)
1200{
1201 const prop_info *pi = __system_property_find(name);
1202
1203 if (pi != 0) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001204 return __system_property_read(pi, nullptr, value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001205 } else {
1206 value[0] = 0;
1207 return 0;
1208 }
1209}
1210
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001211static constexpr uint32_t kProtocolVersion1 = 1;
1212static constexpr uint32_t kProtocolVersion2 = 2; // current
1213
1214static atomic_uint_least32_t g_propservice_protocol_version = 0;
1215
1216static void detect_protocol_version() {
1217 char value[PROP_VALUE_MAX];
1218 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1219 g_propservice_protocol_version = kProtocolVersion1;
1220 __libc_format_log(ANDROID_LOG_WARN, "libc",
1221 "Using old property service protocol (\"%s\" is not set)",
1222 kServiceVersionPropertyName);
1223 } else {
1224 uint32_t version = static_cast<uint32_t>(atoll(value));
1225 if (version >= kProtocolVersion2) {
1226 g_propservice_protocol_version = kProtocolVersion2;
1227 } else {
1228 __libc_format_log(ANDROID_LOG_WARN, "libc",
1229 "Using old property service protocol (\"%s\"=\"%s\")",
1230 kServiceVersionPropertyName, value);
1231 g_propservice_protocol_version = kProtocolVersion1;
1232 }
1233 }
1234}
1235
1236int __system_property_set(const char* key, const char* value) {
1237 if (key == nullptr) return -1;
1238 if (value == nullptr) value = "";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001239 if (strlen(value) >= PROP_VALUE_MAX) return -1;
1240
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001241 if (g_propservice_protocol_version == 0) {
1242 detect_protocol_version();
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001243 }
1244
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001245 int result = -1;
1246 if (g_propservice_protocol_version == kProtocolVersion1) {
1247 // Old protocol does not support long names
1248 if (strlen(key) >= PROP_NAME_MAX) return -1;
1249
1250 prop_msg msg;
1251 memset(&msg, 0, sizeof msg);
1252 msg.cmd = PROP_MSG_SETPROP;
1253 strlcpy(msg.name, key, sizeof msg.name);
1254 strlcpy(msg.value, value, sizeof msg.value);
1255
1256 result = send_prop_msg(&msg);
1257 } else {
1258 // Use proper protocol
1259 PropertyServiceConnection connection;
1260 if (connection.IsValid() &&
1261 connection.SendUint32(PROP_MSG_SETPROP2) &&
1262 connection.SendString(key) &&
1263 connection.SendString(value) &&
1264 connection.RecvInt32(&result)) {
1265 if (result != PROP_SUCCESS) {
1266 __libc_format_log(ANDROID_LOG_WARN, "libc",
1267 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1268 key, value, result);
1269 }
1270 } else {
1271 result = connection.GetLastError();
1272 __libc_format_log(ANDROID_LOG_WARN, "libc",
1273 "Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)",
1274 key, value, result, strerror(result));
1275 }
1276 }
1277
1278 return result != 0 ? -1 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001279}
1280
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001281int __system_property_update(prop_info *pi, const char *value, unsigned int len)
1282{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001283 if (len >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001284 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001285 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001286
Tom Cherry6ed51c02015-12-04 11:34:42 -08001287 prop_area* pa = __system_property_area__;
1288
1289 if (!pa) {
1290 return -1;
1291 }
1292
Hans Boehm30214b92014-07-31 15:53:22 -07001293 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1294 serial |= 1;
1295 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1296 // The memcpy call here also races. Again pretend it
1297 // used memory_order_relaxed atomics, and use the analogous
1298 // counterintuitive fence.
1299 atomic_thread_fence(memory_order_release);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001300 strlcpy(pi->value, value, len + 1);
1301
Hans Boehm30214b92014-07-31 15:53:22 -07001302 atomic_store_explicit(
1303 &pi->serial,
1304 (len << 24) | ((serial + 1) & 0xffffff),
1305 memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001306 __futex_wake(&pi->serial, INT32_MAX);
1307
Hans Boehm30214b92014-07-31 15:53:22 -07001308 atomic_store_explicit(
Tom Cherry926ebe12015-09-23 15:34:40 -07001309 pa->serial(),
1310 atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001311 memory_order_release);
Tom Cherry926ebe12015-09-23 15:34:40 -07001312 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001313
1314 return 0;
1315}
jiaguo879d3302014-03-13 17:39:58 +08001316
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001317int __system_property_add(const char *name, unsigned int namelen,
1318 const char *value, unsigned int valuelen)
1319{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001320 if (valuelen >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001321 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001322 }
1323
1324 if (namelen < 1) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001325 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001326 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001327
Tom Cherry6ed51c02015-12-04 11:34:42 -08001328 if (!__system_property_area__) {
1329 return -1;
1330 }
1331
Tom Cherry49a309f2015-09-23 16:09:47 -07001332 prop_area* pa = get_prop_area_for_name(name);
1333
1334 if (!pa) {
1335 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001336 return -1;
1337 }
1338
Tom Cherry49a309f2015-09-23 16:09:47 -07001339 bool ret = pa->add(name, namelen, value, valuelen);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001340 if (!ret) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001341 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001342 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001343
Hans Boehm30214b92014-07-31 15:53:22 -07001344 // There is only a single mutator, but we want to make sure that
1345 // updates are visible to a reader waiting for the update.
1346 atomic_store_explicit(
Tom Cherry49a309f2015-09-23 16:09:47 -07001347 __system_property_area__->serial(),
1348 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001349 memory_order_release);
Tom Cherry49a309f2015-09-23 16:09:47 -07001350 __futex_wake(__system_property_area__->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001351 return 0;
1352}
1353
Hans Boehm30214b92014-07-31 15:53:22 -07001354// Wait for non-locked serial, and retrieve it with acquire semantics.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001355unsigned int __system_property_serial(const prop_info *pi)
1356{
Hans Boehm1e8587a2014-08-19 14:07:55 -07001357 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001358 while (SERIAL_DIRTY(serial)) {
Hans Boehm30214b92014-07-31 15:53:22 -07001359 __futex_wait(const_cast<volatile void *>(
1360 reinterpret_cast<const void *>(&pi->serial)),
1361 serial, NULL);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001362 serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001363 }
1364 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001365}
1366
1367unsigned int __system_property_wait_any(unsigned int serial)
1368{
1369 prop_area *pa = __system_property_area__;
Hans Boehm30214b92014-07-31 15:53:22 -07001370 uint32_t my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001371
Tom Cherry6ed51c02015-12-04 11:34:42 -08001372 if (!pa) {
1373 return 0;
1374 }
1375
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001376 do {
Tom Cherry926ebe12015-09-23 15:34:40 -07001377 __futex_wait(pa->serial(), serial, NULL);
1378 my_serial = atomic_load_explicit(pa->serial(), memory_order_acquire);
Hans Boehm30214b92014-07-31 15:53:22 -07001379 } while (my_serial == serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001380
Hans Boehm30214b92014-07-31 15:53:22 -07001381 return my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001382}
1383
1384const prop_info *__system_property_find_nth(unsigned n)
1385{
Dimitry Ivanov581b9f62017-01-09 11:05:52 -08001386 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
1387 __libc_fatal("__system_property_find_nth is not supported since Android O,"
1388 " please use __system_property_foreach instead.");
1389 }
1390
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001391 find_nth_cookie cookie(n);
1392
1393 const int err = __system_property_foreach(find_nth_fn, &cookie);
1394 if (err < 0) {
1395 return NULL;
1396 }
1397
1398 return cookie.pi;
1399}
1400
Elliott Hughesf8562c52017-01-26 16:48:57 -08001401int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001402{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001403 if (!__system_property_area__) {
1404 return -1;
1405 }
1406
Tom Cherry845e24a2015-12-03 15:38:52 -08001407 list_foreach(contexts, [propfn, cookie](context_node* l) {
Tom Cherryb4171692015-12-09 15:48:15 -08001408 if (l->check_access_and_open()) {
1409 l->pa()->foreach(propfn, cookie);
Tom Cherry49a309f2015-09-23 16:09:47 -07001410 }
1411 });
1412 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001413}