• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <hilog_adapter.h>
17 #include <stdlib.h>
18 #include "functionalext.h"
19 #include "test.h"
20 #include <stdio.h>
21 #include <stdbool.h>
22 #include <time.h>
23 #include <errno.h>
24 #include <pthread.h>
25 #include <sys/un.h>
26 #include <bits/alltypes.h>
27 #include <sys/socket.h>
28 #include <linux/socket.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31 #include <securec.h>
32 
33 #define NEGATIVE_ONE (-1)
34 #define ZERO (0)
35 
36 // 检查是否有相应的日志生成,如果有当前日志,则返回true,如果没有则返回false
CheckHiLogPrint(char * needToMatch)37 bool CheckHiLogPrint(char *needToMatch)
38 {
39 #define COMMAND_SIZE (50)
40 #define READ_SIZE (49)
41 #define BUFFER_SIZE (1024)
42 #define FAIL_CLOSE (-1)
43     bool flag = false;
44     // 1. 通过system函数读取当前hilog日志信息
45     char command[] = "/bin/hilog -x | grep ";
46     char finalCommand[COMMAND_SIZE];
47     int res = snprintf_s(finalCommand, COMMAND_SIZE, READ_SIZE, "%s%s", command, needToMatch);
48     if (res == NEGATIVE_ONE) {
49         t_error("CheckHiLogPrint command generate snprintf_s failed\n");
50         return false;
51     }
52     finalCommand[READ_SIZE] = '\0';
53     char buffer[BUFFER_SIZE];
54     FILE* pipe;
55     pipe = popen(finalCommand, "r");
56     if (pipe == NULL) {
57         t_error("CheckHiLogPrint: Failed to run command\n");
58         return false;
59     }
60 
61     // 读取命令输出并打印
62     while (fgets(buffer, BUFFER_SIZE, pipe) != NULL) {
63         printf("%s", buffer);
64         flag = true;
65     }
66 
67     // 关闭管道并获取返回值
68     int returnValue = pclose(pipe);
69     if (returnValue == FAIL_CLOSE) {
70         t_error("CheckHiLogPrint pclose failed returnValue=-1 errno=%d\n", errno);
71     }
72     return flag;
73 }
74 
75 #include <stdio.h>
76 #include <signal.h>
77 #include <unistd.h>
78 #include <stdlib.h>
79 
80 // 不执行任何操作的信号处理函数
ignore_signal(int signum)81 void ignore_signal(int signum)
82 {
83     // 什么都不做
84 }
85 
86 #define FREEZE_SIGNAL_35 (35)
87 #define INVALID_SIGNAL_40 (40)
88 
PthreadInternalLockInfoTest(void)89 void PthreadInternalLockInfoTest(void)
90 {
91     // 发送信号35, 38和40到自己
92     sighandler_t sigResult = signal(FREEZE_SIGNAL_35, ignore_signal);
93     if (sigResult == SIG_ERR) {
94         t_error("signal 35 failed errno=%d", errno);
95         return;
96     }
97     int result = raise(FREEZE_SIGNAL_35);
98     if (result != ZERO) {
99         t_error("raise 35 failed errno=%d", errno);
100         return;
101     }
102     bool hilogResult = CheckHiLogPrint("FREEZE_signo_35");
103     EXPECT_EQ("signal35_check_output", hilogResult, true);
104     hilogResult = CheckHiLogPrint("tl_lock_count");
105     EXPECT_EQ("signal35_check_output", hilogResult, true);
106     hilogResult = CheckHiLogPrint("tl_lock_waiters");
107     EXPECT_EQ("signal35_check_output", hilogResult, true);
108     hilogResult = CheckHiLogPrint("tl_lock_tid_fail");
109     EXPECT_EQ("signal35_check_output", hilogResult, true);
110     hilogResult = CheckHiLogPrint("tl_lock_count_tid");
111     EXPECT_EQ("signal35_check_output", hilogResult, true);
112 
113     // 在发送信号40之前,先将它的处理函数设置为不做任何操作
114     sigResult = signal(INVALID_SIGNAL_40, ignore_signal);
115     if (sigResult == SIG_ERR) {
116         t_error("signal 40 failed errno=%d", errno);
117     }
118     result = raise(INVALID_SIGNAL_40);
119     if (result != ZERO) {
120         t_error("raise 40 failed errno=%d", errno);
121         return;
122     }
123     hilogResult = CheckHiLogPrint("FREEZE_signo_40");
124     EXPECT_EQ("signal38_check_output", hilogResult, false);
125 }
126 
main(void)127 int main(void)
128 {
129     // 解除限制
130     system("/bin/hilog -Q pidoff");
131     system("/bin/hilog -Q domainoff");
132     PthreadInternalLockInfoTest();
133     // 恢复限制
134     system("/bin/hilog -Q pidon");
135     system("/bin/hilog -Q domainon");
136     return t_status;
137 }