/[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.28 by sysadm, Fri Jun 20 09:36:58 2025 UTC Revision 1.45 by sysadm, Sat Oct 4 04:01:19 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "section_list_loader.h"  
17  #include "article_cache.h"  #include "article_cache.h"
18    #include "article_view_log.h"
19  #include "bbs.h"  #include "bbs.h"
 #include "log.h"  
20  #include "database.h"  #include "database.h"
21    #include "ip_mask.h"
22    #include "log.h"
23  #include "menu.h"  #include "menu.h"
24  #include <stdio.h>  #include "section_list_loader.h"
25  #include <string.h>  #include "user_priv.h"
26  #include <errno.h>  #include <errno.h>
27  #include <signal.h>  #include <signal.h>
28    #include <stdio.h>
29  #include <stdlib.h>  #include <stdlib.h>
30  #include <string.h>  #include <string.h>
 #include <strings.h>  
31  #include <unistd.h>  #include <unistd.h>
32    
33  int section_list_loader_pid;  int section_list_loader_pid;
34  int last_article_op_log_mid;  int last_article_op_log_mid;
35    
36  int load_section_config_from_db(void)  int load_section_config_from_db(int update_gen_ex)
37  {  {
38          MYSQL *db = NULL;          MYSQL *db = NULL;
39          MYSQL_RES *rs = NULL, *rs2 = NULL;          MYSQL_RES *rs = NULL, *rs2 = NULL;
# Line 41  int load_section_config_from_db(void) Line 42  int load_section_config_from_db(void)
42          int32_t sid;          int32_t sid;
43          char master_list[(BBS_username_max_len + 1) * 3 + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
44          SECTION_LIST *p_section;          SECTION_LIST *p_section;
45            char ex_menu_conf[FILE_PATH_LEN];
46            MENU_SET ex_menu_set_new;
47          int ret = 0;          int ret = 0;
48    
49          db = db_open();          db = db_open();
# Line 53  int load_section_config_from_db(void) Line 56  int load_section_config_from_db(void)
56    
57          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
58                           "SELECT section_config.SID, sname, section_config.title, section_config.CID, "                           "SELECT section_config.SID, sname, section_config.title, section_config.CID, "
59                           "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, "
60                             "UNIX_TIMESTAMP(ex_menu_tm) AS ex_menu_tm "
61                           "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 "
62                           "ORDER BY section_config.SID");                           "ORDER BY section_config.SID");
63    
# Line 133  int load_section_config_from_db(void) Line 137  int load_section_config_from_db(void)
137                                  break;                                  break;
138                          }                          }
139    
140                          strncpy(p_section->sname, row[0], sizeof(p_section->sname) - 1);                          strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1);
141                          p_section->sname[sizeof(p_section->sname) - 1] = '\0';                          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
142                          strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);                          strncpy(p_section->stitle, row[2], sizeof(p_section->stitle) - 1);
143                          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';                          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
144                          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);                          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
145                          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';                          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
# Line 146  int load_section_config_from_db(void) Line 150  int load_section_config_from_db(void)
150                  p_section->write_user_level = atoi(row[5]);                  p_section->write_user_level = atoi(row[5]);
151                  p_section->enable = (int8_t)atoi(row[6]);                  p_section->enable = (int8_t)atoi(row[6]);
152    
153                    // Update gen_ex menu set
154                    if (update_gen_ex && p_section->enable && atoi(row[7]) > p_section->ex_menu_tm)
155                    {
156                            snprintf(ex_menu_conf, sizeof(ex_menu_conf), "%s/%d", VAR_GEN_EX_MENU_DIR, p_section->sid);
157    
158                            ret = load_menu(&ex_menu_set_new, ex_menu_conf);
159                            if (ret < 0)
160                            {
161                                    unload_menu(&ex_menu_set_new);
162                                    log_error("load_menu(%s) error: %d\n", ex_menu_conf, ret);
163                            }
164                            else
165                            {
166                                    if (p_section->ex_menu_tm > 0)
167                                    {
168                                            unload_menu(&(p_section->ex_menu_set));
169                                    }
170    
171                                    ex_menu_set_new.allow_exit = 1; // Allow exit menu
172                                    memcpy(&(p_section->ex_menu_set), &ex_menu_set_new, sizeof(ex_menu_set_new));
173    
174                                    p_section->ex_menu_tm = atol(row[7]);
175    #ifdef _DEBUG
176                                    log_common("Loaded gen_ex_menu of section %d [%s]\n", p_section->sid, p_section->sname);
177    #endif
178                            }
179                    }
180    
181                  // release rw lock                  // release rw lock
182                  ret = section_list_rw_unlock(p_section);                  ret = section_list_rw_unlock(p_section);
183                  if (ret < 0)                  if (ret < 0)
# Line 155  int load_section_config_from_db(void) Line 187  int load_section_config_from_db(void)
187          }          }
188    
189  cleanup:  cleanup:
190            mysql_free_result(rs2);
191          mysql_free_result(rs);          mysql_free_result(rs);
192          mysql_close(db);          mysql_close(db);
193    
# Line 322  cleanup: Line 355  cleanup:
355          mysql_free_result(rs);          mysql_free_result(rs);
356          mysql_close(db);          mysql_close(db);
357    
358            article_cache_cleanup();
359    
360          return (ret < 0 ? ret : article_count);          return (ret < 0 ? ret : article_count);
361  }  }
362    
# Line 588  int apply_article_op_log_from_db(int op_ Line 623  int apply_article_op_log_from_db(int op_
623                          p_article->excerption = 0;                          p_article->excerption = 0;
624                          break;                          break;
625                  case 'F': // Set article on top                  case 'F': // Set article on top
                         p_article->ontop = 1;  
                         break;  
