--- lbbs/src/main.c 2025/10/23 04:09:13 1.65 +++ lbbs/src/main.c 2025/11/18 15:38:49 1.82 @@ -1,20 +1,17 @@ -/*************************************************************************** - main.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * main + * - entry of server program + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "bbs.h" +#include "bwf.h" #include "common.h" #include "file_loader.h" #include "init.h" @@ -26,6 +23,7 @@ #include "user_list.h" #include #include +#include #include #include #include @@ -33,6 +31,7 @@ #include #include #include +#include static void app_help(void) { @@ -61,16 +60,18 @@ int main(int argc, char *argv[]) int ret; int last_aid; struct sigaction act = {0}; + int i; + int j; // 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]) { @@ -81,7 +82,8 @@ 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; default: arg_error(); @@ -103,7 +105,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) @@ -140,6 +143,13 @@ int main(int argc, char *argv[]) return -1; } + // Apply the specified locale + if (setlocale(LC_ALL, "en_US.UTF-8") == NULL) + { + fprintf(stderr, "setlocale(LC_ALL, en_US.UTF-8) error\n"); + return -1; + } + // Initialize log if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0) { @@ -163,6 +173,12 @@ int main(int argc, char *argv[]) return -2; } + // Load BWF config + if (bwf_load(CONF_BWF) < 0) + { + return -2; + } + // Check article cache dir ret = mkdir(VAR_ARTICLE_CACHE_DIR, 0750); if (ret == -1 && errno != EEXIST) @@ -171,6 +187,14 @@ int main(int argc, char *argv[]) goto cleanup; } + // Check section aid location dir + ret = mkdir(VAR_SECTION_AID_LOC_DIR, 0750); + if (ret == -1 && errno != EEXIST) + { + log_error("mkdir(%s) error (%d)\n", VAR_SECTION_AID_LOC_DIR, errno); + goto cleanup; + } + // Initialize data pools if ((fp = fopen(VAR_ARTICLE_BLOCK_SHM, "w")) == NULL) { @@ -199,12 +223,12 @@ int main(int argc, char *argv[]) if (trie_dict_init(VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL) < 0) { - printf("trie_dict_init failed\n"); + 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 / ARTICLE_PER_BLOCK) < 0) + if (article_block_init(VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block) < 0) { - log_error("article_block_init(%s, %d) error\n", VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK); + log_error("article_block_init(%s, %d) error\n", VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block); goto cleanup; } if (section_list_init(VAR_SECTION_LIST_SHM) < 0) @@ -242,14 +266,14 @@ int main(int argc, char *argv[]) { if (load_file(data_files_load_startup[i]) < 0) { - log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); + log_error("load_file(%s) error\n", data_files_load_startup[i]); } } // Load user_list and online_user_list - if (user_list_pool_init() < 0) + if (user_list_pool_init(VAR_USER_LIST_SHM) < 0) { - log_error("user_list_pool_init() error\n"); + log_error("user_list_pool_init(%s) error\n", VAR_USER_LIST_SHM); goto cleanup; } if (user_list_pool_reload(0) < 0) @@ -312,6 +336,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) @@ -324,6 +354,35 @@ 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(); @@ -337,6 +396,9 @@ 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);