前言
本次分析基于 android-11.0.0_r21,如与其他版本有所区别,敬请谅解
一、源码位置
二、 定义
一个 Android 系统服务,分为 Java 层和 Native 层两部分,Java 层负责与 WMS 通信,而 Native 层则是 InputReader 和 InputDispatcher 两个输入系统关键组件的运行容器;
三、服务启动流程
1. init进程启动
1.1 开机上电,加载固化的ROM
。
1.2 加载BootLoader
,拉起Android OS
。
1.3 加载Uboot
,初始外设,引导Kernel
启动等。
1.4 启动Kernel
,加载驱动,硬件。
1.5 启动Android
,挂载分区,加载驱动、服务,init
进程等。
2. zygote 启动
2.1 描述
init进程会解析配置文件init.rc,来启动一些需要在开机时就启动的系统进程,如Zygote进程、ServiceManager进程等。init.rc是由Android初始化语言编写的脚本配置。由于Android 5.0开始支持了64bit程序,在init.rc里改成了通过${ro.zygote}
的值来引入Zygote相关的配置,
2.2 具体流程
2.2.1 加载特定的 init.zygote*.rc
文件
在启动过程中,init
进程会根据硬件架构选择性加载
eg:/system/core/rootdir/init.rc
import /system/etc/init/hw/init.${ro.zygote}.rc
模拟查看ro.zygote属性的值
generic_x86_arm:/ $ getprop ro.zygote
zygote32
2.2.2 rc文件解析过程
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
class main
priority -20
user root
group root readproc reserved_disk
socket zygote stream 660 root system
socket usap_pool_primary stream 660 root system
onrestart exec_background - system system -- /system/bin/vdc volume abort_fuse
onrestart write /sys/power/state on
onrestart restart audioserver
onrestart restart cameraserver
onrestart restart media
onrestart restart netd
onrestart restart wificond
writepid /dev/cpuset/foreground/tasks
这是 Android 系统中的
init.rc
配置文件的一部分,用于定义和配置 Zygote 进程的启动和初始化。以下是对每个配置项的详细解释:这一行定义了一个名为 "zygote" 的系统服务。
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
:
/system/bin/app_process
是 Zygote 进程的可执行文件。-Xzygote
参数表示在运行时设置 Zygote 进程的属性。/system/bin
参数指定了 Zygote 进程启动时的工作目录。--zygote
参数表示这是 Zygote 进程。--start-system-server
参数表示在 Zygote 进程启动时启动系统服务。
class main
:
- 将
zygote
服务分类为主要服务。
priority -20
:
- 设置服务的优先级。负值表示高优先级。
user root
和group root readproc reserved_disk
:
- 指定服务的运行用户和用户组。这里指定了
root
用户,以及root
和readproc
用户组。
socket zygote stream 660 root system
和socket usap_pool_primary stream 660 root system
:
- 定义 Zygote 进程使用的两个套接字(socket)。这些套接字用于与其他进程进行通信。指定了套接字类型为
stream
,权限为660
,所有者为root
用户,组为system
。
onrestart
段:
onrestart
用于指定在服务重新启动时执行的一系列命令。对于 Zygote 进程,以下命令会在重新启动时执行:
exec_background - system system -- /system/bin/vdc volume abort_fuse
:在后台执行/system/bin/vdc volume abort_fuse
命令。write /sys/power/state on
:将字符串 "on" 写入/sys/power/state
文件。restart audioserver
、restart cameraserver
、restart media
、restart netd
、restart wificond
:重新启动一系列系统服务。
writepid /dev/cpuset/foreground/tasks
:
- 将 Zygote 进程的 PID 写入
/dev/cpuset/foreground/tasks
文件。这段配置的目的是在 Android 系统启动时启动 Zygote 进程,并配置它的运行环境,以便能够有效地孵化新的应用进程。通过定义套接字、设置权限、配置重启命令等,确保了 Zygote 进程的正确启动和初始化。
3. systemserver启动
4. InputManagerService启动
t.traceBegin("StartInputManagerService");
inputManager = new InputManagerService(context);
t.traceEnd();
...
t.traceBegin("StartInputManagerService");
inputManager = new InputManagerService(context);
t.traceEnd();
InputManagerService.java
public void start() {
Slog.i(TAG, "Starting input manager");
nativeStart(mPtr);
...
}
com_android_server_input_InputManagerService.cpp
static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
status_t result = im->getInputManager()->start();
if (result) {
jniThrowRuntimeException(env, "Input manager could not be started.");
}
}
启动InputReader线程和InputDispatcher线程
InputReader线程: 通过EventHub监听/dev/input/目录的输入事件和文件变化;
InputDispatcher线程: 负责查找目标窗口,派发touch事件;同时也支持注入事件的功能;
status_t InputManager::start() {
status_t result = mDispatcher->start();
if (result) {
ALOGE("Could not start InputDispatcher thread due to error %d.", result);
return result;
}
result = mReader->start();
if (result) {
ALOGE("Could not start InputReader due to error %d.", result);
mDispatcher->stop();
return result;
}
return OK;
}
status_t InputReader::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
return OK;
}
status_t InputDispatcher::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
return OK;
}
四、参考链接
Android Framework——zygote 启动 SystemServer - 知乎