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> |
| 29 | #include <stdlib.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stddef.h> |
| 32 | #include <errno.h> |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 33 | #include <poll.h> |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame^] | 34 | #include <fcntl.h> |
| 35 | #include <stdbool.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | |
| 37 | #include <sys/mman.h> |
| 38 | |
| 39 | #include <sys/socket.h> |
| 40 | #include <sys/un.h> |
| 41 | #include <sys/select.h> |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame^] | 42 | #include <sys/stat.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include <netinet/in.h> |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 45 | #include <unistd.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 | |
| 47 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 48 | #include <sys/_system_properties.h> |
| 49 | |
| 50 | #include <sys/atomics.h> |
| 51 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 52 | 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] | 53 | |
| 54 | static unsigned dummy_props = 0; |
| 55 | |
| 56 | prop_area *__system_property_area__ = (void*) &dummy_props; |
| 57 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame^] | 58 | static int get_fd_from_env(void) |
| 59 | { |
| 60 | char *env = getenv("ANDROID_PROPERTY_WORKSPACE"); |
| 61 | |
| 62 | if (!env) { |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | return atoi(env); |
| 67 | } |
| 68 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 69 | int __system_properties_init(void) |
| 70 | { |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame^] | 71 | bool fromFile = true; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | |
| 73 | if(__system_property_area__ != ((void*) &dummy_props)) { |
| 74 | return 0; |
| 75 | } |
| 76 | |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame^] | 77 | int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW); |
| 78 | |
| 79 | if ((fd < 0) && (errno == ENOENT)) { |
| 80 | /* |
| 81 | * For backwards compatibility, if the file doesn't |
| 82 | * exist, we use the environment to get the file descriptor. |
| 83 | * For security reasons, we only use this backup if the kernel |
| 84 | * returns ENOENT. We don't want to use the backup if the kernel |
| 85 | * returns other errors such as ENOMEM or ENFILE, since it |
| 86 | * might be possible for an external program to trigger this |
| 87 | * condition. |
| 88 | */ |
| 89 | fd = get_fd_from_env(); |
| 90 | fromFile = false; |
| 91 | } |
| 92 | |
| 93 | if (fd < 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 94 | return -1; |
| 95 | } |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame^] | 96 | |
| 97 | struct stat fd_stat; |
| 98 | if (fstat(fd, &fd_stat) < 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 99 | return -1; |
| 100 | } |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame^] | 101 | |
| 102 | prop_area *pa = mmap(0, fd_stat.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 103 | |
| 104 | if (fromFile) { |
| 105 | close(fd); |
| 106 | } |
| 107 | |
| 108 | if (pa == MAP_FAILED) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) { |
Nick Kralevich | 32417fb | 2013-01-23 09:28:35 -0800 | [diff] [blame^] | 113 | munmap(pa, fd_stat.st_size); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | __system_property_area__ = pa; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | const prop_info *__system_property_find_nth(unsigned n) |
| 122 | { |
| 123 | prop_area *pa = __system_property_area__; |
| 124 | |
| 125 | if(n >= pa->count) { |
| 126 | return 0; |
| 127 | } else { |
| 128 | return TOC_TO_INFO(pa, pa->toc[n]); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | const prop_info *__system_property_find(const char *name) |
| 133 | { |
| 134 | prop_area *pa = __system_property_area__; |
| 135 | unsigned count = pa->count; |
| 136 | unsigned *toc = pa->toc; |
| 137 | unsigned len = strlen(name); |
| 138 | prop_info *pi; |
| 139 | |
| 140 | while(count--) { |
| 141 | unsigned entry = *toc++; |
| 142 | if(TOC_NAME_LEN(entry) != len) continue; |
| 143 | |
| 144 | pi = TOC_TO_INFO(pa, entry); |
| 145 | if(memcmp(name, pi->name, len)) continue; |
| 146 | |
| 147 | return pi; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int __system_property_read(const prop_info *pi, char *name, char *value) |
| 154 | { |
| 155 | unsigned serial, len; |
| 156 | |
| 157 | for(;;) { |
| 158 | serial = pi->serial; |
| 159 | while(SERIAL_DIRTY(serial)) { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 160 | __futex_wait((volatile void *)&pi->serial, serial, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 161 | serial = pi->serial; |
| 162 | } |
| 163 | len = SERIAL_VALUE_LEN(serial); |
| 164 | memcpy(value, pi->value, len + 1); |
| 165 | if(serial == pi->serial) { |
| 166 | if(name != 0) { |
| 167 | strcpy(name, pi->name); |
| 168 | } |
| 169 | return len; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | int __system_property_get(const char *name, char *value) |
| 175 | { |
| 176 | const prop_info *pi = __system_property_find(name); |
| 177 | |
| 178 | if(pi != 0) { |
| 179 | return __system_property_read(pi, 0, value); |
| 180 | } else { |
| 181 | value[0] = 0; |
| 182 | return 0; |
| 183 | } |
| 184 | } |
| 185 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 186 | |
| 187 | static int send_prop_msg(prop_msg *msg) |
| 188 | { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 189 | struct pollfd pollfds[1]; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 190 | struct sockaddr_un addr; |
| 191 | socklen_t alen; |
| 192 | size_t namelen; |
| 193 | int s; |
| 194 | int r; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 195 | int result = -1; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 196 | |
| 197 | s = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 198 | if(s < 0) { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 199 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | memset(&addr, 0, sizeof(addr)); |
| 203 | namelen = strlen(property_service_socket); |
| 204 | strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path); |
| 205 | addr.sun_family = AF_LOCAL; |
| 206 | alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; |
| 207 | |
Jens Gulin | c20d0f3 | 2012-07-19 14:10:46 +0200 | [diff] [blame] | 208 | if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) { |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 209 | close(s); |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 210 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0)); |
| 214 | |
| 215 | if(r == sizeof(prop_msg)) { |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 216 | // We successfully wrote to the property server but now we |
| 217 | // wait for the property server to finish its work. It |
| 218 | // acknowledges its completion by closing the socket so we |
| 219 | // poll here (on nothing), waiting for the socket to close. |
| 220 | // If you 'adb shell setprop foo bar' you'll see the POLLHUP |
| 221 | // once the socket closes. Out of paranoia we cap our poll |
| 222 | // at 250 ms. |
| 223 | pollfds[0].fd = s; |
| 224 | pollfds[0].events = 0; |
| 225 | r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */)); |
| 226 | if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) { |
| 227 | result = 0; |
Brad Fitzpatrick | 8da75ab | 2011-04-01 10:53:12 -0700 | [diff] [blame] | 228 | } else { |
| 229 | // Ignore the timeout and treat it like a success anyway. |
| 230 | // The init process is single-threaded and its property |
| 231 | // service is sometimes slow to respond (perhaps it's off |
| 232 | // starting a child process or something) and thus this |
| 233 | // times out and the caller thinks it failed, even though |
| 234 | // it's still getting around to it. So we fake it here, |
| 235 | // mostly for ctl.* properties, but we do try and wait 250 |
| 236 | // ms so callers who do read-after-write can reliably see |
| 237 | // what they've written. Most of the time. |
| 238 | // TODO: fix the system properties design. |
| 239 | result = 0; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 240 | } |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | close(s); |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 244 | return result; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | int __system_property_set(const char *key, const char *value) |
| 248 | { |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 249 | int err; |
Brad Fitzpatrick | 23bc3ff | 2011-03-30 13:10:04 -0700 | [diff] [blame] | 250 | prop_msg msg; |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 251 | |
| 252 | if(key == 0) return -1; |
| 253 | if(value == 0) value = ""; |
| 254 | if(strlen(key) >= PROP_NAME_MAX) return -1; |
| 255 | if(strlen(value) >= PROP_VALUE_MAX) return -1; |
| 256 | |
| 257 | memset(&msg, 0, sizeof msg); |
| 258 | msg.cmd = PROP_MSG_SETPROP; |
| 259 | strlcpy(msg.name, key, sizeof msg.name); |
| 260 | strlcpy(msg.value, value, sizeof msg.value); |
| 261 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 262 | err = send_prop_msg(&msg); |
| 263 | if(err < 0) { |
| 264 | return err; |
| 265 | } |
| 266 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 270 | int __system_property_wait(const prop_info *pi) |
| 271 | { |
| 272 | unsigned n; |
| 273 | if(pi == 0) { |
| 274 | prop_area *pa = __system_property_area__; |
| 275 | n = pa->serial; |
| 276 | do { |
| 277 | __futex_wait(&pa->serial, n, 0); |
| 278 | } while(n == pa->serial); |
| 279 | } else { |
| 280 | n = pi->serial; |
| 281 | do { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 282 | __futex_wait((volatile void *)&pi->serial, n, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 283 | } while(n == pi->serial); |
| 284 | } |
| 285 | return 0; |
| 286 | } |