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