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