--- lbbs/src/main.c 2025/04/29 03:38:56 1.19 +++ lbbs/src/main.c 2025/05/08 08:05:58 1.26 @@ -1,28 +1,33 @@ /*************************************************************************** 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 "init.h" #include "common.h" +#include "net_server.h" +#include "log.h" #include "io.h" #include "menu.h" +#include #include #include -#include #include +#include +#include +#include void app_help(void) { @@ -43,19 +48,20 @@ void arg_error(void) 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; // 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]) { @@ -106,42 +112,44 @@ 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'; + + chdir(dirname(file_path_temp)); + chdir(".."); // 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) + if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0) + { exit(-1); + } if ((!daemon) && std_log_redir) + { log_std_redirect(2); + } if ((!daemon) && error_log_redir) + { log_err_redirect(3); + } // Load configuration - strcpy(file_config, app_home_dir); - strcat(file_config, "conf/bbsd.conf"); - if (load_conf(file_config) < 0) + if (load_conf(CONF_BBSD) < 0) + { exit(-2); + } // Load menus - strcpy(file_config, app_home_dir); - strcat(file_config, "conf/menu.conf"); - if (load_menu(&bbs_menu, file_config) < 0) + if (load_menu(&bbs_menu, CONF_MENU) < 0) + { + unload_menu(&bbs_menu); exit(-3); + } // Set signal handler signal(SIGCHLD, child_exit); @@ -154,13 +162,14 @@ int main(int argc, char *argv[]) // Wait for child process exit while (SYS_child_process_count > 0) { - log_std("."); + log_std("Waiting for %d child process to exit\n", SYS_child_process_count); sleep(1); } // Cleanup unload_menu(&bbs_menu); - rmdir(app_temp_dir); + log_std("Main process exit normally\n"); + return 0; }