/[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.14 by sysadm, Sat May 24 03:32:32 2025 UTC Revision 1.55 by sysadm, Wed Nov 5 04:19:21 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                             section_list.c  -  description  /*
3                                                           -------------------   * section_list
4          Copyright            : (C) 2004-2025 by Leaflet   *   - data models and basic operations of section and article
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #include "section_list.h"  
9  #include "log.h"  #include "log.h"
10    #include "section_list.h"
11  #include "trie_dict.h"  #include "trie_dict.h"
12    #include "user_list.h"
13    #include <errno.h>
14    #include <signal.h>
15  #include <stdio.h>  #include <stdio.h>
16    #include <stdlib.h>
17  #include <string.h>  #include <string.h>
 #include <signal.h>  
18  #include <unistd.h>  #include <unistd.h>
19  #include <stdlib.h>  #include <sys/ipc.h>
 #include <errno.h>  
20  #include <sys/param.h>  #include <sys/param.h>
21    #include <sys/sem.h>
22  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
23    
24  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #ifdef _SEM_SEMUN_UNDEFINED
25  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  union semun
26  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  {
27            int val;                           /* Value for SETVAL */
28            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
29            unsigned short *array; /* Array for GETALL, SETALL */
30            struct seminfo *__buf; /* Buffer for IPC_INFO
31                                                              (Linux-specific) */
32    };
33    #endif // #ifdef _SEM_SEMUN_UNDEFINED
34    
35    enum _section_list_constant_t
36    {
37            SECTION_TRY_LOCK_WAIT_TIME = 1, // second
38            SECTION_TRY_LOCK_TIMES = 10,
39    
40            ARTICLE_BLOCK_PER_SHM = 1000,           // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
41            ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id)
42            ARTICLE_BLOCK_PER_POOL = (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT),
43    
44  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic          CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections
45    
46  #define SID_STR_LEN 5 // 32-bit + NULL          SID_STR_LEN = 5, // 32-bit + NULL
47    };
48    
49  struct article_block_t  struct article_block_t
50  {  {
51          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[BBS_article_count_per_block];
52          int32_t article_count;          int article_count;
53          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
54  };  };
55  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
56    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
57  struct article_block_pool_t  struct article_block_pool_t
58  {  {
59          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
60            struct
61            {
62                    int shmid;
63                    void *p_shm;
64            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
65          int shm_count;          int shm_count;
66          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
67          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
68          int32_t block_count;          int block_count;
69  };  };
70  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
71    
72  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
73    
74  static int section_list_pool_shmid;  SECTION_LIST_POOL *p_section_list_pool = NULL;
 static SECTION_LIST *p_section_list_pool = NULL;  
 static int section_list_count = 0;  
 static TRIE_NODE *p_trie_dict_section_by_name = NULL;  
 static TRIE_NODE *p_trie_dict_section_by_sid = NULL;  
75    
76  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
77  {  {
# Line 86  int article_block_init(const char *filen Line 91  int article_block_init(const char *filen
91                  return -1;                  return -1;
92          }          }
93    
94          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
95          {          {
96                  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);
97                  return -2;                  return -2;
98          }          }
99    
100          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
101          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
102            key = ftok(filename, proj_id);
103            if (key == -1)
104          {          {
105                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
106                  return -2;                  return -3;
107          }          }
108    
109          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
110            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
111            if (shmid == -1)
112            {
113                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
114                    return -3;
115            }
116            p_shm = shmat(shmid, NULL, 0);
117            if (p_shm == (void *)-1)
118            {
119                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
120                    return -3;
121            }
122    
123            p_article_block_pool = p_shm;
124            p_article_block_pool->shmid = shmid;
125    
126          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
127          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
128    
# Line 108  int article_block_init(const char *filen Line 131  int article_block_init(const char *filen
131                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
132                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
133    
134                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
135                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
136                  if (key == -1)                  if (key == -1)
137                  {                  {
# Line 117  int article_block_init(const char *filen Line 140  int article_block_init(const char *filen
140                          return -3;                          return -3;
141                  }                  }
142    
143                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
144                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
145                  if (shmid == -1)                  if (shmid == -1)
146                  {                  {
# Line 161  int article_block_init(const char *filen Line 184  int article_block_init(const char *filen
184    
185  void article_block_cleanup(void)  void article_block_cleanup(void)
186  {  {
187          if (p_article_block_pool != NULL)          int shmid;
188    
189            if (p_article_block_pool == NULL)
190            {
191                    return;
192            }
193    
194            for (int i = 0; i < p_article_block_pool->shm_count; i++)
195          {          {
196                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
197                  {                  {
198                          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);
199                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
200    
201                          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)
202                          {                  {
203                                  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);
204                          }                  }
205            }
206    
207            shmid = p_article_block_pool->shmid;
208    
209            if (shmdt(p_article_block_pool) == -1)
210            {
211                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
212            }
213    
214            if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
215            {
216                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
217            }
218    
219            p_article_block_pool = NULL;
220    }
221    
222    int set_article_block_shm_readonly(void)
223    {
224            int shmid;
225            void *p_shm;
226            int i;
227    
228            if (p_article_block_pool == NULL)
229            {
230                    log_error("article_block_pool not initialized\n");
231                    return -1;
232            }
233    
234            for (i = 0; i < p_article_block_pool->shm_count; i++)
235            {
236                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
237    
238                    // Remap shared memory in read-only mode
239                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
240                    if (p_shm == (void *)-1)
241                    {
242                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
243                            return -2;
244                  }                  }
245            }
246    
247                  free(p_article_block_pool);          return 0;
248                  p_article_block_pool = NULL;  }
249    
250    int detach_article_block_shm(void)
251    {
252            int shmid;
253    
254            if (p_article_block_pool == NULL)
255            {
256                    return -1;
257            }
258    
259            for (int i = 0; i < p_article_block_pool->shm_count; i++)
260            {
261                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
262                    {
263                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
264                            return -2;
265                    }
266            }
267    
268            shmid = p_article_block_pool->shmid;
269    
270            if (shmdt(p_article_block_pool) == -1)
271            {
272                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
273                    return -3;
274          }          }
275    
276            p_article_block_pool = NULL;
277    
278            return 0;
279  }  }
280    
281  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 241  ARTICLE *article_block_find_by_aid(int32 Line 338  ARTICLE *article_block_find_by_aid(int32
338          }          }
339    
340          left = 0;          left = 0;
341          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
342    
343          // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]          // aid in the range [ head aid of blocks[left], tail aid of blocks[right] ]
344          while (left < right - 1)          while (left < right)
345          {          {
346                  // get block offset no less than mid value of left and right block offsets                  // get block offset no less than mid value of left and right block offsets
347                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (left + right) % 2;
   
                 if (mid >= p_article_block_pool->block_count)  
                 {  
                         log_error("block(mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
348    
349                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
350                  {                  {
351                          right = mid;                          right = mid - 1;
352                  }                  }
353                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
354                  {                  {
355                          left = mid;                          left = mid;
356                  }                  }
# Line 285  ARTICLE *article_block_find_by_aid(int32 Line 376  ARTICLE *article_block_find_by_aid(int32
376                  }                  }
377          }          }
378    
379          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
380            {
381                    return NULL;
382            }
383    
384            return (p_block->articles + left); // found
385  }  }
386    
387  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 298  ARTICLE *article_block_find_by_index(int Line 394  ARTICLE *article_block_find_by_index(int
394                  return NULL;                  return NULL;
395          }          }
396    
397          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)          if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count)
398          {          {
399                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);
400                  return NULL;                  return NULL;
401          }          }
402    
403          p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK];          p_block = p_article_block_pool->p_block[index / BBS_article_count_per_block];
404    
405          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
406          {          {
407                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);
408                  return NULL;                  return NULL;
409          }          }
410    
411          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
412  }  }
413    
414  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
415  {  {
416            int semid;
417          int shmid;          int shmid;
418          int proj_id;          int proj_id;
419          key_t key;          key_t key;
420          size_t size;          size_t size;
421          void *p_shm;          void *p_shm;
422            union semun arg;
423            int i;
424    
425          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)  
426          {          {
427                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                  section_list_cleanup();
                 return -1;  
428          }          }
429    
430          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 343  extern int section_list_pool_init(const Line 435  extern int section_list_pool_init(const
435                  return -3;                  return -3;
436          }          }
437    
438          size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section;          // Allocate shared memory
439            size = sizeof(SECTION_LIST_POOL);
440          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
441          if (shmid == -1)          if (shmid == -1)
442          {          {
443                  log_error("shmget(section_list_pool, size = %d) error (%d)\n", size, errno);                  log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
444                  return -3;                  return -3;
445          }          }
446          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
# Line 357  extern int section_list_pool_init(const Line 450  extern int section_list_pool_init(const
450                  return -3;                  return -3;
451          }          }
452    
         section_list_pool_shmid = shmid;  
