forked from LineageOS/android_system_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_test.cpp
805 lines (686 loc) · 25.1 KB
/
init_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
* 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.
*/
#include <fstream>
#include <functional>
#include <string_view>
#include <thread>
#include <type_traits>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android/api-level.h>
#include <gtest/gtest.h>
#include <selinux/selinux.h>
#include <sys/resource.h>
#include "action.h"
#include "action_manager.h"
#include "action_parser.h"
#include "builtin_arguments.h"
#include "builtins.h"
#include "import_parser.h"
#include "init.h"
#include "keyword_map.h"
#include "parser.h"
#include "service.h"
#include "service_list.h"
#include "service_parser.h"
#include "util.h"
using android::base::GetIntProperty;
using android::base::GetProperty;
using android::base::SetProperty;
using android::base::StringPrintf;
using android::base::StringReplace;
using android::base::WaitForProperty;
using namespace std::literals;
namespace android {
namespace init {
using ActionManagerCommand = std::function<void(ActionManager&)>;
void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
ServiceList* service_list) {
Action::set_function_map(&test_function_map);
Parser parser;
parser.AddSectionParser("service",
std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
parser.AddSectionParser("on", std::make_unique<ActionParser>(action_manager, nullptr));
parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
ASSERT_TRUE(parser.ParseConfig(init_script_file));
for (const auto& command : commands) {
command(*action_manager);
}
while (action_manager->HasMoreCommands()) {
action_manager->ExecuteOneCommand();
}
}
void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
ServiceList* service_list) {
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
TestInit(tf.path, test_function_map, commands, action_manager, service_list);
}
TEST(init, SimpleEventTrigger) {
bool expect_true = false;
std::string init_script =
R"init(
on boot
pass_test
)init";
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}}},
};
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
std::vector<ActionManagerCommand> commands{trigger_boot};
ActionManager action_manager;
ServiceList service_list;
TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
EXPECT_TRUE(expect_true);
}
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());
}
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;
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}}},
};
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
std::vector<ActionManagerCommand> commands{trigger_boot};
ActionManager action_manager;
ServiceList service_list;
TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
EXPECT_EQ(3, num_executed);
}
TEST(init, OverrideService) {
std::string init_script = R"init(
service A something
class first
user nobody
service A something
class second
user nobody
override
)init";
ActionManager action_manager;
ServiceList service_list;
TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
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());
}
TEST(init, StartConsole) {
if (GetProperty("ro.build.type", "") == "user") {
GTEST_SKIP() << "Must run on userdebug/eng builds. b/262090304";
return;
}
if (getuid() != 0) {
GTEST_SKIP() << "Must be run as root.";
return;
}
std::string init_script = R"init(
service console /system/bin/sh
class core
console null
disabled
user root
group root shell log readproc
seclabel u:r:shell:s0
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);
EXPECT_NE(getsid(pid), 0);
service->Stop();
}
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());
}
}
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);
}
}
void InitApexService(const std::string_view& init_template) {
std::string init_script = StringReplace(init_template, "$selabel",
GetSecurityContext(), true);
TestInitText(init_script, BuiltinFunctionMap(), {}, &ActionManager::GetInstance(),
&ServiceList::GetInstance());
}
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;
});
}
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();
ASSERT_EQ(num_svc, ServiceList::GetInstance().size());
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);
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);
CleanupApexServices();
}
TEST(init, StopServiceByApexName) {
if (getuid() != 0) {
GTEST_SKIP() << "Must be run as root.";
return;
}
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) {
if (getuid() != 0) {
GTEST_SKIP() << "Must be run as root.";
return;
}
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) {
if (getuid() != 0) {
GTEST_SKIP() << "Must be run as root.";
return;
}
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) {
if (getuid() != 0) {
GTEST_SKIP() << "Must be run as root.";
return;
}
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) {
if (getuid() != 0) {
GTEST_SKIP() << "Must be run as root.";
return;
}
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"});
}
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
// WriteFile() ensures the right mode is set
ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
// 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;
auto execute_command = [&num_executed](const BuiltinArguments& args) {
EXPECT_EQ(2U, args.size());
EXPECT_EQ(++num_executed, std::stoi(args[1]));
return Result<void>{};
};
BuiltinFunctionMap test_function_map = {
{"execute", {1, 1, {false, execute_command}}},
};
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
std::vector<ActionManagerCommand> commands{trigger_boot};
ActionManager action_manager;
ServiceList service_list;
TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
EXPECT_EQ(6, num_executed);
}
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);
}
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) > 202404) {
ASSERT_EQ(1u, parser.parse_error_count());
} else {
ASSERT_EQ(0u, parser.parse_error_count());
}
}
TEST(init, RejectsCriticalAndOneshotService) {
if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
GTEST_SKIP() << "Test only valid for devices launching with R or later";
}
std::string init_script =
R"init(
service A something
class first
user root
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());
}
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);
}
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);
}
}
pid_t ForkExecvpAsync(const char* argv[]) {
pid_t pid = fork();
if (pid == 0) {
// Child process.
CloseAllFds();
execvp(argv[0], const_cast<char**>(argv));
PLOG(ERROR) << "exec in ForkExecvpAsync init test";
_exit(EXIT_FAILURE);
}
// Parent process.
if (pid == -1) {
PLOG(ERROR) << "fork in ForkExecvpAsync init test";
return -1;
}
return pid;
}
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;
}
TEST(init, GentleKill) {
if (getuid() != 0) {
GTEST_SKIP() << "Must be run as root.";
return;
}
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);
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);
// Give strace the chance to connect
while (TracerPid(pid) == 0) {
std::this_thread::sleep_for(10ms);
}
service->Stop();
int status;
waitpid(strace_pid, &status, 0);
std::string logs;
android::base::ReadFdToString(logfile.fd, &logs);
ASSERT_NE(logs.find("killed by SIGTERM"), std::string::npos);
}
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
}
};
} // namespace init
} // namespace android
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);
testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
return RUN_ALL_TESTS();
}