| 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 |
} |
} |
| 398 |
return NULL; |
return NULL; |
| 399 |
} |
} |
| 400 |
|
|
| 401 |
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) |
| 402 |
{ |
{ |
| 403 |
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); |
| 404 |
return NULL; |
return NULL; |
| 405 |
} |
} |
| 406 |
|
|
| 407 |
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]; |
| 408 |
|
|
| 409 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % BBS_article_count_per_block >= p_block->article_count) |
| 410 |
{ |
{ |
| 411 |
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); |
| 412 |
return NULL; |
return NULL; |
| 413 |
} |
} |
| 414 |
|
|
| 415 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % BBS_article_count_per_block)); |
| 416 |
} |
} |
| 417 |
|
|
| 418 |
extern int section_list_init(const char *filename) |
extern int section_list_init(const char *filename) |
| 530 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 531 |
} |
} |
| 532 |
|
|
| 533 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 534 |
{ |
{ |
| 535 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 536 |
} |
} |
| 774 |
} |
} |
| 775 |
|
|
| 776 |
if (p_article_block_pool->block_count == 0 || |
if (p_article_block_pool->block_count == 0 || |
| 777 |
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) |
| 778 |
{ |
{ |
| 779 |
if ((p_block = pop_free_article_block()) == NULL) |
if ((p_block = pop_free_article_block()) == NULL) |
| 780 |
{ |
{ |
| 784 |
|
|
| 785 |
if (p_article_block_pool->block_count > 0) |
if (p_article_block_pool->block_count > 0) |
| 786 |
{ |
{ |
| 787 |
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; |
| 788 |
} |
} |
| 789 |
|
|
| 790 |
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; |
| 863 |
p_section->p_article_tail = p_article; |
p_section->p_article_tail = p_article; |
| 864 |
|
|
| 865 |
// Update page |
// Update page |
| 866 |
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) || |
| 867 |
p_section->article_count == 1) |
p_section->page_count == 0) |
| 868 |
{ |
{ |
| 869 |
p_section->p_page_first_article[p_section->page_count] = p_article; |
p_section->p_page_first_article[p_section->page_count] = p_article; |
| 870 |
p_section->page_count++; |
p_section->page_count++; |
| 1062 |
return -1; |
return -1; |
| 1063 |
} |
} |
| 1064 |
|
|
| 1065 |
page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) + |
page_count = p_section->page_count - 1 + |
| 1066 |
(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) / |
| 1067 |
((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; |
| 1068 |
|
|
| 1069 |
|
if (page_count < 0) |
| 1070 |
|
{ |
| 1071 |
|
page_count = 0; |
| 1072 |
|
} |
| 1073 |
|
|
| 1074 |
return page_count; |
return page_count; |
| 1075 |
} |
} |
| 1283 |
} while (p_article != p_section->p_article_head); |
} while (p_article != p_section->p_article_head); |
| 1284 |
|
|
| 1285 |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
| 1286 |
p_section->last_page_visible_article_count = visible_article_count; |
p_section->last_page_visible_article_count = (visible_article_count > 0 |
| 1287 |
|
? visible_article_count |
| 1288 |
|
: (page > 0 ? BBS_article_limit_per_page : 0)); |
| 1289 |
|
|
| 1290 |
return 0; |
return 0; |
| 1291 |
} |
} |
| 1307 |
return -1; |
return -1; |
| 1308 |
} |
} |
| 1309 |
|
|
| 1310 |
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block + |
| 1311 |
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; |
| 1312 |
|
|
| 1313 |
return ret; |
return ret; |
| 1609 |
{ |
{ |
| 1610 |
int index; |
int index; |
| 1611 |
struct sembuf sops[4]; |
struct sembuf sops[4]; |
| 1612 |
|
#if !defined(__MSYS__) && !defined(__MINGW32__) |
| 1613 |
struct timespec timeout; |
struct timespec timeout; |
| 1614 |
|
#endif |
| 1615 |
int ret; |
int ret; |
| 1616 |
|
|
| 1617 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1642 |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1643 |
} |
} |
| 1644 |
|
|
| 1645 |
|
#if defined(__MSYS__) || defined(__MINGW32__) |
| 1646 |
|
ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4)); |
| 1647 |
|
#else |
| 1648 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1649 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1650 |
|
|
| 1651 |
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); |
| 1652 |
|
#endif |
| 1653 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1654 |
{ |
{ |
| 1655 |
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
log_error("semop(index = %d, lock read) error %d\n", index, errno); |
| 1656 |
} |
} |
| 1657 |
|
|
| 1658 |
return ret; |
return ret; |
| 1662 |
{ |
{ |
| 1663 |
int index; |
int index; |
| 1664 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
| 1665 |
|
#if !defined(__MSYS__) && !defined(__MINGW32__) |
| 1666 |
struct timespec timeout; |
struct timespec timeout; |
| 1667 |
|
#endif |
| 1668 |
int ret; |
int ret; |
| 1669 |
|
|
| 1670 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1685 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1686 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 1687 |
|
|
| 1688 |
|
#if defined(__MSYS__) || defined(__MINGW32__) |
| 1689 |
|
ret = semop(p_section_list_pool->semid, sops, 3); |
| 1690 |
|
#else |
| 1691 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1692 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1693 |
|
|
| 1694 |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1695 |
|
#endif |
| 1696 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1697 |
{ |
{ |
| 1698 |
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
log_error("semop(index = %d, lock write) error %d\n", index, errno); |
| 1699 |
} |
} |
| 1700 |
|
|
| 1701 |
return ret; |
return ret; |