1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <android-base/properties.h>
20 
21 #include <iostream>
22 
23 using ::android::base::GetProperty;
24 using ::android::base::SetProperty;
25 
ExpectKillingServiceRecovers(const std::string & service_name)26 void ExpectKillingServiceRecovers(const std::string& service_name) {
27     const std::string status_prop = "init.svc." + service_name;
28     const std::string pid_prop = "init.svc_debug_pid." + service_name;
29 
30     const std::string initial_pid = GetProperty(pid_prop, "");
31 
32     EXPECT_EQ("running", GetProperty(status_prop, "")) << status_prop;
33     EXPECT_NE("", initial_pid) << pid_prop;
34 
35     EXPECT_EQ(0, system(("kill -9 " + initial_pid).c_str()));
36 
37     constexpr size_t kMaxWaitMilliseconds = 10000;
38     constexpr size_t kRetryWaitMilliseconds = 100;
39 
40     constexpr size_t kRetryTimes = kMaxWaitMilliseconds / kRetryWaitMilliseconds;
41 
42     for (size_t retry = 0; retry < kRetryTimes; retry++) {
43         const std::string& pid = GetProperty(pid_prop, "");
44         if (pid != initial_pid && pid != "") break;
45         usleep(kRetryWaitMilliseconds * 1000);
46     }
47 
48     // svc_debug_pid is set after svc property
49     EXPECT_EQ("running", GetProperty(status_prop, ""));
50 }
51 
52 class InitKillServicesTest : public ::testing::TestWithParam<std::string> {};
53 
TEST_P(InitKillServicesTest,KillCriticalProcesses)54 TEST_P(InitKillServicesTest, KillCriticalProcesses) {
55     ExpectKillingServiceRecovers(GetParam());
56 
57     // Ensure that init is still responding
58     EXPECT_TRUE(SetProperty("test.death.test", "asdf"));
59     EXPECT_EQ(GetProperty("test.death.test", ""), "asdf");
60     EXPECT_TRUE(SetProperty("test.death.test", ""));
61 }
62 
PrintName(const testing::TestParamInfo<std::string> & info)63 static inline std::string PrintName(const testing::TestParamInfo<std::string>& info) {
64     return info.param;
65 }
66 
67 INSTANTIATE_TEST_CASE_P(DeathTest, InitKillServicesTest,
68                         ::testing::Values("lmkd", "ueventd", "hwservicemanager", "servicemanager"),
69                         PrintName);
70