--- lbbs/src/net_server.c 2025/05/13 07:30:53 1.30 +++ lbbs/src/net_server.c 2025/05/31 14:05:53 1.43 @@ -24,6 +24,8 @@ #include "io.h" #include "fork.h" #include "menu.h" +#include "file_loader.h" +#include "section_list_loader.h" #include #include #include @@ -35,6 +37,7 @@ #include #include #include +#include int net_server(const char *hostaddr, in_port_t port) { @@ -45,6 +48,8 @@ int net_server(const char *hostaddr, in_ struct epoll_event ev, events[MAX_EVENTS]; int nfds, epollfd; siginfo_t siginfo; + int sd_notify_stopping = 0; + MENU_SET *p_bbs_menu_new; socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -112,8 +117,20 @@ int net_server(const char *hostaddr, in_ flags = fcntl(socket_server, F_GETFL, 0); fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); + // Startup complete + sd_notifyf(0, "READY=1\n" + "STATUS=Listening at %s:%d\n" + "MAINPID=%d", + hostaddr_server, port_server, getpid()); + while (!SYS_server_exit || SYS_child_process_count > 0) { + if (SYS_server_exit && !sd_notify_stopping) + { + sd_notify(0, "STOPPING=1"); + sd_notify_stopping = 1; + } + while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) { SYS_child_exit = 0; @@ -145,20 +162,67 @@ int net_server(const char *hostaddr, in_ { log_error("Send SIGTERM signal failed (%d)\n", errno); } + + sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count); } if (SYS_menu_reload && !SYS_server_exit) { SYS_menu_reload = 0; + sd_notify(0, "RELOADING=1"); - if (reload_menu(&bbs_menu) < 0) + p_bbs_menu_new = calloc(1, sizeof(MENU_SET)); + if (p_bbs_menu_new == NULL) + { + log_error("OOM: calloc(MENU_SET)\n"); + } + else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0) { + unload_menu(p_bbs_menu_new); + free(p_bbs_menu_new); + p_bbs_menu_new = NULL; + log_error("Reload menu failed\n"); } else { + unload_menu(p_bbs_menu); + free(p_bbs_menu); + + p_bbs_menu = p_bbs_menu_new; + p_bbs_menu_new = NULL; + log_std("Reload menu successfully\n"); } + + sd_notify(0, "READY=1"); + } + + if (SYS_data_file_reload && !SYS_server_exit) + { + SYS_data_file_reload = 0; + sd_notify(0, "RELOADING=1"); + + for (int i = 0; i < data_files_load_startup_count; i++) + { + if (load_file(data_files_load_startup[i]) < 0) + { + log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); + } + } + + log_std("Reload data files successfully\n"); + sd_notify(0, "READY=1"); + } + + if (SYS_section_list_reload && !SYS_server_exit) + { + SYS_section_list_reload = 0; + + if (section_list_loader_reload() < 0) + { + log_error("ksection_list_loader_reload() failed\n"); + } } nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second @@ -210,9 +274,16 @@ int net_server(const char *hostaddr, in_ log_std("Accept connection from %s:%d\n", hostaddr_client, port_client); - if (fork_server() < 0) + if (SYS_child_process_count - 1 < BBS_max_client) + { + if (fork_server() < 0) + { + log_error("fork_server() error\n"); + } + } + else { - log_error("fork_server() error\n"); + log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1); } if (close(socket_client) == -1)