Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. diff --git a/init/init_test.cpp b/init/init_test.cpp
  2. index 0a4071b98..1d0858f76 100644
  3. --- a/init/init_test.cpp
  4. +++ b/init/init_test.cpp
  5. @@ -24,6 +24,7 @@
  6. #include "builtins.h"
  7. #include "import_parser.h"
  8. #include "init_parser.h"
  9. +#include "service.h"
  10. #include "keyword_map.h"
  11. #include "util.h"
  12.  
  13. @@ -55,12 +56,13 @@ class TestFunctionMap : public KeywordMap<BuiltinFunction> {
  14. using ActionManagerCommand = std::function<void(ActionManager&)>;
  15.  
  16. void TestInit(const std::string& init_script_file, const TestFunctionMap& test_function_map,
  17. - const std::vector<ActionManagerCommand>& commands) {
  18. + const std::vector<ActionManagerCommand>& commands, ServiceManager* service_manager) {
  19. ActionManager am;
  20.  
  21. Action::set_function_map(&test_function_map);
  22.  
  23. Parser parser;
  24. + parser.AddSectionParser("service", std::make_unique<ServiceParser>(service_manager));
  25. parser.AddSectionParser("on", std::make_unique<ActionParser>(&am));
  26. parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
  27.  
  28. @@ -76,11 +78,11 @@ void TestInit(const std::string& init_script_file, const TestFunctionMap& test_f
  29. }
  30.  
  31. void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map,
  32. - const std::vector<ActionManagerCommand>& commands) {
  33. + const std::vector<ActionManagerCommand>& commands, ServiceManager* service_manager) {
  34. TemporaryFile tf;
  35. ASSERT_TRUE(tf.fd != -1);
  36. ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
  37. - TestInit(tf.path, test_function_map, commands);
  38. + TestInit(tf.path, test_function_map, commands, service_manager);
  39. }
  40.  
  41. TEST(init, SimpleEventTrigger) {
  42. @@ -97,7 +99,8 @@ pass_test
  43. ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
  44. std::vector<ActionManagerCommand> commands{trigger_boot};
  45.  
  46. - TestInitText(init_script, test_function_map, commands);
  47. + ServiceManager service_manager;
  48. + TestInitText(init_script, test_function_map, commands, &service_manager);
  49.  
  50. EXPECT_TRUE(expect_true);
  51. }
  52. @@ -125,7 +128,28 @@ execute_third
  53. ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
  54. std::vector<ActionManagerCommand> commands{trigger_boot};
  55.  
  56. - TestInitText(init_script, test_function_map, commands);
  57. + ServiceManager service_manager;
  58. + TestInitText(init_script, test_function_map, commands, &service_manager);
  59. +}
  60. +
  61. +TEST(init, OverrideService) {
  62. + std::string init_script = R"init(
  63. +service A something
  64. + class first
  65. +service A something
  66. + class second
  67. + override
  68. +)init";
  69. +
  70. + ServiceManager service_manager;
  71. + TestInitText(init_script, TestFunctionMap(), {}, &service_manager);
  72. + ASSERT_EQ(1, std::distance(service_manager.begin(), service_manager.end()));
  73. +
  74. + auto service = service_manager.begin()->get();
  75. + ASSERT_NE(nullptr, service);
  76. + EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
  77. + EXPECT_EQ("A", service->name());
  78. + EXPECT_TRUE(service->is_override());
  79. }
  80.  
  81. TEST(init, EventTriggerOrderMultipleFiles) {
  82. @@ -184,7 +208,9 @@ TEST(init, EventTriggerOrderMultipleFiles) {
  83. ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
  84. std::vector<ActionManagerCommand> commands{trigger_boot};
  85.  
  86. - TestInit(start.path, test_function_map, commands);
  87. + ServiceManager service_manager;
  88. +
  89. + TestInit(start.path, test_function_map, commands, &service_manager);
  90.  
  91. EXPECT_EQ(6, num_executed);
  92. }
  93. diff --git a/init/service.cpp b/init/service.cpp
  94. index 2a0e0dfd9..9c07ea9c5 100644
  95. --- a/init/service.cpp
  96. +++ b/init/service.cpp
  97. @@ -514,6 +514,11 @@ bool Service::ParseOomScoreAdjust(const std::vector<std::string>& args, std::str
  98. return true;
  99. }
  100.  
  101. +bool Service::ParseOverride(const std::vector<std::string>& args, std::string* err) {
  102. + override_ = true;
  103. + return true;
  104. +}
  105. +
  106. bool Service::ParseMemcgSwappiness(const std::vector<std::string>& args, std::string* err) {
  107. if (!ParseInt(args[1], &swappiness_, 0)) {
  108. *err = "swappiness value must be equal or greater than 0";
  109. @@ -654,6 +659,7 @@ const Service::OptionParserMap::Map& Service::OptionParserMap::map() const {
  110. {"keycodes", {1, kMax, &Service::ParseKeycodes}},
  111. {"oneshot", {0, 0, &Service::ParseOneshot}},
  112. {"onrestart", {1, kMax, &Service::ParseOnrestart}},
  113. + {"override", {0, 0, &Service::ParseOverride}},
  114. {"oom_score_adjust",
  115. {1, 1, &Service::ParseOomScoreAdjust}},
  116. {"memcg.swappiness",
  117. @@ -1257,12 +1263,6 @@ bool ServiceParser::ParseSection(std::vector<std::string>&& args, const std::str
  118. return false;
  119. }
  120.  
  121. - Service* old_service = service_manager_->FindServiceByName(name);
  122. - if (old_service) {
  123. - *err = "ignored duplicate definition of service '" + name + "'";
  124. - return false;
  125. - }
  126. -
  127. std::vector<std::string> str_args(args.begin() + 2, args.end());
  128. service_ = std::make_unique<Service>(name, str_args);
  129. return true;
  130. @@ -1274,6 +1274,15 @@ bool ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line,
  131.  
  132. void ServiceParser::EndSection() {
  133. if (service_) {
  134. + Service* old_service = service_manager_->FindServiceByName(service_->name());
  135. + if (old_service) {
  136. + if (!service_->is_override()) {
  137. + LOG(ERROR) << "ignored duplicate definition of service '" << service_->name() << "'";
  138. + }
  139. + service_manager_->RemoveService(*old_service);
  140. + old_service = nullptr;
  141. + }
  142. +
  143. service_manager_->AddService(std::move(service_));
  144. }
  145. }
  146. diff --git a/init/service.h b/init/service.h
  147. index 62a3299e1..46cb9618d 100644
  148. --- a/init/service.h
  149. +++ b/init/service.h
  150. @@ -107,6 +107,7 @@ class Service {
  151. int ioprio_pri() const { return ioprio_pri_; }
  152. int priority() const { return priority_; }
  153. int oom_score_adjust() const { return oom_score_adjust_; }
  154. + bool is_override() const { return override_; }
  155. bool process_cgroup_empty() const { return process_cgroup_empty_; }
  156. const std::vector<std::string>& args() const { return args_; }
  157.  
  158. @@ -134,6 +135,7 @@ class Service {
  159. bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
  160. bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
  161. bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
  162. + bool ParseOverride(const std::vector<std::string>& args, std::string* err);
  163. bool ParseMemcgLimitInBytes(const std::vector<std::string>& args, std::string* err);
  164. bool ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args, std::string* err);
  165. bool ParseMemcgSwappiness(const std::vector<std::string>& args, std::string* err);
  166. @@ -190,6 +192,8 @@ class Service {
  167.  
  168. bool process_cgroup_empty_ = false;
  169.  
  170. + bool override_ = false;
  171. +
  172. std::vector<std::string> args_;
  173. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement