| 17 |
#include "section_list_loader.h" |
#include "section_list_loader.h" |
| 18 |
#include "log.h" |
#include "log.h" |
| 19 |
#include "database.h" |
#include "database.h" |
| 20 |
|
#include "menu.h" |
| 21 |
#include <stdio.h> |
#include <stdio.h> |
| 22 |
#include <string.h> |
#include <string.h> |
| 23 |
#include <errno.h> |
#include <errno.h> |
| 24 |
|
#include <signal.h> |
| 25 |
#include <stdlib.h> |
#include <stdlib.h> |
| 26 |
#include <strings.h> |
#include <strings.h> |
| 27 |
|
#include <unistd.h> |
| 28 |
|
|
| 29 |
|
#define SECTION_LIST_LOAD_INTERVAL 10 // second |
| 30 |
|
|
| 31 |
|
static int section_list_loader_pid; |
| 32 |
|
|
| 33 |
int load_section_config_from_db(void) |
int load_section_config_from_db(void) |
| 34 |
{ |
{ |
| 241 |
if ((p_section = section_list_find_by_sid(article.sid)) == NULL) |
if ((p_section = section_list_find_by_sid(article.sid)) == NULL) |
| 242 |
{ |
{ |
| 243 |
log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", article.sid); |
log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", article.sid); |
| 244 |
ret = -4; // known section found |
ret = ERR_UNKNOWN_SECTION; // Unknown section found |
| 245 |
break; |
break; |
| 246 |
} |
} |
| 247 |
|
|
| 248 |
// acquire lock of current section if different from last one |
// acquire lock of current section if different from last one |
| 249 |
if (!global_lock && article.sid != last_sid) |
if (!global_lock && article.sid != last_sid) |
| 250 |
{ |
{ |
| 251 |
if ((ret = section_list_rw_lock(NULL)) < 0) |
if ((ret = section_list_rw_lock(p_section)) < 0) |
| 252 |
{ |
{ |
| 253 |
log_error("section_list_rw_lock(sid = 0) error\n"); |
log_error("section_list_rw_lock(sid = 0) error\n"); |
| 254 |
break; |
break; |
| 292 |
|
|
| 293 |
return ret; |
return ret; |
| 294 |
} |
} |
| 295 |
|
|
| 296 |
|
int section_list_loader_launch(void) |
| 297 |
|
{ |
| 298 |
|
int pid; |
| 299 |
|
int ret; |
| 300 |
|
int32_t last_aid; |
| 301 |
|
int article_count; |
| 302 |
|
int load_count; |
| 303 |
|
int i; |
| 304 |
|
|
| 305 |
|
if (section_list_loader_pid != 0) |
| 306 |
|
{ |
| 307 |
|
log_error("section_list_loader already running, pid = %d\n", section_list_loader_pid); |
| 308 |
|
return -2; |
| 309 |
|
} |
| 310 |
|
|
| 311 |
|
pid = fork(); |
| 312 |
|
|
| 313 |
|
if (pid > 0) // Parent process |
| 314 |
|
{ |
| 315 |
|
SYS_child_process_count++; |
| 316 |
|
section_list_loader_pid = pid; |
| 317 |
|
log_std("Section list loader process (%d) start\n", pid); |
| 318 |
|
return 0; |
| 319 |
|
} |
| 320 |
|
else if (pid < 0) // Error |
| 321 |
|
{ |
| 322 |
|
log_error("fork() error (%d)\n", errno); |
| 323 |
|
return -1; |
| 324 |
|
} |
| 325 |
|
|
| 326 |
|
// Child process |
| 327 |
|
SYS_child_process_count = 0; |
| 328 |
|
|
| 329 |
|
// Detach menu in shared memory |
| 330 |
|
detach_menu_shm(p_bbs_menu); |
| 331 |
|
free(p_bbs_menu); |
| 332 |
|
p_bbs_menu = NULL; |
| 333 |
|
|
| 334 |
|
// Do section data loader periodically |
| 335 |
|
while (!SYS_server_exit) |
| 336 |
|
{ |
| 337 |
|
if (SYS_section_list_reload) |
| 338 |
|
{ |
| 339 |
|
SYS_section_list_reload = 0; |
| 340 |
|
|
| 341 |
|
// Load section config |
| 342 |
|
if (load_section_config_from_db() < 0) |
| 343 |
|
{ |
| 344 |
|
log_error("load_section_config_from_db() error\n"); |
| 345 |
|
} |
| 346 |
|
else |
| 347 |
|
{ |
| 348 |
|
log_error("Reload section config successfully\n"); |
| 349 |
|
} |
| 350 |
|
} |
| 351 |
|
|
| 352 |
|
// Load section articles |
| 353 |
|
last_aid = article_block_last_aid(); |
| 354 |
|
article_count = article_block_article_count(); |
| 355 |
|
|
| 356 |
|
if ((ret = append_articles_from_db(last_aid + 1, 0)) < 0) |
| 357 |
|
{ |
| 358 |
|
log_error("append_articles_from_db(%d, 0) error\n", last_aid + 1); |
| 359 |
|
|
| 360 |
|
if (ret == ERR_UNKNOWN_SECTION) |
| 361 |
|
{ |
| 362 |
|
SYS_section_list_reload = 1; // Force reload section_list |
| 363 |
|
} |
| 364 |
|
} |
| 365 |
|
else |
| 366 |
|
{ |
| 367 |
|
load_count = article_block_article_count() - article_count; |
| 368 |
|
|
| 369 |
|
if (load_count > 0) |
| 370 |
|
{ |
| 371 |
|
log_std("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid()); |
| 372 |
|
} |
| 373 |
|
|
| 374 |
|
for (i = 0; i < SECTION_LIST_LOAD_INTERVAL && !SYS_server_exit && !SYS_section_list_reload; i++) |
| 375 |
|
{ |
| 376 |
|
sleep(1); |
| 377 |
|
} |
| 378 |
|
} |
| 379 |
|
} |
| 380 |
|
|
| 381 |
|
// Child process exit |
| 382 |
|
|
| 383 |
|
// Detach data pools shm |
| 384 |
|
detach_section_list_shm(); |
| 385 |
|
detach_article_block_shm(); |
| 386 |
|
detach_trie_dict_shm(); |
| 387 |
|
|
| 388 |
|
log_std("Section list loader process exit normally\n"); |
| 389 |
|
log_end(); |
| 390 |
|
|
| 391 |
|
section_list_loader_pid = 0; |
| 392 |
|
|
| 393 |
|
_exit(0); |
| 394 |
|
|
| 395 |
|
return 0; |
| 396 |
|
} |
| 397 |
|
|
| 398 |
|
int section_list_loader_reload(void) |
| 399 |
|
{ |
| 400 |
|
if (section_list_loader_pid == 0) |
| 401 |
|
{ |
| 402 |
|
log_error("section_list_loader not running\n"); |
| 403 |
|
return -2; |
| 404 |
|
} |
| 405 |
|
|
| 406 |
|
if (kill(section_list_loader_pid, SIGHUP) < 0) |
| 407 |
|
{ |
| 408 |
|
log_error("Send SIGTERM signal failed (%d)\n", errno); |
| 409 |
|
return -1; |
| 410 |
|
} |
| 411 |
|
|
| 412 |
|
return 0; |
| 413 |
|
} |