453          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
454          section_list_count = 0;          p_section_list_pool->shmid = shmid;
455            p_section_list_pool->section_count = 0;
456    
457            // Allocate semaphore as section locks
458            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
459            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
460            if (semid == -1)
461            {
462                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
463                    return -3;
464            }
465    
466          p_trie_dict_section_by_name = trie_dict_create();          // Initialize sem value to 0
467          if (p_trie_dict_section_by_name == NULL)          arg.val = 0;
468            for (i = 0; i < size; i++)
469            {
470                    if (semctl(semid, i, SETVAL, arg) == -1)
471                    {
472                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
473                            return -3;
474                    }
475            }
476    
477            p_section_list_pool->semid = semid;
478    
479            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
480            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
481          {          {
482                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
483                  return -2;                  return -2;
484          }          }
485    
486          p_trie_dict_section_by_sid = trie_dict_create();          p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
487          if (p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
488          {          {
489                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
490                  return -2;                  return -2;
# Line 378  extern int section_list_pool_init(const Line 493  extern int section_list_pool_init(const
493          return 0;          return 0;
494  }  }
495    
496    void section_list_cleanup(void)
497    {
498            int shmid;
499    
500            if (p_section_list_pool == NULL)
501            {
502                    return;
503            }
504    
505            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
506            {
507                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
508                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
509            }
510    
511            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
512            {
513                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
514                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
515            }
516    
517            shmid = p_section_list_pool->shmid;
518    
519            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
520            {
521                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
522            }
523    
524            if (shmdt(p_section_list_pool) == -1)
525            {
526                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
527            }
528    
529            if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
530            {
531                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
532            }
533    
534            p_section_list_pool = NULL;
535    }
536    
537    int set_section_list_shm_readonly(void)
538    {
539            int shmid;
540            void *p_shm;
541    
542            if (p_section_list_pool == NULL)
543            {
544                    log_error("p_section_list_pool not initialized\n");
545                    return -1;
546            }
547    
548            shmid = p_section_list_pool->shmid;
549    
550            // Remap shared memory in read-only mode
551            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
552            if (p_shm == (void *)-1)
553            {
554                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
555                    return -3;
556            }
557    
558            p_section_list_pool = p_shm;
559    
560            return 0;
561    }
562    
563    int detach_section_list_shm(void)
564    {
565            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
566            {
567                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
568                    return -1;
569            }
570    
571            p_section_list_pool = NULL;
572    
573            return 0;
574    }
575    
576  inline static void sid_to_str(int32_t sid, char *p_sid_str)  inline static void sid_to_str(int32_t sid, char *p_sid_str)
577  {  {
578          uint32_t u_sid;          uint32_t u_sid;
# Line 392  inline static void sid_to_str(int32_t si Line 587  inline static void sid_to_str(int32_t si
587          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
588  }  }
589    
590  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)
591  {  {
592          SECTION_LIST *p_section;          SECTION_LIST *p_section;
593          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
594    
595          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)
596          {          {
597                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
598                  return NULL;                  return NULL;
599          }          }
600    
601          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
602          {          {
603                  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);
604                  return NULL;                  return NULL;
605          }          }
606    
607          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
608    
609          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
610    
611          p_section->sid = sid;          p_section->sid = sid;
612            p_section->ex_menu_tm = 0;
613    
614          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
615          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
616    
617          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
618          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
619    
620          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);
621          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
622    
623          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)
624          {          {
625                  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);
626                  return NULL;                  return NULL;
627          }          }
628    
629          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)
630          {          {
631                  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]);  
632                  return NULL;                  return NULL;
633          }          }
634    
635          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
636    
637          section_list_count++;          p_section_list_pool->section_count++;
638    
639          return p_section;          return p_section;
640  }  }
641    
642    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
643    {
644            int64_t index;
645    
646            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
647            {
648                    log_error("NULL pointer error\n");
649                    return -1;
650            }
651    
652            index = get_section_index(p_section);
653    
654            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
655            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
656    
657            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
658            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
659    
660            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
661            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
662    
663            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
664            {
665                    log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
666                    return -2;
667            }
668    
669            return 0;
670    }
671    
672  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
673  {  {
674          p_section->article_count = 0;          p_section->article_count = 0;
# Line 455  void section_list_reset_articles(SECTION Line 680  void section_list_reset_articles(SECTION
680    
681          p_section->page_count = 0;          p_section->page_count = 0;
682          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
 }  
683    
684  void section_list_pool_cleanup(void)          p_section->ontop_article_count = 0;
 {  
         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;  
         }  
   
         if (p_section_list_pool != NULL)  
         {  
                 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);  
                 }  
         }  
   
         section_list_count = 0;  
