init: handle sys.powerctl immediately
Currently if a process sets the sys.powerctl property, init adds this
property change into the event queue, just like any other property.
The actual logic to shutdown the device is not executed until init
gets to the action associated with the property change.
This is bad for multiple reasons, but explicitly causes deadlock in
the follow scenario:
A service is started with `exec` or `exec_start`
The same service sets sys.powerctl indicating to the system to
shutdown
The same service then waits infinitely
In this case, init doesn't process any further commands until the exec
service completes, including the command to reboot the device.
This change causes init to immediately handle sys.powerctl and reboot
the device regardless of the state of the event queue, wait for exec,
or wait for property conditions.
Bug: 37209359
Bug: 37415192
Test: Init reboots normally
Test: Update verifier can reboot the system
Change-Id: Iff2295aed970840f47e56c4bacc93001b791fa35
diff --git a/init/init.cpp b/init/init.cpp
index 181b18d..9a4aa24 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -67,6 +67,7 @@
#include "keychords.h"
#include "log.h"
#include "property_service.h"
+#include "reboot.h"
#include "service.h"
#include "signal_handler.h"
#include "ueventd.h"
@@ -153,8 +154,13 @@
return true;
}
-void property_changed(const char *name, const char *value)
-{
+void property_changed(const std::string& name, const std::string& value) {
+ // If the property is sys.powerctl, we bypass the event queue and immediately handle it.
+ // This is to ensure that init will always and immediately shutdown/reboot, regardless of
+ // if there are other pending events to process or if init is waiting on an exec service or
+ // waiting on a property.
+ if (name == "sys.powerctl") HandlePowerctlMessage(value);
+
if (property_triggers_enabled)
ActionManager::GetInstance().QueuePropertyTrigger(name, value);
if (waiting_for_prop) {