/[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.19 by sysadm, Sun May 25 06:56:48 2025 UTC Revision 1.62 by sysadm, Thu Nov 20 03:38:49 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     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
 #define _GNU_SOURCE  
   
 #include "section_list.h"  
13  #include "log.h"  #include "log.h"
14    #include "section_list.h"
15  #include "trie_dict.h"  #include "trie_dict.h"
16    #include "user_list.h"
17    #include <errno.h>
18    #include <fcntl.h>
19    #include <signal.h>
20  #include <stdio.h>  #include <stdio.h>
21    #include <stdlib.h>
22  #include <string.h>  #include <string.h>
 #include <signal.h>  
23  #include <unistd.h>  #include <unistd.h>
24  #include <stdlib.h>  #include <sys/mman.h>
 #include <errno.h>  
25  #include <sys/param.h>  #include <sys/param.h>
26  #include <sys/sem.h>  #include <sys/sem.h>
27  #include <sys/shm.h>  #include <sys/stat.h>
 #include <sys/ipc.h>  
28    
29  #ifdef _SEM_SEMUN_UNDEFINED  #if defined(_SEM_SEMUN_UNDEFINED) || defined(__CYGWIN__)
30  union semun  union semun
31  {  {
32          int val;                           /* Value for SETVAL */          int val;                           /* Value for SETVAL */
# Line 39  union semun Line 35  union semun
35          struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
36                                                            (Linux-specific) */                                                            (Linux-specific) */
37  };  };
38  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #if defined(_SEM_SEMUN_UNDEFINED)
39    
40    enum _section_list_constant_t
41    {
42            SECTION_TRY_LOCK_WAIT_TIME = 1, // second
43            SECTION_TRY_LOCK_TIMES = 10,
44    
45  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate          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 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id)          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)          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          CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections
50    
51  #define SID_STR_LEN 5 // 32-bit + NULL          SID_STR_LEN = 5, // 32-bit + NULL
52    };
53    
54  struct article_block_t  struct article_block_t
55  {  {
56          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[BBS_article_count_per_block];
57          int32_t article_count;          int article_count;
58          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
59  };  };
60  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
61    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
62  struct article_block_pool_t  struct article_block_pool_t
63  {  {
64          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          size_t shm_size;
65            struct
66            {
67                    size_t shm_size;
68                    void *p_shm;
69            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
70          int shm_count;          int shm_count;
71          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
72          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
73          int32_t block_count;          int block_count;
74  };  };
75  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
76    
77  static int article_block_pool_shmid;  static char article_block_shm_name[FILE_NAME_LEN];
78  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
79    
80  static int section_list_pool_semid;  static char section_list_shm_name[FILE_NAME_LEN];
81  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;  
82    
83  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
84  {  {
85          int shmid;          char filepath[FILE_PATH_LEN];
86          int proj_id;          int fd;
         key_t key;  
87          size_t size;          size_t size;
88          void *p_shm;          void *p_shm;
89          int i;          int i;
# Line 103  int article_block_init(const char *filen Line 97  int article_block_init(const char *filen
97                  return -1;                  return -1;
98          }          }
99    
100          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
101          {          {
102                  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);
103                  return -2;                  return -2;
104          }          }
105    
106            strncpy(filepath, filename, sizeof(filepath) - 1);
107            filepath[sizeof(filepath) - 1] = '\0';
108            snprintf(article_block_shm_name, sizeof(article_block_shm_name), "/ARTICLE_BLOCK_SHM_%s", basename(filepath));
109    
110          // Allocate shared memory          // Allocate shared memory
111          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm          size = sizeof(ARTICLE_BLOCK_POOL);
112          key = ftok(filename, proj_id);          snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, ARTICLE_BLOCK_SHM_COUNT_LIMIT);
113          if (key == -1)  
114            if (shm_unlink(filepath) == -1 && errno != ENOENT)
115          {          {
116                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);                  log_error("shm_unlink(%s) error (%d)\n", filepath, errno);
117                  return -3;                  return -2;
118          }          }
119    
120          size = sizeof(ARTICLE_BLOCK_POOL);          if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
         shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
         if (shmid == -1)  
121          {          {
122                  log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);                  log_error("shm_open(%s) error (%d)\n", filepath, errno);
123                  return -3;                  return -2;
124          }          }
125          p_shm = shmat(shmid, NULL, 0);          if (ftruncate(fd, (off_t)size) == -1)
         if (p_shm == (void *)-1)  
126          {          {
127                  log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);
128                  return -3;                  close(fd);
129                    return -2;
130            }
131    
132            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
133            if (p_shm == MAP_FAILED)
134            {
135                    log_error("mmap() error (%d)\n", errno);
136                    close(fd);
137                    return -2;
138            }
139    
140            if (close(fd) < 0)
141            {
142                    log_error("close(fd) error (%d)\n", errno);
143                    return -1;
144          }          }
145    
         article_block_pool_shmid = shmid;  
146          p_article_block_pool = p_shm;          p_article_block_pool = p_shm;
147            p_article_block_pool->shm_size = size;
148    
149          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
150          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
# Line 143  int article_block_init(const char *filen Line 154  int article_block_init(const char *filen
154                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
155                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
156    
157                  proj_id = p_article_block_pool->shm_count;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
158                  key = ftok(filename, proj_id);                  snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, p_article_block_pool->shm_count);
159                  if (key == -1)  
160                    if (shm_unlink(filepath) == -1 && errno != ENOENT)
161                  {                  {
162                          log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);                          log_error("shm_unlink(%s) error (%d)\n", filepath, errno);
163                          article_block_cleanup();                          return -2;
                         return -3;  
164                  }                  }
165    
166                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
                 shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
                 if (shmid == -1)  
167                  {                  {
168                          log_error("shmget(shm_index = %d, size = %d) error (%d)\n", p_article_block_pool->shm_count, size, errno);                          log_error("shm_open(%s) error (%d)\n", filepath, errno);
169                          article_block_cleanup();                          return -2;
                         return -3;  
170                  }                  }
171                  p_shm = shmat(shmid, NULL, 0);                  if (ftruncate(fd, (off_t)size) == -1)
                 if (p_shm == (void *)-1)  
