--- lbbs/src/main.c 2025/05/13 11:32:01 1.31 +++ lbbs/src/main.c 2025/06/21 02:15:18 1.55 @@ -15,19 +15,22 @@ ***************************************************************************/ #include "bbs.h" -#include "init.h" #include "common.h" -#include "net_server.h" -#include "log.h" +#include "file_loader.h" +#include "init.h" #include "io.h" +#include "log.h" #include "menu.h" -#include +#include "net_server.h" +#include "section_list_loader.h" +#include +#include #include +#include #include #include -#include -#include #include +#include void app_help(void) { @@ -52,6 +55,10 @@ int main(int argc, char *argv[]) int daemon = 1; int std_log_redir = 0; int error_log_redir = 0; + FILE *fp; + int ret; + int last_aid; + struct sigaction act = {0}; // Parse args for (int i = 1; i < argc; i++) @@ -72,7 +79,7 @@ int main(int argc, char *argv[]) app_help(); return 0; case 'v': - puts(app_version); + puts(APP_INFO); return 0; default: arg_error(); @@ -94,7 +101,7 @@ int main(int argc, char *argv[]) } if (strcmp(argv[i] + 2, "version") == 0) { - puts(app_version); + puts(APP_INFO); return 0; } if (strcmp(argv[i] + 2, "display-log") == 0) @@ -131,41 +138,179 @@ int main(int argc, char *argv[]) if ((!daemon) && std_log_redir) { - log_std_redirect(STDERR_FILENO); + log_common_redir(STDERR_FILENO); } if ((!daemon) && error_log_redir) { - log_err_redirect(STDERR_FILENO); + log_error_redir(STDERR_FILENO); } + log_common("Starting %s\n", APP_INFO); + // Load configuration if (load_conf(CONF_BBSD) < 0) { return -2; } + // Check article cache dir + ret = mkdir(VAR_ARTICLE_CACHE_DIR, 0750); + if (ret == -1 && errno != EEXIST) + { + log_error("mkdir(%s) error (%d)\n", VAR_ARTICLE_CACHE_DIR, errno); + goto cleanup; + } + + // Initialize data pools + 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_ARTICLE_BLOCK_SHM, "w")) == NULL) + { + log_error("fopen(%s) error\n", VAR_ARTICLE_BLOCK_SHM); + goto cleanup; + } + fclose(fp); + if ((fp = fopen(VAR_SECTION_LIST_SHM, "w")) == NULL) + { + log_error("fopen(%s) error\n", VAR_SECTION_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"); + goto cleanup; + } + 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); + goto cleanup; + } + if (section_list_init(VAR_SECTION_LIST_SHM) < 0) + { + log_error("section_list_pool_init(%s) error\n", VAR_SECTION_LIST_SHM); + goto cleanup; + } + + // Load BBS cmd + if (load_cmd() < 0) + { + goto cleanup; + } + // Load menus - if (load_menu(&bbs_menu, CONF_MENU) < 0) + p_bbs_menu = calloc(1, sizeof(MENU_SET)); + if (p_bbs_menu == NULL) { - unload_menu(&bbs_menu); - return -3; + log_error("OOM: calloc(MENU_SET)\n"); + goto cleanup; } + if (load_menu(p_bbs_menu, CONF_MENU) < 0) + { + goto cleanup; + } + + // 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) + { + 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"); + goto cleanup; + } + + set_last_article_op_log_from_db(); + last_aid = 0; + + // Load section articles + do + { + if ((ret = append_articles_from_db(last_aid + 1, 1, LOAD_ARTICLE_COUNT_LIMIT)) < 0) + { + log_error("append_articles_from_db(0, 1, %d) error\n", LOAD_ARTICLE_COUNT_LIMIT); + goto cleanup; + } + + last_aid = article_block_last_aid(); + } while (ret == LOAD_ARTICLE_COUNT_LIMIT); + + log_common("Initially load %d articles, last_aid = %d\n", article_block_article_count(), article_block_last_aid()); // Set signal handler - signal(SIGHUP, sig_hup_handler); - signal(SIGCHLD, sig_chld_handler); - signal(SIGTERM, sig_term_handler); + act.sa_handler = sig_hup_handler; + if (sigaction(SIGHUP, &act, NULL) == -1) + { + log_error("set signal action of SIGHUP error: %d\n", errno); + goto cleanup; + } + act.sa_handler = sig_chld_handler; + if (sigaction(SIGCHLD, &act, NULL) == -1) + { + log_error("set signal action of SIGCHLD error: %d\n", errno); + goto cleanup; + } + act.sa_handler = sig_term_handler; + if (sigaction(SIGTERM, &act, NULL) == -1) + { + log_error("set signal action of SIGTERM error: %d\n", errno); + goto cleanup; + } + + // Launch section_list_loader process + if (section_list_loader_launch() < 0) + { + log_error("section_list_loader_launch() error\n"); + goto cleanup; + } // Initialize socket server - if (net_server(BBS_address, BBS_port) < 0) + net_server(BBS_address, BBS_port); + +cleanup: + // 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); + } + if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0) + { + log_error("unlink(%s) error\n", VAR_ARTICLE_BLOCK_SHM); + } + if (unlink(VAR_SECTION_LIST_SHM) < 0) { - return -4; + log_error("unlink(%s) error\n", VAR_SECTION_LIST_SHM); } - // Cleanup - unload_menu(&bbs_menu); + log_common("Main process exit normally\n"); - log_std("Main process exit normally\n"); - return 0; }