From 847b80a1124a084a309a7c3dee7aba023b899eff Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Fri, 25 Feb 2022 23:28:59 +0000 Subject: [PATCH] Introduce the Service::CheckConsole() method The Service::Start() method is so long that its length negatively affects readability of the code. Hence this patch that splits Service::Start(). Test: Booted Android in Cuttlefish. Change-Id: Ib8e1e87fbd335520cbe3aac2a88d250fcf3b4ff0 Signed-off-by: Bart Van Assche --- init/service.cpp | 36 ++++++++++++++++++++++-------------- init/service.h | 1 + 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/init/service.cpp b/init/service.cpp index f6dd9b9fa..c053761e9 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -405,6 +405,26 @@ static void ClosePipe(const std::array* pipe) { } } +Result Service::CheckConsole() { + if (!(flags_ & SVC_CONSOLE)) { + return {}; + } + + if (proc_attr_.console.empty()) { + proc_attr_.console = "/dev/" + GetProperty("ro.boot.console", "console"); + } + + // Make sure that open call succeeds to ensure a console driver is + // properly registered for the device node + int console_fd = open(proc_attr_.console.c_str(), O_RDWR | O_CLOEXEC); + if (console_fd < 0) { + flags_ |= SVC_DISABLED; + return ErrnoError() << "Couldn't open console '" << proc_attr_.console << "'"; + } + close(console_fd); + return {}; +} + Result Service::Start() { auto reboot_on_failure = make_scope_guard([this] { if (on_failure_reboot_target_) { @@ -442,20 +462,8 @@ Result Service::Start() { return ErrnoError() << "pipe()"; } - bool needs_console = (flags_ & SVC_CONSOLE); - if (needs_console) { - if (proc_attr_.console.empty()) { - proc_attr_.console = "/dev/" + GetProperty("ro.boot.console", "console"); - } - - // Make sure that open call succeeds to ensure a console driver is - // properly registered for the device node - int console_fd = open(proc_attr_.console.c_str(), O_RDWR | O_CLOEXEC); - if (console_fd < 0) { - flags_ |= SVC_DISABLED; - return ErrnoError() << "Couldn't open console '" << proc_attr_.console << "'"; - } - close(console_fd); + if (Result result = CheckConsole(); !result.ok()) { + return result; } struct stat sb; diff --git a/init/service.h b/init/service.h index 3289f5407..7339370ad 100644 --- a/init/service.h +++ b/init/service.h @@ -145,6 +145,7 @@ class Service { void KillProcessGroup(int signal, bool report_oneshot = false); void SetProcessAttributesAndCaps(); void ResetFlagsForStart(); + Result CheckConsole(); static unsigned long next_start_order_; static bool is_exec_service_running_;