Skip to content

Commit 0dbfea7

Browse files
author
Tom Cherry
committed
init: trigger shutdown directly from builtins
Especially now that property_service is a thread, there may be some delay between when init sets sys.powerctl and when the main thread of init receives this and triggers shutdown. It's possible that outstanding init commands are run during this gap and that is not desirable. Instead, have builtins call TriggerShutdown() directly, so we can be sure that the next action that init runs will be to shutdown the device. Test: reboot works Test: reboot into recovery due to bad /data works Change-Id: I26fb9f4f57f46c7451b8b58187138cfedd6fd9eb
1 parent 6b0e789 commit 0dbfea7

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

init/builtins.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,14 @@ static Result<void> reboot_into_recovery(const std::vector<std::string>& options
138138
if (!write_bootloader_message(options, &err)) {
139139
return Error() << "Failed to set bootloader message: " << err;
140140
}
141-
property_set("sys.powerctl", "reboot,recovery");
141+
// This function should only be reached from init and not from vendor_init, and we want to
142+
// immediately trigger reboot instead of relaying through property_service. Older devices may
143+
// still have paths that reach here from vendor_init, so we keep the property_set as a fallback.
144+
if (getpid() == 1) {
145+
TriggerShutdown("reboot,recovery");
146+
} else {
147+
property_set("sys.powerctl", "reboot,recovery");
148+
}
142149
return {};
143150
}
144151

init/host_init_stubs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace android {
3535
namespace init {
3636

3737
// init.h
38-
inline void EnterShutdown(const std::string&) {
38+
inline void TriggerShutdown(const std::string&) {
3939
abort();
4040
}
4141

init/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void ResetWaitForProp() {
179179
waiting_for_prop.reset();
180180
}
181181

182-
void EnterShutdown(const std::string& command) {
182+
void TriggerShutdown(const std::string& command) {
183183
// We can't call HandlePowerctlMessage() directly in this function,
184184
// because it modifies the contents of the action queue, which can cause the action queue
185185
// to get into a bad state if this function is called from a command being executed by the
@@ -197,7 +197,7 @@ void property_changed(const std::string& name, const std::string& value) {
197197
// In non-thermal-shutdown case, 'shutdown' trigger will be fired to let device specific
198198
// commands to be executed.
199199
if (name == "sys.powerctl") {
200-
EnterShutdown(value);
200+
TriggerShutdown(value);
201201
}
202202

203203
if (property_triggers_enabled) ActionManager::GetInstance().QueuePropertyChange(name, value);

init/init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace init {
3131
Parser CreateParser(ActionManager& action_manager, ServiceList& service_list);
3232
Parser CreateServiceOnlyParser(ServiceList& service_list);
3333

34-
void EnterShutdown(const std::string& command);
34+
void TriggerShutdown(const std::string& command);
3535

3636
bool start_waiting_for_property(const char *name, const char *value);
3737

init/reboot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ static Result<void> DoUserspaceReboot() {
710710
auto guard = android::base::make_scope_guard([] {
711711
// Leave shutdown so that we can handle a full reboot.
712712
LeaveShutdown();
713-
property_set("sys.powerctl", "reboot,abort-userspace-reboot");
713+
TriggerShutdown("reboot,abort-userspace-reboot");
714714
});
715715
// Triggering userspace-reboot-requested will result in a bunch of set_prop
716716
// actions. We should make sure, that all of them are propagated before

init/service.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ void Service::Reap(const siginfo_t& siginfo) {
255255

256256
if ((siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) && on_failure_reboot_target_) {
257257
LOG(ERROR) << "Service with 'reboot_on_failure' option failed, shutting down system.";
258-
EnterShutdown(*on_failure_reboot_target_);
258+
TriggerShutdown(*on_failure_reboot_target_);
259259
}
260260

261261
if (flags_ & SVC_EXEC) UnSetExec();
@@ -335,7 +335,7 @@ void Service::DumpState() const {
335335
Result<void> Service::ExecStart() {
336336
auto reboot_on_failure = make_scope_guard([this] {
337337
if (on_failure_reboot_target_) {
338-
EnterShutdown(*on_failure_reboot_target_);
338+
TriggerShutdown(*on_failure_reboot_target_);
339339
}
340340
});
341341

@@ -366,7 +366,7 @@ Result<void> Service::ExecStart() {
366366
Result<void> Service::Start() {
367367
auto reboot_on_failure = make_scope_guard([this] {
368368
if (on_failure_reboot_target_) {
369-
EnterShutdown(*on_failure_reboot_target_);
369+
TriggerShutdown(*on_failure_reboot_target_);
370370
}
371371
});
372372

0 commit comments

Comments
 (0)