/[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.9 by sysadm, Thu May 22 14:12:33 2025 UTC Revision 1.68 by sysadm, Wed Jan 7 14:39:16 2026 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-2026  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    
 #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/shm.h>  #include <sys/sem.h>
27  #include <sys/ipc.h>  #include <sys/stat.h>
28    
29  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #if defined(_SEM_SEMUN_UNDEFINED) || defined(__CYGWIN__)
30  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  union semun
31  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  {
32            int val;                           /* Value for SETVAL */
33            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
34            unsigned short *array; /* Array for GETALL, SETALL */
35            struct seminfo *__buf; /* Buffer for IPC_INFO
36                                                              (Linux-specific) */
37    };
38    #endif // #if defined(_SEM_SEMUN_UNDEFINED)
39    
40  struct article_block_t  enum _section_list_constant_t
41  {  {
42          ARTICLE articles[ARTICLE_PER_BLOCK];          SECTION_TRY_LOCK_WAIT_TIME = 1, // second
43          int32_t article_count;          SECTION_TRY_LOCK_TIMES = 10,
44          struct article_block_t *p_next_block;          SECTION_DEAD_LOCK_TIMEOUT = 15, // second
45    
46            ARTICLE_BLOCK_PER_SHM = 1000,           // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
47            ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id)
48            ARTICLE_BLOCK_PER_POOL = (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT),
49    
50            CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections
51    
52            SID_STR_LEN = 5, // 32-bit + NULL
53  };  };
 typedef struct article_block_t ARTICLE_BLOCK;  
54    
55  struct article_block_shm_t  struct article_block_t
56  {  {
57          int shmid;          ARTICLE articles[BBS_article_count_per_block];
58          void *p_shm;          int article_count;
59            struct article_block_t *p_next_block;
60  };  };
61  typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  typedef struct article_block_t ARTICLE_BLOCK;
62    
63  struct article_block_pool_t  struct article_block_pool_t
64  {  {
65          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          size_t shm_size;
66            struct
67            {
68                    size_t shm_size;
69                    void *p_shm;
70            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
71          int shm_count;          int shm_count;
72          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
73          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
74          int32_t block_count;          int block_count;
75  };  };
76  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
77    
78    static char article_block_shm_name[FILE_NAME_LEN];
79  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
80    
81  static SECTION_LIST *p_section_list_pool = NULL;  static char section_list_shm_name[FILE_NAME_LEN];
82  static int section_list_count = 0;  SECTION_LIST_POOL *p_section_list_pool = NULL;
83  static TRIE_NODE *p_trie_dict_section_list = NULL;  
84    #ifndef HAVE_SYSTEM_V
85    static int section_list_reset_lock(SECTION_LIST *p_section);
86    #endif
87    
88  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
89  {  {
90          int shmid;          char filepath[FILE_PATH_LEN];
91          int proj_id;          int fd;
         key_t key;  
92          size_t size;          size_t size;
93          void *p_shm;          void *p_shm;
94          int i;          int i;
# Line 76  int article_block_init(const char *filen Line 98  int article_block_init(const char *filen
98    
99          if (p_article_block_pool != NULL)          if (p_article_block_pool != NULL)
100          {          {
101                  log_error("article_block_pool already initialized\n");                  log_error("article_block_pool already initialized");
102                  return -1;                  return -1;
103          }          }
104    
105          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
106          {          {
107                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);                  log_error("article_block_count exceed limit %d", ARTICLE_BLOCK_PER_POOL);
108                  return -2;                  return -2;
109          }          }
110    
111          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          strncpy(filepath, filename, sizeof(filepath) - 1);
112          if (p_article_block_pool == NULL)          filepath[sizeof(filepath) - 1] = '\0';
113            snprintf(article_block_shm_name, sizeof(article_block_shm_name), "/ARTICLE_BLOCK_SHM_%s", basename(filepath));
114    
115            // Allocate shared memory
116            size = sizeof(ARTICLE_BLOCK_POOL);
117            snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, ARTICLE_BLOCK_SHM_COUNT_LIMIT);
118    
119            if (shm_unlink(filepath) == -1 && errno != ENOENT)
120          {          {
121                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("shm_unlink(%s) error (%d)", filepath, errno);
122                  return -2;                  return -2;
123          }          }
124    
125          // Allocate shared memory          if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
126            {
127                    log_error("shm_open(%s) error (%d)", filepath, errno);
128                    return -2;
129            }
130            if (ftruncate(fd, (off_t)size) == -1)
131            {
132                    log_error("ftruncate(size=%d) error (%d)", size, errno);
133                    close(fd);
134                    return -2;
135            }
136    
137            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
138            if (p_shm == MAP_FAILED)
139            {
140                    log_error("mmap() error (%d)", errno);
141                    close(fd);
142                    return -2;
143            }
144    
145            if (close(fd) < 0)
146            {
147                    log_error("close(fd) error (%d)", errno);
148                    return -1;
149            }
150    
151            p_article_block_pool = p_shm;
152            p_article_block_pool->shm_size = size;
153    
154          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
155          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
156    
# Line 102  int article_block_init(const char *filen Line 159  int article_block_init(const char *filen
159                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
160                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
161    
162                  proj_id = getpid() + p_article_block_pool->shm_count;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
163                  key = ftok(filename, proj_id);                  snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, p_article_block_pool->shm_count);
164                  if (key == -1)  
165                    if (shm_unlink(filepath) == -1 && errno != ENOENT)
166                  {                  {
167                          log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);                          log_error("shm_unlink(%s) error (%d)", filepath, errno);
168                          article_block_cleanup();                          return -2;
                         return -3;  
169                  }                  }
170    
171                  size = sizeof(shmid) + 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)  
172                  {                  {
173                          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)", filepath, errno);
174                          article_block_cleanup();                          return -2;
                         return -3;  
175                  }                  }
176                  p_shm = shmat(shmid, NULL, 0);                  if (ftruncate(fd, (off_t)size) == -1)
                 if (p_shm == (void *)-1)  
