已开启
使能功能:1、编译时可以指定agentd进程的日志落盘路径,2、CA实现LogPrint函数可以重新定义libteec.so的日志输出方式 #50
使能功能:1、编译时可以指定agentd进程的日志落盘路径,2、CA实现LogPrint函数可以重新定义libteec.so的日志输出方式 #50
已开启
smartwangwang_h创建于 5月17日
5 个文件变更+105-8
MMakefile+65-2
@@ -63,6 +63,10 @@ LIB_SOURCES := src/libteec_vendor/tee_client_api.c \
63 src/libteec_vendor/tee_client_socket.c \63 src/libteec_vendor/tee_client_socket.c \
64 src/libteec_vendor/tee_load_sec_file.c \64 src/libteec_vendor/tee_load_sec_file.c \
65 src/libteec_vendor/tee_session_pool.c65 src/libteec_vendor/tee_session_pool.c
66+ifeq ($(CONFIG_CUSTOM_LIBTEEC_LOGGING), true)
67+LIB_CFLAGS += -DCONFIG_CUSTOM_LIBTEEC_LOGGING
68+LIB_SOURCES += src/common/tee_custom_log.c
69+endif
66 70 
67LIB_OBJECTS := $(LIB_SOURCES:.c=.o)71LIB_OBJECTS := $(LIB_SOURCES:.c=.o)
68 72 
@@ -136,7 +140,7 @@ AGENTD_SOURCES := src/agentd/agentd.c \
136 140 
137AGENTD_CFLAGS := $(COMMON_CFLAGS) -DSEC_STORAGE_DATA_KUNPENG_PATH -D_GNU_SOURCE -DCONFIG_KUNPENG_PLATFORM -DCONFIG_AUTH_USERNAME141AGENTD_CFLAGS := $(COMMON_CFLAGS) -DSEC_STORAGE_DATA_KUNPENG_PATH -D_GNU_SOURCE -DCONFIG_KUNPENG_PLATFORM -DCONFIG_AUTH_USERNAME
138AGENTD_CFLAGS += -DCONFIG_AGENT_FS -DCONFIG_AGENT_SECLOAD -DCONFIG_AGENT_MISC -DCONFIG_AGENTD142AGENTD_CFLAGS += -DCONFIG_AGENT_FS -DCONFIG_AGENT_SECLOAD -DCONFIG_AGENT_MISC -DCONFIG_AGENTD
139-AGENTD_CFLAGS += -DDYNAMIC_TA_PATH=\"/var/itrustee/ta/\" -DCONFIG_CUSTOM_LOGGING=\"/var/log/agentd.log\"143+AGENTD_CFLAGS += -DDYNAMIC_TA_PATH=\"/var/itrustee/ta/\" -DCUSTOM_LOG_FILE_SIZE_LIMIT=524288
140AGENTD_CFLAGS += -Iinclude -Iinclude/cloud -Iext_include -Ilibboundscheck/include -Iinclude -Isrc/inc -Isrc/teecd/144AGENTD_CFLAGS += -Iinclude -Iinclude/cloud -Iext_include -Ilibboundscheck/include -Iinclude -Isrc/inc -Isrc/teecd/
141AGENTD_CFLAGS += -Isrc/authentication/ -Isrc/libteec_vendor/ -Isrc/common145AGENTD_CFLAGS += -Isrc/authentication/ -Isrc/libteec_vendor/ -Isrc/common
142AGENTD_CFLAGS += -Werror -Wall -Wextra -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie -D_FORTIFY_SOURCE=2 -O2146AGENTD_CFLAGS += -Werror -Wall -Wextra -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie -D_FORTIFY_SOURCE=2 -O2
@@ -145,10 +149,69 @@ ifneq ($(strip $(CFG_ENG)), user)
145AGENTD_CFLAGS += -DDEF_ENG149AGENTD_CFLAGS += -DDEF_ENG
146endif150endif
147 151 
152+ifeq ($(CUSTOM_AGENTD_LOGGING),)
153+AGENTD_CFLAGS += -DCONFIG_CUSTOM_LOGGING=\"/var/log/agentd.log\"
154+else
155+AGENTD_CFLAGS += -DCONFIG_CUSTOM_LOGGING=\"$(CUSTOM_AGENTD_LOGGING)\"
156+ifeq ($(filter /%,$(CUSTOM_AGENTD_LOGGING)),)
157+$(error CUSTOM_AGENTD_LOGGING [$(CUSTOM_AGENTD_LOGGING)] must a Linux absolute path )
158+endif
159+endif
160+ 
161+ifeq ($(CUSTOM_AGENTD_LOGGING_BAK),)
162+AGENTD_CFLAGS += -DCONFIG_CUSTOM_LOGGING_BAK=\"/var/log/agentd_bak.log\"
163+else
164+AGENTD_CFLAGS += -DCONFIG_CUSTOM_LOGGING_BAK=\"$(CUSTOM_AGENTD_LOGGING_BAK)\"
165+ifeq ($(filter /%,$(CUSTOM_AGENTD_LOGGING_BAK)),)
166+$(error CUSTOM_AGENTD_LOGGING_BAK [$(CUSTOM_AGENTD_LOGGING_BAK)] must a Linux absolute path )
167+endif
168+endif
169+ 
148AGENTD_OBJECTS := $(AGENTD_SOURCES:.c=.o)170AGENTD_OBJECTS := $(AGENTD_SOURCES:.c=.o)
149 171 
150-$(TARGET_AGENTD): $(AGENTD_SOURCES)172+AGENTD_LOG_DIR := $(dir $(CUSTOM_AGENTD_LOGGING))
173+AGENTD_BAK_LOG_DIR := $(dir $(CONFIG_CUSTOM_LOGGING))
174+ 
175+.PHONY: ensure-agentd-log-file
176+ensure-agentd-log-file:
177+ifeq ($(CUSTOM_AGENTD_LOGGING),)
178+ @echo "CONFIG_CUSTOM_LOGGING is not defined, skipping log file creation."
179+else
180+ @if [ -n "$(AGENTD_LOG_DIR)" ] && [ "$(AGENTD_LOG_DIR)" != "./" ]; then \
181+ echo "Ensuring log directory exists: $(AGENTD_LOG_DIR)"; \
182+ mkdir -p "$(AGENTD_LOG_DIR)"; \
183+ fi
184+ @if [ ! -f "$(CUSTOM_AGENTD_LOGGING)" ]; then \
185+ echo "Creating Agentd log file: $(CUSTOM_AGENTD_LOGGING)"; \
186+ touch "$(CUSTOM_AGENTD_LOGGING)"; \
187+ chmod 640 "$(CUSTOM_AGENTD_LOGGING)"; \
188+ else \
189+ echo "Agentd Log file already exists: $(CUSTOM_AGENTD_LOGGING)"; \
190+ fi
191+endif
192+ 
193+.PHONY: ensure-agentd-bak-log-file
194+ensure-agentd-bak-log-file:
195+ifeq ($(CUSTOM_AGENTD_LOGGING_BAK),)
196+ @echo "CONFIG_CUSTOM_BAK_LOGGING is not defined, skipping log file creation."
197+else
198+ @if [ -n "$(AGENTD_BAK_LOG_DIR)" ] && [ "$(AGENTD_BAK_LOG_DIR)" != "./" ]; then \
199+ echo "Ensuring bak log directory exists: $(AGENTD_BAK_LOG_DIR)"; \
200+ mkdir -p "$(AGENTD_BAK_LOG_DIR)"; \
201+ fi
202+ @if [ ! -f "$(CUSTOM_AGENTD_LOGGING_BAK)" ]; then \
203+ echo "Creating Agentd bak log file: $(CUSTOM_AGENTD_LOGGING_BAK)"; \
204+ touch "$(CUSTOM_AGENTD_LOGGING_BAK)"; \
205+ chmod 640 "$(CUSTOM_AGENTD_LOGGING_BAK)"; \
206+ else \
207+ echo "Agentd bak Log file already exists: $(CUSTOM_AGENTD_LOGGING_BAK)"; \
208+ fi
209+endif
210+ 
211+$(TARGET_AGENTD): $(AGENTD_SOURCES) ensure-agentd-log-file ensure-agentd-bak-log-file
151 @echo "compile agentd"212 @echo "compile agentd"
213+ @echo "CUSTOM_AGENTD_LOGGING is $(CUSTOM_AGENTD_LOGGING)"
214+ @echo "CUSTOM_AGENTD_LOGGING_BAK is $(CUSTOM_AGENTD_LOGGING_BAK)"
152 @$(CC) $(AGENTD_CFLAGS) -o $@ $(AGENTD_SOURCES) $(AGENTD_LDFLAGS)215 @$(CC) $(AGENTD_CFLAGS) -o $@ $(AGENTD_SOURCES) $(AGENTD_LDFLAGS)
153 @mkdir -p $(TARGET_DIR)216 @mkdir -p $(TARGET_DIR)
154 @mv agentd $(TARGET_DIR)217 @mv agentd $(TARGET_DIR)
MREADME.en.md+6-2
@@ -35,8 +35,12 @@ make tee_teleport
35at the same time, we can get a library libboundscheck.so.35at the same time, we can get a library libboundscheck.so.
36All the generated files are stored in the newly created directory dist.36All the generated files are stored in the newly created directory dist.
37 37 
38-When building with make, you can specify the log storage path for teeos and ta by passing the TEE_LOG_PATH_BASE parameter.38+Users can specify the output logs for the teeos/ta/agentd process/libteec.so at compile time, as follows:
39-Please ensure the target directory already exists before compilation. This method also applies when building tlogcat alone.39+The `TEE_LOG_PATH_BASE` parameter can specify the log storage path for TEE OS and TAs, provided that the path exists. This method also applies to single compilation tlogcat scenarios.
40+b. The CUSTOM_AGENTD_LOGGING option can specify the file where the agentd process logs are saved. If the file (including the directory) does not exist, it will be created during compilation.
41+c. Set CONFIG_CUSTOM_LIBTEEC_LOGGING=true, and implement the LogPrint function (input/output references tee_custom_log.c) for CA. This allows libteec.so to output logs using the custom LogPrint function.
42+Compilation method example:
43+TEE_LOG_PATH_BASE=/test/log CUSTOM_AGENTD_LOGGING=/test/agentd/agentd.log CONFIG_CUSTOM_LIBTEEC_LOGGING=true make
40 44 
41#### How to use45#### How to use
42Run teecd or tlogcat46Run teecd or tlogcat
MREADME.md+7-2
@@ -29,8 +29,7 @@ itrustee_client
294)cd xxx(client 源码路径) 。294)cd xxx(client 源码路径) 。
30 30 
315)make 编译出可执行文件teecd,tlogcat,动态库libteec.so,tee_teleport,agentd,存放在新创建的dist目录下。315)make 编译出可执行文件teecd,tlogcat,动态库libteec.so,tee_teleport,agentd,存放在新创建的dist目录下。
32-注意:make编译时,带上TEE_LOG_PATH_BASE参数可以指定teeos和ta的日志存储路径,前提需要确保该路径存在,该方法同样适用于单编tlogcat的情况。32+ 
33-如:TEE_LOG_PATH_BASE=/test/log make,编译出来的tlogcat二进制会将teeos和ta日志落盘在/test/log/tee目录。
346)只编译某一个组件,运行命令:336)只编译某一个组件,运行命令:
35 34 
36```35```
@@ -43,6 +42,12 @@ make tee_teleport
43 42 
447)编译的同时,会生成一个动态库 libboundscheck.so, 所有新生成的文件,都存放在新创建的dist目录下。437)编译的同时,会生成一个动态库 libboundscheck.so, 所有新生成的文件,都存放在新创建的dist目录下。
45 44 
45+8) 用户可以在编译时指定teeos/ta/agentd进程/libteec.so的输出日志,方法如下:
46+a、TEE_LOG_PATH_BASE参数可以指定teeos和ta的日志存储路径,前提需要确保该路径存在,该方法同样适用于单编tlogcat的情况。
47+b、CUSTOM_AGENTD_LOGGING可以指定agentd进程日志保存的文件,如果该文件(包含目录)不存在,编译时会创建该文件(包含目录)。
48+c、CONFIG_CUSTOM_LIBTEEC_LOGGING=true,CA需要自己实现LogPrint函数(输入输出参考tee_custom_log.c),可以让libteec.so按照自实现的LogPrint函数输出日志。
49+编译方法示例:TEE_LOG_PATH_BASE=/test/log CUSTOM_AGENTD_LOGGING=/test/agentd/agentd.log CONFIG_CUSTOM_LIBTEEC_LOGGING=true make
50+ 
46#### 使用说明51#### 使用说明
471)确认tzdriver.ko已正常拉起,确认方法如下: 执行lsmod命令,并搜索关键字tzdriver,如果有显示,则已经正常拉起,示例如下: 521)确认tzdriver.ko已正常拉起,确认方法如下: 执行lsmod命令,并搜索关键字tzdriver,如果有显示,则已经正常拉起,示例如下:
48 53 
Msrc/common/tee_custom_log.c+26-1
@@ -12,6 +12,9 @@
12 12 
13#include <stdio.h>13#include <stdio.h>
14#include <stdint.h>14#include <stdint.h>
15+#include <unistd.h>
16+#include <sys/types.h>
17+#include <sys/stat.h>
15 18 
16#ifdef CONFIG_CUSTOM_LOGGING19#ifdef CONFIG_CUSTOM_LOGGING
17#include <time.h>20#include <time.h>
@@ -57,6 +60,26 @@ static int GetLogTimeInfo(char *logTimeInfo, size_t bufLen)
57 return 0;60 return 0;
58}61}
59 62 
63+static int checkFileLimitAndBackup(void)
64+{
65+ struct stat buf = {0};
66+ int ret = stat(CONFIG_CUSTOM_LOGGING, &buf);
67+ if (ret < 0) {
68+ fprintf(stderr, "stat file failed, errno=%d\n", errno);
69+ return ret;
70+ }
71+ 
72+ if (buf.st_size >= CUSTOM_LOG_FILE_SIZE_LIMIT) {
73+ ret = rename(CONFIG_CUSTOM_LOGGING, CONFIG_CUSTOM_LOGGING_BAK);
74+ if (ret < 0) {
75+ fprintf(stderr, "rename file failed, errno=%d\n", errno);
76+ return ret;
77+ }
78+ }
79+ 
80+ return 0;
81+}
82+ 
60void LogPrint(uint8_t logLevel, const char *fmt, ...)83void LogPrint(uint8_t logLevel, const char *fmt, ...)
61{84{
62 (void)logLevel;85 (void)logLevel;
@@ -98,11 +121,13 @@ void LogPrint(uint8_t logLevel, const char *fmt, ...)
98 return;121 return;
99 }122 }
100 123 
124+ (void)chmod(CONFIG_CUSTOM_LOGGING, S_IRUSR | S_IWUSR | S_IRGRP);
125+ (void)checkFileLimitAndBackup();
101 (void)fclose(fp);126 (void)fclose(fp);
102}127}
103 128 
104#else129#else
105-void LogPrint(uint8_t logLevel, const char *fmt, ...)130+ __attribute__((weak)) void LogPrint(uint8_t logLevel, const char *fmt, ...)
106{131{
107 (void)logLevel;132 (void)logLevel;
108 (void)fmt;133 (void)fmt;
Msrc/inc/tee_log.h+1-1
@@ -13,7 +13,7 @@
13#ifndef TEE_LOG_H13#ifndef TEE_LOG_H
14#define TEE_LOG_H14#define TEE_LOG_H
15 15 
16-#if (defined CONFIG_CUSTOM_LOGGING)16+#if (defined CONFIG_CUSTOM_LOGGING || defined CONFIG_CUSTOM_LIBTEEC_LOGGING)
17#include "tee_custom_log.h"17#include "tee_custom_log.h"
18#else18#else
19#include "tee_sys_log.h"19#include "tee_sys_log.h"