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