blob: 126fea56a9a8b592fbb64852c4928c041c8340da [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 {
69 char name[PROP_NAME_MAX];
70 unsigned volatile serial;
71 char value[PROP_VALUE_MAX];
72};
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 {
95 char name[PROP_NAME_MAX];
96 uint8_t namelen;
97 uint8_t reserved[3];
98
99 prop_off_t prop;
100
101 prop_off_t left;
102 prop_off_t right;
103
104 prop_off_t children;
105};
106
107typedef struct prop_bt prop_bt;
108
satokec7e8cc2011-03-15 11:02:26 +0900109static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800110static char property_filename[PATH_MAX] = PROP_FILENAME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
Greg Hackmanncb215a72013-02-13 14:41:48 -0800112prop_area *__system_property_regions__[PA_REGION_COUNT] = { NULL, };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113
Greg Hackmann996cdc42013-06-20 11:27:56 -0700114const size_t PA_DATA_SIZE = PA_SIZE - sizeof(prop_area);
115
Nick Kralevich32417fb2013-01-23 09:28:35 -0800116static int get_fd_from_env(void)
117{
118 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
119
120 if (!env) {
121 return -1;
122 }
123
124 return atoi(env);
125}
126
Greg Hackmanncb215a72013-02-13 14:41:48 -0800127static int map_prop_region_rw(size_t region)
Colin Cross5cf32de2013-01-23 23:07:06 -0800128{
Greg Hackmanncb215a72013-02-13 14:41:48 -0800129 prop_area *pa;
130 int fd;
131 size_t offset = region * PA_SIZE;
132
133 if (__system_property_regions__[region]) {
134 return 0;
135 }
136
137 /* dev is a tmpfs that we can use to carve a shared workspace
138 * out of, so let's do that...
139 */
140 fd = open(property_filename, O_RDWR | O_CREAT | O_NOFOLLOW, 0644);
141 if (fd < 0) {
142 if (errno == EACCES) {
143 /* for consistency with the case where the process has already
144 * mapped the page in and segfaults when trying to write to it
145 */
146 abort();
147 }
148 return -1;
149 }
150
151 if (ftruncate(fd, offset + PA_SIZE) < 0)
152 goto out;
153
154 pa = mmap(NULL, PA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
155 if(pa == MAP_FAILED)
156 goto out;
157
Colin Cross5cf32de2013-01-23 23:07:06 -0800158 memset(pa, 0, PA_SIZE);
159 pa->magic = PROP_AREA_MAGIC;
160 pa->version = PROP_AREA_VERSION;
161
Greg Hackmann996cdc42013-06-20 11:27:56 -0700162 if (!region) {
163 /* reserve root node */
164 pa->bytes_used += sizeof(prop_bt);
165 }
166
Colin Cross5cf32de2013-01-23 23:07:06 -0800167 /* plug into the lib property services */
Greg Hackmanncb215a72013-02-13 14:41:48 -0800168 __system_property_regions__[region] = pa;
169
170 close(fd);
171 return 0;
172
173out:
174 close(fd);
175 return -1;
Colin Cross5cf32de2013-01-23 23:07:06 -0800176}
177
Greg Hackmanncb215a72013-02-13 14:41:48 -0800178int __system_property_set_filename(const char *filename)
179{
180 size_t len = strlen(filename);
181 if (len >= sizeof(property_filename))
182 return -1;
183
184 strcpy(property_filename, filename);
185 return 0;
186}
187
188int __system_property_area_init()
189{
190 return map_prop_region_rw(0);
191}
192
193static int map_prop_region(size_t region)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194{
Nick Kralevich32417fb2013-01-23 09:28:35 -0800195 bool fromFile = true;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800196 bool swapped;
197 size_t offset = region * PA_SIZE;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800198 int result = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199
Greg Hackmanncb215a72013-02-13 14:41:48 -0800200 if(__system_property_regions__[region]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201 return 0;
202 }
203
Greg Hackmanncb215a72013-02-13 14:41:48 -0800204 int fd = open(property_filename, O_RDONLY | O_NOFOLLOW);
Nick Kralevich32417fb2013-01-23 09:28:35 -0800205
206 if ((fd < 0) && (errno == ENOENT)) {
207 /*
208 * For backwards compatibility, if the file doesn't
209 * exist, we use the environment to get the file descriptor.
210 * For security reasons, we only use this backup if the kernel
211 * returns ENOENT. We don't want to use the backup if the kernel
212 * returns other errors such as ENOMEM or ENFILE, since it
213 * might be possible for an external program to trigger this
214 * condition.
215 */
216 fd = get_fd_from_env();
217 fromFile = false;
218 }
219
220 if (fd < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221 return -1;
222 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800223
224 struct stat fd_stat;
225 if (fstat(fd, &fd_stat) < 0) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800226 goto cleanup;
227 }
228
229 if ((fd_stat.st_uid != 0)
230 || (fd_stat.st_gid != 0)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800231 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
232 || (fd_stat.st_size < offset + PA_SIZE) ) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800233 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800234 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800235
Greg Hackmanncb215a72013-02-13 14:41:48 -0800236 prop_area *pa = mmap(NULL, PA_SIZE, PROT_READ, MAP_SHARED, fd, offset);
Nick Kralevich32417fb2013-01-23 09:28:35 -0800237
Nick Kralevich32417fb2013-01-23 09:28:35 -0800238 if (pa == MAP_FAILED) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800239 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240 }
241
242 if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) {
Greg Hackmanncb215a72013-02-13 14:41:48 -0800243 munmap(pa, PA_SIZE);
Nick Kralevichc16961b2013-01-25 13:07:31 -0800244 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245 }
246
Nick Kralevichc16961b2013-01-25 13:07:31 -0800247 result = 0;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800248 swapped = __sync_bool_compare_and_swap(&__system_property_regions__[region],
249 NULL, pa);
250 if (!swapped) {
251 /**
252 * In the event of a race either mapping is equally good, so
253 * the thread that lost can just throw its mapping away and proceed as
254 * normal.
255 */
256 munmap(pa, PA_SIZE);
257 }
Nick Kralevichc16961b2013-01-25 13:07:31 -0800258
259cleanup:
260 if (fromFile) {
261 close(fd);
262 }
263
264 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265}
266
Greg Hackmanncb215a72013-02-13 14:41:48 -0800267int __system_properties_init()
268{
269 return map_prop_region(0);
270}
271
Greg Hackmann996cdc42013-06-20 11:27:56 -0700272static void *new_prop_obj(size_t size, prop_off_t *off)
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800273{
Greg Hackmann996cdc42013-06-20 11:27:56 -0700274 prop_area *pa;
275 size_t i, idx;
276 size = ALIGN(size, sizeof(uint32_t));
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800277
Greg Hackmann996cdc42013-06-20 11:27:56 -0700278 for (i = 0; i < PA_REGION_COUNT; i++) {
279 int err = map_prop_region_rw(i);
280 if (err < 0) {
281 return NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800282 }
Greg Hackmann996cdc42013-06-20 11:27:56 -0700283
284 pa = __system_property_regions__[i];
285 if (pa->bytes_used + size <= PA_DATA_SIZE)
286 break;
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800287 }
288
Greg Hackmann996cdc42013-06-20 11:27:56 -0700289 if (i == PA_REGION_COUNT)
290 return NULL;
291
292 idx = pa->bytes_used;
293 *off = idx + i * PA_DATA_SIZE;
294 pa->bytes_used += size;
295 return pa->data + idx;
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800296}
297
Greg Hackmann996cdc42013-06-20 11:27:56 -0700298static 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 -0800299{
Greg Hackmann996cdc42013-06-20 11:27:56 -0700300 prop_off_t off_tmp;
301 prop_bt *bt = new_prop_obj(sizeof(prop_bt), &off_tmp);
302 if (bt) {
303 memcpy(bt->name, name, namelen);
304 bt->name[namelen] = '\0';
305 bt->namelen = namelen;
306 ANDROID_MEMBAR_FULL();
307 *off = off_tmp;
308 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800309
Greg Hackmann996cdc42013-06-20 11:27:56 -0700310 return bt;
311}
312
313static prop_info *new_prop_info(const char *name, uint8_t namelen,
314 const char *value, uint8_t valuelen, prop_off_t *off)
315{
316 prop_off_t off_tmp;
317 prop_info *info = new_prop_obj(sizeof(prop_info), &off_tmp);
318 if (info) {
319 memcpy(info->name, name, namelen);
320 info->name[namelen] = '\0';
321 info->serial = (valuelen << 24);
322 memcpy(info->value, value, valuelen);
323 info->value[valuelen] = '\0';
324 ANDROID_MEMBAR_FULL();
325 *off = off_tmp;
326 }
327
328 return info;
329}
330
331static void *to_prop_obj(prop_off_t off)
332{
333 size_t region = off / PA_DATA_SIZE;
334 size_t idx = off % PA_DATA_SIZE;
335
336 if (region > PA_REGION_COUNT)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800337 return NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800338
Greg Hackmann996cdc42013-06-20 11:27:56 -0700339 if (map_prop_region(region) < 0)
340 return NULL;
341
342 return __system_property_regions__[region]->data + idx;
343}
344
345static prop_bt *root_node()
346{
347 return to_prop_obj(0);
348}
349
350static int cmp_prop_name(const char *one, uint8_t one_len, const char *two,
351 uint8_t two_len)
352{
353 if (one_len < two_len)
354 return -1;
355 else if (one_len > two_len)
356 return 1;
357 else
358 return strncmp(one, two, one_len);
359}
360
361static prop_bt *find_prop_bt(prop_bt *bt, const char *name, uint8_t namelen,
362 bool alloc_if_needed)
363{
364 while (true) {
365 int ret;
366 if (!bt)
367 return bt;
368 ret = cmp_prop_name(name, namelen, bt->name, bt->namelen);
369
370 if (ret == 0) {
371 return bt;
372 } else if (ret < 0) {
373 if (bt->left) {
374 bt = to_prop_obj(bt->left);
375 } else {
376 if (!alloc_if_needed)
377 return NULL;
378
379 bt = new_prop_bt(name, namelen, &bt->left);
380 }
381 } else {
382 if (bt->right) {
383 bt = to_prop_obj(bt->right);
384 } else {
385 if (!alloc_if_needed)
386 return NULL;
387
388 bt = new_prop_bt(name, namelen, &bt->right);
389 }
390 }
391 }
392}
393
394static const prop_info *find_property(prop_bt *trie, const char *name,
395 uint8_t namelen, const char *value, uint8_t valuelen,
396 bool alloc_if_needed)
397{
398 const char *remaining_name = name;
399
400 while (true) {
401 char *sep = strchr(remaining_name, '.');
402 bool want_subtree = (sep != NULL);
403 uint8_t substr_size;
404
405 prop_bt *root;
406
407 if (want_subtree) {
408 substr_size = sep - remaining_name;
409 } else {
410 substr_size = strlen(remaining_name);
411 }
412
413 if (!substr_size)
414 return NULL;
415
416 if (trie->children) {
417 root = to_prop_obj(trie->children);
418 } else if (alloc_if_needed) {
419 root = new_prop_bt(remaining_name, substr_size, &trie->children);
420 } else {
421 root = NULL;
422 }
423
424 if (!root)
425 return NULL;
426
427 trie = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
428 if (!trie)
429 return NULL;
430
431 if (!want_subtree)
432 break;
433
434 remaining_name = sep + 1;
435 }
436
437 if (trie->prop) {
438 return to_prop_obj(trie->prop);
439 } else if (alloc_if_needed) {
440 return new_prop_info(name, namelen, value, valuelen, &trie->prop);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800441 } else {
Greg Hackmann996cdc42013-06-20 11:27:56 -0700442 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800443 }
444}
445
446const prop_info *__system_property_find(const char *name)
447{
Greg Hackmann996cdc42013-06-20 11:27:56 -0700448 return find_property(root_node(), name, strlen(name), NULL, 0, false);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449}
450
451int __system_property_read(const prop_info *pi, char *name, char *value)
452{
453 unsigned serial, len;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700454
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800455 for(;;) {
456 serial = pi->serial;
457 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700458 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800459 serial = pi->serial;
460 }
461 len = SERIAL_VALUE_LEN(serial);
462 memcpy(value, pi->value, len + 1);
Greg Hackmannf7511e32013-06-20 10:33:28 -0700463 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800464 if(serial == pi->serial) {
465 if(name != 0) {
466 strcpy(name, pi->name);
467 }
468 return len;
469 }
470 }
471}
472
473int __system_property_get(const char *name, char *value)
474{
475 const prop_info *pi = __system_property_find(name);
476
477 if(pi != 0) {
478 return __system_property_read(pi, 0, value);
479 } else {
480 value[0] = 0;
481 return 0;
482 }
483}
484
satokec7e8cc2011-03-15 11:02:26 +0900485
486static int send_prop_msg(prop_msg *msg)
487{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700488 struct pollfd pollfds[1];
satokec7e8cc2011-03-15 11:02:26 +0900489 struct sockaddr_un addr;
490 socklen_t alen;
491 size_t namelen;
492 int s;
493 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700494 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900495
496 s = socket(AF_LOCAL, SOCK_STREAM, 0);
497 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700498 return result;
satokec7e8cc2011-03-15 11:02:26 +0900499 }
500
501 memset(&addr, 0, sizeof(addr));
502 namelen = strlen(property_service_socket);
503 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
504 addr.sun_family = AF_LOCAL;
505 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
506
Jens Gulinc20d0f32012-07-19 14:10:46 +0200507 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
satokec7e8cc2011-03-15 11:02:26 +0900508 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700509 return result;
satokec7e8cc2011-03-15 11:02:26 +0900510 }
511
512 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
513
514 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700515 // We successfully wrote to the property server but now we
516 // wait for the property server to finish its work. It
517 // acknowledges its completion by closing the socket so we
518 // poll here (on nothing), waiting for the socket to close.
519 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
520 // once the socket closes. Out of paranoia we cap our poll
521 // at 250 ms.
522 pollfds[0].fd = s;
523 pollfds[0].events = 0;
524 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
525 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
526 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700527 } else {
528 // Ignore the timeout and treat it like a success anyway.
529 // The init process is single-threaded and its property
530 // service is sometimes slow to respond (perhaps it's off
531 // starting a child process or something) and thus this
532 // times out and the caller thinks it failed, even though
533 // it's still getting around to it. So we fake it here,
534 // mostly for ctl.* properties, but we do try and wait 250
535 // ms so callers who do read-after-write can reliably see
536 // what they've written. Most of the time.
537 // TODO: fix the system properties design.
538 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700539 }
satokec7e8cc2011-03-15 11:02:26 +0900540 }
541
542 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700543 return result;
satokec7e8cc2011-03-15 11:02:26 +0900544}
545
546int __system_property_set(const char *key, const char *value)
547{
satokec7e8cc2011-03-15 11:02:26 +0900548 int err;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700549 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900550
551 if(key == 0) return -1;
552 if(value == 0) value = "";
553 if(strlen(key) >= PROP_NAME_MAX) return -1;
554 if(strlen(value) >= PROP_VALUE_MAX) return -1;
555
556 memset(&msg, 0, sizeof msg);
557 msg.cmd = PROP_MSG_SETPROP;
558 strlcpy(msg.name, key, sizeof msg.name);
559 strlcpy(msg.value, value, sizeof msg.value);
560
satokec7e8cc2011-03-15 11:02:26 +0900561 err = send_prop_msg(&msg);
562 if(err < 0) {
563 return err;
564 }
565
satokec7e8cc2011-03-15 11:02:26 +0900566 return 0;
567}
568
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800569int __system_property_wait(const prop_info *pi)
570{
571 unsigned n;
572 if(pi == 0) {
Greg Hackmanncb215a72013-02-13 14:41:48 -0800573 prop_area *pa = __system_property_regions__[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800574 n = pa->serial;
575 do {
576 __futex_wait(&pa->serial, n, 0);
577 } while(n == pa->serial);
578 } else {
579 n = pi->serial;
580 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700581 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800582 } while(n == pi->serial);
583 }
584 return 0;
585}
Colin Cross5cf32de2013-01-23 23:07:06 -0800586
587int __system_property_update(prop_info *pi, const char *value, unsigned int len)
588{
Greg Hackmanncb215a72013-02-13 14:41:48 -0800589 prop_area *pa = __system_property_regions__[0];
Colin Cross5cf32de2013-01-23 23:07:06 -0800590
591 if (len >= PROP_VALUE_MAX)
592 return -1;
593
594 pi->serial = pi->serial | 1;
Greg Hackmannf7511e32013-06-20 10:33:28 -0700595 ANDROID_MEMBAR_FULL();
Colin Cross5cf32de2013-01-23 23:07:06 -0800596 memcpy(pi->value, value, len + 1);
Greg Hackmannf7511e32013-06-20 10:33:28 -0700597 ANDROID_MEMBAR_FULL();
Colin Cross5cf32de2013-01-23 23:07:06 -0800598 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
599 __futex_wake(&pi->serial, INT32_MAX);
600
601 pa->serial++;
602 __futex_wake(&pa->serial, INT32_MAX);
603
604 return 0;
605}
606
607int __system_property_add(const char *name, unsigned int namelen,
608 const char *value, unsigned int valuelen)
609{
Greg Hackmann996cdc42013-06-20 11:27:56 -0700610 prop_area *pa = __system_property_regions__[0];
611 const prop_info *pi;
Colin Cross5cf32de2013-01-23 23:07:06 -0800612
Colin Cross5cf32de2013-01-23 23:07:06 -0800613 if (namelen >= PROP_NAME_MAX)
614 return -1;
615 if (valuelen >= PROP_VALUE_MAX)
616 return -1;
617 if (namelen < 1)
618 return -1;
619
Greg Hackmann996cdc42013-06-20 11:27:56 -0700620 pi = find_property(root_node(), name, namelen, value, valuelen, true);
621 if (!pi)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800622 return -1;
623
Colin Cross5cf32de2013-01-23 23:07:06 -0800624 pa->serial++;
625 __futex_wake(&pa->serial, INT32_MAX);
Colin Cross5cf32de2013-01-23 23:07:06 -0800626 return 0;
627}
628
629unsigned int __system_property_serial(const prop_info *pi)
630{
631 return pi->serial;
632}
633
634unsigned int __system_property_wait_any(unsigned int serial)
635{
Greg Hackmanncb215a72013-02-13 14:41:48 -0800636 prop_area *pa = __system_property_regions__[0];
Colin Cross5cf32de2013-01-23 23:07:06 -0800637
638 do {
639 __futex_wait(&pa->serial, serial, 0);
640 } while(pa->serial == serial);
641
642 return pa->serial;
643}
Greg Hackmann996cdc42013-06-20 11:27:56 -0700644
645struct find_nth_cookie {
646 unsigned count;
647 unsigned n;
648 const prop_info *pi;
649};
650
651static void find_nth_fn(const prop_info *pi, void *ptr)
652{
653 struct find_nth_cookie *cookie = ptr;
654
655 if (cookie->n == cookie->count)
656 cookie->pi = pi;
657
658 cookie->count++;
659}
660
661const prop_info *__system_property_find_nth(unsigned n)
662{
663 struct find_nth_cookie cookie;
664 int err;
665
666 memset(&cookie, 0, sizeof(cookie));
667 cookie.n = n;
668
669 err = __system_property_foreach(find_nth_fn, &cookie);
670 if (err < 0)
671 return NULL;
672
673 return cookie.pi;
674}
675
676static int foreach_property(prop_off_t off,
677 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
678{
679 prop_bt *trie = to_prop_obj(off);
680 if (!trie)
681 return -1;
682
683 if (trie->left) {
684 int err = foreach_property(trie->left, propfn, cookie);
685 if (err < 0)
686 return -1;
687 }
688 if (trie->prop) {
689 prop_info *info = to_prop_obj(trie->prop);
690 if (!info)
691 return -1;
692 propfn(info, cookie);
693 }
694 if (trie->children) {
695 int err = foreach_property(trie->children, propfn, cookie);
696 if (err < 0)
697 return -1;
698 }
699 if (trie->right) {
700 int err = foreach_property(trie->right, propfn, cookie);
701 if (err < 0)
702 return -1;
703 }
704
705 return 0;
706}
707
708int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
709 void *cookie)
710{
711 return foreach_property(0, propfn, cookie);
712}