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

Diff of /lbbs/src/section_list.c

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

Revision 1.17 by sysadm, Sat May 24 13:52:44 2025 UTC Revision 1.37 by sysadm, Wed Jun 25 02:49:20 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _GNU_SOURCE  
   
 #include "section_list.h"  
17  #include "log.h"  #include "log.h"
18    #include "section_list.h"
19  #include "trie_dict.h"  #include "trie_dict.h"
20    #include <errno.h>
21    #include <signal.h>
22  #include <stdio.h>  #include <stdio.h>
23    #include <stdlib.h>
24  #include <string.h>  #include <string.h>
 #include <signal.h>  
25  #include <unistd.h>  #include <unistd.h>
26  #include <stdlib.h>  #include <sys/ipc.h>
 #include <errno.h>  
27  #include <sys/param.h>  #include <sys/param.h>
28  #include <sys/sem.h>  #include <sys/sem.h>
29  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
30    
31  #ifdef _SEM_SEMUN_UNDEFINED  #ifdef _SEM_SEMUN_UNDEFINED
32  union semun  union semun
# Line 41  union semun Line 39  union semun
39  };  };
40  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
41    
42  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
43  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  #define SECTION_TRY_LOCK_TIMES 10
44    
45    #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
46    #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
47  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)
48    
49  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections
50    
51  #define SID_STR_LEN 5 // 32-bit + NULL  #define SID_STR_LEN 5 // 32-bit + NULL
52    
53  struct article_block_t  struct article_block_t
54  {  {
55          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[ARTICLE_PER_BLOCK];
56          int32_t article_count;          int article_count;
57          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
58  };  };
59  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
60    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
61  struct article_block_pool_t  struct article_block_pool_t
62  {  {
63          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
64            struct
65            {
66                    int shmid;
67                    void *p_shm;
68            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
69          int shm_count;          int shm_count;
70          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
71          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
72          int32_t block_count;          int block_count;
73  };  };
74  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
75    
76  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
77    
78  static int section_list_pool_shmid;  struct section_list_pool_t
79  static int section_list_pool_semid;  {
80  static SECTION_LIST *p_section_list_pool = NULL;          int shmid;
81  static int section_list_count = 0;          SECTION_LIST sections[BBS_max_section];
82  static TRIE_NODE *p_trie_dict_section_by_name = NULL;          int section_count;
83  static TRIE_NODE *p_trie_dict_section_by_sid = NULL;          int semid;
84            TRIE_NODE *p_trie_dict_section_by_name;
85            TRIE_NODE *p_trie_dict_section_by_sid;
86    };
87    typedef struct section_list_pool_t SECTION_LIST_POOL;
88    
89    static SECTION_LIST_POOL *p_section_list_pool = NULL;
90    
91  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
92  {  {
# Line 101  int article_block_init(const char *filen Line 106  int article_block_init(const char *filen
106                  return -1;                  return -1;
107          }          }
108    
109          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
110          {          {
111                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
112                  return -2;                  return -2;
113          }          }
114    
115          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
116          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
117            key = ftok(filename, proj_id);
118            if (key == -1)
119          {          {
120                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
121                  return -2;                  return -3;
122          }          }
123    
124          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
125            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
126            if (shmid == -1)
127            {
128                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
129                    return -3;
130            }
131            p_shm = shmat(shmid, NULL, 0);
132            if (p_shm == (void *)-1)
133            {
134                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
135                    return -3;
136            }
137    
138            p_article_block_pool = p_shm;
139            p_article_block_pool->shmid = shmid;
140    
141          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
142          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
143    
# Line 123  int article_block_init(const char *filen Line 146  int article_block_init(const char *filen
146                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
147                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
148    
149                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
150                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
151                  if (key == -1)                  if (key == -1)
152                  {                  {
# Line 132  int article_block_init(const char *filen Line 155  int article_block_init(const char *filen
155                          return -3;                          return -3;
156                  }                  }
157    
158                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
159                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
160                  if (shmid == -1)                  if (shmid == -1)
161                  {                  {
# Line 176  int article_block_init(const char *filen Line 199  int article_block_init(const char *filen
199    
200  void article_block_cleanup(void)  void article_block_cleanup(void)
201  {  {
202          if (p_article_block_pool != NULL)          int shmid;
203    
204            if (p_article_block_pool == NULL)
205            {
206                    return;
207            }
208    
209            for (int i = 0; i < p_article_block_pool->shm_count; i++)
210          {          {
211                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
212                  {                  {
213                          if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)                          log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
214                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
215    
216                          if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)                  if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)
217                          {                  {
218                                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
                         }  
219                  }                  }
220            }
221    
222            shmid = p_article_block_pool->shmid;
223    
224            if (shmdt(p_article_block_pool) == -1)
225            {
226                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
227            }
228    
229            if (shmctl(shmid, IPC_RMID, NULL) == -1)
230            {
231                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
232            }
233    
234            p_article_block_pool = NULL;
235    }
236    
237    int set_article_block_shm_readonly(void)
238    {
239            int shmid;
240            void *p_shm;
241            int i;
242    
243                  free(p_article_block_pool);          if (p_article_block_pool == NULL)
244                  p_article_block_pool = NULL;          {
245                    log_error("article_block_pool not initialized\n");
246                    return -1;
247          }          }
248    
249            for (i = 0; i < p_article_block_pool->shm_count; i++)
250            {
251                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
252    
253                    // Remap shared memory in read-only mode
254                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
255                    if (p_shm == (void *)-1)
256                    {
257                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
258                            return -2;
259                    }
260            }
261    
262            return 0;
263    }
264    
265    int detach_article_block_shm(void)
266    {
267            int shmid;
268    
269            if (p_article_block_pool == NULL)
270            {
271                    return -1;
272            }
273    
274            for (int i = 0; i < p_article_block_pool->shm_count; i++)
275            {
276                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
277                    {
278                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
279                            return -2;
280                    }
281            }
282    
283            shmid = p_article_block_pool->shmid;
284    
285            if (shmdt(p_article_block_pool) == -1)
286            {
287                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
288                    return -3;
289            }
290    
291            p_article_block_pool = NULL;
292    
293            return 0;
294  }  }
295    
296  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 300  ARTICLE *article_block_find_by_aid(int32 Line 397  ARTICLE *article_block_find_by_aid(int32
397                  }                  }
398          }          }
399    
400          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
401            {
402                    return NULL;
403            }
404    
405            return (p_block->articles + left); // found
406  }  }
407    
408  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 330  ARTICLE *article_block_find_by_index(int Line 432  ARTICLE *article_block_find_by_index(int
432          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
433  }  }
434    
435  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
436  {  {
437          int semid;          int semid;
438          int shmid;          int shmid;
# Line 341  extern int section_list_pool_init(const Line 443  extern int section_list_pool_init(const
443          union semun arg;          union semun arg;
444          int i;          int i;
445    
446          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool != NULL)
         {  
                 section_list_pool_cleanup();  
         }  
   
         p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));  
         if (p_section_list_pool == NULL)  
