Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 14c24727 authored by Tom Cherry's avatar Tom Cherry
Browse files

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
parent b0321c1d
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -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;
+3 −3
Original line number Diff line number Diff line
@@ -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_;
};

+8 −7
Original line number Diff line number Diff line
@@ -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();
+10 −5
Original line number Diff line number Diff line
@@ -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;
            }
        }
    }
+2 −7
Original line number Diff line number Diff line
@@ -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());
Loading