Merge "Add apex name to service"

This commit is contained in:
Deyao Ren 2022-07-22 23:03:45 +00:00 committed by Gerrit Code Review
commit ec73481e58
9 changed files with 32 additions and 25 deletions

View file

@ -347,8 +347,8 @@ Parser CreateApexConfigParser(ActionManager& action_manager, ServiceList& servic
} }
#endif // RECOVERY #endif // RECOVERY
parser.AddSectionParser("service", parser.AddSectionParser("service",
std::make_unique<ServiceParser>(&service_list, subcontext, std::nullopt, std::make_unique<ServiceParser>(&service_list, subcontext,
/*from_apex=*/true)); std::nullopt));
parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, subcontext)); parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, subcontext));
return parser; return parser;

View file

@ -130,13 +130,13 @@ bool Service::is_exec_service_running_ = false;
std::chrono::time_point<std::chrono::steady_clock> Service::exec_service_started_; std::chrono::time_point<std::chrono::steady_clock> Service::exec_service_started_;
Service::Service(const std::string& name, Subcontext* subcontext_for_restart_commands, Service::Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
const std::vector<std::string>& args, bool from_apex) const std::string& filename, const std::vector<std::string>& args)
: Service(name, 0, 0, 0, {}, 0, "", subcontext_for_restart_commands, args, from_apex) {} : 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, Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
const std::vector<gid_t>& supp_gids, int namespace_flags, const std::vector<gid_t>& supp_gids, int namespace_flags,
const std::string& seclabel, Subcontext* subcontext_for_restart_commands, const std::string& seclabel, Subcontext* subcontext_for_restart_commands,
const std::vector<std::string>& args, bool from_apex) const std::string& filename, const std::vector<std::string>& args)
: name_(name), : name_(name),
classnames_({"default"}), classnames_({"default"}),
flags_(flags), 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), oom_score_adjust_(DEFAULT_OOM_SCORE_ADJUST),
start_order_(0), start_order_(0),
args_(args), args_(args),
from_apex_(from_apex) {} filename_(filename) {}
void Service::NotifyStateChange(const std::string& new_state) const { void Service::NotifyStateChange(const std::string& new_state) const {
if ((flags_ & SVC_TEMPORARY) != 0) { if ((flags_ & SVC_TEMPORARY) != 0) {
@ -860,7 +860,7 @@ Result<std::unique_ptr<Service>> Service::MakeTemporaryOneshotService(
} }
return std::make_unique<Service>(name, flags, *uid, *gid, supp_gids, namespace_flags, seclabel, return std::make_unique<Service>(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 // This is used for snapuserd_proxy, which hands off a socket to snapuserd. It's

View file

@ -66,12 +66,12 @@ class Service {
public: public:
Service(const std::string& name, Subcontext* subcontext_for_restart_commands, Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
const std::vector<std::string>& args, bool from_apex = false); const std::string& filename, const std::vector<std::string>& args);
Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
const std::vector<gid_t>& supp_gids, int namespace_flags, const std::string& seclabel, const std::vector<gid_t>& supp_gids, int namespace_flags, const std::string& seclabel,
Subcontext* subcontext_for_restart_commands, const std::vector<std::string>& args, Subcontext* subcontext_for_restart_commands, const std::string& filename,
bool from_apex = false); const std::vector<std::string>& args);
static Result<std::unique_ptr<Service>> MakeTemporaryOneshotService( static Result<std::unique_ptr<Service>> MakeTemporaryOneshotService(
const std::vector<std::string>& args); const std::vector<std::string>& args);
@ -133,7 +133,7 @@ class Service {
const std::vector<std::string>& args() const { return args_; } const std::vector<std::string>& args() const { return args_; }
bool is_updatable() const { return updatable_; } bool is_updatable() const { return updatable_; }
bool is_post_data() const { return post_data_; } 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) { void set_oneshot(bool value) {
if (value) { if (value) {
flags_ |= SVC_ONESHOT; flags_ |= SVC_ONESHOT;
@ -225,7 +225,7 @@ class Service {
std::optional<std::string> on_failure_reboot_target_; std::optional<std::string> on_failure_reboot_target_;
bool from_apex_ = false; std::string filename_;
}; };
} // namespace init } // namespace init

View file

@ -647,7 +647,7 @@ Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
} }
} }
service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args, from_apex_); service_ = std::make_unique<Service>(name, restart_action_subcontext, filename, str_args);
return {}; return {};
} }

