blob: 9fdb6f5e228332a304c7f048e0f16fc6d866af12 [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>
Elliott Hugheseb847bc2013-10-09 15:50:50 -070053
54#include "private/bionic_atomic_inline.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055
Greg Hackmann996cdc42013-06-20 11:27:56 -070056#define ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))
57
Colin Cross5cf32de2013-01-23 23:07:06 -080058struct prop_area {
Greg Hackmann996cdc42013-06-20 11:27:56 -070059 unsigned bytes_used;
Colin Cross5cf32de2013-01-23 23:07:06 -080060 unsigned volatile serial;
61 unsigned magic;
62 unsigned version;
Greg Hackmann996cdc42013-06-20 11:27:56 -070063 unsigned reserved[28];
64 char data[0];
Colin Cross5cf32de2013-01-23 23:07:06 -080065};
66
67typedef struct prop_area prop_area;
68
69struct prop_info {
Colin Cross5cf32de2013-01-23 23:07:06 -080070 unsigned volatile serial;
71 char value[PROP_VALUE_MAX];
Greg Hackmann836dbf62013-06-21 13:02:38 -070072 char name[0];
Colin Cross5cf32de2013-01-23 23:07:06 -080073};
74
75typedef struct prop_info prop_info;
76
Greg Hackmann996cdc42013-06-20 11:27:56 -070077/*
78 * Properties are stored in a hybrid trie/binary tree structure.
79 * Each property's name is delimited at '.' characters, and the tokens are put
80 * into a trie structure. Siblings at each level of the trie are stored in a
81 * binary tree. For instance, "ro.secure"="1" could be stored as follows:
82 *
83 * +-----+ children +----+ children +--------+
84 * | |-------------->| ro |-------------->| secure |
85 * +-----+ +----+ +--------+
86 * / \ / |
87 * left / \ right left / | prop +===========+
88 * v v v +-------->| ro.secure |
89 * +-----+ +-----+ +-----+ +-----------+
90 * | net | | sys | | com | | 1 |
91 * +-----+ +-----+ +-----+ +===========+
92 */
93
94typedef volatile uint32_t prop_off_t;
95struct prop_bt {
Greg Hackmann996cdc42013-06-20 11:27:56 -070096 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;
Greg Hackmann836dbf62013-06-21 13:02:38 -0700105
106 char name[0];
Greg Hackmann996cdc42013-06-20 11:27:56 -0700107};
108
109typedef struct prop_bt prop_bt;
110
satokec7e8cc2011-03-15 11:02:26 +0900111static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800112static char property_filename[PATH_MAX] = PROP_FILENAME;
Colin Cross5e9a0862013-06-24 18:36:39 -0700113static bool compat_mode = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Greg Hackmann1540f602013-06-19 13:31:21 -0700115prop_area *__system_property_area__ = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
Colin Cross1ec20a02013-06-24 18:42:21 -0700117size_t pa_data_size;
118size_t pa_size;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700119
Nick Kralevich32417fb2013-01-23 09:28:35 -0800120static int get_fd_from_env(void)
121{
122 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
123
124 if (!env) {
125 return -1;
126 }
127
128 return atoi(env);
129}
130
Greg Hackmann1540f602013-06-19 13:31:21 -0700131static int map_prop_area_rw()
Colin Cross5cf32de2013-01-23 23:07:06 -0800132{
Greg Hackmanncb215a72013-02-13 14:41:48 -0800133 prop_area *pa;
134 int fd;
Colin Cross1d36ee12013-06-16 10:19:16 -0700135 int ret;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800136
137 /* dev is a tmpfs that we can use to carve a shared workspace
138 * out of, so let's do that...
139 */
Colin Cross1d36ee12013-06-16 10:19:16 -0700140 fd = open(property_filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC |
141 O_EXCL, 0444);
Greg Hackmanncb215a72013-02-13 14:41:48 -0800142 if (fd < 0) {
143 if (errno == EACCES) {
144 /* for consistency with the case where the process has already
145 * mapped the page in and segfaults when trying to write to it
146 */
147 abort();
148 }
149 return -1;
150 }
151
Colin Cross1d36ee12013-06-16 10:19:16 -0700152 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
153 if (ret < 0)
154 goto out;
155
Greg Hackmann1540f602013-06-19 13:31:21 -0700156 if (ftruncate(fd, PA_SIZE) < 0)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800157 goto out;
158
Colin Cross1ec20a02013-06-24 18:42:21 -0700159 pa_size = PA_SIZE;
160 pa_data_size = pa_size - sizeof(prop_area);
Colin Cross5e9a0862013-06-24 18:36:39 -0700161 compat_mode = false;
Colin Cross1ec20a02013-06-24 18:42:21 -0700162
163 pa = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Greg Hackmanncb215a72013-02-13 14:41:48 -0800164 if(pa == MAP_FAILED)
165 goto out;
166
Colin Cross1ec20a02013-06-24 18:42:21 -0700167 memset(pa, 0, pa_size);
Colin Cross5cf32de2013-01-23 23:07:06 -0800168 pa->magic = PROP_AREA_MAGIC;
169 pa->version = PROP_AREA_VERSION;
Greg Hackmann1540f602013-06-19 13:31:21 -0700170 /* reserve root node */
171 pa->bytes_used = sizeof(prop_bt);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700172
Colin Cross5cf32de2013-01-23 23:07:06 -0800173 /* plug into the lib property services */
Greg Hackmann1540f602013-06-19 13:31:21 -0700174 __system_property_area__ = pa;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800175
176 close(fd);
177 return 0;
178
179out:
180 close(fd);
181 return -1;
Colin Cross5cf32de2013-01-23 23:07:06 -0800182}
183
Greg Hackmanncb215a72013-02-13 14:41:48 -0800184int __system_property_set_filename(const char *filename)
185{
186 size_t len = strlen(filename);
187 if (len >= sizeof(property_filename))
188 return -1;
189
190 strcpy(property_filename, filename);
191 return 0;
192}
193
194int __system_property_area_init()
195{
Greg Hackmann1540f602013-06-19 13:31:21 -0700196 return map_prop_area_rw();
Greg Hackmanncb215a72013-02-13 14:41:48 -0800197}
198
Greg Hackmann1540f602013-06-19 13:31:21 -0700199static int map_prop_area()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200{
Nick Kralevich32417fb2013-01-23 09:28:35 -0800201 bool fromFile = true;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800202 int result = -1;
Colin Cross1d36ee12013-06-16 10:19:16 -0700203 int fd;
204 int ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205
Colin Cross1d36ee12013-06-16 10:19:16 -0700206 fd = open(property_filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
207 if (fd >= 0) {
208 /* For old kernels that don't support O_CLOEXEC */
209 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
210 if (ret < 0)
211 goto cleanup;
212 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800213
214 if ((fd < 0) && (errno == ENOENT)) {
215 /*
216 * For backwards compatibility, if the file doesn't
217 * exist, we use the environment to get the file descriptor.
218 * For security reasons, we only use this backup if the kernel
219 * returns ENOENT. We don't want to use the backup if the kernel
220 * returns other errors such as ENOMEM or ENFILE, since it
221 * might be possible for an external program to trigger this
222 * condition.
223 */
224 fd = get_fd_from_env();
225 fromFile = false;
226 }
227
228 if (fd < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229 return -1;
230 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800231
232 struct stat fd_stat;
233 if (fstat(fd, &fd_stat) < 0) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800234 goto cleanup;
235 }
236
237 if ((fd_stat.st_uid != 0)
238 || (fd_stat.st_gid != 0)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800239 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
Colin Cross1ec20a02013-06-24 18:42:21 -0700240 || (fd_stat.st_size < sizeof(prop_area)) ) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800241 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800243
Colin Cross1ec20a02013-06-24 18:42:21 -0700244 pa_size = fd_stat.st_size;
245 pa_data_size = pa_size - sizeof(prop_area);
246 prop_area *pa = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0);
Nick Kralevich32417fb2013-01-23 09:28:35 -0800247
Nick Kralevich32417fb2013-01-23 09:28:35 -0800248 if (pa == MAP_FAILED) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800249 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800250 }
251
Colin Cross5e9a0862013-06-24 18:36:39 -0700252 if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION &&
253 pa->version != PROP_AREA_VERSION_COMPAT)) {
Colin Cross1ec20a02013-06-24 18:42:21 -0700254 munmap(pa, pa_size);
Nick Kralevichc16961b2013-01-25 13:07:31 -0800255 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800256 }
257
Colin Cross5e9a0862013-06-24 18:36:39 -0700258 if (pa->version == PROP_AREA_VERSION_COMPAT) {
259 compat_mode = true;
260 }
261
Nick Kralevichc16961b2013-01-25 13:07:31 -0800262 result = 0;
Greg Hackmann1540f602013-06-19 13:31:21 -0700263
264 __system_property_area__ = pa;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800265
266cleanup:
267 if (fromFile) {
268 close(fd);
269 }
270
271 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272}
273
Greg Hackmanncb215a72013-02-13 14:41:48 -0800274int __system_properties_init()
275{
Greg Hackmann1540f602013-06-19 13:31:21 -0700276 return map_prop_area();
Greg Hackmanncb215a72013-02-13 14:41:48 -0800277}
278
Greg Hackmann996cdc42013-06-20 11:27:56 -0700279static void *new_prop_obj(size_t size, prop_off_t *off)
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800280{
Greg Hackmann1540f602013-06-19 13:31:21 -0700281 prop_area *pa = __system_property_area__;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700282 size = ALIGN(size, sizeof(uint32_t));
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800283
Colin Cross1ec20a02013-06-24 18:42:21 -0700284 if (pa->bytes_used + size > pa_data_size)
Greg Hackmann996cdc42013-06-20 11:27:56 -0700285 return NULL;
286
Greg Hackmann1540f602013-06-19 13:31:21 -0700287 *off = pa->bytes_used;
288 __system_property_area__->bytes_used += size;
289 return __system_property_area__->data + *off;
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800290}
291
Greg Hackmann996cdc42013-06-20 11:27:56 -0700292static 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 -0800293{
Greg Hackmann996cdc42013-06-20 11:27:56 -0700294 prop_off_t off_tmp;
Greg Hackmann836dbf62013-06-21 13:02:38 -0700295 prop_bt *bt = new_prop_obj(sizeof(prop_bt) + namelen + 1, &off_tmp);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700296 if (bt) {
297 memcpy(bt->name, name, namelen);
298 bt->name[namelen] = '\0';
299 bt->namelen = namelen;
300 ANDROID_MEMBAR_FULL();
301 *off = off_tmp;
302 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303
Greg Hackmann996cdc42013-06-20 11:27:56 -0700304 return bt;
305}
306
307static prop_info *new_prop_info(const char *name, uint8_t namelen,
308 const char *value, uint8_t valuelen, prop_off_t *off)
309{
310 prop_off_t off_tmp;
Greg Hackmann836dbf62013-06-21 13:02:38 -0700311 prop_info *info = new_prop_obj(sizeof(prop_info) + namelen + 1, &off_tmp);
Greg Hackmann996cdc42013-06-20 11:27:56 -0700312 if (info) {
313 memcpy(info->name, name, namelen);
314 info->name[namelen] = '\0';
315 info->serial = (valuelen << 24);
316 memcpy(info->value, value, valuelen);
317 info->value[valuelen] = '\0';
318 ANDROID_MEMBAR_FULL();
319 *off = off_tmp;
320 }
321
322 return info;
323}
324
325static void *to_prop_obj(prop_off_t off)
326{
Colin Cross1ec20a02013-06-24 18:42:21 -0700327 if (off > pa_data_size)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800328 return NULL;
Pavel Chupina21e6962013-08-09 19:17:55 +0400329 if (!__system_property_area__)
330 return NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800331
Greg Hackmann1540f602013-06-19 13:31:21 -0700332 return __system_property_area__->data + off;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700333}
334
335static prop_bt *root_node()
336{
337 return to_prop_obj(0);
338}
339
340static int cmp_prop_name(const char *one, uint8_t one_len, const char *two,
341 uint8_t two_len)
342{
343 if (one_len < two_len)
344 return -1;
345 else if (one_len > two_len)
346 return 1;
347 else
348 return strncmp(one, two, one_len);
349}
350
351static prop_bt *find_prop_bt(prop_bt *bt, const char *name, uint8_t namelen,
352 bool alloc_if_needed)
353{
354 while (true) {
355 int ret;
356 if (!bt)
357 return bt;
358 ret = cmp_prop_name(name, namelen, bt->name, bt->namelen);
359
360 if (ret == 0) {
361 return bt;
362 } else if (ret < 0) {
363 if (bt->left) {
364 bt = to_prop_obj(bt->left);
365 } else {
366 if (!alloc_if_needed)
367 return NULL;
368
369 bt = new_prop_bt(name, namelen, &bt->left);
370 }
371 } else {
372 if (bt->right) {
373 bt = to_prop_obj(bt->right);
374 } else {
375 if (!alloc_if_needed)
376 return NULL;
377
378 bt = new_prop_bt(name, namelen, &bt->right);
379 }
380 }
381 }
382}
383
384static const prop_info *find_property(prop_bt *trie, const char *name,
385 uint8_t namelen, const char *value, uint8_t valuelen,
386 bool alloc_if_needed)
387{
388 const char *remaining_name = name;
389
Pavel Chupina21e6962013-08-09 19:17:55 +0400390 if (!trie) return NULL;
391
Greg Hackmann996cdc42013-06-20 11:27:56 -0700392 while (true) {
393 char *sep = strchr(remaining_name, '.');
394 bool want_subtree = (sep != NULL);
395 uint8_t substr_size;
396
397 prop_bt *root;
398
399 if (want_subtree) {
400 substr_size = sep - remaining_name;
401 } else {
402 substr_size = strlen(remaining_name);
403 }
404
405 if (!substr_size)
406 return NULL;
407
408 if (trie->children) {
409 root = to_prop_obj(trie->children);
410 } else if (alloc_if_needed) {
411 root = new_prop_bt(remaining_name, substr_size, &trie->children);
412 } else {
413 root = NULL;
414 }
415
416 if (!root)
417 return NULL;
418
419 trie = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
420 if (!trie)
421 return NULL;
422
423 if (!want_subtree)
424 break;
425
426 remaining_name = sep + 1;
427 }
428
429 if (trie->prop) {
430 return to_prop_obj(trie->prop);
431 } else if (alloc_if_needed) {
432 return new_prop_info(name, namelen, value, valuelen, &trie->prop);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433 } else {
Greg Hackmann996cdc42013-06-20 11:27:56 -0700434 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435 }
436}
437
438const prop_info *__system_property_find(const char *name)
439{
Colin Cross5e9a0862013-06-24 18:36:39 -0700440 if (__predict_false(compat_mode)) {
441 return __system_property_find_compat(name);
442 }
Greg Hackmann996cdc42013-06-20 11:27:56 -0700443 return find_property(root_node(), name, strlen(name), NULL, 0, false);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800444}
445
446int __system_property_read(const prop_info *pi, char *name, char *value)
447{
448 unsigned serial, len;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700449
Colin Cross5e9a0862013-06-24 18:36:39 -0700450 if (__predict_false(compat_mode)) {
451 return __system_property_read_compat(pi, name, value);
452 }
453
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800454 for(;;) {
455 serial = pi->serial;
456 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700457 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458 serial = pi->serial;
459 }
460 len = SERIAL_VALUE_LEN(serial);
461 memcpy(value, pi->value, len + 1);
Greg Hackmannf7511e32013-06-20 10:33:28 -0700462 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463 if(serial == pi->serial) {
464 if(name != 0) {
465 strcpy(name, pi->name);
466 }
467 return len;
468 }
469 }
470}
471
472int __system_property_get(const char *name, char *value)
473{
474 const prop_info *pi = __system_property_find(name);
475
476 if(pi != 0) {
477 return __system_property_read(pi, 0, value);
478 } else {
479 value[0] = 0;
480 return 0;
481 }
482}
483
satokec7e8cc2011-03-15 11:02:26 +0900484
485static int send_prop_msg(prop_msg *msg)
486{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700487 struct pollfd pollfds[1];
satokec7e8cc2011-03-15 11:02:26 +0900488 struct sockaddr_un addr;
489 socklen_t alen;
490 size_t namelen;
491 int s;
492 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700493 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900494
495 s = socket(AF_LOCAL, SOCK_STREAM, 0);
496 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700497 return result;
satokec7e8cc2011-03-15 11:02:26 +0900498 }
499
500 memset(&addr, 0, sizeof(addr));
501 namelen = strlen(property_service_socket);
502 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
503 addr.sun_family = AF_LOCAL;
504 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
505
Jens Gulinc20d0f32012-07-19 14:10:46 +0200506 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
satokec7e8cc2011-03-15 11:02:26 +0900507 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700508 return result;
satokec7e8cc2011-03-15 11:02:26 +0900509 }
510
511 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
512
513 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700514 // We successfully wrote to the property server but now we
515 // wait for the property server to finish its work. It
516 // acknowledges its completion by closing the socket so we
517 // poll here (on nothing), waiting for the socket to close.
518 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
519 // once the socket closes. Out of paranoia we cap our poll
520 // at 250 ms.
521 pollfds[0].fd = s;
522 pollfds[0].events = 0;
523 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
524 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
525 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700526 } else {
527 // Ignore the timeout and treat it like a success anyway.
528 // The init process is single-threaded and its property
529 // service is sometimes slow to respond (perhaps it's off
530 // starting a child process or something) and thus this
531 // times out and the caller thinks it failed, even though
532 // it's still getting around to it. So we fake it here,
533 // mostly for ctl.* properties, but we do try and wait 250
534 // ms so callers who do read-after-write can reliably see
535 // what they've written. Most of the time.
536 // TODO: fix the system properties design.
537 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700538 }
satokec7e8cc2011-03-15 11:02:26 +0900539 }
540
541 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700542 return result;
satokec7e8cc2011-03-15 11:02:26 +0900543}
544
545int __system_property_set(const char *key, const char *value)
546{
satokec7e8cc2011-03-15 11:02:26 +0900547 int err;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700548 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900549
550 if(key == 0) return -1;
551 if(value == 0) value = "";
552 if(strlen(key) >= PROP_NAME_MAX) return -1;
553 if(strlen(value) >= PROP_VALUE_MAX) return -1;
554
555 memset(&msg, 0, sizeof msg);
556 msg.cmd = PROP_MSG_SETPROP;
557 strlcpy(msg.name, key, sizeof msg.name);
558 strlcpy(msg.value, value, sizeof msg.value);
559
satokec7e8cc2011-03-15 11:02:26 +0900560 err = send_prop_msg(&msg);
561 if(err < 0) {
562 return err;
563 }
564
satokec7e8cc2011-03-15 11:02:26 +0900565 return 0;
566}
567
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800568int __system_property_wait(const prop_info *pi)
569{
570 unsigned n;
571 if(pi == 0) {
Greg Hackmann1540f602013-06-19 13:31:21 -0700572 prop_area *pa = __system_property_area__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800573 n = pa->serial;
574 do {
575 __futex_wait(&pa->serial, n, 0);
576 } while(n == pa->serial);
577 } else {
578 n = pi->serial;
579 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700580 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800581 } while(n == pi->serial);
582 }
583 return 0;
584}
Colin Cross5cf32de2013-01-23 23:07:06 -0800585
586int __system_property_update(prop_info *pi, const char *value, unsigned int len)
587{
Greg Hackmann1540f602013-06-19 13:31:21 -0700588 prop_area *pa = __system_property_area__;
Colin Cross5cf32de2013-01-23 23:07:06 -0800589
590 if (len >= PROP_VALUE_MAX)
591 return -1;
592
593 pi->serial = pi->serial | 1;
Greg Hackmannf7511e32013-06-20 10:33:28 -0700594 ANDROID_MEMBAR_FULL();
Colin Cross5cf32de2013-01-23 23:07:06 -0800595 memcpy(pi->value, value, len + 1);
Greg Hackmannf7511e32013-06-20 10:33:28 -0700596 ANDROID_MEMBAR_FULL();
Colin Cross5cf32de2013-01-23 23:07:06 -0800597 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
598 __futex_wake(&pi->serial, INT32_MAX);
599
600 pa->serial++;
601 __futex_wake(&pa->serial, INT32_MAX);
602
603 return 0;
604}
605
606int __system_property_add(const char *name, unsigned int namelen,
607 const char *value, unsigned int valuelen)
608{
Greg Hackmann1540f602013-06-19 13:31:21 -0700609 prop_area *pa = __system_property_area__;
Greg Hackmann996cdc42013-06-20 11:27:56 -0700610 const prop_info *pi;
Colin Cross5cf32de2013-01-23 23:07:06 -0800611
Colin Cross5cf32de2013-01-23 23:07:06 -0800612 if (namelen >= PROP_NAME_MAX)
613 return -1;
614 if (valuelen >= PROP_VALUE_MAX)
615 return -1;
616 if (namelen < 1)
617 return -1;
618
Greg Hackmann996cdc42013-06-20 11:27:56 -0700619 pi = find_property(root_node(), name, namelen, value, valuelen, true);
620 if (!pi)
Greg Hackmanncb215a72013-02-13 14:41:48 -0800621 return -1;
622
Colin Cross5cf32de2013-01-23 23:07:06 -0800623 pa->serial++;
624 __futex_wake(&pa->serial, INT32_MAX);
Colin Cross5cf32de2013-01-23 23:07:06 -0800625 return 0;
626}
627
628unsigned int __system_property_serial(const prop_info *pi)
629{
630 return pi->serial;
631}
632
633unsigned int __system_property_wait_any(unsigned int serial)
634{
Greg Hackmann1540f602013-06-19 13:31:21 -0700635 prop_area *pa = __system_property_area__;
Colin Cross5cf32de2013-01-23 23:07:06 -0800636
637 do {
638 __futex_wait(&pa->serial, serial, 0);
639 } while(pa->serial == serial);
640
641 return pa->serial;
642}
Greg Hackmann996cdc42013-06-20 11:27:56 -0700643
644struct find_nth_cookie {
645 unsigned count;
646 unsigned n;
647 const prop_info *pi;
648};
649
650static void find_nth_fn(const prop_info *pi, void *ptr)
651{
652 struct find_nth_cookie *cookie = ptr;
653
654 if (cookie->n == cookie->count)
655 cookie->pi = pi;
656
657 cookie->count++;
658}
659
660const prop_info *__system_property_find_nth(unsigned n)
661{
662 struct find_nth_cookie cookie;
663 int err;
664
665 memset(&cookie, 0, sizeof(cookie));
666 cookie.n = n;
667
668 err = __system_property_foreach(find_nth_fn, &cookie);
669 if (err < 0)
670 return NULL;
671
672 return cookie.pi;
673}
674
675static int foreach_property(prop_off_t off,
676 void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
677{
678 prop_bt *trie = to_prop_obj(off);
679 if (!trie)
680 return -1;
681
682 if (trie->left) {
683 int err = foreach_property(trie->left, propfn, cookie);
684 if (err < 0)
685 return -1;
686 }
687 if (trie->prop) {
688 prop_info *info = to_prop_obj(trie->prop);
689 if (!info)
690 return -1;
691 propfn(info, cookie);
692 }
693 if (trie->children) {
694 int err = foreach_property(trie->children, propfn, cookie);
695 if (err < 0)
696 return -1;
697 }
698 if (trie->right) {
699 int err = foreach_property(trie->right, propfn, cookie);
700 if (err < 0)
701 return -1;
702 }
703
704 return 0;
705}
706
707int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
708 void *cookie)
709{
Colin Cross5e9a0862013-06-24 18:36:39 -0700710 if (__predict_false(compat_mode)) {
711 return __system_property_foreach_compat(propfn, cookie);
712 }
Greg Hackmann996cdc42013-06-20 11:27:56 -0700713 return foreach_property(0, propfn, cookie);
714}