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