blob: 5197ef3fffa9bd5cec39625b9cc1a24080c62775 [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>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037
38#include <sys/mman.h>
39
40#include <sys/socket.h>
41#include <sys/un.h>
42#include <sys/select.h>
Nick Kralevich32417fb2013-01-23 09:28:35 -080043#include <sys/stat.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044#include <sys/types.h>
45#include <netinet/in.h>
satokec7e8cc2011-03-15 11:02:26 +090046#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
48#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
49#include <sys/_system_properties.h>
50
51#include <sys/atomics.h>
52
Colin Cross5cf32de2013-01-23 23:07:06 -080053struct prop_area {
54 unsigned volatile count;
55 unsigned volatile serial;
56 unsigned magic;
57 unsigned version;
58 unsigned reserved[4];
59 unsigned toc[1];
60};
61
62typedef struct prop_area prop_area;
63
64struct prop_info {
65 char name[PROP_NAME_MAX];
66 unsigned volatile serial;
67 char value[PROP_VALUE_MAX];
68};
69
70typedef struct prop_info prop_info;
71
satokec7e8cc2011-03-15 11:02:26 +090072static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
74static unsigned dummy_props = 0;
75
76prop_area *__system_property_area__ = (void*) &dummy_props;
77
Nick Kralevich32417fb2013-01-23 09:28:35 -080078static int get_fd_from_env(void)
79{
80 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
81
82 if (!env) {
83 return -1;
84 }
85
86 return atoi(env);
87}
88
Colin Cross5cf32de2013-01-23 23:07:06 -080089void __system_property_area_init(void *data)
90{
91 prop_area *pa = data;
92 memset(pa, 0, PA_SIZE);
93 pa->magic = PROP_AREA_MAGIC;
94 pa->version = PROP_AREA_VERSION;
95
96 /* plug into the lib property services */
97 __system_property_area__ = pa;
98}
99
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100int __system_properties_init(void)
101{
Nick Kralevich32417fb2013-01-23 09:28:35 -0800102 bool fromFile = true;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800103 int result = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
105 if(__system_property_area__ != ((void*) &dummy_props)) {
106 return 0;
107 }
108
Nick Kralevich32417fb2013-01-23 09:28:35 -0800109 int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
110
111 if ((fd < 0) && (errno == ENOENT)) {
112 /*
113 * For backwards compatibility, if the file doesn't
114 * exist, we use the environment to get the file descriptor.
115 * For security reasons, we only use this backup if the kernel
116 * returns ENOENT. We don't want to use the backup if the kernel
117 * returns other errors such as ENOMEM or ENFILE, since it
118 * might be possible for an external program to trigger this
119 * condition.
120 */
121 fd = get_fd_from_env();
122 fromFile = false;
123 }
124
125 if (fd < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126 return -1;
127 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800128
129 struct stat fd_stat;
130 if (fstat(fd, &fd_stat) < 0) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800131 goto cleanup;
132 }
133
134 if ((fd_stat.st_uid != 0)
135 || (fd_stat.st_gid != 0)
136 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)) {
137 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800139
Elliott Hughes0d787c12013-04-04 13:46:46 -0700140 prop_area *pa = mmap(NULL, fd_stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
Nick Kralevich32417fb2013-01-23 09:28:35 -0800141
Nick Kralevich32417fb2013-01-23 09:28:35 -0800142 if (pa == MAP_FAILED) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800143 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144 }
145
146 if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) {
Nick Kralevich32417fb2013-01-23 09:28:35 -0800147 munmap(pa, fd_stat.st_size);
Nick Kralevichc16961b2013-01-25 13:07:31 -0800148 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149 }
150
151 __system_property_area__ = pa;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800152 result = 0;
153
154cleanup:
155 if (fromFile) {
156 close(fd);
157 }
158
159 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160}
161
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800162int __system_property_foreach(
163 void (*propfn)(const prop_info *pi, void *cookie),
164 void *cookie)
165{
166 prop_area *pa = __system_property_area__;
167 unsigned i;
168
169 for (i = 0; i < pa->count; i++) {
170 unsigned entry = pa->toc[i];
171 prop_info *pi = TOC_TO_INFO(pa, entry);
172 propfn(pi, cookie);
173 }
174
175 return 0;
176}
177
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178const prop_info *__system_property_find_nth(unsigned n)
179{
180 prop_area *pa = __system_property_area__;
181
182 if(n >= pa->count) {
183 return 0;
184 } else {
185 return TOC_TO_INFO(pa, pa->toc[n]);
186 }
187}
188
189const prop_info *__system_property_find(const char *name)
190{
191 prop_area *pa = __system_property_area__;
192 unsigned count = pa->count;
193 unsigned *toc = pa->toc;
194 unsigned len = strlen(name);
195 prop_info *pi;
196
Colin Cross5cf32de2013-01-23 23:07:06 -0800197 if (len >= PROP_NAME_MAX)
198 return 0;
199 if (len < 1)
200 return 0;
201
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202 while(count--) {
203 unsigned entry = *toc++;
204 if(TOC_NAME_LEN(entry) != len) continue;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700205
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206 pi = TOC_TO_INFO(pa, entry);
207 if(memcmp(name, pi->name, len)) continue;
208
209 return pi;
210 }
211
212 return 0;
213}
214
215int __system_property_read(const prop_info *pi, char *name, char *value)
216{
217 unsigned serial, len;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700218
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219 for(;;) {
220 serial = pi->serial;
221 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700222 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223 serial = pi->serial;
224 }
225 len = SERIAL_VALUE_LEN(serial);
226 memcpy(value, pi->value, len + 1);
227 if(serial == pi->serial) {
228 if(name != 0) {
229 strcpy(name, pi->name);
230 }
231 return len;
232 }
233 }
234}
235
236int __system_property_get(const char *name, char *value)
237{
238 const prop_info *pi = __system_property_find(name);
239
240 if(pi != 0) {
241 return __system_property_read(pi, 0, value);
242 } else {
243 value[0] = 0;
244 return 0;
245 }
246}
247
satokec7e8cc2011-03-15 11:02:26 +0900248
249static int send_prop_msg(prop_msg *msg)
250{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700251 struct pollfd pollfds[1];
satokec7e8cc2011-03-15 11:02:26 +0900252 struct sockaddr_un addr;
253 socklen_t alen;
254 size_t namelen;
255 int s;
256 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700257 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900258
259 s = socket(AF_LOCAL, SOCK_STREAM, 0);
260 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700261 return result;
satokec7e8cc2011-03-15 11:02:26 +0900262 }
263
264 memset(&addr, 0, sizeof(addr));
265 namelen = strlen(property_service_socket);
266 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
267 addr.sun_family = AF_LOCAL;
268 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
269
Jens Gulinc20d0f32012-07-19 14:10:46 +0200270 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
satokec7e8cc2011-03-15 11:02:26 +0900271 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700272 return result;
satokec7e8cc2011-03-15 11:02:26 +0900273 }
274
275 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
276
277 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700278 // We successfully wrote to the property server but now we
279 // wait for the property server to finish its work. It
280 // acknowledges its completion by closing the socket so we
281 // poll here (on nothing), waiting for the socket to close.
282 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
283 // once the socket closes. Out of paranoia we cap our poll
284 // at 250 ms.
285 pollfds[0].fd = s;
286 pollfds[0].events = 0;
287 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
288 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
289 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700290 } else {
291 // Ignore the timeout and treat it like a success anyway.
292 // The init process is single-threaded and its property
293 // service is sometimes slow to respond (perhaps it's off
294 // starting a child process or something) and thus this
295 // times out and the caller thinks it failed, even though
296 // it's still getting around to it. So we fake it here,
297 // mostly for ctl.* properties, but we do try and wait 250
298 // ms so callers who do read-after-write can reliably see
299 // what they've written. Most of the time.
300 // TODO: fix the system properties design.
301 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700302 }
satokec7e8cc2011-03-15 11:02:26 +0900303 }
304
305 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700306 return result;
satokec7e8cc2011-03-15 11:02:26 +0900307}
308
309int __system_property_set(const char *key, const char *value)
310{
satokec7e8cc2011-03-15 11:02:26 +0900311 int err;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700312 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900313
314 if(key == 0) return -1;
315 if(value == 0) value = "";
316 if(strlen(key) >= PROP_NAME_MAX) return -1;
317 if(strlen(value) >= PROP_VALUE_MAX) return -1;
318
319 memset(&msg, 0, sizeof msg);
320 msg.cmd = PROP_MSG_SETPROP;
321 strlcpy(msg.name, key, sizeof msg.name);
322 strlcpy(msg.value, value, sizeof msg.value);
323
satokec7e8cc2011-03-15 11:02:26 +0900324 err = send_prop_msg(&msg);
325 if(err < 0) {
326 return err;
327 }
328
satokec7e8cc2011-03-15 11:02:26 +0900329 return 0;
330}
331
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800332int __system_property_wait(const prop_info *pi)
333{
334 unsigned n;
335 if(pi == 0) {
336 prop_area *pa = __system_property_area__;
337 n = pa->serial;
338 do {
339 __futex_wait(&pa->serial, n, 0);
340 } while(n == pa->serial);
341 } else {
342 n = pi->serial;
343 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700344 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345 } while(n == pi->serial);
346 }
347 return 0;
348}
Colin Cross5cf32de2013-01-23 23:07:06 -0800349
350int __system_property_update(prop_info *pi, const char *value, unsigned int len)
351{
352 prop_area *pa = __system_property_area__;
353
354 if (len >= PROP_VALUE_MAX)
355 return -1;
356
357 pi->serial = pi->serial | 1;
358 memcpy(pi->value, value, len + 1);
359 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
360 __futex_wake(&pi->serial, INT32_MAX);
361
362 pa->serial++;
363 __futex_wake(&pa->serial, INT32_MAX);
364
365 return 0;
366}
367
368int __system_property_add(const char *name, unsigned int namelen,
369 const char *value, unsigned int valuelen)
370{
371 prop_area *pa = __system_property_area__;
372 prop_info *pa_info_array = (void*) (((char*) pa) + PA_INFO_START);
373 prop_info *pi;
374
375 if (pa->count == PA_COUNT_MAX)
376 return -1;
377 if (namelen >= PROP_NAME_MAX)
378 return -1;
379 if (valuelen >= PROP_VALUE_MAX)
380 return -1;
381 if (namelen < 1)
382 return -1;
383
384 pi = pa_info_array + pa->count;
385 pi->serial = (valuelen << 24);
386 memcpy(pi->name, name, namelen + 1);
387 memcpy(pi->value, value, valuelen + 1);
388
389 pa->toc[pa->count] =
390 (namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
391
392 pa->count++;
393 pa->serial++;
394 __futex_wake(&pa->serial, INT32_MAX);
395
396 return 0;
397}
398
399unsigned int __system_property_serial(const prop_info *pi)
400{
401 return pi->serial;
402}
403
404unsigned int __system_property_wait_any(unsigned int serial)
405{
406 prop_area *pa = __system_property_area__;
407
408 do {
409 __futex_wait(&pa->serial, serial, 0);
410 } while(pa->serial == serial);
411
412 return pa->serial;
413}