blob: 756ee3fa57a8347797bc34d7697b2852f970131e [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>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034
35#include <sys/mman.h>
36
37#include <sys/socket.h>
38#include <sys/un.h>
39#include <sys/select.h>
40#include <sys/types.h>
41#include <netinet/in.h>
satokec7e8cc2011-03-15 11:02:26 +090042#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043
44#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
45#include <sys/_system_properties.h>
46
47#include <sys/atomics.h>
48
satokec7e8cc2011-03-15 11:02:26 +090049static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
51static unsigned dummy_props = 0;
52
53prop_area *__system_property_area__ = (void*) &dummy_props;
54
55int __system_properties_init(void)
56{
57 prop_area *pa;
58 int s, fd;
59 unsigned sz;
60 char *env;
61
62 if(__system_property_area__ != ((void*) &dummy_props)) {
63 return 0;
64 }
65
66 env = getenv("ANDROID_PROPERTY_WORKSPACE");
67 if (!env) {
68 return -1;
69 }
70 fd = atoi(env);
71 env = strchr(env, ',');
72 if (!env) {
73 return -1;
74 }
75 sz = atoi(env + 1);
76
77 pa = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
78
79 if(pa == MAP_FAILED) {
80 return -1;
81 }
82
83 if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) {
84 munmap(pa, sz);
85 return -1;
86 }
87
88 __system_property_area__ = pa;
89 return 0;
90}
91
92const prop_info *__system_property_find_nth(unsigned n)
93{
94 prop_area *pa = __system_property_area__;
95
96 if(n >= pa->count) {
97 return 0;
98 } else {
99 return TOC_TO_INFO(pa, pa->toc[n]);
100 }
101}
102
103const prop_info *__system_property_find(const char *name)
104{
105 prop_area *pa = __system_property_area__;
106 unsigned count = pa->count;
107 unsigned *toc = pa->toc;
108 unsigned len = strlen(name);
109 prop_info *pi;
110
111 while(count--) {
112 unsigned entry = *toc++;
113 if(TOC_NAME_LEN(entry) != len) continue;
114
115 pi = TOC_TO_INFO(pa, entry);
116 if(memcmp(name, pi->name, len)) continue;
117
118 return pi;
119 }
120
121 return 0;
122}
123
124int __system_property_read(const prop_info *pi, char *name, char *value)
125{
126 unsigned serial, len;
127
128 for(;;) {
129 serial = pi->serial;
130 while(SERIAL_DIRTY(serial)) {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700131 __futex_wait((volatile void *)&pi->serial, serial, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132 serial = pi->serial;
133 }
134 len = SERIAL_VALUE_LEN(serial);
135 memcpy(value, pi->value, len + 1);
136 if(serial == pi->serial) {
137 if(name != 0) {
138 strcpy(name, pi->name);
139 }
140 return len;
141 }
142 }
143}
144
145int __system_property_get(const char *name, char *value)
146{
147 const prop_info *pi = __system_property_find(name);
148
149 if(pi != 0) {
150 return __system_property_read(pi, 0, value);
151 } else {
152 value[0] = 0;
153 return 0;
154 }
155}
156
satokec7e8cc2011-03-15 11:02:26 +0900157
158static int send_prop_msg(prop_msg *msg)
159{
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700160 struct pollfd pollfds[1];
Bernhard Rosenkraenzer8d6c7d42011-12-07 01:28:27 +0059161 union {
162 struct sockaddr_un addr;
163 struct sockaddr addr_g;
164 } addr;
satokec7e8cc2011-03-15 11:02:26 +0900165 socklen_t alen;
166 size_t namelen;
167 int s;
168 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700169 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900170
171 s = socket(AF_LOCAL, SOCK_STREAM, 0);
172 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700173 return result;
satokec7e8cc2011-03-15 11:02:26 +0900174 }
175
176 memset(&addr, 0, sizeof(addr));
177 namelen = strlen(property_service_socket);
Bernhard Rosenkraenzer8d6c7d42011-12-07 01:28:27 +0059178 strlcpy(addr.addr.sun_path, property_service_socket, sizeof addr.addr.sun_path);
179 addr.addr.sun_family = AF_LOCAL;
satokec7e8cc2011-03-15 11:02:26 +0900180 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
181
Bernhard Rosenkraenzer8d6c7d42011-12-07 01:28:27 +0059182 if(TEMP_FAILURE_RETRY(connect(s, &addr.addr_g, alen) < 0)) {
satokec7e8cc2011-03-15 11:02:26 +0900183 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700184 return result;
satokec7e8cc2011-03-15 11:02:26 +0900185 }
186
187 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
188
189 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700190 // We successfully wrote to the property server but now we
191 // wait for the property server to finish its work. It
192 // acknowledges its completion by closing the socket so we
193 // poll here (on nothing), waiting for the socket to close.
194 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
195 // once the socket closes. Out of paranoia we cap our poll
196 // at 250 ms.
197 pollfds[0].fd = s;
198 pollfds[0].events = 0;
199 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
200 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
201 result = 0;
Brad Fitzpatrick8da75ab2011-04-01 10:53:12 -0700202 } else {
203 // Ignore the timeout and treat it like a success anyway.
204 // The init process is single-threaded and its property
205 // service is sometimes slow to respond (perhaps it's off
206 // starting a child process or something) and thus this
207 // times out and the caller thinks it failed, even though
208 // it's still getting around to it. So we fake it here,
209 // mostly for ctl.* properties, but we do try and wait 250
210 // ms so callers who do read-after-write can reliably see
211 // what they've written. Most of the time.
212 // TODO: fix the system properties design.
213 result = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700214 }
satokec7e8cc2011-03-15 11:02:26 +0900215 }
216
217 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700218 return result;
satokec7e8cc2011-03-15 11:02:26 +0900219}
220
221int __system_property_set(const char *key, const char *value)
222{
satokec7e8cc2011-03-15 11:02:26 +0900223 int err;
satokec7e8cc2011-03-15 11:02:26 +0900224 int tries = 0;
225 int update_seen = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700226 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900227
228 if(key == 0) return -1;
229 if(value == 0) value = "";
230 if(strlen(key) >= PROP_NAME_MAX) return -1;
231 if(strlen(value) >= PROP_VALUE_MAX) return -1;
232
233 memset(&msg, 0, sizeof msg);
234 msg.cmd = PROP_MSG_SETPROP;
235 strlcpy(msg.name, key, sizeof msg.name);
236 strlcpy(msg.value, value, sizeof msg.value);
237
satokec7e8cc2011-03-15 11:02:26 +0900238 err = send_prop_msg(&msg);
239 if(err < 0) {
240 return err;
241 }
242
satokec7e8cc2011-03-15 11:02:26 +0900243 return 0;
244}
245
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246int __system_property_wait(const prop_info *pi)
247{
248 unsigned n;
249 if(pi == 0) {
250 prop_area *pa = __system_property_area__;
251 n = pa->serial;
252 do {
253 __futex_wait(&pa->serial, n, 0);
254 } while(n == pa->serial);
255 } else {
256 n = pi->serial;
257 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700258 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800259 } while(n == pi->serial);
260 }
261 return 0;
262}