1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <getopt.h>
17 #include <iostream>
18 #include <unistd.h>
19 #include "service_checker.h"
20 #include "selinux_error.h"
21
22 using namespace Selinux;
23
24 static std::unique_ptr<ServiceChecker> g_service = nullptr;
25
26 struct testInput {
27 char cmd = '\0';
28 bool isHdf = false;
29 };
30
PrintUsage()31 static void PrintUsage()
32 {
33 std::cout << "Usage:" << std::endl;
34 std::cout << "step 1:" << std::endl;
35 std::cout << "service_check (-d) -a|-g|-r|-l" << std::endl;
36 std::cout << "step 2:" << std::endl;
37 std::cout << "input service name and press 'enter' to continue, or ctrl+C to end process" << std::endl;
38 std::cout << "" << std::endl;
39 std::cout << "Options:" << std::endl;
40 std::cout << " -h (--help) show the help information. [eg: service_check -h]" << std::endl;
41 std::cout << "***********************optinal*************************************************" << std::endl;
42 std::cout << " -d (--isHdf) service or hdf_service. [eg: service_check -d]" << std::endl;
43 std::cout << "***********************requered: 1 in 4****************************************" << std::endl;
44 std::cout << " -a (--add) add service check. [eg: service_check -a]" << std::endl;
45 std::cout << " -g (--get) get service check. [eg: service_check -g]" << std::endl;
46 std::cout << " -r (--get_remote) get remote service check. [eg: service_check -r]" << std::endl;
47 std::cout << " -l (--list) list service check. [eg: service_check -l]" << std::endl;
48 std::cout << "" << std::endl;
49 }
50
SetOptions(int argc,char * argv[],const option * options,testInput & input)51 static void SetOptions(int argc, char *argv[], const option *options, testInput &input)
52 {
53 int index = 0;
54 const char *optStr = "dhlagr";
55 int para = 0;
56 while ((para = getopt_long(argc, argv, optStr, options, &index)) != -1) {
57 switch (para) {
58 case 'h': {
59 PrintUsage();
60 exit(0);
61 }
62 case 'd': {
63 input.isHdf = true;
64 break;
65 }
66 case 'a': {
67 input.cmd = 'a';
68 break;
69 }
70 case 'g': {
71 input.cmd = 'g';
72 break;
73 }
74 case 'r': {
75 input.cmd = 'r';
76 break;
77 }
78 case 'l': {
79 input.cmd = 'l';
80 break;
81 }
82 default:
83 std::cout << "Try 'service_check -h' for more information." << std::endl;
84 exit(-1);
85 }
86 }
87 }
88
main(int argc,char * argv[])89 int main(int argc, char *argv[])
90 {
91 struct option options[] = {
92 {"help", no_argument, nullptr, 'h'}, {"add", no_argument, nullptr, 'a'},
93 {"get", no_argument, nullptr, 'g'}, {"get_remote", no_argument, nullptr, 'r'},
94 {"isHdf", no_argument, nullptr, 'd'}, {"list", no_argument, nullptr, 'l'},
95 {nullptr, no_argument, nullptr, 0},
96 };
97
98 if (argc == 1) {
99 PrintUsage();
100 exit(0);
101 }
102
103 testInput input;
104 SetOptions(argc, argv, options, input);
105 if (input.isHdf) {
106 g_service = std::make_unique<ServiceChecker>(true);
107 } else {
108 g_service = std::make_unique<ServiceChecker>(false);
109 }
110 std::string serName;
111 switch (input.cmd) {
112 case 'a': {
113 while (std::cin >> serName) {
114 std::cout << GetErrStr(g_service->AddServiceCheck(getpid(), serName)) << std::endl;
115 }
116 exit(0);
117 }
118 case 'g': {
119 while (std::cin >> serName) {
120 std::cout << GetErrStr(g_service->GetServiceCheck(getpid(), serName)) << std::endl;
121 }
122 exit(0);
123 }
124 case 'r': {
125 while (std::cin >> serName) {
126 std::cout << GetErrStr(g_service->GetRemoteServiceCheck(getpid(), serName)) << std::endl;
127 }
128 exit(0);
129 }
130 case 'l': {
131 std::cout << GetErrStr(g_service->ListServiceCheck(getpid())) << std::endl;
132 exit(0);
133 }
134 default:
135 exit(-1);
136 }
137
138 exit(0);
139 }
140