--- lbbs/src/main.c 2025/05/16 14:09:31 1.34 +++ lbbs/src/main.c 2025/05/26 04:48:33 1.42 @@ -22,6 +22,7 @@ #include "io.h" #include "menu.h" #include "file_loader.h" +#include "section_list_loader.h" #include #include #include @@ -53,6 +54,7 @@ int main(int argc, char *argv[]) int daemon = 1; int std_log_redir = 0; int error_log_redir = 0; + FILE *fp; // Parse args for (int i = 1; i < argc; i++) @@ -145,6 +147,48 @@ int main(int argc, char *argv[]) return -2; } + // Initialize data pools + if ((fp = fopen(VAR_TRIE_DICT_SHM, "w")) == NULL) + { + log_error("fopen(%s) error\n", VAR_TRIE_DICT_SHM); + return -1; + } + fclose(fp); + if ((fp = fopen(VAR_ARTICLE_BLOCK_SHM, "w")) == NULL) + { + log_error("fopen(%s) error\n", VAR_ARTICLE_BLOCK_SHM); + return -1; + } + fclose(fp); + if ((fp = fopen(VAR_SECTION_LIST_SHM, "w")) == NULL) + { + log_error("fopen(%s) error\n", VAR_SECTION_LIST_SHM); + return -1; + } + fclose(fp); + + if (trie_dict_init(VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL) < 0) + { + printf("trie_dict_init failed\n"); + return -3; + } + if (article_block_init(VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / ARTICLE_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); + return -3; + } + if (section_list_init(VAR_SECTION_LIST_SHM) < 0) + { + log_error("section_list_pool_init(%s) error\n", VAR_SECTION_LIST_SHM); + return -3; + } + + // Load BBS cmd + if (load_cmd() < 0) + { + return -3; + } + // Load menus p_bbs_menu = calloc(1, sizeof(MENU_SET)); if (p_bbs_menu == NULL) @@ -158,22 +202,28 @@ int main(int argc, char *argv[]) free(p_bbs_menu); return -3; } - unload_menu_shm(p_bbs_menu); // Load data files - if (file_loader_init(FILE_MMAP_COUNT_LIMIT) < 0) + if (file_loader_init() < 0) { log_error("file_loader_init() error\n"); return -4; } for (int i = 0; i < data_files_load_startup_count; i++) { - if (load_file_mmap(data_files_load_startup[i]) < 0) + if (load_file(data_files_load_startup[i]) < 0) { log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); } } + // Load section config + if (load_section_config_from_db() < 0) + { + log_error("load_section_config_from_db() error\n"); + return -5; + } + // Set signal handler signal(SIGHUP, sig_hup_handler); signal(SIGCHLD, sig_chld_handler); @@ -184,13 +234,34 @@ int main(int argc, char *argv[]) // Cleanup loaded data files file_loader_cleanup(); - + // Cleanup menu unload_menu(p_bbs_menu); free(p_bbs_menu); p_bbs_menu = NULL; + // Cleanup data pools + section_list_cleanup(); + article_block_cleanup(); + trie_dict_cleanup(); + + if (unlink(VAR_TRIE_DICT_SHM) < 0) + { + log_error("unlink(%s) error\n", VAR_TRIE_DICT_SHM); + return -1; + } + if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0) + { + log_error("unlink(%s) error\n", VAR_ARTICLE_BLOCK_SHM); + return -1; + } + if (unlink(VAR_SECTION_LIST_SHM) < 0) + { + log_error("unlink(%s) error\n", VAR_SECTION_LIST_SHM); + return -1; + } + log_std("Main process exit normally\n"); - + return 0; }