blob: 3d9aa6b27d77b2d1b0942e720ffe6caf2a8fce9d [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
Sandeep Patil34f0cfa2016-12-27 17:37:44 -0800991static bool initialize_properties_from_file(const char *filename) {
992 FILE* file = fopen(filename, "re");
Tom Cherry49a309f2015-09-23 16:09:47 -0700993 if (!file) {
994 return false;
995 }
996
997 char* buffer = nullptr;
998 size_t line_len;
999 char* prop_prefix = nullptr;
1000 char* context = nullptr;
1001
1002 while (getline(&buffer, &line_len, file) > 0) {
1003 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1004 if (items <= 0) {
1005 continue;
1006 }
1007 if (items == 1) {
1008 free(prop_prefix);
1009 continue;
1010 }
Tom Cherry21eadee2015-12-04 15:53:25 -08001011 /*
1012 * init uses ctl.* properties as an IPC mechanism and does not write them
1013 * to a property file, therefore we do not need to create property files
1014 * to store them.
1015 */
1016 if (!strncmp(prop_prefix, "ctl.", 4)) {
1017 free(prop_prefix);
1018 free(context);
1019 continue;
1020 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001021
Tom Cherry845e24a2015-12-03 15:38:52 -08001022 auto old_context = list_find(
Tom Cherryb4171692015-12-09 15:48:15 -08001023 contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001024 if (old_context) {
1025 list_add_after_len(&prefixes, prop_prefix, old_context);
1026 } else {
1027 list_add(&contexts, context, nullptr);
1028 list_add_after_len(&prefixes, prop_prefix, contexts);
1029 }
1030 free(prop_prefix);
1031 free(context);
1032 }
1033
1034 free(buffer);
1035 fclose(file);
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001036
1037 return true;
1038}
1039
1040static bool initialize_properties() {
1041 // If we do find /property_contexts, then this is being
1042 // run as part of the OTA updater on older release that had
1043 // /property_contexts - b/34370523
1044 if (initialize_properties_from_file("/property_contexts")) {
1045 return true;
1046 }
1047
1048 // TODO: Change path to /system/property_contexts after b/27805372
1049 if (!initialize_properties_from_file("/plat_property_contexts")) {
1050 return false;
1051 }
1052
1053 // TODO: Change path to /vendor/property_contexts after b/27805372
1054 // device-specific property context is optional, so load if it exists.
1055 initialize_properties_from_file("/nonplat_property_contexts");
1056
Tom Cherry49a309f2015-09-23 16:09:47 -07001057 return true;
1058}
1059
1060static bool is_dir(const char* pathname) {
1061 struct stat info;
1062 if (stat(pathname, &info) == -1) {
1063 return false;
1064 }
1065 return S_ISDIR(info.st_mode);
1066}
1067
Tom Cherryb4171692015-12-09 15:48:15 -08001068static void free_and_unmap_contexts() {
1069 list_free(&prefixes);
1070 list_free(&contexts);
1071 if (__system_property_area__) {
1072 munmap(__system_property_area__, pa_size);
1073 __system_property_area__ = nullptr;
1074 }
1075}
1076
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001077int __system_properties_init()
1078{
Tom Cherryb4171692015-12-09 15:48:15 -08001079 if (initialized) {
1080 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
1081 return 0;
1082 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001083 if (is_dir(property_filename)) {
1084 if (!initialize_properties()) {
1085 return -1;
1086 }
1087 if (!map_system_property_area(false, nullptr)) {
Tom Cherryb4171692015-12-09 15:48:15 -08001088 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001089 return -1;
1090 }
1091 } else {
Elliott Hughesf8562c52017-01-26 16:48:57 -08001092 __system_property_area__ = map_prop_area(property_filename);
Tom Cherry49a309f2015-09-23 16:09:47 -07001093 if (!__system_property_area__) {
1094 return -1;
1095 }
1096 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1097 list_add_after_len(&prefixes, "*", contexts);
1098 }
Tom Cherryb4171692015-12-09 15:48:15 -08001099 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001100 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001101}
1102
1103int __system_property_set_filename(const char *filename)
1104{
1105 size_t len = strlen(filename);
1106 if (len >= sizeof(property_filename))
1107 return -1;
1108
1109 strcpy(property_filename, filename);
1110 return 0;
1111}
1112
1113int __system_property_area_init()
1114{
Tom Cherryb4171692015-12-09 15:48:15 -08001115 free_and_unmap_contexts();
Nick Kralevichbb59d472017-01-06 12:04:29 -08001116 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
Tom Cherry49a309f2015-09-23 16:09:47 -07001117 if (!initialize_properties()) {
1118 return -1;
1119 }
Tom Cherryb4171692015-12-09 15:48:15 -08001120 bool open_failed = false;
Tom Cherry49a309f2015-09-23 16:09:47 -07001121 bool fsetxattr_failed = false;
Tom Cherryb4171692015-12-09 15:48:15 -08001122 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1123 if (!l->open(true, &fsetxattr_failed)) {
1124 open_failed = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001125 }
1126 });
Tom Cherryb4171692015-12-09 15:48:15 -08001127 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1128 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001129 return -1;
1130 }
Tom Cherryb4171692015-12-09 15:48:15 -08001131 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001132 return fsetxattr_failed ? -2 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001133}
1134
Mark Salyzynbfd65272015-04-24 09:31:32 -07001135unsigned int __system_property_area_serial()
1136{
1137 prop_area *pa = __system_property_area__;
1138 if (!pa) {
1139 return -1;
1140 }
1141 // Make sure this read fulfilled before __system_property_serial
Tom Cherry926ebe12015-09-23 15:34:40 -07001142 return atomic_load_explicit(pa->serial(), memory_order_acquire);
Mark Salyzynbfd65272015-04-24 09:31:32 -07001143}
1144
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001145const prop_info *__system_property_find(const char *name)
1146{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001147 if (!__system_property_area__) {
1148 return nullptr;
1149 }
1150
Tom Cherry49a309f2015-09-23 16:09:47 -07001151 prop_area* pa = get_prop_area_for_name(name);
1152 if (!pa) {
1153 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001154 return nullptr;
1155 }
1156
Tom Cherry49a309f2015-09-23 16:09:47 -07001157 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001158}
1159
Hans Boehm1e8587a2014-08-19 14:07:55 -07001160// The C11 standard doesn't allow atomic loads from const fields,
1161// though C++11 does. Fudge it until standards get straightened out.
1162static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s,
1163 memory_order mo) {
1164 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1165 return atomic_load_explicit(non_const_s, mo);
1166}
1167
Elliott Hughesf8562c52017-01-26 16:48:57 -08001168int __system_property_read(const prop_info *pi, char *name, char *value) {
jiaguo879d3302014-03-13 17:39:58 +08001169 while (true) {
Hans Boehm30214b92014-07-31 15:53:22 -07001170 uint32_t serial = __system_property_serial(pi); // acquire semantics
jiaguo879d3302014-03-13 17:39:58 +08001171 size_t len = SERIAL_VALUE_LEN(serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001172 memcpy(value, pi->value, len + 1);
Hans Boehm30214b92014-07-31 15:53:22 -07001173 // TODO: Fix the synchronization scheme here.
1174 // There is no fully supported way to implement this kind
1175 // of synchronization in C++11, since the memcpy races with
1176 // updates to pi, and the data being accessed is not atomic.
1177 // The following fence is unintuitive, but would be the
1178 // correct one if memcpy used memory_order_relaxed atomic accesses.
1179 // In practice it seems unlikely that the generated code would
1180 // would be any different, so this should be OK.
1181 atomic_thread_fence(memory_order_acquire);
1182 if (serial ==
Hans Boehm1e8587a2014-08-19 14:07:55 -07001183 load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001184 if (name != nullptr) {
1185 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1186 if(namelen >= PROP_NAME_MAX) {
1187 __libc_format_log(ANDROID_LOG_ERROR, "libc",
1188 "The property name length for \"%s\" is >= %d;"
1189 " please use __system_property_read_callback"
1190 " to read this property. (the name is truncated to \"%s\")",
1191 pi->name, PROP_NAME_MAX - 1, name);
1192 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001193 }
1194 return len;
1195 }
1196 }
1197}
1198
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001199void __system_property_read_callback(const prop_info* pi,
1200 void (*callback)(void* cookie, const char* name, const char* value),
1201 void* cookie) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001202 while (true) {
1203 uint32_t serial = __system_property_serial(pi); // acquire semantics
1204 size_t len = SERIAL_VALUE_LEN(serial);
1205 char value_buf[len+1];
1206
1207 memcpy(value_buf, pi->value, len);
1208 value_buf[len] = '\0';
1209
1210 // TODO: see todo in __system_property_read function
1211 atomic_thread_fence(memory_order_acquire);
1212 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1213 callback(cookie, pi->name, value_buf);
1214 return;
1215 }
1216 }
1217}
1218
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001219int __system_property_get(const char *name, char *value)
1220{
1221 const prop_info *pi = __system_property_find(name);
1222
1223 if (pi != 0) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001224 return __system_property_read(pi, nullptr, value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001225 } else {
1226 value[0] = 0;
1227 return 0;
1228 }
1229}
1230
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001231static constexpr uint32_t kProtocolVersion1 = 1;
1232static constexpr uint32_t kProtocolVersion2 = 2; // current
1233
1234static atomic_uint_least32_t g_propservice_protocol_version = 0;
1235
1236static void detect_protocol_version() {
1237 char value[PROP_VALUE_MAX];
1238 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1239 g_propservice_protocol_version = kProtocolVersion1;
1240 __libc_format_log(ANDROID_LOG_WARN, "libc",
1241 "Using old property service protocol (\"%s\" is not set)",
1242 kServiceVersionPropertyName);
1243 } else {
1244 uint32_t version = static_cast<uint32_t>(atoll(value));
1245 if (version >= kProtocolVersion2) {
1246 g_propservice_protocol_version = kProtocolVersion2;
1247 } else {
1248 __libc_format_log(ANDROID_LOG_WARN, "libc",
1249 "Using old property service protocol (\"%s\"=\"%s\")",
1250 kServiceVersionPropertyName, value);
1251 g_propservice_protocol_version = kProtocolVersion1;
1252 }
1253 }
1254}
1255
1256int __system_property_set(const char* key, const char* value) {
1257 if (key == nullptr) return -1;
1258 if (value == nullptr) value = "";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001259 if (strlen(value) >= PROP_VALUE_MAX) return -1;
1260
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001261 if (g_propservice_protocol_version == 0) {
1262 detect_protocol_version();
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001263 }
1264
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001265 int result = -1;
1266 if (g_propservice_protocol_version == kProtocolVersion1) {
1267 // Old protocol does not support long names
1268 if (strlen(key) >= PROP_NAME_MAX) return -1;
1269
1270 prop_msg msg;
1271 memset(&msg, 0, sizeof msg);
1272 msg.cmd = PROP_MSG_SETPROP;
1273 strlcpy(msg.name, key, sizeof msg.name);
1274 strlcpy(msg.value, value, sizeof msg.value);
1275
1276 result = send_prop_msg(&msg);
1277 } else {
1278 // Use proper protocol
1279 PropertyServiceConnection connection;
1280 if (connection.IsValid() &&
1281 connection.SendUint32(PROP_MSG_SETPROP2) &&
1282 connection.SendString(key) &&
1283 connection.SendString(value) &&
1284 connection.RecvInt32(&result)) {
1285 if (result != PROP_SUCCESS) {
1286 __libc_format_log(ANDROID_LOG_WARN, "libc",
1287 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1288 key, value, result);
1289 }
1290 } else {
1291 result = connection.GetLastError();
1292 __libc_format_log(ANDROID_LOG_WARN, "libc",
1293 "Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)",
1294 key, value, result, strerror(result));
1295 }
1296 }
1297
1298 return result != 0 ? -1 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001299}
1300
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001301int __system_property_update(prop_info *pi, const char *value, unsigned int len)
1302{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001303 if (len >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001304 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001305 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001306
Tom Cherry6ed51c02015-12-04 11:34:42 -08001307 prop_area* pa = __system_property_area__;
1308
1309 if (!pa) {
1310 return -1;
1311 }
1312
Hans Boehm30214b92014-07-31 15:53:22 -07001313 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1314 serial |= 1;
1315 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1316 // The memcpy call here also races. Again pretend it
1317 // used memory_order_relaxed atomics, and use the analogous
1318 // counterintuitive fence.
1319 atomic_thread_fence(memory_order_release);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001320 strlcpy(pi->value, value, len + 1);
1321
Hans Boehm30214b92014-07-31 15:53:22 -07001322 atomic_store_explicit(
1323 &pi->serial,
1324 (len << 24) | ((serial + 1) & 0xffffff),
1325 memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001326 __futex_wake(&pi->serial, INT32_MAX);
1327
Hans Boehm30214b92014-07-31 15:53:22 -07001328 atomic_store_explicit(
Tom Cherry926ebe12015-09-23 15:34:40 -07001329 pa->serial(),
1330 atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001331 memory_order_release);
Tom Cherry926ebe12015-09-23 15:34:40 -07001332 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001333
1334 return 0;
1335}
jiaguo879d3302014-03-13 17:39:58 +08001336
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001337int __system_property_add(const char *name, unsigned int namelen,
1338 const char *value, unsigned int valuelen)
1339{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001340 if (valuelen >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001341 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001342 }
1343
1344 if (namelen < 1) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001345 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001346 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001347
Tom Cherry6ed51c02015-12-04 11:34:42 -08001348 if (!__system_property_area__) {
1349 return -1;
1350 }
1351
Tom Cherry49a309f2015-09-23 16:09:47 -07001352 prop_area* pa = get_prop_area_for_name(name);
1353
1354 if (!pa) {
1355 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001356 return -1;
1357 }
1358
Tom Cherry49a309f2015-09-23 16:09:47 -07001359 bool ret = pa->add(name, namelen, value, valuelen);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001360 if (!ret) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001361 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001362 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001363
Hans Boehm30214b92014-07-31 15:53:22 -07001364 // There is only a single mutator, but we want to make sure that
1365 // updates are visible to a reader waiting for the update.
1366 atomic_store_explicit(
Tom Cherry49a309f2015-09-23 16:09:47 -07001367 __system_property_area__->serial(),
1368 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001369 memory_order_release);
Tom Cherry49a309f2015-09-23 16:09:47 -07001370 __futex_wake(__system_property_area__->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001371 return 0;
1372}
1373
Hans Boehm30214b92014-07-31 15:53:22 -07001374// Wait for non-locked serial, and retrieve it with acquire semantics.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001375unsigned int __system_property_serial(const prop_info *pi)
1376{
Hans Boehm1e8587a2014-08-19 14:07:55 -07001377 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001378 while (SERIAL_DIRTY(serial)) {
Hans Boehm30214b92014-07-31 15:53:22 -07001379 __futex_wait(const_cast<volatile void *>(
1380 reinterpret_cast<const void *>(&pi->serial)),
1381 serial, NULL);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001382 serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001383 }
1384 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001385}
1386
1387unsigned int __system_property_wait_any(unsigned int serial)
1388{
1389 prop_area *pa = __system_property_area__;
Hans Boehm30214b92014-07-31 15:53:22 -07001390 uint32_t my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001391
Tom Cherry6ed51c02015-12-04 11:34:42 -08001392 if (!pa) {
1393 return 0;
1394 }
1395
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001396 do {
Tom Cherry926ebe12015-09-23 15:34:40 -07001397 __futex_wait(pa->serial(), serial, NULL);
1398 my_serial = atomic_load_explicit(pa->serial(), memory_order_acquire);
Hans Boehm30214b92014-07-31 15:53:22 -07001399 } while (my_serial == serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001400
Hans Boehm30214b92014-07-31 15:53:22 -07001401 return my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001402}
1403
1404const prop_info *__system_property_find_nth(unsigned n)
1405{
Dimitry Ivanov581b9f62017-01-09 11:05:52 -08001406 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
1407 __libc_fatal("__system_property_find_nth is not supported since Android O,"
1408 " please use __system_property_foreach instead.");
1409 }
1410
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001411 find_nth_cookie cookie(n);
1412
1413 const int err = __system_property_foreach(find_nth_fn, &cookie);
1414 if (err < 0) {
1415 return NULL;
1416 }
1417
1418 return cookie.pi;
1419}
1420
Elliott Hughesf8562c52017-01-26 16:48:57 -08001421int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001422{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001423 if (!__system_property_area__) {
1424 return -1;
1425 }
1426
Tom Cherry845e24a2015-12-03 15:38:52 -08001427 list_foreach(contexts, [propfn, cookie](context_node* l) {
Tom Cherryb4171692015-12-09 15:48:15 -08001428 if (l->check_access_and_open()) {
1429 l->pa()->foreach(propfn, cookie);
Tom Cherry49a309f2015-09-23 16:09:47 -07001430 }
1431 });
1432 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001433}