172                  {                  {
173                          log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                          log_error("ftruncate(size=%d) error (%d)\n", size, errno);
174                          article_block_cleanup();                          close(fd);
175                          return -3;                          return -2;
176                  }                  }
177    
178                  (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shmid = shmid;                  p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
179                    if (p_shm == MAP_FAILED)
180                    {
181                            log_error("mmap() error (%d)\n", errno);
182                            close(fd);
183                            return -2;
184                    }
185    
186                    if (close(fd) < 0)
187                    {
188                            log_error("close(fd) error (%d)\n", errno);
189                            return -1;
190                    }
191    
192                    (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shm_size = size;
193                  (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->p_shm = p_shm;                  (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->p_shm = p_shm;
194                  p_article_block_pool->shm_count++;                  p_article_block_pool->shm_count++;
195    
# Line 196  int article_block_init(const char *filen Line 217  int article_block_init(const char *filen
217    
218  void article_block_cleanup(void)  void article_block_cleanup(void)
219  {  {
220            char filepath[FILE_PATH_LEN];
221    
222          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
223          {          {
224                  return;                  return;
# Line 203  void article_block_cleanup(void) Line 226  void article_block_cleanup(void)
226    
227          for (int i = 0; i < p_article_block_pool->shm_count; i++)          for (int i = 0; i < p_article_block_pool->shm_count; i++)
228          {          {
229                  if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)                  snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, i);
230    
231                    if (shm_unlink(filepath) == -1 && errno != ENOENT)
232                  {                  {
233                          log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);                          log_error("shm_unlink(%s) error (%d)\n", filepath, errno);
234                  }                  }
235            }
236    
237            snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, ARTICLE_BLOCK_SHM_COUNT_LIMIT);
238    
239            if (shm_unlink(filepath) == -1 && errno != ENOENT)
240            {
241                    log_error("shm_unlink(%s) error (%d)\n", filepath, errno);
242            }
243    
244                  if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)          detach_article_block_shm();
245    }
246    
247    int set_article_block_shm_readonly(void)
248    {
249            int i;
250    
251            if (p_article_block_pool == NULL)
252            {
253                    log_error("article_block_pool not initialized\n");
254                    return -1;
255            }
256    
257            for (i = 0; i < p_article_block_pool->shm_count; i++)
258            {
259                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL &&
260                            mprotect((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size, PROT_READ) < 0)
261                  {                  {
262                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);                          log_error("mprotect() error (%d)\n", errno);
263                            return -2;
264                  }                  }
265          }          }
266    
267          if (shmdt(p_article_block_pool) == -1)          if (p_article_block_pool != NULL &&
268                    mprotect(p_article_block_pool, p_article_block_pool->shm_size, PROT_READ) < 0)
269          {          {
270                  log_error("shmdt(shmid = %d) error (%d)\n", article_block_pool_shmid, errno);                  log_error("mprotect() error (%d)\n", errno);
271                    return -3;
272          }          }
273    
274          if (shmctl(article_block_pool_shmid, IPC_RMID, NULL) == -1)          return 0;
275    }
276    
277    int detach_article_block_shm(void)
278    {
279            if (p_article_block_pool == NULL)
280          {          {
281                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", article_block_pool_shmid, errno);                  return -1;
282            }
283    
284            for (int i = 0; i < p_article_block_pool->shm_count; i++)
285            {
286                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL &&
287                            munmap((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size) < 0)
288                    {
289                            log_error("munmap() error (%d)\n", errno);
290                            return -2;
291                    }
292            }
293    
294            if (p_article_block_pool != NULL && munmap(p_article_block_pool, p_article_block_pool->shm_size) < 0)
295            {
296                    log_error("munmap() error (%d)\n", errno);
297                    return -3;
298          }          }
299    
300          p_article_block_pool = NULL;          p_article_block_pool = NULL;
301    
302            return 0;
303  }  }
304    
305  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 287  ARTICLE *article_block_find_by_aid(int32 Line 362  ARTICLE *article_block_find_by_aid(int32
362          }          }
363    
364          left = 0;          left = 0;
365          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
366    
367          // 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] ]
368          while (left < right - 1)          while (left < right)
369          {          {
370                  // 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
371                  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;  
                 }  
372    
373                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
374                  {                  {
375                          right = mid;                          right = mid - 1;
376                  }                  }
377                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
378                  {                  {
379                          left = mid;                          left = mid;
380                  }                  }
# Line 331  ARTICLE *article_block_find_by_aid(int32 Line 400  ARTICLE *article_block_find_by_aid(int32
400                  }                  }
401          }          }
402    
403          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
404            {
405                    return NULL;
406            }
407    
408            return (p_block->articles + left); // found
409  }  }
410    
411  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 344  ARTICLE *article_block_find_by_index(int Line 418  ARTICLE *article_block_find_by_index(int
418                  return NULL;                  return NULL;
419          }          }
420    
421          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)
422          {          {
423                  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);
424                  return NULL;                  return NULL;
425          }          }
426    
427          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];
428    
429          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
430          {          {
431                  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);
432                  return NULL;                  return NULL;
433          }          }
434    
435          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
436  }  }
437    
438  extern int section_list_init(const char *filename)  extern int section_list_init(const char *filename)
439  {  {
440            char filepath[FILE_PATH_LEN];
441            int fd;
442            size_t size;
443            void *p_shm;
444          int semid;          int semid;
         int shmid;  
445          int proj_id;          int proj_id;
446          key_t key;          key_t key;
         size_t size;  
         void *p_shm;  
447          union semun arg;          union semun arg;
448          int i;          int i;
449    
450          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)
451          {          {
452                  section_list_cleanup();                  section_list_cleanup();
453          }          }
454    
455            // Allocate shared memory
456            size = sizeof(SECTION_LIST_POOL);
457    
458            strncpy(filepath, filename, sizeof(filepath) - 1);
459            filepath[sizeof(filepath) - 1] = '\0';
460            snprintf(section_list_shm_name, sizeof(section_list_shm_name), "/SECTION_LIST_SHM_%s", basename(filepath));
461    
462            if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT)
463            {
464                    log_error("shm_unlink(%s) error (%d)\n", section_list_shm_name, errno);
465                    return -2;
466            }
467    
468            if ((fd = shm_open(section_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
469            {
470                    log_error("shm_open(%s) error (%d)\n", section_list_shm_name, errno);
471                    return -2;
472            }
473            if (ftruncate(fd, (off_t)size) == -1)
474            {
475                    log_error("ftruncate(size=%d) error (%d)\n", size, errno);
476                    close(fd);
477                    return -2;
478            }
479    
480            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
481            if (p_shm == MAP_FAILED)
482            {
483                    log_error("mmap() error (%d)\n", errno);
484                    close(fd);
485                    return -2;
486            }
487    
488            if (close(fd) < 0)
489            {
490                    log_error("close(fd) error (%d)\n", errno);
491                    return -1;
492            }
493    
494            p_section_list_pool = p_shm;
495            p_section_list_pool->shm_size = size;
496            p_section_list_pool->section_count = 0;
497    
498            // Allocate semaphore as section locks
499          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
500          key = ftok(filename, proj_id);          key = ftok(filename, proj_id);
501          if (key == -1)          if (key == -1)
# Line 404  extern int section_list_init(const char Line 523  extern int section_list_init(const char
523                  }                  }
524          }          }
525    
526          section_list_pool_semid = semid;          p_section_list_pool->semid = semid;
527    
528          size = sizeof(SECTION_LIST) * BBS_max_section;          p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
529          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
         if (shmid == -1)  
