--- lbbs/src/main.c 2025/04/29 03:38:56 1.19 +++ lbbs/src/main.c 2025/11/02 08:13:50 1.68 @@ -1,61 +1,76 @@ /*************************************************************************** main.c - description ------------------- - begin : Mon Oct 11 2004 - copyright : (C) 2004 by Leaflet - email : leaflet@leafok.com + 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 2 of the License, or * + * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "bbs.h" #include "common.h" +#include "file_loader.h" +#include "init.h" #include "io.h" +#include "log.h" #include "menu.h" +#include "net_server.h" +#include "section_list_loader.h" +#include "user_list.h" +#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(); } int main(int argc, char *argv[]) { - char log_dir[256], file_log_std[256], file_log_error[256], file_config[256]; - int i, j; - int daemon = 1, std_log_redir = 0, error_log_redir = 0; + char file_path_temp[FILE_PATH_LEN]; + 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 (i = 1; i < argc; i++) + for (int i = 1; i < argc; i++) { switch (argv[i][0]) { case '-': if (argv[i][1] != '-') { - for (j = 1; j < strlen(argv[i]); j++) + for (int j = 1; j < strlen(argv[i]); j++) { switch (argv[i][j]) { @@ -64,13 +79,13 @@ int main(int argc, char *argv[]) break; case 'h': app_help(); - exit(0); + return 0; case 'v': - puts(app_version); - exit(0); + puts(APP_INFO); + return 0; default: arg_error(); - exit(1); + return 1; } } } @@ -84,12 +99,12 @@ int main(int argc, char *argv[]) if (strcmp(argv[i] + 2, "help") == 0) { app_help(); - exit(0); + return 0; } if (strcmp(argv[i] + 2, "version") == 0) { - puts(app_version); - exit(0); + puts(APP_INFO); + return 0; } if (strcmp(argv[i] + 2, "display-log") == 0) { @@ -106,61 +121,250 @@ int main(int argc, char *argv[]) // Initialize daemon if (daemon) + { init_daemon(); + } // Change current dir - strncpy(app_home_dir, argv[0], rindex(argv[0], '/') - argv[0] + 1); - strcat(app_home_dir, "../"); - chdir(app_home_dir); + strncpy(file_path_temp, argv[0], sizeof(file_path_temp) - 1); + file_path_temp[sizeof(file_path_temp) - 1] = '\0'; + + if (chdir(dirname(file_path_temp)) < 0) + { + fprintf(stderr, "chdir(%s) error: %d\n", dirname(file_path_temp), errno); + return -1; + } + if (chdir("..") < 0) + { + fprintf(stderr, "chdir(..) error: %d\n", errno); + return -1; + } // Initialize log - strcpy(app_temp_dir, "var/"); - mkdir(app_temp_dir, 0750); - strcpy(log_dir, app_home_dir); - strcat(log_dir, "log/"); - strcpy(file_log_std, log_dir); - strcpy(file_log_error, log_dir); - strcat(file_log_std, "bbsd.log"); - strcat(file_log_error, "error.log"); - mkdir(log_dir, 0750); - if (log_begin(file_log_std, file_log_error) < 0) - exit(-1); + if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0) + { + return -1; + } if ((!daemon) && std_log_redir) - log_std_redirect(2); + { + log_common_redir(STDERR_FILENO); + } if ((!daemon) && error_log_redir) - log_err_redirect(3); + { + log_error_redir(STDERR_FILENO); + } + + log_common("Starting %s\n", APP_INFO); // Load configuration - strcpy(file_config, app_home_dir); - strcat(file_config, "conf/bbsd.conf"); - if (load_conf(file_config) < 0) - exit(-2); + 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; + } + + // 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) + { + 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 ((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(%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) + { + 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 - strcpy(file_config, app_home_dir); - strcat(file_config, "conf/menu.conf"); - if (load_menu(&bbs_menu, file_config) < 0) - exit(-3); + 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) + { + 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(%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) + { + 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 + 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()); + + if ((ret = user_stat_update()) < 0) + { + log_error("user_stat_update() error\n"); + goto cleanup; + } // Set signal handler - signal(SIGCHLD, child_exit); - signal(SIGTERM, system_exit); - signal(SIG_RELOAD_MENU, reload_bbs_menu); + 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 net_server(BBS_address, BBS_port); - // Wait for child process exit - while (SYS_child_process_count > 0) +cleanup: + // Cleanup loaded data files + file_loader_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(); + + if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0) { - log_std("."); - sleep(1); + log_error("unlink(%s) error\n", VAR_ARTICLE_BLOCK_SHM); + } + if (unlink(VAR_SECTION_LIST_SHM) < 0) + { + 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); } - // Cleanup - unload_menu(&bbs_menu); - rmdir(app_temp_dir); + log_common("Main process exit normally\n"); + + log_end(); return 0; }