diff --git a/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c b/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c index dc25b039d3c29e553528226706f8d8d9ee4a5716..6e7ff8ca1dda08f3bbea2ebe426bc7e177f8f355 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c @@ -169,6 +169,8 @@ static void do_check_stat_serial_port() tlogd("vm %d started, connect fd %d, create read thread\n", i, serial_port->sock); serial_port->opened = true; serial_port->offset = 0; + g_pollfd[i].fd = serial_port->sock; + g_serial_array[i] = serial_port; create_reader_thread(serial_port, i); } } @@ -176,6 +178,9 @@ static void do_check_stat_serial_port() ret = access(serial_port->path, R_OK | W_OK); if (ret) { tlogd("vm %d closed, fd %d is invalid, should close\n", i, serial_port->sock); + g_pollfd[i].fd = -1; + serial_port->opened = false; + g_serial_array[i] = NULL; release_vm_file(serial_port, i); } } diff --git a/trustzone-awared-vm/Host/vtzb_proxy/thread_pool.c b/trustzone-awared-vm/Host/vtzb_proxy/thread_pool.c index e897e78c289777a8639086bfde8b9d2cb6f117d0..69cefd0e0cf7bf9b032e9dbce794aba0b9818b42 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/thread_pool.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/thread_pool.c @@ -222,6 +222,11 @@ static void *deal_packet_thread(void *arg) continue; } buf_len = ret + serial_port->offset; + /* + * while loop will deal all complete packets, left the incomplete one in the + * starting position of rd_buf, so the offset should be 0 echo read times + */ + offset = 0; while (1) { void *packet = NULL; packet = get_packet_item(serial_port->rd_buf, buf_len, &offset); @@ -238,7 +243,12 @@ static void *deal_packet_thread(void *arg) } end: - tlogd("reader thread %d exit\n", serial_port->index); + if (serial_port) { + serial_port->opened = false; + tlogi("reader thread %d exited\n", serial_port->index); + } else { + tloge("serial_port is null, and reader thread exit\n"); + } return NULL; } diff --git a/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c b/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c index 62fa837579b27386af08ec30c0ba49faa1802b9b..2388dbfcfdcbc9f07f7fbcf516779bb1b1efa0d8 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c @@ -775,6 +775,8 @@ END: } int main() { + int ret = 0; + serial_port_list_init(); if (thread_pool_init(&g_pool)) goto END2; @@ -783,6 +785,18 @@ int main() { while (1) { check_stat_serial_port(); + ret = safepoll(g_pollfd, SERIAL_PORT_NUM, 20*1000); + if (ret <= 0) { + continue; + } + for (int i = 0; i < SERIAL_PORT_NUM; i++) { + // when vm restart quickly, use poll can capture this event + if (g_pollfd[i].revents & POLLHUP) { + tlogi("vm %d got POLLHUP event, release vm\n", i); + release_vm_file(g_serial_array[i], i); + } + } + } END1: