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
diff --git a/init/action_parser.cpp b/init/action_parser.cpp
index ff20e43..9736824 100644
--- a/init/action_parser.cpp
+++ b/init/action_parser.cpp
@@ -121,13 +121,8 @@
}
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;
diff --git a/init/action_parser.h b/init/action_parser.h
index 2fe9983..3000132 100644
--- a/init/action_parser.h
+++ b/init/action_parser.h
@@ -30,8 +30,8 @@
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 @@
private:
ActionManager* action_manager_;
- std::vector<Subcontext>* subcontexts_;
+ Subcontext* subcontext_;
std::unique_ptr<Action> action_;
};
diff --git a/init/init.cpp b/init/init.cpp
index 8326466..ad31fa0 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -103,7 +103,7 @@
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 @@
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 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 @@
PLOG(FATAL) << "SetupMountNamespaces failed";
}
- subcontexts = InitializeSubcontexts();
+ subcontext = InitializeSubcontext();
ActionManager& am = ActionManager::GetInstance();
ServiceList& sm = ServiceList::GetInstance();
diff --git a/init/property_service.cpp b/init/property_service.cpp
index c18decc..f5d1143 100644
--- a/init/property_service.cpp
+++ b/init/property_service.cpp
@@ -530,7 +530,7 @@
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 @@
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;
}
}
}
diff --git a/init/service_parser.cpp b/init/service_parser.cpp
index 4322dc7..45d1852 100644
--- a/init/service_parser.cpp
+++ b/init/service_parser.cpp
@@ -537,13 +537,8 @@
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());
diff --git a/init/service_parser.h b/init/service_parser.h
index ace4d70..f063102 100644
--- a/init/service_parser.h
+++ b/init/service_parser.h
@@ -30,10 +30,10 @@
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 @@
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_;
diff --git a/init/subcontext.cpp b/init/subcontext.cpp
index ec93b58..79fc372 100644
--- a/init/subcontext.cpp
+++ b/init/subcontext.cpp
@@ -49,15 +49,6 @@
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 @@
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 @@
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) {
diff --git a/init/subcontext.h b/init/subcontext.h
index 591521f..bcaad29 100644
--- a/init/subcontext.h
+++ b/init/subcontext.h
@@ -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 @@
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
diff --git a/init/subcontext_benchmark.cpp b/init/subcontext_benchmark.cpp
index f6fee8a..ccef2f3 100644
--- a/init/subcontext_benchmark.cpp
+++ b/init/subcontext_benchmark.cpp
@@ -33,7 +33,7 @@
return;
}
- auto subcontext = Subcontext("path", context);
+ auto subcontext = Subcontext({"path"}, context);
free(context);
while (state.KeepRunning()) {
diff --git a/init/subcontext_test.cpp b/init/subcontext_test.cpp
index 7565eb6..9cac35e 100644
--- a/init/subcontext_test.cpp
+++ b/init/subcontext_test.cpp
@@ -52,7 +52,7 @@
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);