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 <fcntl.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21 #include "filepath_util.h"
22
23 /**
24 * @tc.name : getpid_0100
25 * @tc.desc : Verify to get the current process ID (shell command and call getpid function).
26 * @tc.level : Level 0
27 */
getpid_0100(void)28 void getpid_0100(void)
29 {
30 char ptr[PATH_MAX] = {0};
31 FILE_ABSOLUTE_PATH("ps.txt", ptr);
32 char cmd[PATH_MAX] = {0};
33 snprintf(cmd, sizeof(cmd), "ps -eo command,pid | grep -E \"PID|getpid\" > %s", ptr);
34 system(cmd);
35 char abc[256] = {0};
36 bool successflag = false;
37 FILE *fptr = fopen(ptr, "r");
38 if (fptr) {
39 while (!feof(fptr)) {
40 fread(abc, sizeof(abc), 1, fptr);
41 char num[8] = {0};
42 int index = 0;
43 for (int i = 0; i < (int)strlen(abc); i++) {
44 if (abc[i] >= '0' && abc[i] <= '9') {
45 num[index++] += abc[i];
46 }
47 }
48 int32_t intuid = atoi(num);
49 pid_t uid = getpid();
50 if (intuid == uid) {
51 successflag = true;
52 }
53 }
54 }
55 EXPECT_TRUE("getpid_0100", successflag);
56 fclose(fptr);
57 remove(ptr);
58 }
59
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 getpid_0100();
63 return t_status;
64 }