447          {          {
448                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                  section_list_cleanup();
                 return -1;  
449          }          }
450    
451          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 361  extern int section_list_pool_init(const Line 456  extern int section_list_pool_init(const
456                  return -3;                  return -3;
457          }          }
458    
459            // Allocate shared memory
460            size = sizeof(SECTION_LIST_POOL);
461            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
462            if (shmid == -1)
463            {
464                    log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
465                    return -3;
466            }
467            p_shm = shmat(shmid, NULL, 0);
468            if (p_shm == (void *)-1)
469            {
470                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
471                    return -3;
472            }
473    
474            p_section_list_pool = p_shm;
475            p_section_list_pool->shmid = shmid;
476            p_section_list_pool->section_count = 0;
477    
478            // Allocate semaphore as section locks
479          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
480          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
481          if (semid == -1)          if (semid == -1)
# Line 380  extern int section_list_pool_init(const Line 495  extern int section_list_pool_init(const
495                  }                  }
496          }          }
497    
498          section_list_pool_semid = semid;          p_section_list_pool->semid = semid;
499    
500          size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section;          p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
501          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
         if (shmid == -1)  
502          {          {
503                  log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
504                  return -3;                  return -2;
505          }          }
506          p_shm = shmat(shmid, NULL, 0);  
507            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
508            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
509            {
510                    log_error("trie_dict_create() OOM\n", BBS_max_section);
511                    return -2;
512            }
513    
514            return 0;
515    }
516    
517    void section_list_cleanup(void)
518    {
519            int shmid;
520    
521            if (p_section_list_pool == NULL)
522            {
523                    return;
524            }
525    
526            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
527            {
528                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
529                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
530            }
531    
532            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
533            {
534                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
535                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
536            }
537    
538            shmid = p_section_list_pool->shmid;
539    
540            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
541            {
542                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
543            }
544    
545            if (shmdt(p_section_list_pool) == -1)
546            {
547                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
548            }
549    
550            if (shmctl(shmid, IPC_RMID, NULL) == -1)
551            {
552                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
553            }
554    
555            p_section_list_pool = NULL;
556    }
557    
558    void section_list_ex_menu_set_cleanup(void)
559    {
560            int i;
561    
562            for (i = 0; i < p_section_list_pool->section_count; i++)
563            {
564                    if (p_section_list_pool->sections[i].ex_menu_tm > 0)
565                    {
566                            unload_menu(&(p_section_list_pool->sections[i].ex_menu_set));
567                    }
568            }
569    }
570    
571    int set_section_list_shm_readonly(void)
572    {
573            int shmid;
574            void *p_shm;
575    
576            if (p_section_list_pool == NULL)
577            {
578                    log_error("p_section_list_pool not initialized\n");
579                    return -1;
580            }
581    
582            shmid = p_section_list_pool->shmid;
583    
584            // Remap shared memory in read-only mode
585            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
586          if (p_shm == (void *)-1)          if (p_shm == (void *)-1)
587          {          {
588                  log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
589                  return -3;                  return -3;
590          }          }
591    
         section_list_pool_shmid = shmid;  
592          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
         section_list_count = 0;  
593    
594          p_trie_dict_section_by_name = trie_dict_create();          return 0;
595          if (p_trie_dict_section_by_name == NULL)  }
         {  
                 log_error("trie_dict_create() OOM\n", BBS_max_section);  
                 return -2;  
         }  
