blob: ccef2f36ac3c43180dcee80f0628efbb7e59d652 [file] [log] [blame]
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001/*
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 Cherrye6d37cd2017-10-19 14:15:37 -070020#include <selinux/selinux.h>
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070021
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070022namespace android {
23namespace init {
24
25static void BenchmarkSuccess(benchmark::State& state) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070026 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 Cherry14c24722019-09-18 13:47:19 -070036 auto subcontext = Subcontext({"path"}, context);
Tom Cherrye6d37cd2017-10-19 14:15:37 -070037 free(context);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070038
39 while (state.KeepRunning()) {
Tom Cherry9949ec52019-05-16 16:54:49 -070040 subcontext.Execute(std::vector<std::string>{"return_success"});
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070041 }
Tom Cherrye6d37cd2017-10-19 14:15:37 -070042
43 if (subcontext.pid() > 0) {
44 kill(subcontext.pid(), SIGTERM);
45 kill(subcontext.pid(), SIGKILL);
46 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070047}
48
49BENCHMARK(BenchmarkSuccess);
50
Tom Cherryd52a5b32019-07-22 16:05:36 -070051BuiltinFunctionMap BuildTestFunctionMap() {
52 auto function = [](const BuiltinArguments& args) { return Result<void>{}; };
53 BuiltinFunctionMap test_function_map = {
54 {"return_success", {0, 0, {true, function}}},
55 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070056 return test_function_map;
57}
58
59} // namespace init
60} // namespace android
61
62int 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}