--- lbbs/src/net_server.c 2025/06/29 02:28:02 1.69 +++ lbbs/src/net_server.c 2025/10/13 07:13:39 1.72 @@ -45,6 +45,9 @@ #include #include +#define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second +#define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second + struct process_sockaddr_t { pid_t pid; @@ -122,6 +125,7 @@ static ssh_channel new_session_channel(s static int fork_server(void) { ssh_event event; + long int ssh_timeout = 0; int pid; int i; int ret; @@ -173,6 +177,13 @@ static int fork_server(void) ssh_callbacks_init(&cb); ssh_set_server_callbacks(SSH_session, &cb); + ssh_timeout = 60; // second + if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) + { + log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session)); + goto cleanup; + } + if (ssh_handle_key_exchange(SSH_session)) { log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session)); @@ -198,6 +209,13 @@ static int fork_server(void) log_error("SSH auth error, tried %d times\n", cb_data.tries); goto cleanup; } + + ssh_timeout = 0; + if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) + { + log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session)); + goto cleanup; + } } // Redirect Input @@ -258,8 +276,11 @@ int net_server(const char *hostaddr, in_ struct epoll_event ev, events[MAX_EVENTS]; int nfds, epollfd; siginfo_t siginfo; + int notify_child_exit = 0; + time_t tm_notify_child_exit = time(NULL); int sd_notify_stopping = 0; MENU_SET bbs_menu_new; + MENU_SET top10_menu_new; int i, j; pid_t pid; int ssh_log_level = SSH_LOG_NOLOG; @@ -400,15 +421,43 @@ int net_server(const char *hostaddr, in_ if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) { - log_common("Notify %d child process to exit\n", SYS_child_process_count); - if (kill(0, SIGTERM) < 0) + if (notify_child_exit == 0) { - log_error("Send SIGTERM signal failed (%d)\n", errno); + sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count); + log_common("Notify %d child process to exit\n", SYS_child_process_count); + + if (kill(0, SIGTERM) < 0) + { + log_error("Send SIGTERM signal failed (%d)\n", errno); + } + + notify_child_exit = 1; + tm_notify_child_exit = time(NULL); } + else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT) + { + sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); - sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count); + for (i = 0; i < BBS_max_client; i++) + { + if (process_sockaddr_pool[i].pid != 0) + { + log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid); + if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0) + { + log_error("Send SIGKILL signal failed (%d)\n", errno); + } + } + } - sleep(1); // Sleep for a while to avoid notifying child processes too frequently + notify_child_exit = 2; + tm_notify_child_exit = time(NULL); + } + else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT) + { + log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count); + SYS_child_process_count = 0; + } } if (SYS_conf_reload && !SYS_server_exit) @@ -425,13 +474,26 @@ int net_server(const char *hostaddr, in_ if (load_menu(&bbs_menu_new, CONF_MENU) < 0) { unload_menu(&bbs_menu_new); - log_error("Reload menu failed\n"); + log_error("Reload bbs menu failed\n"); } else { unload_menu(&bbs_menu); memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new)); - log_common("Reload menu successfully\n"); + log_common("Reload bbs menu successfully\n"); + } + + if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0) + { + unload_menu(&top10_menu_new); + log_error("Reload top10 menu failed\n"); + } + else + { + unload_menu(&top10_menu); + top10_menu_new.allow_exit = 1; + memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new)); + log_common("Reload top10 menu successfully\n"); } for (int i = 0; i < data_files_load_startup_count; i++)