| 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 |
|
|
| 13 |
#include "log.h" |
#include "log.h" |
| 14 |
#include "section_list.h" |
#include "section_list.h" |
| 25 |
#include <sys/sem.h> |
#include <sys/sem.h> |
| 26 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 27 |
|
|
| 28 |
#ifdef _SEM_SEMUN_UNDEFINED |
#if defined(_SEM_SEMUN_UNDEFINED) || defined(__MSYS__) || defined(__MINGW32__) |
| 29 |
union semun |
union semun |
| 30 |
{ |
{ |
| 31 |
int val; /* Value for SETVAL */ |
int val; /* Value for SETVAL */ |
| 34 |
struct seminfo *__buf; /* Buffer for IPC_INFO |
struct seminfo *__buf; /* Buffer for IPC_INFO |
| 35 |
(Linux-specific) */ |
(Linux-specific) */ |
| 36 |
}; |
}; |
| 37 |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
#endif // #if defined(_SEM_SEMUN_UNDEFINED) |
| 38 |
|
|
| 39 |
#define SECTION_TRY_LOCK_WAIT_TIME 1 // second |
enum _section_list_constant_t |
| 40 |
#define SECTION_TRY_LOCK_TIMES 10 |
{ |
| 41 |
|
SECTION_TRY_LOCK_WAIT_TIME = 1, // second |
| 42 |
|
SECTION_TRY_LOCK_TIMES = 10, |
| 43 |
|
|
| 44 |
#define ARTICLE_BLOCK_PER_SHM 1000 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
ARTICLE_BLOCK_PER_SHM = 1000, // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
| 45 |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
| 46 |
#define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT) |
ARTICLE_BLOCK_PER_POOL = (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT), |
| 47 |
|
|
| 48 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections |
CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections |
| 49 |
|
|
| 50 |
#define SID_STR_LEN 5 // 32-bit + NULL |
SID_STR_LEN = 5, // 32-bit + NULL |
| 51 |
|
}; |
| 52 |
|
|
| 53 |
struct article_block_t |
struct article_block_t |
| 54 |
{ |
{ |
| 55 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[BBS_article_count_per_block]; |
| 56 |
int article_count; |
int article_count; |
| 57 |
struct article_block_t *p_next_block; |
struct article_block_t *p_next_block; |
| 58 |
}; |
}; |
| 215 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 216 |
} |
} |
| 217 |
|
|
| 218 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 219 |
{ |
{ |
| 220 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 221 |
} |
} |
| 240 |
shmid = (p_article_block_pool->shm_pool + i)->shmid; |
shmid = (p_article_block_pool->shm_pool + i)->shmid; |
| 241 |
|
|
| 242 |
// Remap shared memory in read-only mode |
// Remap shared memory in read-only mode |
| 243 |
|
#if defined(__MSYS__) || defined(__MINGW32__) |
| 244 |
|
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
| 245 |
|
{ |
| 246 |
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
| 247 |
|
return -2; |
| 248 |
|
} |
| 249 |
|
p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY); |
| 250 |
|
#else |
| 251 |
p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP); |
p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP); |
| 252 |
|
#endif |
| 253 |
if (p_shm == (void *)-1) |
if (p_shm == (void *)-1) |
| 254 |
{ |
{ |
| 255 |
log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno); |
| 351 |
} |
} |
| 352 |
|
|
| 353 |
left = 0; |
left = 0; |
| 354 |
right = p_article_block_pool->block_count; |
right = p_article_block_pool->block_count - 1; |
| 355 |
|
|
| 356 |
// aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ] |
// aid in the range [ head aid of blocks[left], tail aid of blocks[right] ] |
| 357 |
while (left < right - 1) |
while (left < right) |
| 358 |
{ |
{ |
| 359 |
// 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 |
| 360 |
mid = (left + right) / 2 + (right - left) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
|
|
|
|
if (mid >= p_article_block_pool->block_count) |
|
|
{ |
|
|
log_error("block(mid = %d) is out of boundary\n", mid); |
|
|
return NULL; |
|
|
} |
|
| 361 |
|
|
| 362 |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
| 363 |
{ |
{ |
| 364 |
right = mid; |
right = mid - 1; |
| 365 |
} |
} |
| 366 |
else |
else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid) |
| 367 |
{ |
{ |
| 368 |
left = mid; |
left = mid; |
| 369 |
} |
} |
| 407 |
return NULL; |
return NULL; |
| 408 |
} |
} |
| 409 |
|
|
| 410 |
if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count) |
if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count) |
| 411 |
{ |
{ |
| 412 |
log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count); |
log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count); |
| 413 |
return NULL; |
return NULL; |
| 414 |
} |
} |
| 415 |
|
|
| 416 |
p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK]; |
p_block = p_article_block_pool->p_block[index / BBS_article_count_per_block]; |
| 417 |
|
|
| 418 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % BBS_article_count_per_block >= p_block->article_count) |
| 419 |
{ |
{ |
| 420 |
log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count); |
log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count); |
| 421 |
return NULL; |
return NULL; |
| 422 |
} |
} |
| 423 |
|
|
| 424 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % BBS_article_count_per_block)); |
| 425 |
} |
} |
| 426 |
|
|
| 427 |
extern int section_list_init(const char *filename) |
extern int section_list_init(const char *filename) |
| 539 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 540 |
} |
} |
| 541 |
|
|
| 542 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 543 |
{ |
{ |
| 544 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 545 |
} |
} |
| 561 |
shmid = p_section_list_pool->shmid; |
shmid = p_section_list_pool->shmid; |
| 562 |
|
|
| 563 |
// Remap shared memory in read-only mode |
// Remap shared memory in read-only mode |
| 564 |
|
#if defined(__MSYS__) || defined(__MINGW32__) |
| 565 |
|
if (shmdt(p_section_list_pool) == -1) |
| 566 |
|
{ |
| 567 |
|
log_error("shmdt(section_list_pool) error (%d)\n", errno); |
| 568 |
|
return -1; |
| 569 |
|
} |
| 570 |
|
p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY); |
| 571 |
|
#else |
| 572 |
p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP); |
p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP); |
| 573 |
|
#endif |
| 574 |
if (p_shm == (void *)-1) |
if (p_shm == (void *)-1) |
| 575 |
{ |
{ |
| 576 |
log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno); |
| 792 |
} |
} |
| 793 |
|
|
| 794 |
if (p_article_block_pool->block_count == 0 || |
if (p_article_block_pool->block_count == 0 || |
| 795 |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK) |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= BBS_article_count_per_block) |
| 796 |
{ |
{ |
| 797 |
if ((p_block = pop_free_article_block()) == NULL) |
if ((p_block = pop_free_article_block()) == NULL) |
| 798 |
{ |
{ |
| 802 |
|
|
| 803 |
if (p_article_block_pool->block_count > 0) |
if (p_article_block_pool->block_count > 0) |
| 804 |
{ |
{ |
| 805 |
last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid; |
last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[BBS_article_count_per_block - 1].aid; |
| 806 |
} |
} |
| 807 |
|
|
| 808 |
p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block; |
p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block; |
| 881 |
p_section->p_article_tail = p_article; |
p_section->p_article_tail = p_article; |
| 882 |
|
|
| 883 |
// Update page |
// Update page |
| 884 |
if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) || |
if ((p_article->visible && p_section->last_page_visible_article_count == BBS_article_limit_per_page) || |
| 885 |
p_section->article_count == 1) |
p_section->page_count == 0) |
| 886 |
{ |
{ |
| 887 |
p_section->p_page_first_article[p_section->page_count] = p_article; |
p_section->p_page_first_article[p_section->page_count] = p_article; |
| 888 |
p_section->page_count++; |
p_section->page_count++; |
| 1080 |
return -1; |
return -1; |
| 1081 |
} |
} |
| 1082 |
|
|
| 1083 |
page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) + |
page_count = p_section->page_count - 1 + |
| 1084 |
(p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page + |
(p_section->last_page_visible_article_count + p_section->ontop_article_count + BBS_article_limit_per_page - 1) / |
| 1085 |
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1); |
BBS_article_limit_per_page; |
| 1086 |
|
|
| 1087 |
|
if (page_count < 0) |
| 1088 |
|
{ |
| 1089 |
|
page_count = 0; |
| 1090 |
|
} |
| 1091 |
|
|
| 1092 |
return page_count; |
return page_count; |
| 1093 |
} |
} |
| 1106 |
} |
} |
| 1107 |
else // if (page_id >= p_section->page_count - 1) |
else // if (page_id >= p_section->page_count - 1) |
| 1108 |
{ |
{ |
| 1109 |
return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count - |
return MIN(MAX(0, |
| 1110 |
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))); |
(p_section->last_page_visible_article_count + p_section->ontop_article_count - |
| 1111 |
|
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))), |
| 1112 |
|
BBS_article_limit_per_page); |
| 1113 |
} |
} |
| 1114 |
} |
} |
| 1115 |
|
|
| 1138 |
} |
} |
| 1139 |
|
|
| 1140 |
left = 0; |
left = 0; |
| 1141 |
right = p_section->page_count; |
right = p_section->page_count - 1; |
| 1142 |
|
|
| 1143 |
// aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ] |
// aid in the range [ head aid of pages[left], tail aid of pages[right] ] |
| 1144 |
while (left < right - 1) |
while (left < right) |
| 1145 |
{ |
{ |
| 1146 |
// 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 |
| 1147 |
mid = (left + right) / 2 + (right - left) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
|
|
|
|
if (mid >= p_section->page_count) |
|
|
{ |
|
|
log_error("page id (mid = %d) is out of boundary\n", mid); |
|
|
return NULL; |
|
|
} |
|
| 1148 |
|
|
| 1149 |
if (aid < p_section->p_page_first_article[mid]->aid) |
if (aid < p_section->p_page_first_article[mid]->aid) |
| 1150 |
{ |
{ |
| 1151 |
right = mid; |
right = mid - 1; |
| 1152 |
} |
} |
| 1153 |
else |
else // if (aid < p_section->p_page_first_article[mid]->aid) |
| 1154 |
{ |
{ |
| 1155 |
left = mid; |
left = mid; |
| 1156 |
} |
} |
| 1301 |
} while (p_article != p_section->p_article_head); |
} while (p_article != p_section->p_article_head); |
| 1302 |
|
|
| 1303 |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
| 1304 |
p_section->last_page_visible_article_count = visible_article_count; |
p_section->last_page_visible_article_count = (visible_article_count > 0 |
| 1305 |
|
? visible_article_count |
| 1306 |
|
: (page > 0 ? BBS_article_limit_per_page : 0)); |
| 1307 |
|
|
| 1308 |
return 0; |
return 0; |
| 1309 |
} |
} |
| 1325 |
return -1; |
return -1; |
| 1326 |
} |
} |
| 1327 |
|
|
| 1328 |
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block + |
| 1329 |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count; |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count; |
| 1330 |
|
|
| 1331 |
return ret; |
return ret; |
| 1586 |
return index; |
return index; |
| 1587 |
} |
} |
| 1588 |
|
|
| 1589 |
|
int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list) |
| 1590 |
|
{ |
| 1591 |
|
if (p_section == NULL) |
| 1592 |
|
{ |
| 1593 |
|
log_error("NULL pointer error\n"); |
| 1594 |
|
return -1; |
| 1595 |
|
} |
| 1596 |
|
|
| 1597 |
|
if (section_list_rd_lock(p_section) < 0) |
| 1598 |
|
{ |
| 1599 |
|
log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid); |
| 1600 |
|
return -2; |
| 1601 |
|
} |
| 1602 |
|
|
| 1603 |
|
if (sname != NULL) |
| 1604 |
|
{ |
| 1605 |
|
memcpy(sname, p_section->sname, sizeof(p_section->sname)); |
| 1606 |
|
} |
| 1607 |
|
if (stitle != NULL) |
| 1608 |
|
{ |
| 1609 |
|
memcpy(stitle, p_section->stitle, sizeof(p_section->stitle)); |
| 1610 |
|
} |
| 1611 |
|
if (master_list != NULL) |
| 1612 |
|
{ |
| 1613 |
|
memcpy(master_list, p_section->master_list, sizeof(p_section->master_list)); |
| 1614 |
|
} |
| 1615 |
|
|
| 1616 |
|
// release lock of section |
| 1617 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1618 |
|
{ |
| 1619 |
|
log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid); |
| 1620 |
|
return -2; |
| 1621 |
|
} |
| 1622 |
|
|
| 1623 |
|
return 0; |
| 1624 |
|
} |
| 1625 |
|
|
| 1626 |
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1627 |
{ |
{ |
| 1628 |
int index; |
int index; |
| 1629 |
struct sembuf sops[4]; |
struct sembuf sops[4]; |
| 1630 |
|
#if !defined(__MSYS__) && !defined(__MINGW32__) |
| 1631 |
struct timespec timeout; |
struct timespec timeout; |
| 1632 |
|
#endif |
| 1633 |
int ret; |
int ret; |
| 1634 |
|
|
| 1635 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1660 |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1661 |
} |
} |
| 1662 |
|
|
| 1663 |
|
#if defined(__MSYS__) || defined(__MINGW32__) |
| 1664 |
|
ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4)); |
| 1665 |
|
#else |
| 1666 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1667 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1668 |
|
|
| 1669 |
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); |
| 1670 |
|
#endif |
| 1671 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1672 |
{ |
{ |
| 1673 |
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
log_error("semop(index = %d, lock read) error %d\n", index, errno); |
| 1674 |
} |
} |
| 1675 |
|
|
| 1676 |
return ret; |
return ret; |
| 1680 |
{ |
{ |
| 1681 |
int index; |
int index; |
| 1682 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
| 1683 |
|
#if !defined(__MSYS__) && !defined(__MINGW32__) |
| 1684 |
struct timespec timeout; |
struct timespec timeout; |
| 1685 |
|
#endif |
| 1686 |
int ret; |
int ret; |
| 1687 |
|
|
| 1688 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1703 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1704 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 1705 |
|
|
| 1706 |
|
#if defined(__MSYS__) || defined(__MINGW32__) |
| 1707 |
|
ret = semop(p_section_list_pool->semid, sops, 3); |
| 1708 |
|
#else |
| 1709 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1710 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1711 |
|
|
| 1712 |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1713 |
|
#endif |
| 1714 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1715 |
{ |
{ |
| 1716 |
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
log_error("semop(index = %d, lock write) error %d\n", index, errno); |
| 1717 |
} |
} |
| 1718 |
|
|
| 1719 |
return ret; |
return ret; |