From e3e77d382fd1e1dbb2acb777658064e78be94237 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Tue, 28 Apr 2020 13:55:19 -0700 Subject: [PATCH] init: fix restarting of subcontext When the subcontext code was redone to allow only one subcontext (vendor_init), the code for restarting it and for terminating it during shutdown was not updated, resulting in it not working. Bug: 155203339 Test: kill subcontext init and notice it restart Test: subcontext init stops during shutdown Change-Id: Ib77f59d1e7be0ffcfd3f31c8450dc022c20bb322 --- init/init.cpp | 16 +++++++--------- init/subcontext.cpp | 32 +++++++++++++++----------------- init/subcontext.h | 3 ++- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/init/init.cpp b/init/init.cpp index a9d63013f..b1774950e 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -76,6 +76,7 @@ #include "service.h" #include "service_parser.h" #include "sigchld_handler.h" +#include "subcontext.h" #include "system/core/init/property_service.pb.h" #include "util.h" @@ -100,8 +101,6 @@ static int property_triggers_enabled = 0; static int signal_fd = -1; static int property_fd = -1; -static std::unique_ptr subcontext; - struct PendingControlMessage { std::string message; std::string name; @@ -279,9 +278,8 @@ Parser CreateParser(ActionManager& action_manager, ServiceList& service_list) { Parser parser; parser.AddSectionParser("service", std::make_unique( - &service_list, subcontext.get(), std::nullopt)); - parser.AddSectionParser("on", - std::make_unique(&action_manager, subcontext.get())); + &service_list, GetSubcontext(), std::nullopt)); + parser.AddSectionParser("on", std::make_unique(&action_manager, GetSubcontext())); parser.AddSectionParser("import", std::make_unique(&parser)); return parser; @@ -291,9 +289,9 @@ Parser CreateParser(ActionManager& action_manager, ServiceList& service_list) { Parser CreateServiceOnlyParser(ServiceList& service_list, bool from_apex) { Parser parser; - parser.AddSectionParser("service", - std::make_unique(&service_list, subcontext.get(), - std::nullopt, from_apex)); + parser.AddSectionParser( + "service", std::make_unique(&service_list, GetSubcontext(), std::nullopt, + from_apex)); return parser; } @@ -809,7 +807,7 @@ int SecondStageMain(int argc, char** argv) { PLOG(FATAL) << "SetupMountNamespaces failed"; } - subcontext = InitializeSubcontext(); + InitializeSubcontext(); ActionManager& am = ActionManager::GetInstance(); ServiceList& sm = ServiceList::GetInstance(); diff --git a/init/subcontext.cpp b/init/subcontext.cpp index 5263c14c8..f3dd53826 100644 --- a/init/subcontext.cpp +++ b/init/subcontext.cpp @@ -52,6 +52,8 @@ namespace init { namespace { std::string shutdown_command; +static bool subcontext_terminated_by_shutdown; +static std::unique_ptr subcontext; class SubcontextProcess { public: @@ -323,34 +325,30 @@ Result> Subcontext::ExpandArgs(const std::vector subcontexts; -static bool shutting_down; - -std::unique_ptr InitializeSubcontext() { +void InitializeSubcontext() { if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) { - return std::make_unique(std::vector{"/vendor", "/odm"}, - kVendorContext); + subcontext.reset( + new Subcontext(std::vector{"/vendor", "/odm"}, kVendorContext)); } - return nullptr; +} + +Subcontext* GetSubcontext() { + return subcontext.get(); } bool SubcontextChildReap(pid_t pid) { - for (auto& subcontext : subcontexts) { - if (subcontext.pid() == pid) { - if (!shutting_down) { - subcontext.Restart(); - } - return true; + if (subcontext->pid() == pid) { + if (!subcontext_terminated_by_shutdown) { + subcontext->Restart(); } + return true; } return false; } void SubcontextTerminate() { - shutting_down = true; - for (auto& subcontext : subcontexts) { - kill(subcontext.pid(), SIGTERM); - } + subcontext_terminated_by_shutdown = true; + kill(subcontext->pid(), SIGTERM); } } // namespace init diff --git a/init/subcontext.h b/init/subcontext.h index 5e1d8a80b..788d3be6c 100644 --- a/init/subcontext.h +++ b/init/subcontext.h @@ -60,7 +60,8 @@ class Subcontext { }; int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map); -std::unique_ptr InitializeSubcontext(); +void InitializeSubcontext(); +Subcontext* GetSubcontext(); bool SubcontextChildReap(pid_t pid); void SubcontextTerminate();