| 21 |
#include "log.h" |
#include "log.h" |
| 22 |
#include "io.h" |
#include "io.h" |
| 23 |
#include "menu.h" |
#include "menu.h" |
| 24 |
|
#include "file_loader.h" |
| 25 |
#include <stdlib.h> |
#include <stdlib.h> |
| 26 |
#include <signal.h> |
#include <signal.h> |
| 27 |
#include <string.h> |
#include <string.h> |
| 71 |
break; |
break; |
| 72 |
case 'h': |
case 'h': |
| 73 |
app_help(); |
app_help(); |
| 74 |
exit(0); |
return 0; |
| 75 |
case 'v': |
case 'v': |
| 76 |
puts(app_version); |
puts(app_version); |
| 77 |
exit(0); |
return 0; |
| 78 |
default: |
default: |
| 79 |
arg_error(); |
arg_error(); |
| 80 |
exit(1); |
return 1; |
| 81 |
} |
} |
| 82 |
} |
} |
| 83 |
} |
} |
| 91 |
if (strcmp(argv[i] + 2, "help") == 0) |
if (strcmp(argv[i] + 2, "help") == 0) |
| 92 |
{ |
{ |
| 93 |
app_help(); |
app_help(); |
| 94 |
exit(0); |
return 0; |
| 95 |
} |
} |
| 96 |
if (strcmp(argv[i] + 2, "version") == 0) |
if (strcmp(argv[i] + 2, "version") == 0) |
| 97 |
{ |
{ |
| 98 |
puts(app_version); |
puts(app_version); |
| 99 |
exit(0); |
return 0; |
| 100 |
} |
} |
| 101 |
if (strcmp(argv[i] + 2, "display-log") == 0) |
if (strcmp(argv[i] + 2, "display-log") == 0) |
| 102 |
{ |
{ |
| 127 |
// Initialize log |
// Initialize log |
| 128 |
if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0) |
if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0) |
| 129 |
{ |
{ |
| 130 |
exit(-1); |
return -1; |
| 131 |
} |
} |
| 132 |
|
|
| 133 |
if ((!daemon) && std_log_redir) |
if ((!daemon) && std_log_redir) |
| 134 |
{ |
{ |
| 135 |
log_std_redirect(2); |
log_std_redirect(STDERR_FILENO); |
| 136 |
} |
} |
| 137 |
if ((!daemon) && error_log_redir) |
if ((!daemon) && error_log_redir) |
| 138 |
{ |
{ |
| 139 |
log_err_redirect(3); |
log_err_redirect(STDERR_FILENO); |
| 140 |
} |
} |
| 141 |
|
|
| 142 |
// Load configuration |
// Load configuration |
| 143 |
if (load_conf(CONF_BBSD) < 0) |
if (load_conf(CONF_BBSD) < 0) |
| 144 |
{ |
{ |
| 145 |
exit(-2); |
return -2; |
| 146 |
} |
} |
| 147 |
|
|
| 148 |
// Load menus |
// Load menus |
| 149 |
if (load_menu(&bbs_menu, CONF_MENU) < 0) |
p_bbs_menu = calloc(1, sizeof(MENU_SET)); |
| 150 |
|
if (p_bbs_menu == NULL) |
| 151 |
{ |
{ |
| 152 |
unload_menu(&bbs_menu); |
log_error("OOM: calloc(MENU_SET)\n"); |
| 153 |
exit(-3); |
return -3; |
| 154 |
|
} |
| 155 |
|
if (load_menu(p_bbs_menu, CONF_MENU) < 0) |
| 156 |
|
{ |
| 157 |
|
unload_menu(p_bbs_menu); |
| 158 |
|
free(p_bbs_menu); |
| 159 |
|
return -3; |
| 160 |
|
} |
| 161 |
|
unload_menu_shm(p_bbs_menu); |
| 162 |
|
|
| 163 |
|
// Load data files |
| 164 |
|
if (file_loader_init(FILE_MMAP_COUNT_LIMIT) < 0) |
| 165 |
|
{ |
| 166 |
|
log_error("file_loader_init() error\n"); |
| 167 |
|
return -4; |
| 168 |
|
} |
| 169 |
|
for (int i = 0; i < data_files_load_startup_count; i++) |
| 170 |
|
{ |
| 171 |
|
if (load_file_mmap(data_files_load_startup[i]) < 0) |
| 172 |
|
{ |
| 173 |
|
log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); |
| 174 |
|
} |
| 175 |
} |
} |
| 176 |
|
|
| 177 |
// Set signal handler |
// Set signal handler |
| 178 |
signal(SIGCHLD, child_exit); |
signal(SIGHUP, sig_hup_handler); |
| 179 |
signal(SIGTERM, system_exit); |
signal(SIGCHLD, sig_chld_handler); |
| 180 |
signal(SIG_RELOAD_MENU, reload_bbs_menu); |
signal(SIGTERM, sig_term_handler); |
| 181 |
|
|
| 182 |
// Initialize socket server |
// Initialize socket server |
| 183 |
net_server(BBS_address, BBS_port); |
net_server(BBS_address, BBS_port); |
| 184 |
|
|
| 185 |
// Wait for child process exit |
// Cleanup loaded data files |
| 186 |
while (SYS_child_process_count > 0) |
file_loader_cleanup(); |
| 187 |
{ |
|
| 188 |
log_std("Waiting for %d child process to exit\n", SYS_child_process_count); |
// Cleanup menu |
| 189 |
sleep(1); |
unload_menu(p_bbs_menu); |
| 190 |
} |
free(p_bbs_menu); |
| 191 |
|
p_bbs_menu = NULL; |
|
// Cleanup |
|
|
unload_menu(&bbs_menu); |
|
| 192 |
|
|
| 193 |
log_std("Main process exit normally\n"); |
log_std("Main process exit normally\n"); |
| 194 |
|
|