596    
597          p_trie_dict_section_by_sid = trie_dict_create();  int detach_section_list_shm(void)
598          if (p_trie_dict_section_by_sid == NULL)  {
599            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
600          {          {
601                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("shmdt(section_list_pool) error (%d)\n", errno);
602                  return -2;                  return -1;
603          }          }
604    
605            p_section_list_pool = NULL;
606    
607          return 0;          return 0;
608  }  }
609    
# Line 431  inline static void sid_to_str(int32_t si Line 621  inline static void sid_to_str(int32_t si
621          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
622  }  }
623    
624  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
625  {  {
626          SECTION_LIST *p_section;          SECTION_LIST *p_section;
627          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
628    
629          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
630          {          {
631                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
632                  return NULL;                  return NULL;
633          }          }
634    
635          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
636          {          {
637                  log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section);                  log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
638                  return NULL;                  return NULL;
639          }          }
640    
641          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
642    
643          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
644    
645          p_section->sid = sid;          p_section->sid = sid;
646            p_section->ex_menu_tm = 0;
647    
648          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
649          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
650    
651          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
652          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
653    
654          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
655          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
656    
657          if (trie_dict_set(p_trie_dict_section_by_name, sname, section_list_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
658          {          {
659                  log_error("trie_dict_set(section, %s, %d) error\n", sname, section_list_count);                  log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count);
660                  return NULL;                  return NULL;
661          }          }
662    
663          if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
664          {          {
665                  log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count);                  log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
                 log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]);  
666                  return NULL;                  return NULL;
667          }          }
668    
669          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
670    
671          section_list_count++;          p_section_list_pool->section_count++;
672    
673          return p_section;          return p_section;
674  }  }
# Line 494  void section_list_reset_articles(SECTION Line 684  void section_list_reset_articles(SECTION
684    
685          p_section->page_count = 0;          p_section->page_count = 0;
686          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
 }  
   
 void section_list_pool_cleanup(void)  
 {  
         if (p_trie_dict_section_by_name != NULL)  
         {  
                 trie_dict_destroy(p_trie_dict_section_by_name);  
                 p_trie_dict_section_by_name = NULL;  
         }  
   
         if (p_trie_dict_section_by_sid != NULL)  
         {  
                 trie_dict_destroy(p_trie_dict_section_by_sid);  
                 p_trie_dict_section_by_sid = NULL;  
         }  
687    
688          if (p_section_list_pool != NULL)          p_section->ontop_article_count = 0;
         {  
                 if (shmdt(p_section_list_pool) == -1)  
                 {  
                         log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno);  
                 }  
                 p_section_list_pool = NULL;  
   
                 if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)  
                 {  
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno);  
                 }  
   
                 if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1)  
                 {  
                         log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno);  
                 }  
         }  
   
         section_list_count = 0;  
