2025年System_Server与Zygote共存亡

System_Server与Zygote共存亡Zygote 孵化 System server 的最后阶段 ForkAndSpeci 里会调用 SetSigChldHa 设置信号处理函数 SetSigChldHa 定义了信号处理函数 SigChldHandl 当信号 SIGCHLD 到来的时候 会进入信号处理函数 如果子进程 SystemServer 挂了 Zygote 就会自杀

大家好,我是讯享网,很高兴认识大家。

Zygote孵化System_server的最后阶段ForkAndSpecializeCommon里会调用SetSigChldHandler设置信号处理函数。SetSigChldHandler定义了信号处理函数SigChldHandler,当信号SIGCHLD到来的时候,会进入信号处理函数。如果子进程SystemServer挂了,Zygote就会自杀。从而导致Zygote重启。

frameworks/base/core/jni/com_android_internal_os_Zygote.cpp static void SigChldHandler(int /*signal_number*/) { pid_t pid; int status; //Zygote监听所有子进程的存亡 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { //某一个子进程挂了 if (WIFSIGNALED(status)) { if (WTERMSIG(status) != SIGKILL) { ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status)); } } //如果挂掉的是SystemServer if (pid == gSystemServerPid) { ALOGE("Exit zygote because system server (%d) has terminated", pid); kill(getpid(), SIGKILL); //Zygote自杀 } } }

讯享网

而如果Zygote收到被kill的信号,会调用kill(-pid_, SIGKILL),pid < 0将该信号发送给其进程组id等于pid的绝对值的进程组,导致System_Server被杀。系统重启。


讯享网

讯享网system/core/init/service.cpp bool ServiceManager::ReapOneProcess() { ...... if (WIFEXITED(status)) { NOTICE("%s exited with status %d\n", name.c_str(), WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { NOTICE("%s killed by signal %d\n", name.c_str(), WTERMSIG(status)); } else if (WIFSTOPPED(status)) { NOTICE("%s stopped by signal %d\n", name.c_str(), WSTOPSIG(status)); } else { NOTICE("%s state changed", name.c_str()); } if (svc->Reap()) { waiting_for_exec = false; RemoveService(*svc); } ...... } bool Service::Reap() { if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) { NOTICE("Service '%s' (pid %d) killing any children in process group\n", name_.c_str(), pid_); kill(-pid_, SIGKILL); //pid < 0将该信号发送给其进程组id等于pid的绝对值的进程组,但是不包含某些系统进程! } ................. }

导致Zygote重启的一些事件,从init*rc可以看出,
Zygote:挂掉会触发media、netd以及子进程(包括System_Server进程)重启;
System_Server: 挂掉触发zygote重启;
surfaceflinger:重启触发zygote重启;
servicemanager: 重启触发zygote重启

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server class main socket zygote stream 660 root system onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart media onrestart restart netd service servicemanager /system/bin/servicemanager class core user system group system critical onrestart restart healthd onrestart restart zygote onrestart restart media onrestart restart surfaceflinger onrestart restart drm service surfaceflinger /system/bin/surfaceflinger class core user system group graphics drmrpc onrestart restart zygote

参考资料
http://gityuan.com/2016/02/05/android-init/
http://gityuan.com/2016/02/13/android-zygote/
http://gityuan.com/2016/02/14/android-system-server/
http://gityuan.com/2016/02/20/android-system-server-2/
http://www.jianshu.com/p/98cf2c9b5dee

小讯
上一篇 2025-03-03 21:17
下一篇 2025-01-11 17:28

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/58771.html