1 /*
2 * Copyright (c) 2023 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 #include <cstdint>
16 #include <cstring>
17 #include <iostream>
18 #include <unordered_map>
19 #include <unistd.h>
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "nativetoken_kit.h"
23 #include "res_sched_client.h"
24 #include "token_setproc.h"
25
26 const static int32_t PARAMETERS_NUM_MIN = 2;
27 const static int32_t PARAMETERS_NUM_MIN_KILL_PROCESS = 4;
28 const static int32_t PARAMETERS_NUM_KILL_PROCESS_PROCESSNAME = 5;
29 const static int32_t PARAMETERS_NUM_REPORT_DATA = 6;
30 const static int32_t PARAMETERS_NUM_REQUEST_TEST = 5;
31 const static int32_t PID_INDEX = 3;
32 const static int32_t RES_TYPE_INDEX = 4;
33 const static int32_t UID_INDEX = 2;
34 const static int32_t VALUE_INDEX = 5;
35 const static int32_t REQUEST_COUNT_INDEX = 2;
36 const static int32_t REQUEST_TIME_INDEX = 3;
37 const static int32_t REQUEST_UID_INDEX = 4;
38 const static int32_t DEFAULT_TYPE = 1;
39 const static int32_t DEFAULT_VALUE = 1;
40
MockProcess(int32_t uid)41 static void MockProcess(int32_t uid)
42 {
43 static const char *perms[] = {
44 "ohos.permission.DISTRIBUTED_DATASYNC",
45 "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT"
46 };
47 uint64_t tokenId;
48 NativeTokenInfoParams infoInstance = {
49 .dcapsNum = 0,
50 .permsNum = sizeof(perms) / sizeof(perms[0]),
51 .aclsNum = 0,
52 .dcaps = nullptr,
53 .perms = perms,
54 .acls = nullptr,
55 .processName = "samgr",
56 .aplStr = "system_core",
57 };
58 tokenId = GetAccessTokenId(&infoInstance);
59 SetSelfTokenID(tokenId);
60 setuid(uid);
61 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
62 }
63
GetNowTime()64 static int64_t GetNowTime()
65 {
66 time_t now;
67 (void)time(&now);
68 if (static_cast<int64_t>(now) < 0) {
69 std::cout << "Get now time error " << std::endl;
70 return 0;
71 }
72 auto tarEndTimePoint = std::chrono::system_clock::from_time_t(now);
73 auto tarDuration = std::chrono::duration_cast<std::chrono::microseconds>(tarEndTimePoint.time_since_epoch());
74 int64_t tarDate = tarDuration.count();
75 if (tarDate < 0) {
76 std::cout << "tarDuration is less than 0 " << std::endl;
77 return -1;
78 }
79 return static_cast<int64_t>(tarDate);
80 }
81
KillProcess(int32_t argc,char * argv[])82 static void KillProcess(int32_t argc, char *argv[])
83 {
84 if (argc < PARAMETERS_NUM_MIN_KILL_PROCESS) {
85 return;
86 }
87 int32_t uid = atoi(argv[PARAMETERS_NUM_MIN]);
88 MockProcess(uid);
89 std::unordered_map<std::string, std::string> mapPayload;
90 mapPayload["pid"] = argv[PARAMETERS_NUM_MIN_KILL_PROCESS - 1];
91 if (argc >= PARAMETERS_NUM_KILL_PROCESS_PROCESSNAME) {
92 mapPayload["processName"] = argv[PARAMETERS_NUM_KILL_PROCESS_PROCESSNAME - 1];
93 }
94 int32_t res = OHOS::ResourceSchedule::ResSchedClient::GetInstance().KillProcess(mapPayload);
95 std::cout << "kill result:" << res << std::endl;
96 }
97
ReportData(int32_t argc,char * argv[])98 static void ReportData(int32_t argc, char *argv[])
99 {
100 if (argc != PARAMETERS_NUM_REPORT_DATA) {
101 return;
102 }
103 int32_t uid = atoi(argv[UID_INDEX]);
104 MockProcess(uid);
105 int32_t pid = atoi(argv[PID_INDEX]);
106 int32_t resType = atoi(argv[RES_TYPE_INDEX]);
107 int32_t value = atoi(argv[VALUE_INDEX]);
108 std::unordered_map<std::string, std::string> mapPayload;
109 mapPayload["uid"] = uid;
110 mapPayload["pid"] = pid;
111 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, value, mapPayload);
112 std::cout << "success passing on pid = " << pid << std::endl;
113 }
114
RequestTest(int32_t argc,char * argv[])115 static void RequestTest(int32_t argc, char *argv[])
116 {
117 if (argc != PARAMETERS_NUM_REQUEST_TEST) {
118 return;
119 }
120 int32_t requestCount = atoi(argv[REQUEST_COUNT_INDEX]);
121 int32_t requestTime = atoi(argv[REQUEST_TIME_INDEX]);
122 int32_t uid = atoi(argv[REQUEST_UID_INDEX]);
123 MockProcess(uid);
124 std::unordered_map<std::string, std::string> mapPayload;
125 int64_t startTime = GetNowTime();
126 int32_t count = 0;
127 while (count < requestCount && GetNowTime() - startTime < requestTime) {
128 count++;
129 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(DEFAULT_TYPE, DEFAULT_VALUE, mapPayload);
130 }
131 std::cout << "success RequestTest " << uid << std::endl;
132 }
133
main(int32_t argc,char * argv[])134 int32_t main(int32_t argc, char *argv[])
135 {
136 if (!(argc >= PARAMETERS_NUM_MIN && argv)) {
137 std::cout << "error parameters";
138 return 0;
139 }
140 char* function = argv[1];
141 if (strcmp(function, "KillProcess") == 0) {
142 KillProcess(argc, argv);
143 } else if (strcmp(function, "ReportData") == 0) {
144 ReportData(argc, argv);
145 } else if (strcmp(function, "RequestTest") == 0) {
146 RequestTest(argc, argv);
147 } else {
148 std::cout << "error parameters";
149 }
150 return 0;
151 }