--- lbbs/src/main.c 2025/07/16 05:46:17 1.61 +++ lbbs/src/main.c 2025/11/11 00:28:05 1.74 @@ -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" @@ -23,8 +20,10 @@ #include "menu.h" #include "net_server.h" #include "section_list_loader.h" +#include "user_list.h" #include #include +#include #include #include #include @@ -139,6 +138,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) { @@ -162,6 +168,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) @@ -170,13 +182,15 @@ int main(int argc, char *argv[]) goto cleanup; } - // Initialize data pools - if ((fp = fopen(VAR_TRIE_DICT_SHM, "w")) == NULL) + // Check section aid location dir + ret = mkdir(VAR_SECTION_AID_LOC_DIR, 0750); + if (ret == -1 && errno != EEXIST) { - log_error("fopen(%s) error\n", VAR_TRIE_DICT_SHM); + log_error("mkdir(%s) error (%d)\n", VAR_SECTION_AID_LOC_DIR, errno); goto cleanup; } - fclose(fp); + + // Initialize data pools if ((fp = fopen(VAR_ARTICLE_BLOCK_SHM, "w")) == NULL) { log_error("fopen(%s) error\n", VAR_ARTICLE_BLOCK_SHM); @@ -189,15 +203,27 @@ int main(int argc, char *argv[]) goto cleanup; } fclose(fp); + if ((fp = fopen(VAR_TRIE_DICT_SHM, "w")) == NULL) + { + log_error("fopen(%s) error\n", VAR_TRIE_DICT_SHM); + goto cleanup; + } + fclose(fp); + if ((fp = fopen(VAR_USER_LIST_SHM, "w")) == NULL) + { + log_error("fopen(%s) error\n", VAR_USER_LIST_SHM); + goto cleanup; + } + fclose(fp); if (trie_dict_init(VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL) < 0) { - printf("trie_dict_init failed\n"); + printf("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) @@ -215,8 +241,15 @@ 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); + goto cleanup; + } + if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0) + { + log_error("load_menu(%s) error\n", CONF_TOP10_MENU); goto cleanup; } + top10_menu.allow_exit = 1; // Load data files if (file_loader_init() < 0) @@ -228,10 +261,27 @@ 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(VAR_USER_LIST_SHM) < 0) + { + log_error("user_list_pool_init(%s) error\n", VAR_USER_LIST_SHM); + goto cleanup; + } + if (user_list_pool_reload(0) < 0) + { + log_error("user_list_pool_reload(all_user) error\n"); + goto cleanup; + } + if (user_list_pool_reload(1) < 0) + { + log_error("user_list_pool_reload(online_user) error\n"); + goto cleanup; + } + // Load section config and gen_ex if (load_section_config_from_db(1) < 0) { @@ -256,6 +306,12 @@ int main(int argc, char *argv[]) log_common("Initially load %d articles, last_aid = %d\n", article_block_article_count(), article_block_last_aid()); + if ((ret = user_stat_update()) < 0) + { + log_error("user_stat_update() error\n"); + goto cleanup; + } + // Set signal handler act.sa_handler = sig_hup_handler; if (sigaction(SIGHUP, &act, NULL) == -1) @@ -292,16 +348,17 @@ cleanup: // Cleanup menu unload_menu(&bbs_menu); + unload_menu(&top10_menu); // Cleanup data pools section_list_cleanup(); article_block_cleanup(); trie_dict_cleanup(); + user_list_pool_cleanup(); + + // Cleanup BWF + bwf_unload(); - if (unlink(VAR_TRIE_DICT_SHM) < 0) - { - log_error("unlink(%s) error\n", VAR_TRIE_DICT_SHM); - } if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0) { log_error("unlink(%s) error\n", VAR_ARTICLE_BLOCK_SHM); @@ -310,6 +367,14 @@ cleanup: { log_error("unlink(%s) error\n", VAR_SECTION_LIST_SHM); } + if (unlink(VAR_TRIE_DICT_SHM) < 0) + { + log_error("unlink(%s) error\n", VAR_TRIE_DICT_SHM); + } + if (unlink(VAR_USER_LIST_SHM) < 0) + { + log_error("unlink(%s) error\n", VAR_SECTION_LIST_SHM); + } log_common("Main process exit normally\n");