View file

@ -31,13 +31,11 @@ class ServiceParser : public SectionParser {
public: public:
ServiceParser( ServiceParser(
ServiceList* service_list, Subcontext* subcontext, ServiceList* service_list, Subcontext* subcontext,
const std::optional<InterfaceInheritanceHierarchyMap>& interface_inheritance_hierarchy, const std::optional<InterfaceInheritanceHierarchyMap>& interface_inheritance_hierarchy)
bool from_apex = false)
: service_list_(service_list), : service_list_(service_list),
subcontext_(subcontext), subcontext_(subcontext),
interface_inheritance_hierarchy_(interface_inheritance_hierarchy), interface_inheritance_hierarchy_(interface_inheritance_hierarchy),
service_(nullptr), service_(nullptr) {}
from_apex_(from_apex) {}
Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename, Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
int line) override; int line) override;
Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override; Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
@ -92,7 +90,6 @@ class ServiceParser : public SectionParser {
std::optional<InterfaceInheritanceHierarchyMap> interface_inheritance_hierarchy_; std::optional<InterfaceInheritanceHierarchyMap> interface_inheritance_hierarchy_;
std::unique_ptr<Service> service_; std::unique_ptr<Service> service_;
std::string filename_; std::string filename_;
bool from_apex_ = false;
}; };
} // namespace init } // namespace init

View file

@ -39,7 +39,7 @@ TEST(service, pod_initialized) {
std::vector<std::string> dummy_args{"/bin/test"}; std::vector<std::string> dummy_args{"/bin/test"};
Service* service_in_old_memory = 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(0U, service_in_old_memory->flags());
EXPECT_EQ(0, service_in_old_memory->pid()); 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( Service* service_in_old_memory2 = new (old_memory) Service(
"test_old_memory", 0U, 0U, 0U, std::vector<gid_t>(), 0U, "", nullptr, dummy_args); "test_old_memory", 0U, 0U, 0U, std::vector<gid_t>(), 0U, "",
nullptr, /*filename=*/"", dummy_args);
EXPECT_EQ(0U, service_in_old_memory2->flags()); EXPECT_EQ(0U, service_in_old_memory2->flags());
EXPECT_EQ(0, service_in_old_memory2->pid()); EXPECT_EQ(0, service_in_old_memory2->pid());

View file

@ -251,11 +251,8 @@ void Subcontext::Restart() {
} }
bool Subcontext::PathMatchesSubcontext(const std::string& path) const { bool Subcontext::PathMatchesSubcontext(const std::string& path) const {
static const std::string kApexDir = "/apex/"; auto apex_name = GetApexNameFromFileName(path);
if (StartsWith(path, kApexDir)) { if (!apex_name.empty()) {
auto begin = kApexDir.size();
auto end = path.find('/', begin);
auto apex_name = path.substr(begin, end - begin);
return std::find(apex_list_.begin(), apex_list_.end(), apex_name) != apex_list_.end(); return std::find(apex_list_.begin(), apex_list_.end(), apex_name) != apex_list_.end();
} }
for (const auto& prefix : path_prefixes_) { for (const auto& prefix : path_prefixes_) {

View file

@ -738,5 +738,15 @@ bool Has32BitAbi() {
return has; 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 init
} // namespace android } // namespace android

View file

@ -107,5 +107,7 @@ void SetDefaultMountNamespaceReady();
bool IsMicrodroid(); bool IsMicrodroid();
bool Has32BitAbi(); bool Has32BitAbi();
std::string GetApexNameFromFileName(const std::string& path);
} // namespace init } // namespace init
} // namespace android } // namespace android