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