blob: 0d421956e12162e26222d12097201aea7a43fc26 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Cross5cf32de2013-01-23 23:07:06 -080029#include <stdint.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030#include <stdlib.h>
31#include <unistd.h>
32#include <stddef.h>
33#include <errno.h>
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -070034#include <poll.h>
Nick Kralevich32417fb2013-01-23 09:28:35 -080035#include <fcntl.h>
36#include <stdbool.h>
Greg Hackmann996cdc42013-06-20 11:27:56 -070037#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038
39#include <sys/mman.h>
40
41#include <sys/socket.h>
42#include <sys/un.h>
43#include <sys/select.h>
Nick Kralevich32417fb2013-01-23 09:28:35 -080044#include <sys/stat.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045#include <sys/types.h>
46#include <netinet/in.h>
satokec7e8cc2011-03-15 11:02:26 +090047#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
49#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
50#include <sys/_system_properties.h>
51
52#include <sys/atomics.h>
Greg Hackmannf7511e32013-06-20 10:33:28 -070053#include <bionic_atomic_inline.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054
Greg Hackmann996cdc42013-06-20 11:27:56 -070055#define ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))
56
Colin Cross5cf32de2013-01-23 23:07:06 -080057struct prop_area {
Greg Hackmann996cdc42013-06-20 11:27:56 -070058 unsigned bytes_used;
Colin Cross5cf32de2013-01-23 23:07:06 -080059 unsigned volatile serial;
60 unsigned magic;
61 unsigned version;
Greg Hackmann996cdc42013-06-20 11:27:56 -070062 unsigned reserved[28];
63 char data[0];
Colin Cross5cf32de2013-01-23 23:07:06 -080064};
65
66typedef struct prop_area prop_area;
67
68struct prop_info {
Colin Cross5cf32de2013-01-23 23:07:06 -080069 unsigned volatile serial;
70 char value[PROP_VALUE_MAX];
Greg Hackmann836dbf62013-06-21 13:02:38 -070071 char name[0];
Colin Cross5cf32de2013-01-23 23:07:06 -080072};
73
74typedef struct prop_info prop_info;
75
Greg Hackmann996cdc42013-06-20 11:27:56 -070076/*
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
93typedef volatile uint32_t prop_off_t;
94struct prop_bt {
Greg Hackmann996cdc42013-06-20 11:27:56 -070095 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 Hackmann836dbf62013-06-21 13:02:38 -0700104
105 char name[0];
Greg Hackmann996cdc42013-06-20 11:27:56 -0700106};
107
108typedef struct prop_bt prop_bt;
109
satokec7e8cc2011-03-15 11:02:26 +0900110static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800111static char property_filename[PATH_MAX] = PROP_FILENAME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
Greg Hackmann1540f602013-06-19 13:31:21 -0700113prop_area *__system_property_area__ = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Colin Cross1ec20a02013-06-24 18:42:21 -0700115size_t pa_data_size;
116size_t pa_size;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700117
Nick Kralevich32417fb2013-01-23 09:28:35 -0800118static int get_fd_from_env(void)
119{
120 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
121
122 if (!env) {
123 return -1;
124 }
125
126 return atoi(env);
127}
128
Greg Hackmann1540f602013-06-19 13:31:21 -0700129static int map_prop_area_rw()
Colin Cross5cf32de2013-01-23 23:07:06 -0800130{
Greg Hackmanncb215a72013-02-13 14:41:48 -0800131 prop_area *pa;
132 int fd;
Colin Cross1d36ee12013-06-16 10:19:16 -0700133 int ret;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800134
135 /* dev is a tmpfs that we can use to carve a shared workspace
136 * out of, so let's do that...
137 */
Colin Cross1d36ee12013-06-16 10:19:16 -0700138 fd = open(property_filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC |
139 O_EXCL, 0444);
Greg Hackmanncb215a72013-02-13 14:41:48 -0800140 if (fd < 0) {
141 if (errno == EACCES) {
142 /* for consistency with the case where the process has already
143 * mapped the page in and segfaults when trying to write to it
144 */
145 abort();
146 }
147 return -1;
148 }
149
Colin Cross1d36ee12013-06-16 10:19:16 -0700150 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
151 if (ret < 0)
152 goto out;
153
Greg Hackmann1540f602013-06-19 13:31:21 -0700154 if (ftruncate(fd, PA_SIZE) < 0)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800155 goto out;
156
Colin Cross1ec20a02013-06-24 18:42:21 -0700157 pa_size = PA_SIZE;
158 pa_data_size = pa_size - sizeof(prop_area);
159
160 pa = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Greg Hackmanncb215a72013-02-13 14:41:48 -0800161 if(pa == MAP_FAILED)
162 goto out;
163
Colin Cross1ec20a02013-06-24 18:42:21 -0700164 memset(pa, 0, pa_size);
Colin Cross5cf32de2013-01-23 23:07:06 -0800165 pa->magic = PROP_AREA_MAGIC;
166 pa->version = PROP_AREA_VERSION;
Greg Hackmann1540f602013-06-19 13:31:21 -0700167 /* reserve root node */
168 pa->bytes_used = sizeof(prop_bt);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700169
Colin Cross5cf32de2013-01-23 23:07:06 -0800170 /* plug into the lib property services */
Greg Hackmann1540f602013-06-19 13:31:21 -0700171 __system_property_area__ = pa;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800172
173 close(fd);
174 return 0;
175
176out:
177 close(fd);
178 return -1;
Colin Cross5cf32de2013-01-23 23:07:06 -0800179}
180
Greg Hackmanncb215a72013-02-13 14:41:48 -0800181int __system_property_set_filename(const char *filename)
182{
183 size_t len = strlen(filename);
184 if (len >= sizeof(property_filename))
185 return -1;
186
187 strcpy(property_filename, filename);
188 return 0;
189}
190
191int __system_property_area_init()
192{
Greg Hackmann1540f602013-06-19 13:31:21 -0700193 return map_prop_area_rw();
Greg Hackmanncb215a72013-02-13 14:41:48 -0800194}
195
Greg Hackmann1540f602013-06-19 13:31:21 -0700196static int map_prop_area()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197{
Nick Kralevich32417fb2013-01-23 09:28:35 -0800198 bool fromFile = true;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800199 int result = -1;
Colin Cross1d36ee12013-06-16 10:19:16 -0700200 int fd;
201 int ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202
Colin Cross1d36ee12013-06-16 10:19:16 -0700203 fd = open(property_filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
204 if (fd >= 0) {
205 /* For old kernels that don't support O_CLOEXEC */
206 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
207 if (ret < 0)
208 goto cleanup;
209 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800210
211 if ((fd < 0) && (errno == ENOENT)) {
212 /*
213 * For backwards compatibility, if the file doesn't
214 * exist, we use the environment to get the file descriptor.
215 * For security reasons, we only use this backup if the kernel
216 * returns ENOENT. We don't want to use the backup if the kernel
217 * returns other errors such as ENOMEM or ENFILE, since it
218 * might be possible for an external program to trigger this
219 * condition.
220 */
221 fd = get_fd_from_env();
222 fromFile = false;
223 }
224
225 if (fd < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226 return -1;
227 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800228
229 struct stat fd_stat;
230 if (fstat(fd, &fd_stat) < 0) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800231 goto cleanup;
232 }
233
234 if ((fd_stat.st_uid != 0)
235 || (fd_stat.st_gid != 0)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800236 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
Colin Cross1ec20a02013-06-24 18:42:21 -0700237 || (fd_stat.st_size < sizeof(prop_area)) ) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800238 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800240
Colin Cross1ec20a02013-06-24 18:42:21 -0700241 pa_size = fd_stat.st_size;
242 pa_data_size = pa_size - sizeof(prop_area);
243 prop_area *pa = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0);
Nick Kralevich32417fb2013-01-23 09:28:35 -0800244
Nick Kralevich32417fb2013-01-23 09:28:35 -0800245 if (pa == MAP_FAILED) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800246 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800247 }
248
249 if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) {
Colin Cross1ec20a02013-06-24 18:42:21 -0700250 munmap(pa, pa_size);
Nick Kralevichc16961b2013-01-25 13:07:31 -0800251 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252 }
253
Nick Kralevichc16961b2013-01-25 13:07:31 -0800254 result = 0;
Greg Hackmann1540f602013-06-19 13:31:21 -0700255
256 __system_property_area__ = pa;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800257
258cleanup:
259 if (fromFile) {
260 close(fd);
261 }
262
263 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264}
265
Greg Hackmanncb215a72013-02-13 14:41:48 -0800266int __system_properties_init()
267{
Greg Hackmann1540f602013-06-19 13:31:21 -0700268 return map_prop_area();
Greg Hackmanncb215a72013-02-13 14:41:48 -0800269}
270
Greg Hackmann996cdc42013-06-20 11:27:56 -0700271static void *new_prop_obj(size_t size, prop_off_t *off)
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800272{
Greg Hackmann1540f602013-06-19 13:31:21 -0700273 prop_area *pa = __system_property_area__;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700274 size = ALIGN(size, sizeof(uint32_t));
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800275
Colin Cross1ec20a02013-06-24 18:42:21 -0700276 if (pa->bytes_used + size > pa_data_size)
Greg Hackmann996cdc42013-06-20 11:27:56 -0700277 return NULL;
278
Greg Hackmann1540f602013-06-19 13:31:21 -0700279 *off = pa->bytes_used;
280 __system_property_area__->bytes_used += size;
281 return __system_property_area__->data + *off;
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800282}
283
Greg Hackmann996cdc42013-06-20 11:27:56 -0700284static prop_bt *new_prop_bt(const char *name, uint8_t namelen, prop_off_t *off)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285{
Greg Hackmann996cdc42013-06-20 11:27:56 -0700286 prop_off_t off_tmp;
Greg Hackmann836dbf62013-06-21 13:02:38 -0700287 prop_bt *bt = new_prop_obj(sizeof(prop_bt) + namelen + 1, &off_tmp);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700288 if (bt) {
289 memcpy(bt->name, name, namelen);
290 bt->name[namelen] = '\0';
291 bt->namelen = namelen;
292 ANDROID_MEMBAR_FULL();
293 *off = off_tmp;
294 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800295
Greg Hackmann996cdc42013-06-20 11:27:56 -0700296 return bt;
297}
298
299static prop_info *new_prop_info(const char *name, uint8_t namelen,
300 const char *value, uint8_t valuelen, prop_off_t *off)
301{
302 prop_off_t off_tmp;
Greg Hackmann836dbf62013-06-21 13:02:38 -0700303 prop_info *info = new_prop_obj(sizeof(prop_info) + namelen + 1, &off_tmp);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700304 if (info) {
305 memcpy(info->name, name, namelen);
306 info->name[namelen] = '\0';
307 info->serial = (valuelen << 24);
308 memcpy(info->value, value, valuelen);
309 info->value[valuelen] = '\0';
310 ANDROID_MEMBAR_FULL();
311 *off = off_tmp;
312 }
313
314 return info;
315}
316
317static void *to_prop_obj(prop_off_t off)
318{
Colin Cross1ec20a02013-06-24 18:42:21 -0700319 if (off > pa_data_size)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800320 return NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800321
Greg Hackmann1540f602013-06-19 13:31:21 -0700322 return __system_property_area__->data + off;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700323}
324
325static prop_bt *root_node()
326{
327 return to_prop_obj(0);
328}
329
330static int cmp_prop_name(const char *one, uint8_t one_len, const char *two,
331 uint8_t two_len)
332{
333 if (one_len < two_len)
334 return -1;
335 else if (one_len > two_len)
336 return 1;
337 else
338 return strncmp(one, two, one_len);
339}
340
341static prop_bt *find_prop_bt(prop_bt *bt, const char *name, uint8_t namelen,
342 bool alloc_if_needed)
343{
344 while (true) {
345 int ret;
346 if (!bt)
347 return bt;
348 ret = cmp_prop_name(name, namelen, bt->name, bt->namelen);
349
350 if (ret == 0) {
351 return bt;
352 } else if (ret < 0) {
353 if (bt->left) {
354 bt = to_prop_obj(bt->left);
355 } else {
356 if (!alloc_if_needed)
357 return NULL;
358
359 bt = new_prop_bt(name, namelen, &bt->left);
360 }
361 } else {
362 if (bt->right) {
363 bt = to_prop_obj(bt->right);
364 } else {
365 if (!alloc_if_needed)
366 return NULL;
367
368 bt = new_prop_bt(name, namelen, &bt->right);
369 }
370 }
371 }
372}
373
374static const prop_info *find_property(prop_bt *trie, const char *name,
375 uint8_t namelen, const char *value, uint8_t valuelen,
376 bool alloc_if_needed)
377{
378 const char *remaining_name = name;
379
380 while (true) {
381 char *sep = strchr(remaining_name, '.');
382 bool want_subtree = (sep != NULL);
383 uint8_t substr_size;
384
385 prop_bt *root;
386
387 if (want_subtree) {
388 substr_size = sep - remaining_name;
389 } else {
390 substr_size = strlen(remaining_name);
391 }
392
393 if (!substr_size)
394 return NULL;
395
396 if (trie->children) {
397 root = to_prop_obj(trie->children);
398 } else if (alloc_if_needed) {
399 root = new_prop_bt(remaining_name, substr_size, &trie->children);
400 } else {
401 root = NULL;
402 }
403
404 if (!root)
405 return NULL;
406
407 trie = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
408 if (!trie)
409 return NULL;
410
411 if (!want_subtree)
412 break;
413
414 remaining_name = sep + 1;
415 }
416
417 if (trie->prop) {
418 return to_prop_obj(trie->prop);
419 } else if (alloc_if_needed) {
420 return new_prop_info(name, namelen, value, valuelen, &trie->prop);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800421 } else {
Greg Hackmann996cdc42013-06-20 11:27:56 -0700422 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423 }
424}
425
426const prop_info *__system_property_find(const char *name)
427{
Greg Hackmann996cdc42013-06-20 11:27:56 -0700428 return find_property(root_node(), name, strlen(name), NULL, 0, false);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429}
430
431int __system_property_read(const prop_info *pi, char *name, char *value)
432{
433 unsigned serial, len;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700434
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435 for(;;) {
436 serial = pi->serial;
437 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700438 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800439 serial = pi->serial;
440 }
441 len = SERIAL_VALUE_LEN(serial);
442 memcpy(value, pi->value, len + 1);
Greg Hackmannf7511e32013-06-20 10:33:28 -0700443 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800444 if(serial == pi->serial) {
445 if(name != 0) {
446 strcpy(name, pi->name);
447 }
448 return len;
449 }
450 }
451}
452
453int __system_property_get(const char *name, char *value)
454{
455 const prop_info *pi = __system_property_find(name);
456
457 if(pi != 0) {
458 return __system_property_read(pi, 0, value);
459 } else {
460 value[0] = 0;
461 return 0;
462 }
463}
464
satokec7e8cc2011-03-15 11:02:26 +0900465
466static int send_prop_msg(prop_msg *msg)
467{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700468 struct pollfd pollfds[1];
satokec7e8cc2011-03-15 11:02:26 +0900469 struct sockaddr_un addr;
470 socklen_t alen;
471 size_t namelen;
472 int s;
473 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700474 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900475
476 s = socket(AF_LOCAL, SOCK_STREAM, 0);
477 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700478 return result;
satokec7e8cc2011-03-15 11:02:26 +0900479 }
480
481 memset(&addr, 0, sizeof(addr));
482 namelen = strlen(property_service_socket);
483 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
484 addr.sun_family = AF_LOCAL;
485 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
486
Jens Gulinc20d0f32012-07-19 14:10:46 +0200487 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
satokec7e8cc2011-03-15 11:02:26 +0900488 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700489 return result;
satokec7e8cc2011-03-15 11:02:26 +0900490 }
491
492 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
493
494 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700495 // We successfully wrote to the property server but now we
496 // wait for the property server to finish its work. It
497 // acknowledges its completion by closing the socket so we
498 // poll here (on nothing), waiting for the socket to close.
499 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
500 // once the socket closes. Out of paranoia we cap our poll
501 // at 250 ms.
502 pollfds[0].fd = s;
503 pollfds[0].events = 0;
504 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
505 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
506 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700507 } else {
508 // Ignore the timeout and treat it like a success anyway.
509 // The init process is single-threaded and its property
510 // service is sometimes slow to respond (perhaps it's off
511 // starting a child process or something) and thus this
512 // times out and the caller thinks it failed, even though
513 // it's still getting around to it. So we fake it here,
514 // mostly for ctl.* properties, but we do try and wait 250
515 // ms so callers who do read-after-write can reliably see
516 // what they've written. Most of the time.
517 // TODO: fix the system properties design.
518 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700519 }
satokec7e8cc2011-03-15 11:02:26 +0900520 }
521
522 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700523 return result;
satokec7e8cc2011-03-15 11:02:26 +0900524}
525
526int __system_property_set(const char *key, const char *value)
527{
satokec7e8cc2011-03-15 11:02:26 +0900528 int err;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700529 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900530
531 if(key == 0) return -1;
532 if(value == 0) value = "";
533 if(strlen(key) >= PROP_NAME_MAX) return -1;
534 if(strlen(value) >= PROP_VALUE_MAX) return -1;
535
536 memset(&msg, 0, sizeof msg);
537 msg.cmd = PROP_MSG_SETPROP;
538 strlcpy(msg.name, key, sizeof msg.name);
539 strlcpy(msg.value, value, sizeof msg.value);
540
satokec7e8cc2011-03-15 11:02:26 +0900541 err = send_prop_msg(&msg);
542 if(err < 0) {
543 return err;
544 }
545
satokec7e8cc2011-03-15 11:02:26 +0900546 return 0;
547}
548
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549int __system_property_wait(const prop_info *pi)
550{
551 unsigned n;
552 if(pi == 0) {
Greg Hackmann1540f602013-06-19 13:31:21 -0700553 prop_area *pa = __system_property_area__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800554 n = pa->serial;
555 do {
556 __futex_wait(&pa->serial, n, 0);
557 } while(n == pa->serial);
558 } else {
559 n = pi->serial;
560 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700561 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800562 } while(n == pi->serial);
563 }
564 return 0;
565}
Colin Cross5cf32de2013-01-23 23:07:06 -0800566
567int __system_property_update(prop_info *pi, const char *value, unsigned int len)
568{
Greg Hackmann1540f602013-06-19 13:31:21 -0700569 prop_area *pa = __system_property_area__;
Colin Cross5cf32de2013-01-23 23:07:06 -0800570
571 if (len >= PROP_VALUE_MAX)
572 return -1;
573
574 pi->serial = pi->serial | 1;
Greg Hackmannf7511e32013-06-20 10:33:28 -0700575 ANDROID_MEMBAR_FULL();
Colin Cross5cf32de2013-01-23 23:07:06 -0800576 memcpy(pi->value, value, len + 1);
Greg Hackmannf7511e32013-06-20 10:33:28 -0700577 ANDROID_MEMBAR_FULL();
Colin Cross5cf32de2013-01-23 23:07:06 -0800578 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
579 __futex_wake(&pi->serial, INT32_MAX);
580
581 pa->serial++;
582 __futex_wake(&pa->serial, INT32_MAX);
583
584 return 0;
585}
586
587int __system_property_add(const char *name, unsigned int namelen,
588 const char *value, unsigned int valuelen)
589{
Greg Hackmann1540f602013-06-19 13:31:21 -0700590 prop_area *pa = __system_property_area__;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700591 const prop_info *pi;
Colin Cross5cf32de2013-01-23 23:07:06 -0800592
Colin Cross5cf32de2013-01-23 23:07:06 -0800593 if (namelen >= PROP_NAME_MAX)
594 return -1;
595 if (valuelen >= PROP_VALUE_MAX)
596 return -1;
597 if (namelen < 1)
598 return -1;
599
Greg Hackmann996cdc42013-06-20 11:27:56 -0700600 pi = find_property(root_node(), name, namelen, value, valuelen, true);
601 if (!pi)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800602 return -1;
603
Colin Cross5cf32de2013-01-23 23:07:06 -0800604 pa->serial++;
605 __futex_wake(&pa->serial, INT32_MAX);
Colin Cross5cf32de2013-01-23 23:07:06 -0800606 return 0;
607}
608
609unsigned int __system_property_serial(const prop_info *pi)
610{
611 return pi->serial;
612}
613
614unsigned int __system_property_wait_any(unsigned int serial)
615{
Greg Hackmann1540f602013-06-19 13:31:21 -0700616 prop_area *pa = __system_property_area__;
Colin Cross5cf32de2013-01-23 23:07:06 -0800617
618 do {
619 __futex_wait(&pa->serial, serial, 0);
620 } while(pa->serial == serial);
621
622 return pa->serial;
623}
Greg Hackmann996cdc42013-06-20 11:27:56 -0700624
625struct find_nth_cookie {
626 unsigned count;
627 unsigned n;
628 const prop_info *pi;
629};
630
631static void find_nth_fn(const prop_info *pi, void *ptr)
632{
633 struct find_nth_cookie *cookie = ptr;
634
635 if (cookie->n == cookie->count)
636 cookie->pi = pi;
637
638 cookie->count++;
639}
640
641const prop_info *__system_property_find_nth(unsigned n)
642{
643 struct find_nth_cookie cookie;
644 int err;
645
646 memset(&cookie, 0, sizeof(cookie));
647 cookie.n = n;
648
649 err = __system_property_foreach(find_nth_fn, &cookie);
650 if (err < 0)
651 return NULL;
652
653 return cookie.pi;
654}
655
656static int foreach_property(prop_off_t off,
657 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
658{
659 prop_bt *trie = to_prop_obj(off);
660 if (!trie)
661 return -1;
662
663 if (trie->left) {
664 int err = foreach_property(trie->left, propfn, cookie);
665 if (err < 0)
666 return -1;
667 }
668 if (trie->prop) {
669 prop_info *info = to_prop_obj(trie->prop);
670 if (!info)
671 return -1;
672 propfn(info, cookie);
673 }
674 if (trie->children) {
675 int err = foreach_property(trie->children, propfn, cookie);
676 if (err < 0)
677 return -1;
678 }
679 if (trie->right) {
680 int err = foreach_property(trie->right, propfn, cookie);
681 if (err < 0)
682 return -1;
683 }
684
685 return 0;
686}
687
688int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
689 void *cookie)
690{
691 return foreach_property(0, propfn, cookie);
692}