--- lbbs/src/net_server.c 2025/11/14 08:45:25 1.82 +++ lbbs/src/net_server.c 2025/11/16 13:18:30 1.86 @@ -43,7 +43,10 @@ #include #include #include + +#ifdef HAVE_SYSTEMD_SD_DAEMON_H #include +#endif enum _net_server_constant_t { @@ -393,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"); @@ -401,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"); @@ -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(); @@ -560,18 +563,22 @@ int net_server(const char *hostaddr, in_ } // 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) { @@ -596,24 +603,10 @@ int net_server(const char *hostaddr, in_ } else { - sin.sin_addr.s_addr = (uint32_t)j; - j = 0; - ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, (int64_t *)&j); + ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1); if (ret < 0) { - log_error("hash_dict_get(hash_dict_sockaddr_count, %d) error\n", sin.sin_addr.s_addr); - } - else if (ret == 0) - { - log_error("hash_dict_get(hash_dict_sockaddr_count, %d) not found\n", sin.sin_addr.s_addr); - j = 1; - } - - j--; - ret = hash_dict_set(hash_dict_sockaddr_count, sin.sin_addr.s_addr, j); - if (ret < 0) - { - log_error("hash_dict_set(hash_dict_sockaddr_count, %d, %d) error\n", sin.sin_addr.s_addr, j); + 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); @@ -639,8 +632,10 @@ 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(-getpid(), SIGTERM) < 0) { @@ -652,7 +647,9 @@ 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 if (kill(-getpid(), SIGKILL) < 0) { @@ -672,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) @@ -730,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 @@ -808,10 +822,10 @@ int net_server(const char *hostaddr, in_ log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client); } - ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, j + 1); + ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); if (ret < 0) { - log_error("hash_dict_set(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, j + 1); + log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1); } } }