diff --git a/src/client.c b/src/client.c index 19d3f7d..1d39e99 100644 --- a/src/client.c +++ b/src/client.c @@ -19,14 +19,12 @@ #define BUFFER_SIZE 4096 // 全局变量,用于信号处理器和主线程通信 +static volatile sig_atomic_t g_window_size_changed = 0; static volatile sig_atomic_t g_should_exit = 0; static int g_socket_fd = -1; static pthread_mutex_t g_socket_mutex = PTHREAD_MUTEX_INITIALIZER; static volatile sig_atomic_t g_terminal_modified = 0; // 标记终端是否被修改 -// 用于窗口大小变化通知的管道 -static int g_winch_pipe[2] = {-1, -1}; - // 恢复终端状态的清理函数 static void cleanup_terminal(void) { // 总是尝试恢复终端,即使标志未设置 @@ -41,11 +39,7 @@ static void cleanup_terminal(void) { // SIGWINCH信号处理器 static void handle_sigwinch(int sig) { (void)sig; - if (g_winch_pipe[1] != -1) { - char dummy = 1; - ssize_t result = write(g_winch_pipe[1], &dummy, 1); - (void)result; - } + g_window_size_changed = 1; } // SIGINT/SIGTERM信号处理器 @@ -57,14 +51,12 @@ static void handle_exit_signal(int sig) { // 获取并发送终端信息 static int send_terminal_info(int sock, MessageType msg_type) { - DEBUG_LOG("开始发送终端信息: msg_type=%d", msg_type); TerminalInfoFixed term_info_fixed; memset(&term_info_fixed, 0, sizeof(term_info_fixed)); // 检查是否为TTY int is_tty = isatty(STDIN_FILENO); term_info_fixed.is_tty = is_tty; - DEBUG_LOG("is_tty=%d", is_tty); // 获取窗口大小 if (is_tty) { @@ -74,7 +66,6 @@ static int send_terminal_info(int sock, MessageType msg_type) { term_info_fixed.cols = ws.ws_col; term_info_fixed.x_pixel = ws.ws_xpixel; term_info_fixed.y_pixel = ws.ws_ypixel; - DEBUG_LOG("终端大小: %dx%d", ws.ws_col, ws.ws_row); } // 获取termios属性 @@ -126,11 +117,6 @@ static int send_terminal_info(int sock, MessageType msg_type) { int result = write_message(sock, msg_type, payload, payload_len); free(payload); - if (result == 0) { - DEBUG_LOG("终端信息发送成功"); - } else { - DEBUG_LOG("终端信息发送失败"); - } return result; } @@ -200,16 +186,11 @@ static void* response_listener_thread(void* arg) { int result = read_message(sock, &msg_type, &payload, &payload_len); if (result <= 0) { - DEBUG_LOG("read_message 返回 %d,退出循环", result); break; // 连接关闭或错误 } - DEBUG_LOG("收到消息: type=%d, payload_len=%u", msg_type, payload_len); - if (msg_type == MSG_TYPE_SERVER_RESPONSE && payload != NULL) { - DEBUG_LOG("写入响应到 fd=%d, 长度=%u", *output_fd, payload_len); ssize_t written = write(*output_fd, payload, payload_len); - DEBUG_LOG("实际写入: %zd 字节", written); (void)written; if (*output_fd == STDOUT_FILENO) { fflush(stdout); @@ -222,7 +203,6 @@ static void* response_listener_thread(void* arg) { free_message_payload(payload); } - DEBUG_LOG("响应监听线程退出"); return NULL; } @@ -230,41 +210,31 @@ static void* response_listener_thread(void* arg) { static void* window_monitor_thread(void* arg) { (void)arg; - DEBUG_LOG("窗口监听线程启动"); - - struct pollfd pfd; - pfd.fd = g_winch_pipe[0]; - pfd.events = POLLIN; - while (!g_should_exit) { - int ret = poll(&pfd, 1, 500); - if (ret <= 0) { - continue; - } - - // 清空管道 - char buf[64]; - read(g_winch_pipe[0], buf, sizeof(buf)); - - DEBUG_LOG("检测到窗口大小变化"); + // 等待窗口大小变化信号 + if (g_window_size_changed) { + g_window_size_changed = 0; - pthread_mutex_lock(&g_socket_mutex); - int sock = g_socket_fd; - pthread_mutex_unlock(&g_socket_mutex); + pthread_mutex_lock(&g_socket_mutex); + int sock = g_socket_fd; + pthread_mutex_unlock(&g_socket_mutex); - if (sock < 0) { - break; + if (sock < 0) { + break; + } + + // 发送窗口大小更新消息 + if (send_terminal_info(sock, MSG_TYPE_WINDOW_SIZE_UPDATE) < 0) { + DEBUG_LOG("Failed to send window size update\n"); + break; + } + + DEBUG_LOG("Window size updated sent to server\n"); } - if (send_terminal_info(sock, MSG_TYPE_WINDOW_SIZE_UPDATE) < 0) { - DEBUG_LOG("发送窗口大小更新失败"); - break; - } - - DEBUG_LOG("窗口大小更新已发送"); + usleep(100000); // 睡眠100ms } - DEBUG_LOG("窗口监听线程退出"); return NULL; } @@ -272,13 +242,10 @@ static void* window_monitor_thread(void* arg) { static void* terminal_input_thread(void* arg) { (void)arg; - DEBUG_LOG("终端输入线程启动"); - char buf[BUFFER_SIZE]; // 设置终端为原始模式并启用鼠标跟踪 if (isatty(STDIN_FILENO)) { - DEBUG_LOG("设置终端为原始模式"); setup_terminal_raw_mode(STDIN_FILENO); enable_mouse_tracking(STDOUT_FILENO); g_terminal_modified = 1; // 标记终端已被修改 @@ -332,7 +299,6 @@ static void* terminal_input_thread(void* arg) { int seeking_solutions(const char* filename, char* const argv[], char* const envp[], const char* logPath, int* output_fd) { - DEBUG_LOG("开始 seeking_solutions: filename=%s", filename); char abs_path[PATH_MAX]; char pwd[PATH_MAX]; @@ -369,11 +335,9 @@ int seeking_solutions(const char* filename, char* const argv[], abs_path[PATH_MAX - 1] = '\0'; } - // 创建 socket 连接 - DEBUG_LOG("尝试连接到 %s", SOCKET_PATH); + // 创建socket连接 int sock = socket(AF_UNIX, SOCK_STREAM, 0); if (sock == -1) { - DEBUG_LOG("创建 socket 失败: %s", strerror(errno)); perror("socket"); return -1; } @@ -390,20 +354,11 @@ int seeking_solutions(const char* filename, char* const argv[], return -1; } - DEBUG_LOG("连接成功"); - - // 设置全局 socket + // 设置全局socket pthread_mutex_lock(&g_socket_mutex); g_socket_fd = sock; pthread_mutex_unlock(&g_socket_mutex); - // 创建窗口大小变化通知管道 - if (pipe(g_winch_pipe) < 0) { - DEBUG_LOG("创建管道失败: %s", strerror(errno)); - close(sock); - return -1; - } - // 设置信号处理器 struct sigaction sa_winch; memset(&sa_winch, 0, sizeof(sa_winch)); @@ -552,18 +507,15 @@ int seeking_solutions(const char* filename, char* const argv[], memcpy(ptr, shell_type, shell_type_len); // 发送初始化消息 - DEBUG_LOG("发送初始化消息: payload_len=%u", total_payload_len); if (write_message(sock, MSG_TYPE_INIT, init_payload, total_payload_len) < 0) { DEBUG_LOG("Failed to send init message\n"); free(init_payload); close(sock); return -1; } - DEBUG_LOG("初始化消息发送成功"); free(init_payload); // 启动响应监听线程 - DEBUG_LOG("创建响应监听线程"); pthread_t response_thread; if (pthread_create(&response_thread, NULL, response_listener_thread, output_fd) != 0) { DEBUG_LOG("Failed to create response listener thread\n"); @@ -572,7 +524,6 @@ int seeking_solutions(const char* filename, char* const argv[], } // 启动窗口监听线程 - DEBUG_LOG("创建窗口监听线程"); pthread_t window_thread; if (pthread_create(&window_thread, NULL, window_monitor_thread, NULL) != 0) { DEBUG_LOG("Failed to create window monitor thread\n"); @@ -583,7 +534,6 @@ int seeking_solutions(const char* filename, char* const argv[], } // 启动终端输入监听线程 - DEBUG_LOG("创建终端输入监听线程"); pthread_t input_thread; if (pthread_create(&input_thread, NULL, terminal_input_thread, NULL) != 0) { DEBUG_LOG("Failed to create terminal input thread\n"); @@ -596,35 +546,15 @@ int seeking_solutions(const char* filename, char* const argv[], } // 等待响应线程结束(表示服务器关闭连接) - DEBUG_LOG("等待响应线程结束..."); pthread_join(response_thread, NULL); // 清理 - DEBUG_LOG("开始清理资源"); g_should_exit = 1; - - // 写入管道唤醒窗口监控线程 - if (g_winch_pipe[1] != -1) { - char dummy = 0; - ssize_t result = write(g_winch_pipe[1], &dummy, 1); - (void)result; - } - pthread_cancel(window_thread); pthread_cancel(input_thread); pthread_join(window_thread, NULL); pthread_join(input_thread, NULL); - // 关闭管道 - if (g_winch_pipe[0] != -1) { - close(g_winch_pipe[0]); - g_winch_pipe[0] = -1; - } - if (g_winch_pipe[1] != -1) { - close(g_winch_pipe[1]); - g_winch_pipe[1] = -1; - } - // 恢复终端状态 cleanup_terminal(); diff --git a/src/execve_interceptor.c b/src/execve_interceptor.c index 15d14ae..c40043a 100644 --- a/src/execve_interceptor.c +++ b/src/execve_interceptor.c @@ -143,6 +143,8 @@ int enhance_execve(const char *filename, char *const argv[], // 如果rule是0,也直接返回 if (shared_config->rule_count == 0) { + DEBUG_LOG("No rules defined."); + // 直接执行 #ifdef HOOK return orig_execve(filename, argv, envp); #else @@ -209,6 +211,7 @@ int enhance_execve(const char *filename, char *const argv[], } if (hasMatch == 0) { + DEBUG_LOG("No matching rule found."); // 直接执行 #ifdef HOOK return orig_execve(filename, argv, envp);