| 16 |
#include "file_loader.h" |
#include "file_loader.h" |
| 17 |
#include "init.h" |
#include "init.h" |
| 18 |
#include "io.h" |
#include "io.h" |
| 19 |
|
#include "lml.h" |
| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include "menu.h" |
#include "menu.h" |
| 22 |
#include "net_server.h" |
#include "net_server.h" |
| 219 |
log_error_redir(STDERR_FILENO); |
log_error_redir(STDERR_FILENO); |
| 220 |
} |
} |
| 221 |
|
|
| 222 |
log_common("Starting %s\n", APP_INFO); |
log_common("Starting %s", APP_INFO); |
| 223 |
|
|
| 224 |
// Load configuration |
// Load configuration |
| 225 |
if (load_conf(CONF_BBSD) < 0) |
if (load_conf(CONF_BBSD) < 0) |
| 236 |
// Get EULA modification tm |
// Get EULA modification tm |
| 237 |
if (stat(DATA_EULA, &file_stat) == -1) |
if (stat(DATA_EULA, &file_stat) == -1) |
| 238 |
{ |
{ |
| 239 |
log_error("stat(%s) error\n", DATA_EULA, errno); |
log_error("stat(%s) error", DATA_EULA, errno); |
| 240 |
goto cleanup; |
goto cleanup; |
| 241 |
} |
} |
| 242 |
BBS_eula_tm = file_stat.st_mtim.tv_sec; |
BBS_eula_tm = file_stat.st_mtim.tv_sec; |
| 245 |
ret = mkdir(VAR_ARTICLE_CACHE_DIR, 0750); |
ret = mkdir(VAR_ARTICLE_CACHE_DIR, 0750); |
| 246 |
if (ret == -1 && errno != EEXIST) |
if (ret == -1 && errno != EEXIST) |
| 247 |
{ |
{ |
| 248 |
log_error("mkdir(%s) error (%d)\n", VAR_ARTICLE_CACHE_DIR, errno); |
log_error("mkdir(%s) error (%d)", VAR_ARTICLE_CACHE_DIR, errno); |
| 249 |
goto cleanup; |
goto cleanup; |
| 250 |
} |
} |
| 251 |
|
|
| 253 |
ret = mkdir(VAR_SECTION_AID_LOC_DIR, 0750); |
ret = mkdir(VAR_SECTION_AID_LOC_DIR, 0750); |
| 254 |
if (ret == -1 && errno != EEXIST) |
if (ret == -1 && errno != EEXIST) |
| 255 |
{ |
{ |
| 256 |
log_error("mkdir(%s) error (%d)\n", VAR_SECTION_AID_LOC_DIR, errno); |
log_error("mkdir(%s) error (%d)", VAR_SECTION_AID_LOC_DIR, errno); |
| 257 |
goto cleanup; |
goto cleanup; |
| 258 |
} |
} |
| 259 |
|
|
| 260 |
// Initialize data pools |
// Initialize data pools |
| 261 |
if ((fp = fopen(VAR_ARTICLE_BLOCK_SHM, "w")) == NULL) |
if ((fp = fopen(VAR_ARTICLE_BLOCK_SHM, "w")) == NULL) |
| 262 |
{ |
{ |
| 263 |
log_error("fopen(%s) error\n", VAR_ARTICLE_BLOCK_SHM); |
log_error("fopen(%s) error", VAR_ARTICLE_BLOCK_SHM); |
| 264 |
goto cleanup; |
goto cleanup; |
| 265 |
} |
} |
| 266 |
fclose(fp); |
fclose(fp); |
| 267 |
if ((fp = fopen(VAR_SECTION_LIST_SHM, "w")) == NULL) |
if ((fp = fopen(VAR_SECTION_LIST_SHM, "w")) == NULL) |
| 268 |
{ |
{ |
| 269 |
log_error("fopen(%s) error\n", VAR_SECTION_LIST_SHM); |
log_error("fopen(%s) error", VAR_SECTION_LIST_SHM); |
| 270 |
goto cleanup; |
goto cleanup; |
| 271 |
} |
} |
| 272 |
fclose(fp); |
fclose(fp); |
| 273 |
if ((fp = fopen(VAR_TRIE_DICT_SHM, "w")) == NULL) |
if ((fp = fopen(VAR_TRIE_DICT_SHM, "w")) == NULL) |
| 274 |
{ |
{ |
| 275 |
log_error("fopen(%s) error\n", VAR_TRIE_DICT_SHM); |
log_error("fopen(%s) error", VAR_TRIE_DICT_SHM); |
| 276 |
goto cleanup; |
goto cleanup; |
| 277 |
} |
} |
| 278 |
fclose(fp); |
fclose(fp); |
| 279 |
if ((fp = fopen(VAR_USER_LIST_SHM, "w")) == NULL) |
if ((fp = fopen(VAR_USER_LIST_SHM, "w")) == NULL) |
| 280 |
{ |
{ |
| 281 |
log_error("fopen(%s) error\n", VAR_USER_LIST_SHM); |
log_error("fopen(%s) error", VAR_USER_LIST_SHM); |
| 282 |
goto cleanup; |
goto cleanup; |
| 283 |
} |
} |
| 284 |
fclose(fp); |
fclose(fp); |
| 285 |
|
|
| 286 |
if (trie_dict_init(VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL) < 0) |
if (trie_dict_init(VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL) < 0) |
| 287 |
{ |
{ |
| 288 |
log_error("trie_dict_init(%s, %d) error\n", VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL); |
log_error("trie_dict_init(%s, %d) error", VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL); |
| 289 |
goto cleanup; |
goto cleanup; |
| 290 |
} |
} |
| 291 |
if (article_block_init(VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block) < 0) |
if (article_block_init(VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block) < 0) |
| 292 |
{ |
{ |
| 293 |
log_error("article_block_init(%s, %d) error\n", VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block); |
log_error("article_block_init(%s, %d) error", VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block); |
| 294 |
goto cleanup; |
goto cleanup; |
| 295 |
} |
} |
| 296 |
if (section_list_init(VAR_SECTION_LIST_SHM) < 0) |
if (section_list_init(VAR_SECTION_LIST_SHM) < 0) |
| 297 |
{ |
{ |
| 298 |
log_error("section_list_pool_init(%s) error\n", VAR_SECTION_LIST_SHM); |
log_error("section_list_pool_init(%s) error", VAR_SECTION_LIST_SHM); |
| 299 |
|
goto cleanup; |
| 300 |
|
} |
| 301 |
|
|
| 302 |
|
// Init LML module |
| 303 |
|
if (lml_init() < 0) |
| 304 |
|
{ |
| 305 |
|
log_error("lml_init() error"); |
| 306 |
goto cleanup; |
goto cleanup; |
| 307 |
} |
} |
| 308 |
|
|
| 309 |
// Load BBS cmd |
// Load BBS cmd |
| 310 |
if (load_cmd() < 0) |
if (load_cmd() < 0) |
| 311 |
{ |
{ |
| 312 |
|
log_error("load_cmd() error"); |
| 313 |
goto cleanup; |
goto cleanup; |
| 314 |
} |
} |
| 315 |
|
|
| 316 |
// Load menus |
// Load menus |
| 317 |
if (load_menu(&bbs_menu, CONF_MENU) < 0) |
if (load_menu(&bbs_menu, CONF_MENU) < 0) |
| 318 |
{ |
{ |
| 319 |
log_error("load_menu(bbs_menu) error\n"); |
log_error("load_menu(bbs_menu) error"); |
| 320 |
goto cleanup; |
goto cleanup; |
| 321 |
} |
} |
| 322 |
if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0) |
if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0) |
| 323 |
{ |
{ |
| 324 |
log_error("load_menu(top10_menu) error\n"); |
log_error("load_menu(top10_menu) error"); |
| 325 |
goto cleanup; |
goto cleanup; |
| 326 |
} |
} |
| 327 |
top10_menu.allow_exit = 1; |
top10_menu.allow_exit = 1; |
| 331 |
{ |
{ |
| 332 |
if (load_file(data_files_load_startup[i]) < 0) |
if (load_file(data_files_load_startup[i]) < 0) |
| 333 |
{ |
{ |
| 334 |
log_error("load_file(%s) error\n", data_files_load_startup[i]); |
log_error("load_file(%s) error", data_files_load_startup[i]); |
| 335 |
} |
} |
| 336 |
} |
} |
| 337 |
|
|
| 338 |
// Load user_list and online_user_list |
// Load user_list and online_user_list |
| 339 |
if (user_list_pool_init(VAR_USER_LIST_SHM) < 0) |
if (user_list_pool_init(VAR_USER_LIST_SHM) < 0) |
| 340 |
{ |
{ |
| 341 |
log_error("user_list_pool_init(%s) error\n", VAR_USER_LIST_SHM); |
log_error("user_list_pool_init(%s) error", VAR_USER_LIST_SHM); |
| 342 |
goto cleanup; |
goto cleanup; |
| 343 |
} |
} |
| 344 |
if (user_list_pool_reload(0) < 0) |
if (user_list_pool_reload(0) < 0) |
| 345 |
{ |
{ |
| 346 |
log_error("user_list_pool_reload(all_user) error\n"); |
log_error("user_list_pool_reload(all_user) error"); |
| 347 |
goto cleanup; |
goto cleanup; |
| 348 |
} |
} |
| 349 |
if (user_list_pool_reload(1) < 0) |
if (user_list_pool_reload(1) < 0) |
| 350 |
{ |
{ |
| 351 |
log_error("user_list_pool_reload(online_user) error\n"); |
log_error("user_list_pool_reload(online_user) error"); |
| 352 |
goto cleanup; |
goto cleanup; |
| 353 |
} |
} |
| 354 |
|
|
| 355 |
// Load section config and gen_ex |
// Load section config and gen_ex |
| 356 |
if (load_section_config_from_db(1) < 0) |
if (load_section_config_from_db(1) < 0) |
| 357 |
{ |
{ |
| 358 |
log_error("load_section_config_from_db(0) error\n"); |
log_error("load_section_config_from_db(0) error"); |
| 359 |
goto cleanup; |
goto cleanup; |
| 360 |
} |
} |
| 361 |
|
|
| 367 |
{ |
{ |
| 368 |
if ((ret = append_articles_from_db(last_aid + 1, 1, LOAD_ARTICLE_COUNT_LIMIT)) < 0) |
if ((ret = append_articles_from_db(last_aid + 1, 1, LOAD_ARTICLE_COUNT_LIMIT)) < 0) |
| 369 |
{ |
{ |
| 370 |
log_error("append_articles_from_db(0, 1, %d) error\n", LOAD_ARTICLE_COUNT_LIMIT); |
log_error("append_articles_from_db(0, 1, %d) error", LOAD_ARTICLE_COUNT_LIMIT); |
| 371 |
goto cleanup; |
goto cleanup; |
| 372 |
} |
} |
| 373 |
|
|
| 374 |
last_aid = article_block_last_aid(); |
last_aid = article_block_last_aid(); |
| 375 |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
| 376 |
|
|
| 377 |
log_common("Initially load %d articles, last_aid = %d\n", article_block_article_count(), article_block_last_aid()); |
log_common("Initially load %d articles, last_aid = %d", article_block_article_count(), article_block_last_aid()); |
| 378 |
|
|
| 379 |
if ((ret = user_stat_update()) < 0) |
if ((ret = user_stat_update()) < 0) |
| 380 |
{ |
{ |
| 381 |
log_error("user_stat_update() error\n"); |
log_error("user_stat_update() error"); |
| 382 |
goto cleanup; |
goto cleanup; |
| 383 |
} |
} |
| 384 |
|
|
| 386 |
act.sa_handler = sig_hup_handler; |
act.sa_handler = sig_hup_handler; |
| 387 |
if (sigaction(SIGHUP, &act, NULL) == -1) |
if (sigaction(SIGHUP, &act, NULL) == -1) |
| 388 |
{ |
{ |
| 389 |
log_error("set signal action of SIGHUP error: %d\n", errno); |
log_error("set signal action of SIGHUP error: %d", errno); |
| 390 |
goto cleanup; |
goto cleanup; |
| 391 |
} |
} |
| 392 |
act.sa_handler = sig_chld_handler; |
act.sa_handler = sig_chld_handler; |
| 393 |
if (sigaction(SIGCHLD, &act, NULL) == -1) |
if (sigaction(SIGCHLD, &act, NULL) == -1) |
| 394 |
{ |
{ |
| 395 |
log_error("set signal action of SIGCHLD error: %d\n", errno); |
log_error("set signal action of SIGCHLD error: %d", errno); |
| 396 |
goto cleanup; |
goto cleanup; |
| 397 |
} |
} |
| 398 |
act.sa_handler = sig_term_handler; |
act.sa_handler = sig_term_handler; |
| 399 |
if (sigaction(SIGTERM, &act, NULL) == -1) |
if (sigaction(SIGTERM, &act, NULL) == -1) |
| 400 |
{ |
{ |
| 401 |
log_error("set signal action of SIGTERM error: %d\n", errno); |
log_error("set signal action of SIGTERM error: %d", errno); |
| 402 |
goto cleanup; |
goto cleanup; |
| 403 |
} |
} |
| 404 |
act.sa_handler = SIG_IGN; |
act.sa_handler = SIG_IGN; |
| 405 |
if (sigaction(SIGPIPE, &act, NULL) == -1) |
if (sigaction(SIGPIPE, &act, NULL) == -1) |
| 406 |
{ |
{ |
| 407 |
log_error("set signal action of SIGPIPE error: %d\n", errno); |
log_error("set signal action of SIGPIPE error: %d", errno); |
| 408 |
goto cleanup; |
goto cleanup; |
| 409 |
} |
} |
| 410 |
act.sa_handler = SIG_IGN; |
act.sa_handler = SIG_IGN; |
| 411 |
if (sigaction(SIGUSR1, &act, NULL) == -1) |
if (sigaction(SIGUSR1, &act, NULL) == -1) |
| 412 |
{ |
{ |
| 413 |
log_error("set signal action of SIGUSR1 error: %d\n", errno); |
log_error("set signal action of SIGUSR1 error: %d", errno); |
| 414 |
goto cleanup; |
goto cleanup; |
| 415 |
} |
} |
| 416 |
|
|
| 417 |
// Launch section_list_loader process |
// Launch section_list_loader process |
| 418 |
if (section_list_loader_launch() < 0) |
if (section_list_loader_launch() < 0) |
| 419 |
{ |
{ |
| 420 |
log_error("section_list_loader_launch() error\n"); |
log_error("section_list_loader_launch() error"); |
| 421 |
goto cleanup; |
goto cleanup; |
| 422 |
} |
} |
| 423 |
|
|
| 432 |
|
|
| 433 |
if (kill(section_list_loader_pid, SIGTERM) < 0) |
if (kill(section_list_loader_pid, SIGTERM) < 0) |
| 434 |
{ |
{ |
| 435 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)", errno); |
| 436 |
} |
} |
| 437 |
|
|
| 438 |
for (i = 0; SYS_child_exit == 0 && i < 5; i++) |
for (i = 0; SYS_child_exit == 0 && i < 5; i++) |
| 442 |
|
|
| 443 |
if ((ret = waitpid(section_list_loader_pid, NULL, WNOHANG)) < 0) |
if ((ret = waitpid(section_list_loader_pid, NULL, WNOHANG)) < 0) |
| 444 |
{ |
{ |
| 445 |
log_error("waitpid(%d) error (%d)\n", section_list_loader_pid, errno); |
log_error("waitpid(%d) error (%d)", section_list_loader_pid, errno); |
| 446 |
} |
} |
| 447 |
else if (ret == 0) |
else if (ret == 0) |
| 448 |
{ |
{ |
| 449 |
log_common("Force kill section_list_loader process (%d)\n", section_list_loader_pid); |
log_common("Force kill section_list_loader process (%d)", section_list_loader_pid); |
| 450 |
if (kill(section_list_loader_pid, SIGKILL) < 0) |
if (kill(section_list_loader_pid, SIGKILL) < 0) |
| 451 |
{ |
{ |
| 452 |
log_error("Send SIGKILL signal failed (%d)\n", errno); |
log_error("Send SIGKILL signal failed (%d)", errno); |
| 453 |
} |
} |
| 454 |
} |
} |
| 455 |
} |
} |
| 459 |
{ |
{ |
| 460 |
if (unload_file(data_files_load_startup[i]) < 0) |
if (unload_file(data_files_load_startup[i]) < 0) |
| 461 |
{ |
{ |
| 462 |
log_error("unload_file(%s) error\n", data_files_load_startup[i]); |
log_error("unload_file(%s) error", data_files_load_startup[i]); |
| 463 |
} |
} |
| 464 |
} |
} |
| 465 |
|
|
| 467 |
unload_menu(&bbs_menu); |
unload_menu(&bbs_menu); |
| 468 |
unload_menu(&top10_menu); |
unload_menu(&top10_menu); |
| 469 |
|
|
| 470 |
|
// Cleanup LML module |
| 471 |
|
lml_cleanup(); |
| 472 |
|
|
| 473 |
// Cleanup data pools |
// Cleanup data pools |
| 474 |
section_list_cleanup(); |
section_list_cleanup(); |
| 475 |
article_block_cleanup(); |
article_block_cleanup(); |
| 478 |
|
|
| 479 |
if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0) |
if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0) |
| 480 |
{ |
{ |
| 481 |
log_error("unlink(%s) error\n", VAR_ARTICLE_BLOCK_SHM); |
log_error("unlink(%s) error", VAR_ARTICLE_BLOCK_SHM); |
| 482 |
} |
} |
| 483 |
if (unlink(VAR_SECTION_LIST_SHM) < 0) |
if (unlink(VAR_SECTION_LIST_SHM) < 0) |
| 484 |
{ |
{ |
| 485 |
log_error("unlink(%s) error\n", VAR_SECTION_LIST_SHM); |
log_error("unlink(%s) error", VAR_SECTION_LIST_SHM); |
| 486 |
} |
} |
| 487 |
if (unlink(VAR_TRIE_DICT_SHM) < 0) |
if (unlink(VAR_TRIE_DICT_SHM) < 0) |
| 488 |
{ |
{ |
| 489 |
log_error("unlink(%s) error\n", VAR_TRIE_DICT_SHM); |
log_error("unlink(%s) error", VAR_TRIE_DICT_SHM); |
| 490 |
} |
} |
| 491 |
if (unlink(VAR_USER_LIST_SHM) < 0) |
if (unlink(VAR_USER_LIST_SHM) < 0) |
| 492 |
{ |
{ |
| 493 |
log_error("unlink(%s) error\n", VAR_SECTION_LIST_SHM); |
log_error("unlink(%s) error", VAR_SECTION_LIST_SHM); |
| 494 |
} |
} |
| 495 |
|
|
| 496 |
log_common("Main process exit normally\n"); |
log_common("Main process exit normally"); |
| 497 |
|
|
| 498 |
log_end(); |
log_end(); |
| 499 |
|
|