blob: 3bdc9bf60f7646bdd1c7f0f732ba6d1dfe07b64e [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];
satokec7e8cc2011-03-15 11:02:26 +0900161 struct sockaddr_un addr;
162 socklen_t alen;
163 size_t namelen;
164 int s;
165 int r;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700166 int result = -1;
satokec7e8cc2011-03-15 11:02:26 +0900167
168 s = socket(AF_LOCAL, SOCK_STREAM, 0);
169 if(s < 0) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700170 return result;
satokec7e8cc2011-03-15 11:02:26 +0900171 }
172
173 memset(&addr, 0, sizeof(addr));
174 namelen = strlen(property_service_socket);
175 strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
176 addr.sun_family = AF_LOCAL;
177 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
178
179 if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen) < 0)) {
180 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700181 return result;
satokec7e8cc2011-03-15 11:02:26 +0900182 }
183
184 r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
185
186 if(r == sizeof(prop_msg)) {
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700187 // We successfully wrote to the property server but now we
188 // wait for the property server to finish its work. It
189 // acknowledges its completion by closing the socket so we
190 // poll here (on nothing), waiting for the socket to close.
191 // If you 'adb shell setprop foo bar' you'll see the POLLHUP
192 // once the socket closes. Out of paranoia we cap our poll
193 // at 250 ms.
194 pollfds[0].fd = s;
195 pollfds[0].events = 0;
196 r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
197 if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
198 result = 0;
199 }
satokec7e8cc2011-03-15 11:02:26 +0900200 }
201
202 close(s);
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700203 return result;
satokec7e8cc2011-03-15 11:02:26 +0900204}
205
206int __system_property_set(const char *key, const char *value)
207{
satokec7e8cc2011-03-15 11:02:26 +0900208 int err;
satokec7e8cc2011-03-15 11:02:26 +0900209 int tries = 0;
210 int update_seen = 0;
Brad Fitzpatrick23bc3ff2011-03-30 13:10:04 -0700211 prop_msg msg;
satokec7e8cc2011-03-15 11:02:26 +0900212
213 if(key == 0) return -1;
214 if(value == 0) value = "";
215 if(strlen(key) >= PROP_NAME_MAX) return -1;
216 if(strlen(value) >= PROP_VALUE_MAX) return -1;
217
218 memset(&msg, 0, sizeof msg);
219 msg.cmd = PROP_MSG_SETPROP;
220 strlcpy(msg.name, key, sizeof msg.name);
221 strlcpy(msg.value, value, sizeof msg.value);
222
satokec7e8cc2011-03-15 11:02:26 +0900223 err = send_prop_msg(&msg);
224 if(err < 0) {
225 return err;
226 }
227
satokec7e8cc2011-03-15 11:02:26 +0900228 return 0;
229}
230
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231int __system_property_wait(const prop_info *pi)
232{
233 unsigned n;
234 if(pi == 0) {
235 prop_area *pa = __system_property_area__;
236 n = pa->serial;
237 do {
238 __futex_wait(&pa->serial, n, 0);
239 } while(n == pa->serial);
240 } else {
241 n = pi->serial;
242 do {
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700243 __futex_wait((volatile void *)&pi->serial, n, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244 } while(n == pi->serial);
245 }
246 return 0;
247}