| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 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> | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 20 | #include <selinux/selinux.h> | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 21 |  | 
|  | 22 | #include "test_function_map.h" | 
|  | 23 |  | 
|  | 24 | namespace android { | 
|  | 25 | namespace init { | 
|  | 26 |  | 
|  | 27 | static void BenchmarkSuccess(benchmark::State& state) { | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 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); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | while (state.KeepRunning()) { | 
| Tom Cherry | d987264 | 2018-10-11 10:38:05 -0700 | [diff] [blame] | 42 | subcontext.Execute(std::vector<std::string>{"return_success"}).IgnoreError(); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 43 | } | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | if (subcontext.pid() > 0) { | 
|  | 46 | kill(subcontext.pid(), SIGTERM); | 
|  | 47 | kill(subcontext.pid(), SIGKILL); | 
|  | 48 | } | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
|  | 51 | BENCHMARK(BenchmarkSuccess); | 
|  | 52 |  | 
|  | 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 |  | 
|  | 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 | } |