685  }  }
686    
687  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
688  {  {
689          int64_t index;          int64_t index;
690            int ret;
691    
692          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)          if (p_section_list_pool == NULL)
693          {          {
694                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
695                  return NULL;                  return NULL;
696          }          }
697    
698          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);
699            if (ret < 0)
700          {          {
701                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
702                  return NULL;                  return NULL;
703          }          }
704            else if (ret == 0)
705            {
706                    return NULL;
707            }
708    
709          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
710  }  }
711    
712  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
713  {  {
714          int64_t index;          int64_t index;
715            int ret;
716          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
717    
718          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
719          {          {
720                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
721                  return NULL;                  return NULL;
# Line 520  SECTION_LIST *section_list_find_by_sid(i Line 723  SECTION_LIST *section_list_find_by_sid(i
723    
724          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
725    
726          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);
727            if (ret < 0)
728          {          {
729                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
730                  return NULL;                  return NULL;
731          }          }
732            else if (ret == 0)
733            {
734                    return NULL;
735            }
736    
737          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
738  }  }
739    
740  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 539  int section_list_append_article(SECTION_ Line 747  int section_list_append_article(SECTION_
747    
748          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
749          {          {
750                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
751                  return -1;                  return -1;
752          }          }
753    
# Line 562  int section_list_append_article(SECTION_ Line 770  int section_list_append_article(SECTION_
770          }          }
771    
772          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
773                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK)                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= BBS_article_count_per_block)
774          {          {
775                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
776                  {                  {
# Line 572  int section_list_append_article(SECTION_ Line 780  int section_list_append_article(SECTION_
780    
781                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
782                  {                  {
783                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid;                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[BBS_article_count_per_block - 1].aid;
784                  }                  }
785    
786                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;
# Line 651  int section_list_append_article(SECTION_ Line 859  int section_list_append_article(SECTION_
859          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
860    
861          // Update page          // Update page
862          if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||          if ((p_article->visible && p_section->last_page_visible_article_count == BBS_article_limit_per_page) ||
863                  p_section->article_count == 1)                  p_section->page_count == 0)
864          {          {
865                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
866                  p_section->page_count++;                  p_section->page_count++;
# Line 664  int section_list_append_article(SECTION_ Line 872  int section_list_append_article(SECTION_
872                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
873          }          }
874    
875            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
876            {
877                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
878                                      p_section->sid, p_article->aid);
879                    return -5;
880            }
881    
882          return 0;          return 0;
883  }  }
884    
# Line 675  int section_list_set_article_visible(SEC Line 890  int section_list_set_article_visible(SEC
890    
891          if (p_section == NULL)          if (p_section == NULL)
892          {          {
893                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
894                  return -2;                  return -1;
895          }          }
896    
897          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 687  int section_list_set_article_visible(SEC Line 902  int section_list_set_article_visible(SEC
902    
903          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
904          {          {
905                  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);
906                  return -2;                  return -2;
907          }          }
908    
# Line 700  int section_list_set_article_visible(SEC Line 915  int section_list_set_article_visible(SEC
915          {          {
916                  p_section->visible_article_count--;                  p_section->visible_article_count--;
917    
918                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
919                    {
920                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
921                    }
922    
923                  if (p_article->tid == 0)                  if (p_article->tid == 0)
924                  {                  {
925                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 718  int section_list_set_article_visible(SEC Line 938  int section_list_set_article_visible(SEC
938                                          p_reply->visible = 0;                                          p_reply->visible = 0;
939                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
940                                          affected_count++;                                          affected_count++;
941    
942                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
943                                            {
944                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
945                                            }
946                                  }                                  }
947                          }                          }
948                  }                  }
# Line 730  int section_list_set_article_visible(SEC Line 955  int section_list_set_article_visible(SEC
955                  {                  {
956                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
957                  }                  }
958    
959                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
960                    {
961                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
962                    }
963          }          }
964    
965          p_article->visible = visible;          p_article->visible = visible;
# Line 738  int section_list_set_article_visible(SEC Line 968  int section_list_set_article_visible(SEC
968          return affected_count;          return affected_count;
969  }  }
970    
971    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
972    {
973            int i;
974    
975            if (p_section == NULL || p_article == NULL)
976            {
977                    log_error("NULL pointer error\n");
978                    return -1;
979            }
980    
981            if (p_section->sid != p_article->sid)
982            {
983                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
984                    return -2;
985            }
986    
987            if (p_article->ontop)
988            {
989                    for (i = 0; i < p_section->ontop_article_count; i++)
990                    {
991                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
992                            {
993                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
994                                    return 0;
995                            }
996                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
997                            {
998                                    break;
999                            }
1000                    }
1001    
1002                    // Remove the oldest one if the array of ontop articles is full
1003                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1004                    {
1005                            if (i == 0) // p_article is the oldest one
1006                            {
1007                                    return 0;
1008                            }
1009                            memmove((void *)(p_section->p_ontop_articles),
1010                                            (void *)(p_section->p_ontop_articles + 1),
1011                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1012                            p_section->ontop_article_count--;
1013                            i--;
1014                    }
1015                    else
1016                    {
1017                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1018                                            (void *)(p_section->p_ontop_articles + i),
1019                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1020                    }
1021    
1022                    p_section->p_ontop_articles[i] = p_article;
1023                    p_section->ontop_article_count++;
1024    
1025                    // TODO: debug
1026            }
1027            else // ontop == 0
1028            {
1029                    for (i = 0; i < p_section->ontop_article_count; i++)
1030                    {
1031                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1032                            {
1033                                    break;
1034                            }
1035                    }
1036                    if (i == p_section->ontop_article_count) // not found
1037                    {
1038                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1039                            return 0;
1040                    }
1041    
1042                    memmove((void *)(p_section->p_ontop_articles + i),
1043                                    (void *)(p_section->p_ontop_articles + i + 1),
1044                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1045                    p_section->ontop_article_count--;
1046            }
1047    
1048            return 0;
1049    }
1050    
1051    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1052    {
1053            int page_count;
1054    
1055            if (p_section == NULL)
1056            {
1057                    log_error("NULL pointer error\n");
1058                    return -1;
1059            }
1060    
1061            page_count = p_section->page_count - 1 +
1062                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1063                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page ? 1 : 0);
1064    
1065            if (page_count < 0)
1066            {
1067                    page_count = 0;
1068            }
1069    
1070            return page_count;
1071    }
1072    
1073    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1074    {
1075            if (p_section == NULL)
1076            {
1077                    log_error("NULL pointer error\n");
1078                    return -1;
1079            }
1080    
1081            if (page_id < p_section->page_count - 1)
1082            {
1083                    return BBS_article_limit_per_page;
1084            }
1085            else // if (page_id >= p_section->page_count - 1)
1086            {
1087                    return MIN(MAX(0,
1088                                               (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1089                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1090                                       BBS_article_limit_per_page);
1091            }
1092    }
1093    
1094  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)
1095  {  {
1096          ARTICLE *p_article;          ARTICLE *p_article;
# Line 751  ARTICLE *section_list_find_article_with_ Line 1104  ARTICLE *section_list_find_article_with_
1104    
1105          if (p_section == NULL)          if (p_section == NULL)
1106          {          {
1107                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1108                  return NULL;                  return NULL;
1109          }          }
1110    
# Line 763  ARTICLE *section_list_find_article_with_ Line 1116  ARTICLE *section_list_find_article_with_
1116          }          }
1117    
1118          left = 0;          left = 0;
1119          right = p_section->page_count;          right = p_section->page_count - 1;
1120    
1121          // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]          // aid in the range [ head aid of pages[left], tail aid of pages[right] ]
1122          while (left < right - 1)          while (left < right)
1123          {          {
1124                  // get page id no less than mid value of left page id and right page id                  // get page id no less than mid value of left page id and right page id
1125                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (left + right) % 2;
   
                 if (mid >= p_section->page_count)  
                 {  
                         log_error("page id (mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
1126    
1127                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1128                  {                  {
1129                          right = mid;                          right = mid - 1;
1130                  }                  }
1131                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1132                  {                  {
1133                          left = mid;                          left = mid;
1134                  }                  }
# Line 843  int section_list_calculate_page(SECTION_ Line 1190  int section_list_calculate_page(SECTION_
1190    
1191          if (p_section == NULL)          if (p_section == NULL)
1192          {          {
1193                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1194                  return -1;                  return -1;
1195          }          }
1196    
# Line 932  int section_list_calculate_page(SECTION_ Line 1279  int section_list_calculate_page(SECTION_
1279          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1280    
1281          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1282          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1283                                                                                                              ? visible_article_count
1284                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1285    
1286          return 0;          return 0;
1287  }  }
1288    
1289    int32_t article_block_last_aid(void)
1290    {
1291            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1292            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1293    
1294            return last_aid;
1295    }
1296    
1297    int article_block_article_count(void)
1298    {
1299            int ret;
1300    
1301            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1302            {
1303                    return -1;
1304            }
1305    
1306            ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1307                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1308    
1309            return ret;
1310    }
1311    
1312  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1313  {  {
1314          ARTICLE *p_article;          ARTICLE *p_article;
# Line 952  int article_count_of_topic(int32_t aid) Line 1324  int article_count_of_topic(int32_t aid)
1324    
1325          do          do
1326          {          {
1327                    if (p_article->tid != 0 && p_article->tid != aid)
1328                    {
1329                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1330                            break;
1331                    }
1332    
1333                  article_count++;                  article_count++;
1334                  p_article = p_article->p_topic_next;                  p_article = p_article->p_topic_next;
1335          } while (p_article->aid != aid);          } while (p_article->aid != aid);
# Line 971  int section_list_move_topic(SECTION_LIST Line 1349  int section_list_move_topic(SECTION_LIST
1349          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1350          int move_counter;          int move_counter;
1351    
1352          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1353          {          {
1354                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1355                    return -1;
1356            }
1357    
1358            if (p_section_src->sid == p_section_dest->sid)
1359            {
1360                    log_error("section_list_move_topic() src and dest section are the same\n");
1361                  return -1;                  return -1;
1362          }          }
1363    
1364          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1365          {          {
1366                  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);
1367                  return -2;                  return -2;
1368          }          }
1369    
# Line 1001  int section_list_move_topic(SECTION_LIST Line 1385  int section_list_move_topic(SECTION_LIST
1385          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1386          if (move_article_count <= 0)          if (move_article_count <= 0)
1387          {          {
1388                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1389                  return -2;                  return -2;
1390          }          }
1391    
# Line 1020  int section_list_move_topic(SECTION_LIST Line 1404  int section_list_move_topic(SECTION_LIST
1404          {          {
1405                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1406                  {                  {
1407                          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",
1408                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1409                          return -2;                          p_article = p_article->p_topic_next;
1410                            continue;
1411                  }                  }
1412    
1413                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1048  int section_list_move_topic(SECTION_LIST Line 1433  int section_list_move_topic(SECTION_LIST
1433    
1434                  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)
1435                  {                  {
1436                          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);
1437                          return -4;                          p_article = p_article->p_topic_next;
1438                            continue;
1439                  }                  }
1440    
1441                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1127  int section_list_move_topic(SECTION_LIST Line 1513  int section_list_move_topic(SECTION_LIST
1513    
1514          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)
1515          {          {
1516                  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",
1517                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1518          }          }
1519    
# Line 1150  int section_list_move_topic(SECTION_LIST Line 1536  int section_list_move_topic(SECTION_LIST
1536    
1537          return move_article_count;          return move_article_count;
1538  }  }
1539    
1540    int get_section_index(SECTION_LIST *p_section)
1541    {
1542            int index;
1543    
1544            if (p_section_list_pool == NULL)
1545            {
1546                    log_error("get_section_index() error: uninitialized\n");
1547                    return -1;
1548            }
1549    
1550            if (p_section == NULL)
1551            {
1552                    index = BBS_max_section;
1553            }
1554            else
1555            {
1556                    index = (int)(p_section - p_section_list_pool->sections);
1557                    if (index < 0 || index >= BBS_max_section)
1558                    {
1559                            log_error("get_section_index(%d) error: index out of range\n", index);
1560                            return -2;
1561                    }
1562            }
1563    
1564            return index;
1565    }
1566    
1567    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1568    {
1569            if (p_section == NULL)
1570            {
1571                    log_error("NULL pointer error\n");
1572                    return -1;
1573            }
1574    
1575            if (section_list_rd_lock(p_section) < 0)
1576            {
1577                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1578                    return -2;
1579            }
1580    
1581            if (sname != NULL)
1582            {
1583                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1584            }
1585            if (stitle != NULL)
1586            {
1587                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1588            }
1589            if (master_list != NULL)
1590            {
1591                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1592            }
1593    
1594            // release lock of section
1595            if (section_list_rd_unlock(p_section) < 0)
1596            {
1597                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1598                    return -2;
1599            }
1600    
1601            return 0;
1602    }
1603    
1604    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1605    {
1606            int index;
1607            struct sembuf sops[4];
1608            struct timespec timeout;
1609            int ret;
1610    
1611            index = get_section_index(p_section);
1612            if (index < 0)
1613            {
1614                    return -2;
1615            }
1616    
1617            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1618            sops[0].sem_op = 0;                                                                // wait until unlocked
1619            sops[0].sem_flg = 0;
1620    
1621            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1622            sops[1].sem_op = 1;                                                        // lock
1623            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1624    
1625            // Read lock on any specific section will also acquire single read lock on "all section"
1626            // so that write lock on all section only need to acquire single write on on "all section"
1627            // rather than to acquire multiple write locks on all the available sections.
1628            if (index != BBS_max_section)
1629            {
1630                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1631                    sops[2].sem_op = 0;                                                // wait until unlocked
1632                    sops[2].sem_flg = 0;
1633    
1634                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1635                    sops[3].sem_op = 1;                                        // lock
1636                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1637            }
1638    
1639            timeout.tv_sec = wait_sec;
1640            timeout.tv_nsec = 0;
1641    
1642            ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1643            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1644            {
1645                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1646            }
1647    
1648            return ret;
1649    }
1650    
1651    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1652    {
1653            int index;
1654            struct sembuf sops[3];
1655            struct timespec timeout;
1656            int ret;
1657    
1658            index = get_section_index(p_section);
1659            if (index < 0)
1660            {
1661                    return -2;
1662            }
1663    
1664            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1665            sops[0].sem_op = 0;                                                                // wait until unlocked
1666            sops[0].sem_flg = 0;
1667    
1668            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1669            sops[1].sem_op = 1;                                                                // lock
1670            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1671    
1672            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1673            sops[2].sem_op = 0;                                                        // wait until unlocked
1674            sops[2].sem_flg = 0;
1675    
1676            timeout.tv_sec = wait_sec;
1677            timeout.tv_nsec = 0;
1678    
1679            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1680            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1681            {
1682                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1683            }
1684    
1685            return ret;
1686    }
1687    
1688    int section_list_rd_unlock(SECTION_LIST *p_section)
1689    {
1690            int index;
1691            struct sembuf sops[2];
1692            int ret;
1693    
1694            index = get_section_index(p_section);
1695            if (index < 0)
1696            {
1697                    return -2;
1698            }
1699    
1700            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1701            sops[0].sem_op = -1;                                               // unlock
1702            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1703    
1704            if (index != BBS_max_section)
1705            {
1706                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1707                    sops[1].sem_op = -1;                                     // unlock
1708                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1709            }
1710    
1711            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1712            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1713            {
1714                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1715            }
1716    
1717            return ret;
1718    }
1719    
1720    int section_list_rw_unlock(SECTION_LIST *p_section)
1721    {
1722            int index;
1723            struct sembuf sops[1];
1724            int ret;
1725    
1726            index = get_section_index(p_section);
1727            if (index < 0)
1728            {
1729                    return -2;
1730            }
1731    
1732            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1733            sops[0].sem_op = -1;                                                       // unlock
1734            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1735    
1736            ret = semop(p_section_list_pool->semid, sops, 1);
1737            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1738            {
1739                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1740            }
1741    
1742            return ret;
1743    }
1744    
1745    int section_list_rd_lock(SECTION_LIST *p_section)
1746    {
1747            int timer = 0;
1748            int sid = (p_section == NULL ? 0 : p_section->sid);
1749            int ret = -1;
1750    
1751            while (!SYS_server_exit)
1752            {
1753                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1754                    if (ret == 0) // success
1755                    {
1756                            break;
1757                    }
1758                    else if (errno == EAGAIN || errno == EINTR) // retry
1759                    {
1760                            timer++;
1761                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1762                            {
1763                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);
1764                            }
1765                    }
1766                    else // failed
1767                    {
1768                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1769                            break;
1770                    }
1771            }
1772    
1773            return ret;
1774    }
1775    
1776    int section_list_rw_lock(SECTION_LIST *p_section)
1777    {
1778            int timer = 0;
1779            int sid = (p_section == NULL ? 0 : p_section->sid);
1780            int ret = -1;
1781    
1782            while (!SYS_server_exit)
1783            {
1784                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1785                    if (ret == 0) // success
1786                    {
1787                            break;
1788                    }
1789                    else if (errno == EAGAIN || errno == EINTR) // retry
1790                    {
1791                            timer++;
1792                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1793                            {
1794                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);
1795                            }
1796                    }
1797                    else // failed
1798                    {
1799                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1800                            break;
1801                    }
1802            }
1803    
1804            return ret;
1805    }


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

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