| 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> |
| 53 |
int daemon = 1; |
int daemon = 1; |
| 54 |
int std_log_redir = 0; |
int std_log_redir = 0; |
| 55 |
int error_log_redir = 0; |
int error_log_redir = 0; |
|
int ret; |
|
| 56 |
|
|
| 57 |
// Parse args |
// Parse args |
| 58 |
for (int i = 1; i < argc; i++) |
for (int i = 1; i < argc; i++) |
| 114 |
// Initialize daemon |
// Initialize daemon |
| 115 |
if (daemon) |
if (daemon) |
| 116 |
{ |
{ |
| 117 |
ret = init_daemon(); |
init_daemon(); |
|
if (ret > 0) // Parent process |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
else if (ret < 0) // error |
|
|
{ |
|
|
return ret; |
|
|
} |
|
| 118 |
} |
} |
| 119 |
|
|
| 120 |
// Change current dir |
// Change current dir |
| 145 |
return -2; |
return -2; |
| 146 |
} |
} |
| 147 |
|
|
| 148 |
|
// Initialize trie dict pool |
| 149 |
|
if (trie_dict_init(CONF_BBSD, TRIE_NODE_PER_POOL) < 0) |
| 150 |
|
{ |
| 151 |
|
printf("trie_dict_init failed\n"); |
| 152 |
|
return -3; |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
// Load BBS cmd |
| 156 |
|
if (load_cmd() < 0) |
| 157 |
|
{ |
| 158 |
|
return -3; |
| 159 |
|
} |
| 160 |
|
|
| 161 |
// Load menus |
// Load menus |
| 162 |
if (load_menu(&bbs_menu, CONF_MENU) < 0) |
p_bbs_menu = calloc(1, sizeof(MENU_SET)); |
| 163 |
|
if (p_bbs_menu == NULL) |
| 164 |
|
{ |
| 165 |
|
log_error("OOM: calloc(MENU_SET)\n"); |
| 166 |
|
return -3; |
| 167 |
|
} |
| 168 |
|
if (load_menu(p_bbs_menu, CONF_MENU) < 0) |
| 169 |
{ |
{ |
| 170 |
unload_menu(&bbs_menu); |
unload_menu(p_bbs_menu); |
| 171 |
|
free(p_bbs_menu); |
| 172 |
return -3; |
return -3; |
| 173 |
} |
} |
| 174 |
|
|
| 175 |
|
// Load data files |
| 176 |
|
if (file_loader_init() < 0) |
| 177 |
|
{ |
| 178 |
|
log_error("file_loader_init() error\n"); |
| 179 |
|
return -4; |
| 180 |
|
} |
| 181 |
|
for (int i = 0; i < data_files_load_startup_count; i++) |
| 182 |
|
{ |
| 183 |
|
if (load_file(data_files_load_startup[i]) < 0) |
| 184 |
|
{ |
| 185 |
|
log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
|
|
| 189 |
|
log_std("Debug: trie dict nodes used = %d, limit = %d\n", trie_dict_used_nodes(), TRIE_NODE_PER_POOL); |
| 190 |
|
|
| 191 |
// Set signal handler |
// Set signal handler |
| 192 |
signal(SIGHUP, sig_hup_handler); |
signal(SIGHUP, sig_hup_handler); |
| 193 |
signal(SIGCHLD, sig_chld_handler); |
signal(SIGCHLD, sig_chld_handler); |
| 194 |
signal(SIGTERM, sig_term_handler); |
signal(SIGTERM, sig_term_handler); |
| 195 |
|
|
| 196 |
// Initialize socket server |
// Initialize socket server |
| 197 |
if (net_server(BBS_address, BBS_port) < 0) |
net_server(BBS_address, BBS_port); |
| 198 |
{ |
|
| 199 |
return -4; |
// Cleanup loaded data files |
| 200 |
} |
file_loader_cleanup(); |
| 201 |
|
|
| 202 |
|
// Cleanup menu |
| 203 |
|
unload_menu(p_bbs_menu); |
| 204 |
|
free(p_bbs_menu); |
| 205 |
|
p_bbs_menu = NULL; |
| 206 |
|
|
| 207 |
// Cleanup |
// Cleanup trie dict pool |
| 208 |
unload_menu(&bbs_menu); |
trie_dict_cleanup(); |
| 209 |
|
|
| 210 |
log_std("Main process exit normally\n"); |
log_std("Main process exit normally\n"); |
| 211 |
|
|