blob: a358856d7715e689ca1defca66452156fc364116 [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
151 { "media.stagefright.enable-player", "true" },
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800152 };
153
154 for (int i = 0; i < NELEM(propList); i++)
155 SetProperty(propList[i].key, propList[i].value);
156
157 Preferences* pPrefs = ((MyApp*)wxTheApp)->GetPrefs();
158 bool doCheckJni = false;
159
160 pPrefs->GetBool("check-jni", &doCheckJni);
161 if (doCheckJni)
162 SetProperty(kPropCheckJni, "1");
163 else
164 SetProperty(kPropCheckJni, "0");
165}
166
167/*
168 * Get the value of a property.
169 *
170 * "valueBuf" must hold at least PROPERTY_VALUE_MAX bytes.
171 *
172 * Returns "true" if the property was found.
173 */
174bool PropertyServer::GetProperty(const char* key, char* valueBuf)
175{
176 typedef List<Property>::iterator PropIter;
177
178 assert(key != NULL);
179 assert(valueBuf != NULL);
180
181 for (PropIter pi = mPropList.begin(); pi != mPropList.end(); ++pi) {
182 Property& prop = *pi;
183 if (strcmp(prop.key, key) == 0) {
184 if (strlen(prop.value) >= PROPERTY_VALUE_MAX) {
185 fprintf(stderr,
186 "GLITCH: properties table holds '%s' '%s' (len=%d)\n",
Andy McFadden3c5497c2009-06-08 10:08:18 -0700187 prop.key, prop.value, (int) strlen(prop.value));
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800188 abort();
189 }
190 assert(strlen(prop.value) < PROPERTY_VALUE_MAX);
191 strcpy(valueBuf, prop.value);
192 return true;
193 }
194 }
195
196 //printf("Prop: get [%s] not found\n", key);
197 return false;
198}
199
200/*
201 * Set the value of a property, replacing it if it already exists.
202 *
203 * If "value" is NULL, the property is removed.
204 *
205 * If the property is immutable, this returns "false" without doing
206 * anything. (Not implemented.)
207 */
208bool PropertyServer::SetProperty(const char* key, const char* value)
209{
210 typedef List<Property>::iterator PropIter;
211
212 assert(key != NULL);
213 assert(value != NULL);
214
215 for (PropIter pi = mPropList.begin(); pi != mPropList.end(); ++pi) {
216 Property& prop = *pi;
217 if (strcmp(prop.key, key) == 0) {
218 if (value != NULL) {
219 //printf("Prop: replacing [%s]: [%s] with [%s]\n",
220 // prop.key, prop.value, value);
221 strcpy(prop.value, value);
222 } else {
223 //printf("Prop: removing [%s]\n", prop.key);
224 mPropList.erase(pi);
225 }
226 return true;
227 }
228 }
229
230 //printf("Prop: adding [%s]: [%s]\n", key, value);
231 Property tmp;
232 strcpy(tmp.key, key);
233 strcpy(tmp.value, value);
234 mPropList.push_back(tmp);
235 return true;
236}
237
238/*
239 * Create a UNIX domain socket, carefully removing it if it already
240 * exists.
241 */
242bool PropertyServer::CreateSocket(const char* fileName)
243{
244 struct stat sb;
245 bool result = false;
246 int sock = -1;
247 int cc;
248
249 cc = stat(fileName, &sb);
250 if (cc < 0) {
251 if (errno != ENOENT) {
252 LOG(LOG_ERROR, "sim-prop",
253 "Unable to stat '%s' (errno=%d)\n", fileName, errno);
254 goto bail;
255 }
256 } else {
257 /* don't touch it if it's not a socket */
258 if (!(S_ISSOCK(sb.st_mode))) {
259 LOG(LOG_ERROR, "sim-prop",
260 "File '%s' exists and is not a socket\n", fileName);
261 goto bail;
262 }
263
264 /* remove the cruft */
265 if (unlink(fileName) < 0) {
266 LOG(LOG_ERROR, "sim-prop",
267 "Unable to remove '%s' (errno=%d)\n", fileName, errno);
268 goto bail;
269 }
270 }
271
272 struct sockaddr_un addr;
273
274 sock = ::socket(AF_UNIX, SOCK_STREAM, 0);
275 if (sock < 0) {
276 LOG(LOG_ERROR, "sim-prop",
277 "UNIX domain socket create failed (errno=%d)\n", errno);
278 goto bail;
279 }
280
281 /* bind the socket; this creates the file on disk */
282 strcpy(addr.sun_path, fileName); // max 108 bytes
283 addr.sun_family = AF_UNIX;
284 cc = ::bind(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
285 if (cc < 0) {
286 LOG(LOG_ERROR, "sim",
287 "AF_UNIX bind failed for '%s' (errno=%d)\n", fileName, errno);
288 goto bail;
289 }
290
291 cc = ::listen(sock, 5);
292 if (cc < 0) {
293 LOG(LOG_ERROR, "sim", "AF_UNIX listen failed (errno=%d)\n", errno);
294 goto bail;
295 }
296
297 mListenSock = sock;
298 sock = -1;
299 result = true;
300
301bail:
302 if (sock >= 0)
303 close(sock);
304 return result;
305}
306
307/*
308 * Handle a client request.
309 *
310 * Returns true on success, false if the fd should be closed.
311 */
312bool PropertyServer::HandleRequest(int fd)
313{
314 char reqBuf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX];
315 char valueBuf[1 + PROPERTY_VALUE_MAX];
316 ssize_t actual;
317
318 memset(valueBuf, 'x', sizeof(valueBuf)); // placate valgrind
319
320 /* read the command byte; this determines the message length */
321 actual = read(fd, reqBuf, 1);
322 if (actual <= 0)
323 return false;
324
325 if (reqBuf[0] == kSystemPropertyGet) {
326 actual = read(fd, reqBuf, PROPERTY_KEY_MAX);
327
328 if (actual != PROPERTY_KEY_MAX) {
329 fprintf(stderr, "Bad read on get: %d of %d\n",
Andy McFadden3c5497c2009-06-08 10:08:18 -0700330 (int) actual, PROPERTY_KEY_MAX);
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800331 return false;
332 }
333 if (GetProperty(reqBuf, valueBuf+1))
334 valueBuf[0] = 1;
335 else
336 valueBuf[0] = 0;
337 //printf("GET property [%s]: (found=%d) [%s]\n",
338 // reqBuf, valueBuf[0], valueBuf+1);
339 if (write(fd, valueBuf, sizeof(valueBuf)) != sizeof(valueBuf)) {
340 fprintf(stderr, "Bad write on get\n");
341 return false;
342 }
343 } else if (reqBuf[0] == kSystemPropertySet) {
344 actual = read(fd, reqBuf, PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX);
345 if (actual != PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX) {
346 fprintf(stderr, "Bad read on set: %d of %d\n",
Andy McFadden3c5497c2009-06-08 10:08:18 -0700347 (int) actual, PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX);
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800348 return false;
349 }
350 //printf("SET property '%s'\n", reqBuf);
351 if (SetProperty(reqBuf, reqBuf + PROPERTY_KEY_MAX))
352 valueBuf[0] = 1;
353 else
354 valueBuf[0] = 0;
355 if (write(fd, valueBuf, 1) != 1) {
356 fprintf(stderr, "Bad write on set\n");
357 return false;
358 }
359 } else if (reqBuf[0] == kSystemPropertyList) {
360 /* TODO */
361 assert(false);
362 } else {
363 fprintf(stderr, "Unexpected request %d from prop client\n", reqBuf[0]);
364 return false;
365 }
366
367 return true;
368}
369
370/*
371 * Serve up properties.
372 */
373void PropertyServer::ServeProperties(void)
374{
375 typedef List<int>::iterator IntIter;
376 fd_set readfds;
377 int maxfd;
378
379 while (true) {
380 int cc;
381
382 FD_ZERO(&readfds);
383 FD_SET(mListenSock, &readfds);
384 maxfd = mListenSock;
385
386 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ++ii) {
387 int fd = (*ii);
388
389 FD_SET(fd, &readfds);
390 if (maxfd < fd)
391 maxfd = fd;
392 }
393
394 cc = select(maxfd+1, &readfds, NULL, NULL, NULL);
395 if (cc < 0) {
396 if (errno == EINTR) {
397 printf("hiccup!\n");
398 continue;
399 }
400 return;
401 }
402 if (FD_ISSET(mListenSock, &readfds)) {
403 struct sockaddr_un from;
404 socklen_t fromlen;
405 int newSock;
406
407 fromlen = sizeof(from);
408 newSock = ::accept(mListenSock, (struct sockaddr*) &from, &fromlen);
409 if (newSock < 0) {
410 LOG(LOG_WARN, "sim",
411 "AF_UNIX accept failed (errno=%d)\n", errno);
412 } else {
413 //printf("new props connection on %d --> %d\n",
414 // mListenSock, newSock);
415
416 mClientList.push_back(newSock);
417 }
418 }
419
420 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ) {
421 int fd = (*ii);
422 bool ok = true;
423
424 if (FD_ISSET(fd, &readfds)) {
425 //printf("--- activity on %d\n", fd);
426
427 ok = HandleRequest(fd);
428 }
429
430 if (ok) {
431 ++ii;
432 } else {
433 //printf("--- closing %d\n", fd);
434 close(fd);
435 ii = mClientList.erase(ii);
436 }
437 }
438 }
439}
440
441/*
442 * Thread entry point.
443 *
444 * This just sits and waits for a new connection. It hands it off to the
445 * main thread and then goes back to waiting.
446 *
447 * There is currently no "polite" way to shut this down.
448 */
449void* PropertyServer::Entry(void)
450{
451 if (CreateSocket(SYSTEM_PROPERTY_PIPE_NAME)) {
452 assert(mListenSock >= 0);
453 SetDefaultProperties();
454
455 /* loop until it's time to exit or we fail */
456 ServeProperties();
457
458 ClearProperties();
459
460 /*
461 * Close listen socket and all clients.
462 */
463 LOG(LOG_INFO, "sim", "Cleaning up socket list\n");
464 typedef List<int>::iterator IntIter;
465 for (IntIter ii = mClientList.begin(); ii != mClientList.end(); ++ii)
466 close((*ii));
467 close(mListenSock);
468 }
469
470 LOG(LOG_INFO, "sim", "PropertyServer thread exiting\n");
471 return NULL;
472}
473