Android培训班(39)
<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->
在init.rc文件里,可以看到加载下面的服务:
service installd /system/bin/installd
socket installd stream 600 system system
installd 服务的代码在目录:
Android-2.0/frameworks/base/cmds/installd
installd服务是提供安装dex文件的服务。
它的主要入口函数代码如下:
int main(const int argc, const char *argv[]) {
char buf[BUFFER_MAX];
struct sockaddr addr;
socklen_t alen;
int lsocket, s, count;
创建一个控制的SOCKET。
lsocket = android_get_control_socket(SOCKET_PATH);
if (lsocket < 0) {
LOGE("Failed to get socket from environment: %s/n", strerror(errno));
exit(1);
}
监听这个SOCKET。
if (listen(lsocket, 5)) {
LOGE("Listen on socket failed: %s/n", strerror(errno));
exit(1);
}
fcntl(lsocket, F_SETFD, FD_CLOEXEC);
循环里处理接收到的SOCKET连接。
for (;;) {
alen = sizeof(addr);
s = accept(lsocket, &addr, &alen);
if (s < 0) {
LOGE("Accept failed: %s/n", strerror(errno));
continue;
}
fcntl(s, F_SETFD, FD_CLOEXEC);
从新连接里接收到命令并处理。
LOGI("new connection/n");
for (;;) {
unsigned short count;
if (readx(s, &count, sizeof(count))) {
LOGE("failed to read size/n");
break;
}
if ((count < 1) || (count >= BUFFER_MAX)) {
LOGE("invalid size %d/n", count);
break;
}
if (readx(s, buf, count)) {
LOGE("failed to read command/n");
break;
}
buf[count] = 0;
if (execute(s, buf)) break;
}
LOGI("closing connection/n");
close(s);
}
return 0;
}