blob: c9cf2f75f16aaef4c9ff96469e74aed3156904e1 [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;
Nick Kralevichc16961b2013-01-25 13:07:31 -080072 int result = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
74 if(__system_property_area__ != ((void*) &dummy_props)) {
75 return 0;
76 }
77
Nick Kralevich32417fb2013-01-23 09:28:35 -080078 int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
79
80 if ((fd < 0) && (errno == ENOENT)) {
81 /*
82 * For backwards compatibility, if the file doesn't
83 * exist, we use the environment to get the file descriptor.
84 * For security reasons, we only use this backup if the kernel
85 * returns ENOENT. We don't want to use the backup if the kernel
86 * returns other errors such as ENOMEM or ENFILE, since it
87 * might be possible for an external program to trigger this
88 * condition.
89 */
90 fd = get_fd_from_env();
91 fromFile = false;
92 }
93
94 if (fd < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095 return -1;
96 }
Nick Kralevich32417fb2013-01-23 09:28:35 -080097
98 struct stat fd_stat;
99 if (fstat(fd, &fd_stat) < 0) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800100 goto cleanup;
101 }
102
103 if ((fd_stat.st_uid != 0)
104 || (fd_stat.st_gid != 0)
105 || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)) {
106 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107 }
Nick Kralevich32417fb2013-01-23 09:28:35 -0800108
109 prop_area *pa = mmap(0, fd_stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
110
Nick Kralevich32417fb2013-01-23 09:28:35 -0800111 if (pa == MAP_FAILED) {
Nick Kralevichc16961b2013-01-25 13:07:31 -0800112 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113 }
114
115 if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) {
Nick Kralevich32417fb2013-01-23 09:28:35 -0800116 munmap(pa, fd_stat.st_size);
Nick Kralevichc16961b2013-01-25 13:07:31 -0800117 goto cleanup;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118 }
119
120 __system_property_area__ = pa;
Nick Kralevichc16961b2013-01-25 13:07:31 -0800121 result = 0;
122
123cleanup:
124 if (fromFile) {
125 close(fd);
126 }
127
128 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129}
130
131const prop_info *__system_property_find_nth(unsigned n)
132{
133 prop_area *pa = __system_property_area__;
134
135 if(n >= pa->count) {
136 return 0;
137 } else {
138 return TOC_TO_INFO(pa, pa->toc[n]);
139 }
140}
141
142const prop_info *__system_property_find(const char *name)
143{
144 prop_area *pa = __system_property_area__;
145 unsigned count = pa->count;
146 unsigned *toc = pa->toc;
147 unsigned len = strlen(name);
148 prop_info *pi;
149
150 while(count--) {
151 unsigned entry = *toc++;
152 if(TOC_NAME_LEN(entry) != len) continue;
153
154 pi = TOC_TO_INFO(pa, entry);
155 if(memcmp(name, pi->name, len)) continue;
156
157 return pi;
158 }
159
160 return 0;
161}
162
163int __system_property_read(const prop_info *pi, char *name, char *value)
164{
165 unsigned serial, len;
166
167 for(;;) {
168 serial = pi->serial;
169 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700170 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800171 serial = pi->serial;
172 }
173 len = SERIAL_VALUE_LEN(serial);
174 memcpy(value, pi->value, len + 1);
175 if(serial == pi->serial) {
176 if(name != 0) {
177 strcpy(name, pi->name);
178 }
179 return len;
180 }
181 }
182}
183
184int __system_property_get(const char *name, char *value)
185{
186 const prop_info *pi = __system_property_find(name);
187
188 if(pi != 0) {
189 return __system_property_read(pi, 0, value);
190 } else {
191 value[0] = 0;
192 return 0;
193 }
194}
195
satokec7e8cc2011-03-15 11:02:26 +0900196
197static int send_prop_msg(prop_msg *msg)
198{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700199 struct pollfd pollfds[1];
satokec7e8cc2011-03-15 11:02:26 +0900200 struct sockaddr_un addr;
201 socklen_t alen;
202 size_t namelen;
203 int s;
204 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700205 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900206
207 s = socket(AF_LOCAL, SOCK_STREAM, 0);
208 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700209 return result;
satokec7e8cc2011-03-15 11:02:26 +0900210 }
211
212 memset(&addr, 0, sizeof(addr));
213 namelen = strlen(property_service_socket);
214 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
215 addr.sun_family = AF_LOCAL;
216 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
217
Jens Gulinc20d0f32012-07-19 14:10:46 +0200218 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
satokec7e8cc2011-03-15 11:02:26 +0900219 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700220 return result;
satokec7e8cc2011-03-15 11:02:26 +0900221 }
222
223 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
224
225 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700226 // We successfully wrote to the property server but now we
227 // wait for the property server to finish its work. It
228 // acknowledges its completion by closing the socket so we
229 // poll here (on nothing), waiting for the socket to close.
230 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
231 // once the socket closes. Out of paranoia we cap our poll
232 // at 250 ms.
233 pollfds[0].fd = s;
234 pollfds[0].events = 0;
235 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
236 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
237 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700238 } else {
239 // Ignore the timeout and treat it like a success anyway.
240 // The init process is single-threaded and its property
241 // service is sometimes slow to respond (perhaps it's off
242 // starting a child process or something) and thus this
243 // times out and the caller thinks it failed, even though
244 // it's still getting around to it. So we fake it here,
245 // mostly for ctl.* properties, but we do try and wait 250
246 // ms so callers who do read-after-write can reliably see
247 // what they've written. Most of the time.
248 // TODO: fix the system properties design.
249 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700250 }
satokec7e8cc2011-03-15 11:02:26 +0900251 }
252
253 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700254 return result;
satokec7e8cc2011-03-15 11:02:26 +0900255}
256
257int __system_property_set(const char *key, const char *value)
258{
satokec7e8cc2011-03-15 11:02:26 +0900259 int err;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700260 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900261
262 if(key == 0) return -1;
263 if(value == 0) value = "";
264 if(strlen(key) >= PROP_NAME_MAX) return -1;
265 if(strlen(value) >= PROP_VALUE_MAX) return -1;
266
267 memset(&msg, 0, sizeof msg);
268 msg.cmd = PROP_MSG_SETPROP;
269 strlcpy(msg.name, key, sizeof msg.name);
270 strlcpy(msg.value, value, sizeof msg.value);
271
satokec7e8cc2011-03-15 11:02:26 +0900272 err = send_prop_msg(&msg);
273 if(err < 0) {
274 return err;
275 }
276
satokec7e8cc2011-03-15 11:02:26 +0900277 return 0;
278}
279
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280int __system_property_wait(const prop_info *pi)
281{
282 unsigned n;
283 if(pi == 0) {
284 prop_area *pa = __system_property_area__;
285 n = pa->serial;
286 do {
287 __futex_wait(&pa->serial, n, 0);
288 } while(n == pa->serial);
289 } else {
290 n = pi->serial;
291 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700292 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293 } while(n == pi->serial);
294 }
295 return 0;
296}