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 "nativetoken_kit.h"
20 #include "res_sched_client.h"
21 #include "token_setproc.h"
22
23 const static int32_t PARAMETERS_NUM_MIN = 2;
24 const static int32_t PARAMETERS_NUM_MIN_KILL_PROCESS = 4;
25 const static int32_t PARAMETERS_NUM_KILL_PROCESS_PROCESSNAME = 5;
26
MockProcess(std::string processName)27 static void MockProcess(std::string processName)
28 {
29 static const char *perms[] = {
30 "ohos.permission.DISTRIBUTED_DATASYNC"
31 };
32 uint64_t tokenId;
33 NativeTokenInfoParams infoInstance = {
34 .dcapsNum = 0,
35 .permsNum = 1,
36 .aclsNum = 0,
37 .dcaps = nullptr,
38 .perms = perms,
39 .acls = nullptr,
40 .processName = processName.c_str(),
41 .aplStr = "system_core",
42 };
43 tokenId = GetAccessTokenId(&infoInstance);
44 SetSelfTokenID(tokenId);
45 }
46
KillProcess(int32_t argc,char * argv[])47 static void KillProcess(int32_t argc, char *argv[])
48 {
49 if (argc < PARAMETERS_NUM_MIN_KILL_PROCESS) {
50 return;
51 }
52 std::string caller = argv[PARAMETERS_NUM_MIN];
53 MockProcess(caller);
54 std::unordered_map<std::string, std::string> mapPayload;
55 mapPayload["pid"] = argv[PARAMETERS_NUM_MIN_KILL_PROCESS - 1];
56 if (argc >= PARAMETERS_NUM_KILL_PROCESS_PROCESSNAME) {
57 mapPayload["processName"] = argv[PARAMETERS_NUM_KILL_PROCESS_PROCESSNAME - 1];
58 }
59 int32_t res = OHOS::ResourceSchedule::ResSchedClient::GetInstance().KillProcess(mapPayload);
60 std::cout << "kill result:" << res << std::endl;
61 }
62
main(int32_t argc,char * argv[])63 int32_t main(int32_t argc, char *argv[])
64 {
65 if (!(argc >= PARAMETERS_NUM_MIN && argv)) {
66 std::cout << "error parameters";
67 return 0;
68 }
69 char* function = argv[1];
70 if (strcmp(function, "KillProcess") == 0) {
71 KillProcess(argc, argv);
72 } else {
73 std::cout << "error parameters";
74 }
75 return 0;
76 }