blob: 1a7a3598c853bb8807a4f0d28f419dd0bb949c8d [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>
Tom Cherry49a309f2015-09-23 16:09:47 -070034#include <stddef.h>
35#include <stdint.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070036#include <stdlib.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000037#include <string.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070038#include <unistd.h>
39#include <new>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000040
Tom Cherry49a309f2015-09-23 16:09:47 -070041#include <linux/xattr.h>
42#include <netinet/in.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000043#include <sys/mman.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000044#include <sys/select.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070045#include <sys/socket.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000046#include <sys/stat.h>
47#include <sys/types.h>
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -080048#include <sys/uio.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070049#include <sys/un.h>
50#include <sys/xattr.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000051
52#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
53#include <sys/_system_properties.h>
54#include <sys/system_properties.h>
55
Christopher Ferris7a3681e2017-04-24 17:48:32 -070056#include <async_safe/log.h>
57
Elliott Hughes7ade61c2017-04-11 13:38:36 -070058#include "private/ErrnoRestorer.h"
dimitryfa432522017-10-25 13:07:45 +020059#include "private/bionic_defs.h"
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070060#include "private/bionic_futex.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070061#include "private/bionic_lock.h"
Elliott Hughes8eac9af2014-05-09 19:12:08 -070062#include "private/bionic_macros.h"
Dimitry Ivanov581b9f62017-01-09 11:05:52 -080063#include "private/bionic_sdk_version.h"
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000064
Elliott Hughesf8562c52017-01-26 16:48:57 -080065static constexpr int PROP_FILENAME_MAX = 1024;
66
67static constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250;
68static constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab;
69
70static constexpr size_t PA_SIZE = 128 * 1024;
71
Elliott Hughes9160ed92017-01-30 17:54:57 -080072#define SERIAL_DIRTY(serial) ((serial)&1)
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000073#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
Dimitry Ivanov489f58b2017-01-24 18:39:04 +000074
Tom Cherry8a311632017-10-09 13:49:17 -070075constexpr static const char kLongLegacyError[] = "Must use __system_property_read_callback() to read";
76
77// The error message fits in part of a union with the previous 92 char property value so there must
78// be room left over after the error message for the offset to the new longer property value and
79// future expansion fields if needed.
80// Note that this value cannot ever increase. The offset to the new longer property value appears
81// immediately after it, so an increase of this size will break compatibility.
82constexpr size_t kLongLegacyErrorBufferSize = 56;
83static_assert(sizeof(kLongLegacyError) < kLongLegacyErrorBufferSize,
84 "Error message for long properties read by legacy libc must fit within 56 chars");
85
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000086static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
87static const char* kServiceVersionPropertyName = "ro.property_service.version";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000088
Tom Cherry8a311632017-10-09 13:49:17 -070089// The C11 standard doesn't allow atomic loads from const fields,
90// though C++11 does. Fudge it until standards get straightened out.
91static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, memory_order mo) {
92 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
93 return atomic_load_explicit(non_const_s, mo);
94}
95
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000096/*
97 * Properties are stored in a hybrid trie/binary tree structure.
98 * Each property's name is delimited at '.' characters, and the tokens are put
99 * into a trie structure. Siblings at each level of the trie are stored in a
100 * binary tree. For instance, "ro.secure"="1" could be stored as follows:
101 *
102 * +-----+ children +----+ children +--------+
103 * | |-------------->| ro |-------------->| secure |
104 * +-----+ +----+ +--------+
105 * / \ / |
106 * left / \ right left / | prop +===========+
107 * v v v +-------->| ro.secure |
108 * +-----+ +-----+ +-----+ +-----------+
109 * | net | | sys | | com | | 1 |
110 * +-----+ +-----+ +-----+ +===========+
111 */
112
113// Represents a node in the trie.
114struct prop_bt {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800115 uint32_t namelen;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000116
Elliott Hughes9160ed92017-01-30 17:54:57 -0800117 // The property trie is updated only by the init process (single threaded) which provides
118 // property service. And it can be read by multiple threads at the same time.
119 // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the
120 // left, right, children "pointers" in the trie node. To make sure readers who see the
121 // change of "pointers" can also notice the change of prop_bt structure contents pointed by
122 // the "pointers", we always use release-consume ordering pair when accessing these "pointers".
Yabin Cuib8ce4742015-02-10 21:35:56 -0800123
Elliott Hughes9160ed92017-01-30 17:54:57 -0800124 // prop "points" to prop_info structure if there is a propery associated with the trie node.
125 // Its situation is similar to the left, right, children "pointers". So we use
126 // atomic_uint_least32_t and release-consume ordering to protect it as well.
Yabin Cuib8ce4742015-02-10 21:35:56 -0800127
Elliott Hughes9160ed92017-01-30 17:54:57 -0800128 // We should also avoid rereading these fields redundantly, since not
129 // all processor implementations ensure that multiple loads from the
130 // same field are carried out in the right order.
131 atomic_uint_least32_t prop;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000132
Elliott Hughes9160ed92017-01-30 17:54:57 -0800133 atomic_uint_least32_t left;
134 atomic_uint_least32_t right;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000135
Elliott Hughes9160ed92017-01-30 17:54:57 -0800136 atomic_uint_least32_t children;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000137
Elliott Hughes9160ed92017-01-30 17:54:57 -0800138 char name[0];
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000139
Elliott Hughes9160ed92017-01-30 17:54:57 -0800140 prop_bt(const char* name, const uint32_t name_length) {
141 this->namelen = name_length;
142 memcpy(this->name, name, name_length);
143 this->name[name_length] = '\0';
144 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000145
Elliott Hughes9160ed92017-01-30 17:54:57 -0800146 private:
147 DISALLOW_COPY_AND_ASSIGN(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000148};
149
Tom Cherry926ebe12015-09-23 15:34:40 -0700150class prop_area {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800151 public:
152 prop_area(const uint32_t magic, const uint32_t version) : magic_(magic), version_(version) {
153 atomic_init(&serial_, 0);
154 memset(reserved_, 0, sizeof(reserved_));
155 // Allocate enough space for the root node.
156 bytes_used_ = sizeof(prop_bt);
157 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000158
Elliott Hughes9160ed92017-01-30 17:54:57 -0800159 const prop_info* find(const char* name);
160 bool add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000161
Elliott Hughes9160ed92017-01-30 17:54:57 -0800162 bool foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie);
Tom Cherry926ebe12015-09-23 15:34:40 -0700163
Elliott Hughes9160ed92017-01-30 17:54:57 -0800164 atomic_uint_least32_t* serial() {
165 return &serial_;
166 }
167 uint32_t magic() const {
168 return magic_;
169 }
170 uint32_t version() const {
171 return version_;
172 }
Tom Cherry926ebe12015-09-23 15:34:40 -0700173
Elliott Hughes9160ed92017-01-30 17:54:57 -0800174 private:
175 void* allocate_obj(const size_t size, uint_least32_t* const off);
176 prop_bt* new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off);
177 prop_info* new_prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen,
178 uint_least32_t* const off);
179 void* to_prop_obj(uint_least32_t off);
180 prop_bt* to_prop_bt(atomic_uint_least32_t* off_p);
181 prop_info* to_prop_info(atomic_uint_least32_t* off_p);
Tom Cherry926ebe12015-09-23 15:34:40 -0700182
Elliott Hughes9160ed92017-01-30 17:54:57 -0800183 prop_bt* root_node();
Tom Cherry926ebe12015-09-23 15:34:40 -0700184
Elliott Hughes9160ed92017-01-30 17:54:57 -0800185 prop_bt* find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700186
Elliott Hughes9160ed92017-01-30 17:54:57 -0800187 const prop_info* find_property(prop_bt* const trie, const char* name, uint32_t namelen,
188 const char* value, uint32_t valuelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700189
Elliott Hughes9160ed92017-01-30 17:54:57 -0800190 bool foreach_property(prop_bt* const trie, void (*propfn)(const prop_info* pi, void* cookie),
191 void* cookie);
Tom Cherry926ebe12015-09-23 15:34:40 -0700192
Elliott Hughes9160ed92017-01-30 17:54:57 -0800193 uint32_t bytes_used_;
194 atomic_uint_least32_t serial_;
195 uint32_t magic_;
196 uint32_t version_;
197 uint32_t reserved_[28];
198 char data_[0];
Tom Cherry926ebe12015-09-23 15:34:40 -0700199
Elliott Hughes9160ed92017-01-30 17:54:57 -0800200 DISALLOW_COPY_AND_ASSIGN(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000201};
202
203struct prop_info {
Tom Cherry8a311632017-10-09 13:49:17 -0700204 // Read only properties will not set anything but the bottom most bit of serial and the top byte.
205 // We borrow the 2nd from the top byte for extra flags, and use the bottom most bit of that for
206 // our first user, kLongFlag.
207 constexpr static uint32_t kLongFlag = 1 << 16;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800208 atomic_uint_least32_t serial;
209 // we need to keep this buffer around because the property
210 // value can be modified whereas name is constant.
Tom Cherry8a311632017-10-09 13:49:17 -0700211 union {
212 char value[PROP_VALUE_MAX];
213 struct {
214 char error_message[kLongLegacyErrorBufferSize];
215 uint32_t offset;
216 } long_property;
217 };
Elliott Hughes9160ed92017-01-30 17:54:57 -0800218 char name[0];
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000219
Tom Cherry8a311632017-10-09 13:49:17 -0700220 bool is_long() const {
221 return (load_const_atomic(&serial, memory_order_relaxed) & kLongFlag) != 0;
222 }
223
224 const char* long_value() const {
225 // We can't store pointers here since this is shared memory that will have different absolute
226 // pointers in different processes. We don't have data_ from prop_area, but since we know
227 // `this` is data_ + some offset and long_value is data_ + some other offset, we calculate the
228 // offset from `this` to long_value and store it as long_property.offset.
229 return reinterpret_cast<const char*>(this) + long_property.offset;
230 }
231
Elliott Hughes9160ed92017-01-30 17:54:57 -0800232 prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen) {
233 memcpy(this->name, name, namelen);
234 this->name[namelen] = '\0';
235 atomic_init(&this->serial, valuelen << 24);
236 memcpy(this->value, value, valuelen);
237 this->value[valuelen] = '\0';
238 }
239
Tom Cherry8a311632017-10-09 13:49:17 -0700240 prop_info(const char* name, uint32_t namelen, uint32_t long_offset) {
241 memcpy(this->name, name, namelen);
242 this->name[namelen] = '\0';
243
244 auto error_value_len = sizeof(kLongLegacyError) - 1;
245 atomic_init(&this->serial, error_value_len << 24 | kLongFlag);
246 memcpy(this->long_property.error_message, kLongLegacyError, sizeof(kLongLegacyError));
247
248 this->long_property.offset = long_offset;
249 }
250
Elliott Hughes9160ed92017-01-30 17:54:57 -0800251 private:
252 DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000253};
254
Tom Cherry8a311632017-10-09 13:49:17 -0700255static_assert(sizeof(prop_info) == 96, "size of struct prop_info must be 96 bytes");
256
Elliott Hughesf8562c52017-01-26 16:48:57 -0800257// This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol.
dimitryfa432522017-10-25 13:07:45 +0200258__BIONIC_WEAK_VARIABLE_FOR_NATIVE_BRIDGE
Elliott Hughesf8562c52017-01-26 16:48:57 -0800259prop_area* __system_property_area__ = nullptr;
260
Tom Cherry49a309f2015-09-23 16:09:47 -0700261static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000262static size_t pa_data_size;
263static size_t pa_size;
Tom Cherryb4171692015-12-09 15:48:15 -0800264static bool initialized = false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000265
Tom Cherry49a309f2015-09-23 16:09:47 -0700266static prop_area* map_prop_area_rw(const char* filename, const char* context,
267 bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800268 /* dev is a tmpfs that we can use to carve a shared workspace
269 * out of, so let's do that...
270 */
271 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000272
Elliott Hughes9160ed92017-01-30 17:54:57 -0800273 if (fd < 0) {
274 if (errno == EACCES) {
275 /* for consistency with the case where the process has already
276 * mapped the page in and segfaults when trying to write to it
277 */
278 abort();
Tom Cherry49a309f2015-09-23 16:09:47 -0700279 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800280 return nullptr;
281 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700282
Elliott Hughes9160ed92017-01-30 17:54:57 -0800283 if (context) {
284 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700285 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
286 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800287 /*
288 * fsetxattr() will fail during system properties tests due to selinux policy.
289 * We do not want to create a custom policy for the tester, so we will continue in
290 * this function but set a flag that an error has occurred.
291 * Init, which is the only daemon that should ever call this function will abort
292 * when this error occurs.
293 * Otherwise, the tester will ignore it and continue, albeit without any selinux
294 * property separation.
295 */
296 if (fsetxattr_failed) {
297 *fsetxattr_failed = true;
298 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000299 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800300 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000301
Elliott Hughes9160ed92017-01-30 17:54:57 -0800302 if (ftruncate(fd, PA_SIZE) < 0) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000303 close(fd);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800304 return nullptr;
305 }
306
307 pa_size = PA_SIZE;
308 pa_data_size = pa_size - sizeof(prop_area);
309
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800310 void* const memory_area = mmap(nullptr, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800311 if (memory_area == MAP_FAILED) {
312 close(fd);
313 return nullptr;
314 }
315
316 prop_area* pa = new (memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
317
318 close(fd);
319 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000320}
321
Tom Cherry49a309f2015-09-23 16:09:47 -0700322static prop_area* map_fd_ro(const int fd) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800323 struct stat fd_stat;
324 if (fstat(fd, &fd_stat) < 0) {
325 return nullptr;
326 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000327
Elliott Hughes9160ed92017-01-30 17:54:57 -0800328 if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0) ||
329 ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) ||
330 (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area)))) {
331 return nullptr;
332 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000333
Elliott Hughes9160ed92017-01-30 17:54:57 -0800334 pa_size = fd_stat.st_size;
335 pa_data_size = pa_size - sizeof(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000336
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800337 void* const map_result = mmap(nullptr, pa_size, PROT_READ, MAP_SHARED, fd, 0);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800338 if (map_result == MAP_FAILED) {
339 return nullptr;
340 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000341
Elliott Hughes9160ed92017-01-30 17:54:57 -0800342 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
343 if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) {
344 munmap(pa, pa_size);
345 return nullptr;
346 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000347
Elliott Hughes9160ed92017-01-30 17:54:57 -0800348 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000349}
350
Elliott Hughesf8562c52017-01-26 16:48:57 -0800351static prop_area* map_prop_area(const char* filename) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800352 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
353 if (fd == -1) return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000354
Elliott Hughes9160ed92017-01-30 17:54:57 -0800355 prop_area* map_result = map_fd_ro(fd);
356 close(fd);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000357
Elliott Hughes9160ed92017-01-30 17:54:57 -0800358 return map_result;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000359}
360
Elliott Hughes9160ed92017-01-30 17:54:57 -0800361void* prop_area::allocate_obj(const size_t size, uint_least32_t* const off) {
Dan Alberta613d0d2017-10-05 16:39:33 -0700362 const size_t aligned = __BIONIC_ALIGN(size, sizeof(uint_least32_t));
Elliott Hughes9160ed92017-01-30 17:54:57 -0800363 if (bytes_used_ + aligned > pa_data_size) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800364 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800365 }
366
367 *off = bytes_used_;
368 bytes_used_ += aligned;
369 return data_ + *off;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000370}
371
Elliott Hughes9160ed92017-01-30 17:54:57 -0800372prop_bt* prop_area::new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off) {
373 uint_least32_t new_offset;
374 void* const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800375 if (p != nullptr) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800376 prop_bt* bt = new (p) prop_bt(name, namelen);
377 *off = new_offset;
378 return bt;
379 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000380
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800381 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000382}
383
Elliott Hughes9160ed92017-01-30 17:54:57 -0800384prop_info* prop_area::new_prop_info(const char* name, uint32_t namelen, const char* value,
385 uint32_t valuelen, uint_least32_t* const off) {
386 uint_least32_t new_offset;
387 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
Tom Cherry8a311632017-10-09 13:49:17 -0700388 if (p == nullptr) return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000389
Tom Cherry8a311632017-10-09 13:49:17 -0700390 prop_info* info;
391 if (valuelen >= PROP_VALUE_MAX) {
392 uint32_t long_value_offset = 0;
393 char* long_location = reinterpret_cast<char*>(allocate_obj(valuelen + 1, &long_value_offset));
394 if (!long_location) return nullptr;
395
396 memcpy(long_location, value, valuelen);
397 long_location[valuelen] = '\0';
398
399 // Both new_offset and long_value_offset are offsets based off of data_, however prop_info
400 // does not know what data_ is, so we change this offset to be an offset from the prop_info
401 // pointer that contains it.
402 long_value_offset -= new_offset;
403
404 info = new (p) prop_info(name, namelen, long_value_offset);
405 } else {
406 info = new (p) prop_info(name, namelen, value, valuelen);
407 }
408 *off = new_offset;
409 return info;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000410}
411
Elliott Hughes9160ed92017-01-30 17:54:57 -0800412void* prop_area::to_prop_obj(uint_least32_t off) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800413 if (off > pa_data_size) return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800414
415 return (data_ + off);
416}
417
418inline prop_bt* prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800419 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
420 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
421}
422
Elliott Hughes9160ed92017-01-30 17:54:57 -0800423inline prop_info* prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800424 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
425 return reinterpret_cast<prop_info*>(to_prop_obj(off));
426}
427
Elliott Hughes9160ed92017-01-30 17:54:57 -0800428inline prop_bt* prop_area::root_node() {
429 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000430}
431
Elliott Hughes9160ed92017-01-30 17:54:57 -0800432static int cmp_prop_name(const char* one, uint32_t one_len, const char* two, uint32_t two_len) {
433 if (one_len < two_len)
434 return -1;
435 else if (one_len > two_len)
436 return 1;
437 else
438 return strncmp(one, two, one_len);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000439}
440
Elliott Hughes9160ed92017-01-30 17:54:57 -0800441prop_bt* prop_area::find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen,
442 bool alloc_if_needed) {
443 prop_bt* current = bt;
444 while (true) {
445 if (!current) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800446 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000447 }
448
Elliott Hughes9160ed92017-01-30 17:54:57 -0800449 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
450 if (ret == 0) {
451 return current;
452 }
453
454 if (ret < 0) {
455 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
456 if (left_offset != 0) {
457 current = to_prop_bt(&current->left);
458 } else {
459 if (!alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800460 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800461 }
462
Yabin Cuib8ce4742015-02-10 21:35:56 -0800463 uint_least32_t new_offset;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800464 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
465 if (new_bt) {
466 atomic_store_explicit(&current->left, new_offset, memory_order_release);
467 }
468 return new_bt;
469 }
470 } else {
471 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
472 if (right_offset != 0) {
473 current = to_prop_bt(&current->right);
474 } else {
475 if (!alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800476 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000477 }
478
Elliott Hughes9160ed92017-01-30 17:54:57 -0800479 uint_least32_t new_offset;
480 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
481 if (new_bt) {
482 atomic_store_explicit(&current->right, new_offset, memory_order_release);
483 }
484 return new_bt;
485 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000486 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800487 }
488}
489
490const prop_info* prop_area::find_property(prop_bt* const trie, const char* name, uint32_t namelen,
491 const char* value, uint32_t valuelen,
492 bool alloc_if_needed) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800493 if (!trie) return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800494
495 const char* remaining_name = name;
496 prop_bt* current = trie;
497 while (true) {
498 const char* sep = strchr(remaining_name, '.');
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800499 const bool want_subtree = (sep != nullptr);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800500 const uint32_t substr_size = (want_subtree) ? sep - remaining_name : strlen(remaining_name);
501
502 if (!substr_size) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800503 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800504 }
505
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800506 prop_bt* root = nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800507 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
508 if (children_offset != 0) {
509 root = to_prop_bt(&current->children);
510 } else if (alloc_if_needed) {
511 uint_least32_t new_offset;
512 root = new_prop_bt(remaining_name, substr_size, &new_offset);
513 if (root) {
514 atomic_store_explicit(&current->children, new_offset, memory_order_release);
515 }
516 }
517
518 if (!root) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800519 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800520 }
521
522 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
523 if (!current) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800524 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800525 }
526
527 if (!want_subtree) break;
528
529 remaining_name = sep + 1;
530 }
531
532 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
533 if (prop_offset != 0) {
534 return to_prop_info(&current->prop);
535 } else if (alloc_if_needed) {
536 uint_least32_t new_offset;
537 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
538 if (new_info) {
539 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
540 }
541
542 return new_info;
543 } else {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800544 return nullptr;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800545 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000546}
547
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000548class PropertyServiceConnection {
549 public:
550 PropertyServiceConnection() : last_error_(0) {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800551 socket_ = ::socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
552 if (socket_ == -1) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000553 last_error_ = errno;
554 return;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000555 }
556
557 const size_t namelen = strlen(property_service_socket);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000558 sockaddr_un addr;
559 memset(&addr, 0, sizeof(addr));
560 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
561 addr.sun_family = AF_LOCAL;
562 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000563
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800564 if (TEMP_FAILURE_RETRY(connect(socket_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
Tom Cherrybe4232b2017-04-14 13:45:20 -0700565 last_error_ = errno;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800566 close(socket_);
567 socket_ = -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000568 }
569 }
570
571 bool IsValid() {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800572 return socket_ != -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000573 }
574
575 int GetLastError() {
576 return last_error_;
577 }
578
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000579 bool RecvInt32(int32_t* value) {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800580 int result = TEMP_FAILURE_RETRY(recv(socket_, value, sizeof(*value), MSG_WAITALL));
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000581 return CheckSendRecvResult(result, sizeof(*value));
582 }
583
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800584 int socket() {
585 return socket_;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000586 }
587
588 ~PropertyServiceConnection() {
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800589 if (socket_ != -1) {
590 close(socket_);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000591 }
592 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800593
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000594 private:
595 bool CheckSendRecvResult(int result, int expected_len) {
596 if (result == -1) {
597 last_error_ = errno;
598 } else if (result != expected_len) {
599 last_error_ = -1;
600 } else {
601 last_error_ = 0;
602 }
603
604 return last_error_ == 0;
605 }
606
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800607 int socket_;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000608 int last_error_;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800609
610 friend class SocketWriter;
611};
612
613class SocketWriter {
614 public:
615 explicit SocketWriter(PropertyServiceConnection* connection)
616 : connection_(connection), iov_index_(0), uint_buf_index_(0)
617 {}
618
619 SocketWriter& WriteUint32(uint32_t value) {
620 CHECK(uint_buf_index_ < kUintBufSize);
621 CHECK(iov_index_ < kIovSize);
622 uint32_t* ptr = uint_buf_ + uint_buf_index_;
623 uint_buf_[uint_buf_index_++] = value;
624 iov_[iov_index_].iov_base = ptr;
625 iov_[iov_index_].iov_len = sizeof(*ptr);
626 ++iov_index_;
627 return *this;
628 }
629
630 SocketWriter& WriteString(const char* value) {
631 uint32_t valuelen = strlen(value);
632 WriteUint32(valuelen);
633 if (valuelen == 0) {
634 return *this;
635 }
636
637 CHECK(iov_index_ < kIovSize);
638 iov_[iov_index_].iov_base = const_cast<char*>(value);
639 iov_[iov_index_].iov_len = valuelen;
640 ++iov_index_;
641
642 return *this;
643 }
644
645 bool Send() {
646 if (!connection_->IsValid()) {
647 return false;
648 }
649
650 if (writev(connection_->socket(), iov_, iov_index_) == -1) {
651 connection_->last_error_ = errno;
652 return false;
653 }
654
655 iov_index_ = uint_buf_index_ = 0;
656 return true;
657 }
658
659 private:
660 static constexpr size_t kUintBufSize = 8;
661 static constexpr size_t kIovSize = 8;
662
663 PropertyServiceConnection* connection_;
664 iovec iov_[kIovSize];
665 size_t iov_index_;
666 uint32_t uint_buf_[kUintBufSize];
667 size_t uint_buf_index_;
668
669 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketWriter);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000670};
671
Elliott Hughesf8562c52017-01-26 16:48:57 -0800672struct prop_msg {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800673 unsigned cmd;
674 char name[PROP_NAME_MAX];
675 char value[PROP_VALUE_MAX];
Elliott Hughesf8562c52017-01-26 16:48:57 -0800676};
677
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000678static int send_prop_msg(const prop_msg* msg) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800679 PropertyServiceConnection connection;
680 if (!connection.IsValid()) {
681 return connection.GetLastError();
682 }
683
684 int result = -1;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800685 int s = connection.socket();
Elliott Hughes9160ed92017-01-30 17:54:57 -0800686
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800687 const int num_bytes = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
Elliott Hughes9160ed92017-01-30 17:54:57 -0800688 if (num_bytes == sizeof(prop_msg)) {
689 // We successfully wrote to the property server but now we
690 // wait for the property server to finish its work. It
691 // acknowledges its completion by closing the socket so we
692 // poll here (on nothing), waiting for the socket to close.
693 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
694 // once the socket closes. Out of paranoia we cap our poll
695 // at 250 ms.
696 pollfd pollfds[1];
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800697 pollfds[0].fd = s;
Elliott Hughes9160ed92017-01-30 17:54:57 -0800698 pollfds[0].events = 0;
699 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
700 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
701 result = 0;
702 } else {
703 // Ignore the timeout and treat it like a success anyway.
704 // The init process is single-threaded and its property
705 // service is sometimes slow to respond (perhaps it's off
706 // starting a child process or something) and thus this
707 // times out and the caller thinks it failed, even though
708 // it's still getting around to it. So we fake it here,
709 // mostly for ctl.* properties, but we do try and wait 250
710 // ms so callers who do read-after-write can reliably see
711 // what they've written. Most of the time.
712 // TODO: fix the system properties design.
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700713 async_safe_format_log(ANDROID_LOG_WARN, "libc",
714 "Property service has timed out while trying to set \"%s\" to \"%s\"",
715 msg->name, msg->value);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800716 result = 0;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000717 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800718 }
Dimitry Ivanov489f58b2017-01-24 18:39:04 +0000719
Elliott Hughes9160ed92017-01-30 17:54:57 -0800720 return result;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000721}
722
Elliott Hughes9160ed92017-01-30 17:54:57 -0800723bool prop_area::foreach_property(prop_bt* const trie,
724 void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
725 if (!trie) return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000726
Elliott Hughes9160ed92017-01-30 17:54:57 -0800727 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
728 if (left_offset != 0) {
729 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
730 if (err < 0) return false;
731 }
732 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
733 if (prop_offset != 0) {
734 prop_info* info = to_prop_info(&trie->prop);
735 if (!info) return false;
736 propfn(info, cookie);
737 }
738 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
739 if (children_offset != 0) {
740 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
741 if (err < 0) return false;
742 }
743 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
744 if (right_offset != 0) {
745 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
746 if (err < 0) return false;
747 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000748
Elliott Hughes9160ed92017-01-30 17:54:57 -0800749 return true;
Tom Cherry926ebe12015-09-23 15:34:40 -0700750}
751
Elliott Hughes9160ed92017-01-30 17:54:57 -0800752const prop_info* prop_area::find(const char* name) {
753 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
Tom Cherry926ebe12015-09-23 15:34:40 -0700754}
755
Elliott Hughes9160ed92017-01-30 17:54:57 -0800756bool prop_area::add(const char* name, unsigned int namelen, const char* value,
757 unsigned int valuelen) {
758 return find_property(root_node(), name, namelen, value, valuelen, true);
Tom Cherry926ebe12015-09-23 15:34:40 -0700759}
760
Elliott Hughes9160ed92017-01-30 17:54:57 -0800761bool prop_area::foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
762 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000763}
764
Tom Cherryb4171692015-12-09 15:48:15 -0800765class context_node {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800766 public:
767 context_node(context_node* next, const char* context, prop_area* pa)
768 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
769 lock_.init(false);
770 }
771 ~context_node() {
772 unmap();
773 free(context_);
774 }
775 bool open(bool access_rw, bool* fsetxattr_failed);
776 bool check_access_and_open();
777 void reset_access();
Tom Cherryb4171692015-12-09 15:48:15 -0800778
Elliott Hughes9160ed92017-01-30 17:54:57 -0800779 const char* context() const {
780 return context_;
781 }
782 prop_area* pa() {
783 return pa_;
784 }
Tom Cherryb4171692015-12-09 15:48:15 -0800785
Elliott Hughes9160ed92017-01-30 17:54:57 -0800786 context_node* next;
Tom Cherryb4171692015-12-09 15:48:15 -0800787
Elliott Hughes9160ed92017-01-30 17:54:57 -0800788 private:
789 bool check_access();
790 void unmap();
Tom Cherryb4171692015-12-09 15:48:15 -0800791
Elliott Hughes9160ed92017-01-30 17:54:57 -0800792 Lock lock_;
793 char* context_;
794 prop_area* pa_;
795 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700796};
797
798struct prefix_node {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800799 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
800 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
801 }
802 ~prefix_node() {
803 free(prefix);
804 }
805 char* prefix;
806 const size_t prefix_len;
807 context_node* context;
808 struct prefix_node* next;
Tom Cherry49a309f2015-09-23 16:09:47 -0700809};
810
811template <typename List, typename... Args>
812static inline void list_add(List** list, Args... args) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800813 *list = new List(*list, args...);
Tom Cherry49a309f2015-09-23 16:09:47 -0700814}
815
816static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800817 size_t prefix_len = strlen(prefix);
Tom Cherry49a309f2015-09-23 16:09:47 -0700818
Elliott Hughes9160ed92017-01-30 17:54:57 -0800819 auto next_list = list;
Tom Cherry49a309f2015-09-23 16:09:47 -0700820
Elliott Hughes9160ed92017-01-30 17:54:57 -0800821 while (*next_list) {
822 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
823 list_add(next_list, prefix, context);
824 return;
Tom Cherry49a309f2015-09-23 16:09:47 -0700825 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800826 next_list = &(*next_list)->next;
827 }
828 list_add(next_list, prefix, context);
Tom Cherry49a309f2015-09-23 16:09:47 -0700829}
830
831template <typename List, typename Func>
832static void list_foreach(List* list, Func func) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800833 while (list) {
834 func(list);
835 list = list->next;
836 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700837}
838
839template <typename List, typename Func>
840static List* list_find(List* list, Func func) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800841 while (list) {
842 if (func(list)) {
843 return list;
Tom Cherry49a309f2015-09-23 16:09:47 -0700844 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800845 list = list->next;
846 }
847 return nullptr;
Tom Cherry49a309f2015-09-23 16:09:47 -0700848}
849
850template <typename List>
851static void list_free(List** list) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800852 while (*list) {
853 auto old_list = *list;
854 *list = old_list->next;
855 delete old_list;
856 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700857}
858
859static prefix_node* prefixes = nullptr;
860static context_node* contexts = nullptr;
861
862/*
863 * pthread_mutex_lock() calls into system_properties in the case of contention.
864 * This creates a risk of dead lock if any system_properties functions
865 * use pthread locks after system_property initialization.
866 *
867 * For this reason, the below three functions use a bionic Lock and static
868 * allocation of memory for each filename.
869 */
870
Tom Cherryb4171692015-12-09 15:48:15 -0800871bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800872 lock_.lock();
873 if (pa_) {
Tom Cherryb4171692015-12-09 15:48:15 -0800874 lock_.unlock();
Elliott Hughes9160ed92017-01-30 17:54:57 -0800875 return true;
876 }
877
878 char filename[PROP_FILENAME_MAX];
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700879 int len = async_safe_format_buffer(filename, sizeof(filename), "%s/%s", property_filename,
880 context_);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800881 if (len < 0 || len > PROP_FILENAME_MAX) {
882 lock_.unlock();
883 return false;
884 }
885
886 if (access_rw) {
887 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
888 } else {
889 pa_ = map_prop_area(filename);
890 }
891 lock_.unlock();
892 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700893}
894
Tom Cherryb4171692015-12-09 15:48:15 -0800895bool context_node::check_access_and_open() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800896 if (!pa_ && !no_access_) {
897 if (!check_access() || !open(false, nullptr)) {
898 no_access_ = true;
Tom Cherryb4171692015-12-09 15:48:15 -0800899 }
Elliott Hughes9160ed92017-01-30 17:54:57 -0800900 }
901 return pa_;
Tom Cherryb4171692015-12-09 15:48:15 -0800902}
903
904void context_node::reset_access() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800905 if (!check_access()) {
906 unmap();
907 no_access_ = true;
908 } else {
909 no_access_ = false;
910 }
Tom Cherryb4171692015-12-09 15:48:15 -0800911}
912
913bool context_node::check_access() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800914 char filename[PROP_FILENAME_MAX];
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700915 int len = async_safe_format_buffer(filename, sizeof(filename), "%s/%s", property_filename,
916 context_);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800917 if (len < 0 || len > PROP_FILENAME_MAX) {
918 return false;
919 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700920
Elliott Hughes9160ed92017-01-30 17:54:57 -0800921 return access(filename, R_OK) == 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700922}
923
Tom Cherryb4171692015-12-09 15:48:15 -0800924void context_node::unmap() {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800925 if (!pa_) {
926 return;
927 }
Tom Cherryb4171692015-12-09 15:48:15 -0800928
Elliott Hughes9160ed92017-01-30 17:54:57 -0800929 munmap(pa_, pa_size);
930 if (pa_ == __system_property_area__) {
931 __system_property_area__ = nullptr;
932 }
933 pa_ = nullptr;
Tom Cherryb4171692015-12-09 15:48:15 -0800934}
935
Tom Cherry49a309f2015-09-23 16:09:47 -0700936static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800937 char filename[PROP_FILENAME_MAX];
938 int len =
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700939 async_safe_format_buffer(filename, sizeof(filename), "%s/properties_serial",
940 property_filename);
Elliott Hughes9160ed92017-01-30 17:54:57 -0800941 if (len < 0 || len > PROP_FILENAME_MAX) {
942 __system_property_area__ = nullptr;
943 return false;
944 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700945
Elliott Hughes9160ed92017-01-30 17:54:57 -0800946 if (access_rw) {
947 __system_property_area__ =
948 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
949 } else {
950 __system_property_area__ = map_prop_area(filename);
951 }
952 return __system_property_area__;
Tom Cherry49a309f2015-09-23 16:09:47 -0700953}
954
955static prop_area* get_prop_area_for_name(const char* name) {
Elliott Hughes9160ed92017-01-30 17:54:57 -0800956 auto entry = list_find(prefixes, [name](prefix_node* l) {
957 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
958 });
959 if (!entry) {
960 return nullptr;
961 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700962
Elliott Hughes9160ed92017-01-30 17:54:57 -0800963 auto cnode = entry->context;
964 if (!cnode->pa()) {
965 /*
966 * We explicitly do not check no_access_ in this case because unlike the
967 * case of foreach(), we want to generate an selinux audit for each
968 * non-permitted property access in this function.
969 */
970 cnode->open(false, nullptr);
971 }
972 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700973}
974
975/*
976 * The below two functions are duplicated from label_support.c in libselinux.
977 * TODO: Find a location suitable for these functions such that both libc and
978 * libselinux can share a common source file.
979 */
980
981/*
982 * The read_spec_entries and read_spec_entry functions may be used to
983 * replace sscanf to read entries from spec files. The file and
984 * property services now use these.
985 */
986
987/* Read an entry from a spec file (e.g. file_contexts) */
Elliott Hughes9160ed92017-01-30 17:54:57 -0800988static inline int read_spec_entry(char** entry, char** ptr, int* len) {
Elliott Hughes8e7396c2017-01-31 08:24:43 -0800989 *entry = nullptr;
990 char* tmp_buf = nullptr;
Tom Cherry49a309f2015-09-23 16:09:47 -0700991
Elliott Hughes9160ed92017-01-30 17:54:57 -0800992 while (isspace(**ptr) && **ptr != '\0') (*ptr)++;
Tom Cherry49a309f2015-09-23 16:09:47 -0700993
Elliott Hughes9160ed92017-01-30 17:54:57 -0800994 tmp_buf = *ptr;
995 *len = 0;
Tom Cherry49a309f2015-09-23 16:09:47 -0700996
Elliott Hughes9160ed92017-01-30 17:54:57 -0800997 while (!isspace(**ptr) && **ptr != '\0') {
998 (*ptr)++;
999 (*len)++;
1000 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001001
Elliott Hughes9160ed92017-01-30 17:54:57 -08001002 if (*len) {
1003 *entry = strndup(tmp_buf, *len);
1004 if (!*entry) return -1;
1005 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001006
Elliott Hughes9160ed92017-01-30 17:54:57 -08001007 return 0;
Tom Cherry49a309f2015-09-23 16:09:47 -07001008}
1009
1010/*
1011 * line_buf - Buffer containing the spec entries .
1012 * num_args - The number of spec parameter entries to process.
1013 * ... - A 'char **spec_entry' for each parameter.
1014 * returns - The number of items processed.
1015 *
1016 * This function calls read_spec_entry() to do the actual string processing.
1017 */
Elliott Hughes9160ed92017-01-30 17:54:57 -08001018static int read_spec_entries(char* line_buf, int num_args, ...) {
1019 char **spec_entry, *buf_p;
1020 int len, rc, items, entry_len = 0;
1021 va_list ap;
Tom Cherry49a309f2015-09-23 16:09:47 -07001022
Elliott Hughes9160ed92017-01-30 17:54:57 -08001023 len = strlen(line_buf);
1024 if (line_buf[len - 1] == '\n')
1025 line_buf[len - 1] = '\0';
1026 else
1027 /* Handle case if line not \n terminated by bumping
1028 * the len for the check below (as the line is NUL
1029 * terminated by getline(3)) */
1030 len++;
Tom Cherry49a309f2015-09-23 16:09:47 -07001031
Elliott Hughes9160ed92017-01-30 17:54:57 -08001032 buf_p = line_buf;
1033 while (isspace(*buf_p)) buf_p++;
Tom Cherry49a309f2015-09-23 16:09:47 -07001034
Elliott Hughes9160ed92017-01-30 17:54:57 -08001035 /* Skip comment lines and empty lines. */
1036 if (*buf_p == '#' || *buf_p == '\0') return 0;
Tom Cherry49a309f2015-09-23 16:09:47 -07001037
Elliott Hughes9160ed92017-01-30 17:54:57 -08001038 /* Process the spec file entries */
1039 va_start(ap, num_args);
Tom Cherry49a309f2015-09-23 16:09:47 -07001040
Elliott Hughes9160ed92017-01-30 17:54:57 -08001041 items = 0;
1042 while (items < num_args) {
1043 spec_entry = va_arg(ap, char**);
Tom Cherry49a309f2015-09-23 16:09:47 -07001044
Elliott Hughes9160ed92017-01-30 17:54:57 -08001045 if (len - 1 == buf_p - line_buf) {
1046 va_end(ap);
1047 return items;
Tom Cherry49a309f2015-09-23 16:09:47 -07001048 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001049
1050 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
1051 if (rc < 0) {
1052 va_end(ap);
1053 return rc;
1054 }
1055 if (entry_len) items++;
1056 }
1057 va_end(ap);
1058 return items;
Tom Cherry49a309f2015-09-23 16:09:47 -07001059}
1060
Elliott Hughes9160ed92017-01-30 17:54:57 -08001061static bool initialize_properties_from_file(const char* filename) {
1062 FILE* file = fopen(filename, "re");
1063 if (!file) {
1064 return false;
1065 }
1066
1067 char* buffer = nullptr;
1068 size_t line_len;
1069 char* prop_prefix = nullptr;
1070 char* context = nullptr;
1071
1072 while (getline(&buffer, &line_len, file) > 0) {
1073 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1074 if (items <= 0) {
1075 continue;
1076 }
1077 if (items == 1) {
1078 free(prop_prefix);
1079 continue;
1080 }
1081 /*
1082 * init uses ctl.* properties as an IPC mechanism and does not write them
1083 * to a property file, therefore we do not need to create property files
1084 * to store them.
1085 */
1086 if (!strncmp(prop_prefix, "ctl.", 4)) {
1087 free(prop_prefix);
1088 free(context);
1089 continue;
Tom Cherry49a309f2015-09-23 16:09:47 -07001090 }
1091
Elliott Hughes9160ed92017-01-30 17:54:57 -08001092 auto old_context =
1093 list_find(contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
1094 if (old_context) {
1095 list_add_after_len(&prefixes, prop_prefix, old_context);
1096 } else {
1097 list_add(&contexts, context, nullptr);
1098 list_add_after_len(&prefixes, prop_prefix, contexts);
Tom Cherry49a309f2015-09-23 16:09:47 -07001099 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001100 free(prop_prefix);
1101 free(context);
1102 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001103
Elliott Hughes9160ed92017-01-30 17:54:57 -08001104 free(buffer);
1105 fclose(file);
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001106
Elliott Hughes9160ed92017-01-30 17:54:57 -08001107 return true;
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001108}
1109
1110static bool initialize_properties() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001111 // If we do find /property_contexts, then this is being
1112 // run as part of the OTA updater on older release that had
1113 // /property_contexts - b/34370523
1114 if (initialize_properties_from_file("/property_contexts")) {
Tom Cherry49a309f2015-09-23 16:09:47 -07001115 return true;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001116 }
1117
Alex Klyubin176cf1f2017-03-08 13:16:03 -08001118 // Use property_contexts from /system & /vendor, fall back to those from /
1119 if (access("/system/etc/selinux/plat_property_contexts", R_OK) != -1) {
1120 if (!initialize_properties_from_file("/system/etc/selinux/plat_property_contexts")) {
1121 return false;
1122 }
Sandeep Patil03ed4af2017-05-17 11:33:37 -07001123 // Don't check for failure here, so we always have a sane list of properties.
1124 // E.g. In case of recovery, the vendor partition will not have mounted and we
1125 // still need the system / platform properties to function.
1126 initialize_properties_from_file("/vendor/etc/selinux/nonplat_property_contexts");
Alex Klyubin176cf1f2017-03-08 13:16:03 -08001127 } else {
1128 if (!initialize_properties_from_file("/plat_property_contexts")) {
1129 return false;
1130 }
Sandeep Patil03ed4af2017-05-17 11:33:37 -07001131 initialize_properties_from_file("/nonplat_property_contexts");
Elliott Hughes9160ed92017-01-30 17:54:57 -08001132 }
1133
Elliott Hughes9160ed92017-01-30 17:54:57 -08001134 return true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001135}
1136
1137static bool is_dir(const char* pathname) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001138 struct stat info;
1139 if (stat(pathname, &info) == -1) {
1140 return false;
1141 }
1142 return S_ISDIR(info.st_mode);
Tom Cherry49a309f2015-09-23 16:09:47 -07001143}
1144
Tom Cherryb4171692015-12-09 15:48:15 -08001145static void free_and_unmap_contexts() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001146 list_free(&prefixes);
1147 list_free(&contexts);
1148 if (__system_property_area__) {
1149 munmap(__system_property_area__, pa_size);
1150 __system_property_area__ = nullptr;
1151 }
Tom Cherryb4171692015-12-09 15:48:15 -08001152}
1153
dimitryfa432522017-10-25 13:07:45 +02001154__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001155int __system_properties_init() {
Elliott Hughes7ade61c2017-04-11 13:38:36 -07001156 // This is called from __libc_init_common, and should leave errno at 0 (http://b/37248982).
1157 ErrnoRestorer errno_restorer;
1158
Elliott Hughes9160ed92017-01-30 17:54:57 -08001159 if (initialized) {
1160 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001161 return 0;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001162 }
1163 if (is_dir(property_filename)) {
Tom Cherry49a309f2015-09-23 16:09:47 -07001164 if (!initialize_properties()) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001165 return -1;
Tom Cherry49a309f2015-09-23 16:09:47 -07001166 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001167 if (!map_system_property_area(false, nullptr)) {
1168 free_and_unmap_contexts();
1169 return -1;
Tom Cherry49a309f2015-09-23 16:09:47 -07001170 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001171 } else {
1172 __system_property_area__ = map_prop_area(property_filename);
Tom Cherry6ed51c02015-12-04 11:34:42 -08001173 if (!__system_property_area__) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001174 return -1;
Tom Cherry6ed51c02015-12-04 11:34:42 -08001175 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001176 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1177 list_add_after_len(&prefixes, "*", contexts);
1178 }
1179 initialized = true;
1180 return 0;
1181}
Tom Cherry6ed51c02015-12-04 11:34:42 -08001182
dimitryfa432522017-10-25 13:07:45 +02001183__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001184int __system_property_set_filename(const char* filename) {
1185 size_t len = strlen(filename);
1186 if (len >= sizeof(property_filename)) return -1;
1187
1188 strcpy(property_filename, filename);
1189 return 0;
1190}
1191
dimitryfa432522017-10-25 13:07:45 +02001192__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001193int __system_property_area_init() {
1194 free_and_unmap_contexts();
1195 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
1196 if (!initialize_properties()) {
1197 return -1;
1198 }
1199 bool open_failed = false;
1200 bool fsetxattr_failed = false;
1201 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1202 if (!l->open(true, &fsetxattr_failed)) {
1203 open_failed = true;
Tom Cherry926ebe12015-09-23 15:34:40 -07001204 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001205 });
1206 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1207 free_and_unmap_contexts();
1208 return -1;
1209 }
1210 initialized = true;
1211 return fsetxattr_failed ? -2 : 0;
1212}
Tom Cherry926ebe12015-09-23 15:34:40 -07001213
dimitryfa432522017-10-25 13:07:45 +02001214__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001215uint32_t __system_property_area_serial() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001216 prop_area* pa = __system_property_area__;
1217 if (!pa) {
1218 return -1;
1219 }
1220 // Make sure this read fulfilled before __system_property_serial
1221 return atomic_load_explicit(pa->serial(), memory_order_acquire);
1222}
1223
dimitryfa432522017-10-25 13:07:45 +02001224__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001225const prop_info* __system_property_find(const char* name) {
1226 if (!__system_property_area__) {
1227 return nullptr;
1228 }
1229
1230 prop_area* pa = get_prop_area_for_name(name);
1231 if (!pa) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001232 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001233 return nullptr;
1234 }
1235
1236 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001237}
1238
Tom Cherry8a311632017-10-09 13:49:17 -07001239static bool is_read_only(const char* name) {
1240 return strncmp(name, "ro.", 3) == 0;
Hans Boehm1e8587a2014-08-19 14:07:55 -07001241}
1242
dimitryfa432522017-10-25 13:07:45 +02001243__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001244int __system_property_read(const prop_info* pi, char* name, char* value) {
1245 while (true) {
1246 uint32_t serial = __system_property_serial(pi); // acquire semantics
1247 size_t len = SERIAL_VALUE_LEN(serial);
1248 memcpy(value, pi->value, len + 1);
1249 // TODO: Fix the synchronization scheme here.
1250 // There is no fully supported way to implement this kind
1251 // of synchronization in C++11, since the memcpy races with
1252 // updates to pi, and the data being accessed is not atomic.
1253 // The following fence is unintuitive, but would be the
1254 // correct one if memcpy used memory_order_relaxed atomic accesses.
1255 // In practice it seems unlikely that the generated code would
1256 // would be any different, so this should be OK.
1257 atomic_thread_fence(memory_order_acquire);
1258 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1259 if (name != nullptr) {
1260 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1261 if (namelen >= PROP_NAME_MAX) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001262 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
1263 "The property name length for \"%s\" is >= %d;"
1264 " please use __system_property_read_callback"
1265 " to read this property. (the name is truncated to \"%s\")",
1266 pi->name, PROP_NAME_MAX - 1, name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001267 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001268 }
Tom Cherry8a311632017-10-09 13:49:17 -07001269 if (is_read_only(pi->name) && pi->is_long()) {
1270 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
1271 "The property \"%s\" has a value with length %zu that is too large for"
1272 " __system_property_get()/__system_property_read(); use"
1273 " __system_property_read_callback() instead.",
1274 pi->name, strlen(pi->long_value()));
1275 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001276 return len;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001277 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001278 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001279}
1280
dimitryfa432522017-10-25 13:07:45 +02001281__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001282void __system_property_read_callback(const prop_info* pi,
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001283 void (*callback)(void* cookie,
1284 const char* name,
1285 const char* value,
1286 uint32_t serial),
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001287 void* cookie) {
Tom Cherry8a311632017-10-09 13:49:17 -07001288 // Read only properties don't need to copy the value to a temporary buffer, since it can never
1289 // change.
1290 if (is_read_only(pi->name)) {
1291 uint32_t serial = __system_property_serial(pi);
1292 if (pi->is_long()) {
1293 callback(cookie, pi->name, pi->long_value(), serial);
1294 } else {
1295 callback(cookie, pi->name, pi->value, serial);
1296 }
1297 return;
1298 }
1299
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001300 while (true) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001301 uint32_t serial = __system_property_serial(pi); // acquire semantics
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001302 size_t len = SERIAL_VALUE_LEN(serial);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001303 char value_buf[len + 1];
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001304
1305 memcpy(value_buf, pi->value, len);
1306 value_buf[len] = '\0';
1307
1308 // TODO: see todo in __system_property_read function
1309 atomic_thread_fence(memory_order_acquire);
1310 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001311 callback(cookie, pi->name, value_buf, serial);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001312 return;
1313 }
1314 }
1315}
1316
dimitryfa432522017-10-25 13:07:45 +02001317__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001318int __system_property_get(const char* name, char* value) {
1319 const prop_info* pi = __system_property_find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001320
Elliott Hughes9160ed92017-01-30 17:54:57 -08001321 if (pi != 0) {
1322 return __system_property_read(pi, nullptr, value);
1323 } else {
1324 value[0] = 0;
1325 return 0;
1326 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001327}
1328
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001329static constexpr uint32_t kProtocolVersion1 = 1;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001330static constexpr uint32_t kProtocolVersion2 = 2; // current
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001331
1332static atomic_uint_least32_t g_propservice_protocol_version = 0;
1333
1334static void detect_protocol_version() {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001335 char value[PROP_VALUE_MAX];
1336 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1337 g_propservice_protocol_version = kProtocolVersion1;
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001338 async_safe_format_log(ANDROID_LOG_WARN, "libc",
1339 "Using old property service protocol (\"%s\" is not set)",
1340 kServiceVersionPropertyName);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001341 } else {
1342 uint32_t version = static_cast<uint32_t>(atoll(value));
1343 if (version >= kProtocolVersion2) {
1344 g_propservice_protocol_version = kProtocolVersion2;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001345 } else {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001346 async_safe_format_log(ANDROID_LOG_WARN, "libc",
1347 "Using old property service protocol (\"%s\"=\"%s\")",
1348 kServiceVersionPropertyName, value);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001349 g_propservice_protocol_version = kProtocolVersion1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001350 }
Elliott Hughes9160ed92017-01-30 17:54:57 -08001351 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001352}
1353
dimitryfa432522017-10-25 13:07:45 +02001354__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001355int __system_property_set(const char* key, const char* value) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001356 if (key == nullptr) return -1;
1357 if (value == nullptr) value = "";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001358
Elliott Hughes9160ed92017-01-30 17:54:57 -08001359 if (g_propservice_protocol_version == 0) {
1360 detect_protocol_version();
1361 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001362
Elliott Hughes9160ed92017-01-30 17:54:57 -08001363 if (g_propservice_protocol_version == kProtocolVersion1) {
Tom Cherry8a311632017-10-09 13:49:17 -07001364 // Old protocol does not support long names or values
Elliott Hughes9160ed92017-01-30 17:54:57 -08001365 if (strlen(key) >= PROP_NAME_MAX) return -1;
Tom Cherry8a311632017-10-09 13:49:17 -07001366 if (strlen(value) >= PROP_VALUE_MAX) return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001367
Elliott Hughes9160ed92017-01-30 17:54:57 -08001368 prop_msg msg;
1369 memset(&msg, 0, sizeof msg);
1370 msg.cmd = PROP_MSG_SETPROP;
1371 strlcpy(msg.name, key, sizeof msg.name);
1372 strlcpy(msg.value, value, sizeof msg.value);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001373
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001374 return send_prop_msg(&msg);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001375 } else {
Tom Cherry8a311632017-10-09 13:49:17 -07001376 // New protocol only allows long values for ro. properties only.
1377 if (strlen(value) >= PROP_VALUE_MAX && !is_read_only(key)) return -1;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001378 // Use proper protocol
1379 PropertyServiceConnection connection;
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001380 if (!connection.IsValid()) {
1381 errno = connection.GetLastError();
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001382 async_safe_format_log(ANDROID_LOG_WARN,
1383 "libc",
1384 "Unable to set property \"%s\" to \"%s\": connection failed; errno=%d (%s)",
1385 key,
1386 value,
1387 errno,
1388 strerror(errno));
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001389 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001390 }
1391
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001392 SocketWriter writer(&connection);
1393 if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) {
1394 errno = connection.GetLastError();
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001395 async_safe_format_log(ANDROID_LOG_WARN,
1396 "libc",
1397 "Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)",
1398 key,
1399 value,
1400 errno,
1401 strerror(errno));
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001402 return -1;
1403 }
1404
1405 int result = -1;
1406 if (!connection.RecvInt32(&result)) {
1407 errno = connection.GetLastError();
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001408 async_safe_format_log(ANDROID_LOG_WARN,
1409 "libc",
1410 "Unable to set property \"%s\" to \"%s\": recv failed; errno=%d (%s)",
1411 key,
1412 value,
1413 errno,
1414 strerror(errno));
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001415 return -1;
1416 }
1417
1418 if (result != PROP_SUCCESS) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001419 async_safe_format_log(ANDROID_LOG_WARN,
1420 "libc",
1421 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1422 key,
1423 value,
1424 result);
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -08001425 return -1;
1426 }
1427
1428 return 0;
1429 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001430}
1431
dimitryfa432522017-10-25 13:07:45 +02001432__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001433int __system_property_update(prop_info* pi, const char* value, unsigned int len) {
1434 if (len >= PROP_VALUE_MAX) {
1435 return -1;
1436 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001437
Elliott Hughes9160ed92017-01-30 17:54:57 -08001438 prop_area* pa = __system_property_area__;
Tom Cherry6ed51c02015-12-04 11:34:42 -08001439
Elliott Hughes9160ed92017-01-30 17:54:57 -08001440 if (!pa) {
1441 return -1;
1442 }
Tom Cherry6ed51c02015-12-04 11:34:42 -08001443
Elliott Hughes9160ed92017-01-30 17:54:57 -08001444 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1445 serial |= 1;
1446 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1447 // The memcpy call here also races. Again pretend it
1448 // used memory_order_relaxed atomics, and use the analogous
1449 // counterintuitive fence.
1450 atomic_thread_fence(memory_order_release);
1451 strlcpy(pi->value, value, len + 1);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001452
Elliott Hughes9160ed92017-01-30 17:54:57 -08001453 atomic_store_explicit(&pi->serial, (len << 24) | ((serial + 1) & 0xffffff), memory_order_release);
1454 __futex_wake(&pi->serial, INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001455
Elliott Hughes9160ed92017-01-30 17:54:57 -08001456 atomic_store_explicit(pa->serial(), atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
1457 memory_order_release);
1458 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001459
Elliott Hughes9160ed92017-01-30 17:54:57 -08001460 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001461}
jiaguo879d3302014-03-13 17:39:58 +08001462
dimitryfa432522017-10-25 13:07:45 +02001463__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001464int __system_property_add(const char* name, unsigned int namelen, const char* value,
1465 unsigned int valuelen) {
Tom Cherry8a311632017-10-09 13:49:17 -07001466 if (valuelen >= PROP_VALUE_MAX && !is_read_only(name)) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001467 return -1;
1468 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001469
Elliott Hughes9160ed92017-01-30 17:54:57 -08001470 if (namelen < 1) {
1471 return -1;
1472 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001473
Elliott Hughes9160ed92017-01-30 17:54:57 -08001474 if (!__system_property_area__) {
1475 return -1;
1476 }
Tom Cherry6ed51c02015-12-04 11:34:42 -08001477
Elliott Hughes9160ed92017-01-30 17:54:57 -08001478 prop_area* pa = get_prop_area_for_name(name);
Tom Cherry49a309f2015-09-23 16:09:47 -07001479
Elliott Hughes9160ed92017-01-30 17:54:57 -08001480 if (!pa) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001481 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001482 return -1;
1483 }
Tom Cherry926ebe12015-09-23 15:34:40 -07001484
Elliott Hughes9160ed92017-01-30 17:54:57 -08001485 bool ret = pa->add(name, namelen, value, valuelen);
1486 if (!ret) {
1487 return -1;
1488 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001489
Elliott Hughes9160ed92017-01-30 17:54:57 -08001490 // There is only a single mutator, but we want to make sure that
1491 // updates are visible to a reader waiting for the update.
1492 atomic_store_explicit(
1493 __system_property_area__->serial(),
1494 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
1495 memory_order_release);
1496 __futex_wake(__system_property_area__->serial(), INT32_MAX);
1497 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001498}
1499
Hans Boehm30214b92014-07-31 15:53:22 -07001500// Wait for non-locked serial, and retrieve it with acquire semantics.
dimitryfa432522017-10-25 13:07:45 +02001501__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001502uint32_t __system_property_serial(const prop_info* pi) {
Elliott Hughes9160ed92017-01-30 17:54:57 -08001503 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
1504 while (SERIAL_DIRTY(serial)) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001505 __futex_wait(const_cast<_Atomic(uint_least32_t)*>(&pi->serial), serial, nullptr);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001506 serial = load_const_atomic(&pi->serial, memory_order_acquire);
1507 }
1508 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001509}
1510
dimitryfa432522017-10-25 13:07:45 +02001511__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001512uint32_t __system_property_wait_any(uint32_t old_serial) {
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001513 uint32_t new_serial;
Elliott Hughes40c885a2017-02-16 17:13:04 -08001514 __system_property_wait(nullptr, old_serial, &new_serial, nullptr);
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001515 return new_serial;
1516}
Elliott Hughes9160ed92017-01-30 17:54:57 -08001517
dimitryfa432522017-10-25 13:07:45 +02001518__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes40c885a2017-02-16 17:13:04 -08001519bool __system_property_wait(const prop_info* pi,
1520 uint32_t old_serial,
1521 uint32_t* new_serial_ptr,
1522 const timespec* relative_timeout) {
1523 // Are we waiting on the global serial or a specific serial?
1524 atomic_uint_least32_t* serial_ptr;
1525 if (pi == nullptr) {
1526 if (__system_property_area__ == nullptr) return -1;
1527 serial_ptr = __system_property_area__->serial();
1528 } else {
1529 serial_ptr = const_cast<atomic_uint_least32_t*>(&pi->serial);
1530 }
1531
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001532 uint32_t new_serial;
1533 do {
Elliott Hughes40c885a2017-02-16 17:13:04 -08001534 int rc;
1535 if ((rc = __futex_wait(serial_ptr, old_serial, relative_timeout)) != 0 && rc == -ETIMEDOUT) {
1536 return false;
1537 }
1538 new_serial = load_const_atomic(serial_ptr, memory_order_acquire);
Elliott Hughesa0d374d2017-02-10 18:13:46 -08001539 } while (new_serial == old_serial);
Elliott Hughes40c885a2017-02-16 17:13:04 -08001540
1541 *new_serial_ptr = new_serial;
1542 return true;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001543}
1544
dimitryfa432522017-10-25 13:07:45 +02001545__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001546const prop_info* __system_property_find_nth(unsigned n) {
Elliott Hughes438e0192017-04-17 14:53:07 -07001547 struct find_nth {
1548 const uint32_t sought;
1549 uint32_t current;
1550 const prop_info* result;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001551
Elliott Hughes438e0192017-04-17 14:53:07 -07001552 explicit find_nth(uint32_t n) : sought(n), current(0), result(nullptr) {}
1553 static void fn(const prop_info* pi, void* ptr) {
1554 find_nth* self = reinterpret_cast<find_nth*>(ptr);
1555 if (self->current++ == self->sought) self->result = pi;
1556 }
1557 } state(n);
1558 __system_property_foreach(find_nth::fn, &state);
1559 return state.result;
Elliott Hughes9160ed92017-01-30 17:54:57 -08001560}
1561
dimitryfa432522017-10-25 13:07:45 +02001562__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes9160ed92017-01-30 17:54:57 -08001563int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
1564 if (!__system_property_area__) {
1565 return -1;
1566 }
1567
1568 list_foreach(contexts, [propfn, cookie](context_node* l) {
1569 if (l->check_access_and_open()) {
Elliott Hughes438e0192017-04-17 14:53:07 -07001570 l->pa()->foreach(propfn, cookie);
Elliott Hughes9160ed92017-01-30 17:54:57 -08001571 }
1572 });
1573 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001574}