blob: d96950709310e62766830da4a5150edc8eb643c8 [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
162const prop_info *__system_property_find_nth(unsigned n)
163{
164 prop_area *pa = __system_property_area__;
165
166 if(n >= pa->count) {
167 return 0;
168 } else {
169 return TOC_TO_INFO(pa, pa->toc[n]);
170 }
171}
172
173const prop_info *__system_property_find(const char *name)
174{
175 prop_area *pa = __system_property_area__;
176 unsigned count = pa->count;
177 unsigned *toc = pa->toc;
178 unsigned len = strlen(name);
179 prop_info *pi;
180
Colin Cross5cf32de2013-01-23 23:07:06 -0800181 if (len >= PROP_NAME_MAX)
182 return 0;
183 if (len < 1)
184 return 0;
185
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186 while(count--) {
187 unsigned entry = *toc++;
188 if(TOC_NAME_LEN(entry) != len) continue;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700189
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190 pi = TOC_TO_INFO(pa, entry);
191 if(memcmp(name, pi->name, len)) continue;
192
193 return pi;
194 }
195
196 return 0;
197}
198
199int __system_property_read(const prop_info *pi, char *name, char *value)
200{
201 unsigned serial, len;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700202
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203 for(;;) {
204 serial = pi->serial;
205 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700206 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207 serial = pi->serial;
208 }
209 len = SERIAL_VALUE_LEN(serial);
210 memcpy(value, pi->value, len + 1);
211 if(serial == pi->serial) {
212 if(name != 0) {
213 strcpy(name, pi->name);
214 }
215 return len;
216 }
217 }
218}
219
220int __system_property_get(const char *name, char *value)
221{
222 const prop_info *pi = __system_property_find(name);
223
224 if(pi != 0) {
225 return __system_property_read(pi, 0, value);
226 } else {
227 value[0] = 0;
228 return 0;
229 }
230}
231
satokec7e8cc2011-03-15 11:02:26 +0900232
233static int send_prop_msg(prop_msg *msg)
234{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700235 struct pollfd pollfds[1];
satokec7e8cc2011-03-15 11:02:26 +0900236 struct sockaddr_un addr;
237 socklen_t alen;
238 size_t namelen;
239 int s;
240 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700241 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900242
243 s = socket(AF_LOCAL, SOCK_STREAM, 0);
244 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700245 return result;
satokec7e8cc2011-03-15 11:02:26 +0900246 }
247
248 memset(&addr, 0, sizeof(addr));
249 namelen = strlen(property_service_socket);
250 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
251 addr.sun_family = AF_LOCAL;
252 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
253
Jens Gulinc20d0f32012-07-19 14:10:46 +0200254 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
satokec7e8cc2011-03-15 11:02:26 +0900255 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700256 return result;
satokec7e8cc2011-03-15 11:02:26 +0900257 }
258
259 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
260
261 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700262 // We successfully wrote to the property server but now we
263 // wait for the property server to finish its work. It
264 // acknowledges its completion by closing the socket so we
265 // poll here (on nothing), waiting for the socket to close.
266 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
267 // once the socket closes. Out of paranoia we cap our poll
268 // at 250 ms.
269 pollfds[0].fd = s;
270 pollfds[0].events = 0;
271 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
272 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
273 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700274 } else {
275 // Ignore the timeout and treat it like a success anyway.
276 // The init process is single-threaded and its property
277 // service is sometimes slow to respond (perhaps it's off
278 // starting a child process or something) and thus this
279 // times out and the caller thinks it failed, even though
280 // it's still getting around to it. So we fake it here,
281 // mostly for ctl.* properties, but we do try and wait 250
282 // ms so callers who do read-after-write can reliably see
283 // what they've written. Most of the time.
284 // TODO: fix the system properties design.
285 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700286 }
satokec7e8cc2011-03-15 11:02:26 +0900287 }
288
289 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700290 return result;
satokec7e8cc2011-03-15 11:02:26 +0900291}
292
293int __system_property_set(const char *key, const char *value)
294{
satokec7e8cc2011-03-15 11:02:26 +0900295 int err;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700296 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900297
298 if(key == 0) return -1;
299 if(value == 0) value = "";
300 if(strlen(key) >= PROP_NAME_MAX) return -1;
301 if(strlen(value) >= PROP_VALUE_MAX) return -1;
302
303 memset(&msg, 0, sizeof msg);
304 msg.cmd = PROP_MSG_SETPROP;
305 strlcpy(msg.name, key, sizeof msg.name);
306 strlcpy(msg.value, value, sizeof msg.value);
307
satokec7e8cc2011-03-15 11:02:26 +0900308 err = send_prop_msg(&msg);
309 if(err < 0) {
310 return err;
311 }
312
satokec7e8cc2011-03-15 11:02:26 +0900313 return 0;
314}
315
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316int __system_property_wait(const prop_info *pi)
317{
318 unsigned n;
319 if(pi == 0) {
320 prop_area *pa = __system_property_area__;
321 n = pa->serial;
322 do {
323 __futex_wait(&pa->serial, n, 0);
324 } while(n == pa->serial);
325 } else {
326 n = pi->serial;
327 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700328 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329 } while(n == pi->serial);
330 }
331 return 0;
332}
Colin Cross5cf32de2013-01-23 23:07:06 -0800333
334int __system_property_update(prop_info *pi, const char *value, unsigned int len)
335{
336 prop_area *pa = __system_property_area__;
337
338 if (len >= PROP_VALUE_MAX)
339 return -1;
340
341 pi->serial = pi->serial | 1;
342 memcpy(pi->value, value, len + 1);
343 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
344 __futex_wake(&pi->serial, INT32_MAX);
345
346 pa->serial++;
347 __futex_wake(&pa->serial, INT32_MAX);
348
349 return 0;
350}
351
352int __system_property_add(const char *name, unsigned int namelen,
353 const char *value, unsigned int valuelen)
354{
355 prop_area *pa = __system_property_area__;
356 prop_info *pa_info_array = (void*) (((char*) pa) + PA_INFO_START);
357 prop_info *pi;
358
359 if (pa->count == PA_COUNT_MAX)
360 return -1;
361 if (namelen >= PROP_NAME_MAX)
362 return -1;
363 if (valuelen >= PROP_VALUE_MAX)
364 return -1;
365 if (namelen < 1)
366 return -1;
367
368 pi = pa_info_array + pa->count;
369 pi->serial = (valuelen << 24);
370 memcpy(pi->name, name, namelen + 1);
371 memcpy(pi->value, value, valuelen + 1);
372
373 pa->toc[pa->count] =
374 (namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
375
376 pa->count++;
377 pa->serial++;
378 __futex_wake(&pa->serial, INT32_MAX);
379
380 return 0;
381}
382
383unsigned int __system_property_serial(const prop_info *pi)
384{
385 return pi->serial;
386}
387
388unsigned int __system_property_wait_any(unsigned int serial)
389{
390 prop_area *pa = __system_property_area__;
391
392 do {
393 __futex_wait(&pa->serial, serial, 0);
394 } while(pa->serial == serial);
395
396 return pa->serial;
397}