The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | #include <stdio.h> |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 29 | #include <stdint.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <stddef.h> |
| 33 | #include <errno.h> |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 34 | #include <poll.h> |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 35 | #include <fcntl.h> |
| 36 | #include <stdbool.h> |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 37 | #include <string.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | |
| 39 | #include <sys/mman.h> |
| 40 | |
| 41 | #include <sys/socket.h> |
| 42 | #include <sys/un.h> |
| 43 | #include <sys/select.h> |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 44 | #include <sys/stat.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | #include <sys/types.h> |
| 46 | #include <netinet/in.h> |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 47 | #include <unistd.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 48 | |
| 49 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 50 | #include <sys/_system_properties.h> |
| 51 | |
| 52 | #include <sys/atomics.h> |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame] | 53 | #include <bionic_atomic_inline.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 54 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 55 | #define ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1)) |
| 56 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 57 | struct prop_area { |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 58 | unsigned bytes_used; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 59 | unsigned volatile serial; |
| 60 | unsigned magic; |
| 61 | unsigned version; |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 62 | unsigned reserved[28]; |
| 63 | char data[0]; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | typedef struct prop_area prop_area; |
| 67 | |
| 68 | struct prop_info { |
| 69 | char name[PROP_NAME_MAX]; |
| 70 | unsigned volatile serial; |
| 71 | char value[PROP_VALUE_MAX]; |
| 72 | }; |
| 73 | |
| 74 | typedef struct prop_info prop_info; |
| 75 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 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 | typedef volatile uint32_t prop_off_t; |
| 94 | struct prop_bt { |
| 95 | char name[PROP_NAME_MAX]; |
| 96 | uint8_t namelen; |
| 97 | uint8_t reserved[3]; |
| 98 | |
| 99 | prop_off_t prop; |
| 100 | |
| 101 | prop_off_t left; |
| 102 | prop_off_t right; |
| 103 | |
| 104 | prop_off_t children; |
| 105 | }; |
| 106 | |
| 107 | typedef struct prop_bt prop_bt; |
| 108 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 109 | static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 110 | static char property_filename[PATH_MAX] = PROP_FILENAME; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 111 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 112 | prop_area *__system_property_regions__[PA_REGION_COUNT] = { NULL, }; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 113 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 114 | const size_t PA_DATA_SIZE = PA_SIZE - sizeof(prop_area); |
| 115 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 116 | static int get_fd_from_env(void) |
| 117 | { |
| 118 | char *env = getenv("ANDROID_PROPERTY_WORKSPACE"); |
| 119 | |
| 120 | if (!env) { |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | return atoi(env); |
| 125 | } |
| 126 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 127 | static int map_prop_region_rw(size_t region) |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 128 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 129 | prop_area *pa; |
| 130 | int fd; |
| 131 | size_t offset = region * PA_SIZE; |
| 132 | |
| 133 | if (__system_property_regions__[region]) { |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* dev is a tmpfs that we can use to carve a shared workspace |
| 138 | * out of, so let's do that... |
| 139 | */ |
| 140 | fd = open(property_filename, O_RDWR | O_CREAT | O_NOFOLLOW, 0644); |
| 141 | if (fd < 0) { |
| 142 | if (errno == EACCES) { |
| 143 | /* for consistency with the case where the process has already |
| 144 | * mapped the page in and segfaults when trying to write to it |
| 145 | */ |
| 146 | abort(); |
| 147 | } |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | if (ftruncate(fd, offset + PA_SIZE) < 0) |
| 152 | goto out; |
| 153 | |
| 154 | pa = mmap(NULL, PA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); |
| 155 | if(pa == MAP_FAILED) |
| 156 | goto out; |
| 157 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 158 | memset(pa, 0, PA_SIZE); |
| 159 | pa->magic = PROP_AREA_MAGIC; |
| 160 | pa->version = PROP_AREA_VERSION; |
| 161 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 162 | if (!region) { |
| 163 | /* reserve root node */ |
| 164 | pa->bytes_used += sizeof(prop_bt); |
| 165 | } |
| 166 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 167 | /* plug into the lib property services */ |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 168 | __system_property_regions__[region] = pa; |
| 169 | |
| 170 | close(fd); |
| 171 | return 0; |
| 172 | |
| 173 | out: |
| 174 | close(fd); |
| 175 | return -1; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 178 | int __system_property_set_filename(const char *filename) |
| 179 | { |
| 180 | size_t len = strlen(filename); |
| 181 | if (len >= sizeof(property_filename)) |
| 182 | return -1; |
| 183 | |
| 184 | strcpy(property_filename, filename); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | int __system_property_area_init() |
| 189 | { |
| 190 | return map_prop_region_rw(0); |
| 191 | } |
| 192 | |
| 193 | static int map_prop_region(size_t region) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 194 | { |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 195 | bool fromFile = true; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 196 | bool swapped; |
| 197 | size_t offset = region * PA_SIZE; |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 198 | int result = -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 199 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 200 | if(__system_property_regions__[region]) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 204 | int fd = open(property_filename, O_RDONLY | O_NOFOLLOW); |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 205 | |
| 206 | if ((fd < 0) && (errno == ENOENT)) { |
| 207 | /* |
| 208 | * For backwards compatibility, if the file doesn't |
| 209 | * exist, we use the environment to get the file descriptor. |
| 210 | * For security reasons, we only use this backup if the kernel |
| 211 | * returns ENOENT. We don't want to use the backup if the kernel |
| 212 | * returns other errors such as ENOMEM or ENFILE, since it |
| 213 | * might be possible for an external program to trigger this |
| 214 | * condition. |
| 215 | */ |
| 216 | fd = get_fd_from_env(); |
| 217 | fromFile = false; |
| 218 | } |
| 219 | |
| 220 | if (fd < 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 221 | return -1; |
| 222 | } |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 223 | |
| 224 | struct stat fd_stat; |
| 225 | if (fstat(fd, &fd_stat) < 0) { |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 226 | goto cleanup; |
| 227 | } |
| 228 | |
| 229 | if ((fd_stat.st_uid != 0) |
| 230 | || (fd_stat.st_gid != 0) |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 231 | || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) |
| 232 | || (fd_stat.st_size < offset + PA_SIZE) ) { |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 233 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 234 | } |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 235 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 236 | prop_area *pa = mmap(NULL, PA_SIZE, PROT_READ, MAP_SHARED, fd, offset); |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 237 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 238 | if (pa == MAP_FAILED) { |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 239 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 243 | munmap(pa, PA_SIZE); |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 244 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 247 | result = 0; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 248 | swapped = __sync_bool_compare_and_swap(&__system_property_regions__[region], |
| 249 | NULL, pa); |
| 250 | if (!swapped) { |
| 251 | /** |
| 252 | * In the event of a race either mapping is equally good, so |
| 253 | * the thread that lost can just throw its mapping away and proceed as |
| 254 | * normal. |
| 255 | */ |
| 256 | munmap(pa, PA_SIZE); |
| 257 | } |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 258 | |
| 259 | cleanup: |
| 260 | if (fromFile) { |
| 261 | close(fd); |
| 262 | } |
| 263 | |
| 264 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 267 | int __system_properties_init() |
| 268 | { |
| 269 | return map_prop_region(0); |
| 270 | } |
| 271 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 272 | static void *new_prop_obj(size_t size, prop_off_t *off) |
Greg Hackmann | c6ff844 | 2013-02-12 16:39:31 -0800 | [diff] [blame] | 273 | { |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 274 | prop_area *pa; |
| 275 | size_t i, idx; |
| 276 | size = ALIGN(size, sizeof(uint32_t)); |
Greg Hackmann | c6ff844 | 2013-02-12 16:39:31 -0800 | [diff] [blame] | 277 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 278 | for (i = 0; i < PA_REGION_COUNT; i++) { |
| 279 | int err = map_prop_region_rw(i); |
| 280 | if (err < 0) { |
| 281 | return NULL; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 282 | } |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 283 | |
| 284 | pa = __system_property_regions__[i]; |
| 285 | if (pa->bytes_used + size <= PA_DATA_SIZE) |
| 286 | break; |
Greg Hackmann | c6ff844 | 2013-02-12 16:39:31 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 289 | if (i == PA_REGION_COUNT) |
| 290 | return NULL; |
| 291 | |
| 292 | idx = pa->bytes_used; |
| 293 | *off = idx + i * PA_DATA_SIZE; |
| 294 | pa->bytes_used += size; |
| 295 | return pa->data + idx; |
Greg Hackmann | c6ff844 | 2013-02-12 16:39:31 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 298 | static prop_bt *new_prop_bt(const char *name, uint8_t namelen, prop_off_t *off) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 299 | { |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 300 | prop_off_t off_tmp; |
| 301 | prop_bt *bt = new_prop_obj(sizeof(prop_bt), &off_tmp); |
| 302 | if (bt) { |
| 303 | memcpy(bt->name, name, namelen); |
| 304 | bt->name[namelen] = '\0'; |
| 305 | bt->namelen = namelen; |
| 306 | ANDROID_MEMBAR_FULL(); |
| 307 | *off = off_tmp; |
| 308 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 309 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 310 | return bt; |
| 311 | } |
| 312 | |
| 313 | static prop_info *new_prop_info(const char *name, uint8_t namelen, |
| 314 | const char *value, uint8_t valuelen, prop_off_t *off) |
| 315 | { |
| 316 | prop_off_t off_tmp; |
| 317 | prop_info *info = new_prop_obj(sizeof(prop_info), &off_tmp); |
| 318 | if (info) { |
| 319 | memcpy(info->name, name, namelen); |
| 320 | info->name[namelen] = '\0'; |
| 321 | info->serial = (valuelen << 24); |
| 322 | memcpy(info->value, value, valuelen); |
| 323 | info->value[valuelen] = '\0'; |
| 324 | ANDROID_MEMBAR_FULL(); |
| 325 | *off = off_tmp; |
| 326 | } |
| 327 | |
| 328 | return info; |
| 329 | } |
| 330 | |
| 331 | static void *to_prop_obj(prop_off_t off) |
| 332 | { |
| 333 | size_t region = off / PA_DATA_SIZE; |
| 334 | size_t idx = off % PA_DATA_SIZE; |
| 335 | |
| 336 | if (region > PA_REGION_COUNT) |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 337 | return NULL; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 338 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 339 | if (map_prop_region(region) < 0) |
| 340 | return NULL; |
| 341 | |
| 342 | return __system_property_regions__[region]->data + idx; |
| 343 | } |
| 344 | |
| 345 | static prop_bt *root_node() |
| 346 | { |
| 347 | return to_prop_obj(0); |
| 348 | } |
| 349 | |
| 350 | static int cmp_prop_name(const char *one, uint8_t one_len, const char *two, |
| 351 | uint8_t two_len) |
| 352 | { |
| 353 | if (one_len < two_len) |
| 354 | return -1; |
| 355 | else if (one_len > two_len) |
| 356 | return 1; |
| 357 | else |
| 358 | return strncmp(one, two, one_len); |
| 359 | } |
| 360 | |
| 361 | static prop_bt *find_prop_bt(prop_bt *bt, const char *name, uint8_t namelen, |
| 362 | bool alloc_if_needed) |
| 363 | { |
| 364 | while (true) { |
| 365 | int ret; |
| 366 | if (!bt) |
| 367 | return bt; |
| 368 | ret = cmp_prop_name(name, namelen, bt->name, bt->namelen); |
| 369 | |
| 370 | if (ret == 0) { |
| 371 | return bt; |
| 372 | } else if (ret < 0) { |
| 373 | if (bt->left) { |
| 374 | bt = to_prop_obj(bt->left); |
| 375 | } else { |
| 376 | if (!alloc_if_needed) |
| 377 | return NULL; |
| 378 | |
| 379 | bt = new_prop_bt(name, namelen, &bt->left); |
| 380 | } |
| 381 | } else { |
| 382 | if (bt->right) { |
| 383 | bt = to_prop_obj(bt->right); |
| 384 | } else { |
| 385 | if (!alloc_if_needed) |
| 386 | return NULL; |
| 387 | |
| 388 | bt = new_prop_bt(name, namelen, &bt->right); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | static const prop_info *find_property(prop_bt *trie, const char *name, |
| 395 | uint8_t namelen, const char *value, uint8_t valuelen, |
| 396 | bool alloc_if_needed) |
| 397 | { |
| 398 | const char *remaining_name = name; |
| 399 | |
| 400 | while (true) { |
| 401 | char *sep = strchr(remaining_name, '.'); |
| 402 | bool want_subtree = (sep != NULL); |
| 403 | uint8_t substr_size; |
| 404 | |
| 405 | prop_bt *root; |
| 406 | |
| 407 | if (want_subtree) { |
| 408 | substr_size = sep - remaining_name; |
| 409 | } else { |
| 410 | substr_size = strlen(remaining_name); |
| 411 | } |
| 412 | |
| 413 | if (!substr_size) |
| 414 | return NULL; |
| 415 | |
| 416 | if (trie->children) { |
| 417 | root = to_prop_obj(trie->children); |
| 418 | } else if (alloc_if_needed) { |
| 419 | root = new_prop_bt(remaining_name, substr_size, &trie->children); |
| 420 | } else { |
| 421 | root = NULL; |
| 422 | } |
| 423 | |
| 424 | if (!root) |
| 425 | return NULL; |
| 426 | |
| 427 | trie = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed); |
| 428 | if (!trie) |
| 429 | return NULL; |
| 430 | |
| 431 | if (!want_subtree) |
| 432 | break; |
| 433 | |
| 434 | remaining_name = sep + 1; |
| 435 | } |
| 436 | |
| 437 | if (trie->prop) { |
| 438 | return to_prop_obj(trie->prop); |
| 439 | } else if (alloc_if_needed) { |
| 440 | return new_prop_info(name, namelen, value, valuelen, &trie->prop); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 441 | } else { |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 442 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | const prop_info *__system_property_find(const char *name) |
| 447 | { |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 448 | return find_property(root_node(), name, strlen(name), NULL, 0, false); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | int __system_property_read(const prop_info *pi, char *name, char *value) |
| 452 | { |
| 453 | unsigned serial, len; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 454 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 455 | for(;;) { |
| 456 | serial = pi->serial; |
| 457 | while(SERIAL_DIRTY(serial)) { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 458 | __futex_wait((volatile void *)&pi->serial, serial, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 459 | serial = pi->serial; |
| 460 | } |
| 461 | len = SERIAL_VALUE_LEN(serial); |
| 462 | memcpy(value, pi->value, len + 1); |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame] | 463 | ANDROID_MEMBAR_FULL(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 464 | if(serial == pi->serial) { |
| 465 | if(name != 0) { |
| 466 | strcpy(name, pi->name); |
| 467 | } |
| 468 | return len; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | int __system_property_get(const char *name, char *value) |
| 474 | { |
| 475 | const prop_info *pi = __system_property_find(name); |
| 476 | |
| 477 | if(pi != 0) { |
| 478 | return __system_property_read(pi, 0, value); |
| 479 | } else { |
| 480 | value[0] = 0; |
| 481 | return 0; |
| 482 | } |
| 483 | } |
| 484 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 485 | |
| 486 | static int send_prop_msg(prop_msg *msg) |
| 487 | { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 488 | struct pollfd pollfds[1]; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 489 | struct sockaddr_un addr; |
| 490 | socklen_t alen; |
| 491 | size_t namelen; |
| 492 | int s; |
| 493 | int r; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 494 | int result = -1; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 495 | |
| 496 | s = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 497 | if(s < 0) { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 498 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | memset(&addr, 0, sizeof(addr)); |
| 502 | namelen = strlen(property_service_socket); |
| 503 | strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path); |
| 504 | addr.sun_family = AF_LOCAL; |
| 505 | alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; |
| 506 | |
Jens Gulin | c20d0f3 | 2012-07-19 14:10:46 +0200 | [diff] [blame] | 507 | if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) { |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 508 | close(s); |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 509 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0)); |
| 513 | |
| 514 | if(r == sizeof(prop_msg)) { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 515 | // We successfully wrote to the property server but now we |
| 516 | // wait for the property server to finish its work. It |
| 517 | // acknowledges its completion by closing the socket so we |
| 518 | // poll here (on nothing), waiting for the socket to close. |
| 519 | // If you 'adb shell setprop foo bar' you'll see the POLLHUP |
| 520 | // once the socket closes. Out of paranoia we cap our poll |
| 521 | // at 250 ms. |
| 522 | pollfds[0].fd = s; |
| 523 | pollfds[0].events = 0; |
| 524 | r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */)); |
| 525 | if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) { |
| 526 | result = 0; |
Brad Fitzpatrick | 8da75ab | 2011-04-01 10:53:12 -0700 | [diff] [blame] | 527 | } else { |
| 528 | // Ignore the timeout and treat it like a success anyway. |
| 529 | // The init process is single-threaded and its property |
| 530 | // service is sometimes slow to respond (perhaps it's off |
| 531 | // starting a child process or something) and thus this |
| 532 | // times out and the caller thinks it failed, even though |
| 533 | // it's still getting around to it. So we fake it here, |
| 534 | // mostly for ctl.* properties, but we do try and wait 250 |
| 535 | // ms so callers who do read-after-write can reliably see |
| 536 | // what they've written. Most of the time. |
| 537 | // TODO: fix the system properties design. |
| 538 | result = 0; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 539 | } |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | close(s); |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 543 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | int __system_property_set(const char *key, const char *value) |
| 547 | { |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 548 | int err; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 549 | prop_msg msg; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 550 | |
| 551 | if(key == 0) return -1; |
| 552 | if(value == 0) value = ""; |
| 553 | if(strlen(key) >= PROP_NAME_MAX) return -1; |
| 554 | if(strlen(value) >= PROP_VALUE_MAX) return -1; |
| 555 | |
| 556 | memset(&msg, 0, sizeof msg); |
| 557 | msg.cmd = PROP_MSG_SETPROP; |
| 558 | strlcpy(msg.name, key, sizeof msg.name); |
| 559 | strlcpy(msg.value, value, sizeof msg.value); |
| 560 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 561 | err = send_prop_msg(&msg); |
| 562 | if(err < 0) { |
| 563 | return err; |
| 564 | } |
| 565 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 566 | return 0; |
| 567 | } |
| 568 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 569 | int __system_property_wait(const prop_info *pi) |
| 570 | { |
| 571 | unsigned n; |
| 572 | if(pi == 0) { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 573 | prop_area *pa = __system_property_regions__[0]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 574 | n = pa->serial; |
| 575 | do { |
| 576 | __futex_wait(&pa->serial, n, 0); |
| 577 | } while(n == pa->serial); |
| 578 | } else { |
| 579 | n = pi->serial; |
| 580 | do { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 581 | __futex_wait((volatile void *)&pi->serial, n, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 582 | } while(n == pi->serial); |
| 583 | } |
| 584 | return 0; |
| 585 | } |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 586 | |
| 587 | int __system_property_update(prop_info *pi, const char *value, unsigned int len) |
| 588 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 589 | prop_area *pa = __system_property_regions__[0]; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 590 | |
| 591 | if (len >= PROP_VALUE_MAX) |
| 592 | return -1; |
| 593 | |
| 594 | pi->serial = pi->serial | 1; |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame] | 595 | ANDROID_MEMBAR_FULL(); |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 596 | memcpy(pi->value, value, len + 1); |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame] | 597 | ANDROID_MEMBAR_FULL(); |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 598 | pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff); |
| 599 | __futex_wake(&pi->serial, INT32_MAX); |
| 600 | |
| 601 | pa->serial++; |
| 602 | __futex_wake(&pa->serial, INT32_MAX); |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | int __system_property_add(const char *name, unsigned int namelen, |
| 608 | const char *value, unsigned int valuelen) |
| 609 | { |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 610 | prop_area *pa = __system_property_regions__[0]; |
| 611 | const prop_info *pi; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 612 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 613 | if (namelen >= PROP_NAME_MAX) |
| 614 | return -1; |
| 615 | if (valuelen >= PROP_VALUE_MAX) |
| 616 | return -1; |
| 617 | if (namelen < 1) |
| 618 | return -1; |
| 619 | |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 620 | pi = find_property(root_node(), name, namelen, value, valuelen, true); |
| 621 | if (!pi) |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 622 | return -1; |
| 623 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 624 | pa->serial++; |
| 625 | __futex_wake(&pa->serial, INT32_MAX); |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | unsigned int __system_property_serial(const prop_info *pi) |
| 630 | { |
| 631 | return pi->serial; |
| 632 | } |
| 633 | |
| 634 | unsigned int __system_property_wait_any(unsigned int serial) |
| 635 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 636 | prop_area *pa = __system_property_regions__[0]; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 637 | |
| 638 | do { |
| 639 | __futex_wait(&pa->serial, serial, 0); |
| 640 | } while(pa->serial == serial); |
| 641 | |
| 642 | return pa->serial; |
| 643 | } |
Greg Hackmann | 996cdc4 | 2013-06-20 11:27:56 -0700 | [diff] [blame^] | 644 | |
| 645 | struct find_nth_cookie { |
| 646 | unsigned count; |
| 647 | unsigned n; |
| 648 | const prop_info *pi; |
| 649 | }; |
| 650 | |
| 651 | static void find_nth_fn(const prop_info *pi, void *ptr) |
| 652 | { |
| 653 | struct find_nth_cookie *cookie = ptr; |
| 654 | |
| 655 | if (cookie->n == cookie->count) |
| 656 | cookie->pi = pi; |
| 657 | |
| 658 | cookie->count++; |
| 659 | } |
| 660 | |
| 661 | const prop_info *__system_property_find_nth(unsigned n) |
| 662 | { |
| 663 | struct find_nth_cookie cookie; |
| 664 | int err; |
| 665 | |
| 666 | memset(&cookie, 0, sizeof(cookie)); |
| 667 | cookie.n = n; |
| 668 | |
| 669 | err = __system_property_foreach(find_nth_fn, &cookie); |
| 670 | if (err < 0) |
| 671 | return NULL; |
| 672 | |
| 673 | return cookie.pi; |
| 674 | } |
| 675 | |
| 676 | static int foreach_property(prop_off_t off, |
| 677 | void (*propfn)(const prop_info *pi, void *cookie), void *cookie) |
| 678 | { |
| 679 | prop_bt *trie = to_prop_obj(off); |
| 680 | if (!trie) |
| 681 | return -1; |
| 682 | |
| 683 | if (trie->left) { |
| 684 | int err = foreach_property(trie->left, propfn, cookie); |
| 685 | if (err < 0) |
| 686 | return -1; |
| 687 | } |
| 688 | if (trie->prop) { |
| 689 | prop_info *info = to_prop_obj(trie->prop); |
| 690 | if (!info) |
| 691 | return -1; |
| 692 | propfn(info, cookie); |
| 693 | } |
| 694 | if (trie->children) { |
| 695 | int err = foreach_property(trie->children, propfn, cookie); |
| 696 | if (err < 0) |
| 697 | return -1; |
| 698 | } |
| 699 | if (trie->right) { |
| 700 | int err = foreach_property(trie->right, propfn, cookie); |
| 701 | if (err < 0) |
| 702 | return -1; |
| 703 | } |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie), |
| 709 | void *cookie) |
| 710 | { |
| 711 | return foreach_property(0, propfn, cookie); |
| 712 | } |