blob: ad2f5f6b4e283081c4fe6743b19ede8c1992e6c0 [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" },
Marco Nelissen51d41d52010-05-24 15:19:31 -070093 { "ro.build.version.sdk", "8" },
Marco Nelissenf6bf5802010-06-21 12:32:29 -070094 { "ro.build.version.codename", "Honeycomb" },
95 { "ro.build.version.release", "Honeycomb" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -080096 { "ro.product.device", "simulator" /*"sooner"*/ },
97 { "ro.product.brand", "generic" },
98 { "ro.build.user", "fadden" },
99 { "ro.build.host", "marathon" },
100 { "ro.config.nocheckin", "yes" },
101 { "ro.product.manufacturer", "" },
102 { "ro.radio.use-ppp", "no" },
103 { "ro.FOREGROUND_APP_ADJ", "0" },
104 { "ro.VISIBLE_APP_ADJ", "1" },
Marco Nelissenf6bf5802010-06-21 12:32:29 -0700105 { "ro.PERCEPTIBLE_APP_ADJ", "2" },
106 { "ro.HEAVY_WEIGHT_APP_ADJ", "3" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800107 { "ro.SECONDARY_SERVER_ADJ", "2" },
108 { "ro.HIDDEN_APP_MIN_ADJ", "7" },
109 { "ro.CONTENT_PROVIDER_ADJ", "14" },
110 { "ro.EMPTY_APP_ADJ", "15" },
111 { "ro.FOREGROUND_APP_MEM", "1536" },
112 { "ro.VISIBLE_APP_MEM", "2048" },
Marco Nelissenf6bf5802010-06-21 12:32:29 -0700113 { "ro.PERCEPTIBLE_APP_MEM", "4096" },
114 { "ro.HEAVY_WEIGHT_APP_MEM", "4096" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800115 { "ro.SECONDARY_SERVER_MEM", "4096" },
116 { "ro.HIDDEN_APP_MEM", "8192" },
117 { "ro.EMPTY_APP_MEM", "16384" },
The Android Open Source Project692ab022009-03-09 11:52:11 -0700118 { "ro.HOME_APP_ADJ", "4" },
119 { "ro.HOME_APP_MEM", "4096" },
Andy McFadden3c5497c2009-06-08 10:08:18 -0700120 { "ro.BACKUP_APP_ADJ", "2" },
121 { "ro.BACKUP_APP_MEM", "4096" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800122 //{ "init.svc.adbd", "running" }, // causes ADB-JDWP
123 { "init.svc.usbd", "running" },
124 { "init.svc.debuggerd", "running" },
125 { "init.svc.ril-daemon", "running" },
126 { "init.svc.zygote", "running" },
127 { "init.svc.runtime", "running" },
128 { "init.svc.dbus", "running" },
129 { "init.svc.pppd_gprs", "running" },
130 { "adb.connected", "0" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800131 /*
132 { "status.battery.state", "Slow" },
133 { "status.battery.level", "5" },
134 { "status.battery.level_raw", "50" },
135 { "status.battery.level_scale", "9" },
136 */
137
138 /* disable the annoying setup wizard */
139 { "app.setupwizard.disable", "1" },
140
141 /* Dalvik options, set by AndroidRuntime */
142 { "dalvik.vm.stack-trace-file", "/data/anr/traces.txt" },
143 //{ "dalvik.vm.execution-mode", "int:portable" },
144 { "dalvik.vm.enableassertions", "all" }, // -ea
145 { "dalvik.vm.dexopt-flags", "" }, // e.g. "v=a,o=v,m=n"
146 { "dalvik.vm.deadlock-predict", "off" }, // -Xdeadlockpredict
147 //{ "dalvik.vm.jniopts", "forcecopy" }, // -Xjniopts
148 { "log.redirect-stdio", "false" }, // -Xlog-stdio
149
150 /* SurfaceFlinger options */
Marco Nelissen51d41d52010-05-24 15:19:31 -0700151 { "ro.sf.lcd_density", "160" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800152 { "debug.sf.nobootanimation", "1" },
153 { "debug.sf.showupdates", "0" },
154 { "debug.sf.showcpu", "0" },
155 { "debug.sf.showbackground", "0" },
156 { "debug.sf.showfps", "0" },
Marco Nelissenbbabb3a2009-09-23 09:52:22 -0700157 { "default", "default" },
Marco Nelissen0eae51d2010-01-12 09:44:39 -0800158
Andreas Huber791be382010-02-03 13:55:53 -0800159 /* Stagefright options */
Marco Nelissen0eae51d2010-01-12 09:44:39 -0800160 { "media.stagefright.enable-player", "true" },
Andreas Huber791be382010-02-03 13:55:53 -0800161 { "media.stagefright.enable-meta", "true" },
162 { "media.stagefright.enable-scan", "true" },
163 { "media.stagefright.enable-http", "true" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800164 };
165
166 for (int i = 0; i < NELEM(propList); i++)
167 SetProperty(propList[i].key, propList[i].value);
168
169 Preferences* pPrefs = ((MyApp*)wxTheApp)->GetPrefs();
170 bool doCheckJni = false;
171
172 pPrefs->GetBool("check-jni", &doCheckJni);
173 if (doCheckJni)
174 SetProperty(kPropCheckJni, "1");
175 else
176 SetProperty(kPropCheckJni, "0");
177}
178
179/*
180 * Get the value of a property.
181 *
182 * "valueBuf" must hold at least PROPERTY_VALUE_MAX bytes.
183 *
184 * Returns "true" if the property was found.
185 */
186bool PropertyServer::GetProperty(const char* key, char* valueBuf)
187{
188 typedef List<Property>::iterator PropIter;
189
190 assert(key != NULL);
191 assert(valueBuf != NULL);
192
193 for (PropIter pi = mPropList.begin(); pi != mPropList.end(); ++pi) {
194 Property& prop = *pi;
195 if (strcmp(prop.key, key) == 0) {
196 if (strlen(prop.value) >= PROPERTY_VALUE_MAX) {
197 fprintf(stderr,
198 "GLITCH: properties table holds '%s' '%s' (len=%d)\n",
Andy McFadden3c5497c2009-06-08 10:08:18 -0700199 prop.key, prop.value, (int) strlen(prop.value));
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800200 abort();
201 }
202 assert(strlen(prop.value) < PROPERTY_VALUE_MAX);
203 strcpy(valueBuf, prop.value);
204 return true;
205 }
206 }
207
208 //printf("Prop: get [%s] not found\n", key);
209 return false;
210}
211
212/*
213 * Set the value of a property, replacing it if it already exists.
214 *
215 * If "value" is NULL, the property is removed.
216 *
217 * If the property is immutable, this returns "false" without doing
218 * anything. (Not implemented.)
219 */
220bool PropertyServer::SetProperty(const char* key, const char* value)
221{
222 typedef List<Property>::iterator PropIter;
223
224 assert(key != NULL);
225 assert(value != NULL);
226
227 for (PropIter pi = mPropList.begin(); pi != mPropList.end(); ++pi) {
228 Property& prop = *pi;
229 if (strcmp(prop.key, key) == 0) {
230 if (value != NULL) {
231 //printf("Prop: replacing [%s]: [%s] with [%s]\n",
232 // prop.key, prop.value, value);
233 strcpy(prop.value, value);
234 } else {
235 //printf("Prop: removing [%s]\n", prop.key);
236 mPropList.erase(pi);
237 }
238 return true;
239 }
240 }
241
242 //printf("Prop: adding [%s]: [%s]\n", key, value);
243 Property tmp;
244 strcpy(tmp.key, key);
245 strcpy(tmp.value, value);
246 mPropList.push_back(tmp);
247 return true;
248}
249
250/*
251 * Create a UNIX domain socket, carefully removing it if it already
252 * exists.
253 */
254bool PropertyServer::CreateSocket(const char* fileName)
255{
256 struct stat sb;
257 bool result = false;
258 int sock = -1;
259 int cc;
260
261 cc = stat(fileName, &sb);
262 if (cc < 0) {
263 if (errno != ENOENT) {
264 LOG(LOG_ERROR, "sim-prop",
265 "Unable to stat '%s' (errno=%d)\n", fileName, errno);
266 goto bail;
267 }
268 } else {
269 /* don't touch it if it's not a socket */
270 if (!(S_ISSOCK(sb.st_mode))) {
271 LOG(LOG_ERROR, "sim-prop",
272 "File '%s' exists and is not a socket\n", fileName);
273 goto bail;
274 }
275
276 /* remove the cruft */
277 if (unlink(fileName) < 0) {
278 LOG(LOG_ERROR, "sim-prop",
279 "Unable to remove '%s' (errno=%d)\n", fileName, errno);
280 goto bail;
281 }
282 }
283
284 struct sockaddr_un addr;
285
286 sock = ::socket(AF_UNIX, SOCK_STREAM, 0);
287 if (sock < 0) {
288 LOG(LOG_ERROR, "sim-prop",
289 "UNIX domain socket create failed (errno=%d)\n", errno);
290 goto bail;
291 }
292
293 /* bind the socket; this creates the file on disk */
294 strcpy(addr.sun_path, fileName); // max 108 bytes
295 addr.sun_family = AF_UNIX;
296 cc = ::bind(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
297 if (cc < 0) {
298 LOG(LOG_ERROR, "sim",
299 "AF_UNIX bind failed for '%s' (errno=%d)\n", fileName, errno);
300 goto bail;
301 }
302
303 cc = ::listen(sock, 5);
304 if (cc < 0) {
305 LOG(LOG_ERROR, "sim", "AF_UNIX listen failed (errno=%d)\n", errno);
306 goto bail;
307 }
308
309 mListenSock = sock;
310 sock = -1;
311 result = true;
312
313bail:
314 if (sock >= 0)
315 close(sock);
316 return result;
317}
318
319/*
320 * Handle a client request.
321 *
322 * Returns true on success, false if the fd should be closed.
323 */
324bool PropertyServer::HandleRequest(int fd)
325{
326 char reqBuf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX];
327 char valueBuf[1 + PROPERTY_VALUE_MAX];
328 ssize_t actual;
329
330 memset(valueBuf, 'x', sizeof(valueBuf)); // placate valgrind
331
332 /* read the command byte; this determines the message length */
333 actual = read(fd, reqBuf, 1);
334 if (actual <= 0)
335 return false;
336
337 if (reqBuf[0] == kSystemPropertyGet) {
338 actual = read(fd, reqBuf, PROPERTY_KEY_MAX);
339
340 if (actual != PROPERTY_KEY_MAX) {
341 fprintf(stderr, "Bad read on get: %d of %d\n",
Andy McFadden3c5497c2009-06-08 10:08:18 -0700342 (int) actual, PROPERTY_KEY_MAX);
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800343 return false;
344 }
345 if (GetProperty(reqBuf, valueBuf+1))
346 valueBuf[0] = 1;
347 else
348 valueBuf[0] = 0;
349 //printf("GET property [%s]: (found=%d) [%s]\n",
350 // reqBuf, valueBuf[0], valueBuf+1);
351 if (write(fd, valueBuf, sizeof(valueBuf)) != sizeof(valueBuf)) {
352 fprintf(stderr, "Bad write on get\n");
353 return false;
354 }
355 } else if (reqBuf[0] == kSystemPropertySet) {
356 actual = read(fd, reqBuf, PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX);
357 if (actual != PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX) {
358 fprintf(stderr, "Bad read on set: %d of %d\n",
Andy McFadden3c5497c2009-06-08 10:08:18 -0700359 (int) actual, PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX);
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800360 return false;
361 }
362 //printf("SET property '%s'\n", reqBuf);
363 if (SetProperty(reqBuf, reqBuf + PROPERTY_KEY_MAX))
364 valueBuf[0] = 1;
365 else
366 valueBuf[0] = 0;
367 if (write(fd, valueBuf, 1) != 1) {
368 fprintf(stderr, "Bad write on set\n");
369 return false;
370 }
371 } else if (reqBuf[0] == kSystemPropertyList) {
372 /* TODO */
373 assert(false);
374 } else {
375 fprintf(stderr, "Unexpected request %d from prop client\n", reqBuf[0]);
376 return false;
377 }
378
379 return true;
380}
381
382/*
383 * Serve up properties.
384 */
385void PropertyServer::ServeProperties(void)
386{
387 typedef List<int>::iterator IntIter;
388 fd_set readfds;
389 int maxfd;
390
391 while (true) {
392 int cc;
393
394 FD_ZERO(&readfds);
395 FD_SET(mListenSock, &readfds);
396 maxfd = mListenSock;
397
398 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ++ii) {
399 int fd = (*ii);
400
401 FD_SET(fd, &readfds);
402 if (maxfd < fd)
403 maxfd = fd;
404 }
405
406 cc = select(maxfd+1, &readfds, NULL, NULL, NULL);
407 if (cc < 0) {
408 if (errno == EINTR) {
409 printf("hiccup!\n");
410 continue;
411 }
412 return;
413 }
414 if (FD_ISSET(mListenSock, &readfds)) {
415 struct sockaddr_un from;
416 socklen_t fromlen;
417 int newSock;
418
419 fromlen = sizeof(from);
420 newSock = ::accept(mListenSock, (struct sockaddr*) &from, &fromlen);
421 if (newSock < 0) {
422 LOG(LOG_WARN, "sim",
423 "AF_UNIX accept failed (errno=%d)\n", errno);
424 } else {
425 //printf("new props connection on %d --> %d\n",
426 // mListenSock, newSock);
427
428 mClientList.push_back(newSock);
429 }
430 }
431
432 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ) {
433 int fd = (*ii);
434 bool ok = true;
435
436 if (FD_ISSET(fd, &readfds)) {
437 //printf("--- activity on %d\n", fd);
438
439 ok = HandleRequest(fd);
440 }
441
442 if (ok) {
443 ++ii;
444 } else {
445 //printf("--- closing %d\n", fd);
446 close(fd);
447 ii = mClientList.erase(ii);
448 }
449 }
450 }
451}
452
453/*
454 * Thread entry point.
455 *
456 * This just sits and waits for a new connection. It hands it off to the
457 * main thread and then goes back to waiting.
458 *
459 * There is currently no "polite" way to shut this down.
460 */
461void* PropertyServer::Entry(void)
462{
463 if (CreateSocket(SYSTEM_PROPERTY_PIPE_NAME)) {
464 assert(mListenSock >= 0);
465 SetDefaultProperties();
466
467 /* loop until it's time to exit or we fail */
468 ServeProperties();
469
470 ClearProperties();
471
472 /*
473 * Close listen socket and all clients.
474 */
475 LOG(LOG_INFO, "sim", "Cleaning up socket list\n");
476 typedef List<int>::iterator IntIter;
477 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ++ii)
478 close((*ii));
479 close(mListenSock);
480 }
481
482 LOG(LOG_INFO, "sim", "PropertyServer thread exiting\n");
483 return NULL;
484}
485