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