diff --git a/init/init.cpp b/init/init.cpp index 535033d99..5f516b704 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -347,8 +347,8 @@ Parser CreateApexConfigParser(ActionManager& action_manager, ServiceList& servic } #endif // RECOVERY parser.AddSectionParser("service", - std::make_unique(&service_list, subcontext, std::nullopt, - /*from_apex=*/true)); + std::make_unique(&service_list, subcontext, + std::nullopt)); parser.AddSectionParser("on", std::make_unique(&action_manager, subcontext)); return parser; diff --git a/init/service.cpp b/init/service.cpp index 8c4ee9393..730b6b67c 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -130,13 +130,13 @@ bool Service::is_exec_service_running_ = false; std::chrono::time_point Service::exec_service_started_; Service::Service(const std::string& name, Subcontext* subcontext_for_restart_commands, - const std::vector& args, bool from_apex) - : Service(name, 0, 0, 0, {}, 0, "", subcontext_for_restart_commands, args, from_apex) {} + const std::string& filename, const std::vector& args) + : Service(name, 0, 0, 0, {}, 0, "", subcontext_for_restart_commands, filename, args) {} Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, const std::vector& supp_gids, int namespace_flags, const std::string& seclabel, Subcontext* subcontext_for_restart_commands, - const std::vector& args, bool from_apex) + const std::string& filename, const std::vector& args) : name_(name), classnames_({"default"}), flags_(flags), @@ -156,7 +156,7 @@ Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, oom_score_adjust_(DEFAULT_OOM_SCORE_ADJUST), start_order_(0), args_(args), - from_apex_(from_apex) {} + filename_(filename) {} void Service::NotifyStateChange(const std::string& new_state) const { if ((flags_ & SVC_TEMPORARY) != 0) { @@ -860,7 +860,7 @@ Result> Service::MakeTemporaryOneshotService( } return std::make_unique(name, flags, *uid, *gid, supp_gids, namespace_flags, seclabel, - nullptr, str_args, false); + nullptr, /*filename=*/"", str_args); } // This is used for snapuserd_proxy, which hands off a socket to snapuserd. It's diff --git a/init/service.h b/init/service.h index 4adbaa25e..f7f32d92d 100644 --- a/init/service.h +++ b/init/service.h @@ -66,12 +66,12 @@ class Service { public: Service(const std::string& name, Subcontext* subcontext_for_restart_commands, - const std::vector& args, bool from_apex = false); + const std::string& filename, const std::vector& args); Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, const std::vector& supp_gids, int namespace_flags, const std::string& seclabel, - Subcontext* subcontext_for_restart_commands, const std::vector& args, - bool from_apex = false); + Subcontext* subcontext_for_restart_commands, const std::string& filename, + const std::vector& args); static Result> MakeTemporaryOneshotService( const std::vector& args); @@ -133,7 +133,7 @@ class Service { const std::vector& args() const { return args_; } bool is_updatable() const { return updatable_; } bool is_post_data() const { return post_data_; } - bool is_from_apex() const { return from_apex_; } + bool is_from_apex() const { return base::StartsWith(filename_, "/apex/"); } void set_oneshot(bool value) { if (value) { flags_ |= SVC_ONESHOT; @@ -225,7 +225,7 @@ class Service { std::optional on_failure_reboot_target_; - bool from_apex_ = false; + std::string filename_; }; } // namespace init diff --git a/init/service_parser.cpp b/init/service_parser.cpp index 9e914ee7c..32c57c43c 100644 --- a/init/service_parser.cpp +++ b/init/service_parser.cpp @@ -647,7 +647,7 @@ Result ServiceParser::ParseSection(std::vector&& args, } } - service_ = std::make_unique(name, restart_action_subcontext, str_args, from_apex_); + service_ = std::make_unique(name, restart_action_subcontext, filename, str_args); return {}; } diff --git a/init/service_parser.h b/init/service_parser.h index 0fd2da588..54503dd1c 100644 --- a/init/service_parser.h +++ b/init/service_parser.h @@ -31,13 +31,11 @@ class ServiceParser : public SectionParser { public: ServiceParser( ServiceList* service_list, Subcontext* subcontext, - const std::optional& interface_inheritance_hierarchy, - bool from_apex = false) + const std::optional& interface_inheritance_hierarchy) : service_list_(service_list), subcontext_(subcontext), interface_inheritance_hierarchy_(interface_inheritance_hierarchy), - service_(nullptr), - from_apex_(from_apex) {} + service_(nullptr) {} Result ParseSection(std::vector&& args, const std::string& filename, int line) override; Result ParseLineSection(std::vector&& args, int line) override; @@ -92,7 +90,6 @@ class ServiceParser : public SectionParser { std::optional interface_inheritance_hierarchy_; std::unique_ptr service_; std::string filename_; - bool from_apex_ = false; }; } // namespace init diff --git a/init/service_test.cpp b/init/service_test.cpp index 22ee844ef..87a2ce524 100644 --- a/init/service_test.cpp +++ b/init/service_test.cpp @@ -39,7 +39,7 @@ TEST(service, pod_initialized) { std::vector dummy_args{"/bin/test"}; Service* service_in_old_memory = - new (old_memory) Service("test_old_memory", nullptr, dummy_args); + new (old_memory) Service("test_old_memory", nullptr, /*filename=*/"", dummy_args); EXPECT_EQ(0U, service_in_old_memory->flags()); EXPECT_EQ(0, service_in_old_memory->pid()); @@ -58,7 +58,8 @@ TEST(service, pod_initialized) { } Service* service_in_old_memory2 = new (old_memory) Service( - "test_old_memory", 0U, 0U, 0U, std::vector(), 0U, "", nullptr, dummy_args); + "test_old_memory", 0U, 0U, 0U, std::vector(), 0U, "", + nullptr, /*filename=*/"", dummy_args); EXPECT_EQ(0U, service_in_old_memory2->flags()); EXPECT_EQ(0, service_in_old_memory2->pid()); diff --git a/init/subcontext.cpp b/init/subcontext.cpp index bd2bec5cf..961e006e3 100644 --- a/init/subcontext.cpp +++ b/init/subcontext.cpp @@ -251,11 +251,8 @@ void Subcontext::Restart() { } bool Subcontext::PathMatchesSubcontext(const std::string& path) const { - static const std::string kApexDir = "/apex/"; - if (StartsWith(path, kApexDir)) { - auto begin = kApexDir.size(); - auto end = path.find('/', begin); - auto apex_name = path.substr(begin, end - begin); + auto apex_name = GetApexNameFromFileName(path); + if (!apex_name.empty()) { return std::find(apex_list_.begin(), apex_list_.end(), apex_name) != apex_list_.end(); } for (const auto& prefix : path_prefixes_) { diff --git a/init/util.cpp b/init/util.cpp index 523cce451..bfc3fb6cc 100644 --- a/init/util.cpp +++ b/init/util.cpp @@ -738,5 +738,15 @@ bool Has32BitAbi() { return has; } +std::string GetApexNameFromFileName(const std::string& path) { + static const std::string kApexDir = "/apex/"; + if (StartsWith(path, kApexDir)) { + auto begin = kApexDir.size(); + auto end = path.find('/', begin); + return path.substr(begin, end - begin); + } + return ""; +} + } // namespace init } // namespace android diff --git a/init/util.h b/init/util.h index 099b9eeae..daec470f9 100644 --- a/init/util.h +++ b/init/util.h @@ -107,5 +107,7 @@ void SetDefaultMountNamespaceReady(); bool IsMicrodroid(); bool Has32BitAbi(); + +std::string GetApexNameFromFileName(const std::string& path); } // namespace init } // namespace android