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 | */ |
| 28 | #include <new> |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 29 | #include <stdatomic.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stddef.h> |
| 35 | #include <errno.h> |
| 36 | #include <poll.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <stdbool.h> |
| 39 | #include <string.h> |
| 40 | |
| 41 | #include <sys/mman.h> |
| 42 | |
| 43 | #include <sys/socket.h> |
| 44 | #include <sys/un.h> |
| 45 | #include <sys/select.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <sys/types.h> |
| 48 | #include <netinet/in.h> |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 49 | |
| 50 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 51 | #include <sys/_system_properties.h> |
| 52 | #include <sys/system_properties.h> |
| 53 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 54 | #include "private/bionic_atomic_inline.h" |
Elliott Hughes | d5ed63a | 2014-05-21 18:27:40 -0700 | [diff] [blame] | 55 | #include "private/bionic_futex.h" |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 56 | #include "private/bionic_macros.h" |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 57 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 58 | static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME; |
| 59 | |
| 60 | |
| 61 | /* |
| 62 | * Properties are stored in a hybrid trie/binary tree structure. |
| 63 | * Each property's name is delimited at '.' characters, and the tokens are put |
| 64 | * into a trie structure. Siblings at each level of the trie are stored in a |
| 65 | * binary tree. For instance, "ro.secure"="1" could be stored as follows: |
| 66 | * |
| 67 | * +-----+ children +----+ children +--------+ |
| 68 | * | |-------------->| ro |-------------->| secure | |
| 69 | * +-----+ +----+ +--------+ |
| 70 | * / \ / | |
| 71 | * left / \ right left / | prop +===========+ |
| 72 | * v v v +-------->| ro.secure | |
| 73 | * +-----+ +-----+ +-----+ +-----------+ |
| 74 | * | net | | sys | | com | | 1 | |
| 75 | * +-----+ +-----+ +-----+ +===========+ |
| 76 | */ |
| 77 | |
| 78 | // Represents a node in the trie. |
| 79 | struct prop_bt { |
| 80 | uint8_t namelen; |
| 81 | uint8_t reserved[3]; |
| 82 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 83 | // TODO: The following fields should be declared as atomic_uint32_t. |
| 84 | // They should be assigned to with release semantics, instead of using |
| 85 | // explicit fences. Unfortunately, the read accesses are generally |
| 86 | // followed by more dependent read accesses, and the dependence |
| 87 | // is assumed to enforce memory ordering. Which it does on supported |
| 88 | // hardware. This technically should use memory_order_consume, if |
| 89 | // that worked as intended. |
| 90 | // We should also avoid rereading these fields redundantly, since not |
| 91 | // all processor implementations ensure that multiple loads from the |
| 92 | // same field are carried out in the right order. |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 93 | volatile uint32_t prop; |
| 94 | |
| 95 | volatile uint32_t left; |
| 96 | volatile uint32_t right; |
| 97 | |
| 98 | volatile uint32_t children; |
| 99 | |
| 100 | char name[0]; |
| 101 | |
| 102 | prop_bt(const char *name, const uint8_t name_length) { |
| 103 | this->namelen = name_length; |
| 104 | memcpy(this->name, name, name_length); |
| 105 | this->name[name_length] = '\0'; |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 106 | ANDROID_MEMBAR_FULL(); // TODO: Instead use a release store |
| 107 | // for subsequent pointer assignment. |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private: |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 111 | DISALLOW_COPY_AND_ASSIGN(prop_bt); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | struct prop_area { |
| 115 | uint32_t bytes_used; |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 116 | atomic_uint_least32_t serial; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 117 | uint32_t magic; |
| 118 | uint32_t version; |
| 119 | uint32_t reserved[28]; |
| 120 | char data[0]; |
| 121 | |
| 122 | prop_area(const uint32_t magic, const uint32_t version) : |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 123 | magic(magic), version(version) { |
| 124 | atomic_init(&serial, 0); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 125 | memset(reserved, 0, sizeof(reserved)); |
| 126 | // Allocate enough space for the root node. |
| 127 | bytes_used = sizeof(prop_bt); |
| 128 | } |
| 129 | |
| 130 | private: |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 131 | DISALLOW_COPY_AND_ASSIGN(prop_area); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | struct prop_info { |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 135 | atomic_uint_least32_t serial; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 136 | char value[PROP_VALUE_MAX]; |
| 137 | char name[0]; |
| 138 | |
| 139 | prop_info(const char *name, const uint8_t namelen, const char *value, |
| 140 | const uint8_t valuelen) { |
| 141 | memcpy(this->name, name, namelen); |
| 142 | this->name[namelen] = '\0'; |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 143 | atomic_init(&this->serial, valuelen << 24); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 144 | memcpy(this->value, value, valuelen); |
| 145 | this->value[valuelen] = '\0'; |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 146 | ANDROID_MEMBAR_FULL(); // TODO: Instead use a release store |
| 147 | // for subsequent point assignment. |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 148 | } |
| 149 | private: |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(prop_info); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | struct find_nth_cookie { |
| 154 | uint32_t count; |
| 155 | const uint32_t n; |
| 156 | const prop_info *pi; |
| 157 | |
| 158 | find_nth_cookie(uint32_t n) : count(0), n(n), pi(NULL) { |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | static char property_filename[PATH_MAX] = PROP_FILENAME; |
| 163 | static bool compat_mode = false; |
| 164 | static size_t pa_data_size; |
| 165 | static size_t pa_size; |
| 166 | |
| 167 | // NOTE: This isn't static because system_properties_compat.c |
| 168 | // requires it. |
| 169 | prop_area *__system_property_area__ = NULL; |
| 170 | |
| 171 | static int get_fd_from_env(void) |
| 172 | { |
| 173 | // This environment variable consistes of two decimal integer |
| 174 | // values separated by a ",". The first value is a file descriptor |
| 175 | // and the second is the size of the system properties area. The |
| 176 | // size is currently unused. |
| 177 | char *env = getenv("ANDROID_PROPERTY_WORKSPACE"); |
| 178 | |
| 179 | if (!env) { |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | return atoi(env); |
| 184 | } |
| 185 | |
| 186 | static int map_prop_area_rw() |
| 187 | { |
| 188 | /* dev is a tmpfs that we can use to carve a shared workspace |
| 189 | * out of, so let's do that... |
| 190 | */ |
| 191 | const int fd = open(property_filename, |
| 192 | O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444); |
| 193 | |
| 194 | if (fd < 0) { |
| 195 | if (errno == EACCES) { |
| 196 | /* for consistency with the case where the process has already |
| 197 | * mapped the page in and segfaults when trying to write to it |
| 198 | */ |
| 199 | abort(); |
| 200 | } |
| 201 | return -1; |
| 202 | } |
| 203 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 204 | if (ftruncate(fd, PA_SIZE) < 0) { |
| 205 | close(fd); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | pa_size = PA_SIZE; |
| 210 | pa_data_size = pa_size - sizeof(prop_area); |
| 211 | compat_mode = false; |
| 212 | |
| 213 | void *const memory_area = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 214 | if (memory_area == MAP_FAILED) { |
| 215 | close(fd); |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | prop_area *pa = new(memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION); |
| 220 | |
| 221 | /* plug into the lib property services */ |
| 222 | __system_property_area__ = pa; |
| 223 | |
| 224 | close(fd); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int map_fd_ro(const int fd) { |
| 229 | struct stat fd_stat; |
| 230 | if (fstat(fd, &fd_stat) < 0) { |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | if ((fd_stat.st_uid != 0) |
| 235 | || (fd_stat.st_gid != 0) |
| 236 | || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) |
Narayan Kamath | 37e9570 | 2014-02-24 11:05:02 +0000 | [diff] [blame] | 237 | || (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area))) ) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | pa_size = fd_stat.st_size; |
| 242 | pa_data_size = pa_size - sizeof(prop_area); |
| 243 | |
| 244 | void* const map_result = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0); |
| 245 | if (map_result == MAP_FAILED) { |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | prop_area* pa = reinterpret_cast<prop_area*>(map_result); |
| 250 | if ((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION && |
| 251 | pa->version != PROP_AREA_VERSION_COMPAT)) { |
| 252 | munmap(pa, pa_size); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | if (pa->version == PROP_AREA_VERSION_COMPAT) { |
| 257 | compat_mode = true; |
| 258 | } |
| 259 | |
| 260 | __system_property_area__ = pa; |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int map_prop_area() |
| 265 | { |
Elliott Hughes | f73183f | 2014-08-26 16:20:59 -0700 | [diff] [blame] | 266 | int fd = open(property_filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 267 | bool close_fd = true; |
Elliott Hughes | f73183f | 2014-08-26 16:20:59 -0700 | [diff] [blame] | 268 | if (fd == -1 && errno == ENOENT) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 269 | /* |
| 270 | * For backwards compatibility, if the file doesn't |
| 271 | * exist, we use the environment to get the file descriptor. |
| 272 | * For security reasons, we only use this backup if the kernel |
| 273 | * returns ENOENT. We don't want to use the backup if the kernel |
| 274 | * returns other errors such as ENOMEM or ENFILE, since it |
| 275 | * might be possible for an external program to trigger this |
| 276 | * condition. |
| 277 | */ |
| 278 | fd = get_fd_from_env(); |
| 279 | close_fd = false; |
| 280 | } |
| 281 | |
| 282 | if (fd < 0) { |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | const int map_result = map_fd_ro(fd); |
| 287 | if (close_fd) { |
| 288 | close(fd); |
| 289 | } |
| 290 | |
| 291 | return map_result; |
| 292 | } |
| 293 | |
| 294 | static void *allocate_obj(const size_t size, uint32_t *const off) |
| 295 | { |
| 296 | prop_area *pa = __system_property_area__; |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 297 | const size_t aligned = BIONIC_ALIGN(size, sizeof(uint32_t)); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 298 | if (pa->bytes_used + aligned > pa_data_size) { |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | *off = pa->bytes_used; |
| 303 | pa->bytes_used += aligned; |
| 304 | return pa->data + *off; |
| 305 | } |
| 306 | |
| 307 | static prop_bt *new_prop_bt(const char *name, uint8_t namelen, uint32_t *const off) |
| 308 | { |
| 309 | uint32_t new_offset; |
| 310 | void *const offset = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset); |
| 311 | if (offset) { |
| 312 | prop_bt* bt = new(offset) prop_bt(name, namelen); |
| 313 | *off = new_offset; |
| 314 | return bt; |
| 315 | } |
| 316 | |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | static prop_info *new_prop_info(const char *name, uint8_t namelen, |
| 321 | const char *value, uint8_t valuelen, uint32_t *const off) |
| 322 | { |
| 323 | uint32_t off_tmp; |
| 324 | void* const offset = allocate_obj(sizeof(prop_info) + namelen + 1, &off_tmp); |
| 325 | if (offset) { |
| 326 | prop_info* info = new(offset) prop_info(name, namelen, value, valuelen); |
| 327 | *off = off_tmp; |
| 328 | return info; |
| 329 | } |
| 330 | |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | static void *to_prop_obj(const uint32_t off) |
| 335 | { |
| 336 | if (off > pa_data_size) |
| 337 | return NULL; |
| 338 | if (!__system_property_area__) |
| 339 | return NULL; |
| 340 | |
| 341 | return (__system_property_area__->data + off); |
| 342 | } |
| 343 | |
| 344 | static prop_bt *root_node() |
| 345 | { |
| 346 | return reinterpret_cast<prop_bt*>(to_prop_obj(0)); |
| 347 | } |
| 348 | |
| 349 | static int cmp_prop_name(const char *one, uint8_t one_len, const char *two, |
| 350 | uint8_t two_len) |
| 351 | { |
| 352 | if (one_len < two_len) |
| 353 | return -1; |
| 354 | else if (one_len > two_len) |
| 355 | return 1; |
| 356 | else |
| 357 | return strncmp(one, two, one_len); |
| 358 | } |
| 359 | |
| 360 | static prop_bt *find_prop_bt(prop_bt *const bt, const char *name, |
| 361 | uint8_t namelen, bool alloc_if_needed) |
| 362 | { |
| 363 | |
| 364 | prop_bt* current = bt; |
| 365 | while (true) { |
| 366 | if (!current) { |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | const int ret = cmp_prop_name(name, namelen, current->name, current->namelen); |
| 371 | if (ret == 0) { |
| 372 | return current; |
| 373 | } |
| 374 | |
| 375 | if (ret < 0) { |
| 376 | if (current->left) { |
| 377 | current = reinterpret_cast<prop_bt*>(to_prop_obj(current->left)); |
| 378 | } else { |
| 379 | if (!alloc_if_needed) { |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | // Note that there isn't a race condition here. "clients" never |
| 384 | // reach this code-path since It's only the (single threaded) server |
| 385 | // that allocates new nodes. Though "bt->left" is volatile, it can't |
| 386 | // have changed since the last value was last read. |
| 387 | uint32_t new_offset = 0; |
| 388 | prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset); |
| 389 | if (new_bt) { |
| 390 | current->left = new_offset; |
| 391 | } |
| 392 | return new_bt; |
| 393 | } |
| 394 | } else { |
| 395 | if (current->right) { |
| 396 | current = reinterpret_cast<prop_bt*>(to_prop_obj(current->right)); |
| 397 | } else { |
| 398 | if (!alloc_if_needed) { |
| 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | uint32_t new_offset; |
| 403 | prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset); |
| 404 | if (new_bt) { |
| 405 | current->right = new_offset; |
| 406 | } |
| 407 | return new_bt; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | static const prop_info *find_property(prop_bt *const trie, const char *name, |
| 414 | uint8_t namelen, const char *value, uint8_t valuelen, |
| 415 | bool alloc_if_needed) |
| 416 | { |
| 417 | if (!trie) return NULL; |
| 418 | |
| 419 | const char *remaining_name = name; |
| 420 | prop_bt* current = trie; |
| 421 | while (true) { |
| 422 | const char *sep = strchr(remaining_name, '.'); |
| 423 | const bool want_subtree = (sep != NULL); |
| 424 | const uint8_t substr_size = (want_subtree) ? |
| 425 | sep - remaining_name : strlen(remaining_name); |
| 426 | |
| 427 | if (!substr_size) { |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | prop_bt* root = NULL; |
| 432 | if (current->children) { |
| 433 | root = reinterpret_cast<prop_bt*>(to_prop_obj(current->children)); |
| 434 | } else if (alloc_if_needed) { |
| 435 | uint32_t new_bt_offset; |
| 436 | root = new_prop_bt(remaining_name, substr_size, &new_bt_offset); |
| 437 | if (root) { |
| 438 | current->children = new_bt_offset; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (!root) { |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed); |
| 447 | if (!current) { |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | if (!want_subtree) |
| 452 | break; |
| 453 | |
| 454 | remaining_name = sep + 1; |
| 455 | } |
| 456 | |
| 457 | if (current->prop) { |
| 458 | return reinterpret_cast<prop_info*>(to_prop_obj(current->prop)); |
| 459 | } else if (alloc_if_needed) { |
| 460 | uint32_t new_info_offset; |
| 461 | prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_info_offset); |
| 462 | if (new_info) { |
| 463 | current->prop = new_info_offset; |
| 464 | } |
| 465 | |
| 466 | return new_info; |
| 467 | } else { |
| 468 | return NULL; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | static int send_prop_msg(const prop_msg *msg) |
| 473 | { |
Elliott Hughes | 0dc39f9 | 2014-09-22 17:43:09 -0700 | [diff] [blame^] | 474 | const int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 475 | if (fd == -1) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | const size_t namelen = strlen(property_service_socket); |
| 480 | |
| 481 | sockaddr_un addr; |
| 482 | memset(&addr, 0, sizeof(addr)); |
| 483 | strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path)); |
| 484 | addr.sun_family = AF_LOCAL; |
| 485 | socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1; |
| 486 | if (TEMP_FAILURE_RETRY(connect(fd, reinterpret_cast<sockaddr*>(&addr), alen)) < 0) { |
| 487 | close(fd); |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | const int num_bytes = TEMP_FAILURE_RETRY(send(fd, msg, sizeof(prop_msg), 0)); |
| 492 | |
| 493 | int result = -1; |
| 494 | if (num_bytes == sizeof(prop_msg)) { |
| 495 | // We successfully wrote to the property server but now we |
| 496 | // wait for the property server to finish its work. It |
| 497 | // acknowledges its completion by closing the socket so we |
| 498 | // poll here (on nothing), waiting for the socket to close. |
| 499 | // If you 'adb shell setprop foo bar' you'll see the POLLHUP |
| 500 | // once the socket closes. Out of paranoia we cap our poll |
| 501 | // at 250 ms. |
| 502 | pollfd pollfds[1]; |
| 503 | pollfds[0].fd = fd; |
| 504 | pollfds[0].events = 0; |
| 505 | const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */)); |
| 506 | if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) { |
| 507 | result = 0; |
| 508 | } else { |
| 509 | // Ignore the timeout and treat it like a success anyway. |
| 510 | // The init process is single-threaded and its property |
| 511 | // service is sometimes slow to respond (perhaps it's off |
| 512 | // starting a child process or something) and thus this |
| 513 | // times out and the caller thinks it failed, even though |
| 514 | // it's still getting around to it. So we fake it here, |
| 515 | // mostly for ctl.* properties, but we do try and wait 250 |
| 516 | // ms so callers who do read-after-write can reliably see |
| 517 | // what they've written. Most of the time. |
| 518 | // TODO: fix the system properties design. |
| 519 | result = 0; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | close(fd); |
| 524 | return result; |
| 525 | } |
| 526 | |
| 527 | static void find_nth_fn(const prop_info *pi, void *ptr) |
| 528 | { |
| 529 | find_nth_cookie *cookie = reinterpret_cast<find_nth_cookie*>(ptr); |
| 530 | |
| 531 | if (cookie->n == cookie->count) |
| 532 | cookie->pi = pi; |
| 533 | |
| 534 | cookie->count++; |
| 535 | } |
| 536 | |
| 537 | static int foreach_property(const uint32_t off, |
| 538 | void (*propfn)(const prop_info *pi, void *cookie), void *cookie) |
| 539 | { |
| 540 | prop_bt *trie = reinterpret_cast<prop_bt*>(to_prop_obj(off)); |
| 541 | if (!trie) |
| 542 | return -1; |
| 543 | |
| 544 | if (trie->left) { |
| 545 | const int err = foreach_property(trie->left, propfn, cookie); |
| 546 | if (err < 0) |
| 547 | return -1; |
| 548 | } |
| 549 | if (trie->prop) { |
| 550 | prop_info *info = reinterpret_cast<prop_info*>(to_prop_obj(trie->prop)); |
| 551 | if (!info) |
| 552 | return -1; |
| 553 | propfn(info, cookie); |
| 554 | } |
| 555 | if (trie->children) { |
| 556 | const int err = foreach_property(trie->children, propfn, cookie); |
| 557 | if (err < 0) |
| 558 | return -1; |
| 559 | } |
| 560 | if (trie->right) { |
| 561 | const int err = foreach_property(trie->right, propfn, cookie); |
| 562 | if (err < 0) |
| 563 | return -1; |
| 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | int __system_properties_init() |
| 570 | { |
| 571 | return map_prop_area(); |
| 572 | } |
| 573 | |
| 574 | int __system_property_set_filename(const char *filename) |
| 575 | { |
| 576 | size_t len = strlen(filename); |
| 577 | if (len >= sizeof(property_filename)) |
| 578 | return -1; |
| 579 | |
| 580 | strcpy(property_filename, filename); |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | int __system_property_area_init() |
| 585 | { |
| 586 | return map_prop_area_rw(); |
| 587 | } |
| 588 | |
| 589 | const prop_info *__system_property_find(const char *name) |
| 590 | { |
| 591 | if (__predict_false(compat_mode)) { |
| 592 | return __system_property_find_compat(name); |
| 593 | } |
| 594 | return find_property(root_node(), name, strlen(name), NULL, 0, false); |
| 595 | } |
| 596 | |
Hans Boehm | 1e8587a | 2014-08-19 14:07:55 -0700 | [diff] [blame] | 597 | // The C11 standard doesn't allow atomic loads from const fields, |
| 598 | // though C++11 does. Fudge it until standards get straightened out. |
| 599 | static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, |
| 600 | memory_order mo) { |
| 601 | atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s); |
| 602 | return atomic_load_explicit(non_const_s, mo); |
| 603 | } |
| 604 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 605 | int __system_property_read(const prop_info *pi, char *name, char *value) |
| 606 | { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 607 | if (__predict_false(compat_mode)) { |
| 608 | return __system_property_read_compat(pi, name, value); |
| 609 | } |
| 610 | |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 611 | while (true) { |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 612 | uint32_t serial = __system_property_serial(pi); // acquire semantics |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 613 | size_t len = SERIAL_VALUE_LEN(serial); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 614 | memcpy(value, pi->value, len + 1); |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 615 | // TODO: Fix the synchronization scheme here. |
| 616 | // There is no fully supported way to implement this kind |
| 617 | // of synchronization in C++11, since the memcpy races with |
| 618 | // updates to pi, and the data being accessed is not atomic. |
| 619 | // The following fence is unintuitive, but would be the |
| 620 | // correct one if memcpy used memory_order_relaxed atomic accesses. |
| 621 | // In practice it seems unlikely that the generated code would |
| 622 | // would be any different, so this should be OK. |
| 623 | atomic_thread_fence(memory_order_acquire); |
| 624 | if (serial == |
Hans Boehm | 1e8587a | 2014-08-19 14:07:55 -0700 | [diff] [blame] | 625 | load_const_atomic(&(pi->serial), memory_order_relaxed)) { |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 626 | if (name != 0) { |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 627 | strcpy(name, pi->name); |
| 628 | } |
| 629 | return len; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | int __system_property_get(const char *name, char *value) |
| 635 | { |
| 636 | const prop_info *pi = __system_property_find(name); |
| 637 | |
| 638 | if (pi != 0) { |
| 639 | return __system_property_read(pi, 0, value); |
| 640 | } else { |
| 641 | value[0] = 0; |
| 642 | return 0; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | int __system_property_set(const char *key, const char *value) |
| 647 | { |
| 648 | if (key == 0) return -1; |
| 649 | if (value == 0) value = ""; |
| 650 | if (strlen(key) >= PROP_NAME_MAX) return -1; |
| 651 | if (strlen(value) >= PROP_VALUE_MAX) return -1; |
| 652 | |
| 653 | prop_msg msg; |
| 654 | memset(&msg, 0, sizeof msg); |
| 655 | msg.cmd = PROP_MSG_SETPROP; |
| 656 | strlcpy(msg.name, key, sizeof msg.name); |
| 657 | strlcpy(msg.value, value, sizeof msg.value); |
| 658 | |
| 659 | const int err = send_prop_msg(&msg); |
| 660 | if (err < 0) { |
| 661 | return err; |
| 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 667 | int __system_property_update(prop_info *pi, const char *value, unsigned int len) |
| 668 | { |
| 669 | prop_area *pa = __system_property_area__; |
| 670 | |
| 671 | if (len >= PROP_VALUE_MAX) |
| 672 | return -1; |
| 673 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 674 | uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed); |
| 675 | serial |= 1; |
| 676 | atomic_store_explicit(&pi->serial, serial, memory_order_relaxed); |
| 677 | // The memcpy call here also races. Again pretend it |
| 678 | // used memory_order_relaxed atomics, and use the analogous |
| 679 | // counterintuitive fence. |
| 680 | atomic_thread_fence(memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 681 | memcpy(pi->value, value, len + 1); |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 682 | atomic_store_explicit( |
| 683 | &pi->serial, |
| 684 | (len << 24) | ((serial + 1) & 0xffffff), |
| 685 | memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 686 | __futex_wake(&pi->serial, INT32_MAX); |
| 687 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 688 | atomic_store_explicit( |
| 689 | &pa->serial, |
| 690 | atomic_load_explicit(&pa->serial, memory_order_relaxed) + 1, |
| 691 | memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 692 | __futex_wake(&pa->serial, INT32_MAX); |
| 693 | |
| 694 | return 0; |
| 695 | } |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 696 | |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 697 | int __system_property_add(const char *name, unsigned int namelen, |
| 698 | const char *value, unsigned int valuelen) |
| 699 | { |
| 700 | prop_area *pa = __system_property_area__; |
| 701 | const prop_info *pi; |
| 702 | |
| 703 | if (namelen >= PROP_NAME_MAX) |
| 704 | return -1; |
| 705 | if (valuelen >= PROP_VALUE_MAX) |
| 706 | return -1; |
| 707 | if (namelen < 1) |
| 708 | return -1; |
| 709 | |
| 710 | pi = find_property(root_node(), name, namelen, value, valuelen, true); |
| 711 | if (!pi) |
| 712 | return -1; |
| 713 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 714 | // There is only a single mutator, but we want to make sure that |
| 715 | // updates are visible to a reader waiting for the update. |
| 716 | atomic_store_explicit( |
| 717 | &pa->serial, |
| 718 | atomic_load_explicit(&pa->serial, memory_order_relaxed) + 1, |
| 719 | memory_order_release); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 720 | __futex_wake(&pa->serial, INT32_MAX); |
| 721 | return 0; |
| 722 | } |
| 723 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 724 | // Wait for non-locked serial, and retrieve it with acquire semantics. |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 725 | unsigned int __system_property_serial(const prop_info *pi) |
| 726 | { |
Hans Boehm | 1e8587a | 2014-08-19 14:07:55 -0700 | [diff] [blame] | 727 | uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire); |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 728 | while (SERIAL_DIRTY(serial)) { |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 729 | __futex_wait(const_cast<volatile void *>( |
| 730 | reinterpret_cast<const void *>(&pi->serial)), |
| 731 | serial, NULL); |
Hans Boehm | 1e8587a | 2014-08-19 14:07:55 -0700 | [diff] [blame] | 732 | serial = load_const_atomic(&pi->serial, memory_order_acquire); |
jiaguo | 879d330 | 2014-03-13 17:39:58 +0800 | [diff] [blame] | 733 | } |
| 734 | return serial; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | unsigned int __system_property_wait_any(unsigned int serial) |
| 738 | { |
| 739 | prop_area *pa = __system_property_area__; |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 740 | uint32_t my_serial; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 741 | |
| 742 | do { |
| 743 | __futex_wait(&pa->serial, serial, NULL); |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 744 | my_serial = atomic_load_explicit(&pa->serial, memory_order_acquire); |
| 745 | } while (my_serial == serial); |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 746 | |
Hans Boehm | 30214b9 | 2014-07-31 15:53:22 -0700 | [diff] [blame] | 747 | return my_serial; |
Narayan Kamath | c9ae21a | 2014-02-19 17:59:05 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | const prop_info *__system_property_find_nth(unsigned n) |
| 751 | { |
| 752 | find_nth_cookie cookie(n); |
| 753 | |
| 754 | const int err = __system_property_foreach(find_nth_fn, &cookie); |
| 755 | if (err < 0) { |
| 756 | return NULL; |
| 757 | } |
| 758 | |
| 759 | return cookie.pi; |
| 760 | } |
| 761 | |
| 762 | int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie), |
| 763 | void *cookie) |
| 764 | { |
| 765 | if (__predict_false(compat_mode)) { |
| 766 | return __system_property_foreach_compat(propfn, cookie); |
| 767 | } |
| 768 | |
| 769 | return foreach_property(0, propfn, cookie); |
| 770 | } |