177                  {                  {
178                          log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                          log_error("ftruncate(size=%d) error (%d)", size, errno);
179                          article_block_cleanup();                          close(fd);
180                          return -3;                          return -2;
181                    }
182    
183                    p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
184                    if (p_shm == MAP_FAILED)
185                    {
186                            log_error("mmap() error (%d)", errno);
187                            close(fd);
188                            return -2;
189                  }                  }
190    
191                  (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shmid = shmid;                  if (close(fd) < 0)
192                    {
193                            log_error("close(fd) error (%d)", errno);
194                            return -1;
195                    }
196    
197                    (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shm_size = size;
198                  (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;
199                  p_article_block_pool->shm_count++;                  p_article_block_pool->shm_count++;
200    
# Line 155  int article_block_init(const char *filen Line 222  int article_block_init(const char *filen
222    
223  void article_block_cleanup(void)  void article_block_cleanup(void)
224  {  {
225          if (p_article_block_pool != NULL)          char filepath[FILE_PATH_LEN];
226    
227            if (p_article_block_pool == NULL)
228          {          {
229                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  return;
230            }
231    
232            for (int i = 0; i < p_article_block_pool->shm_count; i++)
233            {
234                    snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, i);
235    
236                    if (shm_unlink(filepath) == -1 && errno != ENOENT)
237                  {                  {
238                          if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)                          log_error("shm_unlink(%s) error (%d)", filepath, errno);
239                          {                  }
240                                  log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);          }
                         }  
241    
242                          if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)          snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, ARTICLE_BLOCK_SHM_COUNT_LIMIT);
243                          {  
244                                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);          if (shm_unlink(filepath) == -1 && errno != ENOENT)
245                          }          {
246                    log_error("shm_unlink(%s) error (%d)", filepath, errno);
247            }
248    
249            detach_article_block_shm();
250    }
251    
252    int set_article_block_shm_readonly(void)
253    {
254            // int i;
255    
256            if (p_article_block_pool == NULL)
257            {
258                    log_error("article_block_pool not initialized");
259                    return -1;
260            }
261    
262            // for (i = 0; i < p_article_block_pool->shm_count; i++)
263            // {
264            //      if ((p_article_block_pool->shm_pool + i)->p_shm != NULL &&
265            //              mprotect((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size, PROT_READ) < 0)
266            //      {
267            //              log_error("mprotect() error (%d)", errno);
268            //              return -2;
269            //      }
270            // }
271    
272            if (p_article_block_pool != NULL &&
273                    mprotect(p_article_block_pool, p_article_block_pool->shm_size, PROT_READ) < 0)
274            {
275                    log_error("mprotect() error (%d)", errno);
276                    return -3;
277            }
278    
279            return 0;
280    }
281    
282    int detach_article_block_shm(void)
283    {
284            int shm_count;
285            size_t pool_shm_size;
286    
287            if (p_article_block_pool == NULL)
288            {
289                    return -1;
290            }
291    
292            shm_count = p_article_block_pool->shm_count;
293            pool_shm_size = p_article_block_pool->shm_size;
294    
295            for (int i = 0; i < shm_count; i++)
296            {
297                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL &&
298                            munmap((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size) < 0)
299                    {
300                            log_error("munmap() error (%d)", errno);
301                            return -2;
302                  }                  }
303            }
304    
305                  free(p_article_block_pool);          if (p_article_block_pool != NULL && munmap(p_article_block_pool, pool_shm_size) < 0)
306                  p_article_block_pool = NULL;          {
307                    log_error("munmap() error (%d)", errno);
308                    return -3;
309          }          }
310    
311            p_article_block_pool = NULL;
312    
313            return 0;
314  }  }
315    
316  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 202  int article_block_reset(void) Line 340  int article_block_reset(void)
340    
341          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
342          {          {
343                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
344                  return -1;                  return -1;
345          }          }
346    
# Line 225  ARTICLE *article_block_find_by_aid(int32 Line 363  ARTICLE *article_block_find_by_aid(int32
363    
364          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
365          {          {
366                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
367                  return NULL;                  return NULL;
368          }          }
369    
# Line 235  ARTICLE *article_block_find_by_aid(int32 Line 373  ARTICLE *article_block_find_by_aid(int32
373          }          }
374    
375          left = 0;          left = 0;
376          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
377    
378          // 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] ]
379          while (left < right - 1)          while (left < right)
380          {          {
381                  // 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
382                  mid = (left + right) / 2 + (right - left) % 2;                  mid = left + (right - left + 1) / 2;
   
                 if (mid >= p_article_block_pool->block_count)  
                 {  
                         log_error("block(mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
383    
384                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
385                  {                  {
386                          right = mid;                          right = mid - 1;
387                  }                  }
388                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
389                  {                  {
390                          left = mid;                          left = mid;
391                  }                  }
# Line 267  ARTICLE *article_block_find_by_aid(int32 Line 399  ARTICLE *article_block_find_by_aid(int32
399          // aid in the range [ aid of articles[left], aid of articles[right] ]          // aid in the range [ aid of articles[left], aid of articles[right] ]
400          while (left < right)          while (left < right)
401          {          {
402                  mid = (left + right) / 2;                  mid = left + (right - left) / 2;
403    
404                  if (aid <= p_block->articles[mid].aid)                  if (aid <= p_block->articles[mid].aid)
405                  {                  {
# Line 279  ARTICLE *article_block_find_by_aid(int32 Line 411  ARTICLE *article_block_find_by_aid(int32
411                  }                  }
412          }          }
413    
414          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
415            {
416                    return NULL;
417            }
418    
419            return (p_block->articles + left); // found
420  }  }
421    
422  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 288  ARTICLE *article_block_find_by_index(int Line 425  ARTICLE *article_block_find_by_index(int
425    
426          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
427          {          {
428                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
429                  return NULL;                  return NULL;
430          }          }
431    
432          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)
433          {          {
434                  log_error("section_data_find_article_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)", index, p_article_block_pool->block_count);
435                  return NULL;                  return NULL;
436          }          }
437    
438          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];
439    
440          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
441          {          {
442                  log_error("section_data_find_article_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)", index, p_block->article_count);
443                  return NULL;                  return NULL;
444          }          }
445    
446          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
447  }  }
448    
449  SECTION_LIST *section_list_create(const char *sname, const char *stitle, const char *master_name)  extern int section_list_init(const char *filename)
450  {  {
451          SECTION_LIST *p_section;          char filepath[FILE_PATH_LEN];
452            int fd;
453            size_t size;
454            void *p_shm;
455    #ifdef HAVE_SYSTEM_V
456            int proj_id;
457            key_t key;
458            int semid;
459            union semun arg;
460    #endif
461            int i;
462    
463          if (p_section_list_pool == NULL)          if (p_section_list_pool != NULL)
464          {          {
465                  p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));                  section_list_cleanup();
466                  if (p_section_list_pool == NULL)          }
467    
468            // Allocate shared memory
469            size = sizeof(SECTION_LIST_POOL);
470    
471            strncpy(filepath, filename, sizeof(filepath) - 1);
472            filepath[sizeof(filepath) - 1] = '\0';
473            snprintf(section_list_shm_name, sizeof(section_list_shm_name), "/SECTION_LIST_SHM_%s", basename(filepath));
474    
475            if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT)
476            {
477                    log_error("shm_unlink(%s) error (%d)", section_list_shm_name, errno);
478                    return -2;
479            }
480    
481            if ((fd = shm_open(section_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
482            {
483                    log_error("shm_open(%s) error (%d)", section_list_shm_name, errno);
484                    return -2;
485            }
486            if (ftruncate(fd, (off_t)size) == -1)
487            {
488                    log_error("ftruncate(size=%d) error (%d)", size, errno);
489                    close(fd);
490                    return -2;
491            }
492    
493            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
494            if (p_shm == MAP_FAILED)
495            {
496                    log_error("mmap() error (%d)", errno);
497                    close(fd);
498                    return -2;
499            }
500    
501            if (close(fd) < 0)
502            {
503                    log_error("close(fd) error (%d)", errno);
504                    return -1;
505            }
506    
507            p_section_list_pool = p_shm;
508            p_section_list_pool->shm_size = size;
509            p_section_list_pool->section_count = 0;
510    
511            // Allocate semaphore as section locks
512    #ifndef HAVE_SYSTEM_V
513            for (i = 0; i <= BBS_max_section; i++)
514            {
515                    if (sem_init(&(p_section_list_pool->sem[i]), 1, 1) == -1)
516                  {                  {
517                          log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                          log_error("sem_init(sem[%d]) error (%d)", i, errno);
518                          return NULL;                          return -3;
519                  }                  }
520    
521                  section_list_count = 0;                  p_section_list_pool->read_lock_count[i] = 0;
522                    p_section_list_pool->write_lock_count[i] = 0;
523            }
524    #else
525            proj_id = (int)(time(NULL) % getpid());
526            key = ftok(filename, proj_id);
527            if (key == -1)
528            {
529                    log_error("ftok(%s, %d) error (%d)", filename, proj_id, errno);
530                    return -3;
531          }          }
532    
533          if (p_trie_dict_section_list == NULL)          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
534            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
535            if (semid == -1)
536          {          {
537                  p_trie_dict_section_list = trie_dict_create();                  log_error("semget(section_list_pool_sem, size = %d) error (%d)", size, errno);
538                  if (p_trie_dict_section_list == NULL)                  return -3;
539            }
540    
541            // Initialize sem value to 0
542            arg.val = 0;
543            for (i = 0; i < size; i++)
544            {
545                    if (semctl(semid, i, SETVAL, arg) == -1)
546                    {
547                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)", errno);
548                            return -3;
549                    }
550            }
551    
552            p_section_list_pool->semid = semid;
553    #endif
554    
555            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
556            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
557            {
558                    log_error("trie_dict_create() OOM", BBS_max_section);
559                    return -2;
560            }
561    
562            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
563            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
564            {
565                    log_error("trie_dict_create() OOM", BBS_max_section);
566                    return -2;
567            }
568    
569            return 0;
570    }
571    
572    void section_list_cleanup(void)
573    {
574            if (p_section_list_pool == NULL)
575            {
576                    return;
577            }
578    
579            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
580            {
581                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
582                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
583            }
584    
585            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
586            {
587                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
588                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
589            }
590    
591    #ifdef HAVE_SYSTEM_V
592            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
593            {
594                    log_error("semctl(semid = %d, IPC_RMID) error (%d)", p_section_list_pool->semid, errno);
595            }
596    #else
597            for (int i = 0; i <= BBS_max_section; i++)
598            {
599                    if (sem_destroy(&(p_section_list_pool->sem[i])) == -1)
600                  {                  {
601                          log_error("trie_dict_create() OOM\n", BBS_max_section);                          log_error("sem_destroy(sem[%d]) error (%d)", i, errno);
                         return NULL;  
602                  }                  }
603          }          }
604    #endif
605    
606            detach_section_list_shm();
607    
608            if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT)
609            {
610                    log_error("shm_unlink(%s) error (%d)", section_list_shm_name, errno);
611            }
612    }
613    
614          if (section_list_count >= BBS_max_section)  int set_section_list_shm_readonly(void)
615    {
616            if (p_section_list_pool == NULL)
617          {          {
618                  log_error("section_list_count exceed limit %d\n", BBS_max_section);                  log_error("p_section_list_pool not initialized");
619                    return -1;
620            }
621    
622            if (p_section_list_pool != NULL &&
623                    mprotect(p_section_list_pool, p_section_list_pool->shm_size, PROT_READ) < 0)
624            {
625                    log_error("mprotect() error (%d)", errno);
626                    return -2;
627            }
628    
629            return 0;
630    }
631    
632    int detach_section_list_shm(void)
633    {
634            if (p_section_list_pool != NULL && munmap(p_section_list_pool, p_section_list_pool->shm_size) < 0)
635            {
636                    log_error("munmap() error (%d)", errno);
637                    return -1;
638            }
639    
640            p_section_list_pool = NULL;
641    
642            return 0;
643    }
644    
645    inline static void sid_to_str(int32_t sid, char *p_sid_str)
646    {
647            uint32_t u_sid;
648            int i;
649    
650            u_sid = (uint32_t)sid;
651            for (i = 0; i < SID_STR_LEN - 1; i++)
652            {
653                    p_sid_str[i] = (char)(u_sid % 255 + 1);
654                    u_sid /= 255;
655            }
656            p_sid_str[i] = '\0';
657    }
658    
659    SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
660    {
661            SECTION_LIST *p_section;
662            char sid_str[SID_STR_LEN];
663    
664            if (p_section_list_pool == NULL)
665            {
666                    log_error("session_list_pool not initialized");
667                    return NULL;
668            }
669    
670            if (p_section_list_pool->section_count >= BBS_max_section)
671            {
672                    log_error("section_count reach limit %d >= %d", p_section_list_pool->section_count, BBS_max_section);
673                  return NULL;                  return NULL;
674          }          }
675    
676          p_section = p_section_list_pool + section_list_count;          sid_to_str(sid, sid_str);
677    
678            p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
679    
680          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          p_section->sid = sid;
681          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->ex_menu_tm = 0;
682    
683          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
684          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
685    
686          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
687          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
688    
689          if (trie_dict_set(p_trie_dict_section_list, sname, section_list_count) != 1)          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
690            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
691    
692            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
693          {          {
694                  log_error("trie_dict_set(section_data, %s, %d) error\n", sname, section_list_count);                  log_error("trie_dict_set(section, %s, %d) error", sname, p_section_list_pool->section_count);
695                    return NULL;
696            }
697    
698            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
699            {
700                    log_error("trie_dict_set(section, %d, %d) error", sid, p_section_list_pool->section_count);
701                  return NULL;                  return NULL;
702          }          }
703    
704          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
705    
706          section_list_count++;          p_section_list_pool->section_count++;
707    
708          return p_section;          return p_section;
709  }  }
710    
711    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
712    {
713            int64_t index;
714    
715            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
716            {
717                    log_error("NULL pointer error");
718                    return -1;
719            }
720    
721            index = get_section_index(p_section);
722    
723            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
724            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
725    
726            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
727            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
728    
729            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
730            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
731    
732            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
733            {
734                    log_error("trie_dict_set(section, %s, %d) error", sname, index);
735                    return -2;
736            }
737    
738            return 0;
739    }
740    
741  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
742  {  {
743          p_section->article_count = 0;          p_section->article_count = 0;
# Line 376  void section_list_reset_articles(SECTION Line 749  void section_list_reset_articles(SECTION
749    
750          p_section->page_count = 0;          p_section->page_count = 0;
751          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
752    
753            p_section->ontop_article_count = 0;
754  }  }
755    
756  void section_list_cleanup(void)  SECTION_LIST *section_list_find_by_name(const char *sname)
757  {  {
758          if (p_trie_dict_section_list != NULL)          int64_t index;
759            int ret;
760    
761            if (p_section_list_pool == NULL)
762          {          {
763                  trie_dict_destroy(p_trie_dict_section_list);                  log_error("section_list not initialized");
764                  p_trie_dict_section_list = NULL;                  return NULL;
765          }          }
766    
767          if (p_section_list_pool != NULL)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
768            if (ret < 0)
769          {          {
770                  free(p_section_list_pool);                  log_error("trie_dict_get(section, %s) error", sname);
771                  p_section_list_pool = NULL;                  return NULL;
772            }
773            else if (ret == 0)
774            {
775                    return NULL;
776          }          }
777    
778          section_list_count = 0;          return (p_section_list_pool->sections + index);
779  }  }
780    
781  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
782  {  {
783          int64_t index;          int64_t index;
784            int ret;
785            char sid_str[SID_STR_LEN];
786    
787          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL)
788          {          {
789                  log_error("section_list not initialized\n");                  log_error("section_list not initialized");
790                  return NULL;                  return NULL;
791          }          }
792    
793          if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1)          sid_to_str(sid, sid_str);
794    
795            ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
796            if (ret < 0)
797            {
798                    log_error("trie_dict_get(section, %d) error", sid);
799                    return NULL;
800            }
801            else if (ret == 0)
802          {          {
                 log_error("trie_dict_get(section_data, %s) error\n", sname);  
803                  return NULL;                  return NULL;
804          }          }
805    
806          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
807  }  }
808    
809  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 424  int section_list_append_article(SECTION_ Line 816  int section_list_append_article(SECTION_
816    
817          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
818          {          {
819                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error");
820                  return -1;                  return -1;
821          }          }
822    
823          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
824          {          {
825                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
826                  return -1;                  return -1;
827          }          }
828    
829            if (p_section->sid != p_article_src->sid)
830            {
831                    log_error("section_list_append_article() error: section sid %d != article sid %d", p_section->sid, p_article_src->sid);
832                    return -2;
833            }
834    
835          if (p_section->article_count >= BBS_article_limit_per_section)          if (p_section->article_count >= BBS_article_limit_per_section)
836          {          {
837                  log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);                  log_error("section_list_append_article() error: article_count reach limit in section %d", p_section->sid);
838                  return -2;                  return -2;
839          }          }
840    
841          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
842                  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)
843          {          {
844                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
845                  {                  {
846                          log_error("pop_free_article_block() error\n");                          log_error("pop_free_article_block() error");
847                          return -2;                          return -2;
848                  }                  }
849    
850                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
851                  {                  {
852                          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;
853                  }                  }
854    
855                  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 466  int section_list_append_article(SECTION_ Line 864  int section_list_append_article(SECTION_
864          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
865          if (p_article_src->aid <= last_aid)          if (p_article_src->aid <= last_aid)
866          {          {
867                  log_error("section_data_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);                  log_error("section_list_append_article(aid=%d) error: last_aid=%d", p_article_src->aid, last_aid);
868                  return -3;                  return -3;
869          }          }
870    
# Line 488  int section_list_append_article(SECTION_ Line 886  int section_list_append_article(SECTION_
886                  p_topic_head = article_block_find_by_aid(p_article->tid);                  p_topic_head = article_block_find_by_aid(p_article->tid);
887                  if (p_topic_head == NULL)                  if (p_topic_head == NULL)
888                  {                  {
889                          log_error("search head of topic (aid=%d) error\n", p_article->tid);                          log_error("search head of topic (aid=%d) error", p_article->tid);
890                          return -4;                          return -4;
891                  }                  }
892    
893                  p_topic_tail = p_topic_head->p_topic_prior;                  p_topic_tail = p_topic_head->p_topic_prior;
894                  if (p_topic_tail == NULL)                  if (p_topic_tail == NULL)
895                  {                  {
896                          log_error("tail of topic (aid=%d) is NULL\n", p_article->tid);                          log_error("tail of topic (aid=%d) is NULL", p_article->tid);
897                          return -4;                          return -4;
898                  }                  }
899          }          }
# Line 530  int section_list_append_article(SECTION_ Line 928  int section_list_append_article(SECTION_
928          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
929    
930          // Update page          // Update page
931          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) ||
932                    p_section->page_count == 0)
933          {          {
934                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
935                  p_section->page_count++;                  p_section->page_count++;
# Line 542  int section_list_append_article(SECTION_ Line 941  int section_list_append_article(SECTION_
941                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
942          }          }
943    
944            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
945            {
946                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error",
947                                      p_section->sid, p_article->aid);
948                    return -5;
949            }
950    
951          return 0;          return 0;
952  }  }
953    
# Line 553  int section_list_set_article_visible(SEC Line 959  int section_list_set_article_visible(SEC
959    
960          if (p_section == NULL)          if (p_section == NULL)
961          {          {
962                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error");
963                  return -2;                  return -1;
964          }          }
965    
966          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 563  int section_list_set_article_visible(SEC Line 969  int section_list_set_article_visible(SEC
969                  return -1; // Not found                  return -1; // Not found
970          }          }
971    
972            if (p_section->sid != p_article->sid)
973            {
974                    log_error("Inconsistent section sid %d != article sid %d", p_section->sid, p_article->sid);
975                    return -2;
976            }
977    
978          if (p_article->visible == visible)          if (p_article->visible == visible)
979          {          {
980                  return 0; // Already set                  return 0; // Already set
# Line 572  int section_list_set_article_visible(SEC Line 984  int section_list_set_article_visible(SEC
984          {          {
985                  p_section->visible_article_count--;                  p_section->visible_article_count--;
986    
987                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
988                    {
989                            log_error("user_article_cnt_inc(uid=%d, -1) error", p_article->uid);
990                    }
991    
992                  if (p_article->tid == 0)                  if (p_article->tid == 0)
993                  {                  {
994                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 581  int section_list_set_article_visible(SEC Line 998  int section_list_set_article_visible(SEC
998                          {                          {
999                                  if (p_reply->tid != aid)                                  if (p_reply->tid != aid)
1000                                  {                                  {
1001                                          log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid);                                          log_error("Inconsistent tid = %d found in reply %d of topic %d", p_reply->tid, p_reply->aid, aid);
1002                                          continue;                                          continue;
1003                                  }                                  }
1004    
# Line 590  int section_list_set_article_visible(SEC Line 1007  int section_list_set_article_visible(SEC
1007                                          p_reply->visible = 0;                                          p_reply->visible = 0;
1008                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
1009                                          affected_count++;                                          affected_count++;
1010    
1011                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
1012                                            {
1013                                                    log_error("user_article_cnt_inc(uid=%d, -1) error", p_reply->uid);
1014                                            }
1015                                  }                                  }
1016                          }                          }
1017                  }                  }
# Line 602  int section_list_set_article_visible(SEC Line 1024  int section_list_set_article_visible(SEC
1024                  {                  {
1025                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
1026                  }                  }
1027    
1028                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
1029                    {
1030                            log_error("user_article_cnt_inc(uid=%d, 1) error", p_article->uid);
1031                    }
1032          }          }
1033    
1034          p_article->visible = visible;          p_article->visible = visible;
# Line 610  int section_list_set_article_visible(SEC Line 1037  int section_list_set_article_visible(SEC
1037          return affected_count;          return affected_count;
1038  }  }
1039    
1040  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset)  int section_list_set_article_excerption(SECTION_LIST *p_section, int32_t aid, int8_t excerption)
1041    {
1042            ARTICLE *p_article;
1043    
1044            if (p_section == NULL)
1045            {
1046                    log_error("NULL pointer error");
1047                    return -1;
1048            }
1049    
1050            p_article = article_block_find_by_aid(aid);
1051            if (p_article == NULL)
1052            {
1053                    return -1; // Not found
1054            }
1055    
1056            if (p_section->sid != p_article->sid)
1057            {
1058                    log_error("Inconsistent section sid %d != article sid %d", p_section->sid, p_article->sid);
1059                    return -2;
1060            }
1061    
1062            if (p_article->excerption == excerption)
1063            {
1064                    return 0; // Already set
1065            }
1066    
1067            p_article->excerption = excerption;
1068    
1069            return 1;
1070    }
1071    
1072    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
1073    {
1074            int i;
1075    
1076            if (p_section == NULL || p_article == NULL)
1077            {
1078                    log_error("NULL pointer error");
1079                    return -1;
1080            }
1081    
1082            if (p_section->sid != p_article->sid)
1083            {
1084                    log_error("Inconsistent section sid %d != article sid %d", p_section->sid, p_article->sid);
1085                    return -2;
1086            }
1087    
1088            if (p_article->ontop)
1089            {
1090                    for (i = 0; i < p_section->ontop_article_count; i++)
1091                    {
1092                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1093                            {
1094                                    log_error("Inconsistent state found: article %d already ontop in section %d", p_article->aid, p_section->sid);
1095                                    return 0;
1096                            }
1097                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
1098                            {
1099                                    break;
1100                            }
1101                    }
1102    
1103                    // Remove the oldest one if the array of ontop articles is full
1104                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1105                    {
1106                            if (i == 0) // p_article is the oldest one
1107                            {
1108                                    return 0;
1109                            }
1110                            memmove((void *)(p_section->p_ontop_articles),
1111                                            (void *)(p_section->p_ontop_articles + 1),
1112                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1113                            p_section->ontop_article_count--;
1114                            i--;
1115                    }
1116                    else
1117                    {
1118                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1119                                            (void *)(p_section->p_ontop_articles + i),
1120                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1121                    }
1122    
1123                    p_section->p_ontop_articles[i] = p_article;
1124                    p_section->ontop_article_count++;
1125    
1126                    // TODO: debug
1127            }
1128            else // ontop == 0
1129            {
1130                    for (i = 0; i < p_section->ontop_article_count; i++)
1131                    {
1132                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1133                            {
1134                                    break;
1135                            }
1136                    }
1137                    if (i == p_section->ontop_article_count) // not found
1138                    {
1139                            log_error("Inconsistent state found: article %d not ontop in section %d", p_article->aid, p_section->sid);
1140                            return 0;
1141                    }
1142    
1143                    memmove((void *)(p_section->p_ontop_articles + i),
1144                                    (void *)(p_section->p_ontop_articles + i + 1),
1145                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1146                    p_section->ontop_article_count--;
1147            }
1148    
1149            return 0;
1150    }
1151    
1152    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1153    {
1154            int page_count;
1155    
1156            if (p_section == NULL)
1157            {
1158                    log_error("NULL pointer error");
1159                    return -1;
1160            }
1161    
1162            page_count = p_section->page_count - 1 +
1163                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count + BBS_article_limit_per_page - 1) /
1164                                             BBS_article_limit_per_page;
1165    
1166            if (page_count < 0)
1167            {
1168                    page_count = 0;
1169            }
1170    
1171            return page_count;
1172    }
1173    
1174    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1175    {
1176            if (p_section == NULL)
1177            {
1178                    log_error("NULL pointer error");
1179                    return -1;
1180            }
1181    
1182            if (page_id < p_section->page_count - 1)
1183            {
1184                    return BBS_article_limit_per_page;
1185            }
1186            else // if (page_id >= p_section->page_count - 1)
1187            {
1188                    return MIN(MAX(0,
1189                                               (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1190                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1191                                       BBS_article_limit_per_page);
1192            }
1193    }
1194    
1195    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)
1196  {  {
1197          ARTICLE *p_article;          ARTICLE *p_article;
1198          int left;          int left;
# Line 619  ARTICLE *section_list_find_article_with_ Line 1201  ARTICLE *section_list_find_article_with_
1201    
1202          *p_page = -1;          *p_page = -1;
1203          *p_offset = -1;          *p_offset = -1;
1204            *pp_next = NULL;
1205    
1206          if (p_section == NULL)          if (p_section == NULL)
1207          {          {
1208                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error");
1209                  return NULL;                  return NULL;
1210          }          }
1211    
# Line 634  ARTICLE *section_list_find_article_with_ Line 1217  ARTICLE *section_list_find_article_with_
1217          }          }
1218    
1219          left = 0;          left = 0;
1220          right = p_section->page_count;          right = p_section->page_count - 1;
1221    
1222          // 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] ]
1223          while (left < right - 1)          while (left < right)
1224          {          {
1225                  // 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
1226                  mid = (left + right) / 2 + (right - left) % 2;                  mid = left + (right - left + 1) / 2;
   
                 if (mid >= p_section->page_count)  
                 {  
                         log_error("page id (mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
1227    
1228                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1229                  {                  {
1230                          right = mid;                          right = mid - 1;
1231                  }                  }
1232                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1233                  {                  {
1234                          left = mid;                          left = mid;
1235                  }                  }
# Line 663  ARTICLE *section_list_find_article_with_ Line 1240  ARTICLE *section_list_find_article_with_
1240          p_article = p_section->p_page_first_article[*p_page];          p_article = p_section->p_page_first_article[*p_page];
1241    
1242          // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid          // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid
1243          right = (*p_page == p_section->page_count - 1 ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);          right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);
1244    
1245          // left will be the offset of article found or offset to insert          // left will be the offset of article found or offset to insert
1246          left = 0;          left = 0;
1247    
1248          while (1)          while (aid > p_article->aid)
1249          {          {
1250                  if (aid == p_article->aid) // found                  p_article = p_article->p_next;
1251                  {                  left++;
1252                          break;  
1253                  }                  if (aid == p_article->aid)
                 else if (aid < p_article->aid) // not exist  
1254                  {                  {
1255                          p_article = NULL;                          *pp_next = p_article->p_next;
1256                          break;                          break;
1257                  }                  }
1258    
                 // aid > p_article->aid  
                 p_article = p_article->p_next;  
                 left++;  
   
1259                  // over last article in the page                  // over last article in the page
1260                  if (p_article == p_section->p_article_head || p_article->aid >= right)                  if (p_article == p_section->p_article_head || p_article->aid >= right)
1261                  {                  {
1262                          p_article = NULL;                          *pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]);
1263                          break;                          *p_offset = left;
1264                            return NULL; // not found
1265                  }                  }
1266          }          }
1267    
1268            if (aid < p_article->aid)
1269            {
1270                    *pp_next = p_article;
1271                    p_article = NULL; // not found
1272            }
1273            else // aid == p_article->aid
1274            {
1275                    *pp_next = p_article->p_next;
1276            }
1277    
1278          *p_offset = left;          *p_offset = left;
1279    
1280          return p_article;          return p_article;
# Line 700  ARTICLE *section_list_find_article_with_ Line 1283  ARTICLE *section_list_find_article_with_
1283  int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)  int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)
1284  {  {
1285          ARTICLE *p_article;          ARTICLE *p_article;
1286            ARTICLE *p_next;
1287          int32_t page;          int32_t page;
1288          int32_t offset;          int32_t offset;
1289          int visible_article_count;          int visible_article_count;
# Line 707  int section_list_calculate_page(SECTION_ Line 1291  int section_list_calculate_page(SECTION_
1291    
1292          if (p_section == NULL)          if (p_section == NULL)
1293          {          {
1294                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error");
1295                  return -1;                  return -1;
1296          }          }
1297    
1298          p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset);          if (p_section->article_count == 0) // empty
1299          if (p_article == NULL)          {
1300                    p_section->page_count = 0;
1301                    p_section->last_page_visible_article_count = 0;
1302    
1303                    return 0;
1304            }
1305    
1306            if (start_aid > 0)
1307          {          {
1308                  if (page < 0)                  p_article = article_block_find_by_aid(start_aid);
1309                    if (p_article == NULL)
1310                  {                  {
1311                          return 0;                          return -1; // Not found
1312                  }                  }
1313    
1314                  log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n", start_aid, p_section->sid);                  if (p_section->sid != p_article->sid)
1315          }                  {
1316                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d", p_section->sid, p_article->sid);
1317                            return -2;
1318                    }
1319    
1320                    p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
1321                    if (p_article == NULL)
1322                    {
1323                            if (page < 0)
1324                            {
1325                                    return -1;
1326                            }
1327                            log_error("section_list_calculate_page() aid = %d not found in section sid = %d",
1328                                              start_aid, p_section->sid);
1329                            return -2;
1330                    }
1331    
1332          if (offset > 0)                  if (offset > 0)
1333                    {
1334                            p_article = p_section->p_page_first_article[page];
1335                    }
1336            }
1337            else
1338          {          {
1339                  p_article = p_section->p_page_first_article[page];                  p_article = p_section->p_article_head;
1340                    page = 0;
1341                    offset = 0;
1342          }          }
1343    
1344          visible_article_count = 0;          visible_article_count = 0;
# Line 759  int section_list_calculate_page(SECTION_ Line 1373  int section_list_calculate_page(SECTION_
1373    
1374                          if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)                          if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)
1375                          {                          {
1376                                  log_error("Count of page exceed limit in section %d\n", p_section->sid);                                  log_error("Count of page exceed limit in section %d", p_section->sid);
1377                                  break;                                  break;
1378                          }                          }
1379                  }                  }
1380          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1381    
1382          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1383          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1384                                                                                                              ? visible_article_count
1385                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1386    
1387            return 0;
1388    }
1389    
1390    int32_t article_block_last_aid(void)
1391    {
1392            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1393            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1394    
1395            return last_aid;
1396    }
1397    
1398    int article_block_article_count(void)
1399    {
1400            int ret;
1401    
1402            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1403            {
1404                    return -1;
1405            }
1406    
1407            ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1408                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1409    
1410            return ret;
1411    }
1412    
1413    int article_count_of_topic(int32_t aid)
1414    {
1415            ARTICLE *p_article;
1416            int article_count;
1417    
1418            p_article = article_block_find_by_aid(aid);
1419            if (p_article == NULL)
1420            {
1421                    return 0; // Not found
1422            }
1423    
1424            article_count = 0;
1425    
1426            do
1427            {
1428                    if (p_article->tid != 0 && p_article->tid != aid)
1429                    {
1430                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic", aid, p_article->aid);
1431                            break;
1432                    }
1433    
1434                    article_count++;
1435                    p_article = p_article->p_topic_next;
1436            } while (p_article->aid != aid);
1437    
1438            return article_count;
1439    }
1440    
1441    int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid)
1442    {
1443            ARTICLE *p_article;
1444            ARTICLE *p_next;
1445            int32_t page;
1446            int32_t offset;
1447            int32_t move_article_count;
1448            int32_t dest_article_count_old;
1449            int32_t last_unaffected_aid_src;
1450            int32_t first_inserted_aid_dest;
1451            int move_counter;
1452    
1453            if (p_section_src == NULL || p_section_dest == NULL)
1454            {
1455                    log_error("NULL pointer error");
1456                    return -1;
1457            }
1458    
1459            if (p_section_src->sid == p_section_dest->sid)
1460            {
1461                    log_error("section_list_move_topic() src and dest section are the same");
1462                    return -1;
1463            }
1464    
1465            if ((p_article = article_block_find_by_aid(aid)) == NULL)
1466            {
1467                    log_error("article_block_find_by_aid(aid = %d) error: article not found", aid);
1468                    return -2;
1469            }
1470    
1471            if (p_section_src->sid != p_article->sid)
1472            {
1473                    log_error("section_list_move_topic() error: src section sid %d != article %d sid %d",
1474                                      p_section_src->sid, p_article->aid, p_article->sid);
1475                    return -2;
1476            }
1477    
1478            if (p_article->tid != 0)
1479            {
1480                    log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d", aid, p_article->tid);
1481                    return -2;
1482            }
1483    
1484            last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1485    
1486            move_article_count = article_count_of_topic(aid);
1487            if (move_article_count <= 0)
1488            {
1489                    log_error("article_count_of_topic(aid = %d) <= 0", aid);
1490                    return -2;
1491            }
1492    
1493            if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
1494            {
1495                    log_error("section_list_move_topic() error: article_count %d reach limit in section %d",
1496                                      p_section_dest->article_count + move_article_count, p_section_dest->sid);
1497                    return -3;
1498            }
1499    
1500            dest_article_count_old = p_section_dest->article_count;
1501            move_counter = 0;
1502            first_inserted_aid_dest = p_article->aid;
1503    
1504            do
1505            {
1506                    if (p_section_src->sid != p_article->sid)
1507                    {
1508                            log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d",
1509                                              p_section_src->sid, p_article->aid, p_article->sid);
1510                            p_article = p_article->p_topic_next;
1511                            continue;
1512                    }
1513    
1514                    // Remove from bi-directional article list of src section
1515                    if (p_section_src->p_article_head == p_article)
1516                    {
1517                            p_section_src->p_article_head = p_article->p_next;
1518                    }
1519                    if (p_section_src->p_article_tail == p_article)
1520                    {
1521                            p_section_src->p_article_tail = p_article->p_prior;
1522                    }
1523                    if (p_section_src->p_article_head == p_article) // Single element list
1524                    {
1525                            p_section_src->p_article_head = NULL;
1526                            p_section_src->p_article_tail = NULL;
1527                    }
1528                    else
1529                    {
1530                            // Only update neighbor pointers if list is not empty after removal
1531                            p_article->p_prior->p_next = p_article->p_next;
1532                            p_article->p_next->p_prior = p_article->p_prior;
1533                    }
1534    
1535                    // Update sid of article
1536                    p_article->sid = p_section_dest->sid;
1537    
1538                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1539                    {
1540                            log_error("section_list_move_topic() warning: article %d already in section %d", p_article->aid, p_section_dest->sid);
1541                            p_article = p_article->p_topic_next;
1542                            continue;
1543                    }
1544    
1545                    // Insert into bi-directional article list of dest section
1546                    if (p_next == NULL) // empty section
1547                    {
1548                            p_section_dest->p_article_head = p_article;
1549                            p_section_dest->p_article_tail = p_article;
1550                            p_article->p_prior = p_article;
1551                            p_article->p_next = p_article;
1552                    }
1553                    else
1554                    {
1555                            if (p_section_dest->p_article_head == p_next)
1556                            {
1557                                    if (p_article->aid < p_next->aid)
1558                                    {
1559                                            p_section_dest->p_article_head = p_article;
1560                                    }
1561                                    else // p_article->aid > p_next->aid
1562                                    {
1563                                            p_section_dest->p_article_tail = p_article;
1564                                    }
1565                            }
1566    
1567                            p_article->p_prior = p_next->p_prior;
1568                            p_article->p_next = p_next;
1569                            p_next->p_prior->p_next = p_article;
1570                            p_next->p_prior = p_article;
1571                    }
1572    
1573                    // Update article / topic counter of src / desc section
1574                    p_section_src->article_count--;
1575                    p_section_dest->article_count++;
1576                    if (p_article->tid == 0)
1577                    {
1578                            p_section_src->topic_count--;
1579                            p_section_dest->topic_count++;
1580                    }
1581    
1582                    // Update visible article / topic counter of src / desc section
1583                    if (p_article->visible)
1584                    {
1585                            p_section_src->visible_article_count--;
1586                            p_section_dest->visible_article_count++;
1587                            if (p_article->tid == 0)
1588                            {
1589                                    p_section_src->visible_topic_count--;
1590                                    p_section_dest->visible_topic_count++;
1591                            }
1592                    }
1593    
1594                    // Update page for empty dest section
1595                    if (p_section_dest->article_count == 1)
1596                    {
1597                            p_section_dest->p_page_first_article[0] = p_article;
1598                            p_section_dest->page_count = 1;
1599                            p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0);
1600                    }
1601    
1602                    p_article = p_article->p_topic_next;
1603    
1604                    move_counter++;
1605                    if (move_counter % CALCULATE_PAGE_THRESHOLD == 0)
1606                    {
1607                            // Re-calculate pages of desc section
1608                            if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1609                            {
1610                                    log_error("section_list_calculate_page(dest section = %d, aid = %d) error",
1611                                                      p_section_dest->sid, first_inserted_aid_dest);
1612                            }
1613    
1614                            first_inserted_aid_dest = p_article->aid;
1615                    }
1616            } while (p_article->aid != aid);
1617    
1618            if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1619            {
1620                    log_error("section_list_move_topic() warning: count of moved articles %d != %d",
1621                                      p_section_dest->article_count - dest_article_count_old, move_article_count);
1622            }
1623    
1624            // Re-calculate pages of src section
1625            if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1626            {
1627                    log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d",
1628                                      p_section_src->sid, last_unaffected_aid_src, aid);
1629            }
1630    
1631            if (move_counter % CALCULATE_PAGE_THRESHOLD != 0)
1632            {
1633                    // Re-calculate pages of desc section
1634                    if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1635                    {
1636                            log_error("section_list_calculate_page(dest section = %d, aid = %d) error",
1637                                              p_section_dest->sid, first_inserted_aid_dest);
1638                    }
1639            }
1640    
1641            return move_article_count;
1642    }
1643    
1644    int get_section_index(SECTION_LIST *p_section)
1645    {
1646            int index;
1647    
1648            if (p_section_list_pool == NULL)
1649            {
1650                    log_error("get_section_index() error: uninitialized");
1651                    return -1;
1652            }
1653    
1654            if (p_section == NULL)
1655            {
1656                    index = BBS_max_section;
1657            }
1658            else
1659            {
1660                    index = (int)(p_section - p_section_list_pool->sections);
1661                    if (index < 0 || index >= BBS_max_section)
1662                    {
1663                            log_error("get_section_index(%d) error: index out of range", index);
1664                            return -2;
1665                    }
1666            }
1667    
1668            return index;
1669    }
1670    
1671    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1672    {
1673            if (p_section == NULL)
1674            {
1675                    log_error("NULL pointer error");
1676                    return -1;
1677            }
1678    
1679            if (section_list_rd_lock(p_section) < 0)
1680            {
1681                    log_error("section_list_rd_lock(sid=%d) error", p_section->sid);
1682                    return -2;
1683            }
1684    
1685            if (sname != NULL)
1686            {
1687                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1688            }
1689            if (stitle != NULL)
1690            {
1691                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1692            }
1693            if (master_list != NULL)
1694            {
1695                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1696            }
1697    
1698            // release lock of section
1699            if (section_list_rd_unlock(p_section) < 0)
1700            {
1701                    log_error("section_list_rd_unlock(sid=%d) error", p_section->sid);
1702                    return -2;
1703            }
1704    
1705            return 0;
1706    }
1707    
1708    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1709    {
1710            int index;
1711    #ifdef HAVE_SYSTEM_V
1712            struct sembuf sops[4];
1713    #endif
1714            struct timespec timeout;
1715            int ret = 0;
1716    
1717            index = get_section_index(p_section);
1718            if (index < 0)
1719            {
1720                    return -2;
1721            }
1722    
1723            timeout.tv_sec = wait_sec;
1724            timeout.tv_nsec = 0;
1725    
1726    #ifdef HAVE_SYSTEM_V
1727            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1728            sops[0].sem_op = 0;                                                                // wait until unlocked
1729            sops[0].sem_flg = 0;
1730    
1731            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1732            sops[1].sem_op = 1;                                                        // lock
1733            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1734    
1735            // Read lock on any specific section will also acquire single read lock on "all section"
1736            // so that write lock on all section only need to acquire single write on on "all section"
1737            // rather than to acquire multiple write locks on all the available sections.
1738            if (index != BBS_max_section)
1739            {
1740                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1741                    sops[2].sem_op = 0;                                                // wait until unlocked
1742                    sops[2].sem_flg = 0;
1743    
1744                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1745                    sops[3].sem_op = 1;                                        // lock
1746                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1747            }
1748    
1749            ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1750            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1751            {
1752                    log_error("semop(index = %d, lock read) error %d", index, errno);
1753            }
1754    #else
1755            if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1)
1756            {
1757                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1758                    {
1759                            log_error("sem_timedwait(sem[%d]) error %d", index, errno);
1760                    }
1761                    return -1;
1762            }
1763    
1764            if (index != BBS_max_section)
1765            {
1766                    if (sem_timedwait(&(p_section_list_pool->sem[BBS_max_section]), &timeout) == -1)
1767                    {
1768                            if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1769                            {
1770                                    log_error("sem_timedwait(sem[%d]) error %d", BBS_max_section, errno);
1771                            }
1772                            // release previously acquired lock
1773                            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1774                            {
1775                                    log_error("sem_post(sem[%d]) error %d", index, errno);
1776                            }
1777                            return -1;
1778                    }
1779            }
1780    
1781            if (p_section_list_pool->write_lock_count[index] == 0 &&
1782                    (index != BBS_max_section && p_section_list_pool->write_lock_count[BBS_max_section] == 0))
1783            {
1784                    p_section_list_pool->read_lock_count[index]++;
1785                    if (index != BBS_max_section)
1786                    {
1787                            p_section_list_pool->read_lock_count[BBS_max_section]++;
1788                    }
1789            }
1790            else
1791            {
1792                    errno = EAGAIN;
1793                    ret = -1;
1794            }
1795    
1796            if (index != BBS_max_section)
1797            {
1798                    // release lock on "all section"
1799                    if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1)
1800                    {
1801                            log_error("sem_post(sem[%d]) error %d", BBS_max_section, errno);
1802                            ret = -1;
1803                    }
1804            }
1805    
1806            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1807            {
1808                    log_error("sem_post(sem[%d]) error %d", index, errno);
1809                    return -1;
1810            }
1811    #endif
1812    
1813            return ret;
1814    }
1815    
1816    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1817    {
1818            int index;
1819    #ifdef HAVE_SYSTEM_V
1820            struct sembuf sops[3];
1821    #endif
1822            struct timespec timeout;
1823            int ret = 0;
1824    
1825            index = get_section_index(p_section);
1826            if (index < 0)
1827            {
1828                    return -2;
1829            }
1830    
1831            timeout.tv_sec = wait_sec;
1832            timeout.tv_nsec = 0;
1833    
1834    #ifdef HAVE_SYSTEM_V
1835            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1836            sops[0].sem_op = 0;                                                                // wait until unlocked
1837            sops[0].sem_flg = 0;
1838    
1839            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1840            sops[1].sem_op = 1;                                                                // lock
1841            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1842    
1843            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1844            sops[2].sem_op = 0;                                                        // wait until unlocked
1845            sops[2].sem_flg = 0;
1846    
1847            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1848            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1849            {
1850                    log_error("semop(index = %d, lock write) error %d", index, errno);
1851            }
1852    #else
1853            if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1)
1854            {
1855                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1856                    {
1857                            log_error("sem_timedwait(sem[%d]) error %d", index, errno);
1858                    }
1859                    return -1;
1860            }
1861    
1862            if (p_section_list_pool->read_lock_count[index] == 0 && p_section_list_pool->write_lock_count[index] == 0)
1863            {
1864                    p_section_list_pool->write_lock_count[index]++;
1865            }
1866            else
1867            {
1868                    errno = EAGAIN;
1869                    ret = -1;
1870            }
1871    
1872            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1873            {
1874                    log_error("sem_post(sem[%d]) error %d", index, errno);
1875                    return -1;
1876            }
1877    #endif
1878    
1879            return ret;
1880    }
1881    
1882    int section_list_rd_unlock(SECTION_LIST *p_section)
1883    {
1884            int index;
1885    #ifdef HAVE_SYSTEM_V
1886            struct sembuf sops[2];
1887    #endif
1888            int ret = 0;
1889    
1890            index = get_section_index(p_section);
1891            if (index < 0)
1892            {
1893                    return -2;
1894            }
1895    
1896    #ifdef HAVE_SYSTEM_V
1897            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1898            sops[0].sem_op = -1;                                               // unlock
1899            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1900    
1901            if (index != BBS_max_section)
1902            {
1903                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1904                    sops[1].sem_op = -1;                                     // unlock
1905                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1906            }
1907    
1908            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1909            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1910            {
1911                    log_error("semop(index = %d, unlock read) error %d", index, errno);
1912            }
1913    #else
1914            if (sem_wait(&(p_section_list_pool->sem[index])) == -1)
1915            {
1916                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1917                    {
1918                            log_error("sem_wait(sem[%d]) error %d", index, errno);
1919                    }
1920                    return -1;
1921            }
1922    
1923            if (index != BBS_max_section)
1924            {
1925                    if (sem_wait(&(p_section_list_pool->sem[BBS_max_section])) == -1)
1926                    {
1927                            if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1928                            {
1929                                    log_error("sem_wait(sem[%d]) error %d", BBS_max_section, errno);
1930                            }
1931                            // release previously acquired lock
1932                            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1933                            {
1934                                    log_error("sem_post(sem[%d]) error %d", index, errno);
1935                            }
1936                            return -1;
1937                    }
1938            }
1939    
1940            if (p_section_list_pool->read_lock_count[index] > 0)
1941            {
1942                    p_section_list_pool->read_lock_count[index]--;
1943            }
1944            else
1945            {
1946                    log_error("read_lock_count[%d] already 0", index);
1947            }
1948    
1949            if (index != BBS_max_section && p_section_list_pool->read_lock_count[BBS_max_section] > 0)
1950            {
1951                    p_section_list_pool->read_lock_count[BBS_max_section]--;
1952            }
1953            else
1954            {
1955                    log_error("read_lock_count[%d] already 0", BBS_max_section);
1956            }
1957    
1958            if (index != BBS_max_section)
1959            {
1960                    // release lock on "all section"
1961                    if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1)
1962                    {
1963                            log_error("sem_post(sem[%d]) error %d", BBS_max_section, errno);
1964                            ret = -1;
1965                    }
1966            }
1967    
1968            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1969            {
1970                    log_error("sem_post(sem[%d]) error %d", index, errno);
1971                    return -1;
1972            }
1973    #endif
1974    
1975            return ret;
1976    }
1977    
1978    int section_list_rw_unlock(SECTION_LIST *p_section)
1979    {
1980            int index;
1981    #ifdef HAVE_SYSTEM_V
1982            struct sembuf sops[1];
1983    #endif
1984            int ret = 0;
1985    
1986            index = get_section_index(p_section);
1987            if (index < 0)
1988            {
1989                    return -2;
1990            }
1991    
1992    #ifdef HAVE_SYSTEM_V
1993            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1994            sops[0].sem_op = -1;                                                       // unlock
1995            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1996    
1997            ret = semop(p_section_list_pool->semid, sops, 1);
1998            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1999            {
2000                    log_error("semop(index = %d, unlock write) error %d", index, errno);
2001            }
2002    #else
2003            if (sem_wait(&(p_section_list_pool->sem[index])) == -1)
2004            {
2005                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
2006                    {
2007                            log_error("sem_wait(sem[%d]) error %d", index, errno);
2008                    }
2009                    return -1;
2010            }
2011    
2012            if (p_section_list_pool->write_lock_count[index] > 0)
2013            {
2014                    p_section_list_pool->write_lock_count[index]--;
2015            }
2016            else
2017            {
2018                    log_error("write_lock_count[%d] already 0", index);
2019            }
2020    
2021            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
2022            {
2023                    log_error("sem_post(sem[%d]) error %d", index, errno);
2024                    return -1;
2025            }
2026    #endif
2027    
2028            return ret;
2029    }
2030    
2031    int section_list_rd_lock(SECTION_LIST *p_section)
2032    {
2033            int timer = 0;
2034            int sid = (p_section == NULL ? 0 : p_section->sid);
2035            int ret = -1;
2036            time_t tm_first_failure = 0;
2037    
2038            while (!SYS_server_exit)
2039            {
2040                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
2041                    if (ret == 0) // success
2042                    {
2043                            break;
2044                    }
2045                    else if (errno == EAGAIN || errno == EINTR) // retry
2046                    {
2047                            // Dead lock detection
2048                            if (tm_first_failure == 0)
2049                            {
2050                                    time(&tm_first_failure);
2051                            }
2052    
2053                            timer++;
2054                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
2055                            {
2056                                    log_error("section_list_try_rd_lock() tried %d times on section %d", timer, sid);
2057                                    if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT)
2058                                    {
2059                                            log_error("Unable to acquire rd_lock for %d seconds", time(NULL) - tm_first_failure);
2060    #ifndef HAVE_SYSTEM_V
2061                                            section_list_reset_lock(p_section);
2062                                            log_error("Reset POSIX semaphore to resolve dead lock");
2063    #endif
2064                                            break;
2065                                    }
2066                            }
2067                            usleep(100 * 1000); // 0.1 second
2068                    }
2069                    else // failed
2070                    {
2071                            log_error("section_list_try_rd_lock() failed on section %d", sid);
2072                            break;
2073                    }
2074            }
2075    
2076            return ret;
2077    }
2078    
2079    int section_list_rw_lock(SECTION_LIST *p_section)
2080    {
2081            int timer = 0;
2082            int sid = (p_section == NULL ? 0 : p_section->sid);
2083            int ret = -1;
2084            time_t tm_first_failure = 0;
2085    
2086            while (!SYS_server_exit)
2087            {
2088                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
2089                    if (ret == 0) // success
2090                    {
2091                            break;
2092                    }
2093                    else if (errno == EAGAIN || errno == EINTR) // retry
2094                    {
2095                            // Dead lock detection
2096                            if (tm_first_failure == 0)
2097                            {
2098                                    time(&tm_first_failure);
2099                            }
2100    
2101                            timer++;
2102                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
2103                            {
2104                                    log_error("section_list_try_rw_lock() tried %d times on section %d", timer, sid);
2105                                    if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT)
2106                                    {
2107                                            log_error("Unable to acquire rw_lock for %d seconds", time(NULL) - tm_first_failure);
2108    #ifndef HAVE_SYSTEM_V
2109                                            section_list_reset_lock(p_section);
2110                                            log_error("Reset POSIX semaphore to resolve dead lock");
2111    #endif
2112                                            break;
2113                                    }
2114                            }
2115                            usleep(100 * 1000); // 0.1 second
2116                    }
2117                    else // failed
2118                    {
2119                            log_error("section_list_try_rw_lock() failed on section %d", sid);
2120                            break;
2121                    }
2122            }
2123    
2124            return ret;
2125    }
2126    
2127    #ifndef HAVE_SYSTEM_V
2128    int section_list_reset_lock(SECTION_LIST *p_section)
2129    {
2130            int index;
2131    
2132            if (p_section == NULL)
2133            {
2134                    log_error("NULL pointer error");
2135                    return -1;
2136            }
2137    
2138            index = get_section_index(p_section);
2139            if (index < 0)
2140            {
2141                    return -2;
2142            }
2143    
2144            if (sem_destroy(&(p_section_list_pool->sem[index])) == -1)
2145            {
2146                    log_error("sem_destroy(sem[%d]) error (%d)", index, errno);
2147            }
2148    
2149            p_section_list_pool->read_lock_count[index] = 0;
2150            p_section_list_pool->write_lock_count[index] = 0;
2151    
2152            if (sem_init(&(p_section_list_pool->sem[index]), 1, 1) == -1)
2153            {
2154                    log_error("sem_init(sem[%d]) error (%d)", index, errno);
2155            }
2156    
2157            if (index != BBS_max_section)
2158            {
2159                    if (sem_destroy(&(p_section_list_pool->sem[BBS_max_section])) == -1)
2160                    {
2161                            log_error("sem_destroy(sem[%d]) error (%d)", BBS_max_section, errno);
2162                    }
2163    
2164                    p_section_list_pool->read_lock_count[BBS_max_section] = 0;
2165                    p_section_list_pool->write_lock_count[BBS_max_section] = 0;
2166    
2167                    if (sem_init(&(p_section_list_pool->sem[BBS_max_section]), 1, 1) == -1)
2168                    {
2169                            log_error("sem_init(sem[%d]) error (%d)", BBS_max_section, errno);
2170                    }
2171            }
2172    
2173          return 0;          return 0;
2174  }  }
2175    #endif


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

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