530          {          {
531                  log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
532                  return -3;                  return -2;
533          }          }
534          p_shm = shmat(shmid, NULL, 0);  
535          if (p_shm == (void *)-1)          p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
536            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
537          {          {
538                  log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
539                  return -3;                  return -2;
540          }          }
541    
542          section_list_pool_shmid = shmid;          return 0;
543          p_section_list_pool = p_shm;  }
         section_list_count = 0;  
544    
545          p_trie_dict_section_by_name = trie_dict_create();  void section_list_cleanup(void)
546          if (p_trie_dict_section_by_name == NULL)  {
547            if (p_section_list_pool == NULL)
548          {          {
549                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  return;
                 return -2;  
550          }          }
551    
552          p_trie_dict_section_by_sid = trie_dict_create();          if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
         if (p_trie_dict_section_by_sid == NULL)  
553          {          {
554                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
555                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
556            }
557    
558            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
559            {
560                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
561                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
562            }
563    
564            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
565            {
566                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
567            }
568    
569            detach_section_list_shm();
570    
571            if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT)
572            {
573                    log_error("shm_unlink(%s) error (%d)\n", section_list_shm_name, errno);
574            }
575    }
576    
577    int set_section_list_shm_readonly(void)
578    {
579            if (p_section_list_pool == NULL)
580            {
581                    log_error("p_section_list_pool not initialized\n");
582                    return -1;
583            }
584    
585            if (p_section_list_pool != NULL &&
586                    mprotect(p_section_list_pool, p_section_list_pool->shm_size, PROT_READ) < 0)
587            {
588                    log_error("mprotect() error (%d)\n", errno);
589                  return -2;                  return -2;
590          }          }
591    
592          return 0;          return 0;
593  }  }
594    
595    int detach_section_list_shm(void)
596    {
597            if (p_section_list_pool != NULL && munmap(p_section_list_pool, p_section_list_pool->shm_size) < 0)
598            {
599                    log_error("munmap() error (%d)\n", errno);
600                    return -1;
601            }
602    
603            p_section_list_pool = NULL;
604    
605            return 0;
606    }
607    
608  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)
609  {  {
610          uint32_t u_sid;          uint32_t u_sid;
# Line 455  inline static void sid_to_str(int32_t si Line 619  inline static void sid_to_str(int32_t si
619          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
620  }  }
621    
622  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)
623  {  {
624          SECTION_LIST *p_section;          SECTION_LIST *p_section;
625          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
626    
627          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)
628          {          {
629                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
630                  return NULL;                  return NULL;
631          }          }
632    
633          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
634          {          {
635                  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);
636                  return NULL;                  return NULL;
637          }          }
638    
639          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
640    
641          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
642    
643          p_section->sid = sid;          p_section->sid = sid;
644            p_section->ex_menu_tm = 0;
645    
646          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
647          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
648    
649          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
650          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
651    
652          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);
653          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
654    
655          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)
656          {          {
657                  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);
658                  return NULL;                  return NULL;
659          }          }
660    
661          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)
662          {          {
663                  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]);  
664                  return NULL;                  return NULL;
665          }          }
666    
667          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
668    
669          section_list_count++;          p_section_list_pool->section_count++;
670    
671          return p_section;          return p_section;
672  }  }
673    
674    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
675    {
676            int64_t index;
677    
678            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
679            {
680                    log_error("NULL pointer error\n");
681                    return -1;
682            }
683    
684            index = get_section_index(p_section);
685    
686            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
687            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
688    
689            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
690            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
691    
692            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
693            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
694    
695            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
696            {
697                    log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
698                    return -2;
699            }
700    
701            return 0;
702    }
703    
704  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
705  {  {
706          p_section->article_count = 0;          p_section->article_count = 0;
# Line 518  void section_list_reset_articles(SECTION Line 712  void section_list_reset_articles(SECTION
712    
713          p_section->page_count = 0;          p_section->page_count = 0;
714          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
 }  
   
 void section_list_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;  
         }  
   
         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;  
