/[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.64 by sysadm, Thu Nov 20 11:31:56 2025 UTC Revision 1.68 by sysadm, Wed Jan 7 14:39:16 2026 UTC
# Line 3  Line 3 
3   * section_list   * section_list
4   *   - data models and basic operations of section and article   *   - data models and basic operations of section and article
5   *   *
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7   */   */
8    
9  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
# Line 98  int article_block_init(const char *filen Line 98  int article_block_init(const char *filen
98    
99          if (p_article_block_pool != NULL)          if (p_article_block_pool != NULL)
100          {          {
101                  log_error("article_block_pool already initialized\n");                  log_error("article_block_pool already initialized");
102                  return -1;                  return -1;
103          }          }
104    
105          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
106          {          {
107                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);                  log_error("article_block_count exceed limit %d", ARTICLE_BLOCK_PER_POOL);
108                  return -2;                  return -2;
109          }          }
110    
# Line 118  int article_block_init(const char *filen Line 118  int article_block_init(const char *filen
118    
119          if (shm_unlink(filepath) == -1 && errno != ENOENT)          if (shm_unlink(filepath) == -1 && errno != ENOENT)
120          {          {
121                  log_error("shm_unlink(%s) error (%d)\n", filepath, errno);                  log_error("shm_unlink(%s) error (%d)", filepath, errno);
122                  return -2;                  return -2;
123          }          }
124    
125          if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)          if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
126          {          {
127                  log_error("shm_open(%s) error (%d)\n", filepath, errno);                  log_error("shm_open(%s) error (%d)", filepath, errno);
128                  return -2;                  return -2;
129          }          }
130          if (ftruncate(fd, (off_t)size) == -1)          if (ftruncate(fd, (off_t)size) == -1)
131          {          {
132                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);                  log_error("ftruncate(size=%d) error (%d)", size, errno);
133                  close(fd);                  close(fd);
134                  return -2;                  return -2;
135          }          }
# Line 137  int article_block_init(const char *filen Line 137  int article_block_init(const char *filen
137          p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);          p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
138          if (p_shm == MAP_FAILED)          if (p_shm == MAP_FAILED)
139          {          {
140                  log_error("mmap() error (%d)\n", errno);                  log_error("mmap() error (%d)", errno);
141                  close(fd);                  close(fd);
142                  return -2;                  return -2;
143          }          }
144    
145          if (close(fd) < 0)          if (close(fd) < 0)
146          {          {
147                  log_error("close(fd) error (%d)\n", errno);                  log_error("close(fd) error (%d)", errno);
148                  return -1;                  return -1;
149          }          }
150    
# Line 164  int article_block_init(const char *filen Line 164  int article_block_init(const char *filen
164    
165                  if (shm_unlink(filepath) == -1 && errno != ENOENT)                  if (shm_unlink(filepath) == -1 && errno != ENOENT)
166                  {                  {
167                          log_error("shm_unlink(%s) error (%d)\n", filepath, errno);                          log_error("shm_unlink(%s) error (%d)", filepath, errno);
168                          return -2;                          return -2;
169                  }                  }
170    
171                  if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)                  if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
172                  {                  {
173                          log_error("shm_open(%s) error (%d)\n", filepath, errno);                          log_error("shm_open(%s) error (%d)", filepath, errno);
174                          return -2;                          return -2;
175                  }                  }
176                  if (ftruncate(fd, (off_t)size) == -1)                  if (ftruncate(fd, (off_t)size) == -1)
177                  {                  {
178                          log_error("ftruncate(size=%d) error (%d)\n", size, errno);                          log_error("ftruncate(size=%d) error (%d)", size, errno);
179                          close(fd);                          close(fd);
180                          return -2;                          return -2;
181                  }                  }
# Line 183  int article_block_init(const char *filen Line 183  int article_block_init(const char *filen
183                  p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);                  p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
184                  if (p_shm == MAP_FAILED)                  if (p_shm == MAP_FAILED)
185                  {                  {
186                          log_error("mmap() error (%d)\n", errno);                          log_error("mmap() error (%d)", errno);
187                          close(fd);                          close(fd);
188                          return -2;                          return -2;
189                  }                  }
190    
191                  if (close(fd) < 0)                  if (close(fd) < 0)
192                  {                  {
193                          log_error("close(fd) error (%d)\n", errno);                          log_error("close(fd) error (%d)", errno);
194                          return -1;                          return -1;
195                  }                  }
196    
# Line 235  void article_block_cleanup(void) Line 235  void article_block_cleanup(void)
235    
236                  if (shm_unlink(filepath) == -1 && errno != ENOENT)                  if (shm_unlink(filepath) == -1 && errno != ENOENT)
237                  {                  {
238                          log_error("shm_unlink(%s) error (%d)\n", filepath, errno);                          log_error("shm_unlink(%s) error (%d)", filepath, errno);
239                  }                  }
240          }          }
241    
# Line 243  void article_block_cleanup(void) Line 243  void article_block_cleanup(void)
243    
244          if (shm_unlink(filepath) == -1 && errno != ENOENT)          if (shm_unlink(filepath) == -1 && errno != ENOENT)
245          {          {
246                  log_error("shm_unlink(%s) error (%d)\n", filepath, errno);                  log_error("shm_unlink(%s) error (%d)", filepath, errno);
247          }          }
248    
249          detach_article_block_shm();          detach_article_block_shm();
# Line 251  void article_block_cleanup(void) Line 251  void article_block_cleanup(void)
251    
252  int set_article_block_shm_readonly(void)  int set_article_block_shm_readonly(void)
253  {  {
254          int i;          // int i;
255    
256          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
257          {          {
258                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
259                  return -1;                  return -1;
260          }          }
261    
262          for (i = 0; i < p_article_block_pool->shm_count; i++)          // for (i = 0; i < p_article_block_pool->shm_count; i++)
263          {          // {
264                  if ((p_article_block_pool->shm_pool + i)->p_shm != NULL &&          //      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)          //              mprotect((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size, PROT_READ) < 0)
266                  {          //      {
267                          log_error("mprotect() error (%d)\n", errno);          //              log_error("mprotect() error (%d)", errno);
268                          return -2;          //              return -2;
269                  }          //      }
270          }          // }
271    
272          if (p_article_block_pool != NULL &&          if (p_article_block_pool != NULL &&
273                  mprotect(p_article_block_pool, p_article_block_pool->shm_size, PROT_READ) < 0)                  mprotect(p_article_block_pool, p_article_block_pool->shm_size, PROT_READ) < 0)
274          {          {
275                  log_error("mprotect() error (%d)\n", errno);                  log_error("mprotect() error (%d)", errno);
276                  return -3;                  return -3;
277          }          }
278    
# Line 281  int set_article_block_shm_readonly(void) Line 281  int set_article_block_shm_readonly(void)
281    
282  int detach_article_block_shm(void)  int detach_article_block_shm(void)
283  {  {
284            int shm_count;
285            size_t pool_shm_size;
286    
287          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
288          {          {
289                  return -1;                  return -1;
290          }          }
291    
292          for (int i = 0; i < p_article_block_pool->shm_count; i++)          shm_count = p_article_block_pool->shm_count;
293            pool_shm_size = p_article_block_pool->shm_size;
294    
295            for (int i = 0; i < shm_count; i++)
296          {          {
297                  if ((p_article_block_pool->shm_pool + i)->p_shm != NULL &&                  if ((p_article_block_pool->shm_pool + i)->p_shm != NULL &&
298                          munmap((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size) < 0)                          munmap((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size) < 0)
299                  {                  {
300                          log_error("munmap() error (%d)\n", errno);                          log_error("munmap() error (%d)", errno);
301                          return -2;                          return -2;
302                  }                  }
303          }          }
304    
305          if (p_article_block_pool != NULL && munmap(p_article_block_pool, p_article_block_pool->shm_size) < 0)          if (p_article_block_pool != NULL && munmap(p_article_block_pool, pool_shm_size) < 0)
306          {          {
307                  log_error("munmap() error (%d)\n", errno);                  log_error("munmap() error (%d)", errno);
308                  return -3;                  return -3;
309          }          }
310    
# Line 334  int article_block_reset(void) Line 340  int article_block_reset(void)
340    
341          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
342          {          {
343                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
344                  return -1;                  return -1;
345          }          }
346    
# Line 357  ARTICLE *article_block_find_by_aid(int32 Line 363  ARTICLE *article_block_find_by_aid(int32
363    
364          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
365          {          {
366                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
367                  return NULL;                  return NULL;
368          }          }
369    
# Line 373  ARTICLE *article_block_find_by_aid(int32 Line 379  ARTICLE *article_block_find_by_aid(int32
379          while (left < right)          while (left < right)
380          {          {
381                  // get block offset no less than mid value of left and right block offsets                  // get block offset no less than mid value of left and right block offsets
382                  mid = (left + right) / 2 + (left + right) % 2;                  mid = left + (right - left + 1) / 2;
383    
384                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
385                  {                  {
# Line 393  ARTICLE *article_block_find_by_aid(int32 Line 399  ARTICLE *article_block_find_by_aid(int32
399          // aid in the range [ aid of articles[left], aid of articles[right] ]          // aid in the range [ aid of articles[left], aid of articles[right] ]
400          while (left < right)          while (left < right)
401          {          {
402                  mid = (left + right) / 2;                  mid = left + (right - left) / 2;
403    
404                  if (aid <= p_block->articles[mid].aid)                  if (aid <= p_block->articles[mid].aid)
405                  {                  {
# Line 419  ARTICLE *article_block_find_by_index(int Line 425  ARTICLE *article_block_find_by_index(int
425    
426          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
427          {          {
428                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
429                  return NULL;                  return NULL;
430          }          }
431    
432          if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count)          if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count)
433          {          {
434                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)", index, p_article_block_pool->block_count);
435                  return NULL;                  return NULL;
436          }          }
437    
# Line 433  ARTICLE *article_block_find_by_index(int Line 439  ARTICLE *article_block_find_by_index(int
439    
440          if (index % BBS_article_count_per_block >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
441          {          {
442                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)", index, p_block->article_count);
443                  return NULL;                  return NULL;
444          }          }
445    
# Line 468  extern int section_list_init(const char Line 474  extern int section_list_init(const char
474    
475          if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT)          if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT)
476          {          {
477                  log_error("shm_unlink(%s) error (%d)\n", section_list_shm_name, errno);                  log_error("shm_unlink(%s) error (%d)", section_list_shm_name, errno);
478                  return -2;                  return -2;
479          }          }
480    
481          if ((fd = shm_open(section_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)          if ((fd = shm_open(section_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
482          {          {
483                  log_error("shm_open(%s) error (%d)\n", section_list_shm_name, errno);                  log_error("shm_open(%s) error (%d)", section_list_shm_name, errno);
484                  return -2;                  return -2;
485          }          }
486          if (ftruncate(fd, (off_t)size) == -1)          if (ftruncate(fd, (off_t)size) == -1)
487          {          {
488                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);                  log_error("ftruncate(size=%d) error (%d)", size, errno);
489                  close(fd);                  close(fd);
490                  return -2;                  return -2;
491          }          }
# Line 487  extern int section_list_init(const char Line 493  extern int section_list_init(const char
493          p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);          p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
494          if (p_shm == MAP_FAILED)          if (p_shm == MAP_FAILED)
495          {          {
496                  log_error("mmap() error (%d)\n", errno);                  log_error("mmap() error (%d)", errno);
497                  close(fd);                  close(fd);
498                  return -2;                  return -2;
499          }          }
500    
501          if (close(fd) < 0)          if (close(fd) < 0)
502          {          {
503                  log_error("close(fd) error (%d)\n", errno);                  log_error("close(fd) error (%d)", errno);
504                  return -1;                  return -1;
505          }          }
506    
# Line 508  extern int section_list_init(const char Line 514  extern int section_list_init(const char
514          {          {
515                  if (sem_init(&(p_section_list_pool->sem[i]), 1, 1) == -1)                  if (sem_init(&(p_section_list_pool->sem[i]), 1, 1) == -1)
516                  {                  {
517                          log_error("sem_init(sem[%d]) error (%d)\n", i, errno);                          log_error("sem_init(sem[%d]) error (%d)", i, errno);
518                          return -3;                          return -3;
519                  }                  }
520    
# Line 520  extern int section_list_init(const char Line 526  extern int section_list_init(const char
526          key = ftok(filename, proj_id);          key = ftok(filename, proj_id);
527          if (key == -1)          if (key == -1)
528          {          {
529                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);                  log_error("ftok(%s, %d) error (%d)", filename, proj_id, errno);
530                  return -3;                  return -3;
531          }          }
532    
# Line 528  extern int section_list_init(const char Line 534  extern int section_list_init(const char
534          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
535          if (semid == -1)          if (semid == -1)
536          {          {
537                  log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);                  log_error("semget(section_list_pool_sem, size = %d) error (%d)", size, errno);
538                  return -3;                  return -3;
539          }          }
540    
# Line 538  extern int section_list_init(const char Line 544  extern int section_list_init(const char
544          {          {
545                  if (semctl(semid, i, SETVAL, arg) == -1)                  if (semctl(semid, i, SETVAL, arg) == -1)
546                  {                  {
547                          log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);                          log_error("semctl(section_list_pool_sem, SETVAL) error (%d)", errno);
548                          return -3;                          return -3;
549                  }                  }
550          }          }
# Line 549  extern int section_list_init(const char Line 555  extern int section_list_init(const char
555          p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();          p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
556          if (p_section_list_pool->p_trie_dict_section_by_name == NULL)          if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
557          {          {
558                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM", BBS_max_section);
559                  return -2;                  return -2;
560          }          }
561    
562          p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();          p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
563          if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
564          {          {
565                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM", BBS_max_section);
566                  return -2;                  return -2;
567          }          }
568    
# Line 585  void section_list_cleanup(void) Line 591  void section_list_cleanup(void)
591  #ifdef HAVE_SYSTEM_V  #ifdef HAVE_SYSTEM_V
592          if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)          if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
593          {          {
594                  log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);                  log_error("semctl(semid = %d, IPC_RMID) error (%d)", p_section_list_pool->semid, errno);
595          }          }
596  #else  #else
597          for (int i = 0; i <= BBS_max_section; i++)          for (int i = 0; i <= BBS_max_section; i++)
598          {          {
599                  if (sem_destroy(&(p_section_list_pool->sem[i])) == -1)                  if (sem_destroy(&(p_section_list_pool->sem[i])) == -1)
600                  {                  {
601                          log_error("sem_destroy(sem[%d]) error (%d)\n", i, errno);                          log_error("sem_destroy(sem[%d]) error (%d)", i, errno);
602                  }                  }
603          }          }
604  #endif  #endif
# Line 601  void section_list_cleanup(void) Line 607  void section_list_cleanup(void)
607    
608          if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT)          if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT)
609          {          {
610                  log_error("shm_unlink(%s) error (%d)\n", section_list_shm_name, errno);                  log_error("shm_unlink(%s) error (%d)", section_list_shm_name, errno);
611          }          }
612  }  }
613    
# Line 609  int set_section_list_shm_readonly(void) Line 615  int set_section_list_shm_readonly(void)
615  {  {
616          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
617          {          {
618                  log_error("p_section_list_pool not initialized\n");                  log_error("p_section_list_pool not initialized");
619                  return -1;                  return -1;
620          }          }
621    
622          if (p_section_list_pool != NULL &&          if (p_section_list_pool != NULL &&
623                  mprotect(p_section_list_pool, p_section_list_pool->shm_size, PROT_READ) < 0)                  mprotect(p_section_list_pool, p_section_list_pool->shm_size, PROT_READ) < 0)
624          {          {
625                  log_error("mprotect() error (%d)\n", errno);                  log_error("mprotect() error (%d)", errno);
626                  return -2;                  return -2;
627          }          }
628    
# Line 627  int detach_section_list_shm(void) Line 633  int detach_section_list_shm(void)
633  {  {
634          if (p_section_list_pool != NULL && munmap(p_section_list_pool, p_section_list_pool->shm_size) < 0)          if (p_section_list_pool != NULL && munmap(p_section_list_pool, p_section_list_pool->shm_size) < 0)
635          {          {
636                  log_error("munmap() error (%d)\n", errno);                  log_error("munmap() error (%d)", errno);
637                  return -1;                  return -1;
638          }          }
639    
# Line 657  SECTION_LIST *section_list_create(int32_ Line 663  SECTION_LIST *section_list_create(int32_
663    
664          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
665          {          {
666                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized");
667                  return NULL;                  return NULL;
668          }          }
669    
670          if (p_section_list_pool->section_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
671          {          {
672                  log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);                  log_error("section_count reach limit %d >= %d", p_section_list_pool->section_count, BBS_max_section);
673                  return NULL;                  return NULL;
674          }          }
675    
# Line 685  SECTION_LIST *section_list_create(int32_ Line 691  SECTION_LIST *section_list_create(int32_
691    
692          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
693          {          {
694                  log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count);                  log_error("trie_dict_set(section, %s, %d) error", sname, p_section_list_pool->section_count);
695                  return NULL;                  return NULL;
696          }          }
697    
698          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
699          {          {
700                  log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);                  log_error("trie_dict_set(section, %d, %d) error", sid, p_section_list_pool->section_count);
701                  return NULL;                  return NULL;
702          }          }
703    
# Line 708  int section_list_update(SECTION_LIST *p_ Line 714  int section_list_update(SECTION_LIST *p_
714    
715          if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)          if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
716          {          {
717                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
718                  return -1;                  return -1;
719          }          }
720    
# Line 725  int section_list_update(SECTION_LIST *p_ Line 731  int section_list_update(SECTION_LIST *p_
731    
732          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
733          {          {
734                  log_error("trie_dict_set(section, %s, %d) error\n", sname, index);                  log_error("trie_dict_set(section, %s, %d) error", sname, index);
735                  return -2;                  return -2;
736          }          }
737    
# Line 754  SECTION_LIST *section_list_find_by_name( Line 760  SECTION_LIST *section_list_find_by_name(
760    
761          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
762          {          {
763                  log_error("section_list not initialized\n");                  log_error("section_list not initialized");
764                  return NULL;                  return NULL;
765          }          }
766    
767          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
768          if (ret < 0)          if (ret < 0)
769          {          {
770                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error", sname);
771                  return NULL;                  return NULL;
772          }          }
773          else if (ret == 0)          else if (ret == 0)
# Line 780  SECTION_LIST *section_list_find_by_sid(i Line 786  SECTION_LIST *section_list_find_by_sid(i
786    
787          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
788          {          {
789                  log_error("section_list not initialized\n");                  log_error("section_list not initialized");
790                  return NULL;                  return NULL;
791          }          }
792    
# Line 789  SECTION_LIST *section_list_find_by_sid(i Line 795  SECTION_LIST *section_list_find_by_sid(i
795          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
796          if (ret < 0)          if (ret < 0)
797          {          {
798                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error", sid);
799                  return NULL;                  return NULL;
800          }          }
801          else if (ret == 0)          else if (ret == 0)
# Line 810  int section_list_append_article(SECTION_ Line 816  int section_list_append_article(SECTION_
816    
817          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
818          {          {
819                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
820                  return -1;                  return -1;
821          }          }
822    
823          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
824          {          {
825                  log_error("article_block_pool not initialized\n");                  log_error("article_block_pool not initialized");
826                  return -1;                  return -1;
827          }          }
828    
829          if (p_section->sid != p_article_src->sid)          if (p_section->sid != p_article_src->sid)
830          {          {
831                  log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);                  log_error("section_list_append_article() error: section sid %d != article sid %d", p_section->sid, p_article_src->sid);
832                  return -2;                  return -2;
833          }          }
834    
835          if (p_section->article_count >= BBS_article_limit_per_section)          if (p_section->article_count >= BBS_article_limit_per_section)
836          {          {
837                  log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);                  log_error("section_list_append_article() error: article_count reach limit in section %d", p_section->sid);
838                  return -2;                  return -2;
839          }          }
840    
# Line 837  int section_list_append_article(SECTION_ Line 843  int section_list_append_article(SECTION_
843          {          {
844                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
845                  {                  {
846                          log_error("pop_free_article_block() error\n");                          log_error("pop_free_article_block() error");
847                          return -2;                          return -2;
848                  }                  }
849    
# Line 858  int section_list_append_article(SECTION_ Line 864  int section_list_append_article(SECTION_
864          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
865          if (p_article_src->aid <= last_aid)          if (p_article_src->aid <= last_aid)
866          {          {
867                  log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);                  log_error("section_list_append_article(aid=%d) error: last_aid=%d", p_article_src->aid, last_aid);
868                  return -3;                  return -3;
869          }          }
870    
# Line 880  int section_list_append_article(SECTION_ Line 886  int section_list_append_article(SECTION_
886                  p_topic_head = article_block_find_by_aid(p_article->tid);                  p_topic_head = article_block_find_by_aid(p_article->tid);
887                  if (p_topic_head == NULL)                  if (p_topic_head == NULL)
888                  {                  {
889                          log_error("search head of topic (aid=%d) error\n", p_article->tid);                          log_error("search head of topic (aid=%d) error", p_article->tid);
890                          return -4;                          return -4;
891                  }                  }
892    
893                  p_topic_tail = p_topic_head->p_topic_prior;                  p_topic_tail = p_topic_head->p_topic_prior;
894                  if (p_topic_tail == NULL)                  if (p_topic_tail == NULL)
895                  {                  {
896                          log_error("tail of topic (aid=%d) is NULL\n", p_article->tid);                          log_error("tail of topic (aid=%d) is NULL", p_article->tid);
897                          return -4;                          return -4;
898                  }                  }
899          }          }
# Line 937  int section_list_append_article(SECTION_ Line 943  int section_list_append_article(SECTION_
943    
944          if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)          if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
945          {          {
946                  log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",                  log_error("section_list_update_article_ontop(sid=%d, aid=%d) error",
947                                    p_section->sid, p_article->aid);                                    p_section->sid, p_article->aid);
948                  return -5;                  return -5;
949          }          }
# Line 953  int section_list_set_article_visible(SEC Line 959  int section_list_set_article_visible(SEC
959    
960          if (p_section == NULL)          if (p_section == NULL)
961          {          {
962                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
963                  return -1;                  return -1;
964          }          }
965    
# Line 965  int section_list_set_article_visible(SEC Line 971  int section_list_set_article_visible(SEC
971    
972          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
973          {          {
974                  log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);                  log_error("Inconsistent section sid %d != article sid %d", p_section->sid, p_article->sid);
975                  return -2;                  return -2;
976          }          }
977    
# Line 980  int section_list_set_article_visible(SEC Line 986  int section_list_set_article_visible(SEC
986    
987                  if (user_article_cnt_inc(p_article->uid, -1) < 0)                  if (user_article_cnt_inc(p_article->uid, -1) < 0)
988                  {                  {
989                          log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);                          log_error("user_article_cnt_inc(uid=%d, -1) error", p_article->uid);
990                  }                  }
991    
992                  if (p_article->tid == 0)                  if (p_article->tid == 0)
# Line 992  int section_list_set_article_visible(SEC Line 998  int section_list_set_article_visible(SEC
998                          {                          {
999                                  if (p_reply->tid != aid)                                  if (p_reply->tid != aid)
1000                                  {                                  {
1001                                          log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid);                                          log_error("Inconsistent tid = %d found in reply %d of topic %d", p_reply->tid, p_reply->aid, aid);
1002                                          continue;                                          continue;
1003                                  }                                  }
1004    
# Line 1004  int section_list_set_article_visible(SEC Line 1010  int section_list_set_article_visible(SEC
1010    
1011                                          if (user_article_cnt_inc(p_reply->uid, -1) < 0)                                          if (user_article_cnt_inc(p_reply->uid, -1) < 0)
1012                                          {                                          {
1013                                                  log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);                                                  log_error("user_article_cnt_inc(uid=%d, -1) error", p_reply->uid);
1014                                          }                                          }
1015                                  }                                  }
1016                          }                          }
# Line 1021  int section_list_set_article_visible(SEC Line 1027  int section_list_set_article_visible(SEC
1027    
1028                  if (user_article_cnt_inc(p_article->uid, 1) < 0)                  if (user_article_cnt_inc(p_article->uid, 1) < 0)
1029                  {                  {
1030                          log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);                          log_error("user_article_cnt_inc(uid=%d, 1) error", p_article->uid);
1031                  }                  }
1032          }          }
1033    
# Line 1031  int section_list_set_article_visible(SEC Line 1037  int section_list_set_article_visible(SEC
1037          return affected_count;          return affected_count;
1038  }  }
1039    
1040    int section_list_set_article_excerption(SECTION_LIST *p_section, int32_t aid, int8_t excerption)
1041    {
1042            ARTICLE *p_article;
1043    
1044            if (p_section == NULL)
1045            {
1046                    log_error("NULL pointer error");
1047                    return -1;
1048            }
1049    
1050            p_article = article_block_find_by_aid(aid);
1051            if (p_article == NULL)
1052            {
1053                    return -1; // Not found
1054            }
1055    
1056            if (p_section->sid != p_article->sid)
1057            {
1058                    log_error("Inconsistent section sid %d != article sid %d", p_section->sid, p_article->sid);
1059                    return -2;
1060            }
1061    
1062            if (p_article->excerption == excerption)
1063            {
1064                    return 0; // Already set
1065            }
1066    
1067            p_article->excerption = excerption;
1068    
1069            return 1;
1070    }
1071    
1072  int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)  int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
1073  {  {
1074          int i;          int i;
1075    
1076          if (p_section == NULL || p_article == NULL)          if (p_section == NULL || p_article == NULL)
1077          {          {
1078                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
1079                  return -1;                  return -1;
1080          }          }
1081    
1082          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
1083          {          {
1084                  log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);                  log_error("Inconsistent section sid %d != article sid %d", p_section->sid, p_article->sid);
1085                  return -2;                  return -2;
1086          }          }
1087    
# Line 1053  int section_list_update_article_ontop(SE Line 1091  int section_list_update_article_ontop(SE
1091                  {                  {
1092                          if (p_section->p_ontop_articles[i]->aid == p_article->aid)                          if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1093                          {                          {
1094                                  log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);                                  log_error("Inconsistent state found: article %d already ontop in section %d", p_article->aid, p_section->sid);
1095                                  return 0;                                  return 0;
1096                          }                          }
1097                          else if (p_section->p_ontop_articles[i]->aid > p_article->aid)                          else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
# Line 1098  int section_list_update_article_ontop(SE Line 1136  int section_list_update_article_ontop(SE
1136                  }                  }
1137                  if (i == p_section->ontop_article_count) // not found                  if (i == p_section->ontop_article_count) // not found
1138                  {                  {
1139                          log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);                          log_error("Inconsistent state found: article %d not ontop in section %d", p_article->aid, p_section->sid);
1140                          return 0;                          return 0;
1141                  }                  }
1142    
# Line 1117  int section_list_page_count_with_ontop(S Line 1155  int section_list_page_count_with_ontop(S
1155    
1156          if (p_section == NULL)          if (p_section == NULL)
1157          {          {
1158                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
1159                  return -1;                  return -1;
1160          }          }
1161    
# Line 1137  int section_list_page_article_count_with Line 1175  int section_list_page_article_count_with
1175  {  {
1176          if (p_section == NULL)          if (p_section == NULL)
1177          {          {
1178                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
1179                  return -1;                  return -1;
1180          }          }
1181    
# Line 1167  ARTICLE *section_list_find_article_with_ Line 1205  ARTICLE *section_list_find_article_with_
1205    
1206          if (p_section == NULL)          if (p_section == NULL)
1207          {          {
1208                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
1209                  return NULL;                  return NULL;
1210          }          }
1211    
# Line 1185  ARTICLE *section_list_find_article_with_ Line 1223  ARTICLE *section_list_find_article_with_
1223          while (left < right)          while (left < right)
1224          {          {
1225                  // get page id no less than mid value of left page id and right page id                  // get page id no less than mid value of left page id and right page id
1226                  mid = (left + right) / 2 + (left + right) % 2;                  mid = left + (right - left + 1) / 2;
1227    
1228                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1229                  {                  {
# Line 1253  int section_list_calculate_page(SECTION_ Line 1291  int section_list_calculate_page(SECTION_
1291    
1292          if (p_section == NULL)          if (p_section == NULL)
1293          {          {
1294                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
1295                  return -1;                  return -1;
1296          }          }
1297    
# Line 1275  int section_list_calculate_page(SECTION_ Line 1313  int section_list_calculate_page(SECTION_
1313    
1314                  if (p_section->sid != p_article->sid)                  if (p_section->sid != p_article->sid)
1315                  {                  {
1316                          log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);                          log_error("section_list_calculate_page() error: section sid %d != start article sid %d", p_section->sid, p_article->sid);
1317                          return -2;                          return -2;
1318                  }                  }
1319    
# Line 1286  int section_list_calculate_page(SECTION_ Line 1324  int section_list_calculate_page(SECTION_
1324                          {                          {
1325                                  return -1;                                  return -1;
1326                          }                          }
1327                          log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n",                          log_error("section_list_calculate_page() aid = %d not found in section sid = %d",
1328                                            start_aid, p_section->sid);                                            start_aid, p_section->sid);
1329                          return -2;                          return -2;
1330                  }                  }
# Line 1335  int section_list_calculate_page(SECTION_ Line 1373  int section_list_calculate_page(SECTION_
1373    
1374                          if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)                          if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)
1375                          {                          {
1376                                  log_error("Count of page exceed limit in section %d\n", p_section->sid);                                  log_error("Count of page exceed limit in section %d", p_section->sid);
1377                                  break;                                  break;
1378                          }                          }
1379                  }                  }
# Line 1389  int article_count_of_topic(int32_t aid) Line 1427  int article_count_of_topic(int32_t aid)
1427          {          {
1428                  if (p_article->tid != 0 && p_article->tid != aid)                  if (p_article->tid != 0 && p_article->tid != aid)
1429                  {                  {
1430                          log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);                          log_error("article_count_of_topic(%d) error: article %d not linked to the topic", aid, p_article->aid);
1431                          break;                          break;
1432                  }                  }
1433    
# Line 1414  int section_list_move_topic(SECTION_LIST Line 1452  int section_list_move_topic(SECTION_LIST
1452    
1453          if (p_section_src == NULL || p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1454          {          {
1455                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
1456                  return -1;                  return -1;
1457          }          }
1458    
1459          if (p_section_src->sid == p_section_dest->sid)          if (p_section_src->sid == p_section_dest->sid)
1460          {          {
1461                  log_error("section_list_move_topic() src and dest section are the same\n");                  log_error("section_list_move_topic() src and dest section are the same");
1462                  return -1;                  return -1;
1463          }          }
1464    
1465          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1466          {          {
1467                  log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);                  log_error("article_block_find_by_aid(aid = %d) error: article not found", aid);
1468                  return -2;                  return -2;
1469          }          }
1470    
1471          if (p_section_src->sid != p_article->sid)          if (p_section_src->sid != p_article->sid)
1472          {          {
1473                  log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",                  log_error("section_list_move_topic() error: src section sid %d != article %d sid %d",
1474                                    p_section_src->sid, p_article->aid, p_article->sid);                                    p_section_src->sid, p_article->aid, p_article->sid);
1475                  return -2;                  return -2;
1476          }          }
1477    
1478          if (p_article->tid != 0)          if (p_article->tid != 0)
1479          {          {
1480                  log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid);                  log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d", aid, p_article->tid);
1481                  return -2;                  return -2;
1482          }          }
1483    
# Line 1448  int section_list_move_topic(SECTION_LIST Line 1486  int section_list_move_topic(SECTION_LIST
1486          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1487          if (move_article_count <= 0)          if (move_article_count <= 0)
1488          {          {
1489                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0", aid);
1490                  return -2;                  return -2;
1491          }          }
1492    
1493          if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)          if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
1494          {          {
1495                  log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n",                  log_error("section_list_move_topic() error: article_count %d reach limit in section %d",
1496                                    p_section_dest->article_count + move_article_count, p_section_dest->sid);                                    p_section_dest->article_count + move_article_count, p_section_dest->sid);
1497                  return -3;                  return -3;
1498          }          }
# Line 1467  int section_list_move_topic(SECTION_LIST Line 1505  int section_list_move_topic(SECTION_LIST
1505          {          {
1506                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1507                  {                  {
1508                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d",
1509                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1510                          p_article = p_article->p_topic_next;                          p_article = p_article->p_topic_next;
1511                          continue;                          continue;
# Line 1482  int section_list_move_topic(SECTION_LIST Line 1520  int section_list_move_topic(SECTION_LIST
1520                  {                  {
1521                          p_section_src->p_article_tail = p_article->p_prior;                          p_section_src->p_article_tail = p_article->p_prior;
1522                  }                  }
1523                  if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article                  if (p_section_src->p_article_head == p_article) // Single element list
1524                  {                  {
1525                          p_section_src->p_article_head = NULL;                          p_section_src->p_article_head = NULL;
1526                          p_section_src->p_article_tail = NULL;                          p_section_src->p_article_tail = NULL;
1527                  }                  }
1528                    else
1529                  p_article->p_prior->p_next = p_article->p_next;                  {
1530                  p_article->p_next->p_prior = p_article->p_prior;                          // Only update neighbor pointers if list is not empty after removal
1531                            p_article->p_prior->p_next = p_article->p_next;
1532                            p_article->p_next->p_prior = p_article->p_prior;
1533                    }
1534    
1535                  // Update sid of article                  // Update sid of article
1536                  p_article->sid = p_section_dest->sid;                  p_article->sid = p_section_dest->sid;
1537    
1538                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1539                  {                  {
1540                          log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);                          log_error("section_list_move_topic() warning: article %d already in section %d", p_article->aid, p_section_dest->sid);
1541                          p_article = p_article->p_topic_next;                          p_article = p_article->p_topic_next;
1542                          continue;                          continue;
1543                  }                  }
# Line 1566  int section_list_move_topic(SECTION_LIST Line 1607  int section_list_move_topic(SECTION_LIST
1607                          // Re-calculate pages of desc section                          // Re-calculate pages of desc section
1608                          if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)                          if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1609                          {                          {
1610                                  log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",                                  log_error("section_list_calculate_page(dest section = %d, aid = %d) error",
1611                                                    p_section_dest->sid, first_inserted_aid_dest);                                                    p_section_dest->sid, first_inserted_aid_dest);
1612                          }                          }
1613    
# Line 1576  int section_list_move_topic(SECTION_LIST Line 1617  int section_list_move_topic(SECTION_LIST
1617    
1618          if (p_section_dest->article_count - dest_article_count_old != move_article_count)          if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1619          {          {
1620                  log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",                  log_error("section_list_move_topic() warning: count of moved articles %d != %d",
1621                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1622          }          }
1623    
1624          // Re-calculate pages of src section          // Re-calculate pages of src section
1625          if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)          if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1626          {          {
1627                  log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",                  log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d",
1628                                    p_section_src->sid, last_unaffected_aid_src, aid);                                    p_section_src->sid, last_unaffected_aid_src, aid);
1629          }          }
1630    
# Line 1592  int section_list_move_topic(SECTION_LIST Line 1633  int section_list_move_topic(SECTION_LIST
1633                  // Re-calculate pages of desc section                  // Re-calculate pages of desc section
1634                  if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)                  if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1635                  {                  {
1636                          log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",                          log_error("section_list_calculate_page(dest section = %d, aid = %d) error",
1637                                            p_section_dest->sid, first_inserted_aid_dest);                                            p_section_dest->sid, first_inserted_aid_dest);
1638                  }                  }
1639          }          }
# Line 1606  int get_section_index(SECTION_LIST *p_se Line 1647  int get_section_index(SECTION_LIST *p_se
1647    
1648          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
1649          {          {
1650                  log_error("get_section_index() error: uninitialized\n");                  log_error("get_section_index() error: uninitialized");
1651                  return -1;                  return -1;
1652          }          }
1653    
# Line 1619  int get_section_index(SECTION_LIST *p_se Line 1660  int get_section_index(SECTION_LIST *p_se
1660                  index = (int)(p_section - p_section_list_pool->sections);                  index = (int)(p_section - p_section_list_pool->sections);
1661                  if (index < 0 || index >= BBS_max_section)                  if (index < 0 || index >= BBS_max_section)
1662                  {                  {
1663                          log_error("get_section_index(%d) error: index out of range\n", index);                          log_error("get_section_index(%d) error: index out of range", index);
1664                          return -2;                          return -2;
1665                  }                  }
1666          }          }
# Line 1631  int get_section_info(SECTION_LIST *p_sec Line 1672  int get_section_info(SECTION_LIST *p_sec
1672  {  {
1673          if (p_section == NULL)          if (p_section == NULL)
1674          {          {
1675                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
1676                  return -1;                  return -1;
1677          }          }
1678    
1679          if (section_list_rd_lock(p_section) < 0)          if (section_list_rd_lock(p_section) < 0)
1680          {          {
1681                  log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);                  log_error("section_list_rd_lock(sid=%d) error", p_section->sid);
1682                  return -2;                  return -2;
1683          }          }
1684    
# Line 1657  int get_section_info(SECTION_LIST *p_sec Line 1698  int get_section_info(SECTION_LIST *p_sec
1698          // release lock of section          // release lock of section
1699          if (section_list_rd_unlock(p_section) < 0)          if (section_list_rd_unlock(p_section) < 0)
1700          {          {
1701                  log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);                  log_error("section_list_rd_unlock(sid=%d) error", p_section->sid);
1702                  return -2;                  return -2;
1703          }          }
1704    
# Line 1708  int section_list_try_rd_lock(SECTION_LIS Line 1749  int section_list_try_rd_lock(SECTION_LIS
1749          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1750          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1751          {          {
1752                  log_error("semop(index = %d, lock read) error %d\n", index, errno);                  log_error("semop(index = %d, lock read) error %d", index, errno);
1753          }          }
1754  #else  #else
1755          if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1)          if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1)
1756          {          {
1757                  if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)                  if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1758                  {                  {
1759                          log_error("sem_timedwait(sem[%d]) error %d\n", index, errno);                          log_error("sem_timedwait(sem[%d]) error %d", index, errno);
1760                  }                  }
1761                  return -1;                  return -1;
1762          }          }
# Line 1726  int section_list_try_rd_lock(SECTION_LIS Line 1767  int section_list_try_rd_lock(SECTION_LIS
1767                  {                  {
1768                          if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)                          if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1769                          {                          {
1770                                  log_error("sem_timedwait(sem[%d]) error %d\n", BBS_max_section, errno);                                  log_error("sem_timedwait(sem[%d]) error %d", BBS_max_section, errno);
1771                          }                          }
1772                          // release previously acquired lock                          // release previously acquired lock
1773                          if (sem_post(&(p_section_list_pool->sem[index])) == -1)                          if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1774                          {                          {
1775                                  log_error("sem_post(sem[%d]) error %d\n", index, errno);                                  log_error("sem_post(sem[%d]) error %d", index, errno);
1776                          }                          }
1777                          return -1;                          return -1;
1778                  }                  }
# Line 1757  int section_list_try_rd_lock(SECTION_LIS Line 1798  int section_list_try_rd_lock(SECTION_LIS
1798                  // release lock on "all section"                  // release lock on "all section"
1799                  if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1)                  if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1)
1800                  {                  {
1801                          log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno);                          log_error("sem_post(sem[%d]) error %d", BBS_max_section, errno);
1802                          ret = -1;                          ret = -1;
1803                  }                  }
1804          }          }
1805    
1806          if (sem_post(&(p_section_list_pool->sem[index])) == -1)          if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1807          {          {
1808                  log_error("sem_post(sem[%d]) error %d\n", index, errno);                  log_error("sem_post(sem[%d]) error %d", index, errno);
1809                  return -1;                  return -1;
1810          }          }
1811  #endif  #endif
# Line 1806  int section_list_try_rw_lock(SECTION_LIS Line 1847  int section_list_try_rw_lock(SECTION_LIS
1847          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1848          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1849          {          {
1850                  log_error("semop(index = %d, lock write) error %d\n", index, errno);                  log_error("semop(index = %d, lock write) error %d", index, errno);
1851          }          }
1852  #else  #else
1853          if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1)          if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1)
1854          {          {
1855                  if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)                  if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1856                  {                  {
1857                          log_error("sem_timedwait(sem[%d]) error %d\n", index, errno);                          log_error("sem_timedwait(sem[%d]) error %d", index, errno);
1858                  }                  }
1859                  return -1;                  return -1;
1860          }          }
# Line 1830  int section_list_try_rw_lock(SECTION_LIS Line 1871  int section_list_try_rw_lock(SECTION_LIS
1871    
1872          if (sem_post(&(p_section_list_pool->sem[index])) == -1)          if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1873          {          {
1874                  log_error("sem_post(sem[%d]) error %d\n", index, errno);                  log_error("sem_post(sem[%d]) error %d", index, errno);
1875                  return -1;                  return -1;
1876          }          }
1877  #endif  #endif
# Line 1867  int section_list_rd_unlock(SECTION_LIST Line 1908  int section_list_rd_unlock(SECTION_LIST
1908          ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));          ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1909          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1910          {          {
1911                  log_error("semop(index = %d, unlock read) error %d\n", index, errno);                  log_error("semop(index = %d, unlock read) error %d", index, errno);
1912          }          }
1913  #else  #else
1914          if (sem_wait(&(p_section_list_pool->sem[index])) == -1)          if (sem_wait(&(p_section_list_pool->sem[index])) == -1)
1915          {          {
1916                  if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)                  if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1917                  {                  {
1918                          log_error("sem_wait(sem[%d]) error %d\n", index, errno);                          log_error("sem_wait(sem[%d]) error %d", index, errno);
1919                  }                  }
1920                  return -1;                  return -1;
1921          }          }
# Line 1885  int section_list_rd_unlock(SECTION_LIST Line 1926  int section_list_rd_unlock(SECTION_LIST
1926                  {                  {
1927                          if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)                          if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1928                          {                          {
1929                                  log_error("sem_wait(sem[%d]) error %d\n", BBS_max_section, errno);                                  log_error("sem_wait(sem[%d]) error %d", BBS_max_section, errno);
1930                          }                          }
1931                          // release previously acquired lock                          // release previously acquired lock
1932                          if (sem_post(&(p_section_list_pool->sem[index])) == -1)                          if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1933                          {                          {
1934                                  log_error("sem_post(sem[%d]) error %d\n", index, errno);                                  log_error("sem_post(sem[%d]) error %d", index, errno);
1935                          }                          }
1936                          return -1;                          return -1;
1937                  }                  }
# Line 1902  int section_list_rd_unlock(SECTION_LIST Line 1943  int section_list_rd_unlock(SECTION_LIST
1943          }          }
1944          else          else
1945          {          {
1946                  log_error("read_lock_count[%d] already 0\n", index);                  log_error("read_lock_count[%d] already 0", index);
1947          }          }
1948    
1949          if (index != BBS_max_section && p_section_list_pool->read_lock_count[BBS_max_section] > 0)          if (index != BBS_max_section && p_section_list_pool->read_lock_count[BBS_max_section] > 0)
# Line 1911  int section_list_rd_unlock(SECTION_LIST Line 1952  int section_list_rd_unlock(SECTION_LIST
1952          }          }
1953          else          else
1954          {          {
1955                  log_error("read_lock_count[%d] already 0\n", BBS_max_section);                  log_error("read_lock_count[%d] already 0", BBS_max_section);
1956          }          }
1957    
1958          if (index != BBS_max_section)          if (index != BBS_max_section)
# Line 1919  int section_list_rd_unlock(SECTION_LIST Line 1960  int section_list_rd_unlock(SECTION_LIST
1960                  // release lock on "all section"                  // release lock on "all section"
1961                  if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1)                  if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1)
1962                  {                  {
1963                          log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno);                          log_error("sem_post(sem[%d]) error %d", BBS_max_section, errno);
1964                          ret = -1;                          ret = -1;
1965                  }                  }
1966          }          }
1967    
1968          if (sem_post(&(p_section_list_pool->sem[index])) == -1)          if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1969          {          {
1970                  log_error("sem_post(sem[%d]) error %d\n", index, errno);                  log_error("sem_post(sem[%d]) error %d", index, errno);
1971                  return -1;                  return -1;
1972          }          }
1973  #endif  #endif
# Line 1956  int section_list_rw_unlock(SECTION_LIST Line 1997  int section_list_rw_unlock(SECTION_LIST
1997          ret = semop(p_section_list_pool->semid, sops, 1);          ret = semop(p_section_list_pool->semid, sops, 1);
1998          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1999          {          {
2000                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);                  log_error("semop(index = %d, unlock write) error %d", index, errno);
2001          }          }
2002  #else  #else
2003          if (sem_wait(&(p_section_list_pool->sem[index])) == -1)          if (sem_wait(&(p_section_list_pool->sem[index])) == -1)
2004          {          {
2005                  if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)                  if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
2006                  {                  {
2007                          log_error("sem_wait(sem[%d]) error %d\n", index, errno);                          log_error("sem_wait(sem[%d]) error %d", index, errno);
2008                  }                  }
2009                  return -1;                  return -1;
2010          }          }
# Line 1974  int section_list_rw_unlock(SECTION_LIST Line 2015  int section_list_rw_unlock(SECTION_LIST
2015          }          }
2016          else          else
2017          {          {
2018                  log_error("write_lock_count[%d] already 0\n", index);                  log_error("write_lock_count[%d] already 0", index);
2019          }          }
2020    
2021          if (sem_post(&(p_section_list_pool->sem[index])) == -1)          if (sem_post(&(p_section_list_pool->sem[index])) == -1)
2022          {          {
2023                  log_error("sem_post(sem[%d]) error %d\n", index, errno);                  log_error("sem_post(sem[%d]) error %d", index, errno);
2024                  return -1;                  return -1;
2025          }          }
2026  #endif  #endif
# Line 2012  int section_list_rd_lock(SECTION_LIST *p Line 2053  int section_list_rd_lock(SECTION_LIST *p
2053                          timer++;                          timer++;
2054                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
2055                          {                          {
2056                                  log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);                                  log_error("section_list_try_rd_lock() tried %d times on section %d", timer, sid);
2057                                  if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT)                                  if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT)
2058                                  {                                  {
2059                                          log_error("Unable to acquire rd_lock for %d seconds\n", time(NULL) - tm_first_failure);                                          log_error("Unable to acquire rd_lock for %d seconds", time(NULL) - tm_first_failure);
2060  #ifndef HAVE_SYSTEM_V  #ifndef HAVE_SYSTEM_V
2061                                          section_list_reset_lock(p_section);                                          section_list_reset_lock(p_section);
2062                                          log_error("Reset POSIX semaphore to resolve dead lock\n");                                          log_error("Reset POSIX semaphore to resolve dead lock");
2063  #endif  #endif
2064                                          break;                                          break;
2065                                  }                                  }
# Line 2027  int section_list_rd_lock(SECTION_LIST *p Line 2068  int section_list_rd_lock(SECTION_LIST *p
2068                  }                  }
2069                  else // failed                  else // failed
2070                  {                  {
2071                          log_error("section_list_try_rd_lock() failed on section %d\n", sid);                          log_error("section_list_try_rd_lock() failed on section %d", sid);
2072                          break;                          break;
2073                  }                  }
2074          }          }
# Line 2060  int section_list_rw_lock(SECTION_LIST *p Line 2101  int section_list_rw_lock(SECTION_LIST *p
2101                          timer++;                          timer++;
2102                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
2103                          {                          {
2104                                  log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);                                  log_error("section_list_try_rw_lock() tried %d times on section %d", timer, sid);
2105                                  if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT)                                  if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT)
2106                                  {                                  {
2107                                          log_error("Unable to acquire rw_lock for %d seconds\n", time(NULL) - tm_first_failure);                                          log_error("Unable to acquire rw_lock for %d seconds", time(NULL) - tm_first_failure);
2108  #ifndef HAVE_SYSTEM_V  #ifndef HAVE_SYSTEM_V
2109                                          section_list_reset_lock(p_section);                                          section_list_reset_lock(p_section);
2110                                          log_error("Reset POSIX semaphore to resolve dead lock\n");                                          log_error("Reset POSIX semaphore to resolve dead lock");
2111  #endif  #endif
2112                                          break;                                          break;
2113                                  }                                  }
# Line 2075  int section_list_rw_lock(SECTION_LIST *p Line 2116  int section_list_rw_lock(SECTION_LIST *p
2116                  }                  }
2117                  else // failed                  else // failed
2118                  {                  {
2119                          log_error("section_list_try_rw_lock() failed on section %d\n", sid);                          log_error("section_list_try_rw_lock() failed on section %d", sid);
2120                          break;                          break;
2121                  }                  }
2122          }          }
# Line 2090  int section_list_reset_lock(SECTION_LIST Line 2131  int section_list_reset_lock(SECTION_LIST
2131    
2132          if (p_section == NULL)          if (p_section == NULL)
2133          {          {
2134                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
2135                  return -1;                  return -1;
2136          }          }
2137    
# Line 2102  int section_list_reset_lock(SECTION_LIST Line 2143  int section_list_reset_lock(SECTION_LIST
2143    
2144          if (sem_destroy(&(p_section_list_pool->sem[index])) == -1)          if (sem_destroy(&(p_section_list_pool->sem[index])) == -1)
2145          {          {
2146                  log_error("sem_destroy(sem[%d]) error (%d)\n", index, errno);                  log_error("sem_destroy(sem[%d]) error (%d)", index, errno);
2147          }          }
2148    
2149          p_section_list_pool->read_lock_count[index] = 0;          p_section_list_pool->read_lock_count[index] = 0;
# Line 2110  int section_list_reset_lock(SECTION_LIST Line 2151  int section_list_reset_lock(SECTION_LIST
2151    
2152          if (sem_init(&(p_section_list_pool->sem[index]), 1, 1) == -1)          if (sem_init(&(p_section_list_pool->sem[index]), 1, 1) == -1)
2153          {          {
2154                  log_error("sem_init(sem[%d]) error (%d)\n", index, errno);                  log_error("sem_init(sem[%d]) error (%d)", index, errno);
2155          }          }
2156    
2157          if (index != BBS_max_section)          if (index != BBS_max_section)
2158          {          {
2159                  if (sem_destroy(&(p_section_list_pool->sem[BBS_max_section])) == -1)                  if (sem_destroy(&(p_section_list_pool->sem[BBS_max_section])) == -1)
2160                  {                  {
2161                          log_error("sem_destroy(sem[%d]) error (%d)\n", BBS_max_section, errno);                          log_error("sem_destroy(sem[%d]) error (%d)", BBS_max_section, errno);
2162                  }                  }
2163    
2164                  p_section_list_pool->read_lock_count[BBS_max_section] = 0;                  p_section_list_pool->read_lock_count[BBS_max_section] = 0;
# Line 2125  int section_list_reset_lock(SECTION_LIST Line 2166  int section_list_reset_lock(SECTION_LIST
2166    
2167                  if (sem_init(&(p_section_list_pool->sem[BBS_max_section]), 1, 1) == -1)                  if (sem_init(&(p_section_list_pool->sem[BBS_max_section]), 1, 1) == -1)
2168                  {                  {
2169                          log_error("sem_init(sem[%d]) error (%d)\n", BBS_max_section, errno);                          log_error("sem_init(sem[%d]) error (%d)", BBS_max_section, errno);
2170                  }                  }
2171          }          }
2172    


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

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