From 013ec303795f68c7f7b918114af49cff22c06237 Mon Sep 17 00:00:00 2001 From: chenzheng Date: Thu, 19 Sep 2024 03:46:18 +0000 Subject: [PATCH] bugfix: vtz_proxy use err sock when vm close sock --- trustzone-awared-vm/Host/vtzb_proxy/agent.c | 2 +- .../Host/vtzb_proxy/serial_port.c | 39 +++++-------------- .../Host/vtzb_proxy/vtzb_proxy.c | 19 +++++---- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/trustzone-awared-vm/Host/vtzb_proxy/agent.c b/trustzone-awared-vm/Host/vtzb_proxy/agent.c index b8e6da7..d508f72 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/agent.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/agent.c @@ -68,7 +68,7 @@ void register_agent(struct_packet_cmd_regagent *packet_cmd, ret = ioctl(packet_cmd->ptzfd, TC_NS_CLIENT_IOCTL_REGISTER_AGENT, buf); if (!ret) { /* Add the agent buffer to the linked list. */ - struct_agent_args *tmp = (struct_agent_args *)malloc(sizeof(struct_agent_args)); + struct_agent_args *tmp = (struct_agent_args *)calloc(1, sizeof(struct_agent_args)); if (!tmp) { tloge("Failed to allocate memory for agent buffer\n"); ret = -ENOMEM; diff --git a/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c b/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c index 2884b2c..1187d25 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c @@ -45,6 +45,7 @@ int serial_port_list_init() serial_port->offset = 0; serial_port->rd_buf = (char *)malloc(BUF_LEN_MAX_RD); serial_port->vm_file = NULL; + g_pollfd[i].fd = -1; if (!serial_port->rd_buf) { tloge("Failed to allocate memory for rd_buf\n"); free(serial_port); @@ -84,7 +85,6 @@ void serial_port_list_destroy() int send_to_vm(struct serial_port_file *serial_port, void *packet_rsp, size_t size_rsp) { int ret = 0; - int index; if (!serial_port || serial_port->sock <= 0 || !packet_rsp) return -1; pthread_mutex_lock(&serial_port->lock); @@ -92,21 +92,6 @@ int send_to_vm(struct serial_port_file *serial_port, void *packet_rsp, size_t si if (ret == -1) { if (errno == EPIPE) { // 处理 EPIPE 错误 - close(serial_port->sock); - for (index = 0; index < g_pollfd_len; index++) { - if (g_pollfd[index].fd == serial_port->sock) { - break; - } - } - serial_port->sock = 0; - g_serial_array[index] = g_serial_array[g_pollfd_len - 1]; - g_pollfd[index] = g_pollfd[g_pollfd_len - 1]; - g_pollfd_len--; - serial_port->opened = false; - if (serial_port->vm_file) { - destroy_vm_file(serial_port->vm_file); - } - serial_port->vm_file = NULL; tloge("Send failed with EPIPE: Broken pipe, socket closed\n"); } else { @@ -144,7 +129,7 @@ static int connect_domsock_chardev(char *dev_path, int *sock) static void do_check_stat_serial_port() { int ret; - int index; + int i = 0; struct serial_port_file *serial_port; (void)pthread_mutex_lock(&g_serial_list.lock); LIST_FOR_EACH_ENTRY(serial_port, &g_serial_list.head, head){ @@ -157,25 +142,18 @@ static void do_check_stat_serial_port() } else { serial_port->opened = true; serial_port->offset = 0; - g_serial_array[g_pollfd_len] = serial_port; - g_pollfd[g_pollfd_len].fd = serial_port->sock; - g_pollfd[g_pollfd_len].events = POLLIN; - g_pollfd_len++; + g_serial_array[i] = serial_port; + g_pollfd[i].fd = serial_port->sock; + g_pollfd[i].events = POLLIN; } } } else { ret = access(serial_port->path, R_OK | W_OK); if (ret) { close(serial_port->sock); - for (index = 0; index < g_pollfd_len; index++) { - if (g_pollfd[index].fd == serial_port->sock) { - break; - } - } - serial_port->sock = 0; - g_serial_array[index] = g_serial_array[g_pollfd_len - 1]; - g_pollfd[index] = g_pollfd[g_pollfd_len - 1]; - g_pollfd_len--; + serial_port->sock = -1; + g_serial_array[i] = NULL; + g_pollfd[i].fd = -1; serial_port->opened = false; if (serial_port->vm_file) { destroy_vm_file(serial_port->vm_file); @@ -183,6 +161,7 @@ static void do_check_stat_serial_port() serial_port->vm_file = NULL; } } + i++; } (void)pthread_mutex_unlock(&g_serial_list.lock); } diff --git a/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c b/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c index 2b42939..05d48e5 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c @@ -681,7 +681,6 @@ void *thread_entry(void *args) struct serial_port_file *serial_port = (struct serial_port_file *)u64; char *rd_buf = (char *)(args) + sizeof(uint64_t); ui32_cmd = *(uint32_t *)(rd_buf + sizeof(uint32_t)); - tloge("CMD is %d\n", ui32_cmd); if (ui32_cmd == VTZ_OPEN_TZD) { (void)open_tzdriver((struct_packet_cmd_open_tzd *)rd_buf, serial_port); @@ -805,7 +804,7 @@ int main() { while (1) { check_stat_serial_port(); - ret = safepoll(g_pollfd, g_pollfd_len, 20*1000); + ret = safepoll(g_pollfd, SERIAL_PORT_NUM, 20*1000); if (ret == -1) { tloge("pollfd failed, ret = %d \n", ret); return -1; @@ -814,16 +813,20 @@ int main() { continue; } - for (i = 0; i < g_pollfd_len; i++) { - if (g_pollfd[i].revents & POLLHUP || - g_pollfd[i].revents & POLLERR || + for (i = 0; i < SERIAL_PORT_NUM; i++) { + if (g_pollfd[i].revents & POLLIN) { + proc_event(g_serial_array[i]); + } + + if (g_pollfd[i].revents & POLLERR || g_pollfd[i].revents & POLLNVAL) { - sleep(CHECK_TIME_SEC); continue; } - if (g_pollfd[i].revents & POLLIN) { - proc_event(g_serial_array[i]); + if (g_pollfd[i].revents & POLLHUP) { + g_serial_array[i]->opened = false; + close(g_serial_array[i]->sock); + g_pollfd[i].fd = -1; } } } -- Gitee