--- lbbs/src/net_server.c 2025/05/17 05:56:17 1.36 +++ lbbs/src/net_server.c 2025/06/05 05:24:56 1.51 @@ -22,9 +22,11 @@ #include "common.h" #include "log.h" #include "io.h" +#include "init.h" #include "fork.h" #include "menu.h" #include "file_loader.h" +#include "section_list_loader.h" #include #include #include @@ -36,10 +38,22 @@ #include #include #include +#include #include +#include + +struct process_sockaddr_t +{ + pid_t pid; + in_addr_t s_addr; +}; +typedef struct process_sockaddr_t PROCESS_SOCKADDR; + +static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; int net_server(const char *hostaddr, in_port_t port) { + ssh_bind sshbind; unsigned int namelen; int ret; int flags; @@ -49,7 +63,24 @@ int net_server(const char *hostaddr, in_ siginfo_t siginfo; int sd_notify_stopping = 0; MENU_SET *p_bbs_menu_new; - + int i, j; + pid_t pid; + int ssh_log_level = SSH_LOG_NOLOG; + + ssh_init(); + + sshbind = ssh_bind_new(); + + if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) + { + log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); + ssh_bind_free(sshbind); + return -1; + } + socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (socket_server < 0) @@ -92,7 +123,7 @@ int net_server(const char *hostaddr, in_ port_server = ntohs(sin.sin_port); namelen = sizeof(sin); - log_std("Listening at %s:%d\n", hostaddr_server, port_server); + log_common("Listening at %s:%d\n", hostaddr_server, port_server); epollfd = epoll_create1(0); if (epollfd < 0) @@ -129,6 +160,7 @@ int net_server(const char *hostaddr, in_ 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; @@ -140,7 +172,24 @@ int net_server(const char *hostaddr, in_ SYS_child_exit = 1; // Retry waitid SYS_child_process_count--; - log_std("Child process (%d) exited\n", siginfo.si_pid); + log_common("Child process (%d) exited\n", siginfo.si_pid); + + if (siginfo.si_pid != section_list_loader_pid) + { + i = 0; + for (; i < BBS_max_client; i++) + { + if (process_sockaddr_pool[i].pid == siginfo.si_pid) + { + process_sockaddr_pool[i].pid = 0; + break; + } + } + if (i >= BBS_max_client) + { + log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid); + } + } } else if (ret == 0) { @@ -155,7 +204,7 @@ int net_server(const char *hostaddr, in_ if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) { - log_std("Notify %d child process to exit\n", 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); @@ -164,9 +213,16 @@ int net_server(const char *hostaddr, in_ sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count); } - if (SYS_menu_reload && !SYS_server_exit) + if (SYS_conf_reload && !SYS_server_exit) { - SYS_menu_reload = 0; + SYS_conf_reload = 0; + sd_notify(0, "RELOADING=1"); + + // Reload configuration + if (load_conf(CONF_BBSD) < 0) + { + log_error("Reload conf failed\n"); + } p_bbs_menu_new = calloc(1, sizeof(MENU_SET)); if (p_bbs_menu_new == NULL) @@ -177,36 +233,49 @@ int net_server(const char *hostaddr, in_ { 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_shm(p_bbs_menu_new); - 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"); + log_common("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_shm(data_files_load_startup[i]) < 0) + 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"); + log_common("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 @@ -256,11 +325,56 @@ int net_server(const char *hostaddr, in_ port_client = ntohs(sin.sin_port); - log_std("Accept connection from %s:%d\n", hostaddr_client, port_client); + log_common("Accept connection from %s:%d\n", hostaddr_client, port_client); - if (fork_server() < 0) + if (SYS_child_process_count - 1 < BBS_max_client) { - log_error("fork_server() error\n"); + j = 0; + for (i = 0; i < BBS_max_client; i++) + { + if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr) + { + j++; + if (j >= BBS_max_client_per_ip) + { + log_common("Too many client connections (%d) from %s\n", j, hostaddr_client); + break; + } + } + } + + if (j < BBS_max_client_per_ip) + { + if ((pid = fork_server(sshbind)) < 0) + { + log_error("fork_server() error\n"); + } + else if (pid > 0) + { + i = 0; + for (; i < BBS_max_client; i++) + { + if (process_sockaddr_pool[i].pid == 0) + { + break; + } + } + + if (i >= BBS_max_client) + { + log_error("Process sockaddr pool depleted\n"); + } + else + { + process_sockaddr_pool[i].pid = pid; + process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr; + } + } + } + } + else + { + log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1); } if (close(socket_client) == -1) @@ -284,5 +398,8 @@ int net_server(const char *hostaddr, in_ log_error("Close server socket failed\n"); } + ssh_bind_free(sshbind); + ssh_finalize(); + return 0; }