Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/init/init_test.cpp b/init/init_test.cpp
- index 0a4071b98..1d0858f76 100644
- --- a/init/init_test.cpp
- +++ b/init/init_test.cpp
- @@ -24,6 +24,7 @@
- #include "builtins.h"
- #include "import_parser.h"
- #include "init_parser.h"
- +#include "service.h"
- #include "keyword_map.h"
- #include "util.h"
- @@ -55,12 +56,13 @@ class TestFunctionMap : public KeywordMap<BuiltinFunction> {
- using ActionManagerCommand = std::function<void(ActionManager&)>;
- void TestInit(const std::string& init_script_file, const TestFunctionMap& test_function_map,
- - const std::vector<ActionManagerCommand>& commands) {
- + const std::vector<ActionManagerCommand>& commands, ServiceManager* service_manager) {
- ActionManager am;
- Action::set_function_map(&test_function_map);
- Parser parser;
- + parser.AddSectionParser("service", std::make_unique<ServiceParser>(service_manager));
- parser.AddSectionParser("on", std::make_unique<ActionParser>(&am));
- parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
- @@ -76,11 +78,11 @@ void TestInit(const std::string& init_script_file, const TestFunctionMap& test_f
- }
- void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map,
- - const std::vector<ActionManagerCommand>& commands) {
- + const std::vector<ActionManagerCommand>& commands, ServiceManager* service_manager) {
- TemporaryFile tf;
- ASSERT_TRUE(tf.fd != -1);
- ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
- - TestInit(tf.path, test_function_map, commands);
- + TestInit(tf.path, test_function_map, commands, service_manager);
- }
- TEST(init, SimpleEventTrigger) {
- @@ -97,7 +99,8 @@ pass_test
- ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
- std::vector<ActionManagerCommand> commands{trigger_boot};
- - TestInitText(init_script, test_function_map, commands);
- + ServiceManager service_manager;
- + TestInitText(init_script, test_function_map, commands, &service_manager);
- EXPECT_TRUE(expect_true);
- }
- @@ -125,7 +128,28 @@ execute_third
- ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
- std::vector<ActionManagerCommand> commands{trigger_boot};
- - TestInitText(init_script, test_function_map, commands);
- + ServiceManager service_manager;
- + TestInitText(init_script, test_function_map, commands, &service_manager);
- +}
- +
- +TEST(init, OverrideService) {
- + std::string init_script = R"init(
- +service A something
- + class first
- +service A something
- + class second
- + override
- +)init";
- +
- + ServiceManager service_manager;
- + TestInitText(init_script, TestFunctionMap(), {}, &service_manager);
- + ASSERT_EQ(1, std::distance(service_manager.begin(), service_manager.end()));
- +
- + auto service = service_manager.begin()->get();
- + ASSERT_NE(nullptr, service);
- + EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
- + EXPECT_EQ("A", service->name());
- + EXPECT_TRUE(service->is_override());
- }
- TEST(init, EventTriggerOrderMultipleFiles) {
- @@ -184,7 +208,9 @@ TEST(init, EventTriggerOrderMultipleFiles) {
- ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
- std::vector<ActionManagerCommand> commands{trigger_boot};
- - TestInit(start.path, test_function_map, commands);
- + ServiceManager service_manager;
- +
- + TestInit(start.path, test_function_map, commands, &service_manager);
- EXPECT_EQ(6, num_executed);
- }
- diff --git a/init/service.cpp b/init/service.cpp
- index 2a0e0dfd9..9c07ea9c5 100644
- --- a/init/service.cpp
- +++ b/init/service.cpp
- @@ -514,6 +514,11 @@ bool Service::ParseOomScoreAdjust(const std::vector<std::string>& args, std::str
- return true;
- }
- +bool Service::ParseOverride(const std::vector<std::string>& args, std::string* err) {
- + override_ = true;
- + return true;
- +}
- +
- bool Service::ParseMemcgSwappiness(const std::vector<std::string>& args, std::string* err) {
- if (!ParseInt(args[1], &swappiness_, 0)) {
- *err = "swappiness value must be equal or greater than 0";
- @@ -654,6 +659,7 @@ const Service::OptionParserMap::Map& Service::OptionParserMap::map() const {
- {"keycodes", {1, kMax, &Service::ParseKeycodes}},
- {"oneshot", {0, 0, &Service::ParseOneshot}},
- {"onrestart", {1, kMax, &Service::ParseOnrestart}},
- + {"override", {0, 0, &Service::ParseOverride}},
- {"oom_score_adjust",
- {1, 1, &Service::ParseOomScoreAdjust}},
- {"memcg.swappiness",
- @@ -1257,12 +1263,6 @@ bool ServiceParser::ParseSection(std::vector<std::string>&& args, const std::str
- return false;
- }
- - Service* old_service = service_manager_->FindServiceByName(name);
- - if (old_service) {
- - *err = "ignored duplicate definition of service '" + name + "'";
- - return false;
- - }
- -
- std::vector<std::string> str_args(args.begin() + 2, args.end());
- service_ = std::make_unique<Service>(name, str_args);
- return true;
- @@ -1274,6 +1274,15 @@ bool ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line,
- void ServiceParser::EndSection() {
- if (service_) {
- + Service* old_service = service_manager_->FindServiceByName(service_->name());
- + if (old_service) {
- + if (!service_->is_override()) {
- + LOG(ERROR) << "ignored duplicate definition of service '" << service_->name() << "'";
- + }
- + service_manager_->RemoveService(*old_service);
- + old_service = nullptr;
- + }
- +
- service_manager_->AddService(std::move(service_));
- }
- }
- diff --git a/init/service.h b/init/service.h
- index 62a3299e1..46cb9618d 100644
- --- a/init/service.h
- +++ b/init/service.h
- @@ -107,6 +107,7 @@ class Service {
- int ioprio_pri() const { return ioprio_pri_; }
- int priority() const { return priority_; }
- int oom_score_adjust() const { return oom_score_adjust_; }
- + bool is_override() const { return override_; }
- bool process_cgroup_empty() const { return process_cgroup_empty_; }
- const std::vector<std::string>& args() const { return args_; }
- @@ -134,6 +135,7 @@ class Service {
- bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
- bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
- bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
- + bool ParseOverride(const std::vector<std::string>& args, std::string* err);
- bool ParseMemcgLimitInBytes(const std::vector<std::string>& args, std::string* err);
- bool ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args, std::string* err);
- bool ParseMemcgSwappiness(const std::vector<std::string>& args, std::string* err);
- @@ -190,6 +192,8 @@ class Service {
- bool process_cgroup_empty_ = false;
- + bool override_ = false;
- +
- std::vector<std::string> args_;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement