blob: 1d26a13bd5dcc510badc571c1f4779c3fc268b75 [file] [log] [blame]
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Tom Cherry49a309f2015-09-23 16:09:47 -070028#include <ctype.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000029#include <errno.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000030#include <fcntl.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070031#include <poll.h>
32#include <stdatomic.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000033#include <stdbool.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070034#include <stddef.h>
35#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000038#include <string.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070039#include <unistd.h>
40#include <new>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000041
Tom Cherry49a309f2015-09-23 16:09:47 -070042#include <linux/xattr.h>
43#include <netinet/in.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000044#include <sys/mman.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000045#include <sys/select.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070046#include <sys/socket.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000047#include <sys/stat.h>
48#include <sys/types.h>
Tom Cherry49a309f2015-09-23 16:09:47 -070049#include <sys/un.h>
50#include <sys/xattr.h>
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000051
52#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
53#include <sys/_system_properties.h>
54#include <sys/system_properties.h>
55
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070056#include "private/bionic_futex.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070057#include "private/bionic_lock.h"
Elliott Hughes8eac9af2014-05-09 19:12:08 -070058#include "private/bionic_macros.h"
Dimitry Ivanov581b9f62017-01-09 11:05:52 -080059#include "private/bionic_sdk_version.h"
Tom Cherry49a309f2015-09-23 16:09:47 -070060#include "private/libc_logging.h"
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000061
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000062#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
Dimitry Ivanov489f58b2017-01-24 18:39:04 +000063
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000064static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
65static const char* kServiceVersionPropertyName = "ro.property_service.version";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000066
67/*
68 * Properties are stored in a hybrid trie/binary tree structure.
69 * Each property's name is delimited at '.' characters, and the tokens are put
70 * into a trie structure. Siblings at each level of the trie are stored in a
71 * binary tree. For instance, "ro.secure"="1" could be stored as follows:
72 *
73 * +-----+ children +----+ children +--------+
74 * | |-------------->| ro |-------------->| secure |
75 * +-----+ +----+ +--------+
76 * / \ / |
77 * left / \ right left / | prop +===========+
78 * v v v +-------->| ro.secure |
79 * +-----+ +-----+ +-----+ +-----------+
80 * | net | | sys | | com | | 1 |
81 * +-----+ +-----+ +-----+ +===========+
82 */
83
84// Represents a node in the trie.
85struct prop_bt {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000086 uint32_t namelen;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +000087
Yabin Cuib8ce4742015-02-10 21:35:56 -080088 // The property trie is updated only by the init process (single threaded) which provides
89 // property service. And it can be read by multiple threads at the same time.
90 // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the
91 // left, right, children "pointers" in the trie node. To make sure readers who see the
92 // change of "pointers" can also notice the change of prop_bt structure contents pointed by
93 // the "pointers", we always use release-consume ordering pair when accessing these "pointers".
94
95 // prop "points" to prop_info structure if there is a propery associated with the trie node.
96 // Its situation is similar to the left, right, children "pointers". So we use
97 // atomic_uint_least32_t and release-consume ordering to protect it as well.
98
Hans Boehm30214b92014-07-31 15:53:22 -070099 // We should also avoid rereading these fields redundantly, since not
100 // all processor implementations ensure that multiple loads from the
101 // same field are carried out in the right order.
Yabin Cuib8ce4742015-02-10 21:35:56 -0800102 atomic_uint_least32_t prop;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000103
Yabin Cuib8ce4742015-02-10 21:35:56 -0800104 atomic_uint_least32_t left;
105 atomic_uint_least32_t right;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000106
Yabin Cuib8ce4742015-02-10 21:35:56 -0800107 atomic_uint_least32_t children;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000108
109 char name[0];
110
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000111 prop_bt(const char *name, const uint32_t name_length) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000112 this->namelen = name_length;
113 memcpy(this->name, name, name_length);
114 this->name[name_length] = '\0';
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000115 }
116
117private:
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700118 DISALLOW_COPY_AND_ASSIGN(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000119};
120
Tom Cherry926ebe12015-09-23 15:34:40 -0700121class prop_area {
122public:
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000123
124 prop_area(const uint32_t magic, const uint32_t version) :
Tom Cherry926ebe12015-09-23 15:34:40 -0700125 magic_(magic), version_(version) {
126 atomic_init(&serial_, 0);
127 memset(reserved_, 0, sizeof(reserved_));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000128 // Allocate enough space for the root node.
Tom Cherry926ebe12015-09-23 15:34:40 -0700129 bytes_used_ = sizeof(prop_bt);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000130 }
131
Tom Cherry926ebe12015-09-23 15:34:40 -0700132 const prop_info *find(const char *name);
133 bool add(const char *name, unsigned int namelen,
134 const char *value, unsigned int valuelen);
135
136 bool foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie);
137
138 atomic_uint_least32_t *serial() { return &serial_; }
139 uint32_t magic() const { return magic_; }
140 uint32_t version() const { return version_; }
141
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000142private:
Tom Cherry926ebe12015-09-23 15:34:40 -0700143 void *allocate_obj(const size_t size, uint_least32_t *const off);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000144 prop_bt *new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off);
145 prop_info *new_prop_info(const char *name, uint32_t namelen,
146 const char *value, uint32_t valuelen,
Tom Cherry926ebe12015-09-23 15:34:40 -0700147 uint_least32_t *const off);
148 void *to_prop_obj(uint_least32_t off);
149 prop_bt *to_prop_bt(atomic_uint_least32_t *off_p);
150 prop_info *to_prop_info(atomic_uint_least32_t *off_p);
151
152 prop_bt *root_node();
153
154 prop_bt *find_prop_bt(prop_bt *const bt, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000155 uint32_t namelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700156
157 const prop_info *find_property(prop_bt *const trie, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000158 uint32_t namelen, const char *value,
159 uint32_t valuelen, bool alloc_if_needed);
Tom Cherry926ebe12015-09-23 15:34:40 -0700160
161 bool foreach_property(prop_bt *const trie,
162 void (*propfn)(const prop_info *pi, void *cookie),
163 void *cookie);
164
165 uint32_t bytes_used_;
166 atomic_uint_least32_t serial_;
167 uint32_t magic_;
168 uint32_t version_;
169 uint32_t reserved_[28];
170 char data_[0];
171
Elliott Hughes8eac9af2014-05-09 19:12:08 -0700172 DISALLOW_COPY_AND_ASSIGN(prop_area);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000173};
174
175struct prop_info {
Hans Boehm30214b92014-07-31 15:53:22 -0700176 atomic_uint_least32_t serial;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000177 // we need to keep this buffer around because the property
178 // value can be modified whereas name is constant.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000179 char value[PROP_VALUE_MAX];
180 char name[0];
181
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000182 prop_info(const char *name, uint32_t namelen, const char *value, uint32_t valuelen) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000183 memcpy(this->name, name, namelen);
184 this->name[namelen] = '\0';
Hans Boehm30214b92014-07-31 15:53:22 -0700185 atomic_init(&this->serial, valuelen << 24);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000186 memcpy(this->value, value, valuelen);
187 this->value[valuelen] = '\0';
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000188 }
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000189 private:
190 DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000191};
192
193struct find_nth_cookie {
194 uint32_t count;
195 const uint32_t n;
196 const prop_info *pi;
197
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700198 explicit find_nth_cookie(uint32_t n) : count(0), n(n), pi(NULL) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000199 }
200};
201
Tom Cherry49a309f2015-09-23 16:09:47 -0700202static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000203static bool compat_mode = false;
204static size_t pa_data_size;
205static size_t pa_size;
Tom Cherryb4171692015-12-09 15:48:15 -0800206static bool initialized = false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000207
208// NOTE: This isn't static because system_properties_compat.c
209// requires it.
210prop_area *__system_property_area__ = NULL;
211
212static int get_fd_from_env(void)
213{
214 // This environment variable consistes of two decimal integer
215 // values separated by a ",". The first value is a file descriptor
216 // and the second is the size of the system properties area. The
217 // size is currently unused.
218 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
219
220 if (!env) {
221 return -1;
222 }
223
224 return atoi(env);
225}
226
Tom Cherry49a309f2015-09-23 16:09:47 -0700227static prop_area* map_prop_area_rw(const char* filename, const char* context,
228 bool* fsetxattr_failed) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000229 /* dev is a tmpfs that we can use to carve a shared workspace
230 * out of, so let's do that...
231 */
Tom Cherry49a309f2015-09-23 16:09:47 -0700232 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000233
234 if (fd < 0) {
235 if (errno == EACCES) {
236 /* for consistency with the case where the process has already
237 * mapped the page in and segfaults when trying to write to it
238 */
239 abort();
240 }
Tom Cherry49a309f2015-09-23 16:09:47 -0700241 return nullptr;
242 }
243
244 if (context) {
245 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
246 __libc_format_log(ANDROID_LOG_ERROR, "libc",
247 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
248 /*
249 * fsetxattr() will fail during system properties tests due to selinux policy.
250 * We do not want to create a custom policy for the tester, so we will continue in
251 * this function but set a flag that an error has occurred.
252 * Init, which is the only daemon that should ever call this function will abort
253 * when this error occurs.
254 * Otherwise, the tester will ignore it and continue, albeit without any selinux
255 * property separation.
256 */
257 if (fsetxattr_failed) {
258 *fsetxattr_failed = true;
259 }
260 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000261 }
262
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000263 if (ftruncate(fd, PA_SIZE) < 0) {
264 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700265 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000266 }
267
268 pa_size = PA_SIZE;
269 pa_data_size = pa_size - sizeof(prop_area);
270 compat_mode = false;
271
272 void *const memory_area = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
273 if (memory_area == MAP_FAILED) {
274 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700275 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000276 }
277
278 prop_area *pa = new(memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
279
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000280 close(fd);
Tom Cherry49a309f2015-09-23 16:09:47 -0700281 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000282}
283
Tom Cherry49a309f2015-09-23 16:09:47 -0700284static prop_area* map_fd_ro(const int fd) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000285 struct stat fd_stat;
286 if (fstat(fd, &fd_stat) < 0) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700287 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000288 }
289
290 if ((fd_stat.st_uid != 0)
291 || (fd_stat.st_gid != 0)
292 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
Narayan Kamath37e95702014-02-24 11:05:02 +0000293 || (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area))) ) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700294 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000295 }
296
297 pa_size = fd_stat.st_size;
298 pa_data_size = pa_size - sizeof(prop_area);
299
300 void* const map_result = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0);
301 if (map_result == MAP_FAILED) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700302 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000303 }
304
305 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
Tom Cherry926ebe12015-09-23 15:34:40 -0700306 if ((pa->magic() != PROP_AREA_MAGIC) ||
307 (pa->version() != PROP_AREA_VERSION &&
308 pa->version() != PROP_AREA_VERSION_COMPAT)) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000309 munmap(pa, pa_size);
Tom Cherry49a309f2015-09-23 16:09:47 -0700310 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000311 }
312
Tom Cherry926ebe12015-09-23 15:34:40 -0700313 if (pa->version() == PROP_AREA_VERSION_COMPAT) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000314 compat_mode = true;
315 }
316
Tom Cherry49a309f2015-09-23 16:09:47 -0700317 return pa;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000318}
319
Tom Cherry49a309f2015-09-23 16:09:47 -0700320static prop_area* map_prop_area(const char* filename, bool is_legacy) {
321 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000322 bool close_fd = true;
Tom Cherry49a309f2015-09-23 16:09:47 -0700323 if (fd == -1 && errno == ENOENT && is_legacy) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000324 /*
325 * For backwards compatibility, if the file doesn't
326 * exist, we use the environment to get the file descriptor.
327 * For security reasons, we only use this backup if the kernel
328 * returns ENOENT. We don't want to use the backup if the kernel
329 * returns other errors such as ENOMEM or ENFILE, since it
330 * might be possible for an external program to trigger this
331 * condition.
Tom Cherry49a309f2015-09-23 16:09:47 -0700332 * Only do this for the legacy prop file, secured prop files
333 * do not have a backup
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000334 */
335 fd = get_fd_from_env();
336 close_fd = false;
337 }
338
339 if (fd < 0) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700340 return nullptr;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000341 }
342
Tom Cherry49a309f2015-09-23 16:09:47 -0700343 prop_area* map_result = map_fd_ro(fd);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000344 if (close_fd) {
345 close(fd);
346 }
347
348 return map_result;
349}
350
Tom Cherry926ebe12015-09-23 15:34:40 -0700351void *prop_area::allocate_obj(const size_t size, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000352{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800353 const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t));
Tom Cherry926ebe12015-09-23 15:34:40 -0700354 if (bytes_used_ + aligned > pa_data_size) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000355 return NULL;
356 }
357
Tom Cherry926ebe12015-09-23 15:34:40 -0700358 *off = bytes_used_;
359 bytes_used_ += aligned;
360 return data_ + *off;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000361}
362
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000363prop_bt *prop_area::new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000364{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800365 uint_least32_t new_offset;
366 void *const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
367 if (p != NULL) {
368 prop_bt* bt = new(p) prop_bt(name, namelen);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000369 *off = new_offset;
370 return bt;
371 }
372
373 return NULL;
374}
375
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000376prop_info *prop_area::new_prop_info(const char *name, uint32_t namelen,
377 const char *value, uint32_t valuelen, uint_least32_t *const off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000378{
Yabin Cuib8ce4742015-02-10 21:35:56 -0800379 uint_least32_t new_offset;
380 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
381 if (p != NULL) {
382 prop_info* info = new(p) prop_info(name, namelen, value, valuelen);
383 *off = new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000384 return info;
385 }
386
387 return NULL;
388}
389
Tom Cherry926ebe12015-09-23 15:34:40 -0700390void *prop_area::to_prop_obj(uint_least32_t off)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000391{
392 if (off > pa_data_size)
393 return NULL;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000394
Tom Cherry926ebe12015-09-23 15:34:40 -0700395 return (data_ + off);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000396}
397
Tom Cherry926ebe12015-09-23 15:34:40 -0700398inline prop_bt *prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800399 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
400 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
401}
402
Tom Cherry926ebe12015-09-23 15:34:40 -0700403inline prop_info *prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800404 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
405 return reinterpret_cast<prop_info*>(to_prop_obj(off));
406}
407
Tom Cherry926ebe12015-09-23 15:34:40 -0700408inline prop_bt *prop_area::root_node()
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000409{
410 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
411}
412
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000413static int cmp_prop_name(const char *one, uint32_t one_len, const char *two,
414 uint32_t two_len)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000415{
416 if (one_len < two_len)
417 return -1;
418 else if (one_len > two_len)
419 return 1;
420 else
421 return strncmp(one, two, one_len);
422}
423
Tom Cherry926ebe12015-09-23 15:34:40 -0700424prop_bt *prop_area::find_prop_bt(prop_bt *const bt, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000425 uint32_t namelen, bool alloc_if_needed)
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000426{
427
428 prop_bt* current = bt;
429 while (true) {
430 if (!current) {
431 return NULL;
432 }
433
434 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
435 if (ret == 0) {
436 return current;
437 }
438
439 if (ret < 0) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800440 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
441 if (left_offset != 0) {
442 current = to_prop_bt(&current->left);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000443 } else {
444 if (!alloc_if_needed) {
445 return NULL;
446 }
447
Yabin Cuib8ce4742015-02-10 21:35:56 -0800448 uint_least32_t new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000449 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
450 if (new_bt) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800451 atomic_store_explicit(&current->left, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000452 }
453 return new_bt;
454 }
455 } else {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800456 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
457 if (right_offset != 0) {
458 current = to_prop_bt(&current->right);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000459 } else {
460 if (!alloc_if_needed) {
461 return NULL;
462 }
463
Yabin Cuib8ce4742015-02-10 21:35:56 -0800464 uint_least32_t new_offset;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000465 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
466 if (new_bt) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800467 atomic_store_explicit(&current->right, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000468 }
469 return new_bt;
470 }
471 }
472 }
473}
474
Tom Cherry926ebe12015-09-23 15:34:40 -0700475const prop_info *prop_area::find_property(prop_bt *const trie, const char *name,
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000476 uint32_t namelen, const char *value, uint32_t valuelen,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000477 bool alloc_if_needed)
478{
479 if (!trie) return NULL;
480
481 const char *remaining_name = name;
482 prop_bt* current = trie;
483 while (true) {
484 const char *sep = strchr(remaining_name, '.');
485 const bool want_subtree = (sep != NULL);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000486 const uint32_t substr_size = (want_subtree) ?
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000487 sep - remaining_name : strlen(remaining_name);
488
489 if (!substr_size) {
490 return NULL;
491 }
492
493 prop_bt* root = NULL;
Yabin Cuib8ce4742015-02-10 21:35:56 -0800494 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
495 if (children_offset != 0) {
496 root = to_prop_bt(&current->children);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000497 } else if (alloc_if_needed) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800498 uint_least32_t new_offset;
499 root = new_prop_bt(remaining_name, substr_size, &new_offset);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000500 if (root) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800501 atomic_store_explicit(&current->children, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000502 }
503 }
504
505 if (!root) {
506 return NULL;
507 }
508
509 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
510 if (!current) {
511 return NULL;
512 }
513
514 if (!want_subtree)
515 break;
516
517 remaining_name = sep + 1;
518 }
519
Yabin Cuib8ce4742015-02-10 21:35:56 -0800520 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
521 if (prop_offset != 0) {
522 return to_prop_info(&current->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000523 } else if (alloc_if_needed) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800524 uint_least32_t new_offset;
525 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000526 if (new_info) {
Yabin Cuib8ce4742015-02-10 21:35:56 -0800527 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000528 }
529
530 return new_info;
531 } else {
532 return NULL;
533 }
534}
535
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000536class PropertyServiceConnection {
537 public:
538 PropertyServiceConnection() : last_error_(0) {
539 fd_ = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
540 if (fd_ == -1) {
541 last_error_ = errno;
542 return;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000543 }
544
545 const size_t namelen = strlen(property_service_socket);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000546 sockaddr_un addr;
547 memset(&addr, 0, sizeof(addr));
548 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
549 addr.sun_family = AF_LOCAL;
550 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000551
552 if (TEMP_FAILURE_RETRY(connect(fd_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
553 close(fd_);
554 fd_ = -1;
555 last_error_ = errno;
556 }
557 }
558
559 bool IsValid() {
560 return fd_ != -1;
561 }
562
563 int GetLastError() {
564 return last_error_;
565 }
566
567 bool SendUint32(uint32_t value) {
568 int result = TEMP_FAILURE_RETRY(send(fd_, &value, sizeof(value), 0));
569 return CheckSendRecvResult(result, sizeof(value));
570 }
571
572 bool SendString(const char* value) {
573 uint32_t valuelen = strlen(value);
574 if (!SendUint32(valuelen)) {
575 return false;
Dimitry Ivanov5c1ce272015-12-03 11:26:38 -0800576 }
Dimitry Ivanov5c1ce272015-12-03 11:26:38 -0800577
Dimitry Ivanovcafd3552017-01-24 12:39:33 -0800578 // Trying to send even 0 bytes to closed socket may lead to
579 // broken pipe (http://b/34670529).
580 if (valuelen == 0) {
581 return true;
582 }
583
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000584 int result = TEMP_FAILURE_RETRY(send(fd_, value, valuelen, 0));
585 return CheckSendRecvResult(result, valuelen);
586 }
587
588 bool RecvInt32(int32_t* value) {
589 int result = TEMP_FAILURE_RETRY(recv(fd_, value, sizeof(*value), MSG_WAITALL));
590 return CheckSendRecvResult(result, sizeof(*value));
591 }
592
593 int GetFd() {
594 return fd_;
595 }
596
597 ~PropertyServiceConnection() {
598 if (fd_ != -1) {
599 close(fd_);
600 }
601 }
602 private:
603 bool CheckSendRecvResult(int result, int expected_len) {
604 if (result == -1) {
605 last_error_ = errno;
606 } else if (result != expected_len) {
607 last_error_ = -1;
608 } else {
609 last_error_ = 0;
610 }
611
612 return last_error_ == 0;
613 }
614
615 int fd_;
616 int last_error_;
617};
618
619static int send_prop_msg(const prop_msg* msg) {
620 PropertyServiceConnection connection;
621 if (!connection.IsValid()) {
622 return connection.GetLastError();
623 }
Dimitry Ivanov489f58b2017-01-24 18:39:04 +0000624
625 int result = -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000626 int fd = connection.GetFd();
627
628 const int num_bytes = TEMP_FAILURE_RETRY(send(fd, msg, sizeof(prop_msg), 0));
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000629 if (num_bytes == sizeof(prop_msg)) {
630 // We successfully wrote to the property server but now we
631 // wait for the property server to finish its work. It
632 // acknowledges its completion by closing the socket so we
633 // poll here (on nothing), waiting for the socket to close.
634 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
635 // once the socket closes. Out of paranoia we cap our poll
636 // at 250 ms.
637 pollfd pollfds[1];
638 pollfds[0].fd = fd;
639 pollfds[0].events = 0;
640 const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
641 if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
642 result = 0;
643 } else {
644 // Ignore the timeout and treat it like a success anyway.
645 // The init process is single-threaded and its property
646 // service is sometimes slow to respond (perhaps it's off
647 // starting a child process or something) and thus this
648 // times out and the caller thinks it failed, even though
649 // it's still getting around to it. So we fake it here,
650 // mostly for ctl.* properties, but we do try and wait 250
651 // ms so callers who do read-after-write can reliably see
652 // what they've written. Most of the time.
653 // TODO: fix the system properties design.
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000654 __libc_format_log(ANDROID_LOG_WARN, "libc",
655 "Property service has timed out while trying to set \"%s\" to \"%s\"",
656 msg->name, msg->value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000657 result = 0;
658 }
659 }
660
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000661 return result;
662}
663
664static void find_nth_fn(const prop_info *pi, void *ptr)
665{
666 find_nth_cookie *cookie = reinterpret_cast<find_nth_cookie*>(ptr);
667
668 if (cookie->n == cookie->count)
669 cookie->pi = pi;
670
671 cookie->count++;
672}
673
Tom Cherry926ebe12015-09-23 15:34:40 -0700674bool prop_area::foreach_property(prop_bt *const trie,
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000675 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
676{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000677 if (!trie)
Tom Cherry926ebe12015-09-23 15:34:40 -0700678 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000679
Yabin Cuib8ce4742015-02-10 21:35:56 -0800680 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
681 if (left_offset != 0) {
682 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000683 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700684 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000685 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800686 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
687 if (prop_offset != 0) {
688 prop_info *info = to_prop_info(&trie->prop);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000689 if (!info)
Tom Cherry926ebe12015-09-23 15:34:40 -0700690 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000691 propfn(info, cookie);
692 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800693 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
694 if (children_offset != 0) {
695 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000696 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700697 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000698 }
Yabin Cuib8ce4742015-02-10 21:35:56 -0800699 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
700 if (right_offset != 0) {
701 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000702 if (err < 0)
Tom Cherry926ebe12015-09-23 15:34:40 -0700703 return false;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000704 }
705
Tom Cherry926ebe12015-09-23 15:34:40 -0700706 return true;
707}
708
709const prop_info *prop_area::find(const char *name) {
710 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
711}
712
713bool prop_area::add(const char *name, unsigned int namelen,
714 const char *value, unsigned int valuelen) {
715 return find_property(root_node(), name, namelen, value, valuelen, true);
716}
717
718bool prop_area::foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
719 return foreach_property(root_node(), propfn, cookie);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +0000720}
721
Tom Cherryb4171692015-12-09 15:48:15 -0800722class context_node {
723public:
724 context_node(context_node* next, const char* context, prop_area* pa)
725 : next(next), context_(strdup(context)), pa_(pa), no_access_(false) {
726 lock_.init(false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700727 }
728 ~context_node() {
Tom Cherryb4171692015-12-09 15:48:15 -0800729 unmap();
730 free(context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700731 }
Tom Cherryb4171692015-12-09 15:48:15 -0800732 bool open(bool access_rw, bool* fsetxattr_failed);
733 bool check_access_and_open();
734 void reset_access();
735
736 const char* context() const { return context_; }
737 prop_area* pa() { return pa_; }
738
739 context_node* next;
740
741private:
742 bool check_access();
743 void unmap();
744
745 Lock lock_;
746 char* context_;
747 prop_area* pa_;
748 bool no_access_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700749};
750
751struct prefix_node {
752 prefix_node(struct prefix_node* next, const char* prefix, context_node* context)
753 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
754 }
755 ~prefix_node() {
756 free(prefix);
757 }
758 char* prefix;
759 const size_t prefix_len;
760 context_node* context;
761 struct prefix_node* next;
762};
763
764template <typename List, typename... Args>
765static inline void list_add(List** list, Args... args) {
766 *list = new List(*list, args...);
767}
768
769static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) {
770 size_t prefix_len = strlen(prefix);
771
772 auto next_list = list;
773
774 while (*next_list) {
775 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
776 list_add(next_list, prefix, context);
777 return;
778 }
779 next_list = &(*next_list)->next;
780 }
781 list_add(next_list, prefix, context);
782}
783
784template <typename List, typename Func>
785static void list_foreach(List* list, Func func) {
786 while (list) {
787 func(list);
788 list = list->next;
789 }
790}
791
792template <typename List, typename Func>
793static List* list_find(List* list, Func func) {
794 while (list) {
795 if (func(list)) {
796 return list;
797 }
798 list = list->next;
799 }
800 return nullptr;
801}
802
803template <typename List>
804static void list_free(List** list) {
805 while (*list) {
806 auto old_list = *list;
807 *list = old_list->next;
808 delete old_list;
809 }
810}
811
812static prefix_node* prefixes = nullptr;
813static context_node* contexts = nullptr;
814
815/*
816 * pthread_mutex_lock() calls into system_properties in the case of contention.
817 * This creates a risk of dead lock if any system_properties functions
818 * use pthread locks after system_property initialization.
819 *
820 * For this reason, the below three functions use a bionic Lock and static
821 * allocation of memory for each filename.
822 */
823
Tom Cherryb4171692015-12-09 15:48:15 -0800824bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
825 lock_.lock();
826 if (pa_) {
827 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700828 return true;
829 }
830
831 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800832 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
833 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700834 if (len < 0 || len > PROP_FILENAME_MAX) {
Tom Cherryb4171692015-12-09 15:48:15 -0800835 lock_.unlock();
Tom Cherry49a309f2015-09-23 16:09:47 -0700836 return false;
837 }
838
839 if (access_rw) {
Tom Cherryb4171692015-12-09 15:48:15 -0800840 pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
Tom Cherry49a309f2015-09-23 16:09:47 -0700841 } else {
Tom Cherryb4171692015-12-09 15:48:15 -0800842 pa_ = map_prop_area(filename, false);
Tom Cherry49a309f2015-09-23 16:09:47 -0700843 }
Tom Cherryb4171692015-12-09 15:48:15 -0800844 lock_.unlock();
845 return pa_;
Tom Cherry49a309f2015-09-23 16:09:47 -0700846}
847
Tom Cherryb4171692015-12-09 15:48:15 -0800848bool context_node::check_access_and_open() {
849 if (!pa_ && !no_access_) {
850 if (!check_access() || !open(false, nullptr)) {
851 no_access_ = true;
852 }
853 }
854 return pa_;
855}
856
857void context_node::reset_access() {
858 if (!check_access()) {
859 unmap();
860 no_access_ = true;
861 } else {
862 no_access_ = false;
863 }
864}
865
866bool context_node::check_access() {
Tom Cherry49a309f2015-09-23 16:09:47 -0700867 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800868 int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
869 property_filename, context_);
Tom Cherry49a309f2015-09-23 16:09:47 -0700870 if (len < 0 || len > PROP_FILENAME_MAX) {
871 return false;
872 }
873
874 return access(filename, R_OK) == 0;
875}
876
Tom Cherryb4171692015-12-09 15:48:15 -0800877void context_node::unmap() {
878 if (!pa_) {
879 return;
880 }
881
882 munmap(pa_, pa_size);
883 if (pa_ == __system_property_area__) {
884 __system_property_area__ = nullptr;
885 }
886 pa_ = nullptr;
887}
888
Tom Cherry49a309f2015-09-23 16:09:47 -0700889static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
890 char filename[PROP_FILENAME_MAX];
Tom Cherry83524752016-01-26 15:27:07 -0800891 int len = __libc_format_buffer(filename, sizeof(filename),
892 "%s/properties_serial", property_filename);
Tom Cherry49a309f2015-09-23 16:09:47 -0700893 if (len < 0 || len > PROP_FILENAME_MAX) {
894 __system_property_area__ = nullptr;
895 return false;
896 }
897
898 if (access_rw) {
899 __system_property_area__ =
900 map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
901 } else {
902 __system_property_area__ = map_prop_area(filename, false);
903 }
904 return __system_property_area__;
905}
906
907static prop_area* get_prop_area_for_name(const char* name) {
Tom Cherry845e24a2015-12-03 15:38:52 -0800908 auto entry = list_find(prefixes, [name](prefix_node* l) {
Tom Cherry49a309f2015-09-23 16:09:47 -0700909 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
910 });
911 if (!entry) {
912 return nullptr;
913 }
914
915 auto cnode = entry->context;
Tom Cherryb4171692015-12-09 15:48:15 -0800916 if (!cnode->pa()) {
917 /*
918 * We explicitly do not check no_access_ in this case because unlike the
919 * case of foreach(), we want to generate an selinux audit for each
920 * non-permitted property access in this function.
921 */
922 cnode->open(false, nullptr);
Tom Cherry49a309f2015-09-23 16:09:47 -0700923 }
Tom Cherryb4171692015-12-09 15:48:15 -0800924 return cnode->pa();
Tom Cherry49a309f2015-09-23 16:09:47 -0700925}
926
927/*
928 * The below two functions are duplicated from label_support.c in libselinux.
929 * TODO: Find a location suitable for these functions such that both libc and
930 * libselinux can share a common source file.
931 */
932
933/*
934 * The read_spec_entries and read_spec_entry functions may be used to
935 * replace sscanf to read entries from spec files. The file and
936 * property services now use these.
937 */
938
939/* Read an entry from a spec file (e.g. file_contexts) */
940static inline int read_spec_entry(char **entry, char **ptr, int *len)
941{
942 *entry = NULL;
943 char *tmp_buf = NULL;
944
945 while (isspace(**ptr) && **ptr != '\0')
946 (*ptr)++;
947
948 tmp_buf = *ptr;
949 *len = 0;
950
951 while (!isspace(**ptr) && **ptr != '\0') {
952 (*ptr)++;
953 (*len)++;
954 }
955
956 if (*len) {
957 *entry = strndup(tmp_buf, *len);
958 if (!*entry)
959 return -1;
960 }
961
962 return 0;
963}
964
965/*
966 * line_buf - Buffer containing the spec entries .
967 * num_args - The number of spec parameter entries to process.
968 * ... - A 'char **spec_entry' for each parameter.
969 * returns - The number of items processed.
970 *
971 * This function calls read_spec_entry() to do the actual string processing.
972 */
973static int read_spec_entries(char *line_buf, int num_args, ...)
974{
975 char **spec_entry, *buf_p;
976 int len, rc, items, entry_len = 0;
977 va_list ap;
978
979 len = strlen(line_buf);
980 if (line_buf[len - 1] == '\n')
981 line_buf[len - 1] = '\0';
982 else
983 /* Handle case if line not \n terminated by bumping
984 * the len for the check below (as the line is NUL
985 * terminated by getline(3)) */
986 len++;
987
988 buf_p = line_buf;
989 while (isspace(*buf_p))
990 buf_p++;
991
992 /* Skip comment lines and empty lines. */
993 if (*buf_p == '#' || *buf_p == '\0')
994 return 0;
995
996 /* Process the spec file entries */
997 va_start(ap, num_args);
998
999 items = 0;
1000 while (items < num_args) {
1001 spec_entry = va_arg(ap, char **);
1002
1003 if (len - 1 == buf_p - line_buf) {
1004 va_end(ap);
1005 return items;
1006 }
1007
1008 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
1009 if (rc < 0) {
1010 va_end(ap);
1011 return rc;
1012 }
1013 if (entry_len)
1014 items++;
1015 }
1016 va_end(ap);
1017 return items;
1018}
1019
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001020static bool initialize_properties_from_file(const char *filename) {
1021 FILE* file = fopen(filename, "re");
Tom Cherry49a309f2015-09-23 16:09:47 -07001022 if (!file) {
1023 return false;
1024 }
1025
1026 char* buffer = nullptr;
1027 size_t line_len;
1028 char* prop_prefix = nullptr;
1029 char* context = nullptr;
1030
1031 while (getline(&buffer, &line_len, file) > 0) {
1032 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
1033 if (items <= 0) {
1034 continue;
1035 }
1036 if (items == 1) {
1037 free(prop_prefix);
1038 continue;
1039 }
Tom Cherry21eadee2015-12-04 15:53:25 -08001040 /*
1041 * init uses ctl.* properties as an IPC mechanism and does not write them
1042 * to a property file, therefore we do not need to create property files
1043 * to store them.
1044 */
1045 if (!strncmp(prop_prefix, "ctl.", 4)) {
1046 free(prop_prefix);
1047 free(context);
1048 continue;
1049 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001050
Tom Cherry845e24a2015-12-03 15:38:52 -08001051 auto old_context = list_find(
Tom Cherryb4171692015-12-09 15:48:15 -08001052 contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
Tom Cherry49a309f2015-09-23 16:09:47 -07001053 if (old_context) {
1054 list_add_after_len(&prefixes, prop_prefix, old_context);
1055 } else {
1056 list_add(&contexts, context, nullptr);
1057 list_add_after_len(&prefixes, prop_prefix, contexts);
1058 }
1059 free(prop_prefix);
1060 free(context);
1061 }
1062
1063 free(buffer);
1064 fclose(file);
Sandeep Patil34f0cfa2016-12-27 17:37:44 -08001065
1066 return true;
1067}
1068
1069static bool initialize_properties() {
1070 // If we do find /property_contexts, then this is being
1071 // run as part of the OTA updater on older release that had
1072 // /property_contexts - b/34370523
1073 if (initialize_properties_from_file("/property_contexts")) {
1074 return true;
1075 }
1076
1077 // TODO: Change path to /system/property_contexts after b/27805372
1078 if (!initialize_properties_from_file("/plat_property_contexts")) {
1079 return false;
1080 }
1081
1082 // TODO: Change path to /vendor/property_contexts after b/27805372
1083 // device-specific property context is optional, so load if it exists.
1084 initialize_properties_from_file("/nonplat_property_contexts");
1085
Tom Cherry49a309f2015-09-23 16:09:47 -07001086 return true;
1087}
1088
1089static bool is_dir(const char* pathname) {
1090 struct stat info;
1091 if (stat(pathname, &info) == -1) {
1092 return false;
1093 }
1094 return S_ISDIR(info.st_mode);
1095}
1096
Tom Cherryb4171692015-12-09 15:48:15 -08001097static void free_and_unmap_contexts() {
1098 list_free(&prefixes);
1099 list_free(&contexts);
1100 if (__system_property_area__) {
1101 munmap(__system_property_area__, pa_size);
1102 __system_property_area__ = nullptr;
1103 }
1104}
1105
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001106int __system_properties_init()
1107{
Tom Cherryb4171692015-12-09 15:48:15 -08001108 if (initialized) {
1109 list_foreach(contexts, [](context_node* l) { l->reset_access(); });
1110 return 0;
1111 }
Tom Cherry49a309f2015-09-23 16:09:47 -07001112 if (is_dir(property_filename)) {
1113 if (!initialize_properties()) {
1114 return -1;
1115 }
1116 if (!map_system_property_area(false, nullptr)) {
Tom Cherryb4171692015-12-09 15:48:15 -08001117 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001118 return -1;
1119 }
1120 } else {
1121 __system_property_area__ = map_prop_area(property_filename, true);
1122 if (!__system_property_area__) {
1123 return -1;
1124 }
1125 list_add(&contexts, "legacy_system_prop_area", __system_property_area__);
1126 list_add_after_len(&prefixes, "*", contexts);
1127 }
Tom Cherryb4171692015-12-09 15:48:15 -08001128 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001129 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001130}
1131
1132int __system_property_set_filename(const char *filename)
1133{
1134 size_t len = strlen(filename);
1135 if (len >= sizeof(property_filename))
1136 return -1;
1137
1138 strcpy(property_filename, filename);
1139 return 0;
1140}
1141
1142int __system_property_area_init()
1143{
Tom Cherryb4171692015-12-09 15:48:15 -08001144 free_and_unmap_contexts();
Nick Kralevichbb59d472017-01-06 12:04:29 -08001145 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
Tom Cherry49a309f2015-09-23 16:09:47 -07001146 if (!initialize_properties()) {
1147 return -1;
1148 }
Tom Cherryb4171692015-12-09 15:48:15 -08001149 bool open_failed = false;
Tom Cherry49a309f2015-09-23 16:09:47 -07001150 bool fsetxattr_failed = false;
Tom Cherryb4171692015-12-09 15:48:15 -08001151 list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) {
1152 if (!l->open(true, &fsetxattr_failed)) {
1153 open_failed = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001154 }
1155 });
Tom Cherryb4171692015-12-09 15:48:15 -08001156 if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) {
1157 free_and_unmap_contexts();
Tom Cherry49a309f2015-09-23 16:09:47 -07001158 return -1;
1159 }
Tom Cherryb4171692015-12-09 15:48:15 -08001160 initialized = true;
Tom Cherry49a309f2015-09-23 16:09:47 -07001161 return fsetxattr_failed ? -2 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001162}
1163
Mark Salyzynbfd65272015-04-24 09:31:32 -07001164unsigned int __system_property_area_serial()
1165{
1166 prop_area *pa = __system_property_area__;
1167 if (!pa) {
1168 return -1;
1169 }
1170 // Make sure this read fulfilled before __system_property_serial
Tom Cherry926ebe12015-09-23 15:34:40 -07001171 return atomic_load_explicit(pa->serial(), memory_order_acquire);
Mark Salyzynbfd65272015-04-24 09:31:32 -07001172}
1173
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001174const prop_info *__system_property_find(const char *name)
1175{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001176 if (!__system_property_area__) {
1177 return nullptr;
1178 }
1179
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001180 if (__predict_false(compat_mode)) {
1181 return __system_property_find_compat(name);
1182 }
Tom Cherry926ebe12015-09-23 15:34:40 -07001183
Tom Cherry49a309f2015-09-23 16:09:47 -07001184 prop_area* pa = get_prop_area_for_name(name);
1185 if (!pa) {
1186 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001187 return nullptr;
1188 }
1189
Tom Cherry49a309f2015-09-23 16:09:47 -07001190 return pa->find(name);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001191}
1192
Hans Boehm1e8587a2014-08-19 14:07:55 -07001193// The C11 standard doesn't allow atomic loads from const fields,
1194// though C++11 does. Fudge it until standards get straightened out.
1195static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s,
1196 memory_order mo) {
1197 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
1198 return atomic_load_explicit(non_const_s, mo);
1199}
1200
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001201int __system_property_read(const prop_info *pi, char *name, char *value)
1202{
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001203 if (__predict_false(compat_mode)) {
1204 return __system_property_read_compat(pi, name, value);
1205 }
1206
jiaguo879d3302014-03-13 17:39:58 +08001207 while (true) {
Hans Boehm30214b92014-07-31 15:53:22 -07001208 uint32_t serial = __system_property_serial(pi); // acquire semantics
jiaguo879d3302014-03-13 17:39:58 +08001209 size_t len = SERIAL_VALUE_LEN(serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001210 memcpy(value, pi->value, len + 1);
Hans Boehm30214b92014-07-31 15:53:22 -07001211 // TODO: Fix the synchronization scheme here.
1212 // There is no fully supported way to implement this kind
1213 // of synchronization in C++11, since the memcpy races with
1214 // updates to pi, and the data being accessed is not atomic.
1215 // The following fence is unintuitive, but would be the
1216 // correct one if memcpy used memory_order_relaxed atomic accesses.
1217 // In practice it seems unlikely that the generated code would
1218 // would be any different, so this should be OK.
1219 atomic_thread_fence(memory_order_acquire);
1220 if (serial ==
Hans Boehm1e8587a2014-08-19 14:07:55 -07001221 load_const_atomic(&(pi->serial), memory_order_relaxed)) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001222 if (name != nullptr) {
1223 size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
1224 if(namelen >= PROP_NAME_MAX) {
1225 __libc_format_log(ANDROID_LOG_ERROR, "libc",
1226 "The property name length for \"%s\" is >= %d;"
1227 " please use __system_property_read_callback"
1228 " to read this property. (the name is truncated to \"%s\")",
1229 pi->name, PROP_NAME_MAX - 1, name);
1230 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001231 }
1232 return len;
1233 }
1234 }
1235}
1236
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001237void __system_property_read_callback(const prop_info* pi,
1238 void (*callback)(void* cookie, const char* name, const char* value),
1239 void* cookie) {
1240 // TODO (dimitry): do we need compat mode for this function?
1241 if (__predict_false(compat_mode)) {
1242 char value_buf[PROP_VALUE_MAX];
1243 __system_property_read_compat(pi, nullptr, value_buf);
1244 callback(cookie, pi->name, value_buf);
1245 return;
1246 }
1247
1248 while (true) {
1249 uint32_t serial = __system_property_serial(pi); // acquire semantics
1250 size_t len = SERIAL_VALUE_LEN(serial);
1251 char value_buf[len+1];
1252
1253 memcpy(value_buf, pi->value, len);
1254 value_buf[len] = '\0';
1255
1256 // TODO: see todo in __system_property_read function
1257 atomic_thread_fence(memory_order_acquire);
1258 if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
1259 callback(cookie, pi->name, value_buf);
1260 return;
1261 }
1262 }
1263}
1264
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001265int __system_property_get(const char *name, char *value)
1266{
1267 const prop_info *pi = __system_property_find(name);
1268
1269 if (pi != 0) {
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001270 return __system_property_read(pi, nullptr, value);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001271 } else {
1272 value[0] = 0;
1273 return 0;
1274 }
1275}
1276
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001277static constexpr uint32_t kProtocolVersion1 = 1;
1278static constexpr uint32_t kProtocolVersion2 = 2; // current
1279
1280static atomic_uint_least32_t g_propservice_protocol_version = 0;
1281
1282static void detect_protocol_version() {
1283 char value[PROP_VALUE_MAX];
1284 if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
1285 g_propservice_protocol_version = kProtocolVersion1;
1286 __libc_format_log(ANDROID_LOG_WARN, "libc",
1287 "Using old property service protocol (\"%s\" is not set)",
1288 kServiceVersionPropertyName);
1289 } else {
1290 uint32_t version = static_cast<uint32_t>(atoll(value));
1291 if (version >= kProtocolVersion2) {
1292 g_propservice_protocol_version = kProtocolVersion2;
1293 } else {
1294 __libc_format_log(ANDROID_LOG_WARN, "libc",
1295 "Using old property service protocol (\"%s\"=\"%s\")",
1296 kServiceVersionPropertyName, value);
1297 g_propservice_protocol_version = kProtocolVersion1;
1298 }
1299 }
1300}
1301
1302int __system_property_set(const char* key, const char* value) {
1303 if (key == nullptr) return -1;
1304 if (value == nullptr) value = "";
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001305 if (strlen(value) >= PROP_VALUE_MAX) return -1;
1306
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001307 if (g_propservice_protocol_version == 0) {
1308 detect_protocol_version();
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001309 }
1310
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001311 int result = -1;
1312 if (g_propservice_protocol_version == kProtocolVersion1) {
1313 // Old protocol does not support long names
1314 if (strlen(key) >= PROP_NAME_MAX) return -1;
1315
1316 prop_msg msg;
1317 memset(&msg, 0, sizeof msg);
1318 msg.cmd = PROP_MSG_SETPROP;
1319 strlcpy(msg.name, key, sizeof msg.name);
1320 strlcpy(msg.value, value, sizeof msg.value);
1321
1322 result = send_prop_msg(&msg);
1323 } else {
1324 // Use proper protocol
1325 PropertyServiceConnection connection;
1326 if (connection.IsValid() &&
1327 connection.SendUint32(PROP_MSG_SETPROP2) &&
1328 connection.SendString(key) &&
1329 connection.SendString(value) &&
1330 connection.RecvInt32(&result)) {
1331 if (result != PROP_SUCCESS) {
1332 __libc_format_log(ANDROID_LOG_WARN, "libc",
1333 "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
1334 key, value, result);
1335 }
1336 } else {
1337 result = connection.GetLastError();
1338 __libc_format_log(ANDROID_LOG_WARN, "libc",
1339 "Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)",
1340 key, value, result, strerror(result));
1341 }
1342 }
1343
1344 return result != 0 ? -1 : 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001345}
1346
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001347int __system_property_update(prop_info *pi, const char *value, unsigned int len)
1348{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001349 if (len >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001350 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001351 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001352
Tom Cherry6ed51c02015-12-04 11:34:42 -08001353 prop_area* pa = __system_property_area__;
1354
1355 if (!pa) {
1356 return -1;
1357 }
1358
Hans Boehm30214b92014-07-31 15:53:22 -07001359 uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed);
1360 serial |= 1;
1361 atomic_store_explicit(&pi->serial, serial, memory_order_relaxed);
1362 // The memcpy call here also races. Again pretend it
1363 // used memory_order_relaxed atomics, and use the analogous
1364 // counterintuitive fence.
1365 atomic_thread_fence(memory_order_release);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001366 strlcpy(pi->value, value, len + 1);
1367
Hans Boehm30214b92014-07-31 15:53:22 -07001368 atomic_store_explicit(
1369 &pi->serial,
1370 (len << 24) | ((serial + 1) & 0xffffff),
1371 memory_order_release);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001372 __futex_wake(&pi->serial, INT32_MAX);
1373
Hans Boehm30214b92014-07-31 15:53:22 -07001374 atomic_store_explicit(
Tom Cherry926ebe12015-09-23 15:34:40 -07001375 pa->serial(),
1376 atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001377 memory_order_release);
Tom Cherry926ebe12015-09-23 15:34:40 -07001378 __futex_wake(pa->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001379
1380 return 0;
1381}
jiaguo879d3302014-03-13 17:39:58 +08001382
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001383int __system_property_add(const char *name, unsigned int namelen,
1384 const char *value, unsigned int valuelen)
1385{
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001386 if (valuelen >= PROP_VALUE_MAX) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001387 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001388 }
1389
1390 if (namelen < 1) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001391 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001392 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001393
Tom Cherry6ed51c02015-12-04 11:34:42 -08001394 if (!__system_property_area__) {
1395 return -1;
1396 }
1397
Tom Cherry49a309f2015-09-23 16:09:47 -07001398 prop_area* pa = get_prop_area_for_name(name);
1399
1400 if (!pa) {
1401 __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name);
Tom Cherry926ebe12015-09-23 15:34:40 -07001402 return -1;
1403 }
1404
Tom Cherry49a309f2015-09-23 16:09:47 -07001405 bool ret = pa->add(name, namelen, value, valuelen);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001406 if (!ret) {
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001407 return -1;
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +00001408 }
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001409
Hans Boehm30214b92014-07-31 15:53:22 -07001410 // There is only a single mutator, but we want to make sure that
1411 // updates are visible to a reader waiting for the update.
1412 atomic_store_explicit(
Tom Cherry49a309f2015-09-23 16:09:47 -07001413 __system_property_area__->serial(),
1414 atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1,
Hans Boehm30214b92014-07-31 15:53:22 -07001415 memory_order_release);
Tom Cherry49a309f2015-09-23 16:09:47 -07001416 __futex_wake(__system_property_area__->serial(), INT32_MAX);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001417 return 0;
1418}
1419
Hans Boehm30214b92014-07-31 15:53:22 -07001420// Wait for non-locked serial, and retrieve it with acquire semantics.
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001421unsigned int __system_property_serial(const prop_info *pi)
1422{
Hans Boehm1e8587a2014-08-19 14:07:55 -07001423 uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001424 while (SERIAL_DIRTY(serial)) {
Hans Boehm30214b92014-07-31 15:53:22 -07001425 __futex_wait(const_cast<volatile void *>(
1426 reinterpret_cast<const void *>(&pi->serial)),
1427 serial, NULL);
Hans Boehm1e8587a2014-08-19 14:07:55 -07001428 serial = load_const_atomic(&pi->serial, memory_order_acquire);
jiaguo879d3302014-03-13 17:39:58 +08001429 }
1430 return serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001431}
1432
1433unsigned int __system_property_wait_any(unsigned int serial)
1434{
1435 prop_area *pa = __system_property_area__;
Hans Boehm30214b92014-07-31 15:53:22 -07001436 uint32_t my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001437
Tom Cherry6ed51c02015-12-04 11:34:42 -08001438 if (!pa) {
1439 return 0;
1440 }
1441
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001442 do {
Tom Cherry926ebe12015-09-23 15:34:40 -07001443 __futex_wait(pa->serial(), serial, NULL);
1444 my_serial = atomic_load_explicit(pa->serial(), memory_order_acquire);
Hans Boehm30214b92014-07-31 15:53:22 -07001445 } while (my_serial == serial);
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001446
Hans Boehm30214b92014-07-31 15:53:22 -07001447 return my_serial;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001448}
1449
1450const prop_info *__system_property_find_nth(unsigned n)
1451{
Dimitry Ivanov581b9f62017-01-09 11:05:52 -08001452 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
1453 __libc_fatal("__system_property_find_nth is not supported since Android O,"
1454 " please use __system_property_foreach instead.");
1455 }
1456
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001457 find_nth_cookie cookie(n);
1458
1459 const int err = __system_property_foreach(find_nth_fn, &cookie);
1460 if (err < 0) {
1461 return NULL;
1462 }
1463
1464 return cookie.pi;
1465}
1466
1467int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
1468 void *cookie)
1469{
Tom Cherry6ed51c02015-12-04 11:34:42 -08001470 if (!__system_property_area__) {
1471 return -1;
1472 }
1473
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001474 if (__predict_false(compat_mode)) {
1475 return __system_property_foreach_compat(propfn, cookie);
1476 }
1477
Tom Cherry845e24a2015-12-03 15:38:52 -08001478 list_foreach(contexts, [propfn, cookie](context_node* l) {
Tom Cherryb4171692015-12-09 15:48:15 -08001479 if (l->check_access_and_open()) {
1480 l->pa()->foreach(propfn, cookie);
Tom Cherry49a309f2015-09-23 16:09:47 -07001481 }
1482 });
1483 return 0;
Narayan Kamathc9ae21a2014-02-19 17:59:05 +00001484}