<!-- @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", strerror(errno));
exit(1);
}
監聽這個SOCKET。
if (listen(lsocket, 5)) {
LOGE("Listen on socket failed: %s", 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", strerror(errno));
continue;
}
fcntl(s, F_SETFD, FD_CLOEXEC);
從新串連裡接收到命令並處理。
LOGI("new connection");
for (;;) {
unsigned short count;
if (readx(s, &count, sizeof(count))) {
LOGE("failed to read size");
break;
}
if ((count < 1) || (count >= BUFFER_MAX)) {
LOGE("invalid size %d", count);
break;
}
if (readx(s, buf, count)) {
LOGE("failed to read command");
break;
}
buf[count] = 0;
if (execute(s, buf)) break;
}
LOGI("closing connection");
close(s);
}
return 0;
}