1.ui 上有 关机提示,那是走framework 关机
正常关机flow :详细可参考:https://blog.csdn.net/pillarbuaa/article/details/79207242?utm_source=blogxgwz7
(1):UI行为
长按powerkey的行为被PhoneWIndowManager处理,然后调到GlobalActionDialog会创建显示关机对话框。
点击powerOFF以后,WindowManagerService这边会调到Shutdownthread的shutdown函数
(2)Framework关机
ShutdownThread.java从shutdown函数开始依次调用beginShutdownSequence,申请锁,防止系统休眠和黑屏;然后调用run函数开始发送关机广播,关闭AMS radio PMS等;然后调到PowerManagerService里面的lowlevelShutdown函数,向sys.powerctl这个property写值
“shutdown“
3167 public static void lowLevelShutdown(String reason) {
3168 if (reason == null) {
3169 reason = “”;
3170 }
3171 SystemProperties.set(“sys.powerctl”, “shutdown,” + reason);
3172 }
(3)native关机
property_service.cpp(/system/core/init/)因为在start_property_service函数里面创建了socket:/dev/socket/property_service,所以会通过socket去监听prop的改变,监听到Property的变化以后会去调用handle_property_set_fd,然后调到HandlePropertySet,
PropertySet(),继续调到/system/core/init/init.cpp的property_changed,在该函数中会置变量Shuting_down为true。Init.cpp的main函数里面有一个while(1)循环,当
do_shutdown=true的时候会调到HandlePowerctlMessage,然后调到DoReboot,这里会做发signal关闭service等
void start_property_service() {
857 property_set_fd = CreateSocket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
858 false, 0666, 0, 0, nullptr);
863 listen(property_set_fd, 8);
864
865 register_epoll_handler(property_set_fd,handle_property_set_fd);
435 uint32_t HandlePropertySet(const std::string& name, const std::string& value,
436 const std::string& source_context, const ucred& cr, std::string* error) {
468 // sys.powerctl is a special property that is used to make the device reboot. We want to log
469 // any process that sets this property to be able to accurately blame the cause of a shutdown.
470 if (name == “sys.powerctl”) {
471 std::string cmdline_path = StringPrintf(“proc/%d/cmdline”, cr.pid);
472 std::string process_cmdline;
473 std::string process_log_string;
474 if (ReadFileToString(cmdline_path, &process_cmdline)) {
475 // Since cmdline is null deliminated, .c_str() conveniently gives us just the process
476 // path.
477 process_log_string = StringPrintf(" (%s)", process_cmdline.c_str());
478 }
479 LOG(INFO) << “Received sys.powerctl=’” << value << "’ from pid: " << cr.pid
480 << process_log_string;
481 }
500 bool HandlePowerctlMessage(const std::string& command) {
501 unsigned int cmd = 0;
502 std::vectorstd::string cmd_params = Split(command, “,”);
503 std::string reboot_target = “”;
504 bool run_fsck = false;
505 bool command_invalid = false;
506
507 if (cmd_params.size() > 3) {
508 command_invalid = true;
509 } else if (cmd_params[0] == “shutdown”) {
510 cmd = ANDROID_RB_POWEROFF;
511 if (cmd_params.size() == 2) {
512 if (cmd_params[1] == “userrequested”) {
513 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
514 // Run fsck once the file system is remounted in read-only mode.
515 run_fsck = true;
550 LOG(INFO) << “Clear action queue and start shutdown trigger”;
551 ActionManager::GetInstance().ClearQueue();
552 // Queue shutdown trigger first
553 ActionManager::GetInstance().QueueEventTrigger(“shutdown”);
554 // Queue built-in shutdown_done
555 auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
556 DoReboot(cmd, command, reboot_target, run_fsck);
557 return Success();
186 void attribute((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
187 LOG(INFO) << “Reboot ending, jumping to kernel”;
195 switch (cmd) {
196 case ANDROID_RB_POWEROFF:
197 reboot(RB_POWER_OFF);