689  }  }
690    
691  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
692  {  {
693          int64_t index;          int64_t index;
694            int ret;
695    
696          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)          if (p_section_list_pool == NULL)
697          {          {
698                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
699                  return NULL;                  return NULL;
700          }          }
701    
702          if (trie_dict_get(p_trie_dict_section_by_name, sname, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
703            if (ret < 0)
704          {          {
705                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
706                  return NULL;                  return NULL;
707          }          }
708            else if (ret == 0)
709            {
710                    return NULL;
711            }
712    
713          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
714  }  }
715    
716  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
717  {  {
718          int64_t index;          int64_t index;
719            int ret;
720          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
721    
722          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
723          {          {
724                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
725                  return NULL;                  return NULL;
# Line 564  SECTION_LIST *section_list_find_by_sid(i Line 727  SECTION_LIST *section_list_find_by_sid(i
727    
728          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
729    
730          if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
731            if (ret < 0)
732          {          {
733                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
734                  return NULL;                  return NULL;
735          }          }
736            else if (ret == 0)
737            {
738                    return NULL;
739            }
740    
741          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
742  }  }
743    
744  int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)  int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)
# Line 708  int section_list_append_article(SECTION_ Line 876  int section_list_append_article(SECTION_
876                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
877          }          }
878    
879            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
880            {
881                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
882                                      p_section->sid, p_article->aid);
883                    return -5;
884            }
885    
886          return 0;          return 0;
887  }  }
888    
# Line 719  int section_list_set_article_visible(SEC Line 894  int section_list_set_article_visible(SEC
894    
895          if (p_section == NULL)          if (p_section == NULL)
896          {          {
897                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
898                  return -2;                  return -1;
899          }          }
900    
901          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 731  int section_list_set_article_visible(SEC Line 906  int section_list_set_article_visible(SEC
906    
907          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
908          {          {
909                  log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);                  log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
910                  return -2;                  return -2;
911          }          }
912    
# Line 782  int section_list_set_article_visible(SEC Line 957  int section_list_set_article_visible(SEC
957          return affected_count;          return affected_count;
958  }  }
959    
960    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
961    {
962            int i;
963    
964            if (p_section == NULL || p_article == NULL)
965            {
966                    log_error("NULL pointer error\n");
967                    return -1;
968            }
969    
970            if (p_section->sid != p_article->sid)
971            {
972                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
973                    return -2;
974            }
975    
976            if (p_article->ontop)
977            {
978                    for (i = 0; i < p_section->ontop_article_count; i++)
979                    {
980                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
981                            {
982                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
983                                    return 0;
984                            }
985                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
986                            {
987                                    break;
988                            }
989                    }
990    
991                    // Remove the oldest one if the array of ontop articles is full
992                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
993                    {
994                            if (i == 0) // p_article is the oldest one
995                            {
996                                    return 0;
997                            }
998                            memmove((void *)(p_section->p_ontop_articles),
999                                            (void *)(p_section->p_ontop_articles + 1),
1000                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1001                            p_section->ontop_article_count--;
1002                            i--;
1003                    }
1004                    else
1005                    {
1006                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1007                                            (void *)(p_section->p_ontop_articles + i),
1008                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1009                    }
1010    
1011                    p_section->p_ontop_articles[i] = p_article;
1012                    p_section->ontop_article_count++;
1013    
1014                    // TODO: debug
1015            }
1016            else // ontop == 0
1017            {
1018                    for (i = 0; i < p_section->ontop_article_count; i++)
1019                    {
1020                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1021                            {
1022                                    break;
1023                            }
1024                    }
1025                    if (i == p_section->ontop_article_count) // not found
1026                    {
1027                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1028                            return 0;
1029                    }
1030    
1031                    memmove((void *)(p_section->p_ontop_articles + i),
1032                                    (void *)(p_section->p_ontop_articles + i + 1),
1033                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1034                    p_section->ontop_article_count--;
1035            }
1036    
1037            return 0;
1038    }
1039    
1040    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1041    {
1042            int page_count;
1043    
1044            if (p_section == NULL)
1045            {
1046                    log_error("NULL pointer error\n");
1047                    return -1;
1048            }
1049    
1050            page_count = p_section->page_count - 1 +
1051                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1052                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1053    
1054            return page_count;
1055    }
1056    
1057    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1058    {
1059            if (p_section == NULL)
1060            {
1061                    log_error("NULL pointer error\n");
1062                    return -1;
1063            }
1064    
1065            if (page_id < p_section->page_count - 1)
1066            {
1067                    return BBS_article_limit_per_page;
1068            }
1069            else // if (page_id >= p_section->page_count - 1)
1070            {
1071                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1072                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1073            }
1074    }
1075    
1076  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)
1077  {  {
1078          ARTICLE *p_article;          ARTICLE *p_article;
# Line 981  int section_list_calculate_page(SECTION_ Line 1272  int section_list_calculate_page(SECTION_
1272          return 0;          return 0;
1273  }  }
1274    
1275    int32_t article_block_last_aid(void)
1276    {
1277            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1278            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1279    
1280            return last_aid;
1281    }
1282    
1283    int article_block_article_count(void)
1284    {
1285            int ret;
1286    
1287            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1288            {
1289                    return -1;
1290            }
1291    
1292            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1293                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1294    
1295            return ret;
1296    }
1297    
1298  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1299  {  {
1300          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1021  int section_list_move_topic(SECTION_LIST Line 1335  int section_list_move_topic(SECTION_LIST
1335          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1336          int move_counter;          int move_counter;
1337    
1338          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1339          {          {
1340                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("section_list_move_topic() NULL pointer error\n");
1341                  return -1;                  return -1;
1342          }          }
1343    
1344            if (p_section_src->sid == p_section_dest->sid)
1345            {
1346                    log_error("section_list_move_topic() src and dest section are the same\n");
1347                    return -1;
1348            }
1349    
1350          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1351          {          {
1352                  log_error("section_list_move_topic() error: article %d not found in block\n", aid);                  log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1353                  return -2;                  return -2;
1354          }          }
1355    
# Line 1051  int section_list_move_topic(SECTION_LIST Line 1371  int section_list_move_topic(SECTION_LIST
1371          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1372          if (move_article_count <= 0)          if (move_article_count <= 0)
1373          {          {
1374                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1375                  return -2;                  return -2;
1376          }          }
1377    
# Line 1070  int section_list_move_topic(SECTION_LIST Line 1390  int section_list_move_topic(SECTION_LIST
1390          {          {
1391                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1392                  {                  {
1393                          log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1394                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1395                          return -2;                          p_article = p_article->p_topic_next;
1396                            continue;
1397                  }                  }
1398    
1399                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1098  int section_list_move_topic(SECTION_LIST Line 1419  int section_list_move_topic(SECTION_LIST
1419    
1420                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1421                  {                  {
1422                          log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);                          log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1423                          return -4;                          p_article = p_article->p_topic_next;
1424                            continue;
1425                  }                  }
1426    
1427                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1177  int section_list_move_topic(SECTION_LIST Line 1499  int section_list_move_topic(SECTION_LIST
1499    
1500          if (p_section_dest->article_count - dest_article_count_old != move_article_count)          if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1501          {          {
1502                  log_error("section_list_move_topic() error: count of moved articles %d != %d\n",                  log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1503                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1504          }          }
1505    
# Line 1217  int get_section_index(SECTION_LIST *p_se Line 1539  int get_section_index(SECTION_LIST *p_se
1539          }          }
1540          else          else
1541          {          {
1542                  index = (int)(p_section - p_section_list_pool);                  index = (int)(p_section - p_section_list_pool->sections);
1543                  if (index < 0 || index >= BBS_max_section)                  if (index < 0 || index >= BBS_max_section)
1544                  {                  {
1545                          log_error("get_section_index(%d) error: index out of range\n", index);                          log_error("get_section_index(%d) error: index out of range\n", index);
# Line 1252  int section_list_try_rd_lock(SECTION_LIS Line 1574  int section_list_try_rd_lock(SECTION_LIS
1574          // Read lock on any specific section will also acquire single read lock on "all section"          // Read lock on any specific section will also acquire single read lock on "all section"
1575          // so that write lock on all section only need to acquire single write on on "all section"          // so that write lock on all section only need to acquire single write on on "all section"
1576          // rather than to acquire multiple write locks on all the available sections.          // rather than to acquire multiple write locks on all the available sections.
1577          if (index == BBS_max_section)          if (index != BBS_max_section)
1578          {          {
1579                  sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section                  sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1580                  sops[2].sem_op = 0;                                                // wait until unlocked                  sops[2].sem_op = 0;                                                // wait until unlocked
# Line 1266  int section_list_try_rd_lock(SECTION_LIS Line 1588  int section_list_try_rd_lock(SECTION_LIS
1588          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1589          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1590    
1591          ret = semtimedop(section_list_pool_semid, sops, (index == BBS_max_section ? 4 : 2), &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1592          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1593          {          {
1594                  log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);                  log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
# Line 1303  int section_list_try_rw_lock(SECTION_LIS Line 1625  int section_list_try_rw_lock(SECTION_LIS
1625          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1626          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1627    
1628          ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1629          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1630          {          {
1631                  log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);                  log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
# Line 1328  int section_list_rd_unlock(SECTION_LIST Line 1650  int section_list_rd_unlock(SECTION_LIST
1650          sops[0].sem_op = -1;                                               // unlock          sops[0].sem_op = -1;                                               // unlock
1651          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1652    
1653          // The same reason as section_list_try_rd_lock()          if (index != BBS_max_section)
         if (index == BBS_max_section)  
1654          {          {
1655                  sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section                  sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1656                  sops[1].sem_op = -1;                                     // unlock                  sops[1].sem_op = -1;                                     // unlock
1657                  sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1658          }          }
1659    
1660          ret = semop(section_list_pool_semid, sops, (index == BBS_max_section ? 2 : 1));          ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1661          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1662          {          {
1663                  log_error("semop(index = %d, unlock read) error %d\n", index, errno);                  log_error("semop(index = %d, unlock read) error %d\n", index, errno);
# Line 1361  int section_list_rw_unlock(SECTION_LIST Line 1682  int section_list_rw_unlock(SECTION_LIST
1682          sops[0].sem_op = -1;                                                       // unlock          sops[0].sem_op = -1;                                                       // unlock
1683          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1684    
1685          ret = semop(section_list_pool_semid, sops, 1);          ret = semop(p_section_list_pool->semid, sops, 1);
1686          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1687          {          {
1688                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1689          }          }
1690    
1691          return ret;          return ret;
1692    }
1693    
1694    int section_list_rd_lock(SECTION_LIST *p_section)
1695    {
1696            int timer = 0;
1697            int sid = (p_section == NULL ? 0 : p_section->sid);
1698            int ret = -1;
1699    
1700            while (!SYS_server_exit)
1701            {
1702                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1703                    if (ret == 0) // success
1704                    {
1705                            break;
1706                    }
1707                    else if (errno == EAGAIN || errno == EINTR) // retry
1708                    {
1709                            timer++;
1710                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1711                            {
1712                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", sid, timer);
1713                            }
1714                    }
1715                    else // failed
1716                    {
1717                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1718                            break;
1719                    }
1720            }
1721    
1722            return ret;
1723    }
1724    
1725    int section_list_rw_lock(SECTION_LIST *p_section)
1726    {
1727            int timer = 0;
1728            int sid = (p_section == NULL ? 0 : p_section->sid);
1729            int ret = -1;
1730    
1731            while (!SYS_server_exit)
1732            {
1733                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1734                    if (ret == 0) // success
1735                    {
1736                            break;
1737                    }
1738                    else if (errno == EAGAIN || errno == EINTR) // retry
1739                    {
1740                            timer++;
1741                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1742                            {
1743                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", sid, timer);
1744                            }
1745                    }
1746                    else // failed
1747                    {
1748                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1749                            break;
1750                    }
1751            }
1752    
1753            return ret;
1754  }  }


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

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