/[LeafOK_CVS]/lbbs/src/section_list_loader.c
ViewVC logotype

Diff of /lbbs/src/section_list_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.32 by sysadm, Mon Jun 23 08:38:01 2025 UTC Revision 1.53 by sysadm, Wed Oct 22 04:48:53 2025 UTC
# Line 15  Line 15 
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>
# Line 30  Line 34 
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(void)  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;
# Line 39  int load_section_config_from_db(void) Line 43  int load_section_config_from_db(void)
43          int32_t sid;          int32_t sid;
44          char master_list[(BBS_username_max_len + 1) * 3 + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
45          SECTION_LIST *p_section;          SECTION_LIST *p_section;
46            char ex_menu_conf[FILE_PATH_LEN];
47            MENU_SET ex_menu_set_new;
48          int ret = 0;          int ret = 0;
49    
50          db = db_open();          db = db_open();
# Line 51  int load_section_config_from_db(void) Line 57  int load_section_config_from_db(void)
57    
58          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
59                           "SELECT section_config.SID, sname, section_config.title, section_config.CID, "                           "SELECT section_config.SID, sname, section_config.title, section_config.CID, "
60                           "read_user_level, write_user_level, section_config.enable * section_class.enable AS enable "                           "read_user_level, write_user_level, section_config.enable * section_class.enable AS enable, "
61                             "UNIX_TIMESTAMP(ex_menu_tm) AS ex_menu_tm "
62                           "FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID "                           "FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID "
63                           "ORDER BY section_config.SID");                           "ORDER BY section_config.SID");
64    
# Line 131  int load_section_config_from_db(void) Line 138  int load_section_config_from_db(void)
138                                  break;                                  break;
139                          }                          }
140    
141                          strncpy(p_section->sname, row[0], 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[1], 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]);
# Line 144  int load_section_config_from_db(void) Line 151  int load_section_config_from_db(void)
151                  p_section->write_user_level = atoi(row[5]);                  p_section->write_user_level = atoi(row[5]);
152                  p_section->enable = (int8_t)atoi(row[6]);                  p_section->enable = (int8_t)atoi(row[6]);
153    
154                    // Update gen_ex menu set
155                    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);
158    
159                            ret = load_menu(&ex_menu_set_new, ex_menu_conf);
160                            if (ret < 0)
161                            {
162                                    unload_menu(&ex_menu_set_new);
163                                    log_error("load_menu(%s) error: %d\n", ex_menu_conf, ret);
164                            }
165                            else
166                            {
167                                    if (p_section->ex_menu_tm > 0)
168                                    {
169                                            unload_menu(&(p_section->ex_menu_set));
170                                    }
171    
172                                    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));
174    
175                                    p_section->ex_menu_tm = atol(row[7]);
176    #ifdef _DEBUG
177                                    log_common("Loaded gen_ex_menu of section %d [%s]\n", p_section->sid, p_section->sname);
178    #endif
179                            }
180                    }
181    
182                  // release rw lock                  // release rw lock
183                  ret = section_list_rw_unlock(p_section);                  ret = section_list_rw_unlock(p_section);
184                  if (ret < 0)                  if (ret < 0)
# Line 633  cleanup: Line 668  cleanup:
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};
687          int pid;          int pid;
688          int ret;          int ret;
689          int32_t last_aid;          int32_t last_aid;
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          {          {
# Line 668  int section_list_loader_launch(void) Line 719  int section_list_loader_launch(void)
719          SYS_child_process_count = 0;          SYS_child_process_count = 0;
720    
721          // Detach menu in shared memory          // Detach menu in shared memory
722          detach_menu_shm(p_bbs_menu);          detach_menu_shm(&bbs_menu);
723          free(p_bbs_menu);          detach_menu_shm(&top10_menu);
724          p_bbs_menu = NULL;  
725            // Set signal handler
726            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)
733            {
734                    log_error("set signal action of SIGCHLD error: %d\n", errno);
735            }
736    
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() < 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                          {                          {
# Line 703  int section_list_loader_launch(void) Line 767  int section_list_loader_launch(void)
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);
# Line 714  int section_list_loader_launch(void) Line 778  int section_list_loader_launch(void)
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                  }                  }
# Line 730  int section_list_loader_launch(void) Line 794  int section_list_loader_launch(void)
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);
# Line 740  int section_list_loader_launch(void) Line 804  int section_list_loader_launch(void)
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                  for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++)                  // 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                    // 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                  }                  }
# Line 755  int section_list_loader_launch(void) Line 835  int section_list_loader_launch(void)
835    
836          article_cache_cleanup();          article_cache_cleanup();
837    
838            // gen_ex_menu cleanup
839            section_list_ex_menu_set_cleanup();
840    
841          // Detach data pools shm          // Detach data pools shm
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();
# Line 770  int section_list_loader_launch(void) Line 854  int section_list_loader_launch(void)
854          return 0;          return 0;
855  }  }
856    
857  int section_list_loader_reload(void)  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)
         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;  
 }  
   
 int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count)  
