--- lbbs/src/main.c 2025/05/27 03:25:02 1.45 +++ lbbs/src/main.c 2025/06/26 12:17:02 1.60 @@ -15,36 +15,38 @@ ***************************************************************************/ #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 "file_loader.h" +#include "net_server.h" #include "section_list_loader.h" -#include +#include +#include #include +#include +#include #include #include -#include -#include #include +#include -void app_help(void) +static void app_help(void) { - prints("Usage: bbsd [-fhv] [...]\n\n" - "-f\t--foreground\t\tForce program run in foreground\n" - "-h\t--help\t\t\tDisplay this help message\n" - "-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" - "\n If meet any bug, please report to \n\n"); + fprintf(stderr, "Usage: bbsd [-fhv] [...]\n\n" + "-f\t--foreground\t\tForce program run in foreground\n" + "-h\t--help\t\t\tDisplay this help message\n" + "-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" + "\n If meet any bug, please report to \n\n"); } -void arg_error(void) +static void arg_error(void) { - prints("Invalid arguments\n"); + fprintf(stderr, "Invalid arguments\n"); app_help(); } @@ -55,6 +57,9 @@ int main(int argc, char *argv[]) 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++) @@ -75,7 +80,7 @@ int main(int argc, char *argv[]) app_help(); return 0; case 'v': - puts(app_version); + puts(APP_INFO); return 0; default: arg_error(); @@ -97,7 +102,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) @@ -134,19 +139,29 @@ 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) { @@ -190,13 +205,7 @@ int main(int argc, char *argv[]) } // Load menus - p_bbs_menu = calloc(1, sizeof(MENU_SET)); - if (p_bbs_menu == NULL) - { - log_error("OOM: calloc(MENU_SET)\n"); - goto cleanup; - } - if (load_menu(p_bbs_menu, CONF_MENU) < 0) + if (load_menu(&bbs_menu, CONF_MENU) < 0) { goto cleanup; } @@ -215,26 +224,49 @@ int main(int argc, char *argv[]) } } - // Load section config - if (load_section_config_from_db() < 0) + // Load section config and gen_ex + if (load_section_config_from_db(1) < 0) { - log_error("load_section_config_from_db() error\n"); + log_error("load_section_config_from_db(0) error\n"); goto cleanup; } + set_last_article_op_log_from_db(); + last_aid = 0; + // Load section articles - if (append_articles_from_db(0, 1) < 0) + do { - log_error("append_articles_from_db(0, 1) error\n"); - goto cleanup; - } + 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; + } - log_std("Initially load %d articles, last_aid = %d\n", article_block_article_count(), article_block_last_aid()); + 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) @@ -251,9 +283,7 @@ cleanup: file_loader_cleanup(); // Cleanup menu - unload_menu(p_bbs_menu); - free(p_bbs_menu); - p_bbs_menu = NULL; + unload_menu(&bbs_menu); // Cleanup data pools section_list_cleanup(); @@ -273,7 +303,9 @@ cleanup: log_error("unlink(%s) error\n", VAR_SECTION_LIST_SHM); } - log_std("Main process exit normally\n"); + log_common("Main process exit normally\n"); + + log_end(); return 0; }