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