626                  case 'V': // Unset article on top                  case 'V': // Unset article on top
627                          p_article->ontop = 0;                          p_article->ontop = (row[2][0] == 'F' ? 1 : 0);
628                            if (section_list_update_article_ontop(p_section, p_article) < 0)
629                            {
630                                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
631                                                      p_section->sid, p_article->aid);
632                            }
633                          break;                          break;
634                  case 'Z': // Set article as trnasship                  case 'Z': // Set article as trnasship
635                          p_article->transship = 1;                          p_article->transship = 1;
# Line 629  cleanup: Line 667  cleanup:
667          return (ret < 0 ? ret : op_count);          return (ret < 0 ? ret : op_count);
668  }  }
669    
670    static void section_list_ex_menu_set_cleanup(void)
671    {
672            int i;
673    
674            for (i = 0; i < p_section_list_pool->section_count; i++)
675            {
676                    if (p_section_list_pool->sections[i].ex_menu_tm > 0)
677                    {
678                            unload_menu(&(p_section_list_pool->sections[i].ex_menu_set));
679                    }
680            }
681    }
682    
683  int section_list_loader_launch(void)  int section_list_loader_launch(void)
684  {  {
685            struct sigaction act = {0};
686          int pid;          int pid;
687          int ret;          int ret;
688          int32_t last_aid;          int32_t last_aid;
# Line 664  int section_list_loader_launch(void) Line 716  int section_list_loader_launch(void)
716          SYS_child_process_count = 0;          SYS_child_process_count = 0;
717    
718          // Detach menu in shared memory          // Detach menu in shared memory
719          detach_menu_shm(p_bbs_menu);          detach_menu_shm(&bbs_menu);
720          free(p_bbs_menu);  
721          p_bbs_menu = NULL;          // Set signal handler
722            act.sa_handler = SIG_DFL;
723            if (sigaction(SIGHUP, &act, NULL) == -1)
724            {
725                    log_error("set signal action of SIGHUP error: %d\n", errno);
726            }
727            act.sa_handler = SIG_DFL;
728            if (sigaction(SIGCHLD, &act, NULL) == -1)
729            {
730                    log_error("set signal action of SIGCHLD error: %d\n", errno);
731            }
732    
733          // Do section data loader periodically          // Do section data loader periodically
734          while (!SYS_server_exit)          while (!SYS_server_exit)
735          {          {
736                  if (SYS_section_list_reload)                  if (SYS_conf_reload)
737                  {                  {
738                          SYS_section_list_reload = 0;                          SYS_conf_reload = 0;
739    
740                          // Load section config                          // Load section config
741                          if (load_section_config_from_db() < 0)                          if (load_section_config_from_db(0) < 0)
742                          {                          {
743                                  log_error("load_section_config_from_db() error\n");                                  log_error("load_section_config_from_db(0) error\n");
744                          }                          }
745                          else                          else
746                          {                          {
# Line 699  int section_list_loader_launch(void) Line 761  int section_list_loader_launch(void)
761    
762                                  if (ret == ERR_UNKNOWN_SECTION)                                  if (ret == ERR_UNKNOWN_SECTION)
763                                  {                                  {
764                                          SYS_section_list_reload = 1; // Force reload section_list                                          SYS_conf_reload = 1; // Force reload section_list
765                                  }                                  }
766                          }                          }
767                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
# Line 710  int section_list_loader_launch(void) Line 772  int section_list_loader_launch(void)
772                          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());
773                  }                  }
774    
775                  if (SYS_section_list_reload)                  if (SYS_conf_reload)
776                  {                  {
777                          continue;                          continue;
778                  }                  }
# Line 726  int section_list_loader_launch(void) Line 788  int section_list_loader_launch(void)
788    
789                                  if (ret == ERR_UNKNOWN_SECTION)                                  if (ret == ERR_UNKNOWN_SECTION)
790                                  {                                  {
791                                          SYS_section_list_reload = 1; // Force reload section_list                                          SYS_conf_reload = 1; // Force reload section_list
792                                  }                                  }
793                          }                          }
794                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
# Line 736  int section_list_loader_launch(void) Line 798  int section_list_loader_launch(void)
798                          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);
799                  }                  }
800    
801                  if (SYS_section_list_reload)                  if (SYS_conf_reload)
802                  {                  {
803                          continue;                          continue;
804                  }                  }
805    
806                  for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++)                  for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_conf_reload; i++)
807                  {                  {
808                          sleep(1);                          sleep(1);
809                  }                  }
# Line 749  int section_list_loader_launch(void) Line 811  int section_list_loader_launch(void)
811    
812          // Child process exit          // Child process exit
813    
814            article_cache_cleanup();
815    
816            // gen_ex_menu cleanup
817            section_list_ex_menu_set_cleanup();
818    
819          // Detach data pools shm          // Detach data pools shm
820          detach_section_list_shm();          detach_section_list_shm();
821          detach_article_block_shm();          detach_article_block_shm();
# Line 764  int section_list_loader_launch(void) Line 831  int section_list_loader_launch(void)
831          return 0;          return 0;
832  }  }
833    
834  int section_list_loader_reload(void)  int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[],
835  {                                                     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)  
836  {  {
837          ARTICLE *p_article;          ARTICLE *p_article;
838          ARTICLE *p_next_page_first_article;          ARTICLE *p_next_page_first_article;
839          int ret = 0;          int ret = 0;
840            int i;
841    
842          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)
843          {          {
844                  log_error("query_section_articles() NULL pointer error\n");                  log_error("NULL pointer error\n");
845                  return -1;                  return -1;
846          }          }
847    
# Line 800  int query_section_articles(SECTION_LIST Line 852  int query_section_articles(SECTION_LIST
852                  return -2;                  return -2;
853          }          }
854    
855          *p_page_count = p_section->page_count;          *p_page_count = section_list_page_count_with_ontop(p_section);
856            *p_article_count = 0;
857            *p_ontop_start_offset = BBS_article_limit_per_page;
858    
859          if (p_section->visible_article_count == 0)          if (p_section->visible_article_count == 0)
860          {          {
861                  *p_article_count = 0;                  // empty section
862          }          }
863          else if (page_id < 0 || page_id >= p_section->page_count)          else if (page_id < 0 || page_id >= *p_page_count)
864          {          {
865                  log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);                  log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, *p_page_count);
866                  ret = -3;                  ret = -3;
867          }          }
868          else          else
869          {          {
870                  ret = page_id;                  ret = page_id;
                 p_article = p_section->p_page_first_article[page_id];  
                 p_next_page_first_article =  
                         (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);  
                 *p_article_count = 0;  
871    
872                  do                  if (page_id <= p_section->page_count - 1)
873                  {                  {
874                          if (p_article->visible)                          p_article = p_section->p_page_first_article[page_id];
875                            p_next_page_first_article =
876                                    (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);
877    
878                            do
879                            {
880                                    if (p_article->visible)
881                                    {
882                                            p_articles[*p_article_count] = p_article;
883                                            (*p_article_count)++;
884                                    }
885                                    p_article = p_article->p_next;
886                            } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);
887    
888                            if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))
889                          {                          {
890                                  p_articles[*p_article_count] = p_article;                                  log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);
                                 (*p_article_count)++;  
891                          }                          }
892                          p_article = p_article->p_next;                  }
893                  } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);  
894                    *p_ontop_start_offset = *p_article_count;
895    
896                  if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))                  // Append ontop articles
897                    if (page_id >= p_section->page_count - 1)
898                  {                  {
899                          log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);                          i = (page_id == p_section->page_count - 1
900                                             ? 0
901                                             : (page_id - p_section->page_count + 1) * BBS_article_limit_per_page - p_section->last_page_visible_article_count);
902                            while (*p_article_count < BBS_article_limit_per_page && i < p_section->ontop_article_count)
903                            {
904                                    p_articles[(*p_article_count)++] = p_section->p_ontop_articles[i++];
905                            }
906                  }                  }
907          }          }
908    
# Line 849  int locate_article_in_section(SECTION_LI Line 920  int locate_article_in_section(SECTION_LI
920                                                            int *p_page_id, int *p_visible_offset, int *p_article_count)                                                            int *p_page_id, int *p_visible_offset, int *p_article_count)
921  {  {
922          const ARTICLE *p_article = NULL;          const ARTICLE *p_article = NULL;
923            const ARTICLE *p_article_last = NULL;
924          ARTICLE *p_tmp;          ARTICLE *p_tmp;
925          int32_t aid = 0;          int32_t aid = 0;
926          int page_id = 0;          int page_id = 0;
# Line 858  int locate_article_in_section(SECTION_LI Line 930  int locate_article_in_section(SECTION_LI
930    
931          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)
932          {          {
933                  log_error("locate_article_in_section() NULL pointer error\n");                  log_error("NULL pointer error\n");
934                  return -1;                  return -1;
935          }          }
936    
# Line 875  int locate_article_in_section(SECTION_LI Line 947  int locate_article_in_section(SECTION_LI
947          }          }
948          else if (direction == 1)          else if (direction == 1)
949          {          {
950                  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;
951                             step > 0 && p_article->aid > p_article_cur->aid;
952                             p_article = p_article->p_topic_next)
953                  {                  {
954                          if (p_article->visible)                          if (p_article->visible)
955                          {                          {
956                                  step--;                                  step--;
957                                    p_article_last = p_article;
958                          }                          }
959                  }                  }
960                    p_article = p_article_last;
961    
962                  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);
963          }          }
964          else if (direction == -1)          else if (direction == -1)
965          {          {
966                  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;
967                             step > 0 && p_article->aid < p_article_cur->aid;
968                             p_article = p_article->p_topic_prior)
969                  {                  {
970                          if (p_article->visible)                          if (p_article->visible)
971                          {                          {
972                                  step--;                                  step--;
973                                    p_article_last = p_article;
974                          }                          }
975                  }                  }
976                    p_article = p_article_last;
977    
978                  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);
979          }          }
# Line 928  int locate_article_in_section(SECTION_LI Line 1008  int locate_article_in_section(SECTION_LI
1008                                  }                                  }
1009                                  p_article = p_article->p_next;                                  p_article = p_article->p_next;
1010                          }                          }
1011    
1012                            // Include ontop articles
1013                            *p_article_count = section_list_page_article_count_with_ontop(p_section, page_id);
1014                  }                  }
1015          }          }
1016    
# Line 940  int locate_article_in_section(SECTION_LI Line 1023  int locate_article_in_section(SECTION_LI
1023    
1024          return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));          return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
1025  }  }
1026    
1027    int scan_unread_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, const ARTICLE **pp_article_unread)
1028    {
1029            ARTICLE *p_article;
1030            int ret = 0;
1031    
1032            if (p_section == NULL || p_article_cur == NULL || pp_article_unread == NULL)
1033            {
1034                    log_error("NULL pointer error\n");
1035                    return -1;
1036            }
1037    
1038            if (p_article_cur->sid != p_section->sid)
1039            {
1040                    log_error("Inconsistent SID\n");
1041                    return -1;
1042            }
1043    
1044            // acquire lock of section
1045            if ((ret = section_list_rd_lock(p_section)) < 0)
1046            {
1047                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1048                    return -2;
1049            }
1050    
1051            *pp_article_unread = NULL;
1052    
1053            for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next)
1054            {
1055                    if (p_article->visible && p_article->uid != BBS_priv.uid && !article_view_log_is_viewed(p_article->aid, &BBS_article_view_log))
1056                    {
1057                            *pp_article_unread = p_article;
1058                            break;
1059                    }
1060            }
1061    
1062            // release lock of section
1063            if (section_list_rd_unlock(p_section) < 0)
1064            {
1065                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1066                    return -2;
1067            }
1068    
1069            return (p_article != NULL && p_article != p_article_cur ? 1 : 0);
1070    }
1071    
1072    int get_section_ex_menu_set(SECTION_LIST *p_section, MENU_SET *p_ex_menu_set)
1073    {
1074            int ret = 0;
1075    
1076            if (p_section == NULL || p_ex_menu_set == NULL)
1077            {
1078                    log_error("NULL pointer error\n");
1079                    return -1;
1080            }
1081    
1082            // acquire lock of section
1083            if ((ret = section_list_rd_lock(p_section)) < 0)
1084            {
1085                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1086                    return -2;
1087            }
1088    
1089            memcpy(p_ex_menu_set, &(p_section->ex_menu_set), sizeof(p_section->ex_menu_set));
1090    
1091            // release lock of section
1092            if (section_list_rd_unlock(p_section) < 0)
1093            {
1094                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1095                    ret = -2;
1096            }
1097    
1098            return ret;
1099    }


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

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