blob: a1312af11cdfbbaf0df9c265687cadb8e3063031 [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>
29#include <stdlib.h>
30#include <unistd.h>
31#include <stddef.h>
32#include <errno.h>
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -070033#include <poll.h>
Nick Kralevich32417fb2013-01-23 09:28:35 -080034#include <fcntl.h>
35#include <stdbool.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
37#include <sys/mman.h>
38
39#include <sys/socket.h>
40#include <sys/un.h>
41#include <sys/select.h>
Nick Kralevich32417fb2013-01-23 09:28:35 -080042#include <sys/stat.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043#include <sys/types.h>
44#include <netinet/in.h>
satokec7e8cc2011-03-15 11:02:26 +090045#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046
47#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
48#include <sys/_system_properties.h>
49
50#include <sys/atomics.h>
51
satokec7e8cc2011-03-15 11:02:26 +090052static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
54static unsigned dummy_props = 0;
55
56prop_area *__system_property_area__ = (void*) &dummy_props;
57
Nick Kralevich32417fb2013-01-23 09:28:35 -080058static int get_fd_from_env(void)
59{
60 char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
61
62 if (!env) {
63 return -1;
64 }
65
66 return atoi(env);
67}
68
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069int __system_properties_init(void)
70{
Nick Kralevich32417fb2013-01-23 09:28:35 -080071 bool fromFile = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072
73 if(__system_property_area__ != ((void*) &dummy_props)) {
74 return 0;
75 }
76
Nick Kralevich32417fb2013-01-23 09:28:35 -080077 int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
78
79 if ((fd < 0) && (errno == ENOENT)) {
80 /*
81 * For backwards compatibility, if the file doesn't
82 * exist, we use the environment to get the file descriptor.
83 * For security reasons, we only use this backup if the kernel
84 * returns ENOENT. We don't want to use the backup if the kernel
85 * returns other errors such as ENOMEM or ENFILE, since it
86 * might be possible for an external program to trigger this
87 * condition.
88 */
89 fd = get_fd_from_env();
90 fromFile = false;
91 }
92
93 if (fd < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094 return -1;
95 }
Nick Kralevich32417fb2013-01-23 09:28:35 -080096
97 struct stat fd_stat;
98 if (fstat(fd, &fd_stat) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099 return -1;
100 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800101
102 prop_area *pa = mmap(0, fd_stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
103
104 if (fromFile) {
105 close(fd);
106 }
107
108 if (pa == MAP_FAILED) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109 return -1;
110 }
111
112 if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) {
Nick Kralevich32417fb2013-01-23 09:28:35 -0800113 munmap(pa, fd_stat.st_size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114 return -1;
115 }
116
117 __system_property_area__ = pa;
118 return 0;
119}
120
121const prop_info *__system_property_find_nth(unsigned n)
122{
123 prop_area *pa = __system_property_area__;
124
125 if(n >= pa->count) {
126 return 0;
127 } else {
128 return TOC_TO_INFO(pa, pa->toc[n]);
129 }
130}
131
132const prop_info *__system_property_find(const char *name)
133{
134 prop_area *pa = __system_property_area__;
135 unsigned count = pa->count;
136 unsigned *toc = pa->toc;
137 unsigned len = strlen(name);
138 prop_info *pi;
139
140 while(count--) {
141 unsigned entry = *toc++;
142 if(TOC_NAME_LEN(entry) != len) continue;
143
144 pi = TOC_TO_INFO(pa, entry);
145 if(memcmp(name, pi->name, len)) continue;
146
147 return pi;
148 }
149
150 return 0;
151}
152
153int __system_property_read(const prop_info *pi, char *name, char *value)
154{
155 unsigned serial, len;
156
157 for(;;) {
158 serial = pi->serial;
159 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700160 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161 serial = pi->serial;
162 }
163 len = SERIAL_VALUE_LEN(serial);
164 memcpy(value, pi->value, len + 1);
165 if(serial == pi->serial) {
166 if(name != 0) {
167 strcpy(name, pi->name);
168 }
169 return len;
170 }
171 }
172}
173
174int __system_property_get(const char *name, char *value)
175{
176 const prop_info *pi = __system_property_find(name);
177
178 if(pi != 0) {
179 return __system_property_read(pi, 0, value);
180 } else {
181 value[0] = 0;
182 return 0;
183 }
184}
185
satokec7e8cc2011-03-15 11:02:26 +0900186
187static int send_prop_msg(prop_msg *msg)
188{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700189 struct pollfd pollfds[1];
satokec7e8cc2011-03-15 11:02:26 +0900190 struct sockaddr_un addr;
191 socklen_t alen;
192 size_t namelen;
193 int s;
194 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700195 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900196
197 s = socket(AF_LOCAL, SOCK_STREAM, 0);
198 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700199 return result;
satokec7e8cc2011-03-15 11:02:26 +0900200 }
201
202 memset(&addr, 0, sizeof(addr));
203 namelen = strlen(property_service_socket);
204 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
205 addr.sun_family = AF_LOCAL;
206 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
207
Jens Gulinc20d0f32012-07-19 14:10:46 +0200208 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
satokec7e8cc2011-03-15 11:02:26 +0900209 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700210 return result;
satokec7e8cc2011-03-15 11:02:26 +0900211 }
212
213 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
214
215 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700216 // We successfully wrote to the property server but now we
217 // wait for the property server to finish its work. It
218 // acknowledges its completion by closing the socket so we
219 // poll here (on nothing), waiting for the socket to close.
220 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
221 // once the socket closes. Out of paranoia we cap our poll
222 // at 250 ms.
223 pollfds[0].fd = s;
224 pollfds[0].events = 0;
225 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
226 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
227 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700228 } else {
229 // Ignore the timeout and treat it like a success anyway.
230 // The init process is single-threaded and its property
231 // service is sometimes slow to respond (perhaps it's off
232 // starting a child process or something) and thus this
233 // times out and the caller thinks it failed, even though
234 // it's still getting around to it. So we fake it here,
235 // mostly for ctl.* properties, but we do try and wait 250
236 // ms so callers who do read-after-write can reliably see
237 // what they've written. Most of the time.
238 // TODO: fix the system properties design.
239 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700240 }
satokec7e8cc2011-03-15 11:02:26 +0900241 }
242
243 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700244 return result;
satokec7e8cc2011-03-15 11:02:26 +0900245}
246
247int __system_property_set(const char *key, const char *value)
248{
satokec7e8cc2011-03-15 11:02:26 +0900249 int err;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700250 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900251
252 if(key == 0) return -1;
253 if(value == 0) value = "";
254 if(strlen(key) >= PROP_NAME_MAX) return -1;
255 if(strlen(value) >= PROP_VALUE_MAX) return -1;
256
257 memset(&msg, 0, sizeof msg);
258 msg.cmd = PROP_MSG_SETPROP;
259 strlcpy(msg.name, key, sizeof msg.name);
260 strlcpy(msg.value, value, sizeof msg.value);
261
satokec7e8cc2011-03-15 11:02:26 +0900262 err = send_prop_msg(&msg);
263 if(err < 0) {
264 return err;
265 }
266
satokec7e8cc2011-03-15 11:02:26 +0900267 return 0;
268}
269
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270int __system_property_wait(const prop_info *pi)
271{
272 unsigned n;
273 if(pi == 0) {
274 prop_area *pa = __system_property_area__;
275 n = pa->serial;
276 do {
277 __futex_wait(&pa->serial, n, 0);
278 } while(n == pa->serial);
279 } else {
280 n = pi->serial;
281 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700282 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283 } while(n == pi->serial);
284 }
285 return 0;
286}