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