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