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> |
| 52 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 53 | struct prop_area { |
| 54 | unsigned volatile count; |
| 55 | unsigned volatile serial; |
| 56 | unsigned magic; |
| 57 | unsigned version; |
| 58 | unsigned reserved[4]; |
| 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; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 73 | |
| 74 | static unsigned dummy_props = 0; |
| 75 | |
| 76 | prop_area *__system_property_area__ = (void*) &dummy_props; |
| 77 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 78 | static int get_fd_from_env(void) |
| 79 | { |
| 80 | char *env = getenv("ANDROID_PROPERTY_WORKSPACE"); |
| 81 | |
| 82 | if (!env) { |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | return atoi(env); |
| 87 | } |
| 88 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 89 | void __system_property_area_init(void *data) |
| 90 | { |
| 91 | prop_area *pa = data; |
| 92 | memset(pa, 0, PA_SIZE); |
| 93 | pa->magic = PROP_AREA_MAGIC; |
| 94 | pa->version = PROP_AREA_VERSION; |
| 95 | |
| 96 | /* plug into the lib property services */ |
| 97 | __system_property_area__ = pa; |
| 98 | } |
| 99 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 | int __system_properties_init(void) |
| 101 | { |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 102 | bool fromFile = true; |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 103 | int result = -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 104 | |
| 105 | if(__system_property_area__ != ((void*) &dummy_props)) { |
| 106 | return 0; |
| 107 | } |
| 108 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 109 | int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW); |
| 110 | |
| 111 | if ((fd < 0) && (errno == ENOENT)) { |
| 112 | /* |
| 113 | * For backwards compatibility, if the file doesn't |
| 114 | * exist, we use the environment to get the file descriptor. |
| 115 | * For security reasons, we only use this backup if the kernel |
| 116 | * returns ENOENT. We don't want to use the backup if the kernel |
| 117 | * returns other errors such as ENOMEM or ENFILE, since it |
| 118 | * might be possible for an external program to trigger this |
| 119 | * condition. |
| 120 | */ |
| 121 | fd = get_fd_from_env(); |
| 122 | fromFile = false; |
| 123 | } |
| 124 | |
| 125 | if (fd < 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 126 | return -1; |
| 127 | } |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 128 | |
| 129 | struct stat fd_stat; |
| 130 | if (fstat(fd, &fd_stat) < 0) { |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 131 | goto cleanup; |
| 132 | } |
| 133 | |
| 134 | if ((fd_stat.st_uid != 0) |
| 135 | || (fd_stat.st_gid != 0) |
| 136 | || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)) { |
| 137 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 138 | } |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 139 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 140 | prop_area *pa = mmap(NULL, fd_stat.st_size, PROT_READ, MAP_SHARED, fd, 0); |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 141 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 142 | if (pa == MAP_FAILED) { |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 143 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) { |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame] | 147 | munmap(pa, fd_stat.st_size); |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 148 | goto cleanup; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | __system_property_area__ = pa; |
Nick Kralevich | c16961b | 2013-01-25 13:07:31 -0800 | [diff] [blame] | 152 | result = 0; |
| 153 | |
| 154 | cleanup: |
| 155 | if (fromFile) { |
| 156 | close(fd); |
| 157 | } |
| 158 | |
| 159 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | const prop_info *__system_property_find_nth(unsigned n) |
| 163 | { |
| 164 | prop_area *pa = __system_property_area__; |
| 165 | |
| 166 | if(n >= pa->count) { |
| 167 | return 0; |
| 168 | } else { |
| 169 | return TOC_TO_INFO(pa, pa->toc[n]); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | const prop_info *__system_property_find(const char *name) |
| 174 | { |
| 175 | prop_area *pa = __system_property_area__; |
| 176 | unsigned count = pa->count; |
| 177 | unsigned *toc = pa->toc; |
| 178 | unsigned len = strlen(name); |
| 179 | prop_info *pi; |
| 180 | |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 181 | if (len >= PROP_NAME_MAX) |
| 182 | return 0; |
| 183 | if (len < 1) |
| 184 | return 0; |
| 185 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 186 | while(count--) { |
| 187 | unsigned entry = *toc++; |
| 188 | if(TOC_NAME_LEN(entry) != len) continue; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 189 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 190 | pi = TOC_TO_INFO(pa, entry); |
| 191 | if(memcmp(name, pi->name, len)) continue; |
| 192 | |
| 193 | return pi; |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | int __system_property_read(const prop_info *pi, char *name, char *value) |
| 200 | { |
| 201 | unsigned serial, len; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 202 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 203 | for(;;) { |
| 204 | serial = pi->serial; |
| 205 | while(SERIAL_DIRTY(serial)) { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 206 | __futex_wait((volatile void *)&pi->serial, serial, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 207 | serial = pi->serial; |
| 208 | } |
| 209 | len = SERIAL_VALUE_LEN(serial); |
| 210 | memcpy(value, pi->value, len + 1); |
| 211 | if(serial == pi->serial) { |
| 212 | if(name != 0) { |
| 213 | strcpy(name, pi->name); |
| 214 | } |
| 215 | return len; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | int __system_property_get(const char *name, char *value) |
| 221 | { |
| 222 | const prop_info *pi = __system_property_find(name); |
| 223 | |
| 224 | if(pi != 0) { |
| 225 | return __system_property_read(pi, 0, value); |
| 226 | } else { |
| 227 | value[0] = 0; |
| 228 | return 0; |
| 229 | } |
| 230 | } |
| 231 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 232 | |
| 233 | static int send_prop_msg(prop_msg *msg) |
| 234 | { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 235 | struct pollfd pollfds[1]; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 236 | struct sockaddr_un addr; |
| 237 | socklen_t alen; |
| 238 | size_t namelen; |
| 239 | int s; |
| 240 | int r; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 241 | int result = -1; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 242 | |
| 243 | s = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 244 | if(s < 0) { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 245 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | memset(&addr, 0, sizeof(addr)); |
| 249 | namelen = strlen(property_service_socket); |
| 250 | strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path); |
| 251 | addr.sun_family = AF_LOCAL; |
| 252 | alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; |
| 253 | |
Jens Gulin | c20d0f3 | 2012-07-19 14:10:46 +0200 | [diff] [blame] | 254 | if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) { |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 255 | close(s); |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 256 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0)); |
| 260 | |
| 261 | if(r == sizeof(prop_msg)) { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 262 | // We successfully wrote to the property server but now we |
| 263 | // wait for the property server to finish its work. It |
| 264 | // acknowledges its completion by closing the socket so we |
| 265 | // poll here (on nothing), waiting for the socket to close. |
| 266 | // If you 'adb shell setprop foo bar' you'll see the POLLHUP |
| 267 | // once the socket closes. Out of paranoia we cap our poll |
| 268 | // at 250 ms. |
| 269 | pollfds[0].fd = s; |
| 270 | pollfds[0].events = 0; |
| 271 | r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */)); |
| 272 | if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) { |
| 273 | result = 0; |
Brad Fitzpatrick | 8da75ab | 2011-04-01 10:53:12 -0700 | [diff] [blame] | 274 | } else { |
| 275 | // Ignore the timeout and treat it like a success anyway. |
| 276 | // The init process is single-threaded and its property |
| 277 | // service is sometimes slow to respond (perhaps it's off |
| 278 | // starting a child process or something) and thus this |
| 279 | // times out and the caller thinks it failed, even though |
| 280 | // it's still getting around to it. So we fake it here, |
| 281 | // mostly for ctl.* properties, but we do try and wait 250 |
| 282 | // ms so callers who do read-after-write can reliably see |
| 283 | // what they've written. Most of the time. |
| 284 | // TODO: fix the system properties design. |
| 285 | result = 0; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 286 | } |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | close(s); |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 290 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | int __system_property_set(const char *key, const char *value) |
| 294 | { |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 295 | int err; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 296 | prop_msg msg; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 297 | |
| 298 | if(key == 0) return -1; |
| 299 | if(value == 0) value = ""; |
| 300 | if(strlen(key) >= PROP_NAME_MAX) return -1; |
| 301 | if(strlen(value) >= PROP_VALUE_MAX) return -1; |
| 302 | |
| 303 | memset(&msg, 0, sizeof msg); |
| 304 | msg.cmd = PROP_MSG_SETPROP; |
| 305 | strlcpy(msg.name, key, sizeof msg.name); |
| 306 | strlcpy(msg.value, value, sizeof msg.value); |
| 307 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 308 | err = send_prop_msg(&msg); |
| 309 | if(err < 0) { |
| 310 | return err; |
| 311 | } |
| 312 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 316 | int __system_property_wait(const prop_info *pi) |
| 317 | { |
| 318 | unsigned n; |
| 319 | if(pi == 0) { |
| 320 | prop_area *pa = __system_property_area__; |
| 321 | n = pa->serial; |
| 322 | do { |
| 323 | __futex_wait(&pa->serial, n, 0); |
| 324 | } while(n == pa->serial); |
| 325 | } else { |
| 326 | n = pi->serial; |
| 327 | do { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 328 | __futex_wait((volatile void *)&pi->serial, n, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 329 | } while(n == pi->serial); |
| 330 | } |
| 331 | return 0; |
| 332 | } |
Colin Cross | 5cf32de | 2013-01-23 23:07:06 -0800 | [diff] [blame] | 333 | |
| 334 | int __system_property_update(prop_info *pi, const char *value, unsigned int len) |
| 335 | { |
| 336 | prop_area *pa = __system_property_area__; |
| 337 | |
| 338 | if (len >= PROP_VALUE_MAX) |
| 339 | return -1; |
| 340 | |
| 341 | pi->serial = pi->serial | 1; |
| 342 | memcpy(pi->value, value, len + 1); |
| 343 | pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff); |
| 344 | __futex_wake(&pi->serial, INT32_MAX); |
| 345 | |
| 346 | pa->serial++; |
| 347 | __futex_wake(&pa->serial, INT32_MAX); |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | int __system_property_add(const char *name, unsigned int namelen, |
| 353 | const char *value, unsigned int valuelen) |
| 354 | { |
| 355 | prop_area *pa = __system_property_area__; |
| 356 | prop_info *pa_info_array = (void*) (((char*) pa) + PA_INFO_START); |
| 357 | prop_info *pi; |
| 358 | |
| 359 | if (pa->count == PA_COUNT_MAX) |
| 360 | return -1; |
| 361 | if (namelen >= PROP_NAME_MAX) |
| 362 | return -1; |
| 363 | if (valuelen >= PROP_VALUE_MAX) |
| 364 | return -1; |
| 365 | if (namelen < 1) |
| 366 | return -1; |
| 367 | |
| 368 | pi = pa_info_array + pa->count; |
| 369 | pi->serial = (valuelen << 24); |
| 370 | memcpy(pi->name, name, namelen + 1); |
| 371 | memcpy(pi->value, value, valuelen + 1); |
| 372 | |
| 373 | pa->toc[pa->count] = |
| 374 | (namelen << 24) | (((unsigned) pi) - ((unsigned) pa)); |
| 375 | |
| 376 | pa->count++; |
| 377 | pa->serial++; |
| 378 | __futex_wake(&pa->serial, INT32_MAX); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | unsigned int __system_property_serial(const prop_info *pi) |
| 384 | { |
| 385 | return pi->serial; |
| 386 | } |
| 387 | |
| 388 | unsigned int __system_property_wait_any(unsigned int serial) |
| 389 | { |
| 390 | prop_area *pa = __system_property_area__; |
| 391 | |
| 392 | do { |
| 393 | __futex_wait(&pa->serial, serial, 0); |
| 394 | } while(pa->serial == serial); |
| 395 | |
| 396 | return pa->serial; |
| 397 | } |