--- lbbs/src/main.c 2025/11/08 08:21:31 1.73 +++ lbbs/src/main.c 2025/12/02 08:04:29 1.88 @@ -6,6 +6,10 @@ * Copyright (C) 2004-2025 Leaflet */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "bbs.h" #include "bwf.h" #include "common.h" @@ -27,8 +31,9 @@ #include #include #include +#include -static void app_help(void) +static inline void app_help(void) { fprintf(stderr, "Usage: bbsd [-fhv] [...]\n\n" "-f\t--foreground\t\tForce program run in foreground\n" @@ -36,15 +41,59 @@ static void app_help(void) "-v\t--version\t\tDisplay version information\n" "\t--display-log\t\tDisplay standard log information\n" "\t--display-error-log\tDisplay error log information\n" + "-C\t--compile-config\tDisplay compile configuration\n" "\n If meet any bug, please report to \n\n"); } -static void arg_error(void) +static inline void arg_error(void) { fprintf(stderr, "Invalid arguments\n"); app_help(); } +static inline void app_compile_info(void) +{ + printf("%s\n" + "--enable-shared\t\t[%s]\n" + "--enable-systemd\t[%s]\n" + "--with-debug\t\t[%s]\n" + "--with-epoll\t\t[%s]\n" + "--with-mariadb\t\t[%s]\n" + "--with-sysv\t\t[%s]\n", + APP_INFO, +#ifdef _ENABLE_SHARED + "yes", +#else + "no", +#endif +#ifdef HAVE_SYSTEMD_SD_DAEMON_H + "yes", +#else + "no", +#endif +#ifdef _DEBUG + "yes", +#else + "no", +#endif +#ifdef HAVE_SYS_EPOLL_H + "yes", +#else + "no", +#endif +#ifdef HAVE_MARIADB_CLIENT + "yes", +#else + "no", +#endif +#ifdef HAVE_SYSTEM_V + "yes" +#else + "no" +#endif + ); +} + int main(int argc, char *argv[]) { char file_path_temp[FILE_PATH_LEN]; @@ -55,16 +104,19 @@ int main(int argc, char *argv[]) int ret; int last_aid; struct sigaction act = {0}; + int i; + int j; + struct stat file_stat; // Parse args - for (int i = 1; i < argc; i++) + for (i = 1; i < argc; i++) { switch (argv[i][0]) { case '-': if (argv[i][1] != '-') { - for (int j = 1; j < strlen(argv[i]); j++) + for (j = 1; j < strlen(argv[i]); j++) { switch (argv[i][j]) { @@ -75,7 +127,11 @@ int main(int argc, char *argv[]) app_help(); return 0; case 'v': - puts(APP_INFO); + printf("%s\n", APP_INFO); + printf("%s\n", COPYRIGHT_INFO); + return 0; + case 'C': + app_compile_info(); return 0; default: arg_error(); @@ -97,7 +153,8 @@ int main(int argc, char *argv[]) } if (strcmp(argv[i] + 2, "version") == 0) { - puts(APP_INFO); + printf("%s\n", APP_INFO); + printf("%s\n", COPYRIGHT_INFO); return 0; } if (strcmp(argv[i] + 2, "display-log") == 0) @@ -108,6 +165,11 @@ int main(int argc, char *argv[]) { error_log_redir = 1; } + if (strcmp(argv[i] + 2, "compile-config") == 0) + { + app_compile_info(); + return 0; + } } break; } @@ -170,6 +232,14 @@ int main(int argc, char *argv[]) return -2; } + // Get EULA modification tm + if (stat(DATA_EULA, &file_stat) == -1) + { + log_error("stat(%s) error\n", DATA_EULA, errno); + goto cleanup; + } + BBS_eula_tm = file_stat.st_mtim.tv_sec; + // Check article cache dir ret = mkdir(VAR_ARTICLE_CACHE_DIR, 0750); if (ret == -1 && errno != EEXIST) @@ -214,7 +284,7 @@ int main(int argc, char *argv[]) if (trie_dict_init(VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL) < 0) { - printf("trie_dict_init(%s, %d) error\n", VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL); + log_error("trie_dict_init(%s, %d) error\n", VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL); goto cleanup; } if (article_block_init(VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block) < 0) @@ -237,22 +307,17 @@ int main(int argc, char *argv[]) // Load menus if (load_menu(&bbs_menu, CONF_MENU) < 0) { - log_error("load_menu(%s) error\n", CONF_MENU); + log_error("load_menu(bbs_menu) error\n"); goto cleanup; } if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0) { - log_error("load_menu(%s) error\n", CONF_TOP10_MENU); + log_error("load_menu(top10_menu) error\n"); goto cleanup; } top10_menu.allow_exit = 1; // Load data files - if (file_loader_init() < 0) - { - log_error("file_loader_init() error\n"); - goto cleanup; - } for (int i = 0; i < data_files_load_startup_count; i++) { if (load_file(data_files_load_startup[i]) < 0) @@ -327,6 +392,12 @@ int main(int argc, char *argv[]) log_error("set signal action of SIGTERM error: %d\n", errno); goto cleanup; } + act.sa_handler = SIG_IGN; + if (sigaction(SIGUSR1, &act, NULL) == -1) + { + log_error("set signal action of SIGUSR1 error: %d\n", errno); + goto cleanup; + } // Launch section_list_loader process if (section_list_loader_launch() < 0) @@ -339,8 +410,43 @@ int main(int argc, char *argv[]) net_server(BBS_address, BBS_port); cleanup: + // Cleanup loader process if still running + if (SYS_child_process_count > 0) + { + SYS_child_exit = 0; + + if (kill(section_list_loader_pid, SIGTERM) < 0) + { + log_error("Send SIGTERM signal failed (%d)\n", errno); + } + + for (i = 0; SYS_child_exit == 0 && i < 5; i++) + { + sleep(1); // second + } + + if ((ret = waitpid(section_list_loader_pid, NULL, WNOHANG)) < 0) + { + log_error("waitpid(%d) error (%d)\n", section_list_loader_pid, errno); + } + else if (ret == 0) + { + log_common("Force kill section_list_loader process (%d)\n", section_list_loader_pid); + if (kill(section_list_loader_pid, SIGKILL) < 0) + { + log_error("Send SIGKILL signal failed (%d)\n", errno); + } + } + } + // Cleanup loaded data files - file_loader_cleanup(); + for (int i = 0; i < data_files_load_startup_count; i++) + { + if (unload_file(data_files_load_startup[i]) < 0) + { + log_error("unload_file(%s) error\n", data_files_load_startup[i]); + } + } // Cleanup menu unload_menu(&bbs_menu); @@ -352,9 +458,6 @@ cleanup: trie_dict_cleanup(); user_list_pool_cleanup(); - // Cleanup BWF - bwf_unload(); - if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0) { log_error("unlink(%s) error\n", VAR_ARTICLE_BLOCK_SHM);