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> |
| 33 | |
| 34 | #include <sys/mman.h> |
| 35 | |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/un.h> |
| 38 | #include <sys/select.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <netinet/in.h> |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame^] | 41 | #include <unistd.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | |
| 43 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 44 | #include <sys/_system_properties.h> |
| 45 | |
| 46 | #include <sys/atomics.h> |
| 47 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame^] | 48 | 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] | 49 | |
| 50 | static unsigned dummy_props = 0; |
| 51 | |
| 52 | prop_area *__system_property_area__ = (void*) &dummy_props; |
| 53 | |
| 54 | int __system_properties_init(void) |
| 55 | { |
| 56 | prop_area *pa; |
| 57 | int s, fd; |
| 58 | unsigned sz; |
| 59 | char *env; |
| 60 | |
| 61 | if(__system_property_area__ != ((void*) &dummy_props)) { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | env = getenv("ANDROID_PROPERTY_WORKSPACE"); |
| 66 | if (!env) { |
| 67 | return -1; |
| 68 | } |
| 69 | fd = atoi(env); |
| 70 | env = strchr(env, ','); |
| 71 | if (!env) { |
| 72 | return -1; |
| 73 | } |
| 74 | sz = atoi(env + 1); |
| 75 | |
| 76 | pa = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0); |
| 77 | |
| 78 | if(pa == MAP_FAILED) { |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) { |
| 83 | munmap(pa, sz); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | __system_property_area__ = pa; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | const prop_info *__system_property_find_nth(unsigned n) |
| 92 | { |
| 93 | prop_area *pa = __system_property_area__; |
| 94 | |
| 95 | if(n >= pa->count) { |
| 96 | return 0; |
| 97 | } else { |
| 98 | return TOC_TO_INFO(pa, pa->toc[n]); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | const prop_info *__system_property_find(const char *name) |
| 103 | { |
| 104 | prop_area *pa = __system_property_area__; |
| 105 | unsigned count = pa->count; |
| 106 | unsigned *toc = pa->toc; |
| 107 | unsigned len = strlen(name); |
| 108 | prop_info *pi; |
| 109 | |
| 110 | while(count--) { |
| 111 | unsigned entry = *toc++; |
| 112 | if(TOC_NAME_LEN(entry) != len) continue; |
| 113 | |
| 114 | pi = TOC_TO_INFO(pa, entry); |
| 115 | if(memcmp(name, pi->name, len)) continue; |
| 116 | |
| 117 | return pi; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int __system_property_read(const prop_info *pi, char *name, char *value) |
| 124 | { |
| 125 | unsigned serial, len; |
| 126 | |
| 127 | for(;;) { |
| 128 | serial = pi->serial; |
| 129 | while(SERIAL_DIRTY(serial)) { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 130 | __futex_wait((volatile void *)&pi->serial, serial, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 131 | serial = pi->serial; |
| 132 | } |
| 133 | len = SERIAL_VALUE_LEN(serial); |
| 134 | memcpy(value, pi->value, len + 1); |
| 135 | if(serial == pi->serial) { |
| 136 | if(name != 0) { |
| 137 | strcpy(name, pi->name); |
| 138 | } |
| 139 | return len; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | int __system_property_get(const char *name, char *value) |
| 145 | { |
| 146 | const prop_info *pi = __system_property_find(name); |
| 147 | |
| 148 | if(pi != 0) { |
| 149 | return __system_property_read(pi, 0, value); |
| 150 | } else { |
| 151 | value[0] = 0; |
| 152 | return 0; |
| 153 | } |
| 154 | } |
| 155 | |
satok | ec7e8cc | 2011-03-15 11:02:26 +0900 | [diff] [blame^] | 156 | |
| 157 | static int send_prop_msg(prop_msg *msg) |
| 158 | { |
| 159 | struct sockaddr_un addr; |
| 160 | socklen_t alen; |
| 161 | size_t namelen; |
| 162 | int s; |
| 163 | int r; |
| 164 | |
| 165 | s = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 166 | if(s < 0) { |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | memset(&addr, 0, sizeof(addr)); |
| 171 | namelen = strlen(property_service_socket); |
| 172 | strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path); |
| 173 | addr.sun_family = AF_LOCAL; |
| 174 | alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; |
| 175 | |
| 176 | if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen) < 0)) { |
| 177 | close(s); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0)); |
| 182 | |
| 183 | if(r == sizeof(prop_msg)) { |
| 184 | r = 0; |
| 185 | } else { |
| 186 | r = -1; |
| 187 | } |
| 188 | |
| 189 | close(s); |
| 190 | return r; |
| 191 | } |
| 192 | |
| 193 | int __system_property_set(const char *key, const char *value) |
| 194 | { |
| 195 | unsigned old_serial; |
| 196 | volatile unsigned *serial; |
| 197 | prop_msg msg; |
| 198 | int err; |
| 199 | prop_area *pa = __system_property_area__; |
| 200 | int tries = 0; |
| 201 | int update_seen = 0; |
| 202 | |
| 203 | if(key == 0) return -1; |
| 204 | if(value == 0) value = ""; |
| 205 | if(strlen(key) >= PROP_NAME_MAX) return -1; |
| 206 | if(strlen(value) >= PROP_VALUE_MAX) return -1; |
| 207 | |
| 208 | memset(&msg, 0, sizeof msg); |
| 209 | msg.cmd = PROP_MSG_SETPROP; |
| 210 | strlcpy(msg.name, key, sizeof msg.name); |
| 211 | strlcpy(msg.value, value, sizeof msg.value); |
| 212 | |
| 213 | /* Note the system properties serial number before we do our update. */ |
| 214 | const prop_info *pi = __system_property_find(key); |
| 215 | if(pi != NULL) { |
| 216 | serial = &pi->serial; |
| 217 | } else { |
| 218 | serial = &pa->serial; |
| 219 | } |
| 220 | old_serial = *serial; |
| 221 | |
| 222 | err = send_prop_msg(&msg); |
| 223 | if(err < 0) { |
| 224 | return err; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Wait for the shared memory page to be written back and be |
| 229 | * visible in our address space before returning to the caller |
| 230 | * who might reasonably expect subsequent reads to match what was |
| 231 | * just written. |
| 232 | * |
| 233 | * Sleep 5 ms after failed checks and only wait up to a 500 ms |
| 234 | * total, just in case the system property server fails to update |
| 235 | * for whatever reason. |
| 236 | */ |
| 237 | do { |
| 238 | struct timespec timeout; |
| 239 | timeout.tv_sec = 0; |
| 240 | timeout.tv_nsec = 2500000; // 2.5 ms |
| 241 | |
| 242 | if(tries++ > 0) { |
| 243 | usleep(2500); // 2.5 ms |
| 244 | } |
| 245 | __futex_wait(serial, old_serial, &timeout); |
| 246 | if(pi != NULL) { |
| 247 | unsigned new_serial = *serial; |
| 248 | /* Waiting on a specific prop_info to be updated. */ |
| 249 | if (old_serial != new_serial && !SERIAL_DIRTY(new_serial)) { |
| 250 | update_seen = 1; |
| 251 | } |
| 252 | } else { |
| 253 | /* Waiting for a prop_info to be created. */ |
| 254 | const prop_info *new_pi = __system_property_find(key); |
| 255 | if(new_pi != NULL && !SERIAL_DIRTY(new_pi->serial)) { |
| 256 | update_seen = 1; |
| 257 | } |
| 258 | } |
| 259 | } while (!update_seen && tries < 100); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 264 | int __system_property_wait(const prop_info *pi) |
| 265 | { |
| 266 | unsigned n; |
| 267 | if(pi == 0) { |
| 268 | prop_area *pa = __system_property_area__; |
| 269 | n = pa->serial; |
| 270 | do { |
| 271 | __futex_wait(&pa->serial, n, 0); |
| 272 | } while(n == pa->serial); |
| 273 | } else { |
| 274 | n = pi->serial; |
| 275 | do { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 276 | __futex_wait((volatile void *)&pi->serial, n, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 277 | } while(n == pi->serial); |
| 278 | } |
| 279 | return 0; |
| 280 | } |