| 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 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 22 | namespace android { | 
|  | 23 | namespace init { | 
|  | 24 |  | 
|  | 25 | static void BenchmarkSuccess(benchmark::State& state) { | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 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 |  | 
| Tom Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 36 | auto subcontext = Subcontext({"path"}, context); | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 37 | free(context); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | while (state.KeepRunning()) { | 
| Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame] | 40 | subcontext.Execute(std::vector<std::string>{"return_success"}); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 41 | } | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | if (subcontext.pid() > 0) { | 
|  | 44 | kill(subcontext.pid(), SIGTERM); | 
|  | 45 | kill(subcontext.pid(), SIGKILL); | 
|  | 46 | } | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
|  | 49 | BENCHMARK(BenchmarkSuccess); | 
|  | 50 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 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 | }; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 56 | return test_function_map; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | }  // namespace init | 
|  | 60 | }  // namespace android | 
|  | 61 |  | 
|  | 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 | } |