--- lbbs/src/main.c 2025/06/17 13:17:04 1.53 +++ lbbs/src/main.c 2025/10/21 06:24:51 1.63 @@ -15,37 +15,39 @@ ***************************************************************************/ #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 "user_list.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(); } @@ -79,7 +81,7 @@ int main(int argc, char *argv[]) app_help(); return 0; case 'v': - puts(APP_NAME_VER); + puts(APP_INFO); return 0; default: arg_error(); @@ -101,7 +103,7 @@ int main(int argc, char *argv[]) } if (strcmp(argv[i] + 2, "version") == 0) { - puts(APP_NAME_VER); + puts(APP_INFO); return 0; } if (strcmp(argv[i] + 2, "display-log") == 0) @@ -127,8 +129,16 @@ int main(int argc, char *argv[]) strncpy(file_path_temp, argv[0], sizeof(file_path_temp) - 1); file_path_temp[sizeof(file_path_temp) - 1] = '\0'; - chdir(dirname(file_path_temp)); - chdir(".."); + 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 if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0) @@ -145,7 +155,7 @@ int main(int argc, char *argv[]) log_error_redir(STDERR_FILENO); } - log_common("BBS Server (%s) is staring...\n", APP_NAME_VER); + log_common("Starting %s\n", APP_INFO); // Load configuration if (load_conf(CONF_BBSD) < 0) @@ -162,12 +172,6 @@ int main(int argc, char *argv[]) } // 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); @@ -180,6 +184,18 @@ 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) { @@ -204,16 +220,17 @@ int main(int argc, char *argv[]) } // Load menus - p_bbs_menu = calloc(1, sizeof(MENU_SET)); - if (p_bbs_menu == NULL) + if (load_menu(&bbs_menu, CONF_MENU) < 0) { - log_error("OOM: calloc(MENU_SET)\n"); + log_error("load_menu(%s) error\n", CONF_MENU); goto cleanup; } - if (load_menu(p_bbs_menu, CONF_MENU) < 0) + 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) @@ -229,10 +246,10 @@ 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; } @@ -253,6 +270,13 @@ int main(int argc, char *argv[]) log_common("Initially load %d articles, last_aid = %d\n", article_block_article_count(), article_block_last_aid()); + // Load user list + if (user_list_pool_init() < 0) + { + log_error("user_list_pool_init() error\n"); + goto cleanup; + } + // Set signal handler act.sa_handler = sig_hup_handler; if (sigaction(SIGHUP, &act, NULL) == -1) @@ -288,19 +312,15 @@ cleanup: file_loader_cleanup(); // Cleanup menu - unload_menu(p_bbs_menu); - free(p_bbs_menu); - p_bbs_menu = NULL; + 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_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); @@ -309,8 +329,18 @@ 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"); + log_end(); + return 0; }