--- lbbs/src/main.c 2004/10/22 15:20:32 1.7 +++ lbbs/src/main.c 2026/01/08 14:22:57 1.94 @@ -1,138 +1,528 @@ -/*************************************************************************** - main.c - description - ------------------- - begin : Mon Oct 11 2004 - copyright : (C) 2004 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 * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * main + * - entry of server program + * + * Copyright (C) 2004-2026 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "bbs.h" +#include "bwf.h" #include "common.h" +#include "file_loader.h" +#include "init.h" #include "io.h" +#include "lml.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 +#include + +typedef void (*arg_option_handler_t)(void); + +struct arg_option_t +{ + const char *short_arg; + const char *long_arg; + const char *description; + arg_option_handler_t handler; +}; +typedef struct arg_option_t ARG_OPTION; + +static void arg_foreground(void); +static void arg_help(void); +static void arg_version(void); +static void arg_display_log(void); +static void arg_display_error_log(void); +static void arg_compile_info(void); +static void arg_error(void); + +static const ARG_OPTION arg_options[] = { + {"-f", "--foreground", "Run in foreground", arg_foreground}, + {"-h", "--help", "Display help message", arg_help}, + {"-v", "--version", "Display version information", arg_version}, + {NULL, "--display-log", "Display standard log information", arg_display_log}, + {NULL, "--display-error-log", "Display error log information", arg_display_error_log}, + {"-C", "--compile-config", "Display compile configuration", arg_compile_info}}; + +static const int arg_options_count = sizeof(arg_options) / sizeof(ARG_OPTION); + +static int daemon_service = 1; +static int std_log_redir = 0; +static int error_log_redir = 0; + +static void arg_foreground(void) +{ + daemon_service = 0; +} + +static void arg_usage(void) +{ + fprintf(stderr, "Usage: bbsd [-fhvC] [...]\n\n"); + + for (int i = 0; i < arg_options_count; i++) + { + fprintf(stderr, "%s%*s%s%*s%s\n", + arg_options[i].short_arg ? arg_options[i].short_arg : "", + 8 - (arg_options[i].short_arg ? (int)strlen(arg_options[i].short_arg) : 0), "", + arg_options[i].long_arg ? arg_options[i].long_arg : "", + 24 - (arg_options[i].long_arg ? (int)strlen(arg_options[i].long_arg) : 0), "", + arg_options[i].description ? arg_options[i].description : ""); + } + + fprintf(stderr, "\n If meet any bug, please report to \n\n"); +} + +static void arg_help(void) +{ + arg_usage(); + + exit(0); +} -void -app_help(void) +static void arg_version(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" - ); -} - -void -arg_error(void) -{ - prints ("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; - - //Parse args - for (i=1; i 0) + { + SYS_child_exit = 0; + + if (kill(section_list_loader_pid, SIGTERM) < 0) + { + log_error("Send SIGTERM signal failed (%d)", errno); + } + + for (i = 0; SYS_child_exit == 0 && i < 5; i++) + { + sleep(1); // second + } + + if ((ret = waitpid(section_list_loader_pid, NULL, WNOHANG)) < 0) + { + log_error("waitpid(%d) error (%d)", section_list_loader_pid, errno); + } + else if (ret == 0) + { + log_common("Force kill section_list_loader process (%d)", section_list_loader_pid); + if (kill(section_list_loader_pid, SIGKILL) < 0) + { + log_error("Send SIGKILL signal failed (%d)", errno); + } + } + } + + // Cleanup loaded data files + for (int i = 0; i < data_files_load_startup_count; i++) + { + if (unload_file(data_files_load_startup[i]) < 0) + { + log_error("unload_file(%s) error", data_files_load_startup[i]); + } + } + + // Cleanup menu + unload_menu(&bbs_menu); + unload_menu(&top10_menu); + + // Cleanup LML module + lml_cleanup(); + + // Cleanup data pools + section_list_cleanup(); + article_block_cleanup(); + trie_dict_cleanup(); + user_list_pool_cleanup(); + + if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0) + { + log_error("unlink(%s) error", VAR_ARTICLE_BLOCK_SHM); + } + if (unlink(VAR_SECTION_LIST_SHM) < 0) + { + log_error("unlink(%s) error", VAR_SECTION_LIST_SHM); + } + if (unlink(VAR_TRIE_DICT_SHM) < 0) + { + log_error("unlink(%s) error", VAR_TRIE_DICT_SHM); + } + if (unlink(VAR_USER_LIST_SHM) < 0) + { + log_error("unlink(%s) error", VAR_SECTION_LIST_SHM); + } + + log_common("Main process exit normally"); - //Initialize socket server - net_server(BBS_address, BBS_port); + log_end(); - return 0; + return 0; }