init: degeneralize subcontext init into only vendor_init
This code is more generic than it needs to be and one of the side effects is that an extra init process is forked for odm_init, despite it having the same context as vendor_init. I don't think anything is going to change regarding that soon, so this change stops forking that extra process to save its memory and simplifies the code overall. Bug: 141164879 Test: init still uses vendor_init for vendor_scripts Test: init unit tests Test: init only has one subcontext process Change-Id: I0d224455604a681711e32f89fb20132378f69060
This commit is contained in:
parent
b0321c1de1
commit
14c2472734
10 changed files with 51 additions and 60 deletions
|
@ -121,13 +121,8 @@ Result<void> ActionParser::ParseSection(std::vector<std::string>&& args,
|
|||
}
|
||||
|
||||
Subcontext* action_subcontext = nullptr;
|
||||
if (subcontexts_) {
|
||||
for (auto& subcontext : *subcontexts_) {
|
||||
if (StartsWith(filename, subcontext.path_prefix())) {
|
||||
action_subcontext = &subcontext;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) {
|
||||
action_subcontext = subcontext_;
|
||||
}
|
||||
|
||||
std::string event_trigger;
|
||||
|
|
|
@ -30,8 +30,8 @@ namespace init {
|
|||
|
||||
class ActionParser : public SectionParser {
|
||||
public:
|
||||
ActionParser(ActionManager* action_manager, std::vector<Subcontext>* subcontexts)
|
||||
: action_manager_(action_manager), subcontexts_(subcontexts), action_(nullptr) {}
|
||||
ActionParser(ActionManager* action_manager, Subcontext* subcontext)
|
||||
: action_manager_(action_manager), subcontext_(subcontext), action_(nullptr) {}
|
||||
Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
|
||||
int line) override;
|
||||
Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
|
||||
|
@ -39,7 +39,7 @@ class ActionParser : public SectionParser {
|
|||
|
||||
private:
|
||||
ActionManager* action_manager_;
|
||||
std::vector<Subcontext>* subcontexts_;
|
||||
Subcontext* subcontext_;
|
||||
std::unique_ptr<Action> action_;
|
||||
};
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ static std::string shutdown_command;
|
|||
static bool do_shutdown = false;
|
||||
static bool load_debug_prop = false;
|
||||
|
||||
static std::vector<Subcontext>* subcontexts;
|
||||
static std::unique_ptr<Subcontext> subcontext;
|
||||
|
||||
void DumpState() {
|
||||
ServiceList::GetInstance().DumpState();
|
||||
|
@ -113,9 +113,10 @@ void DumpState() {
|
|||
Parser CreateParser(ActionManager& action_manager, ServiceList& service_list) {
|
||||
Parser parser;
|
||||
|
||||
parser.AddSectionParser(
|
||||
"service", std::make_unique<ServiceParser>(&service_list, subcontexts, std::nullopt));
|
||||
parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, subcontexts));
|
||||
parser.AddSectionParser("service", std::make_unique<ServiceParser>(
|
||||
&service_list, subcontext.get(), std::nullopt));
|
||||
parser.AddSectionParser("on",
|
||||
std::make_unique<ActionParser>(&action_manager, subcontext.get()));
|
||||
parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
|
||||
|
||||
return parser;
|
||||
|
@ -125,8 +126,8 @@ Parser CreateParser(ActionManager& action_manager, ServiceList& service_list) {
|
|||
Parser CreateServiceOnlyParser(ServiceList& service_list) {
|
||||
Parser parser;
|
||||
|
||||
parser.AddSectionParser(
|
||||
"service", std::make_unique<ServiceParser>(&service_list, subcontexts, std::nullopt));
|
||||
parser.AddSectionParser("service", std::make_unique<ServiceParser>(
|
||||
&service_list, subcontext.get(), std::nullopt));
|
||||
return parser;
|
||||
}
|
||||
|
||||
|
@ -749,7 +750,7 @@ int SecondStageMain(int argc, char** argv) {
|
|||
PLOG(FATAL) << "SetupMountNamespaces failed";
|
||||
}
|
||||
|
||||
subcontexts = InitializeSubcontexts();
|
||||
subcontext = InitializeSubcontext();
|
||||
|
||||
ActionManager& am = ActionManager::GetInstance();
|
||||
ServiceList& sm = ServiceList::GetInstance();
|
||||
|
|
|
@ -530,7 +530,7 @@ uint32_t InitPropertySet(const std::string& name, const std::string& value) {
|
|||
uint32_t result = 0;
|
||||
ucred cr = {.pid = 1, .uid = 0, .gid = 0};
|
||||
std::string error;
|
||||
result = HandlePropertySet(name, value, kInitContext.c_str(), cr, nullptr, &error);
|
||||
result = HandlePropertySet(name, value, kInitContext, cr, nullptr, &error);
|
||||
if (result != PROP_SUCCESS) {
|
||||
LOG(ERROR) << "Init cannot set '" << name << "' to '" << value << "': " << error;
|
||||
}
|
||||
|
@ -645,11 +645,16 @@ static void LoadProperties(char* data, const char* filter, const char* filename,
|
|||
char *key, *value, *eol, *sol, *tmp, *fn;
|
||||
size_t flen = 0;
|
||||
|
||||
const char* context = kInitContext.c_str();
|
||||
static constexpr const char* const kVendorPathPrefixes[2] = {
|
||||
"/vendor",
|
||||
"/odm",
|
||||
};
|
||||
|
||||
const char* context = kInitContext;
|
||||
if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
|
||||
for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
|
||||
if (StartsWith(filename, path_prefix)) {
|
||||
context = secontext;
|
||||
for (const auto& vendor_path_prefix : kVendorPathPrefixes) {
|
||||
if (StartsWith(filename, vendor_path_prefix)) {
|
||||
context = kVendorContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -537,13 +537,8 @@ Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
|
|||
filename_ = filename;
|
||||
|
||||
Subcontext* restart_action_subcontext = nullptr;
|
||||
if (subcontexts_) {
|
||||
for (auto& subcontext : *subcontexts_) {
|
||||
if (StartsWith(filename, subcontext.path_prefix())) {
|
||||
restart_action_subcontext = &subcontext;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) {
|
||||
restart_action_subcontext = subcontext_;
|
||||
}
|
||||
|
||||
std::vector<std::string> str_args(args.begin() + 2, args.end());
|
||||
|
|
|
@ -30,10 +30,10 @@ namespace init {
|
|||
class ServiceParser : public SectionParser {
|
||||
public:
|
||||
ServiceParser(
|
||||
ServiceList* service_list, std::vector<Subcontext>* subcontexts,
|
||||
ServiceList* service_list, Subcontext* subcontext,
|
||||
const std::optional<InterfaceInheritanceHierarchyMap>& interface_inheritance_hierarchy)
|
||||
: service_list_(service_list),
|
||||
subcontexts_(subcontexts),
|
||||
subcontext_(subcontext),
|
||||
interface_inheritance_hierarchy_(interface_inheritance_hierarchy),
|
||||
service_(nullptr) {}
|
||||
Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
|
||||
|
@ -84,7 +84,7 @@ class ServiceParser : public SectionParser {
|
|||
bool IsValidName(const std::string& name) const;
|
||||
|
||||
ServiceList* service_list_;
|
||||
std::vector<Subcontext>* subcontexts_;
|
||||
Subcontext* subcontext_;
|
||||
std::optional<InterfaceInheritanceHierarchyMap> interface_inheritance_hierarchy_;
|
||||
std::unique_ptr<Service> service_;
|
||||
std::string filename_;
|
||||
|
|
|
@ -49,15 +49,6 @@ using android::base::unique_fd;
|
|||
|
||||
namespace android {
|
||||
namespace init {
|
||||
|
||||
const std::string kInitContext = "u:r:init:s0";
|
||||
const std::string kVendorContext = "u:r:vendor_init:s0";
|
||||
|
||||
const char* const paths_and_secontexts[2][2] = {
|
||||
{"/vendor", kVendorContext.c_str()},
|
||||
{"/odm", kVendorContext.c_str()},
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
class SubcontextProcess {
|
||||
|
@ -237,6 +228,15 @@ void Subcontext::Restart() {
|
|||
Fork();
|
||||
}
|
||||
|
||||
bool Subcontext::PathMatchesSubcontext(const std::string& path) {
|
||||
for (const auto& prefix : path_prefixes_) {
|
||||
if (StartsWith(path, prefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Result<SubcontextReply> Subcontext::TransmitMessage(const SubcontextCommand& subcontext_command) {
|
||||
if (auto result = SendMessage(socket_, subcontext_command); !result) {
|
||||
Restart();
|
||||
|
@ -313,13 +313,12 @@ Result<std::vector<std::string>> Subcontext::ExpandArgs(const std::vector<std::s
|
|||
static std::vector<Subcontext> subcontexts;
|
||||
static bool shutting_down;
|
||||
|
||||
std::vector<Subcontext>* InitializeSubcontexts() {
|
||||
std::unique_ptr<Subcontext> InitializeSubcontext() {
|
||||
if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
|
||||
for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
|
||||
subcontexts.emplace_back(path_prefix, secontext);
|
||||
}
|
||||
return std::make_unique<Subcontext>(std::vector<std::string>{"/vendor", "/odm"},
|
||||
kVendorContext);
|
||||
}
|
||||
return &subcontexts;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool SubcontextChildReap(pid_t pid) {
|
||||
|
|
|
@ -14,8 +14,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _INIT_SUBCONTEXT_H
|
||||
#define _INIT_SUBCONTEXT_H
|
||||
#pragma once
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
|
@ -31,22 +30,21 @@
|
|||
namespace android {
|
||||
namespace init {
|
||||
|
||||
extern const std::string kInitContext;
|
||||
extern const std::string kVendorContext;
|
||||
extern const char* const paths_and_secontexts[2][2];
|
||||
static constexpr const char kInitContext[] = "u:r:init:s0";
|
||||
static constexpr const char kVendorContext[] = "u:r:vendor_init:s0";
|
||||
|
||||
class Subcontext {
|
||||
public:
|
||||
Subcontext(std::string path_prefix, std::string context)
|
||||
: path_prefix_(std::move(path_prefix)), context_(std::move(context)), pid_(0) {
|
||||
Subcontext(std::vector<std::string> path_prefixes, std::string context)
|
||||
: path_prefixes_(std::move(path_prefixes)), context_(std::move(context)), pid_(0) {
|
||||
Fork();
|
||||
}
|
||||
|
||||
Result<void> Execute(const std::vector<std::string>& args);
|
||||
Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args);
|
||||
void Restart();
|
||||
bool PathMatchesSubcontext(const std::string& path);
|
||||
|
||||
const std::string& path_prefix() const { return path_prefix_; }
|
||||
const std::string& context() const { return context_; }
|
||||
pid_t pid() const { return pid_; }
|
||||
|
||||
|
@ -54,18 +52,16 @@ class Subcontext {
|
|||
void Fork();
|
||||
Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command);
|
||||
|
||||
std::string path_prefix_;
|
||||
std::vector<std::string> path_prefixes_;
|
||||
std::string context_;
|
||||
pid_t pid_;
|
||||
android::base::unique_fd socket_;
|
||||
};
|
||||
|
||||
int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map);
|
||||
std::vector<Subcontext>* InitializeSubcontexts();
|
||||
std::unique_ptr<Subcontext> InitializeSubcontext();
|
||||
bool SubcontextChildReap(pid_t pid);
|
||||
void SubcontextTerminate();
|
||||
|
||||
} // namespace init
|
||||
} // namespace android
|
||||
|
||||
#endif
|
||||
|
|
|
@ -33,7 +33,7 @@ static void BenchmarkSuccess(benchmark::State& state) {
|
|||
return;
|
||||
}
|
||||
|
||||
auto subcontext = Subcontext("path", context);
|
||||
auto subcontext = Subcontext({"path"}, context);
|
||||
free(context);
|
||||
|
||||
while (state.KeepRunning()) {
|
||||
|
|
|
@ -52,7 +52,7 @@ void RunTest(F&& test_function) {
|
|||
auto context_string = std::string(context);
|
||||
free(context);
|
||||
|
||||
auto subcontext = Subcontext("dummy_path", context_string);
|
||||
auto subcontext = Subcontext({"dummy_path"}, context_string);
|
||||
ASSERT_NE(0, subcontext.pid());
|
||||
|
||||
test_function(subcontext, context_string);
|
||||
|
|
Loading…
Reference in a new issue