Add apex name to service

Passed apex file name to service. The file name will be parsed
to determine 1) whether the service is from an apex; 2) apex name

Bug: 236090201

Change-Id: I2c292c0c067f4bf44bb25b1f80e4f972b94f7258
This commit is contained in:
Deyao Ren 2022-07-14 22:51:10 +00:00
parent 44f1f094b3
commit df40ed1be1
9 changed files with 32 additions and 25 deletions

View file

@ -347,8 +347,8 @@ Parser CreateApexConfigParser(ActionManager& action_manager, ServiceList& servic
}
#endif // RECOVERY
parser.AddSectionParser("service",
std::make_unique<ServiceParser>(&service_list, subcontext, std::nullopt,
/*from_apex=*/true));
std::make_unique<ServiceParser>(&service_list, subcontext,
std::nullopt));
parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, subcontext));
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_;
Service::Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
const std::vector<std::string>& args, bool from_apex)
: Service(name, 0, 0, 0, {}, 0, "", subcontext_for_restart_commands, args, from_apex) {}
const std::string& filename, const std::vector<std::string>& 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<gid_t>& supp_gids, int namespace_flags,
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),
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<std::unique_ptr<Service>> Service::MakeTemporaryOneshotService(
}
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

View file

@ -66,12 +66,12 @@ class Service {
public:
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,
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,
bool from_apex = false);
Subcontext* subcontext_for_restart_commands, const std::string& filename,
const std::vector<std::string>& args);
static Result<std::unique_ptr<Service>> MakeTemporaryOneshotService(
const std::vector<std::string>& args);
@ -133,7 +133,7 @@ class Service {
const std::vector<std::string>& 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<std::string> on_failure_reboot_target_;
bool from_apex_ = false;
std::string filename_;
};
} // 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 {};
}

View file

@ -31,13 +31,11 @@ class ServiceParser : public SectionParser {
public:
ServiceParser(
ServiceList* service_list, Subcontext* subcontext,
const std::optional<InterfaceInheritanceHierarchyMap>& interface_inheritance_hierarchy,
bool from_apex = false)
const std::optional<InterfaceInheritanceHierarchyMap>& interface_inheritance_hierarchy)
: service_list_(service_list),
subcontext_(subcontext),
interface_inheritance_hierarchy_(interface_inheritance_hierarchy),
service_(nullptr),
from_apex_(from_apex) {}
service_(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;
@ -92,7 +90,6 @@ class ServiceParser : public SectionParser {
std::optional<InterfaceInheritanceHierarchyMap> interface_inheritance_hierarchy_;
std::unique_ptr<Service> service_;
std::string filename_;
bool from_apex_ = false;
};
} // namespace init

View file

@ -39,7 +39,7 @@ TEST(service, pod_initialized) {
std::vector<std::string> 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<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(0, service_in_old_memory2->pid());

View file

@ -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_) {

View file

@ -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

View file

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