--- lbbs/src/net_server.c 2025/11/05 04:19:21 1.79 +++ lbbs/src/net_server.c 2025/11/16 13:18:30 1.86 @@ -6,11 +6,17 @@ * Copyright (C) 2004-2025 Leaflet */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "bbs.h" #include "bbs_main.h" +#include "bwf.h" #include "common.h" #include "database.h" #include "file_loader.h" +#include "hash_dict.h" #include "io.h" #include "init.h" #include "log.h" @@ -37,7 +43,10 @@ #include #include #include + +#ifdef HAVE_SYSTEMD_SD_DAEMON_H #include +#endif enum _net_server_constant_t { @@ -47,15 +56,6 @@ enum _net_server_constant_t SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds }; -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]; - static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; /* A userdata struct for session. */ @@ -396,7 +396,6 @@ static int fork_server(void) } // Redirect Input - close(STDIN_FILENO); if (dup2(socket_client, STDIN_FILENO) == -1) { log_error("Redirect stdin to client socket failed\n"); @@ -404,7 +403,6 @@ static int fork_server(void) } // Redirect Output - close(STDOUT_FILENO); if (dup2(socket_client, STDOUT_FILENO) == -1) { log_error("Redirect stdout to client socket failed\n"); @@ -451,6 +449,9 @@ cleanup: int net_server(const char *hostaddr, in_port_t port[]) { + HASH_DICT *hash_dict_pid_sockaddr = NULL; + HASH_DICT *hash_dict_sockaddr_count = NULL; + unsigned int addrlen; int ret; int flags[2]; @@ -460,12 +461,14 @@ int net_server(const char *hostaddr, in_ 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; +#ifdef HAVE_SYSTEMD_SD_DAEMON_H + int sd_notify_stopping = 0; +#endif ssh_init(); @@ -546,19 +549,36 @@ int net_server(const char *hostaddr, in_ fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK); } + hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); + if (hash_dict_pid_sockaddr == NULL) + { + log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n"); + return -1; + } + hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT); + if (hash_dict_sockaddr_count == NULL) + { + log_error("hash_dict_create(hash_dict_sockaddr_count) error\n"); + return -1; + } + // Startup complete +#ifdef HAVE_SYSTEMD_SD_DAEMON_H sd_notifyf(0, "READY=1\n" "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" "MAINPID=%d", hostaddr, port[0], hostaddr, port[1], getpid()); +#endif while (!SYS_server_exit || SYS_child_process_count > 0) { +#ifdef HAVE_SYSTEMD_SD_DAEMON_H if (SYS_server_exit && !sd_notify_stopping) { sd_notify(0, "STOPPING=1"); sd_notify_stopping = 1; } +#endif while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) { @@ -575,18 +595,25 @@ int net_server(const char *hostaddr, in_ if (siginfo.si_pid != section_list_loader_pid) { - i = 0; - for (; i < BBS_max_client; i++) + j = 0; + ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j); + if (ret < 0) { - if (process_sockaddr_pool[i].pid == siginfo.si_pid) - { - process_sockaddr_pool[i].pid = 0; - break; - } + log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid); } - if (i >= BBS_max_client) + else { - log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid); + ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1); + if (ret < 0) + { + log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j); + } + + ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid); + if (ret < 0) + { + log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid); + } } } } @@ -605,10 +632,12 @@ int net_server(const char *hostaddr, in_ { if (notify_child_exit == 0) { +#ifdef HAVE_SYSTEMD_SD_DAEMON_H 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); +#endif - if (kill(0, SIGTERM) < 0) + if (kill(-getpid(), SIGTERM) < 0) { log_error("Send SIGTERM signal failed (%d)\n", errno); } @@ -618,18 +647,13 @@ int net_server(const char *hostaddr, in_ } else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT) { +#ifdef HAVE_SYSTEMD_SD_DAEMON_H sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); +#endif - for (i = 0; i < BBS_max_client; i++) + if (kill(-getpid(), SIGKILL) < 0) { - 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); - } - } + log_error("Send SIGKILL signal failed (%d)\n", errno); } notify_child_exit = 2; @@ -645,7 +669,16 @@ int net_server(const char *hostaddr, in_ if (SYS_conf_reload && !SYS_server_exit) { SYS_conf_reload = 0; + +#ifdef HAVE_SYSTEMD_SD_DAEMON_H sd_notify(0, "RELOADING=1"); +#endif + + // Restart log + if (log_restart() < 0) + { + log_error("Restart logging failed\n"); + } // Reload configuration if (load_conf(CONF_BBSD) < 0) @@ -653,6 +686,12 @@ int net_server(const char *hostaddr, in_ log_error("Reload conf failed\n"); } + // Reload BWF config + if (bwf_load(CONF_BWF) < 0) + { + log_error("Reload BWF conf failed\n"); + } + if (load_menu(&bbs_menu_new, CONF_MENU) < 0) { unload_menu(&bbs_menu_new); @@ -697,7 +736,15 @@ int net_server(const char *hostaddr, in_ log_common("Reload section config and gen_ex successfully\n"); } + // Notify child processes to reload configuration + if (kill(-getpid(), SIGUSR1) < 0) + { + log_error("Send SIGUSR1 signal failed (%d)\n", errno); + } + +#ifdef HAVE_SYSTEMD_SD_DAEMON_H sd_notify(0, "READY=1"); +#endif } nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second @@ -755,17 +802,10 @@ int net_server(const char *hostaddr, in_ if (SYS_child_process_count - 1 < BBS_max_client) { j = 0; - for (i = 0; i < BBS_max_client; i++) + ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j); + if (ret < 0) { - 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; - } - } + log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client); } if (j < BBS_max_client_per_ip) @@ -776,26 +816,23 @@ int net_server(const char *hostaddr, in_ } else if (pid > 0) { - i = 0; - for (; i < BBS_max_client; i++) + ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr); + if (ret < 0) { - if (process_sockaddr_pool[i].pid == 0) - { - break; - } + log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client); } - if (i >= BBS_max_client) - { - log_error("Process sockaddr pool depleted\n"); - } - else + ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); + if (ret < 0) { - process_sockaddr_pool[i].pid = pid; - process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr; + log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1); } } } + else + { + log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip); + } } else { @@ -826,6 +863,9 @@ int net_server(const char *hostaddr, in_ } } + hash_dict_destroy(hash_dict_pid_sockaddr); + hash_dict_destroy(hash_dict_sockaddr_count); + ssh_bind_free(sshbind); ssh_finalize();