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