• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <signal.h>
33 
34 #define NEGATIVE_ONE (-1)
35 #define ZERO (0)
36 
WaitUntilSignal(int signum)37 void WaitUntilSignal(int signum)
38 {
39     sigset_t set;
40     sigemptyset(&set);
41     sigaddset(&set, signum);
42     // 设置超时结构,设置超时为 0.5 秒
43     struct timespec timeout;
44     timeout.tv_sec = 0;          // 秒部分为 0
45     timeout.tv_nsec = 500000000; // 纳秒部分为 500000000,即 0.5 秒
46     (void)sigtimedwait(&set, NULL, &timeout);
47 }
48 
49 // 检查是否有相应的日志生成,如果有当前日志,则返回true,如果没有则返回false
CheckHiLogPrint(char * needToMatch)50 bool CheckHiLogPrint(char *needToMatch)
51 {
52 #define COMMAND_SIZE (50)
53 #define READ_SIZE (49)
54 #define BUFFER_SIZE (1024)
55 #define FAIL_CLOSE (-1)
56     bool flag = false;
57     // 1. 通过system函数读取当前hilog日志信息
58     char command[] = "/bin/hilog -x | grep ";
59     char finalCommand[COMMAND_SIZE];
60     int res = snprintf_s(finalCommand, COMMAND_SIZE, READ_SIZE, "%s%s", command, needToMatch);
61     if (res == NEGATIVE_ONE) {
62         printf("CheckHiLogPrint command generate snprintf_s failed\n");
63         return false;
64     }
65     finalCommand[READ_SIZE] = '\0';
66     char buffer[BUFFER_SIZE];
67     FILE* pipe;
68     pipe = popen(finalCommand, "r");
69     if (pipe == NULL) {
70         printf("CheckHiLogPrint: Failed to run command\n");
71         return false;
72     }
73 
74     // 读取命令输出并打印
75     while (fgets(buffer, BUFFER_SIZE, pipe) != NULL) {
76         printf("%s", buffer);
77         flag = true;
78     }
79 
80     // 关闭管道并获取返回值
81     int returnValue = pclose(pipe);
82     if (returnValue == FAIL_CLOSE) {
83         printf("CheckHiLogPrint pclose failed returnValue=-1 errno=%d\n", errno);
84     }
85     return flag;
86 }
87 
88 #include <stdio.h>
89 #include <signal.h>
90 #include <unistd.h>
91 #include <stdlib.h>
92 
93 #define FREEZE_SIGNAL_35 (35)
94 
PthreadInternalLockInfoTest(void)95 void PthreadInternalLockInfoTest(void)
96 {
97     // 发送信号35和40到自己
98     int result = raise(FREEZE_SIGNAL_35);
99     if (result != ZERO) {
100         printf("raise 35 failed errno=%d\n", errno);
101         return;
102     }
103     WaitUntilSignal(FREEZE_SIGNAL_35);
104     bool hilogResult = CheckHiLogPrint("FREEZE_signo_35");
105     printf("hilogResult=%d\n", hilogResult);
106     hilogResult = CheckHiLogPrint("tl_lock_count");
107     printf("hilogResult=%d\n", hilogResult);
108     hilogResult = CheckHiLogPrint("tl_lock_waiters");
109     printf("hilogResult=%d\n", hilogResult);
110     hilogResult = CheckHiLogPrint("tl_lock_tid_fail");
111     printf("hilogResult=%d\n", hilogResult);
112     hilogResult = CheckHiLogPrint("tl_lock_count_tid");
113     printf("hilogResult=%d\n", hilogResult);
114 }
115 
main(void)116 int main(void)
117 {
118     // 解除限制
119     system("/bin/hilog -Q pidoff");
120     system("/bin/hilog -Q domainoff");
121     system("param set musl.log.level WARN");
122     PthreadInternalLockInfoTest();
123     // 恢复限制
124     system("/bin/hilog -Q pidon");
125     system("/bin/hilog -Q domainon");
126     return t_status;
127 }