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
This commit is contained in:
Tom Cherry 2020-04-28 13:55:19 -07:00
parent 91fd50c78f
commit e3e77d382f
3 changed files with 24 additions and 27 deletions

View file

@ -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> 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<ServiceParser>(
&service_list, subcontext.get(), std::nullopt));
parser.AddSectionParser("on",
std::make_unique<ActionParser>(&action_manager, subcontext.get()));
&service_list, GetSubcontext(), std::nullopt));
parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, GetSubcontext()));
parser.AddSectionParser("import", std::make_unique<ImportParser>(&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<ServiceParser>(&service_list, subcontext.get(),
std::nullopt, from_apex));
parser.AddSectionParser(
"service", std::make_unique<ServiceParser>(&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();

View file

@ -52,6 +52,8 @@ namespace init {
namespace {
std::string shutdown_command;
static bool subcontext_terminated_by_shutdown;
static std::unique_ptr<Subcontext> subcontext;
class SubcontextProcess {
public:
@ -323,34 +325,30 @@ Result<std::vector<std::string>> Subcontext::ExpandArgs(const std::vector<std::s
return expanded_args;
}
static std::vector<Subcontext> subcontexts;
static bool shutting_down;
std::unique_ptr<Subcontext> InitializeSubcontext() {
void InitializeSubcontext() {
if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
return std::make_unique<Subcontext>(std::vector<std::string>{"/vendor", "/odm"},
kVendorContext);
subcontext.reset(
new Subcontext(std::vector<std::string>{"/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

View file

@ -60,7 +60,8 @@ class Subcontext {
};
int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map);
std::unique_ptr<Subcontext> InitializeSubcontext();
void InitializeSubcontext();
Subcontext* GetSubcontext();
bool SubcontextChildReap(pid_t pid);
void SubcontextTerminate();