859  {  {
860          ARTICLE *p_article;          ARTICLE *p_article;
861          ARTICLE *p_next_page_first_article;          ARTICLE *p_next_page_first_article;
862          int ret = 0;          int ret = 0;
863          int i;          int i;
864    
865          if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)          if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL || p_ontop_start_offset == NULL)
866          {          {
867                  log_error("query_section_articles() NULL pointer error\n");                  log_error("NULL pointer error\n");
868                  return -1;                  return -1;
869          }          }
870    
# Line 809  int query_section_articles(SECTION_LIST Line 877  int query_section_articles(SECTION_LIST
877    
878          *p_page_count = section_list_page_count_with_ontop(p_section);          *p_page_count = section_list_page_count_with_ontop(p_section);
879          *p_article_count = 0;          *p_article_count = 0;
880            *p_ontop_start_offset = BBS_article_limit_per_page;
881    
882          if (p_section->visible_article_count == 0)          if (p_section->visible_article_count == 0)
883          {          {
# Line 845  int query_section_articles(SECTION_LIST Line 914  int query_section_articles(SECTION_LIST
914                          }                          }
915                  }                  }
916    
917                    *p_ontop_start_offset = *p_article_count;
918    
919                  // Append ontop articles                  // Append ontop articles
920                  if (page_id >= p_section->page_count - 1)                  if (page_id >= p_section->page_count - 1)
921                  {                  {
# Line 872  int locate_article_in_section(SECTION_LI Line 943  int locate_article_in_section(SECTION_LI
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;
# Line 881  int locate_article_in_section(SECTION_LI Line 953  int locate_article_in_section(SECTION_LI
953    
954          if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)          if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
955          {          {
956                  log_error("locate_article_in_section() NULL pointer error\n");                  log_error("NULL pointer error\n");
957                  return -1;                  return -1;
958          }          }
959    
# Line 898  int locate_article_in_section(SECTION_LI Line 970  int locate_article_in_section(SECTION_LI
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          }          }
# Line 966  int locate_article_in_section(SECTION_LI Line 1046  int locate_article_in_section(SECTION_LI
1046    
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)
1096    {
1097            int ret = 0;
1098    
1099            if (p_section == NULL || p_ex_menu_set == NULL)
1100            {
1101                    log_error("NULL pointer error\n");
1102                    return -1;
1103            }
1104    
1105            // acquire lock of section
1106            if ((ret = section_list_rd_lock(p_section)) < 0)
1107            {
1108                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1109                    return -2;
1110            }
1111    
1112            memcpy(p_ex_menu_set, &(p_section->ex_menu_set), sizeof(p_section->ex_menu_set));
1113    
1114            // release lock of section
1115            if (section_list_rd_unlock(p_section) < 0)
1116            {
1117                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1118                    ret = -2;
1119            }
1120    
1121            return ret;
1122    }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1