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