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