| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "article_cache.h" |
#include "article_cache.h" |
| 18 |
|
#include "article_view_log.h" |
| 19 |
#include "bbs.h" |
#include "bbs.h" |
| 20 |
#include "database.h" |
#include "database.h" |
| 21 |
|
#include "ip_mask.h" |
| 22 |
#include "log.h" |
#include "log.h" |
| 23 |
#include "menu.h" |
#include "menu.h" |
| 24 |
#include "section_list_loader.h" |
#include "section_list_loader.h" |
| 25 |
|
#include "user_list.h" |
| 26 |
|
#include "user_priv.h" |
| 27 |
#include <errno.h> |
#include <errno.h> |
| 28 |
#include <signal.h> |
#include <signal.h> |
| 29 |
#include <stdio.h> |
#include <stdio.h> |
| 34 |
int section_list_loader_pid; |
int section_list_loader_pid; |
| 35 |
int last_article_op_log_mid; |
int last_article_op_log_mid; |
| 36 |
|
|
| 37 |
int load_section_config_from_db(int reload) |
int load_section_config_from_db(int update_gen_ex) |
| 38 |
{ |
{ |
| 39 |
MYSQL *db = NULL; |
MYSQL *db = NULL; |
| 40 |
MYSQL_RES *rs = NULL, *rs2 = NULL; |
MYSQL_RES *rs = NULL, *rs2 = NULL; |
| 138 |
break; |
break; |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1); |
if (section_list_update(p_section, row[1], row[2], master_list) < 0) |
| 142 |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
{ |
| 143 |
strncpy(p_section->stitle, row[2], sizeof(p_section->stitle) - 1); |
log_error("section_list_update(sid=%d) error\n", p_section->sid); |
| 144 |
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
ret = -4; |
| 145 |
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
break; |
| 146 |
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
} |
| 147 |
} |
} |
| 148 |
|
|
| 149 |
p_section->class_id = atoi(row[3]); |
p_section->class_id = atoi(row[3]); |
| 152 |
p_section->enable = (int8_t)atoi(row[6]); |
p_section->enable = (int8_t)atoi(row[6]); |
| 153 |
|
|
| 154 |
// Update gen_ex menu set |
// Update gen_ex menu set |
| 155 |
if (reload && p_section->enable && atoi(row[7]) > p_section->ex_menu_tm) |
if (update_gen_ex && p_section->enable && atol(row[7] == NULL ? "0" : row[7]) > p_section->ex_menu_tm) |
| 156 |
{ |
{ |
| 157 |
snprintf(ex_menu_conf, sizeof(ex_menu_conf), "%s/%d", VAR_GEN_EX_MENU_DIR, p_section->sid); |
snprintf(ex_menu_conf, sizeof(ex_menu_conf), "%s/%d", VAR_GEN_EX_MENU_DIR, p_section->sid); |
| 158 |
|
|
| 159 |
// acquire rw lock of all sections to avoid conflict with menu reload in main process |
ret = load_menu(&ex_menu_set_new, ex_menu_conf); |
|
ret = section_list_rw_lock(NULL); |
|
| 160 |
if (ret < 0) |
if (ret < 0) |
| 161 |
{ |
{ |
| 162 |
log_error("section_list_rw_lock(NULL) error\n"); |
unload_menu(&ex_menu_set_new); |
| 163 |
|
log_error("load_menu(%s) error: %d\n", ex_menu_conf, ret); |
| 164 |
} |
} |
| 165 |
else |
else |
| 166 |
{ |
{ |
| 167 |
ret = load_menu(&ex_menu_set_new, ex_menu_conf); |
if (p_section->ex_menu_tm > 0) |
|
if (ret < 0) |
|
| 168 |
{ |
{ |
| 169 |
unload_menu(&ex_menu_set_new); |
unload_menu(&(p_section->ex_menu_set)); |
|
log_error("load_menu(%s) error: %d\n", ex_menu_conf, ret); |
|
| 170 |
} |
} |
|
else |
|
|
{ |
|
|
if (p_section->ex_menu_tm > 0) |
|
|
{ |
|
|
unload_menu(&(p_section->ex_menu_set)); |
|
|
} |
|
| 171 |
|
|
| 172 |
ex_menu_set_new.allow_exit = 1; // Allow exit menu |
ex_menu_set_new.allow_exit = 1; // Allow exit menu |
| 173 |
memcpy(&(p_section->ex_menu_set), &ex_menu_set_new, sizeof(ex_menu_set_new)); |
memcpy(&(p_section->ex_menu_set), &ex_menu_set_new, sizeof(ex_menu_set_new)); |
| 174 |
|
|
| 175 |
p_section->ex_menu_tm = atol(row[7]); |
p_section->ex_menu_tm = atol(row[7]); |
| 176 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 177 |
log_common("Loaded gen_ex_menu of section %d [%s]\n", p_section->sid, p_section->sname); |
log_common("Loaded gen_ex_menu of section %d [%s]\n", p_section->sid, p_section->sname); |
| 178 |
#endif |
#endif |
|
} |
|
|
|
|
|
// release rw lock of all sections |
|
|
ret = section_list_rw_unlock(NULL); |
|
|
if (ret < 0) |
|
|
{ |
|
|
log_error("section_list_rw_unlock(NULL) error\n"); |
|
|
} |
|
| 179 |
} |
} |
| 180 |
} |
} |
| 181 |
|
|
| 668 |
return (ret < 0 ? ret : op_count); |
return (ret < 0 ? ret : op_count); |
| 669 |
} |
} |
| 670 |
|
|
| 671 |
|
static void section_list_ex_menu_set_cleanup(void) |
| 672 |
|
{ |
| 673 |
|
int i; |
| 674 |
|
|
| 675 |
|
for (i = 0; i < p_section_list_pool->section_count; i++) |
| 676 |
|
{ |
| 677 |
|
if (p_section_list_pool->sections[i].ex_menu_tm > 0) |
| 678 |
|
{ |
| 679 |
|
unload_menu(&(p_section_list_pool->sections[i].ex_menu_set)); |
| 680 |
|
} |
| 681 |
|
} |
| 682 |
|
} |
| 683 |
|
|
| 684 |
int section_list_loader_launch(void) |
int section_list_loader_launch(void) |
| 685 |
{ |
{ |
| 686 |
struct sigaction act = {0}; |
struct sigaction act = {0}; |
| 690 |
int article_count; |
int article_count; |
| 691 |
int load_count; |
int load_count; |
| 692 |
int last_mid; |
int last_mid; |
| 693 |
int i; |
time_t tm_section_list_reload = 0; |
| 694 |
|
time_t tm_user_list_reload = 0; |
| 695 |
|
time_t tm_user_online_list_reload = 0; |
| 696 |
|
|
| 697 |
if (section_list_loader_pid != 0) |
if (section_list_loader_pid != 0) |
| 698 |
{ |
{ |
| 720 |
|
|
| 721 |
// Detach menu in shared memory |
// Detach menu in shared memory |
| 722 |
detach_menu_shm(&bbs_menu); |
detach_menu_shm(&bbs_menu); |
| 723 |
|
detach_menu_shm(&top10_menu); |
| 724 |
|
|
| 725 |
// Set signal handler |
// Set signal handler |
| 726 |
act.sa_handler = SIG_DFL; |
act.sa_handler = SIG_DFL; |
| 727 |
|
if (sigaction(SIGHUP, &act, NULL) == -1) |
| 728 |
|
{ |
| 729 |
|
log_error("set signal action of SIGHUP error: %d\n", errno); |
| 730 |
|
} |
| 731 |
|
act.sa_handler = SIG_DFL; |
| 732 |
if (sigaction(SIGCHLD, &act, NULL) == -1) |
if (sigaction(SIGCHLD, &act, NULL) == -1) |
| 733 |
{ |
{ |
| 734 |
log_error("set signal action of SIGCHLD error: %d\n", errno); |
log_error("set signal action of SIGCHLD error: %d\n", errno); |
| 735 |
} |
} |
| 736 |
|
|
|
// Force reload to load gen_ex_menu |
|
|
SYS_section_list_reload = 1; |
|
|
|
|
| 737 |
// Do section data loader periodically |
// Do section data loader periodically |
| 738 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 739 |
{ |
{ |
| 740 |
if (SYS_section_list_reload) |
tm_section_list_reload = time(NULL); |
| 741 |
|
|
| 742 |
|
if (SYS_conf_reload) |
| 743 |
{ |
{ |
| 744 |
SYS_section_list_reload = 0; |
SYS_conf_reload = 0; |
| 745 |
|
|
| 746 |
// Load section config |
// Load section config |
| 747 |
if (load_section_config_from_db(1) < 0) |
if (load_section_config_from_db(0) < 0) |
| 748 |
{ |
{ |
| 749 |
log_error("load_section_config_from_db() error\n"); |
log_error("load_section_config_from_db(0) error\n"); |
| 750 |
} |
} |
| 751 |
else |
else |
| 752 |
{ |
{ |
| 767 |
|
|
| 768 |
if (ret == ERR_UNKNOWN_SECTION) |
if (ret == ERR_UNKNOWN_SECTION) |
| 769 |
{ |
{ |
| 770 |
SYS_section_list_reload = 1; // Force reload section_list |
SYS_conf_reload = 1; // Force reload section_list |
| 771 |
} |
} |
| 772 |
} |
} |
| 773 |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
| 778 |
log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid()); |
log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid()); |
| 779 |
} |
} |
| 780 |
|
|
| 781 |
if (SYS_section_list_reload) |
if (SYS_conf_reload) |
| 782 |
{ |
{ |
| 783 |
continue; |
continue; |
| 784 |
} |
} |
| 794 |
|
|
| 795 |
if (ret == ERR_UNKNOWN_SECTION) |
if (ret == ERR_UNKNOWN_SECTION) |
| 796 |
{ |
{ |
| 797 |
SYS_section_list_reload = 1; // Force reload section_list |
SYS_conf_reload = 1; // Force reload section_list |
| 798 |
} |
} |
| 799 |
} |
} |
| 800 |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
| 804 |
log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid); |
log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid); |
| 805 |
} |
} |
| 806 |
|
|
| 807 |
if (SYS_section_list_reload) |
// Reload user list |
| 808 |
|
if (time(NULL) - tm_user_list_reload >= BBS_user_list_load_interval) |
| 809 |
{ |
{ |
| 810 |
continue; |
if (user_list_pool_reload(0) < 0) |
| 811 |
|
{ |
| 812 |
|
log_error("user_list_pool_reload(all_user) error\n"); |
| 813 |
|
} |
| 814 |
|
tm_user_list_reload = time(NULL); |
| 815 |
|
} |
| 816 |
|
|
| 817 |
|
// Reload user online list |
| 818 |
|
if (time(NULL) - tm_user_online_list_reload >= BBS_user_online_list_load_interval) |
| 819 |
|
{ |
| 820 |
|
if (user_list_pool_reload(1) < 0) |
| 821 |
|
{ |
| 822 |
|
log_error("user_list_pool_reload(online_user) error\n"); |
| 823 |
|
} |
| 824 |
|
tm_user_online_list_reload = time(NULL); |
| 825 |
} |
} |
| 826 |
|
|
| 827 |
for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++) |
// Wait for BBS_section_list_load_interval seconds |
| 828 |
|
while (!SYS_server_exit && time(NULL) - tm_section_list_reload < BBS_section_list_load_interval) |
| 829 |
{ |
{ |
| 830 |
sleep(1); |
sleep(1); |
| 831 |
} |
} |
| 842 |
detach_section_list_shm(); |
detach_section_list_shm(); |
| 843 |
detach_article_block_shm(); |
detach_article_block_shm(); |
| 844 |
detach_trie_dict_shm(); |
detach_trie_dict_shm(); |
| 845 |
|
detach_user_list_pool_shm(); |
| 846 |
|
|
| 847 |
log_common("Section list loader process exit normally\n"); |
log_common("Section list loader process exit normally\n"); |
| 848 |
log_end(); |
log_end(); |
| 854 |
return 0; |
return 0; |
| 855 |
} |
} |
| 856 |
|
|
|
int section_list_loader_reload(void) |
|
|
{ |
|
|
if (section_list_loader_pid == 0) |
|
|
{ |
|
|
log_error("section_list_loader not running\n"); |
|
|
return -2; |
|
|
} |
|
|
|
|
|
if (kill(section_list_loader_pid, SIGHUP) < 0) |
|
|
{ |
|
|
log_error("Send SIGTERM signal failed (%d)\n", errno); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
| 857 |
int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], |
int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], |
| 858 |
int *p_article_count, int *p_page_count, int *p_ontop_start_offset) |
int *p_article_count, int *p_page_count, int *p_ontop_start_offset) |
| 859 |
{ |
{ |
| 943 |
int *p_page_id, int *p_visible_offset, int *p_article_count) |
int *p_page_id, int *p_visible_offset, int *p_article_count) |
| 944 |
{ |
{ |
| 945 |
const ARTICLE *p_article = NULL; |
const ARTICLE *p_article = NULL; |
| 946 |
|
const ARTICLE *p_article_last = NULL; |
| 947 |
ARTICLE *p_tmp; |
ARTICLE *p_tmp; |
| 948 |
int32_t aid = 0; |
int32_t aid = 0; |
| 949 |
int page_id = 0; |
int page_id = 0; |
| 970 |
} |
} |
| 971 |
else if (direction == 1) |
else if (direction == 1) |
| 972 |
{ |
{ |
| 973 |
for (p_article = p_article_cur; step > 0 && p_article->p_topic_next->aid > p_article_cur->aid; p_article = p_article->p_topic_next) |
for (p_article_last = p_article_cur, p_article = p_article_cur->p_topic_next; |
| 974 |
|
step > 0 && p_article->aid > p_article_cur->aid; |
| 975 |
|
p_article = p_article->p_topic_next) |
| 976 |
{ |
{ |
| 977 |
if (p_article->visible) |
if (p_article->visible) |
| 978 |
{ |
{ |
| 979 |
step--; |
step--; |
| 980 |
|
p_article_last = p_article; |
| 981 |
} |
} |
| 982 |
} |
} |
| 983 |
|
p_article = p_article_last; |
| 984 |
|
|
| 985 |
aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0); |
aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0); |
| 986 |
} |
} |
| 987 |
else if (direction == -1) |
else if (direction == -1) |
| 988 |
{ |
{ |
| 989 |
for (p_article = p_article_cur; step > 0 && p_article->p_topic_prior->aid < p_article_cur->aid; p_article = p_article->p_topic_prior) |
for (p_article_last = p_article_cur, p_article = p_article_cur->p_topic_prior; |
| 990 |
|
step > 0 && p_article->aid < p_article_cur->aid; |
| 991 |
|
p_article = p_article->p_topic_prior) |
| 992 |
{ |
{ |
| 993 |
if (p_article->visible) |
if (p_article->visible) |
| 994 |
{ |
{ |
| 995 |
step--; |
step--; |
| 996 |
|
p_article_last = p_article; |
| 997 |
} |
} |
| 998 |
} |
} |
| 999 |
|
p_article = p_article_last; |
| 1000 |
|
|
| 1001 |
aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0); |
aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0); |
| 1002 |
} |
} |
| 1047 |
return (ret < 0 ? ret : (p_article == NULL ? 0 : 1)); |
return (ret < 0 ? ret : (p_article == NULL ? 0 : 1)); |
| 1048 |
} |
} |
| 1049 |
|
|
| 1050 |
|
int scan_unread_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, const ARTICLE **pp_article_unread) |
| 1051 |
|
{ |
| 1052 |
|
ARTICLE *p_article; |
| 1053 |
|
int ret = 0; |
| 1054 |
|
|
| 1055 |
|
if (p_section == NULL || p_article_cur == NULL || pp_article_unread == NULL) |
| 1056 |
|
{ |
| 1057 |
|
log_error("NULL pointer error\n"); |
| 1058 |
|
return -1; |
| 1059 |
|
} |
| 1060 |
|
|
| 1061 |
|
if (p_article_cur->sid != p_section->sid) |
| 1062 |
|
{ |
| 1063 |
|
log_error("Inconsistent SID\n"); |
| 1064 |
|
return -1; |
| 1065 |
|
} |
| 1066 |
|
|
| 1067 |
|
// acquire lock of section |
| 1068 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 1069 |
|
{ |
| 1070 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 1071 |
|
return -2; |
| 1072 |
|
} |
| 1073 |
|
|
| 1074 |
|
*pp_article_unread = NULL; |
| 1075 |
|
|
| 1076 |
|
for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next) |
| 1077 |
|
{ |
| 1078 |
|
if (p_article->visible && p_article->uid != BBS_priv.uid && !article_view_log_is_viewed(p_article->aid, &BBS_article_view_log)) |
| 1079 |
|
{ |
| 1080 |
|
*pp_article_unread = p_article; |
| 1081 |
|
break; |
| 1082 |
|
} |
| 1083 |
|
} |
| 1084 |
|
|
| 1085 |
|
// release lock of section |
| 1086 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1087 |
|
{ |
| 1088 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 1089 |
|
return -2; |
| 1090 |
|
} |
| 1091 |
|
|
| 1092 |
|
return (p_article != NULL && p_article != p_article_cur ? 1 : 0); |
| 1093 |
|
} |
| 1094 |
|
|
| 1095 |
int get_section_ex_menu_set(SECTION_LIST *p_section, MENU_SET *p_ex_menu_set) |
int get_section_ex_menu_set(SECTION_LIST *p_section, MENU_SET *p_ex_menu_set) |
| 1096 |
{ |
{ |
| 1097 |
int ret = 0; |
int ret = 0; |