Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1 | /* |
| 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 Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 28 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 29 | #include <ctype.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 32 | #include <poll.h> |
| 33 | #include <stdatomic.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 34 | #include <stdbool.h> |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 35 | #include <stddef.h> |
| 36 | #include <stdint.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 39 | #include <string.h> |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | #include <new> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 42 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 43 | #include <linux/xattr.h> |
| 44 | #include <netinet/in.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 45 | #include <sys/mman.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 46 | #include <sys/select.h> |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 47 | #include <sys/socket.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 48 | #include <sys/stat.h> |
| 49 | #include <sys/types.h> |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 50 | #include <sys/un.h> |
| 51 | #include <sys/xattr.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 52 | |
| 53 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 54 | #include <sys/_system_properties.h> |
| 55 | #include <sys/system_properties.h> |
| 56 | |
Elliott Hughes | d5ed63a | 2014-05-21 18:27:40 -0700 | [diff] [blame] | 57 | #include "private/bionic_futex.h" |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 58 | #include "private/bionic_lock.h" |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 59 | #include "private/bionic_macros.h" |
Dimitry Ivanov | 581b9f6 | 2017-01-09 11:05:52 -0800 | [diff] [blame] | 60 | #include "private/bionic_sdk_version.h" |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 61 | #include "private/libc_logging.h" |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 62 | |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 63 | static constexpr int PROP_FILENAME_MAX = 1024; |
| 64 | |
| 65 | static constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250; |
| 66 | static constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab; |
| 67 | |
| 68 | static constexpr size_t PA_SIZE = 128 * 1024; |
| 69 | |
| 70 | #define SERIAL_DIRTY(serial) ((serial) & 1) |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 71 | #define SERIAL_VALUE_LEN(serial) ((serial) >> 24) |
Dimitry Ivanov | 489f58b | 2017-01-24 18:39:04 +0000 | [diff] [blame] | 72 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 73 | static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME; |
| 74 | static const char* kServiceVersionPropertyName = "ro.property_service.version"; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Properties are stored in a hybrid trie/binary tree structure. |
| 78 | * Each property's name is delimited at '.' characters, and the tokens are put |
| 79 | * into a trie structure. Siblings at each level of the trie are stored in a |
| 80 | * binary tree. For instance, "ro.secure"="1" could be stored as follows: |
| 81 | * |
| 82 | * +-----+ children +----+ children +--------+ |
| 83 | * | |-------------->| ro |-------------->| secure | |
| 84 | * +-----+ +----+ +--------+ |
| 85 | * / \ / | |
| 86 | * left / \ right left / | prop +===========+ |
| 87 | * v v v +-------->| ro.secure | |
| 88 | * +-----+ +-----+ +-----+ +-----------+ |
| 89 | * | net | | sys | | com | | 1 | |
| 90 | * +-----+ +-----+ +-----+ +===========+ |
| 91 | */ |
| 92 | |
| 93 | // Represents a node in the trie. |
| 94 | struct prop_bt { |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 95 | uint32_t namelen; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 96 | |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 97 | // The property trie is updated only by the init process (single threaded) which provides |
| 98 | // property service. And it can be read by multiple threads at the same time. |
| 99 | // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the |
| 100 | // left, right, children "pointers" in the trie node. To make sure readers who see the |
| 101 | // change of "pointers" can also notice the change of prop_bt structure contents pointed by |
| 102 | // the "pointers", we always use release-consume ordering pair when accessing these "pointers". |
| 103 | |
| 104 | // prop "points" to prop_info structure if there is a propery associated with the trie node. |
| 105 | // Its situation is similar to the left, right, children "pointers". So we use |
| 106 | // atomic_uint_least32_t and release-consume ordering to protect it as well. |
| 107 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 108 | // We should also avoid rereading these fields redundantly, since not |
| 109 | // all processor implementations ensure that multiple loads from the |
| 110 | // same field are carried out in the right order. |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 111 | atomic_uint_least32_t prop; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 112 | |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 113 | atomic_uint_least32_t left; |
| 114 | atomic_uint_least32_t right; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 115 | |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 116 | atomic_uint_least32_t children; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 117 | |
| 118 | char name[0]; |
| 119 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 120 | prop_bt(const char *name, const uint32_t name_length) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 121 | this->namelen = name_length; |
| 122 | memcpy(this->name, name, name_length); |
| 123 | this->name[name_length] = '\0'; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | private: |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 127 | DISALLOW_COPY_AND_ASSIGN(prop_bt); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 130 | class prop_area { |
| 131 | public: |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 132 | |
| 133 | prop_area(const uint32_t magic, const uint32_t version) : |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 134 | magic_(magic), version_(version) { |
| 135 | atomic_init(&serial_, 0); |
| 136 | memset(reserved_, 0, sizeof(reserved_)); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 137 | // Allocate enough space for the root node. |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 138 | bytes_used_ = sizeof(prop_bt); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 141 | const prop_info *find(const char *name); |
| 142 | bool add(const char *name, unsigned int namelen, |
| 143 | const char *value, unsigned int valuelen); |
| 144 | |
| 145 | bool foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie); |
| 146 | |
| 147 | atomic_uint_least32_t *serial() { return &serial_; } |
| 148 | uint32_t magic() const { return magic_; } |
| 149 | uint32_t version() const { return version_; } |
| 150 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 151 | private: |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 152 | void *allocate_obj(const size_t size, uint_least32_t *const off); |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 153 | prop_bt *new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off); |
| 154 | prop_info *new_prop_info(const char *name, uint32_t namelen, |
| 155 | const char *value, uint32_t valuelen, |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 156 | uint_least32_t *const off); |
| 157 | void *to_prop_obj(uint_least32_t off); |
| 158 | prop_bt *to_prop_bt(atomic_uint_least32_t *off_p); |
| 159 | prop_info *to_prop_info(atomic_uint_least32_t *off_p); |
| 160 | |
| 161 | prop_bt *root_node(); |
| 162 | |
| 163 | prop_bt *find_prop_bt(prop_bt *const bt, const char *name, |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 164 | uint32_t namelen, bool alloc_if_needed); |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 165 | |
| 166 | const prop_info *find_property(prop_bt *const trie, const char *name, |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 167 | uint32_t namelen, const char *value, |
| 168 | uint32_t valuelen, bool alloc_if_needed); |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 169 | |
| 170 | bool foreach_property(prop_bt *const trie, |
| 171 | void (*propfn)(const prop_info *pi, void *cookie), |
| 172 | void *cookie); |
| 173 | |
| 174 | uint32_t bytes_used_; |
| 175 | atomic_uint_least32_t serial_; |
| 176 | uint32_t magic_; |
| 177 | uint32_t version_; |
| 178 | uint32_t reserved_[28]; |
| 179 | char data_[0]; |
| 180 | |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 181 | DISALLOW_COPY_AND_ASSIGN(prop_area); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | struct prop_info { |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 185 | atomic_uint_least32_t serial; |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 186 | // we need to keep this buffer around because the property |
| 187 | // value can be modified whereas name is constant. |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 188 | char value[PROP_VALUE_MAX]; |
| 189 | char name[0]; |
| 190 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 191 | prop_info(const char *name, uint32_t namelen, const char *value, uint32_t valuelen) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 192 | memcpy(this->name, name, namelen); |
| 193 | this->name[namelen] = '\0'; |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 194 | atomic_init(&this->serial, valuelen << 24); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 195 | memcpy(this->value, value, valuelen); |
| 196 | this->value[valuelen] = '\0'; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 197 | } |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 198 | private: |
| 199 | DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | struct find_nth_cookie { |
| 203 | uint32_t count; |
| 204 | const uint32_t n; |
| 205 | const prop_info *pi; |
| 206 | |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 207 | explicit find_nth_cookie(uint32_t n) : count(0), n(n), pi(NULL) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 208 | } |
| 209 | }; |
| 210 | |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 211 | // This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol. |
| 212 | // It's also used in a libnativehelper test. |
| 213 | prop_area* __system_property_area__ = nullptr; |
| 214 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 215 | static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 216 | static size_t pa_data_size; |
| 217 | static size_t pa_size; |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 218 | static bool initialized = false; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 219 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 220 | static prop_area* map_prop_area_rw(const char* filename, const char* context, |
| 221 | bool* fsetxattr_failed) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 222 | /* dev is a tmpfs that we can use to carve a shared workspace |
| 223 | * out of, so let's do that... |
| 224 | */ |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 225 | const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 226 | |
| 227 | if (fd < 0) { |
| 228 | if (errno == EACCES) { |
| 229 | /* for consistency with the case where the process has already |
| 230 | * mapped the page in and segfaults when trying to write to it |
| 231 | */ |
| 232 | abort(); |
| 233 | } |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 234 | return nullptr; |
| 235 | } |
| 236 | |
| 237 | if (context) { |
| 238 | if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) { |
| 239 | __libc_format_log(ANDROID_LOG_ERROR, "libc", |
| 240 | "fsetxattr failed to set context (%s) for \"%s\"", context, filename); |
| 241 | /* |
| 242 | * fsetxattr() will fail during system properties tests due to selinux policy. |
| 243 | * We do not want to create a custom policy for the tester, so we will continue in |
| 244 | * this function but set a flag that an error has occurred. |
| 245 | * Init, which is the only daemon that should ever call this function will abort |
| 246 | * when this error occurs. |
| 247 | * Otherwise, the tester will ignore it and continue, albeit without any selinux |
| 248 | * property separation. |
| 249 | */ |
| 250 | if (fsetxattr_failed) { |
| 251 | *fsetxattr_failed = true; |
| 252 | } |
| 253 | } |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 256 | if (ftruncate(fd, PA_SIZE) < 0) { |
| 257 | close(fd); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 258 | return nullptr; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | pa_size = PA_SIZE; |
| 262 | pa_data_size = pa_size - sizeof(prop_area); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 263 | |
| 264 | void *const memory_area = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 265 | if (memory_area == MAP_FAILED) { |
| 266 | close(fd); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 267 | return nullptr; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | prop_area *pa = new(memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION); |
| 271 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 272 | close(fd); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 273 | return pa; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 276 | static prop_area* map_fd_ro(const int fd) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 277 | struct stat fd_stat; |
| 278 | if (fstat(fd, &fd_stat) < 0) { |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 279 | return nullptr; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if ((fd_stat.st_uid != 0) |
| 283 | || (fd_stat.st_gid != 0) |
| 284 | || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) |
Narayan Kamath | 37e9570 | 2014-02-24 11:05:02 +0000 | [diff] [blame] | 285 | || (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area))) ) { |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 286 | return nullptr; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | pa_size = fd_stat.st_size; |
| 290 | pa_data_size = pa_size - sizeof(prop_area); |
| 291 | |
| 292 | void* const map_result = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0); |
| 293 | if (map_result == MAP_FAILED) { |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 294 | return nullptr; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | prop_area* pa = reinterpret_cast<prop_area*>(map_result); |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 298 | if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 299 | munmap(pa, pa_size); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 300 | return nullptr; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 303 | return pa; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 306 | static prop_area* map_prop_area(const char* filename) { |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 307 | int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY); |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 308 | if (fd == -1) return nullptr; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 309 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 310 | prop_area* map_result = map_fd_ro(fd); |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 311 | close(fd); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 312 | |
| 313 | return map_result; |
| 314 | } |
| 315 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 316 | void *prop_area::allocate_obj(const size_t size, uint_least32_t *const off) |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 317 | { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 318 | const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t)); |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 319 | if (bytes_used_ + aligned > pa_data_size) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 320 | return NULL; |
| 321 | } |
| 322 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 323 | *off = bytes_used_; |
| 324 | bytes_used_ += aligned; |
| 325 | return data_ + *off; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 328 | prop_bt *prop_area::new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off) |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 329 | { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 330 | uint_least32_t new_offset; |
| 331 | void *const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset); |
| 332 | if (p != NULL) { |
| 333 | prop_bt* bt = new(p) prop_bt(name, namelen); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 334 | *off = new_offset; |
| 335 | return bt; |
| 336 | } |
| 337 | |
| 338 | return NULL; |
| 339 | } |
| 340 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 341 | prop_info *prop_area::new_prop_info(const char *name, uint32_t namelen, |
| 342 | const char *value, uint32_t valuelen, uint_least32_t *const off) |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 343 | { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 344 | uint_least32_t new_offset; |
| 345 | void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset); |
| 346 | if (p != NULL) { |
| 347 | prop_info* info = new(p) prop_info(name, namelen, value, valuelen); |
| 348 | *off = new_offset; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 349 | return info; |
| 350 | } |
| 351 | |
| 352 | return NULL; |
| 353 | } |
| 354 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 355 | void *prop_area::to_prop_obj(uint_least32_t off) |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 356 | { |
| 357 | if (off > pa_data_size) |
| 358 | return NULL; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 359 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 360 | return (data_ + off); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 363 | inline prop_bt *prop_area::to_prop_bt(atomic_uint_least32_t* off_p) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 364 | uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume); |
| 365 | return reinterpret_cast<prop_bt*>(to_prop_obj(off)); |
| 366 | } |
| 367 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 368 | inline prop_info *prop_area::to_prop_info(atomic_uint_least32_t* off_p) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 369 | uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume); |
| 370 | return reinterpret_cast<prop_info*>(to_prop_obj(off)); |
| 371 | } |
| 372 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 373 | inline prop_bt *prop_area::root_node() |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 374 | { |
| 375 | return reinterpret_cast<prop_bt*>(to_prop_obj(0)); |
| 376 | } |
| 377 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 378 | static int cmp_prop_name(const char *one, uint32_t one_len, const char *two, |
| 379 | uint32_t two_len) |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 380 | { |
| 381 | if (one_len < two_len) |
| 382 | return -1; |
| 383 | else if (one_len > two_len) |
| 384 | return 1; |
| 385 | else |
| 386 | return strncmp(one, two, one_len); |
| 387 | } |
| 388 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 389 | prop_bt *prop_area::find_prop_bt(prop_bt *const bt, const char *name, |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 390 | uint32_t namelen, bool alloc_if_needed) |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 391 | { |
| 392 | |
| 393 | prop_bt* current = bt; |
| 394 | while (true) { |
| 395 | if (!current) { |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | const int ret = cmp_prop_name(name, namelen, current->name, current->namelen); |
| 400 | if (ret == 0) { |
| 401 | return current; |
| 402 | } |
| 403 | |
| 404 | if (ret < 0) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 405 | uint_least32_t left_offset = atomic_load_explicit(¤t->left, memory_order_relaxed); |
| 406 | if (left_offset != 0) { |
| 407 | current = to_prop_bt(¤t->left); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 408 | } else { |
| 409 | if (!alloc_if_needed) { |
| 410 | return NULL; |
| 411 | } |
| 412 | |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 413 | uint_least32_t new_offset; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 414 | prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset); |
| 415 | if (new_bt) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 416 | atomic_store_explicit(¤t->left, new_offset, memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 417 | } |
| 418 | return new_bt; |
| 419 | } |
| 420 | } else { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 421 | uint_least32_t right_offset = atomic_load_explicit(¤t->right, memory_order_relaxed); |
| 422 | if (right_offset != 0) { |
| 423 | current = to_prop_bt(¤t->right); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 424 | } else { |
| 425 | if (!alloc_if_needed) { |
| 426 | return NULL; |
| 427 | } |
| 428 | |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 429 | uint_least32_t new_offset; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 430 | prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset); |
| 431 | if (new_bt) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 432 | atomic_store_explicit(¤t->right, new_offset, memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 433 | } |
| 434 | return new_bt; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 440 | const prop_info *prop_area::find_property(prop_bt *const trie, const char *name, |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 441 | uint32_t namelen, const char *value, uint32_t valuelen, |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 442 | bool alloc_if_needed) |
| 443 | { |
| 444 | if (!trie) return NULL; |
| 445 | |
| 446 | const char *remaining_name = name; |
| 447 | prop_bt* current = trie; |
| 448 | while (true) { |
| 449 | const char *sep = strchr(remaining_name, '.'); |
| 450 | const bool want_subtree = (sep != NULL); |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 451 | const uint32_t substr_size = (want_subtree) ? |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 452 | sep - remaining_name : strlen(remaining_name); |
| 453 | |
| 454 | if (!substr_size) { |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | prop_bt* root = NULL; |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 459 | uint_least32_t children_offset = atomic_load_explicit(¤t->children, memory_order_relaxed); |
| 460 | if (children_offset != 0) { |
| 461 | root = to_prop_bt(¤t->children); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 462 | } else if (alloc_if_needed) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 463 | uint_least32_t new_offset; |
| 464 | root = new_prop_bt(remaining_name, substr_size, &new_offset); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 465 | if (root) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 466 | atomic_store_explicit(¤t->children, new_offset, memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | if (!root) { |
| 471 | return NULL; |
| 472 | } |
| 473 | |
| 474 | current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed); |
| 475 | if (!current) { |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | if (!want_subtree) |
| 480 | break; |
| 481 | |
| 482 | remaining_name = sep + 1; |
| 483 | } |
| 484 | |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 485 | uint_least32_t prop_offset = atomic_load_explicit(¤t->prop, memory_order_relaxed); |
| 486 | if (prop_offset != 0) { |
| 487 | return to_prop_info(¤t->prop); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 488 | } else if (alloc_if_needed) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 489 | uint_least32_t new_offset; |
| 490 | prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 491 | if (new_info) { |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 492 | atomic_store_explicit(¤t->prop, new_offset, memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | return new_info; |
| 496 | } else { |
| 497 | return NULL; |
| 498 | } |
| 499 | } |
| 500 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 501 | class PropertyServiceConnection { |
| 502 | public: |
| 503 | PropertyServiceConnection() : last_error_(0) { |
| 504 | fd_ = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 505 | if (fd_ == -1) { |
| 506 | last_error_ = errno; |
| 507 | return; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | const size_t namelen = strlen(property_service_socket); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 511 | sockaddr_un addr; |
| 512 | memset(&addr, 0, sizeof(addr)); |
| 513 | strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path)); |
| 514 | addr.sun_family = AF_LOCAL; |
| 515 | socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1; |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 516 | |
| 517 | if (TEMP_FAILURE_RETRY(connect(fd_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) { |
| 518 | close(fd_); |
| 519 | fd_ = -1; |
| 520 | last_error_ = errno; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | bool IsValid() { |
| 525 | return fd_ != -1; |
| 526 | } |
| 527 | |
| 528 | int GetLastError() { |
| 529 | return last_error_; |
| 530 | } |
| 531 | |
| 532 | bool SendUint32(uint32_t value) { |
| 533 | int result = TEMP_FAILURE_RETRY(send(fd_, &value, sizeof(value), 0)); |
| 534 | return CheckSendRecvResult(result, sizeof(value)); |
| 535 | } |
| 536 | |
| 537 | bool SendString(const char* value) { |
| 538 | uint32_t valuelen = strlen(value); |
| 539 | if (!SendUint32(valuelen)) { |
| 540 | return false; |
Dimitry Ivanov | 5c1ce27 | 2015-12-03 11:26:38 -0800 | [diff] [blame] | 541 | } |
Dimitry Ivanov | 5c1ce27 | 2015-12-03 11:26:38 -0800 | [diff] [blame] | 542 | |
Dimitry Ivanov | cafd355 | 2017-01-24 12:39:33 -0800 | [diff] [blame] | 543 | // Trying to send even 0 bytes to closed socket may lead to |
| 544 | // broken pipe (http://b/34670529). |
| 545 | if (valuelen == 0) { |
| 546 | return true; |
| 547 | } |
| 548 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 549 | int result = TEMP_FAILURE_RETRY(send(fd_, value, valuelen, 0)); |
| 550 | return CheckSendRecvResult(result, valuelen); |
| 551 | } |
| 552 | |
| 553 | bool RecvInt32(int32_t* value) { |
| 554 | int result = TEMP_FAILURE_RETRY(recv(fd_, value, sizeof(*value), MSG_WAITALL)); |
| 555 | return CheckSendRecvResult(result, sizeof(*value)); |
| 556 | } |
| 557 | |
| 558 | int GetFd() { |
| 559 | return fd_; |
| 560 | } |
| 561 | |
| 562 | ~PropertyServiceConnection() { |
| 563 | if (fd_ != -1) { |
| 564 | close(fd_); |
| 565 | } |
| 566 | } |
| 567 | private: |
| 568 | bool CheckSendRecvResult(int result, int expected_len) { |
| 569 | if (result == -1) { |
| 570 | last_error_ = errno; |
| 571 | } else if (result != expected_len) { |
| 572 | last_error_ = -1; |
| 573 | } else { |
| 574 | last_error_ = 0; |
| 575 | } |
| 576 | |
| 577 | return last_error_ == 0; |
| 578 | } |
| 579 | |
| 580 | int fd_; |
| 581 | int last_error_; |
| 582 | }; |
| 583 | |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 584 | struct prop_msg { |
| 585 | unsigned cmd; |
| 586 | char name[PROP_NAME_MAX]; |
| 587 | char value[PROP_VALUE_MAX]; |
| 588 | }; |
| 589 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 590 | static int send_prop_msg(const prop_msg* msg) { |
| 591 | PropertyServiceConnection connection; |
| 592 | if (!connection.IsValid()) { |
| 593 | return connection.GetLastError(); |
| 594 | } |
Dimitry Ivanov | 489f58b | 2017-01-24 18:39:04 +0000 | [diff] [blame] | 595 | |
| 596 | int result = -1; |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 597 | int fd = connection.GetFd(); |
| 598 | |
| 599 | const int num_bytes = TEMP_FAILURE_RETRY(send(fd, msg, sizeof(prop_msg), 0)); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 600 | if (num_bytes == sizeof(prop_msg)) { |
| 601 | // We successfully wrote to the property server but now we |
| 602 | // wait for the property server to finish its work. It |
| 603 | // acknowledges its completion by closing the socket so we |
| 604 | // poll here (on nothing), waiting for the socket to close. |
| 605 | // If you 'adb shell setprop foo bar' you'll see the POLLHUP |
| 606 | // once the socket closes. Out of paranoia we cap our poll |
| 607 | // at 250 ms. |
| 608 | pollfd pollfds[1]; |
| 609 | pollfds[0].fd = fd; |
| 610 | pollfds[0].events = 0; |
| 611 | const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */)); |
| 612 | if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) { |
| 613 | result = 0; |
| 614 | } else { |
| 615 | // Ignore the timeout and treat it like a success anyway. |
| 616 | // The init process is single-threaded and its property |
| 617 | // service is sometimes slow to respond (perhaps it's off |
| 618 | // starting a child process or something) and thus this |
| 619 | // times out and the caller thinks it failed, even though |
| 620 | // it's still getting around to it. So we fake it here, |
| 621 | // mostly for ctl.* properties, but we do try and wait 250 |
| 622 | // ms so callers who do read-after-write can reliably see |
| 623 | // what they've written. Most of the time. |
| 624 | // TODO: fix the system properties design. |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 625 | __libc_format_log(ANDROID_LOG_WARN, "libc", |
| 626 | "Property service has timed out while trying to set \"%s\" to \"%s\"", |
| 627 | msg->name, msg->value); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 628 | result = 0; |
| 629 | } |
| 630 | } |
| 631 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 632 | return result; |
| 633 | } |
| 634 | |
| 635 | static void find_nth_fn(const prop_info *pi, void *ptr) |
| 636 | { |
| 637 | find_nth_cookie *cookie = reinterpret_cast<find_nth_cookie*>(ptr); |
| 638 | |
| 639 | if (cookie->n == cookie->count) |
| 640 | cookie->pi = pi; |
| 641 | |
| 642 | cookie->count++; |
| 643 | } |
| 644 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 645 | bool prop_area::foreach_property(prop_bt *const trie, |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 646 | void (*propfn)(const prop_info *pi, void *cookie), void *cookie) |
| 647 | { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 648 | if (!trie) |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 649 | return false; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 650 | |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 651 | uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed); |
| 652 | if (left_offset != 0) { |
| 653 | const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 654 | if (err < 0) |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 655 | return false; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 656 | } |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 657 | uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed); |
| 658 | if (prop_offset != 0) { |
| 659 | prop_info *info = to_prop_info(&trie->prop); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 660 | if (!info) |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 661 | return false; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 662 | propfn(info, cookie); |
| 663 | } |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 664 | uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed); |
| 665 | if (children_offset != 0) { |
| 666 | const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 667 | if (err < 0) |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 668 | return false; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 669 | } |
Yabin Cui | b8ce474 | 2015-02-10 21:35:56 -0800 | [diff] [blame] | 670 | uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed); |
| 671 | if (right_offset != 0) { |
| 672 | const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 673 | if (err < 0) |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 674 | return false; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 677 | return true; |
| 678 | } |
| 679 | |
| 680 | const prop_info *prop_area::find(const char *name) { |
| 681 | return find_property(root_node(), name, strlen(name), nullptr, 0, false); |
| 682 | } |
| 683 | |
| 684 | bool prop_area::add(const char *name, unsigned int namelen, |
| 685 | const char *value, unsigned int valuelen) { |
| 686 | return find_property(root_node(), name, namelen, value, valuelen, true); |
| 687 | } |
| 688 | |
| 689 | bool prop_area::foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) { |
| 690 | return foreach_property(root_node(), propfn, cookie); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 693 | class context_node { |
| 694 | public: |
| 695 | context_node(context_node* next, const char* context, prop_area* pa) |
| 696 | : next(next), context_(strdup(context)), pa_(pa), no_access_(false) { |
| 697 | lock_.init(false); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 698 | } |
| 699 | ~context_node() { |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 700 | unmap(); |
| 701 | free(context_); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 702 | } |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 703 | bool open(bool access_rw, bool* fsetxattr_failed); |
| 704 | bool check_access_and_open(); |
| 705 | void reset_access(); |
| 706 | |
| 707 | const char* context() const { return context_; } |
| 708 | prop_area* pa() { return pa_; } |
| 709 | |
| 710 | context_node* next; |
| 711 | |
| 712 | private: |
| 713 | bool check_access(); |
| 714 | void unmap(); |
| 715 | |
| 716 | Lock lock_; |
| 717 | char* context_; |
| 718 | prop_area* pa_; |
| 719 | bool no_access_; |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 720 | }; |
| 721 | |
| 722 | struct prefix_node { |
| 723 | prefix_node(struct prefix_node* next, const char* prefix, context_node* context) |
| 724 | : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) { |
| 725 | } |
| 726 | ~prefix_node() { |
| 727 | free(prefix); |
| 728 | } |
| 729 | char* prefix; |
| 730 | const size_t prefix_len; |
| 731 | context_node* context; |
| 732 | struct prefix_node* next; |
| 733 | }; |
| 734 | |
| 735 | template <typename List, typename... Args> |
| 736 | static inline void list_add(List** list, Args... args) { |
| 737 | *list = new List(*list, args...); |
| 738 | } |
| 739 | |
| 740 | static void list_add_after_len(prefix_node** list, const char* prefix, context_node* context) { |
| 741 | size_t prefix_len = strlen(prefix); |
| 742 | |
| 743 | auto next_list = list; |
| 744 | |
| 745 | while (*next_list) { |
| 746 | if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') { |
| 747 | list_add(next_list, prefix, context); |
| 748 | return; |
| 749 | } |
| 750 | next_list = &(*next_list)->next; |
| 751 | } |
| 752 | list_add(next_list, prefix, context); |
| 753 | } |
| 754 | |
| 755 | template <typename List, typename Func> |
| 756 | static void list_foreach(List* list, Func func) { |
| 757 | while (list) { |
| 758 | func(list); |
| 759 | list = list->next; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | template <typename List, typename Func> |
| 764 | static List* list_find(List* list, Func func) { |
| 765 | while (list) { |
| 766 | if (func(list)) { |
| 767 | return list; |
| 768 | } |
| 769 | list = list->next; |
| 770 | } |
| 771 | return nullptr; |
| 772 | } |
| 773 | |
| 774 | template <typename List> |
| 775 | static void list_free(List** list) { |
| 776 | while (*list) { |
| 777 | auto old_list = *list; |
| 778 | *list = old_list->next; |
| 779 | delete old_list; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | static prefix_node* prefixes = nullptr; |
| 784 | static context_node* contexts = nullptr; |
| 785 | |
| 786 | /* |
| 787 | * pthread_mutex_lock() calls into system_properties in the case of contention. |
| 788 | * This creates a risk of dead lock if any system_properties functions |
| 789 | * use pthread locks after system_property initialization. |
| 790 | * |
| 791 | * For this reason, the below three functions use a bionic Lock and static |
| 792 | * allocation of memory for each filename. |
| 793 | */ |
| 794 | |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 795 | bool context_node::open(bool access_rw, bool* fsetxattr_failed) { |
| 796 | lock_.lock(); |
| 797 | if (pa_) { |
| 798 | lock_.unlock(); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 799 | return true; |
| 800 | } |
| 801 | |
| 802 | char filename[PROP_FILENAME_MAX]; |
Tom Cherry | 8352475 | 2016-01-26 15:27:07 -0800 | [diff] [blame] | 803 | int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", |
| 804 | property_filename, context_); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 805 | if (len < 0 || len > PROP_FILENAME_MAX) { |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 806 | lock_.unlock(); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 807 | return false; |
| 808 | } |
| 809 | |
| 810 | if (access_rw) { |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 811 | pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 812 | } else { |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 813 | pa_ = map_prop_area(filename); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 814 | } |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 815 | lock_.unlock(); |
| 816 | return pa_; |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 819 | bool context_node::check_access_and_open() { |
| 820 | if (!pa_ && !no_access_) { |
| 821 | if (!check_access() || !open(false, nullptr)) { |
| 822 | no_access_ = true; |
| 823 | } |
| 824 | } |
| 825 | return pa_; |
| 826 | } |
| 827 | |
| 828 | void context_node::reset_access() { |
| 829 | if (!check_access()) { |
| 830 | unmap(); |
| 831 | no_access_ = true; |
| 832 | } else { |
| 833 | no_access_ = false; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | bool context_node::check_access() { |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 838 | char filename[PROP_FILENAME_MAX]; |
Tom Cherry | 8352475 | 2016-01-26 15:27:07 -0800 | [diff] [blame] | 839 | int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", |
| 840 | property_filename, context_); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 841 | if (len < 0 || len > PROP_FILENAME_MAX) { |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | return access(filename, R_OK) == 0; |
| 846 | } |
| 847 | |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 848 | void context_node::unmap() { |
| 849 | if (!pa_) { |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | munmap(pa_, pa_size); |
| 854 | if (pa_ == __system_property_area__) { |
| 855 | __system_property_area__ = nullptr; |
| 856 | } |
| 857 | pa_ = nullptr; |
| 858 | } |
| 859 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 860 | static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) { |
| 861 | char filename[PROP_FILENAME_MAX]; |
Tom Cherry | 8352475 | 2016-01-26 15:27:07 -0800 | [diff] [blame] | 862 | int len = __libc_format_buffer(filename, sizeof(filename), |
| 863 | "%s/properties_serial", property_filename); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 864 | if (len < 0 || len > PROP_FILENAME_MAX) { |
| 865 | __system_property_area__ = nullptr; |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | if (access_rw) { |
| 870 | __system_property_area__ = |
| 871 | map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed); |
| 872 | } else { |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 873 | __system_property_area__ = map_prop_area(filename); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 874 | } |
| 875 | return __system_property_area__; |
| 876 | } |
| 877 | |
| 878 | static prop_area* get_prop_area_for_name(const char* name) { |
Tom Cherry | 845e24a | 2015-12-03 15:38:52 -0800 | [diff] [blame] | 879 | auto entry = list_find(prefixes, [name](prefix_node* l) { |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 880 | return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len); |
| 881 | }); |
| 882 | if (!entry) { |
| 883 | return nullptr; |
| 884 | } |
| 885 | |
| 886 | auto cnode = entry->context; |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 887 | if (!cnode->pa()) { |
| 888 | /* |
| 889 | * We explicitly do not check no_access_ in this case because unlike the |
| 890 | * case of foreach(), we want to generate an selinux audit for each |
| 891 | * non-permitted property access in this function. |
| 892 | */ |
| 893 | cnode->open(false, nullptr); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 894 | } |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 895 | return cnode->pa(); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* |
| 899 | * The below two functions are duplicated from label_support.c in libselinux. |
| 900 | * TODO: Find a location suitable for these functions such that both libc and |
| 901 | * libselinux can share a common source file. |
| 902 | */ |
| 903 | |
| 904 | /* |
| 905 | * The read_spec_entries and read_spec_entry functions may be used to |
| 906 | * replace sscanf to read entries from spec files. The file and |
| 907 | * property services now use these. |
| 908 | */ |
| 909 | |
| 910 | /* Read an entry from a spec file (e.g. file_contexts) */ |
| 911 | static inline int read_spec_entry(char **entry, char **ptr, int *len) |
| 912 | { |
| 913 | *entry = NULL; |
| 914 | char *tmp_buf = NULL; |
| 915 | |
| 916 | while (isspace(**ptr) && **ptr != '\0') |
| 917 | (*ptr)++; |
| 918 | |
| 919 | tmp_buf = *ptr; |
| 920 | *len = 0; |
| 921 | |
| 922 | while (!isspace(**ptr) && **ptr != '\0') { |
| 923 | (*ptr)++; |
| 924 | (*len)++; |
| 925 | } |
| 926 | |
| 927 | if (*len) { |
| 928 | *entry = strndup(tmp_buf, *len); |
| 929 | if (!*entry) |
| 930 | return -1; |
| 931 | } |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * line_buf - Buffer containing the spec entries . |
| 938 | * num_args - The number of spec parameter entries to process. |
| 939 | * ... - A 'char **spec_entry' for each parameter. |
| 940 | * returns - The number of items processed. |
| 941 | * |
| 942 | * This function calls read_spec_entry() to do the actual string processing. |
| 943 | */ |
| 944 | static int read_spec_entries(char *line_buf, int num_args, ...) |
| 945 | { |
| 946 | char **spec_entry, *buf_p; |
| 947 | int len, rc, items, entry_len = 0; |
| 948 | va_list ap; |
| 949 | |
| 950 | len = strlen(line_buf); |
| 951 | if (line_buf[len - 1] == '\n') |
| 952 | line_buf[len - 1] = '\0'; |
| 953 | else |
| 954 | /* Handle case if line not \n terminated by bumping |
| 955 | * the len for the check below (as the line is NUL |
| 956 | * terminated by getline(3)) */ |
| 957 | len++; |
| 958 | |
| 959 | buf_p = line_buf; |
| 960 | while (isspace(*buf_p)) |
| 961 | buf_p++; |
| 962 | |
| 963 | /* Skip comment lines and empty lines. */ |
| 964 | if (*buf_p == '#' || *buf_p == '\0') |
| 965 | return 0; |
| 966 | |
| 967 | /* Process the spec file entries */ |
| 968 | va_start(ap, num_args); |
| 969 | |
| 970 | items = 0; |
| 971 | while (items < num_args) { |
| 972 | spec_entry = va_arg(ap, char **); |
| 973 | |
| 974 | if (len - 1 == buf_p - line_buf) { |
| 975 | va_end(ap); |
| 976 | return items; |
| 977 | } |
| 978 | |
| 979 | rc = read_spec_entry(spec_entry, &buf_p, &entry_len); |
| 980 | if (rc < 0) { |
| 981 | va_end(ap); |
| 982 | return rc; |
| 983 | } |
| 984 | if (entry_len) |
| 985 | items++; |
| 986 | } |
| 987 | va_end(ap); |
| 988 | return items; |
| 989 | } |
| 990 | |
Sandeep Patil | 34f0cfa | 2016-12-27 17:37:44 -0800 | [diff] [blame] | 991 | static bool initialize_properties_from_file(const char *filename) { |
| 992 | FILE* file = fopen(filename, "re"); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 993 | if (!file) { |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | char* buffer = nullptr; |
| 998 | size_t line_len; |
| 999 | char* prop_prefix = nullptr; |
| 1000 | char* context = nullptr; |
| 1001 | |
| 1002 | while (getline(&buffer, &line_len, file) > 0) { |
| 1003 | int items = read_spec_entries(buffer, 2, &prop_prefix, &context); |
| 1004 | if (items <= 0) { |
| 1005 | continue; |
| 1006 | } |
| 1007 | if (items == 1) { |
| 1008 | free(prop_prefix); |
| 1009 | continue; |
| 1010 | } |
Tom Cherry | 21eadee | 2015-12-04 15:53:25 -0800 | [diff] [blame] | 1011 | /* |
| 1012 | * init uses ctl.* properties as an IPC mechanism and does not write them |
| 1013 | * to a property file, therefore we do not need to create property files |
| 1014 | * to store them. |
| 1015 | */ |
| 1016 | if (!strncmp(prop_prefix, "ctl.", 4)) { |
| 1017 | free(prop_prefix); |
| 1018 | free(context); |
| 1019 | continue; |
| 1020 | } |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1021 | |
Tom Cherry | 845e24a | 2015-12-03 15:38:52 -0800 | [diff] [blame] | 1022 | auto old_context = list_find( |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1023 | contexts, [context](context_node* l) { return !strcmp(l->context(), context); }); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1024 | if (old_context) { |
| 1025 | list_add_after_len(&prefixes, prop_prefix, old_context); |
| 1026 | } else { |
| 1027 | list_add(&contexts, context, nullptr); |
| 1028 | list_add_after_len(&prefixes, prop_prefix, contexts); |
| 1029 | } |
| 1030 | free(prop_prefix); |
| 1031 | free(context); |
| 1032 | } |
| 1033 | |
| 1034 | free(buffer); |
| 1035 | fclose(file); |
Sandeep Patil | 34f0cfa | 2016-12-27 17:37:44 -0800 | [diff] [blame] | 1036 | |
| 1037 | return true; |
| 1038 | } |
| 1039 | |
| 1040 | static bool initialize_properties() { |
| 1041 | // If we do find /property_contexts, then this is being |
| 1042 | // run as part of the OTA updater on older release that had |
| 1043 | // /property_contexts - b/34370523 |
| 1044 | if (initialize_properties_from_file("/property_contexts")) { |
| 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | // TODO: Change path to /system/property_contexts after b/27805372 |
| 1049 | if (!initialize_properties_from_file("/plat_property_contexts")) { |
| 1050 | return false; |
| 1051 | } |
| 1052 | |
| 1053 | // TODO: Change path to /vendor/property_contexts after b/27805372 |
| 1054 | // device-specific property context is optional, so load if it exists. |
| 1055 | initialize_properties_from_file("/nonplat_property_contexts"); |
| 1056 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1057 | return true; |
| 1058 | } |
| 1059 | |
| 1060 | static bool is_dir(const char* pathname) { |
| 1061 | struct stat info; |
| 1062 | if (stat(pathname, &info) == -1) { |
| 1063 | return false; |
| 1064 | } |
| 1065 | return S_ISDIR(info.st_mode); |
| 1066 | } |
| 1067 | |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1068 | static void free_and_unmap_contexts() { |
| 1069 | list_free(&prefixes); |
| 1070 | list_free(&contexts); |
| 1071 | if (__system_property_area__) { |
| 1072 | munmap(__system_property_area__, pa_size); |
| 1073 | __system_property_area__ = nullptr; |
| 1074 | } |
| 1075 | } |
| 1076 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1077 | int __system_properties_init() |
| 1078 | { |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1079 | if (initialized) { |
| 1080 | list_foreach(contexts, [](context_node* l) { l->reset_access(); }); |
| 1081 | return 0; |
| 1082 | } |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1083 | if (is_dir(property_filename)) { |
| 1084 | if (!initialize_properties()) { |
| 1085 | return -1; |
| 1086 | } |
| 1087 | if (!map_system_property_area(false, nullptr)) { |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1088 | free_and_unmap_contexts(); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1089 | return -1; |
| 1090 | } |
| 1091 | } else { |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 1092 | __system_property_area__ = map_prop_area(property_filename); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1093 | if (!__system_property_area__) { |
| 1094 | return -1; |
| 1095 | } |
| 1096 | list_add(&contexts, "legacy_system_prop_area", __system_property_area__); |
| 1097 | list_add_after_len(&prefixes, "*", contexts); |
| 1098 | } |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1099 | initialized = true; |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1100 | return 0; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | int __system_property_set_filename(const char *filename) |
| 1104 | { |
| 1105 | size_t len = strlen(filename); |
| 1106 | if (len >= sizeof(property_filename)) |
| 1107 | return -1; |
| 1108 | |
| 1109 | strcpy(property_filename, filename); |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | int __system_property_area_init() |
| 1114 | { |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1115 | free_and_unmap_contexts(); |
Nick Kralevich | bb59d47 | 2017-01-06 12:04:29 -0800 | [diff] [blame] | 1116 | mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1117 | if (!initialize_properties()) { |
| 1118 | return -1; |
| 1119 | } |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1120 | bool open_failed = false; |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1121 | bool fsetxattr_failed = false; |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1122 | list_foreach(contexts, [&fsetxattr_failed, &open_failed](context_node* l) { |
| 1123 | if (!l->open(true, &fsetxattr_failed)) { |
| 1124 | open_failed = true; |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1125 | } |
| 1126 | }); |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1127 | if (open_failed || !map_system_property_area(true, &fsetxattr_failed)) { |
| 1128 | free_and_unmap_contexts(); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1129 | return -1; |
| 1130 | } |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1131 | initialized = true; |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1132 | return fsetxattr_failed ? -2 : 0; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Mark Salyzyn | bfd6527 | 2015-04-24 09:31:32 -0700 | [diff] [blame] | 1135 | unsigned int __system_property_area_serial() |
| 1136 | { |
| 1137 | prop_area *pa = __system_property_area__; |
| 1138 | if (!pa) { |
| 1139 | return -1; |
| 1140 | } |
| 1141 | // Make sure this read fulfilled before __system_property_serial |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 1142 | return atomic_load_explicit(pa->serial(), memory_order_acquire); |
Mark Salyzyn | bfd6527 | 2015-04-24 09:31:32 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1145 | const prop_info *__system_property_find(const char *name) |
| 1146 | { |
Tom Cherry | 6ed51c0 | 2015-12-04 11:34:42 -0800 | [diff] [blame] | 1147 | if (!__system_property_area__) { |
| 1148 | return nullptr; |
| 1149 | } |
| 1150 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1151 | prop_area* pa = get_prop_area_for_name(name); |
| 1152 | if (!pa) { |
| 1153 | __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name); |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 1154 | return nullptr; |
| 1155 | } |
| 1156 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1157 | return pa->find(name); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Hans Boehm | 1e8587a | 2014-08-19 14:07:55 -0700 | [diff] [blame] | 1160 | // The C11 standard doesn't allow atomic loads from const fields, |
| 1161 | // though C++11 does. Fudge it until standards get straightened out. |
| 1162 | static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, |
| 1163 | memory_order mo) { |
| 1164 | atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s); |
| 1165 | return atomic_load_explicit(non_const_s, mo); |
| 1166 | } |
| 1167 | |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 1168 | int __system_property_read(const prop_info *pi, char *name, char *value) { |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 1169 | while (true) { |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1170 | uint32_t serial = __system_property_serial(pi); // acquire semantics |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 1171 | size_t len = SERIAL_VALUE_LEN(serial); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1172 | memcpy(value, pi->value, len + 1); |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1173 | // TODO: Fix the synchronization scheme here. |
| 1174 | // There is no fully supported way to implement this kind |
| 1175 | // of synchronization in C++11, since the memcpy races with |
| 1176 | // updates to pi, and the data being accessed is not atomic. |
| 1177 | // The following fence is unintuitive, but would be the |
| 1178 | // correct one if memcpy used memory_order_relaxed atomic accesses. |
| 1179 | // In practice it seems unlikely that the generated code would |
| 1180 | // would be any different, so this should be OK. |
| 1181 | atomic_thread_fence(memory_order_acquire); |
| 1182 | if (serial == |
Hans Boehm | 1e8587a | 2014-08-19 14:07:55 -0700 | [diff] [blame] | 1183 | load_const_atomic(&(pi->serial), memory_order_relaxed)) { |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1184 | if (name != nullptr) { |
| 1185 | size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX); |
| 1186 | if(namelen >= PROP_NAME_MAX) { |
| 1187 | __libc_format_log(ANDROID_LOG_ERROR, "libc", |
| 1188 | "The property name length for \"%s\" is >= %d;" |
| 1189 | " please use __system_property_read_callback" |
| 1190 | " to read this property. (the name is truncated to \"%s\")", |
| 1191 | pi->name, PROP_NAME_MAX - 1, name); |
| 1192 | } |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1193 | } |
| 1194 | return len; |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1199 | void __system_property_read_callback(const prop_info* pi, |
| 1200 | void (*callback)(void* cookie, const char* name, const char* value), |
| 1201 | void* cookie) { |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1202 | while (true) { |
| 1203 | uint32_t serial = __system_property_serial(pi); // acquire semantics |
| 1204 | size_t len = SERIAL_VALUE_LEN(serial); |
| 1205 | char value_buf[len+1]; |
| 1206 | |
| 1207 | memcpy(value_buf, pi->value, len); |
| 1208 | value_buf[len] = '\0'; |
| 1209 | |
| 1210 | // TODO: see todo in __system_property_read function |
| 1211 | atomic_thread_fence(memory_order_acquire); |
| 1212 | if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) { |
| 1213 | callback(cookie, pi->name, value_buf); |
| 1214 | return; |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1219 | int __system_property_get(const char *name, char *value) |
| 1220 | { |
| 1221 | const prop_info *pi = __system_property_find(name); |
| 1222 | |
| 1223 | if (pi != 0) { |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1224 | return __system_property_read(pi, nullptr, value); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1225 | } else { |
| 1226 | value[0] = 0; |
| 1227 | return 0; |
| 1228 | } |
| 1229 | } |
| 1230 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1231 | static constexpr uint32_t kProtocolVersion1 = 1; |
| 1232 | static constexpr uint32_t kProtocolVersion2 = 2; // current |
| 1233 | |
| 1234 | static atomic_uint_least32_t g_propservice_protocol_version = 0; |
| 1235 | |
| 1236 | static void detect_protocol_version() { |
| 1237 | char value[PROP_VALUE_MAX]; |
| 1238 | if (__system_property_get(kServiceVersionPropertyName, value) == 0) { |
| 1239 | g_propservice_protocol_version = kProtocolVersion1; |
| 1240 | __libc_format_log(ANDROID_LOG_WARN, "libc", |
| 1241 | "Using old property service protocol (\"%s\" is not set)", |
| 1242 | kServiceVersionPropertyName); |
| 1243 | } else { |
| 1244 | uint32_t version = static_cast<uint32_t>(atoll(value)); |
| 1245 | if (version >= kProtocolVersion2) { |
| 1246 | g_propservice_protocol_version = kProtocolVersion2; |
| 1247 | } else { |
| 1248 | __libc_format_log(ANDROID_LOG_WARN, "libc", |
| 1249 | "Using old property service protocol (\"%s\"=\"%s\")", |
| 1250 | kServiceVersionPropertyName, value); |
| 1251 | g_propservice_protocol_version = kProtocolVersion1; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | int __system_property_set(const char* key, const char* value) { |
| 1257 | if (key == nullptr) return -1; |
| 1258 | if (value == nullptr) value = ""; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1259 | if (strlen(value) >= PROP_VALUE_MAX) return -1; |
| 1260 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1261 | if (g_propservice_protocol_version == 0) { |
| 1262 | detect_protocol_version(); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1265 | int result = -1; |
| 1266 | if (g_propservice_protocol_version == kProtocolVersion1) { |
| 1267 | // Old protocol does not support long names |
| 1268 | if (strlen(key) >= PROP_NAME_MAX) return -1; |
| 1269 | |
| 1270 | prop_msg msg; |
| 1271 | memset(&msg, 0, sizeof msg); |
| 1272 | msg.cmd = PROP_MSG_SETPROP; |
| 1273 | strlcpy(msg.name, key, sizeof msg.name); |
| 1274 | strlcpy(msg.value, value, sizeof msg.value); |
| 1275 | |
| 1276 | result = send_prop_msg(&msg); |
| 1277 | } else { |
| 1278 | // Use proper protocol |
| 1279 | PropertyServiceConnection connection; |
| 1280 | if (connection.IsValid() && |
| 1281 | connection.SendUint32(PROP_MSG_SETPROP2) && |
| 1282 | connection.SendString(key) && |
| 1283 | connection.SendString(value) && |
| 1284 | connection.RecvInt32(&result)) { |
| 1285 | if (result != PROP_SUCCESS) { |
| 1286 | __libc_format_log(ANDROID_LOG_WARN, "libc", |
| 1287 | "Unable to set property \"%s\" to \"%s\": error code: 0x%x", |
| 1288 | key, value, result); |
| 1289 | } |
| 1290 | } else { |
| 1291 | result = connection.GetLastError(); |
| 1292 | __libc_format_log(ANDROID_LOG_WARN, "libc", |
| 1293 | "Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)", |
| 1294 | key, value, result, strerror(result)); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | return result != 0 ? -1 : 0; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1301 | int __system_property_update(prop_info *pi, const char *value, unsigned int len) |
| 1302 | { |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1303 | if (len >= PROP_VALUE_MAX) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1304 | return -1; |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1305 | } |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1306 | |
Tom Cherry | 6ed51c0 | 2015-12-04 11:34:42 -0800 | [diff] [blame] | 1307 | prop_area* pa = __system_property_area__; |
| 1308 | |
| 1309 | if (!pa) { |
| 1310 | return -1; |
| 1311 | } |
| 1312 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1313 | uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed); |
| 1314 | serial |= 1; |
| 1315 | atomic_store_explicit(&pi->serial, serial, memory_order_relaxed); |
| 1316 | // The memcpy call here also races. Again pretend it |
| 1317 | // used memory_order_relaxed atomics, and use the analogous |
| 1318 | // counterintuitive fence. |
| 1319 | atomic_thread_fence(memory_order_release); |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1320 | strlcpy(pi->value, value, len + 1); |
| 1321 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1322 | atomic_store_explicit( |
| 1323 | &pi->serial, |
| 1324 | (len << 24) | ((serial + 1) & 0xffffff), |
| 1325 | memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1326 | __futex_wake(&pi->serial, INT32_MAX); |
| 1327 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1328 | atomic_store_explicit( |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 1329 | pa->serial(), |
| 1330 | atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1, |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1331 | memory_order_release); |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 1332 | __futex_wake(pa->serial(), INT32_MAX); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1333 | |
| 1334 | return 0; |
| 1335 | } |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 1336 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1337 | int __system_property_add(const char *name, unsigned int namelen, |
| 1338 | const char *value, unsigned int valuelen) |
| 1339 | { |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1340 | if (valuelen >= PROP_VALUE_MAX) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1341 | return -1; |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | if (namelen < 1) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1345 | return -1; |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1346 | } |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1347 | |
Tom Cherry | 6ed51c0 | 2015-12-04 11:34:42 -0800 | [diff] [blame] | 1348 | if (!__system_property_area__) { |
| 1349 | return -1; |
| 1350 | } |
| 1351 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1352 | prop_area* pa = get_prop_area_for_name(name); |
| 1353 | |
| 1354 | if (!pa) { |
| 1355 | __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property \"%s\"", name); |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 1356 | return -1; |
| 1357 | } |
| 1358 | |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1359 | bool ret = pa->add(name, namelen, value, valuelen); |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1360 | if (!ret) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1361 | return -1; |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 1362 | } |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1363 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1364 | // There is only a single mutator, but we want to make sure that |
| 1365 | // updates are visible to a reader waiting for the update. |
| 1366 | atomic_store_explicit( |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1367 | __system_property_area__->serial(), |
| 1368 | atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1, |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1369 | memory_order_release); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1370 | __futex_wake(__system_property_area__->serial(), INT32_MAX); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1371 | return 0; |
| 1372 | } |
| 1373 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1374 | // Wait for non-locked serial, and retrieve it with acquire semantics. |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1375 | unsigned int __system_property_serial(const prop_info *pi) |
| 1376 | { |
Hans Boehm | 1e8587a | 2014-08-19 14:07:55 -0700 | [diff] [blame] | 1377 | uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire); |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 1378 | while (SERIAL_DIRTY(serial)) { |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1379 | __futex_wait(const_cast<volatile void *>( |
| 1380 | reinterpret_cast<const void *>(&pi->serial)), |
| 1381 | serial, NULL); |
Hans Boehm | 1e8587a | 2014-08-19 14:07:55 -0700 | [diff] [blame] | 1382 | serial = load_const_atomic(&pi->serial, memory_order_acquire); |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 1383 | } |
| 1384 | return serial; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | unsigned int __system_property_wait_any(unsigned int serial) |
| 1388 | { |
| 1389 | prop_area *pa = __system_property_area__; |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1390 | uint32_t my_serial; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1391 | |
Tom Cherry | 6ed51c0 | 2015-12-04 11:34:42 -0800 | [diff] [blame] | 1392 | if (!pa) { |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1396 | do { |
Tom Cherry | 926ebe1 | 2015-09-23 15:34:40 -0700 | [diff] [blame] | 1397 | __futex_wait(pa->serial(), serial, NULL); |
| 1398 | my_serial = atomic_load_explicit(pa->serial(), memory_order_acquire); |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1399 | } while (my_serial == serial); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1400 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 1401 | return my_serial; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | const prop_info *__system_property_find_nth(unsigned n) |
| 1405 | { |
Dimitry Ivanov | 581b9f6 | 2017-01-09 11:05:52 -0800 | [diff] [blame] | 1406 | if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) { |
| 1407 | __libc_fatal("__system_property_find_nth is not supported since Android O," |
| 1408 | " please use __system_property_foreach instead."); |
| 1409 | } |
| 1410 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1411 | find_nth_cookie cookie(n); |
| 1412 | |
| 1413 | const int err = __system_property_foreach(find_nth_fn, &cookie); |
| 1414 | if (err < 0) { |
| 1415 | return NULL; |
| 1416 | } |
| 1417 | |
| 1418 | return cookie.pi; |
| 1419 | } |
| 1420 | |
Elliott Hughes | f8562c5 | 2017-01-26 16:48:57 -0800 | [diff] [blame] | 1421 | int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie) |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1422 | { |
Tom Cherry | 6ed51c0 | 2015-12-04 11:34:42 -0800 | [diff] [blame] | 1423 | if (!__system_property_area__) { |
| 1424 | return -1; |
| 1425 | } |
| 1426 | |
Tom Cherry | 845e24a | 2015-12-03 15:38:52 -0800 | [diff] [blame] | 1427 | list_foreach(contexts, [propfn, cookie](context_node* l) { |
Tom Cherry | b417169 | 2015-12-09 15:48:15 -0800 | [diff] [blame] | 1428 | if (l->check_access_and_open()) { |
| 1429 | l->pa()->foreach(propfn, cookie); |
Tom Cherry | 49a309f | 2015-09-23 16:09:47 -0700 | [diff] [blame] | 1430 | } |
| 1431 | }); |
| 1432 | return 0; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 1433 | } |