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> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | |
| 38 | #include <sys/mman.h> |
| 39 | |
| 40 | #include <sys/socket.h> |
| 41 | #include <sys/un.h> |
| 42 | #include <sys/select.h> |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 43 | #include <sys/stat.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | #include <sys/types.h> |
| 45 | #include <netinet/in.h> |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 46 | #include <unistd.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | |
| 48 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 49 | #include <sys/_system_properties.h> |
| 50 | |
| 51 | #include <sys/atomics.h> |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame^] | 52 | #include <bionic_atomic_inline.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 53 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 54 | struct prop_area { |
| 55 | unsigned volatile count; |
| 56 | unsigned volatile serial; |
| 57 | unsigned magic; |
| 58 | unsigned version; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 59 | unsigned toc[1]; |
| 60 | }; |
| 61 | |
| 62 | typedef struct prop_area prop_area; |
| 63 | |
| 64 | struct prop_info { |
| 65 | char name[PROP_NAME_MAX]; |
| 66 | unsigned volatile serial; |
| 67 | char value[PROP_VALUE_MAX]; |
| 68 | }; |
| 69 | |
| 70 | typedef struct prop_info prop_info; |
| 71 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 72 | static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 73 | static char property_filename[PATH_MAX] = PROP_FILENAME; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 75 | prop_area *__system_property_regions__[PA_REGION_COUNT] = { NULL, }; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 76 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 77 | static int get_fd_from_env(void) |
| 78 | { |
| 79 | char *env = getenv("ANDROID_PROPERTY_WORKSPACE"); |
| 80 | |
| 81 | if (!env) { |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | return atoi(env); |
| 86 | } |
| 87 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 88 | static int map_prop_region_rw(size_t region) |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 89 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 90 | prop_area *pa; |
| 91 | int fd; |
| 92 | size_t offset = region * PA_SIZE; |
| 93 | |
| 94 | if (__system_property_regions__[region]) { |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /* dev is a tmpfs that we can use to carve a shared workspace |
| 99 | * out of, so let's do that... |
| 100 | */ |
| 101 | fd = open(property_filename, O_RDWR | O_CREAT | O_NOFOLLOW, 0644); |
| 102 | if (fd < 0) { |
| 103 | if (errno == EACCES) { |
| 104 | /* for consistency with the case where the process has already |
| 105 | * mapped the page in and segfaults when trying to write to it |
| 106 | */ |
| 107 | abort(); |
| 108 | } |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | if (ftruncate(fd, offset + PA_SIZE) < 0) |
| 113 | goto out; |
| 114 | |
| 115 | pa = mmap(NULL, PA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); |
| 116 | if(pa == MAP_FAILED) |
| 117 | goto out; |
| 118 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 119 | memset(pa, 0, PA_SIZE); |
| 120 | pa->magic = PROP_AREA_MAGIC; |
| 121 | pa->version = PROP_AREA_VERSION; |
| 122 | |
| 123 | /* plug into the lib property services */ |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 124 | __system_property_regions__[region] = pa; |
| 125 | |
| 126 | close(fd); |
| 127 | return 0; |
| 128 | |
| 129 | out: |
| 130 | close(fd); |
| 131 | return -1; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 134 | int __system_property_set_filename(const char *filename) |
| 135 | { |
| 136 | size_t len = strlen(filename); |
| 137 | if (len >= sizeof(property_filename)) |
| 138 | return -1; |
| 139 | |
| 140 | strcpy(property_filename, filename); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int __system_property_area_init() |
| 145 | { |
| 146 | return map_prop_region_rw(0); |
| 147 | } |
| 148 | |
| 149 | static int map_prop_region(size_t region) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 150 | { |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 151 | bool fromFile = true; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 152 | bool swapped; |
| 153 | size_t offset = region * PA_SIZE; |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 154 | int result = -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 155 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 156 | if(__system_property_regions__[region]) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 160 | int fd = open(property_filename, O_RDONLY | O_NOFOLLOW); |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 161 | |
| 162 | if ((fd < 0) && (errno == ENOENT)) { |
| 163 | /* |
| 164 | * For backwards compatibility, if the file doesn't |
| 165 | * exist, we use the environment to get the file descriptor. |
| 166 | * For security reasons, we only use this backup if the kernel |
| 167 | * returns ENOENT. We don't want to use the backup if the kernel |
| 168 | * returns other errors such as ENOMEM or ENFILE, since it |
| 169 | * might be possible for an external program to trigger this |
| 170 | * condition. |
| 171 | */ |
| 172 | fd = get_fd_from_env(); |
| 173 | fromFile = false; |
| 174 | } |
| 175 | |
| 176 | if (fd < 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 177 | return -1; |
| 178 | } |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 179 | |
| 180 | struct stat fd_stat; |
| 181 | if (fstat(fd, &fd_stat) < 0) { |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 182 | goto cleanup; |
| 183 | } |
| 184 | |
| 185 | if ((fd_stat.st_uid != 0) |
| 186 | || (fd_stat.st_gid != 0) |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 187 | || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) |
| 188 | || (fd_stat.st_size < offset + PA_SIZE) ) { |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 189 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 190 | } |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 191 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 192 | prop_area *pa = mmap(NULL, PA_SIZE, PROT_READ, MAP_SHARED, fd, offset); |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 193 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 194 | if (pa == MAP_FAILED) { |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 195 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 199 | munmap(pa, PA_SIZE); |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 200 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 203 | result = 0; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 204 | swapped = __sync_bool_compare_and_swap(&__system_property_regions__[region], |
| 205 | NULL, pa); |
| 206 | if (!swapped) { |
| 207 | /** |
| 208 | * In the event of a race either mapping is equally good, so |
| 209 | * the thread that lost can just throw its mapping away and proceed as |
| 210 | * normal. |
| 211 | */ |
| 212 | munmap(pa, PA_SIZE); |
| 213 | } |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 214 | |
| 215 | cleanup: |
| 216 | if (fromFile) { |
| 217 | close(fd); |
| 218 | } |
| 219 | |
| 220 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 223 | int __system_properties_init() |
| 224 | { |
| 225 | return map_prop_region(0); |
| 226 | } |
| 227 | |
Greg Hackmann | c6ff844 | 2013-02-12 16:39:31 -0800 | [diff] [blame] | 228 | int __system_property_foreach( |
| 229 | void (*propfn)(const prop_info *pi, void *cookie), |
| 230 | void *cookie) |
| 231 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 232 | size_t region; |
Greg Hackmann | c6ff844 | 2013-02-12 16:39:31 -0800 | [diff] [blame] | 233 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 234 | for (region = 0; region < PA_REGION_COUNT; region++) { |
| 235 | prop_area *pa; |
| 236 | unsigned i; |
| 237 | |
| 238 | int err = map_prop_region(region); |
| 239 | if (err < 0) |
| 240 | break; |
| 241 | pa = __system_property_regions__[region]; |
| 242 | |
| 243 | for (i = 0; i < pa->count; i++) { |
| 244 | unsigned entry = pa->toc[i]; |
| 245 | prop_info *pi = TOC_TO_INFO(pa, entry); |
| 246 | propfn(pi, cookie); |
| 247 | } |
Greg Hackmann | c6ff844 | 2013-02-12 16:39:31 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 253 | const prop_info *__system_property_find_nth(unsigned n) |
| 254 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 255 | size_t region = n / PA_COUNT_MAX; |
| 256 | prop_area *pa; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 257 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 258 | int err = map_prop_region(region); |
| 259 | if (err < 0) |
| 260 | return NULL; |
| 261 | pa = __system_property_regions__[region]; |
| 262 | |
| 263 | if((n % PA_COUNT_MAX) >= pa->count) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 264 | return 0; |
| 265 | } else { |
| 266 | return TOC_TO_INFO(pa, pa->toc[n]); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | const prop_info *__system_property_find(const char *name) |
| 271 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 272 | unsigned len = strlen(name); |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 273 | size_t region; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 274 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 275 | if (len >= PROP_NAME_MAX) |
| 276 | return 0; |
| 277 | if (len < 1) |
| 278 | return 0; |
| 279 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 280 | for (region = 0; region < PA_REGION_COUNT; region++) { |
| 281 | prop_area *pa; |
| 282 | unsigned count; |
| 283 | unsigned *toc; |
| 284 | prop_info *pi; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 285 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 286 | int err = map_prop_region(region); |
| 287 | if (err < 0) |
| 288 | return 0; |
| 289 | pa = __system_property_regions__[region]; |
| 290 | count = pa->count; |
| 291 | toc = pa->toc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 292 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 293 | while(count--) { |
| 294 | unsigned entry = *toc++; |
| 295 | if(TOC_NAME_LEN(entry) != len) continue; |
| 296 | |
| 297 | pi = TOC_TO_INFO(pa, entry); |
| 298 | if(memcmp(name, pi->name, len)) continue; |
| 299 | |
| 300 | return pi; |
| 301 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | int __system_property_read(const prop_info *pi, char *name, char *value) |
| 308 | { |
| 309 | unsigned serial, len; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 310 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 311 | for(;;) { |
| 312 | serial = pi->serial; |
| 313 | while(SERIAL_DIRTY(serial)) { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 314 | __futex_wait((volatile void *)&pi->serial, serial, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 315 | serial = pi->serial; |
| 316 | } |
| 317 | len = SERIAL_VALUE_LEN(serial); |
| 318 | memcpy(value, pi->value, len + 1); |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame^] | 319 | ANDROID_MEMBAR_FULL(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 320 | if(serial == pi->serial) { |
| 321 | if(name != 0) { |
| 322 | strcpy(name, pi->name); |
| 323 | } |
| 324 | return len; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | int __system_property_get(const char *name, char *value) |
| 330 | { |
| 331 | const prop_info *pi = __system_property_find(name); |
| 332 | |
| 333 | if(pi != 0) { |
| 334 | return __system_property_read(pi, 0, value); |
| 335 | } else { |
| 336 | value[0] = 0; |
| 337 | return 0; |
| 338 | } |
| 339 | } |
| 340 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 341 | |
| 342 | static int send_prop_msg(prop_msg *msg) |
| 343 | { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 344 | struct pollfd pollfds[1]; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 345 | struct sockaddr_un addr; |
| 346 | socklen_t alen; |
| 347 | size_t namelen; |
| 348 | int s; |
| 349 | int r; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 350 | int result = -1; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 351 | |
| 352 | s = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 353 | if(s < 0) { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 354 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | memset(&addr, 0, sizeof(addr)); |
| 358 | namelen = strlen(property_service_socket); |
| 359 | strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path); |
| 360 | addr.sun_family = AF_LOCAL; |
| 361 | alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; |
| 362 | |
Jens Gulin | c20d0f3 | 2012-07-19 14:10:46 +0200 | [diff] [blame] | 363 | if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) { |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 364 | close(s); |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 365 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0)); |
| 369 | |
| 370 | if(r == sizeof(prop_msg)) { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 371 | // We successfully wrote to the property server but now we |
| 372 | // wait for the property server to finish its work. It |
| 373 | // acknowledges its completion by closing the socket so we |
| 374 | // poll here (on nothing), waiting for the socket to close. |
| 375 | // If you 'adb shell setprop foo bar' you'll see the POLLHUP |
| 376 | // once the socket closes. Out of paranoia we cap our poll |
| 377 | // at 250 ms. |
| 378 | pollfds[0].fd = s; |
| 379 | pollfds[0].events = 0; |
| 380 | r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */)); |
| 381 | if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) { |
| 382 | result = 0; |
Brad Fitzpatrick | 8da75ab | 2011-04-01 10:53:12 -0700 | [diff] [blame] | 383 | } else { |
| 384 | // Ignore the timeout and treat it like a success anyway. |
| 385 | // The init process is single-threaded and its property |
| 386 | // service is sometimes slow to respond (perhaps it's off |
| 387 | // starting a child process or something) and thus this |
| 388 | // times out and the caller thinks it failed, even though |
| 389 | // it's still getting around to it. So we fake it here, |
| 390 | // mostly for ctl.* properties, but we do try and wait 250 |
| 391 | // ms so callers who do read-after-write can reliably see |
| 392 | // what they've written. Most of the time. |
| 393 | // TODO: fix the system properties design. |
| 394 | result = 0; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 395 | } |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | close(s); |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 399 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | int __system_property_set(const char *key, const char *value) |
| 403 | { |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 404 | int err; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 405 | prop_msg msg; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 406 | |
| 407 | if(key == 0) return -1; |
| 408 | if(value == 0) value = ""; |
| 409 | if(strlen(key) >= PROP_NAME_MAX) return -1; |
| 410 | if(strlen(value) >= PROP_VALUE_MAX) return -1; |
| 411 | |
| 412 | memset(&msg, 0, sizeof msg); |
| 413 | msg.cmd = PROP_MSG_SETPROP; |
| 414 | strlcpy(msg.name, key, sizeof msg.name); |
| 415 | strlcpy(msg.value, value, sizeof msg.value); |
| 416 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 417 | err = send_prop_msg(&msg); |
| 418 | if(err < 0) { |
| 419 | return err; |
| 420 | } |
| 421 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 425 | int __system_property_wait(const prop_info *pi) |
| 426 | { |
| 427 | unsigned n; |
| 428 | if(pi == 0) { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 429 | prop_area *pa = __system_property_regions__[0]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 430 | n = pa->serial; |
| 431 | do { |
| 432 | __futex_wait(&pa->serial, n, 0); |
| 433 | } while(n == pa->serial); |
| 434 | } else { |
| 435 | n = pi->serial; |
| 436 | do { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 437 | __futex_wait((volatile void *)&pi->serial, n, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 438 | } while(n == pi->serial); |
| 439 | } |
| 440 | return 0; |
| 441 | } |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 442 | |
| 443 | int __system_property_update(prop_info *pi, const char *value, unsigned int len) |
| 444 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 445 | prop_area *pa = __system_property_regions__[0]; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 446 | |
| 447 | if (len >= PROP_VALUE_MAX) |
| 448 | return -1; |
| 449 | |
| 450 | pi->serial = pi->serial | 1; |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame^] | 451 | ANDROID_MEMBAR_FULL(); |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 452 | memcpy(pi->value, value, len + 1); |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame^] | 453 | ANDROID_MEMBAR_FULL(); |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 454 | pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff); |
| 455 | __futex_wake(&pi->serial, INT32_MAX); |
| 456 | |
| 457 | pa->serial++; |
| 458 | __futex_wake(&pa->serial, INT32_MAX); |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | int __system_property_add(const char *name, unsigned int namelen, |
| 464 | const char *value, unsigned int valuelen) |
| 465 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 466 | prop_area *pa; |
| 467 | prop_info *pa_info_array; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 468 | prop_info *pi; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 469 | size_t region; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 470 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 471 | if (namelen >= PROP_NAME_MAX) |
| 472 | return -1; |
| 473 | if (valuelen >= PROP_VALUE_MAX) |
| 474 | return -1; |
| 475 | if (namelen < 1) |
| 476 | return -1; |
| 477 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 478 | for (region = 0; region < PA_REGION_COUNT; region++) |
| 479 | { |
| 480 | int err = map_prop_region_rw(region); |
| 481 | if (err < 0) |
| 482 | return -1; |
| 483 | |
| 484 | pa = __system_property_regions__[region]; |
| 485 | |
| 486 | if (pa->count < PA_COUNT_MAX) |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | if (region == PA_REGION_COUNT) |
| 491 | return -1; |
| 492 | |
| 493 | pa_info_array = (void*) (((char*) pa) + PA_INFO_START); |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 494 | pi = pa_info_array + pa->count; |
| 495 | pi->serial = (valuelen << 24); |
| 496 | memcpy(pi->name, name, namelen + 1); |
| 497 | memcpy(pi->value, value, valuelen + 1); |
| 498 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 499 | pa->toc[pa->count] = (namelen << 24) | (((unsigned) pi) - ((unsigned) pa)); |
Greg Hackmann | f7511e3 | 2013-06-20 10:33:28 -0700 | [diff] [blame^] | 500 | ANDROID_MEMBAR_FULL(); |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 501 | |
| 502 | pa->count++; |
| 503 | pa->serial++; |
| 504 | __futex_wake(&pa->serial, INT32_MAX); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | unsigned int __system_property_serial(const prop_info *pi) |
| 510 | { |
| 511 | return pi->serial; |
| 512 | } |
| 513 | |
| 514 | unsigned int __system_property_wait_any(unsigned int serial) |
| 515 | { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 516 | prop_area *pa = __system_property_regions__[0]; |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 517 | |
| 518 | do { |
| 519 | __futex_wait(&pa->serial, serial, 0); |
| 520 | } while(pa->serial == serial); |
| 521 | |
| 522 | return pa->serial; |
| 523 | } |