--- lbbs/src/main.c 2025/05/07 05:47:49 1.25 +++ lbbs/src/main.c 2025/05/15 05:14:57 1.32 @@ -70,13 +70,13 @@ int main(int argc, char *argv[]) break; case 'h': app_help(); - exit(0); + return 0; case 'v': puts(app_version); - exit(0); + return 0; default: arg_error(); - exit(1); + return 1; } } } @@ -90,12 +90,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); + return 0; } if (strcmp(argv[i] + 2, "display-log") == 0) { @@ -126,43 +126,52 @@ int main(int argc, char *argv[]) // Initialize log if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0) { - exit(-1); + return -1; } if ((!daemon) && std_log_redir) { - log_std_redirect(2); + log_std_redirect(STDERR_FILENO); } if ((!daemon) && error_log_redir) { - log_err_redirect(3); + log_err_redirect(STDERR_FILENO); } // Load configuration - if (load_conf("conf/bbsd.conf") < 0) - exit(-2); + if (load_conf(CONF_BBSD) < 0) + { + return -2; + } // Load menus - if (load_menu(&bbs_menu, "conf/menu.conf") < 0) - exit(-3); + p_bbs_menu = calloc(1, sizeof(MENU_SET)); + if (p_bbs_menu == NULL) + { + log_error("OOM: calloc(MENU_SET)\n"); + return -3; + } + if (load_menu(p_bbs_menu, CONF_MENU) < 0) + { + unload_menu(p_bbs_menu); + return -3; + } // Set signal handler - signal(SIGCHLD, child_exit); - signal(SIGTERM, system_exit); - signal(SIG_RELOAD_MENU, reload_bbs_menu); + signal(SIGHUP, sig_hup_handler); + signal(SIGCHLD, sig_chld_handler); + signal(SIGTERM, sig_term_handler); // Initialize socket server - net_server(BBS_address, BBS_port); - - // Wait for child process exit - while (SYS_child_process_count > 0) + if (net_server(BBS_address, BBS_port) < 0) { - log_std("Waiting for %d child process to exit\n", SYS_child_process_count); - sleep(1); + return -4; } - // Cleanup - unload_menu(&bbs_menu); + // Cleanup menu + unload_menu(p_bbs_menu); + free(p_bbs_menu); + p_bbs_menu = NULL; log_std("Main process exit normally\n");