blob: 4b1aedd687058d21d8e9af3d09244b8a6d00aa7f [file] [log] [blame]
The Android Open Source Project52d4c302009-03-03 19:29:09 -08001//
2// Copyright 2007 The Android Open Source Project
3//
4// Property sever. Mimics behavior provided on the device by init(8) and
5// some code built into libc.
6//
7
8// For compilers that support precompilation, include "wx/wx.h".
9#include "wx/wxprec.h"
10
11// Otherwise, include all standard headers
12#ifndef WX_PRECOMP
13# include "wx/wx.h"
14#endif
15#include "wx/image.h"
16
17#include "PropertyServer.h"
18#include "MyApp.h"
19#include "Preferences.h"
20#include "MainFrame.h"
21#include "utils.h"
22
23#include <stdlib.h>
24#include <unistd.h>
25#include <string.h>
26#include <errno.h>
27#include <assert.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <sys/stat.h>
31#include <sys/un.h>
32
33
34using namespace android;
35
36const char* PropertyServer::kPropCheckJni = "ro.kernel.android.checkjni";
37
38/*
39 * Destructor.
40 */
41PropertyServer::~PropertyServer(void)
42{
43 if (IsRunning()) {
44 // TODO: cause thread to stop, then Wait for it
45 }
46 printf("Sim: in ~PropertyServer()\n");
47}
48
49/*
50 * Create and run the thread.
51 */
52bool PropertyServer::StartThread(void)
53{
54 if (Create() != wxTHREAD_NO_ERROR) {
55 fprintf(stderr, "Sim: ERROR: can't create PropertyServer thread\n");
56 return false;
57 }
58
59 Run();
60 return true;
61}
62
63
64/*
65 * Clear out the list.
66 */
67void PropertyServer::ClearProperties(void)
68{
69 typedef List<Property>::iterator PropIter;
70
71 for (PropIter pi = mPropList.begin(); pi != mPropList.end(); ++pi) {
72 pi = mPropList.erase(pi);
73 }
74}
75
76/*
77 * Set default values for several properties.
78 */
79void PropertyServer::SetDefaultProperties(void)
80{
81 static const struct {
82 const char* key;
83 const char* value;
84 } propList[] = {
85 { "net.bt.name", "Android" },
86 { "ro.kernel.mem", "60M" },
87 { "ro.kernel.board_sardine.version", "4" },
88 { "ro.kernel.console", "null" },
89 { "ro.build.id", "engineering" },
90 { "ro.build.date", "Wed Nov 28 07:44:14 PST 2007" },
91 { "ro.build.date.utc", "1196264654" },
92 { "ro.build.type", "eng" },
93 { "ro.product.device", "simulator" /*"sooner"*/ },
94 { "ro.product.brand", "generic" },
95 { "ro.build.user", "fadden" },
96 { "ro.build.host", "marathon" },
97 { "ro.config.nocheckin", "yes" },
98 { "ro.product.manufacturer", "" },
99 { "ro.radio.use-ppp", "no" },
100 { "ro.FOREGROUND_APP_ADJ", "0" },
101 { "ro.VISIBLE_APP_ADJ", "1" },
102 { "ro.SECONDARY_SERVER_ADJ", "2" },
103 { "ro.HIDDEN_APP_MIN_ADJ", "7" },
104 { "ro.CONTENT_PROVIDER_ADJ", "14" },
105 { "ro.EMPTY_APP_ADJ", "15" },
106 { "ro.FOREGROUND_APP_MEM", "1536" },
107 { "ro.VISIBLE_APP_MEM", "2048" },
108 { "ro.SECONDARY_SERVER_MEM", "4096" },
109 { "ro.HIDDEN_APP_MEM", "8192" },
110 { "ro.EMPTY_APP_MEM", "16384" },
The Android Open Source Project692ab022009-03-09 11:52:11 -0700111 { "ro.HOME_APP_ADJ", "4" },
112 { "ro.HOME_APP_MEM", "4096" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800113 //{ "init.svc.adbd", "running" }, // causes ADB-JDWP
114 { "init.svc.usbd", "running" },
115 { "init.svc.debuggerd", "running" },
116 { "init.svc.ril-daemon", "running" },
117 { "init.svc.zygote", "running" },
118 { "init.svc.runtime", "running" },
119 { "init.svc.dbus", "running" },
120 { "init.svc.pppd_gprs", "running" },
121 { "adb.connected", "0" },
122 //{ "use-adb-networking", "1" },
123 /*
124 { "status.battery.state", "Slow" },
125 { "status.battery.level", "5" },
126 { "status.battery.level_raw", "50" },
127 { "status.battery.level_scale", "9" },
128 */
129
130 /* disable the annoying setup wizard */
131 { "app.setupwizard.disable", "1" },
132
133 /* Dalvik options, set by AndroidRuntime */
134 { "dalvik.vm.stack-trace-file", "/data/anr/traces.txt" },
135 //{ "dalvik.vm.execution-mode", "int:portable" },
136 { "dalvik.vm.enableassertions", "all" }, // -ea
137 { "dalvik.vm.dexopt-flags", "" }, // e.g. "v=a,o=v,m=n"
138 { "dalvik.vm.deadlock-predict", "off" }, // -Xdeadlockpredict
139 //{ "dalvik.vm.jniopts", "forcecopy" }, // -Xjniopts
140 { "log.redirect-stdio", "false" }, // -Xlog-stdio
141
142 /* SurfaceFlinger options */
143 { "debug.sf.nobootanimation", "1" },
144 { "debug.sf.showupdates", "0" },
145 { "debug.sf.showcpu", "0" },
146 { "debug.sf.showbackground", "0" },
147 { "debug.sf.showfps", "0" },
148 };
149
150 for (int i = 0; i < NELEM(propList); i++)
151 SetProperty(propList[i].key, propList[i].value);
152
153 Preferences* pPrefs = ((MyApp*)wxTheApp)->GetPrefs();
154 bool doCheckJni = false;
155
156 pPrefs->GetBool("check-jni", &doCheckJni);
157 if (doCheckJni)
158 SetProperty(kPropCheckJni, "1");
159 else
160 SetProperty(kPropCheckJni, "0");
161}
162
163/*
164 * Get the value of a property.
165 *
166 * "valueBuf" must hold at least PROPERTY_VALUE_MAX bytes.
167 *
168 * Returns "true" if the property was found.
169 */
170bool PropertyServer::GetProperty(const char* key, char* valueBuf)
171{
172 typedef List<Property>::iterator PropIter;
173
174 assert(key != NULL);
175 assert(valueBuf != NULL);
176
177 for (PropIter pi = mPropList.begin(); pi != mPropList.end(); ++pi) {
178 Property& prop = *pi;
179 if (strcmp(prop.key, key) == 0) {
180 if (strlen(prop.value) >= PROPERTY_VALUE_MAX) {
181 fprintf(stderr,
182 "GLITCH: properties table holds '%s' '%s' (len=%d)\n",
183 prop.key, prop.value, strlen(prop.value));
184 abort();
185 }
186 assert(strlen(prop.value) < PROPERTY_VALUE_MAX);
187 strcpy(valueBuf, prop.value);
188 return true;
189 }
190 }
191
192 //printf("Prop: get [%s] not found\n", key);
193 return false;
194}
195
196/*
197 * Set the value of a property, replacing it if it already exists.
198 *
199 * If "value" is NULL, the property is removed.
200 *
201 * If the property is immutable, this returns "false" without doing
202 * anything. (Not implemented.)
203 */
204bool PropertyServer::SetProperty(const char* key, const char* value)
205{
206 typedef List<Property>::iterator PropIter;
207
208 assert(key != NULL);
209 assert(value != NULL);
210
211 for (PropIter pi = mPropList.begin(); pi != mPropList.end(); ++pi) {
212 Property& prop = *pi;
213 if (strcmp(prop.key, key) == 0) {
214 if (value != NULL) {
215 //printf("Prop: replacing [%s]: [%s] with [%s]\n",
216 // prop.key, prop.value, value);
217 strcpy(prop.value, value);
218 } else {
219 //printf("Prop: removing [%s]\n", prop.key);
220 mPropList.erase(pi);
221 }
222 return true;
223 }
224 }
225
226 //printf("Prop: adding [%s]: [%s]\n", key, value);
227 Property tmp;
228 strcpy(tmp.key, key);
229 strcpy(tmp.value, value);
230 mPropList.push_back(tmp);
231 return true;
232}
233
234/*
235 * Create a UNIX domain socket, carefully removing it if it already
236 * exists.
237 */
238bool PropertyServer::CreateSocket(const char* fileName)
239{
240 struct stat sb;
241 bool result = false;
242 int sock = -1;
243 int cc;
244
245 cc = stat(fileName, &sb);
246 if (cc < 0) {
247 if (errno != ENOENT) {
248 LOG(LOG_ERROR, "sim-prop",
249 "Unable to stat '%s' (errno=%d)\n", fileName, errno);
250 goto bail;
251 }
252 } else {
253 /* don't touch it if it's not a socket */
254 if (!(S_ISSOCK(sb.st_mode))) {
255 LOG(LOG_ERROR, "sim-prop",
256 "File '%s' exists and is not a socket\n", fileName);
257 goto bail;
258 }
259
260 /* remove the cruft */
261 if (unlink(fileName) < 0) {
262 LOG(LOG_ERROR, "sim-prop",
263 "Unable to remove '%s' (errno=%d)\n", fileName, errno);
264 goto bail;
265 }
266 }
267
268 struct sockaddr_un addr;
269
270 sock = ::socket(AF_UNIX, SOCK_STREAM, 0);
271 if (sock < 0) {
272 LOG(LOG_ERROR, "sim-prop",
273 "UNIX domain socket create failed (errno=%d)\n", errno);
274 goto bail;
275 }
276
277 /* bind the socket; this creates the file on disk */
278 strcpy(addr.sun_path, fileName); // max 108 bytes
279 addr.sun_family = AF_UNIX;
280 cc = ::bind(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
281 if (cc < 0) {
282 LOG(LOG_ERROR, "sim",
283 "AF_UNIX bind failed for '%s' (errno=%d)\n", fileName, errno);
284 goto bail;
285 }
286
287 cc = ::listen(sock, 5);
288 if (cc < 0) {
289 LOG(LOG_ERROR, "sim", "AF_UNIX listen failed (errno=%d)\n", errno);
290 goto bail;
291 }
292
293 mListenSock = sock;
294 sock = -1;
295 result = true;
296
297bail:
298 if (sock >= 0)
299 close(sock);
300 return result;
301}
302
303/*
304 * Handle a client request.
305 *
306 * Returns true on success, false if the fd should be closed.
307 */
308bool PropertyServer::HandleRequest(int fd)
309{
310 char reqBuf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX];
311 char valueBuf[1 + PROPERTY_VALUE_MAX];
312 ssize_t actual;
313
314 memset(valueBuf, 'x', sizeof(valueBuf)); // placate valgrind
315
316 /* read the command byte; this determines the message length */
317 actual = read(fd, reqBuf, 1);
318 if (actual <= 0)
319 return false;
320
321 if (reqBuf[0] == kSystemPropertyGet) {
322 actual = read(fd, reqBuf, PROPERTY_KEY_MAX);
323
324 if (actual != PROPERTY_KEY_MAX) {
325 fprintf(stderr, "Bad read on get: %d of %d\n",
326 actual, PROPERTY_KEY_MAX);
327 return false;
328 }
329 if (GetProperty(reqBuf, valueBuf+1))
330 valueBuf[0] = 1;
331 else
332 valueBuf[0] = 0;
333 //printf("GET property [%s]: (found=%d) [%s]\n",
334 // reqBuf, valueBuf[0], valueBuf+1);
335 if (write(fd, valueBuf, sizeof(valueBuf)) != sizeof(valueBuf)) {
336 fprintf(stderr, "Bad write on get\n");
337 return false;
338 }
339 } else if (reqBuf[0] == kSystemPropertySet) {
340 actual = read(fd, reqBuf, PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX);
341 if (actual != PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX) {
342 fprintf(stderr, "Bad read on set: %d of %d\n",
343 actual, PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX);
344 return false;
345 }
346 //printf("SET property '%s'\n", reqBuf);
347 if (SetProperty(reqBuf, reqBuf + PROPERTY_KEY_MAX))
348 valueBuf[0] = 1;
349 else
350 valueBuf[0] = 0;
351 if (write(fd, valueBuf, 1) != 1) {
352 fprintf(stderr, "Bad write on set\n");
353 return false;
354 }
355 } else if (reqBuf[0] == kSystemPropertyList) {
356 /* TODO */
357 assert(false);
358 } else {
359 fprintf(stderr, "Unexpected request %d from prop client\n", reqBuf[0]);
360 return false;
361 }
362
363 return true;
364}
365
366/*
367 * Serve up properties.
368 */
369void PropertyServer::ServeProperties(void)
370{
371 typedef List<int>::iterator IntIter;
372 fd_set readfds;
373 int maxfd;
374
375 while (true) {
376 int cc;
377
378 FD_ZERO(&readfds);
379 FD_SET(mListenSock, &readfds);
380 maxfd = mListenSock;
381
382 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ++ii) {
383 int fd = (*ii);
384
385 FD_SET(fd, &readfds);
386 if (maxfd < fd)
387 maxfd = fd;
388 }
389
390 cc = select(maxfd+1, &readfds, NULL, NULL, NULL);
391 if (cc < 0) {
392 if (errno == EINTR) {
393 printf("hiccup!\n");
394 continue;
395 }
396 return;
397 }
398 if (FD_ISSET(mListenSock, &readfds)) {
399 struct sockaddr_un from;
400 socklen_t fromlen;
401 int newSock;
402
403 fromlen = sizeof(from);
404 newSock = ::accept(mListenSock, (struct sockaddr*) &from, &fromlen);
405 if (newSock < 0) {
406 LOG(LOG_WARN, "sim",
407 "AF_UNIX accept failed (errno=%d)\n", errno);
408 } else {
409 //printf("new props connection on %d --> %d\n",
410 // mListenSock, newSock);
411
412 mClientList.push_back(newSock);
413 }
414 }
415
416 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ) {
417 int fd = (*ii);
418 bool ok = true;
419
420 if (FD_ISSET(fd, &readfds)) {
421 //printf("--- activity on %d\n", fd);
422
423 ok = HandleRequest(fd);
424 }
425
426 if (ok) {
427 ++ii;
428 } else {
429 //printf("--- closing %d\n", fd);
430 close(fd);
431 ii = mClientList.erase(ii);
432 }
433 }
434 }
435}
436
437/*
438 * Thread entry point.
439 *
440 * This just sits and waits for a new connection. It hands it off to the
441 * main thread and then goes back to waiting.
442 *
443 * There is currently no "polite" way to shut this down.
444 */
445void* PropertyServer::Entry(void)
446{
447 if (CreateSocket(SYSTEM_PROPERTY_PIPE_NAME)) {
448 assert(mListenSock >= 0);
449 SetDefaultProperties();
450
451 /* loop until it's time to exit or we fail */
452 ServeProperties();
453
454 ClearProperties();
455
456 /*
457 * Close listen socket and all clients.
458 */
459 LOG(LOG_INFO, "sim", "Cleaning up socket list\n");
460 typedef List<int>::iterator IntIter;
461 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ++ii)
462 close((*ii));
463 close(mListenSock);
464 }
465
466 LOG(LOG_INFO, "sim", "PropertyServer thread exiting\n");
467 return NULL;
468}
469