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