2017-04-20 01:18:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-02-14 16:54:58 +01:00
|
|
|
#include <fstream>
|
2017-04-20 01:18:50 +02:00
|
|
|
#include <functional>
|
2022-07-16 00:02:14 +02:00
|
|
|
#include <string_view>
|
2023-01-04 03:28:34 +01:00
|
|
|
#include <thread>
|
2022-07-16 00:02:14 +02:00
|
|
|
#include <type_traits>
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
#include <android-base/file.h>
|
2020-12-11 18:59:38 +01:00
|
|
|
#include <android-base/logging.h>
|
2020-07-09 17:47:24 +02:00
|
|
|
#include <android-base/properties.h>
|
2023-02-14 16:54:58 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2022-12-08 00:39:05 +01:00
|
|
|
#include <android/api-level.h>
|
2017-04-20 01:18:50 +02:00
|
|
|
#include <gtest/gtest.h>
|
2022-07-16 00:02:14 +02:00
|
|
|
#include <selinux/selinux.h>
|
2022-12-08 00:39:05 +01:00
|
|
|
#include <sys/resource.h>
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
#include "action.h"
|
2018-02-14 00:36:14 +01:00
|
|
|
#include "action_manager.h"
|
2018-02-14 00:25:29 +01:00
|
|
|
#include "action_parser.h"
|
2019-07-23 01:05:36 +02:00
|
|
|
#include "builtin_arguments.h"
|
2017-04-20 01:18:50 +02:00
|
|
|
#include "builtins.h"
|
|
|
|
#include "import_parser.h"
|
2022-07-16 00:02:14 +02:00
|
|
|
#include "init.h"
|
2017-04-20 01:18:50 +02:00
|
|
|
#include "keyword_map.h"
|
2017-07-27 21:54:48 +02:00
|
|
|
#include "parser.h"
|
2017-11-14 00:31:54 +01:00
|
|
|
#include "service.h"
|
2019-06-26 19:46:20 +02:00
|
|
|
#include "service_list.h"
|
|
|
|
#include "service_parser.h"
|
2017-04-20 01:18:50 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2020-07-09 17:47:24 +02:00
|
|
|
using android::base::GetIntProperty;
|
2022-07-07 08:25:02 +02:00
|
|
|
using android::base::GetProperty;
|
|
|
|
using android::base::SetProperty;
|
2023-02-14 16:54:58 +01:00
|
|
|
using android::base::StringPrintf;
|
2022-07-16 00:02:14 +02:00
|
|
|
using android::base::StringReplace;
|
2022-07-07 08:25:02 +02:00
|
|
|
using android::base::WaitForProperty;
|
|
|
|
using namespace std::literals;
|
2020-07-09 17:47:24 +02:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2017-04-20 01:18:50 +02:00
|
|
|
using ActionManagerCommand = std::function<void(ActionManager&)>;
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
|
2022-05-09 20:16:51 +02:00
|
|
|
const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
|
|
|
|
ServiceList* service_list) {
|
2017-04-20 01:18:50 +02:00
|
|
|
Action::set_function_map(&test_function_map);
|
|
|
|
|
|
|
|
Parser parser;
|
2019-06-27 21:18:08 +02:00
|
|
|
parser.AddSectionParser("service",
|
|
|
|
std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
|
2022-05-09 20:16:51 +02:00
|
|
|
parser.AddSectionParser("on", std::make_unique<ActionParser>(action_manager, nullptr));
|
2017-04-20 01:18:50 +02:00
|
|
|
parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser.ParseConfig(init_script_file));
|
|
|
|
|
|
|
|
for (const auto& command : commands) {
|
2022-05-09 20:16:51 +02:00
|
|
|
command(*action_manager);
|
2017-04-20 01:18:50 +02:00
|
|
|
}
|
|
|
|
|
2022-05-09 20:16:51 +02:00
|
|
|
while (action_manager->HasMoreCommands()) {
|
|
|
|
action_manager->ExecuteOneCommand();
|
2017-04-20 01:18:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
|
2022-05-09 20:16:51 +02:00
|
|
|
const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
|
|
|
|
ServiceList* service_list) {
|
2017-04-20 01:18:50 +02:00
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
|
2022-05-09 20:16:51 +02:00
|
|
|
TestInit(tf.path, test_function_map, commands, action_manager, service_list);
|
2017-04-20 01:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, SimpleEventTrigger) {
|
|
|
|
bool expect_true = false;
|
|
|
|
std::string init_script =
|
|
|
|
R"init(
|
|
|
|
on boot
|
|
|
|
pass_test
|
|
|
|
)init";
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_pass_test = [&expect_true](const BuiltinArguments&) {
|
|
|
|
expect_true = true;
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
BuiltinFunctionMap test_function_map = {
|
|
|
|
{"pass_test", {0, 0, {false, do_pass_test}}},
|
|
|
|
};
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
|
|
|
|
std::vector<ActionManagerCommand> commands{trigger_boot};
|
|
|
|
|
2022-05-09 20:16:51 +02:00
|
|
|
ActionManager action_manager;
|
2017-11-14 00:31:54 +01:00
|
|
|
ServiceList service_list;
|
2022-05-09 20:16:51 +02:00
|
|
|
TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(expect_true);
|
|
|
|
}
|
|
|
|
|
2019-10-10 21:42:37 +02:00
|
|
|
TEST(init, WrongEventTrigger) {
|
|
|
|
std::string init_script =
|
|
|
|
R"init(
|
|
|
|
on boot:
|
|
|
|
pass_test
|
|
|
|
)init";
|
|
|
|
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
|
|
|
|
|
|
|
|
ActionManager am;
|
|
|
|
|
|
|
|
Parser parser;
|
|
|
|
parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser.ParseConfig(tf.path));
|
|
|
|
ASSERT_EQ(1u, parser.parse_error_count());
|
|
|
|
}
|
|
|
|
|
2017-04-20 01:18:50 +02:00
|
|
|
TEST(init, EventTriggerOrder) {
|
|
|
|
std::string init_script =
|
|
|
|
R"init(
|
|
|
|
on boot
|
|
|
|
execute_first
|
|
|
|
|
|
|
|
on boot && property:ro.hardware=*
|
|
|
|
execute_second
|
|
|
|
|
|
|
|
on boot
|
|
|
|
execute_third
|
|
|
|
|
|
|
|
)init";
|
|
|
|
|
|
|
|
int num_executed = 0;
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_execute_first = [&num_executed](const BuiltinArguments&) {
|
|
|
|
EXPECT_EQ(0, num_executed++);
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
auto do_execute_second = [&num_executed](const BuiltinArguments&) {
|
|
|
|
EXPECT_EQ(1, num_executed++);
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
auto do_execute_third = [&num_executed](const BuiltinArguments&) {
|
|
|
|
EXPECT_EQ(2, num_executed++);
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
|
|
|
|
BuiltinFunctionMap test_function_map = {
|
|
|
|
{"execute_first", {0, 0, {false, do_execute_first}}},
|
|
|
|
{"execute_second", {0, 0, {false, do_execute_second}}},
|
|
|
|
{"execute_third", {0, 0, {false, do_execute_third}}},
|
|
|
|
};
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
|
|
|
|
std::vector<ActionManagerCommand> commands{trigger_boot};
|
|
|
|
|
2022-05-09 20:16:51 +02:00
|
|
|
ActionManager action_manager;
|
2017-11-14 00:31:54 +01:00
|
|
|
ServiceList service_list;
|
2022-05-09 20:16:51 +02:00
|
|
|
TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
|
2022-05-11 03:02:01 +02:00
|
|
|
EXPECT_EQ(3, num_executed);
|
2017-11-14 00:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, OverrideService) {
|
|
|
|
std::string init_script = R"init(
|
|
|
|
service A something
|
|
|
|
class first
|
2023-04-25 01:54:59 +02:00
|
|
|
user nobody
|
2017-11-14 00:31:54 +01:00
|
|
|
|
|
|
|
service A something
|
|
|
|
class second
|
2023-04-25 01:54:59 +02:00
|
|
|
user nobody
|
2017-11-14 00:31:54 +01:00
|
|
|
override
|
|
|
|
|
|
|
|
)init";
|
|
|
|
|
2022-05-09 20:16:51 +02:00
|
|
|
ActionManager action_manager;
|
2017-11-14 00:31:54 +01:00
|
|
|
ServiceList service_list;
|
2022-05-09 20:16:51 +02:00
|
|
|
TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
|
2017-11-14 00:31:54 +01:00
|
|
|
ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
|
|
|
|
|
|
|
|
auto service = service_list.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());
|
2017-04-20 01:18:50 +02:00
|
|
|
}
|
|
|
|
|
2022-11-14 18:30:51 +01:00
|
|
|
TEST(init, StartConsole) {
|
2022-12-20 01:18:49 +01:00
|
|
|
if (GetProperty("ro.build.type", "") == "user") {
|
|
|
|
GTEST_SKIP() << "Must run on userdebug/eng builds. b/262090304";
|
|
|
|
return;
|
|
|
|
}
|
2023-04-07 22:02:42 +02:00
|
|
|
if (getuid() != 0) {
|
|
|
|
GTEST_SKIP() << "Must be run as root.";
|
|
|
|
return;
|
|
|
|
}
|
2022-11-14 18:30:51 +01:00
|
|
|
std::string init_script = R"init(
|
|
|
|
service console /system/bin/sh
|
|
|
|
class core
|
2022-12-15 08:29:01 +01:00
|
|
|
console null
|
2022-11-14 18:30:51 +01:00
|
|
|
disabled
|
|
|
|
user root
|
|
|
|
group root shell log readproc
|
2022-11-23 01:53:05 +01:00
|
|
|
seclabel u:r:shell:s0
|
2022-11-14 18:30:51 +01:00
|
|
|
setenv HOSTNAME console
|
|
|
|
)init";
|
|
|
|
|
|
|
|
ActionManager action_manager;
|
|
|
|
ServiceList service_list;
|
|
|
|
TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
|
|
|
|
ASSERT_EQ(std::distance(service_list.begin(), service_list.end()), 1);
|
|
|
|
|
|
|
|
auto service = service_list.begin()->get();
|
|
|
|
ASSERT_NE(service, nullptr);
|
|
|
|
ASSERT_RESULT_OK(service->Start());
|
|
|
|
const pid_t pid = service->pid();
|
|
|
|
ASSERT_GT(pid, 0);
|
2022-11-23 01:53:05 +01:00
|
|
|
EXPECT_NE(getsid(pid), 0);
|
2022-11-14 18:30:51 +01:00
|
|
|
service->Stop();
|
|
|
|
}
|
|
|
|
|
2022-07-16 00:02:14 +02:00
|
|
|
static std::string GetSecurityContext() {
|
|
|
|
char* ctx;
|
|
|
|
if (getcon(&ctx) == -1) {
|
|
|
|
ADD_FAILURE() << "Failed to call getcon : " << strerror(errno);
|
|
|
|
}
|
|
|
|
std::string result = std::string(ctx);
|
|
|
|
freecon(ctx);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestStartApexServices(const std::vector<std::string>& service_names,
|
|
|
|
const std::string& apex_name) {
|
|
|
|
for (auto const& svc : service_names) {
|
|
|
|
auto service = ServiceList::GetInstance().FindService(svc);
|
|
|
|
ASSERT_NE(nullptr, service);
|
|
|
|
ASSERT_RESULT_OK(service->Start());
|
|
|
|
ASSERT_TRUE(service->IsRunning());
|
|
|
|
LOG(INFO) << "Service " << svc << " is running";
|
|
|
|
if (!apex_name.empty()) {
|
|
|
|
service->set_filename("/apex/" + apex_name + "/init_test.rc");
|
|
|
|
} else {
|
|
|
|
service->set_filename("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!apex_name.empty()) {
|
|
|
|
auto apex_services = ServiceList::GetInstance().FindServicesByApexName(apex_name);
|
|
|
|
EXPECT_EQ(service_names.size(), apex_services.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestStopApexServices(const std::vector<std::string>& service_names, bool expect_to_run) {
|
|
|
|
for (auto const& svc : service_names) {
|
|
|
|
auto service = ServiceList::GetInstance().FindService(svc);
|
|
|
|
ASSERT_NE(nullptr, service);
|
|
|
|
EXPECT_EQ(expect_to_run, service->IsRunning());
|
|
|
|
}
|
2022-07-22 01:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRemoveApexService(const std::vector<std::string>& service_names, bool exist) {
|
|
|
|
for (auto const& svc : service_names) {
|
|
|
|
auto service = ServiceList::GetInstance().FindService(svc);
|
|
|
|
ASSERT_EQ(exist, service != nullptr);
|
|
|
|
}
|
2022-07-16 00:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitApexService(const std::string_view& init_template) {
|
|
|
|
std::string init_script = StringReplace(init_template, "$selabel",
|
|
|
|
GetSecurityContext(), true);
|
|
|
|
|
2022-07-22 01:05:13 +02:00
|
|
|
TestInitText(init_script, BuiltinFunctionMap(), {}, &ActionManager::GetInstance(),
|
2022-07-16 00:02:14 +02:00
|
|
|
&ServiceList::GetInstance());
|
|
|
|
}
|
|
|
|
|
2022-09-08 00:25:44 +02:00
|
|
|
void CleanupApexServices() {
|
|
|
|
std::vector<std::string> names;
|
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
|
|
|
names.push_back(s->name());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& name : names) {
|
|
|
|
auto s = ServiceList::GetInstance().FindService(name);
|
|
|
|
auto pid = s->pid();
|
|
|
|
ServiceList::GetInstance().RemoveService(*s);
|
|
|
|
if (pid > 0) {
|
|
|
|
kill(pid, SIGTERM);
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionManager::GetInstance().RemoveActionIf([&](const std::unique_ptr<Action>& s) -> bool {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-16 00:02:14 +02:00
|
|
|
void TestApexServicesInit(const std::vector<std::string>& apex_services,
|
|
|
|
const std::vector<std::string>& other_apex_services,
|
|
|
|
const std::vector<std::string> non_apex_services) {
|
|
|
|
auto num_svc = apex_services.size() + other_apex_services.size() + non_apex_services.size();
|
2022-07-22 01:05:13 +02:00
|
|
|
ASSERT_EQ(num_svc, ServiceList::GetInstance().size());
|
2022-07-16 00:02:14 +02:00
|
|
|
|
|
|
|
TestStartApexServices(apex_services, "com.android.apex.test_service");
|
|
|
|
TestStartApexServices(other_apex_services, "com.android.other_apex.test_service");
|
|
|
|
TestStartApexServices(non_apex_services, /*apex_anme=*/ "");
|
|
|
|
|
|
|
|
StopServicesFromApex("com.android.apex.test_service");
|
|
|
|
TestStopApexServices(apex_services, /*expect_to_run=*/ false);
|
|
|
|
TestStopApexServices(other_apex_services, /*expect_to_run=*/ true);
|
|
|
|
TestStopApexServices(non_apex_services, /*expect_to_run=*/ true);
|
|
|
|
|
2022-07-22 01:05:13 +02:00
|
|
|
RemoveServiceAndActionFromApex("com.android.apex.test_service");
|
|
|
|
ASSERT_EQ(other_apex_services.size() + non_apex_services.size(),
|
|
|
|
ServiceList::GetInstance().size());
|
|
|
|
|
|
|
|
// TODO(b/244232142): Add test to check if actions are removed
|
|
|
|
TestRemoveApexService(apex_services, /*exist*/ false);
|
|
|
|
TestRemoveApexService(other_apex_services, /*exist*/ true);
|
|
|
|
TestRemoveApexService(non_apex_services, /*exist*/ true);
|
|
|
|
|
2022-09-08 00:25:44 +02:00
|
|
|
CleanupApexServices();
|
2022-07-16 00:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, StopServiceByApexName) {
|
2022-09-06 02:58:47 +02:00
|
|
|
if (getuid() != 0) {
|
|
|
|
GTEST_SKIP() << "Must be run as root.";
|
|
|
|
return;
|
|
|
|
}
|
2022-07-16 00:02:14 +02:00
|
|
|
std::string_view script_template = R"init(
|
|
|
|
service apex_test_service /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(script_template);
|
|
|
|
TestApexServicesInit({"apex_test_service"}, {}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, StopMultipleServicesByApexName) {
|
2022-09-06 02:58:47 +02:00
|
|
|
if (getuid() != 0) {
|
|
|
|
GTEST_SKIP() << "Must be run as root.";
|
|
|
|
return;
|
|
|
|
}
|
2022-07-16 00:02:14 +02:00
|
|
|
std::string_view script_template = R"init(
|
|
|
|
service apex_test_service_multiple_a /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
service apex_test_service_multiple_b /system/bin/id
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(script_template);
|
|
|
|
TestApexServicesInit({"apex_test_service_multiple_a",
|
|
|
|
"apex_test_service_multiple_b"}, {}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, StopServicesFromMultipleApexes) {
|
2022-09-06 02:58:47 +02:00
|
|
|
if (getuid() != 0) {
|
|
|
|
GTEST_SKIP() << "Must be run as root.";
|
|
|
|
return;
|
|
|
|
}
|
2022-07-16 00:02:14 +02:00
|
|
|
std::string_view apex_script_template = R"init(
|
|
|
|
service apex_test_service_multi_apex_a /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
service apex_test_service_multi_apex_b /system/bin/id
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(apex_script_template);
|
|
|
|
|
|
|
|
std::string_view other_apex_script_template = R"init(
|
|
|
|
service apex_test_service_multi_apex_c /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(other_apex_script_template);
|
|
|
|
|
|
|
|
TestApexServicesInit({"apex_test_service_multi_apex_a",
|
|
|
|
"apex_test_service_multi_apex_b"}, {"apex_test_service_multi_apex_c"}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, StopServicesFromApexAndNonApex) {
|
2022-09-06 02:58:47 +02:00
|
|
|
if (getuid() != 0) {
|
|
|
|
GTEST_SKIP() << "Must be run as root.";
|
|
|
|
return;
|
|
|
|
}
|
2022-07-16 00:02:14 +02:00
|
|
|
std::string_view apex_script_template = R"init(
|
|
|
|
service apex_test_service_apex_a /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
service apex_test_service_apex_b /system/bin/id
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(apex_script_template);
|
|
|
|
|
|
|
|
std::string_view non_apex_script_template = R"init(
|
|
|
|
service apex_test_service_non_apex /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(non_apex_script_template);
|
|
|
|
|
|
|
|
TestApexServicesInit({"apex_test_service_apex_a",
|
|
|
|
"apex_test_service_apex_b"}, {}, {"apex_test_service_non_apex"});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, StopServicesFromApexMixed) {
|
2022-09-06 02:58:47 +02:00
|
|
|
if (getuid() != 0) {
|
|
|
|
GTEST_SKIP() << "Must be run as root.";
|
|
|
|
return;
|
|
|
|
}
|
2022-07-16 00:02:14 +02:00
|
|
|
std::string_view script_template = R"init(
|
|
|
|
service apex_test_service_mixed_a /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(script_template);
|
|
|
|
|
|
|
|
std::string_view other_apex_script_template = R"init(
|
|
|
|
service apex_test_service_mixed_b /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(other_apex_script_template);
|
|
|
|
|
|
|
|
std::string_view non_apex_script_template = R"init(
|
|
|
|
service apex_test_service_mixed_c /system/bin/yes
|
|
|
|
user shell
|
|
|
|
group shell
|
|
|
|
seclabel $selabel
|
|
|
|
)init";
|
|
|
|
InitApexService(non_apex_script_template);
|
|
|
|
|
|
|
|
TestApexServicesInit({"apex_test_service_mixed_a"},
|
|
|
|
{"apex_test_service_mixed_b"}, {"apex_test_service_mixed_c"});
|
|
|
|
}
|
|
|
|
|
2017-04-20 01:18:50 +02:00
|
|
|
TEST(init, EventTriggerOrderMultipleFiles) {
|
|
|
|
// 6 total files, which should have their triggers executed in the following order:
|
|
|
|
// 1: start - original script parsed
|
|
|
|
// 2: first_import - immediately imported by first_script
|
|
|
|
// 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
|
|
|
|
// 4: a_import - file imported by dir_a
|
|
|
|
// 5: dir_b - file named 'b.rc' in dir
|
|
|
|
// 6: last_import - imported after dir is imported
|
|
|
|
|
|
|
|
TemporaryFile first_import;
|
|
|
|
ASSERT_TRUE(first_import.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
|
|
|
|
|
|
|
|
TemporaryFile dir_a_import;
|
|
|
|
ASSERT_TRUE(dir_a_import.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
|
|
|
|
|
|
|
|
TemporaryFile last_import;
|
|
|
|
ASSERT_TRUE(last_import.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
|
|
|
|
|
|
|
|
TemporaryDir dir;
|
|
|
|
// clang-format off
|
|
|
|
std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
|
|
|
|
"on boot\n"
|
|
|
|
"execute 3";
|
|
|
|
// clang-format on
|
2017-05-05 03:17:33 +02:00
|
|
|
// WriteFile() ensures the right mode is set
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
|
2017-04-20 01:18:50 +02:00
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
std::string start_script = "import " + std::string(first_import.path) + "\n"
|
|
|
|
"import " + std::string(dir.path) + "\n"
|
|
|
|
"import " + std::string(last_import.path) + "\n"
|
|
|
|
"on boot\n"
|
|
|
|
"execute 1";
|
|
|
|
// clang-format on
|
|
|
|
TemporaryFile start;
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
|
|
|
|
|
|
|
|
int num_executed = 0;
|
2017-09-13 00:58:47 +02:00
|
|
|
auto execute_command = [&num_executed](const BuiltinArguments& args) {
|
2017-04-20 01:18:50 +02:00
|
|
|
EXPECT_EQ(2U, args.size());
|
|
|
|
EXPECT_EQ(++num_executed, std::stoi(args[1]));
|
2019-06-10 20:08:01 +02:00
|
|
|
return Result<void>{};
|
2017-04-20 01:18:50 +02:00
|
|
|
};
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
BuiltinFunctionMap test_function_map = {
|
|
|
|
{"execute", {1, 1, {false, execute_command}}},
|
|
|
|
};
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
|
|
|
|
std::vector<ActionManagerCommand> commands{trigger_boot};
|
|
|
|
|
2022-05-09 20:16:51 +02:00
|
|
|
ActionManager action_manager;
|
2017-11-14 00:31:54 +01:00
|
|
|
ServiceList service_list;
|
2022-05-09 20:16:51 +02:00
|
|
|
TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(6, num_executed);
|
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
2022-05-09 20:16:51 +02:00
|
|
|
BuiltinFunctionMap GetTestFunctionMapForLazyLoad(int& num_executed, ActionManager& action_manager) {
|
|
|
|
auto execute_command = [&num_executed](const BuiltinArguments& args) {
|
|
|
|
EXPECT_EQ(2U, args.size());
|
|
|
|
EXPECT_EQ(++num_executed, std::stoi(args[1]));
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
auto load_command = [&action_manager](const BuiltinArguments& args) -> Result<void> {
|
|
|
|
EXPECT_EQ(2U, args.size());
|
|
|
|
Parser parser;
|
|
|
|
parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, nullptr));
|
|
|
|
if (!parser.ParseConfig(args[1])) {
|
|
|
|
return Error() << "Failed to load";
|
|
|
|
}
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
auto trigger_command = [&action_manager](const BuiltinArguments& args) {
|
|
|
|
EXPECT_EQ(2U, args.size());
|
|
|
|
LOG(INFO) << "Queue event trigger: " << args[1];
|
|
|
|
action_manager.QueueEventTrigger(args[1]);
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
BuiltinFunctionMap test_function_map = {
|
|
|
|
{"execute", {1, 1, {false, execute_command}}},
|
|
|
|
{"load", {1, 1, {false, load_command}}},
|
|
|
|
{"trigger", {1, 1, {false, trigger_command}}},
|
|
|
|
};
|
|
|
|
return test_function_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, LazilyLoadedActionsCantBeTriggeredByTheSameTrigger) {
|
|
|
|
// "start" script loads "lazy" script. Even though "lazy" scripts
|
|
|
|
// defines "on boot" action, it's not executed by the current "boot"
|
|
|
|
// event because it's already processed.
|
|
|
|
TemporaryFile lazy;
|
|
|
|
ASSERT_TRUE(lazy.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", lazy.fd));
|
|
|
|
|
|
|
|
TemporaryFile start;
|
|
|
|
// clang-format off
|
|
|
|
std::string start_script = "on boot\n"
|
|
|
|
"load " + std::string(lazy.path) + "\n"
|
|
|
|
"execute 1";
|
|
|
|
// clang-format on
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
|
|
|
|
|
|
|
|
int num_executed = 0;
|
|
|
|
ActionManager action_manager;
|
|
|
|
ServiceList service_list;
|
|
|
|
BuiltinFunctionMap test_function_map =
|
|
|
|
GetTestFunctionMapForLazyLoad(num_executed, action_manager);
|
|
|
|
|
|
|
|
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
|
|
|
|
std::vector<ActionManagerCommand> commands{trigger_boot};
|
|
|
|
TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
|
|
|
|
|
|
|
|
EXPECT_EQ(1, num_executed);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, LazilyLoadedActionsCanBeTriggeredByTheNextTrigger) {
|
|
|
|
// "start" script loads "lazy" script and then triggers "next" event
|
|
|
|
// which executes "on next" action loaded by the previous command.
|
|
|
|
TemporaryFile lazy;
|
|
|
|
ASSERT_TRUE(lazy.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("on next\nexecute 2", lazy.fd));
|
|
|
|
|
|
|
|
TemporaryFile start;
|
|
|
|
// clang-format off
|
|
|
|
std::string start_script = "on boot\n"
|
|
|
|
"load " + std::string(lazy.path) + "\n"
|
|
|
|
"execute 1\n"
|
|
|
|
"trigger next";
|
|
|
|
// clang-format on
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
|
|
|
|
|
|
|
|
int num_executed = 0;
|
|
|
|
ActionManager action_manager;
|
|
|
|
ServiceList service_list;
|
|
|
|
BuiltinFunctionMap test_function_map =
|
|
|
|
GetTestFunctionMapForLazyLoad(num_executed, action_manager);
|
|
|
|
|
|
|
|
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
|
|
|
|
std::vector<ActionManagerCommand> commands{trigger_boot};
|
|
|
|
TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
|
|
|
|
|
|
|
|
EXPECT_EQ(2, num_executed);
|
|
|
|
}
|
|
|
|
|
2023-04-25 01:54:59 +02:00
|
|
|
TEST(init, RejectsNoUserStartingInV) {
|
|
|
|
std::string init_script =
|
|
|
|
R"init(
|
|
|
|
service A something
|
|
|
|
class first
|
|
|
|
)init";
|
|
|
|
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
|
|
|
|
|
|
|
|
ServiceList service_list;
|
|
|
|
Parser parser;
|
|
|
|
parser.AddSectionParser("service",
|
|
|
|
std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt));
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser.ParseConfig(tf.path));
|
|
|
|
|
|
|
|
if (GetIntProperty("ro.vendor.api_level", 0) > __ANDROID_API_U__) {
|
|
|
|
ASSERT_EQ(1u, parser.parse_error_count());
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(0u, parser.parse_error_count());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:40:39 +02:00
|
|
|
TEST(init, RejectsCriticalAndOneshotService) {
|
2020-07-09 17:47:24 +02:00
|
|
|
if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
|
|
|
|
GTEST_SKIP() << "Test only valid for devices launching with R or later";
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:40:39 +02:00
|
|
|
std::string init_script =
|
|
|
|
R"init(
|
|
|
|
service A something
|
|
|
|
class first
|
2023-04-25 01:54:59 +02:00
|
|
|
user root
|
2020-04-30 20:40:39 +02:00
|
|
|
critical
|
|
|
|
oneshot
|
|
|
|
)init";
|
|
|
|
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
|
|
|
|
|
|
|
|
ServiceList service_list;
|
|
|
|
Parser parser;
|
|
|
|
parser.AddSectionParser("service",
|
|
|
|
std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt));
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser.ParseConfig(tf.path));
|
|
|
|
ASSERT_EQ(1u, parser.parse_error_count());
|
|
|
|
}
|
|
|
|
|
2022-12-08 00:39:05 +01:00
|
|
|
TEST(init, MemLockLimit) {
|
|
|
|
// Test is enforced only for U+ devices
|
|
|
|
if (android::base::GetIntProperty("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
|
|
|
|
GTEST_SKIP();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are running memlock at, or under, 64KB
|
|
|
|
const unsigned long max_limit = 65536;
|
|
|
|
struct rlimit curr_limit;
|
|
|
|
ASSERT_EQ(getrlimit(RLIMIT_MEMLOCK, &curr_limit), 0);
|
|
|
|
ASSERT_LE(curr_limit.rlim_cur, max_limit);
|
|
|
|
ASSERT_LE(curr_limit.rlim_max, max_limit);
|
|
|
|
}
|
|
|
|
|
2023-02-14 18:52:31 +01:00
|
|
|
void CloseAllFds() {
|
|
|
|
DIR* dir;
|
|
|
|
struct dirent* ent;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((dir = opendir("/proc/self/fd"))) {
|
|
|
|
while ((ent = readdir(dir))) {
|
|
|
|
if (sscanf(ent->d_name, "%d", &fd) == 1) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:44:54 +01:00
|
|
|
pid_t ForkExecvpAsync(const char* argv[]) {
|
2023-01-04 03:28:34 +01:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
2023-02-14 16:44:54 +01:00
|
|
|
// Child process.
|
2023-02-14 18:52:31 +01:00
|
|
|
CloseAllFds();
|
2023-01-04 03:28:34 +01:00
|
|
|
|
2023-02-14 16:44:54 +01:00
|
|
|
execvp(argv[0], const_cast<char**>(argv));
|
2023-01-04 03:28:34 +01:00
|
|
|
PLOG(ERROR) << "exec in ForkExecvpAsync init test";
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
2023-02-14 16:44:54 +01:00
|
|
|
// Parent process.
|
2023-01-04 03:28:34 +01:00
|
|
|
if (pid == -1) {
|
|
|
|
PLOG(ERROR) << "fork in ForkExecvpAsync init test";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:54:58 +01:00
|
|
|
pid_t TracerPid(pid_t pid) {
|
|
|
|
static constexpr std::string_view prefix{"TracerPid:"};
|
|
|
|
std::ifstream is(StringPrintf("/proc/%d/status", pid));
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(is, line)) {
|
|
|
|
if (line.find(prefix) == 0) {
|
|
|
|
return atoi(line.substr(prefix.length()).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-01-04 03:28:34 +01:00
|
|
|
TEST(init, GentleKill) {
|
2023-02-09 02:25:47 +01:00
|
|
|
if (getuid() != 0) {
|
|
|
|
GTEST_SKIP() << "Must be run as root.";
|
|
|
|
return;
|
|
|
|
}
|
2023-01-04 03:28:34 +01:00
|
|
|
std::string init_script = R"init(
|
|
|
|
service test_gentle_kill /system/bin/sleep 1000
|
|
|
|
disabled
|
|
|
|
oneshot
|
|
|
|
gentle_kill
|
|
|
|
user root
|
|
|
|
group root
|
|
|
|
seclabel u:r:toolbox:s0
|
|
|
|
)init";
|
|
|
|
|
|
|
|
ActionManager action_manager;
|
|
|
|
ServiceList service_list;
|
|
|
|
TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
|
|
|
|
ASSERT_EQ(std::distance(service_list.begin(), service_list.end()), 1);
|
|
|
|
|
|
|
|
auto service = service_list.begin()->get();
|
|
|
|
ASSERT_NE(service, nullptr);
|
|
|
|
ASSERT_RESULT_OK(service->Start());
|
|
|
|
const pid_t pid = service->pid();
|
|
|
|
ASSERT_GT(pid, 0);
|
|
|
|
EXPECT_NE(getsid(pid), 0);
|
|
|
|
|
|
|
|
TemporaryFile logfile;
|
|
|
|
logfile.DoNotRemove();
|
|
|
|
ASSERT_TRUE(logfile.fd != -1);
|
|
|
|
|
2023-02-14 16:44:54 +01:00
|
|
|
std::string pid_str = std::to_string(pid);
|
|
|
|
const char* argv[] = {"/system/bin/strace", "-o", logfile.path, "-e", "signal", "-p",
|
|
|
|
pid_str.c_str(), nullptr};
|
|
|
|
pid_t strace_pid = ForkExecvpAsync(argv);
|
2023-01-04 03:28:34 +01:00
|
|
|
|
2023-02-14 16:54:58 +01:00
|
|
|
// Give strace the chance to connect
|
|
|
|
while (TracerPid(pid) == 0) {
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
}
|
2023-01-04 03:28:34 +01:00
|
|
|
service->Stop();
|
|
|
|
|
|
|
|
int status;
|
|
|
|
waitpid(strace_pid, &status, 0);
|
|
|
|
|
|
|
|
std::string logs;
|
|
|
|
android::base::ReadFdToString(logfile.fd, &logs);
|
2023-02-14 16:44:54 +01:00
|
|
|
ASSERT_NE(logs.find("killed by SIGTERM"), std::string::npos);
|
2023-01-04 03:28:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 18:59:38 +01:00
|
|
|
class TestCaseLogger : public ::testing::EmptyTestEventListener {
|
|
|
|
void OnTestStart(const ::testing::TestInfo& test_info) override {
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " ("
|
|
|
|
<< test_info.file() << ":" << test_info.line() << ")";
|
|
|
|
#else
|
|
|
|
UNUSED(test_info);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|
2019-08-08 01:02:28 +02:00
|
|
|
|
|
|
|
int SubcontextTestChildMain(int, char**);
|
|
|
|
int FirmwareTestChildMain(int, char**);
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
if (argc > 1 && !strcmp(argv[1], "subcontext")) {
|
|
|
|
return SubcontextTestChildMain(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1 && !strcmp(argv[1], "firmware")) {
|
|
|
|
return FirmwareTestChildMain(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
testing::InitGoogleTest(&argc, argv);
|
2020-12-11 18:59:38 +01:00
|
|
|
testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
|
2019-08-08 01:02:28 +02:00
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|