1 /*
2 * Copyright (C) 2017 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 "subcontext.h"
18
19 #include <benchmark/benchmark.h>
20 #include <selinux/selinux.h>
21
22 namespace android {
23 namespace init {
24
BenchmarkSuccess(benchmark::State & state)25 static void BenchmarkSuccess(benchmark::State& state) {
26 if (getuid() != 0) {
27 state.SkipWithError("Skipping benchmark, must be run as root.");
28 return;
29 }
30 char* context;
31 if (getcon(&context) != 0) {
32 state.SkipWithError("getcon() failed");
33 return;
34 }
35
36 auto subcontext = Subcontext({"path"}, context);
37 free(context);
38
39 while (state.KeepRunning()) {
40 subcontext.Execute(std::vector<std::string>{"return_success"});
41 }
42
43 if (subcontext.pid() > 0) {
44 kill(subcontext.pid(), SIGTERM);
45 kill(subcontext.pid(), SIGKILL);
46 }
47 }
48
49 BENCHMARK(BenchmarkSuccess);
50
BuildTestFunctionMap()51 BuiltinFunctionMap BuildTestFunctionMap() {
52 auto function = [](const BuiltinArguments& args) { return Result<void>{}; };
53 BuiltinFunctionMap test_function_map = {
54 {"return_success", {0, 0, {true, function}}},
55 };
56 return test_function_map;
57 }
58
59 } // namespace init
60 } // namespace android
61
main(int argc,char ** argv)62 int main(int argc, char** argv) {
63 if (argc > 1 && !strcmp(basename(argv[1]), "subcontext")) {
64 auto test_function_map = android::init::BuildTestFunctionMap();
65 return android::init::SubcontextMain(argc, argv, &test_function_map);
66 }
67
68 ::benchmark::Initialize(&argc, argv);
69 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
70 ::benchmark::RunSpecifiedBenchmarks();
71 }
72