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


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

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