blob: 4c2e5a21f7af64acb749723368a68e3420be35fe [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;
Colin Cross5e9a0862013-06-24 18:36:39 -0700112static bool compat_mode = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113
Greg Hackmann1540f602013-06-19 13:31:21 -0700114prop_area *__system_property_area__ = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115
Colin Cross1ec20a02013-06-24 18:42:21 -0700116size_t pa_data_size;
117size_t pa_size;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700118
Nick Kralevich32417fb2013-01-23 09:28:35 -0800119static int get_fd_from_env(void)
120{
121 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
122
123 if (!env) {
124 return -1;
125 }
126
127 return atoi(env);
128}
129
Greg Hackmann1540f602013-06-19 13:31:21 -0700130static int map_prop_area_rw()
Colin Cross5cf32de2013-01-23 23:07:06 -0800131{
Greg Hackmanncb215a72013-02-13 14:41:48 -0800132 prop_area *pa;
133 int fd;
Colin Cross1d36ee12013-06-16 10:19:16 -0700134 int ret;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800135
136 /* dev is a tmpfs that we can use to carve a shared workspace
137 * out of, so let's do that...
138 */
Colin Cross1d36ee12013-06-16 10:19:16 -0700139 fd = open(property_filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC |
140 O_EXCL, 0444);
Greg Hackmanncb215a72013-02-13 14:41:48 -0800141 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
Colin Cross1d36ee12013-06-16 10:19:16 -0700151 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
152 if (ret < 0)
153 goto out;
154
Greg Hackmann1540f602013-06-19 13:31:21 -0700155 if (ftruncate(fd, PA_SIZE) < 0)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800156 goto out;
157
Colin Cross1ec20a02013-06-24 18:42:21 -0700158 pa_size = PA_SIZE;
159 pa_data_size = pa_size - sizeof(prop_area);
Colin Cross5e9a0862013-06-24 18:36:39 -0700160 compat_mode = false;
Colin Cross1ec20a02013-06-24 18:42:21 -0700161
162 pa = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Greg Hackmanncb215a72013-02-13 14:41:48 -0800163 if(pa == MAP_FAILED)
164 goto out;
165
Colin Cross1ec20a02013-06-24 18:42:21 -0700166 memset(pa, 0, pa_size);
Colin Cross5cf32de2013-01-23 23:07:06 -0800167 pa->magic = PROP_AREA_MAGIC;
168 pa->version = PROP_AREA_VERSION;
Greg Hackmann1540f602013-06-19 13:31:21 -0700169 /* reserve root node */
170 pa->bytes_used = sizeof(prop_bt);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700171
Colin Cross5cf32de2013-01-23 23:07:06 -0800172 /* plug into the lib property services */
Greg Hackmann1540f602013-06-19 13:31:21 -0700173 __system_property_area__ = pa;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800174
175 close(fd);
176 return 0;
177
178out:
179 close(fd);
180 return -1;
Colin Cross5cf32de2013-01-23 23:07:06 -0800181}
182
Greg Hackmanncb215a72013-02-13 14:41:48 -0800183int __system_property_set_filename(const char *filename)
184{
185 size_t len = strlen(filename);
186 if (len >= sizeof(property_filename))
187 return -1;
188
189 strcpy(property_filename, filename);
190 return 0;
191}
192
193int __system_property_area_init()
194{
Greg Hackmann1540f602013-06-19 13:31:21 -0700195 return map_prop_area_rw();
Greg Hackmanncb215a72013-02-13 14:41:48 -0800196}
197
Greg Hackmann1540f602013-06-19 13:31:21 -0700198static int map_prop_area()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199{
Nick Kralevich32417fb2013-01-23 09:28:35 -0800200 bool fromFile = true;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800201 int result = -1;
Colin Cross1d36ee12013-06-16 10:19:16 -0700202 int fd;
203 int ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204
Colin Cross1d36ee12013-06-16 10:19:16 -0700205 fd = open(property_filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
206 if (fd >= 0) {
207 /* For old kernels that don't support O_CLOEXEC */
208 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
209 if (ret < 0)
210 goto cleanup;
211 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800212
213 if ((fd < 0) && (errno == ENOENT)) {
214 /*
215 * For backwards compatibility, if the file doesn't
216 * exist, we use the environment to get the file descriptor.
217 * For security reasons, we only use this backup if the kernel
218 * returns ENOENT. We don't want to use the backup if the kernel
219 * returns other errors such as ENOMEM or ENFILE, since it
220 * might be possible for an external program to trigger this
221 * condition.
222 */
223 fd = get_fd_from_env();
224 fromFile = false;
225 }
226
227 if (fd < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228 return -1;
229 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800230
231 struct stat fd_stat;
232 if (fstat(fd, &fd_stat) < 0) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800233 goto cleanup;
234 }
235
236 if ((fd_stat.st_uid != 0)
237 || (fd_stat.st_gid != 0)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800238 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
Colin Cross1ec20a02013-06-24 18:42:21 -0700239 || (fd_stat.st_size < sizeof(prop_area)) ) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800240 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800242
Colin Cross1ec20a02013-06-24 18:42:21 -0700243 pa_size = fd_stat.st_size;
244 pa_data_size = pa_size - sizeof(prop_area);
245 prop_area *pa = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0);
Nick Kralevich32417fb2013-01-23 09:28:35 -0800246
Nick Kralevich32417fb2013-01-23 09:28:35 -0800247 if (pa == MAP_FAILED) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800248 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800249 }
250
Colin Cross5e9a0862013-06-24 18:36:39 -0700251 if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION &&
252 pa->version != PROP_AREA_VERSION_COMPAT)) {
Colin Cross1ec20a02013-06-24 18:42:21 -0700253 munmap(pa, pa_size);
Nick Kralevichc16961b2013-01-25 13:07:31 -0800254 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255 }
256
Colin Cross5e9a0862013-06-24 18:36:39 -0700257 if (pa->version == PROP_AREA_VERSION_COMPAT) {
258 compat_mode = true;
259 }
260
Nick Kralevichc16961b2013-01-25 13:07:31 -0800261 result = 0;
Greg Hackmann1540f602013-06-19 13:31:21 -0700262
263 __system_property_area__ = pa;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800264
265cleanup:
266 if (fromFile) {
267 close(fd);
268 }
269
270 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271}
272
Greg Hackmanncb215a72013-02-13 14:41:48 -0800273int __system_properties_init()
274{
Greg Hackmann1540f602013-06-19 13:31:21 -0700275 return map_prop_area();
Greg Hackmanncb215a72013-02-13 14:41:48 -0800276}
277
Greg Hackmann996cdc42013-06-20 11:27:56 -0700278static void *new_prop_obj(size_t size, prop_off_t *off)
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800279{
Greg Hackmann1540f602013-06-19 13:31:21 -0700280 prop_area *pa = __system_property_area__;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700281 size = ALIGN(size, sizeof(uint32_t));
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800282
Colin Cross1ec20a02013-06-24 18:42:21 -0700283 if (pa->bytes_used + size > pa_data_size)
Greg Hackmann996cdc42013-06-20 11:27:56 -0700284 return NULL;
285
Greg Hackmann1540f602013-06-19 13:31:21 -0700286 *off = pa->bytes_used;
287 __system_property_area__->bytes_used += size;
288 return __system_property_area__->data + *off;
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800289}
290
Greg Hackmann996cdc42013-06-20 11:27:56 -0700291static 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 -0800292{
Greg Hackmann996cdc42013-06-20 11:27:56 -0700293 prop_off_t off_tmp;
Greg Hackmann836dbf62013-06-21 13:02:38 -0700294 prop_bt *bt = new_prop_obj(sizeof(prop_bt) + namelen + 1, &off_tmp);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700295 if (bt) {
296 memcpy(bt->name, name, namelen);
297 bt->name[namelen] = '\0';
298 bt->namelen = namelen;
299 ANDROID_MEMBAR_FULL();
300 *off = off_tmp;
301 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800302
Greg Hackmann996cdc42013-06-20 11:27:56 -0700303 return bt;
304}
305
306static prop_info *new_prop_info(const char *name, uint8_t namelen,
307 const char *value, uint8_t valuelen, prop_off_t *off)
308{
309 prop_off_t off_tmp;
Greg Hackmann836dbf62013-06-21 13:02:38 -0700310 prop_info *info = new_prop_obj(sizeof(prop_info) + namelen + 1, &off_tmp);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700311 if (info) {
312 memcpy(info->name, name, namelen);
313 info->name[namelen] = '\0';
314 info->serial = (valuelen << 24);
315 memcpy(info->value, value, valuelen);
316 info->value[valuelen] = '\0';
317 ANDROID_MEMBAR_FULL();
318 *off = off_tmp;
319 }
320
321 return info;
322}
323
324static void *to_prop_obj(prop_off_t off)
325{
Colin Cross1ec20a02013-06-24 18:42:21 -0700326 if (off > pa_data_size)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800327 return NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800328
Greg Hackmann1540f602013-06-19 13:31:21 -0700329 return __system_property_area__->data + off;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700330}
331
332static prop_bt *root_node()
333{
334 return to_prop_obj(0);
335}
336
337static int cmp_prop_name(const char *one, uint8_t one_len, const char *two,
338 uint8_t two_len)
339{
340 if (one_len < two_len)
341 return -1;
342 else if (one_len > two_len)
343 return 1;
344 else
345 return strncmp(one, two, one_len);
346}
347
348static prop_bt *find_prop_bt(prop_bt *bt, const char *name, uint8_t namelen,
349 bool alloc_if_needed)
350{
351 while (true) {
352 int ret;
353 if (!bt)
354 return bt;
355 ret = cmp_prop_name(name, namelen, bt->name, bt->namelen);
356
357 if (ret == 0) {
358 return bt;
359 } else if (ret < 0) {
360 if (bt->left) {
361 bt = to_prop_obj(bt->left);
362 } else {
363 if (!alloc_if_needed)
364 return NULL;
365
366 bt = new_prop_bt(name, namelen, &bt->left);
367 }
368 } else {
369 if (bt->right) {
370 bt = to_prop_obj(bt->right);
371 } else {
372 if (!alloc_if_needed)
373 return NULL;
374
375 bt = new_prop_bt(name, namelen, &bt->right);
376 }
377 }
378 }
379}
380
381static const prop_info *find_property(prop_bt *trie, const char *name,
382 uint8_t namelen, const char *value, uint8_t valuelen,
383 bool alloc_if_needed)
384{
385 const char *remaining_name = name;
386
387 while (true) {
388 char *sep = strchr(remaining_name, '.');
389 bool want_subtree = (sep != NULL);
390 uint8_t substr_size;
391
392 prop_bt *root;
393
394 if (want_subtree) {
395 substr_size = sep - remaining_name;
396 } else {
397 substr_size = strlen(remaining_name);
398 }
399
400 if (!substr_size)
401 return NULL;
402
403 if (trie->children) {
404 root = to_prop_obj(trie->children);
405 } else if (alloc_if_needed) {
406 root = new_prop_bt(remaining_name, substr_size, &trie->children);
407 } else {
408 root = NULL;
409 }
410
411 if (!root)
412 return NULL;
413
414 trie = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
415 if (!trie)
416 return NULL;
417
418 if (!want_subtree)
419 break;
420
421 remaining_name = sep + 1;
422 }
423
424 if (trie->prop) {
425 return to_prop_obj(trie->prop);
426 } else if (alloc_if_needed) {
427 return new_prop_info(name, namelen, value, valuelen, &trie->prop);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 } else {
Greg Hackmann996cdc42013-06-20 11:27:56 -0700429 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430 }
431}
432
433const prop_info *__system_property_find(const char *name)
434{
Colin Cross5e9a0862013-06-24 18:36:39 -0700435 if (__predict_false(compat_mode)) {
436 return __system_property_find_compat(name);
437 }
Greg Hackmann996cdc42013-06-20 11:27:56 -0700438 return find_property(root_node(), name, strlen(name), NULL, 0, false);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800439}
440
441int __system_property_read(const prop_info *pi, char *name, char *value)
442{
443 unsigned serial, len;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700444
Colin Cross5e9a0862013-06-24 18:36:39 -0700445 if (__predict_false(compat_mode)) {
446 return __system_property_read_compat(pi, name, value);
447 }
448
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449 for(;;) {
450 serial = pi->serial;
451 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700452 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800453 serial = pi->serial;
454 }
455 len = SERIAL_VALUE_LEN(serial);
456 memcpy(value, pi->value, len + 1);
Greg Hackmannf7511e32013-06-20 10:33:28 -0700457 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458 if(serial == pi->serial) {
459 if(name != 0) {
460 strcpy(name, pi->name);
461 }
462 return len;
463 }
464 }
465}
466
467int __system_property_get(const char *name, char *value)
468{
469 const prop_info *pi = __system_property_find(name);
470
471 if(pi != 0) {
472 return __system_property_read(pi, 0, value);
473 } else {
474 value[0] = 0;
475 return 0;
476 }
477}
478
satokec7e8cc2011-03-15 11:02:26 +0900479
480static int send_prop_msg(prop_msg *msg)
481{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700482 struct pollfd pollfds[1];
satokec7e8cc2011-03-15 11:02:26 +0900483 struct sockaddr_un addr;
484 socklen_t alen;
485 size_t namelen;
486 int s;
487 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700488 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900489
490 s = socket(AF_LOCAL, SOCK_STREAM, 0);
491 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700492 return result;
satokec7e8cc2011-03-15 11:02:26 +0900493 }
494
495 memset(&addr, 0, sizeof(addr));
496 namelen = strlen(property_service_socket);
497 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
498 addr.sun_family = AF_LOCAL;
499 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
500
Jens Gulinc20d0f32012-07-19 14:10:46 +0200501 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
satokec7e8cc2011-03-15 11:02:26 +0900502 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700503 return result;
satokec7e8cc2011-03-15 11:02:26 +0900504 }
505
506 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
507
508 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700509 // We successfully wrote to the property server but now we
510 // wait for the property server to finish its work. It
511 // acknowledges its completion by closing the socket so we
512 // poll here (on nothing), waiting for the socket to close.
513 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
514 // once the socket closes. Out of paranoia we cap our poll
515 // at 250 ms.
516 pollfds[0].fd = s;
517 pollfds[0].events = 0;
518 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
519 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
520 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700521 } else {
522 // Ignore the timeout and treat it like a success anyway.
523 // The init process is single-threaded and its property
524 // service is sometimes slow to respond (perhaps it's off
525 // starting a child process or something) and thus this
526 // times out and the caller thinks it failed, even though
527 // it's still getting around to it. So we fake it here,
528 // mostly for ctl.* properties, but we do try and wait 250
529 // ms so callers who do read-after-write can reliably see
530 // what they've written. Most of the time.
531 // TODO: fix the system properties design.
532 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700533 }
satokec7e8cc2011-03-15 11:02:26 +0900534 }
535
536 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700537 return result;
satokec7e8cc2011-03-15 11:02:26 +0900538}
539
540int __system_property_set(const char *key, const char *value)
541{
satokec7e8cc2011-03-15 11:02:26 +0900542 int err;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700543 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900544
545 if(key == 0) return -1;
546 if(value == 0) value = "";
547 if(strlen(key) >= PROP_NAME_MAX) return -1;
548 if(strlen(value) >= PROP_VALUE_MAX) return -1;
549
550 memset(&msg, 0, sizeof msg);
551 msg.cmd = PROP_MSG_SETPROP;
552 strlcpy(msg.name, key, sizeof msg.name);
553 strlcpy(msg.value, value, sizeof msg.value);
554
satokec7e8cc2011-03-15 11:02:26 +0900555 err = send_prop_msg(&msg);
556 if(err < 0) {
557 return err;
558 }
559
satokec7e8cc2011-03-15 11:02:26 +0900560 return 0;
561}
562
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800563int __system_property_wait(const prop_info *pi)
564{
565 unsigned n;
566 if(pi == 0) {
Greg Hackmann1540f602013-06-19 13:31:21 -0700567 prop_area *pa = __system_property_area__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800568 n = pa->serial;
569 do {
570 __futex_wait(&pa->serial, n, 0);
571 } while(n == pa->serial);
572 } else {
573 n = pi->serial;
574 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700575 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800576 } while(n == pi->serial);
577 }
578 return 0;
579}
Colin Cross5cf32de2013-01-23 23:07:06 -0800580
581int __system_property_update(prop_info *pi, const char *value, unsigned int len)
582{
Greg Hackmann1540f602013-06-19 13:31:21 -0700583 prop_area *pa = __system_property_area__;
Colin Cross5cf32de2013-01-23 23:07:06 -0800584
585 if (len >= PROP_VALUE_MAX)
586 return -1;
587
588 pi->serial = pi->serial | 1;
Greg Hackmannf7511e32013-06-20 10:33:28 -0700589 ANDROID_MEMBAR_FULL();
Colin Cross5cf32de2013-01-23 23:07:06 -0800590 memcpy(pi->value, value, len + 1);
Greg Hackmannf7511e32013-06-20 10:33:28 -0700591 ANDROID_MEMBAR_FULL();
Colin Cross5cf32de2013-01-23 23:07:06 -0800592 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
593 __futex_wake(&pi->serial, INT32_MAX);
594
595 pa->serial++;
596 __futex_wake(&pa->serial, INT32_MAX);
597
598 return 0;
599}
600
601int __system_property_add(const char *name, unsigned int namelen,
602 const char *value, unsigned int valuelen)
603{
Greg Hackmann1540f602013-06-19 13:31:21 -0700604 prop_area *pa = __system_property_area__;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700605 const prop_info *pi;
Colin Cross5cf32de2013-01-23 23:07:06 -0800606
Colin Cross5cf32de2013-01-23 23:07:06 -0800607 if (namelen >= PROP_NAME_MAX)
608 return -1;
609 if (valuelen >= PROP_VALUE_MAX)
610 return -1;
611 if (namelen < 1)
612 return -1;
613
Greg Hackmann996cdc42013-06-20 11:27:56 -0700614 pi = find_property(root_node(), name, namelen, value, valuelen, true);
615 if (!pi)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800616 return -1;
617
Colin Cross5cf32de2013-01-23 23:07:06 -0800618 pa->serial++;
619 __futex_wake(&pa->serial, INT32_MAX);
Colin Cross5cf32de2013-01-23 23:07:06 -0800620 return 0;
621}
622
623unsigned int __system_property_serial(const prop_info *pi)
624{
625 return pi->serial;
626}
627
628unsigned int __system_property_wait_any(unsigned int serial)
629{
Greg Hackmann1540f602013-06-19 13:31:21 -0700630 prop_area *pa = __system_property_area__;
Colin Cross5cf32de2013-01-23 23:07:06 -0800631
632 do {
633 __futex_wait(&pa->serial, serial, 0);
634 } while(pa->serial == serial);
635
636 return pa->serial;
637}
Greg Hackmann996cdc42013-06-20 11:27:56 -0700638
639struct find_nth_cookie {
640 unsigned count;
641 unsigned n;
642 const prop_info *pi;
643};
644
645static void find_nth_fn(const prop_info *pi, void *ptr)
646{
647 struct find_nth_cookie *cookie = ptr;
648
649 if (cookie->n == cookie->count)
650 cookie->pi = pi;
651
652 cookie->count++;
653}
654
655const prop_info *__system_property_find_nth(unsigned n)
656{
657 struct find_nth_cookie cookie;
658 int err;
659
660 memset(&cookie, 0, sizeof(cookie));
661 cookie.n = n;
662
663 err = __system_property_foreach(find_nth_fn, &cookie);
664 if (err < 0)
665 return NULL;
666
667 return cookie.pi;
668}
669
670static int foreach_property(prop_off_t off,
671 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
672{
673 prop_bt *trie = to_prop_obj(off);
674 if (!trie)
675 return -1;
676
677 if (trie->left) {
678 int err = foreach_property(trie->left, propfn, cookie);
679 if (err < 0)
680 return -1;
681 }
682 if (trie->prop) {
683 prop_info *info = to_prop_obj(trie->prop);
684 if (!info)
685 return -1;
686 propfn(info, cookie);
687 }
688 if (trie->children) {
689 int err = foreach_property(trie->children, propfn, cookie);
690 if (err < 0)
691 return -1;
692 }
693 if (trie->right) {
694 int err = foreach_property(trie->right, propfn, cookie);
695 if (err < 0)
696 return -1;
697 }
698
699 return 0;
700}
701
702int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
703 void *cookie)
704{
Colin Cross5e9a0862013-06-24 18:36:39 -0700705 if (__predict_false(compat_mode)) {
706 return __system_property_foreach_compat(propfn, cookie);
707 }
Greg Hackmann996cdc42013-06-20 11:27:56 -0700708 return foreach_property(0, propfn, cookie);
709}