715    
716                  if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)          p_section->ontop_article_count = 0;
                 {  
                         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;  
717  }  }
718    
719  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
720  {  {
721          int64_t index;          int64_t index;
722            int ret;
723    
724          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)          if (p_section_list_pool == NULL)
725          {          {
726                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
727                  return NULL;                  return NULL;
728          }          }
729    
730          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);
731            if (ret < 0)
732          {          {
733                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
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  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
745  {  {
746          int64_t index;          int64_t index;
747            int ret;
748          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
749    
750          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
751          {          {
752                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
753                  return NULL;                  return NULL;
# Line 588  SECTION_LIST *section_list_find_by_sid(i Line 755  SECTION_LIST *section_list_find_by_sid(i
755    
756          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
757    
758          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);
759            if (ret < 0)
760          {          {
761                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
762                  return NULL;                  return NULL;
763          }          }
764            else if (ret == 0)
765            {
766                    return NULL;
767            }
768    
769          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
770  }  }
771    
772  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 607  int section_list_append_article(SECTION_ Line 779  int section_list_append_article(SECTION_
779    
780          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
781          {          {
782                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
783                  return -1;                  return -1;
784          }          }
785    
# Line 630  int section_list_append_article(SECTION_ Line 802  int section_list_append_article(SECTION_
802          }          }
803    
804          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
805                  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)
806          {          {
807                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
808                  {                  {
# Line 640  int section_list_append_article(SECTION_ Line 812  int section_list_append_article(SECTION_
812    
813                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
814                  {                  {
815                          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;
816                  }                  }
817    
818                  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 719  int section_list_append_article(SECTION_ Line 891  int section_list_append_article(SECTION_
891          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
892    
893          // Update page          // Update page
894          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) ||
895                  p_section->article_count == 1)                  p_section->page_count == 0)
896          {          {
897                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
898                  p_section->page_count++;                  p_section->page_count++;
# Line 732  int section_list_append_article(SECTION_ Line 904  int section_list_append_article(SECTION_
904                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
905          }          }
906    
907            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
908            {
909                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
910                                      p_section->sid, p_article->aid);
911                    return -5;
912            }
913    
914          return 0;          return 0;
915  }  }
916    
# Line 743  int section_list_set_article_visible(SEC Line 922  int section_list_set_article_visible(SEC
922    
923          if (p_section == NULL)          if (p_section == NULL)
924          {          {
925                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
926                  return -2;                  return -1;
927          }          }
928    
929          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 755  int section_list_set_article_visible(SEC Line 934  int section_list_set_article_visible(SEC
934    
935          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
936          {          {
937                  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);
938                  return -2;                  return -2;
939          }          }
940    
# Line 768  int section_list_set_article_visible(SEC Line 947  int section_list_set_article_visible(SEC
947          {          {
948                  p_section->visible_article_count--;                  p_section->visible_article_count--;
949    
950                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
951                    {
952                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
953                    }
954    
955                  if (p_article->tid == 0)                  if (p_article->tid == 0)
956                  {                  {
957                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 786  int section_list_set_article_visible(SEC Line 970  int section_list_set_article_visible(SEC
970                                          p_reply->visible = 0;                                          p_reply->visible = 0;
971                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
972                                          affected_count++;                                          affected_count++;
973    
974                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
975                                            {
976                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
977                                            }
978                                  }                                  }
979                          }                          }
980                  }                  }
# Line 798  int section_list_set_article_visible(SEC Line 987  int section_list_set_article_visible(SEC
987                  {                  {
988                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
989                  }                  }
990    
991                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
992                    {
993                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
994                    }
995          }          }
996    
997          p_article->visible = visible;          p_article->visible = visible;
# Line 806  int section_list_set_article_visible(SEC Line 1000  int section_list_set_article_visible(SEC
1000          return affected_count;          return affected_count;
1001  }  }
1002    
1003    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
1004    {
1005            int i;
1006    
1007            if (p_section == NULL || p_article == NULL)
1008            {
1009                    log_error("NULL pointer error\n");
1010                    return -1;
1011            }
1012    
1013            if (p_section->sid != p_article->sid)
1014            {
1015                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
1016                    return -2;
1017            }
1018    
1019            if (p_article->ontop)
1020            {
1021                    for (i = 0; i < p_section->ontop_article_count; i++)
1022                    {
1023                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1024                            {
1025                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
1026                                    return 0;
1027                            }
1028                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
1029                            {
1030                                    break;
1031                            }
1032                    }
1033    
1034                    // Remove the oldest one if the array of ontop articles is full
1035                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1036                    {
1037                            if (i == 0) // p_article is the oldest one
1038                            {
1039                                    return 0;
1040                            }
1041                            memmove((void *)(p_section->p_ontop_articles),
1042                                            (void *)(p_section->p_ontop_articles + 1),
1043                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1044                            p_section->ontop_article_count--;
1045                            i--;
1046                    }
1047                    else
1048                    {
1049                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1050                                            (void *)(p_section->p_ontop_articles + i),
1051                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1052                    }
1053    
1054                    p_section->p_ontop_articles[i] = p_article;
1055                    p_section->ontop_article_count++;
1056    
1057                    // TODO: debug
1058            }
1059            else // ontop == 0
1060            {
1061                    for (i = 0; i < p_section->ontop_article_count; i++)
1062                    {
1063                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1064                            {
1065                                    break;
1066                            }
1067                    }
1068                    if (i == p_section->ontop_article_count) // not found
1069                    {
1070                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1071                            return 0;
1072                    }
1073    
1074                    memmove((void *)(p_section->p_ontop_articles + i),
1075                                    (void *)(p_section->p_ontop_articles + i + 1),
1076                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1077                    p_section->ontop_article_count--;
1078            }
1079    
1080            return 0;
1081    }
1082    
1083    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1084    {
1085            int page_count;
1086    
1087            if (p_section == NULL)
1088            {
1089                    log_error("NULL pointer error\n");
1090                    return -1;
1091            }
1092    
1093            page_count = p_section->page_count - 1 +
1094                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count + BBS_article_limit_per_page - 1) /
1095                                             BBS_article_limit_per_page;
1096    
1097            if (page_count < 0)
1098            {
1099                    page_count = 0;
1100            }
1101    
1102            return page_count;
1103    }
1104    
1105    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1106    {
1107            if (p_section == NULL)
1108            {
1109                    log_error("NULL pointer error\n");
1110                    return -1;
1111            }
1112    
1113            if (page_id < p_section->page_count - 1)
1114            {
1115                    return BBS_article_limit_per_page;
1116            }
1117            else // if (page_id >= p_section->page_count - 1)
1118            {
1119                    return MIN(MAX(0,
1120                                               (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1121                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1122                                       BBS_article_limit_per_page);
1123            }
1124    }
1125    
1126  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)
1127  {  {
1128          ARTICLE *p_article;          ARTICLE *p_article;
# Line 819  ARTICLE *section_list_find_article_with_ Line 1136  ARTICLE *section_list_find_article_with_
1136    
1137          if (p_section == NULL)          if (p_section == NULL)
1138          {          {
1139                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1140                  return NULL;                  return NULL;
1141          }          }
1142    
# Line 831  ARTICLE *section_list_find_article_with_ Line 1148  ARTICLE *section_list_find_article_with_
1148          }          }
1149    
1150          left = 0;          left = 0;
1151          right = p_section->page_count;          right = p_section->page_count - 1;
1152    
1153          // 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] ]
1154          while (left < right - 1)          while (left < right)
1155          {          {
1156                  // 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
1157                  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;  
                 }  
1158    
1159                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1160                  {                  {
1161                          right = mid;                          right = mid - 1;
1162                  }                  }
1163                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1164                  {                  {
1165                          left = mid;                          left = mid;
1166                  }                  }
# Line 911  int section_list_calculate_page(SECTION_ Line 1222  int section_list_calculate_page(SECTION_
1222    
1223          if (p_section == NULL)          if (p_section == NULL)
1224          {          {
1225                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1226                  return -1;                  return -1;
1227          }          }
1228    
# Line 1000  int section_list_calculate_page(SECTION_ Line 1311  int section_list_calculate_page(SECTION_
1311          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1312    
1313          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1314          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1315                                                                                                              ? visible_article_count
1316                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1317    
1318          return 0;          return 0;
1319  }  }
1320    
1321    int32_t article_block_last_aid(void)
1322    {
1323            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1324            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1325    
1326            return last_aid;
1327    }
1328    
1329    int article_block_article_count(void)
1330    {
1331            int ret;
1332    
1333            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1334            {
1335                    return -1;
1336            }
1337    
1338            ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1339                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1340    
1341            return ret;
1342    }
1343    
1344  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1345  {  {
1346          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1045  int section_list_move_topic(SECTION_LIST Line 1381  int section_list_move_topic(SECTION_LIST
1381          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1382          int move_counter;          int move_counter;
1383    
1384          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1385            {
1386                    log_error("NULL pointer error\n");
1387                    return -1;
1388            }
1389    
1390            if (p_section_src->sid == p_section_dest->sid)
1391          {          {
1392                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("section_list_move_topic() src and dest section are the same\n");
1393                  return -1;                  return -1;
1394          }          }
1395    
1396          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1397          {          {
1398                  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);
1399                  return -2;                  return -2;
1400          }          }
1401    
# Line 1075  int section_list_move_topic(SECTION_LIST Line 1417  int section_list_move_topic(SECTION_LIST
1417          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1418          if (move_article_count <= 0)          if (move_article_count <= 0)
1419          {          {
1420                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1421                  return -2;                  return -2;
1422          }          }
1423    
# Line 1094  int section_list_move_topic(SECTION_LIST Line 1436  int section_list_move_topic(SECTION_LIST
1436          {          {
1437                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1438                  {                  {
1439                          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",
1440                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1441                          return -2;                          p_article = p_article->p_topic_next;
1442                            continue;
1443                  }                  }
1444    
1445                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1122  int section_list_move_topic(SECTION_LIST Line 1465  int section_list_move_topic(SECTION_LIST
1465    
1466                  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)
1467                  {                  {
1468                          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);
1469                          return -4;                          p_article = p_article->p_topic_next;
1470                            continue;
1471                  }                  }
1472    
1473                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1201  int section_list_move_topic(SECTION_LIST Line 1545  int section_list_move_topic(SECTION_LIST
1545    
1546          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)
1547          {          {
1548                  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",
1549                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1550          }          }
1551    
# Line 1241  int get_section_index(SECTION_LIST *p_se Line 1585  int get_section_index(SECTION_LIST *p_se
1585          }          }
1586          else          else
1587          {          {
1588                  index = (int)(p_section - p_section_list_pool);                  index = (int)(p_section - p_section_list_pool->sections);
1589                  if (index < 0 || index >= BBS_max_section)                  if (index < 0 || index >= BBS_max_section)
1590                  {                  {
1591                          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 get_section_index(SECTION_LIST *p_se Line 1596  int get_section_index(SECTION_LIST *p_se
1596          return index;          return index;
1597  }  }
1598    
1599    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1600    {
1601            if (p_section == NULL)
1602            {
1603                    log_error("NULL pointer error\n");
1604                    return -1;
1605            }
1606    
1607            if (section_list_rd_lock(p_section) < 0)
1608            {
1609                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1610                    return -2;
1611            }
1612    
1613            if (sname != NULL)
1614            {
1615                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1616            }
1617            if (stitle != NULL)
1618            {
1619                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1620            }
1621            if (master_list != NULL)
1622            {
1623                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1624            }
1625    
1626            // release lock of section
1627            if (section_list_rd_unlock(p_section) < 0)
1628            {
1629                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1630                    return -2;
1631            }
1632    
1633            return 0;
1634    }
1635    
1636  int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)  int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1637  {  {
1638          int index;          int index;
1639          struct sembuf sops[4];          struct sembuf sops[4];
1640    #ifndef __CYGWIN__
1641          struct timespec timeout;          struct timespec timeout;
1642    #endif
1643          int ret;          int ret;
1644    
1645          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1276  int section_list_try_rd_lock(SECTION_LIS Line 1659  int section_list_try_rd_lock(SECTION_LIS
1659          // 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"
1660          // 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"
1661          // rather than to acquire multiple write locks on all the available sections.          // rather than to acquire multiple write locks on all the available sections.
1662          if (index == BBS_max_section)          if (index != BBS_max_section)
1663          {          {
1664                  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
1665                  sops[2].sem_op = 0;                                                // wait until unlocked                  sops[2].sem_op = 0;                                                // wait until unlocked
# Line 1287  int section_list_try_rd_lock(SECTION_LIS Line 1670  int section_list_try_rd_lock(SECTION_LIS
1670                  sops[3].sem_flg = SEM_UNDO;                        // undo on terminate                  sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1671          }          }
1672    
1673    #ifdef __CYGWIN__
1674            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4));
1675    #else
1676          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1677          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1678    
1679          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);
1680    #endif
1681          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1682          {          {
1683                  log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);                  log_error("semop(index = %d, lock read) error %d\n", index, errno);
1684          }          }
1685    
1686          return ret;          return ret;
# Line 1303  int section_list_try_rw_lock(SECTION_LIS Line 1690  int section_list_try_rw_lock(SECTION_LIS
1690  {  {
1691          int index;          int index;
1692          struct sembuf sops[3];          struct sembuf sops[3];
1693    #ifndef __CYGWIN__
1694          struct timespec timeout;          struct timespec timeout;
1695    #endif
1696          int ret;          int ret;
1697    
1698          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1324  int section_list_try_rw_lock(SECTION_LIS Line 1713  int section_list_try_rw_lock(SECTION_LIS
1713          sops[2].sem_op = 0;                                                        // wait until unlocked          sops[2].sem_op = 0;                                                        // wait until unlocked
1714          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
1715    
1716    #ifdef __CYGWIN__
1717            ret = semop(p_section_list_pool->semid, sops, 3);
1718    #else
1719          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1720          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1721    
1722          ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1723    #endif
1724          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1725          {          {
1726                  log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);                  log_error("semop(index = %d, lock write) error %d\n", index, errno);
1727          }          }
1728    
1729          return ret;          return ret;
# Line 1352  int section_list_rd_unlock(SECTION_LIST Line 1745  int section_list_rd_unlock(SECTION_LIST
1745          sops[0].sem_op = -1;                                               // unlock          sops[0].sem_op = -1;                                               // unlock
1746          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1747    
1748          // The same reason as section_list_try_rd_lock()          if (index != BBS_max_section)
         if (index == BBS_max_section)  
1749          {          {
1750                  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
1751                  sops[1].sem_op = -1;                                     // unlock                  sops[1].sem_op = -1;                                     // unlock
1752                  sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1753          }          }
1754    
1755          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));
1756          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1757          {          {
1758                  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 1385  int section_list_rw_unlock(SECTION_LIST Line 1777  int section_list_rw_unlock(SECTION_LIST
1777          sops[0].sem_op = -1;                                                       // unlock          sops[0].sem_op = -1;                                                       // unlock
1778          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1779    
1780          ret = semop(section_list_pool_semid, sops, 1);          ret = semop(p_section_list_pool->semid, sops, 1);
1781          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1782          {          {
1783                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1784          }          }
1785    
1786          return ret;          return ret;
1787    }
1788    
1789    int section_list_rd_lock(SECTION_LIST *p_section)
1790    {
1791            int timer = 0;
1792            int sid = (p_section == NULL ? 0 : p_section->sid);
1793            int ret = -1;
1794    
1795            while (!SYS_server_exit)
1796            {
1797                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1798                    if (ret == 0) // success
1799                    {
1800                            break;
1801                    }
1802                    else if (errno == EAGAIN || errno == EINTR) // retry
1803                    {
1804                            timer++;
1805                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1806                            {
1807                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);
1808                            }
1809                    }
1810                    else // failed
1811                    {
1812                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1813                            break;
1814                    }
1815            }
1816    
1817            return ret;
1818    }
1819    
1820    int section_list_rw_lock(SECTION_LIST *p_section)
1821    {
1822            int timer = 0;
1823            int sid = (p_section == NULL ? 0 : p_section->sid);
1824            int ret = -1;
1825    
1826            while (!SYS_server_exit)
1827            {
1828                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1829                    if (ret == 0) // success
1830                    {
1831                            break;
1832                    }
1833                    else if (errno == EAGAIN || errno == EINTR) // retry
1834                    {
1835                            timer++;
1836                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1837                            {
1838                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);
1839                            }
1840                    }
1841                    else // failed
1842                    {
1843                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1844                            break;
1845                    }
1846            }
1847    
1848            return ret;
1849  }  }


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

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