| 6 |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 12 |
|
|
| 13 |
#include "log.h" |
#include "log.h" |
| 14 |
#include "section_list.h" |
#include "section_list.h" |
| 15 |
#include "trie_dict.h" |
#include "trie_dict.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(__CYGWIN__) |
| 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 |
}; |
}; |
| 225 |
|
|
| 226 |
int set_article_block_shm_readonly(void) |
int set_article_block_shm_readonly(void) |
| 227 |
{ |
{ |
| 228 |
|
#ifndef __CYGWIN__ |
| 229 |
int shmid; |
int shmid; |
| 230 |
void *p_shm; |
void *p_shm; |
| 231 |
int i; |
int i; |
| 248 |
return -2; |
return -2; |
| 249 |
} |
} |
| 250 |
} |
} |
| 251 |
|
#endif |
| 252 |
|
|
| 253 |
return 0; |
return 0; |
| 254 |
} |
} |
| 400 |
return NULL; |
return NULL; |
| 401 |
} |
} |
| 402 |
|
|
| 403 |
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) |
| 404 |
{ |
{ |
| 405 |
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); |
| 406 |
return NULL; |
return NULL; |
| 407 |
} |
} |
| 408 |
|
|
| 409 |
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]; |
| 410 |
|
|
| 411 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % BBS_article_count_per_block >= p_block->article_count) |
| 412 |
{ |
{ |
| 413 |
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); |
| 414 |
return NULL; |
return NULL; |
| 415 |
} |
} |
| 416 |
|
|
| 417 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % BBS_article_count_per_block)); |
| 418 |
} |
} |
| 419 |
|
|
| 420 |
extern int section_list_init(const char *filename) |
extern int section_list_init(const char *filename) |
| 542 |
|
|
| 543 |
int set_section_list_shm_readonly(void) |
int set_section_list_shm_readonly(void) |
| 544 |
{ |
{ |
| 545 |
|
#ifndef __CYGWIN__ |
| 546 |
int shmid; |
int shmid; |
| 547 |
void *p_shm; |
void *p_shm; |
| 548 |
|
|
| 563 |
} |
} |
| 564 |
|
|
| 565 |
p_section_list_pool = p_shm; |
p_section_list_pool = p_shm; |
| 566 |
|
#endif |
| 567 |
|
|
| 568 |
return 0; |
return 0; |
| 569 |
} |
} |
| 778 |
} |
} |
| 779 |
|
|
| 780 |
if (p_article_block_pool->block_count == 0 || |
if (p_article_block_pool->block_count == 0 || |
| 781 |
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) |
| 782 |
{ |
{ |
| 783 |
if ((p_block = pop_free_article_block()) == NULL) |
if ((p_block = pop_free_article_block()) == NULL) |
| 784 |
{ |
{ |
| 788 |
|
|
| 789 |
if (p_article_block_pool->block_count > 0) |
if (p_article_block_pool->block_count > 0) |
| 790 |
{ |
{ |
| 791 |
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; |
| 792 |
} |
} |
| 793 |
|
|
| 794 |
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; |
| 1067 |
} |
} |
| 1068 |
|
|
| 1069 |
page_count = p_section->page_count - 1 + |
page_count = p_section->page_count - 1 + |
| 1070 |
(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) / |
| 1071 |
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page ? 1 : 0); |
BBS_article_limit_per_page; |
| 1072 |
|
|
| 1073 |
if (page_count < 0) |
if (page_count < 0) |
| 1074 |
{ |
{ |
| 1311 |
return -1; |
return -1; |
| 1312 |
} |
} |
| 1313 |
|
|
| 1314 |
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block + |
| 1315 |
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; |
| 1316 |
|
|
| 1317 |
return ret; |
return ret; |
| 1613 |
{ |
{ |
| 1614 |
int index; |
int index; |
| 1615 |
struct sembuf sops[4]; |
struct sembuf sops[4]; |
| 1616 |
|
#ifndef __CYGWIN__ |
| 1617 |
struct timespec timeout; |
struct timespec timeout; |
| 1618 |
|
#endif |
| 1619 |
int ret; |
int ret; |
| 1620 |
|
|
| 1621 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1646 |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1647 |
} |
} |
| 1648 |
|
|
| 1649 |
|
#ifdef __CYGWIN__ |
| 1650 |
|
ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4)); |
| 1651 |
|
#else |
| 1652 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1653 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1654 |
|
|
| 1655 |
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); |
| 1656 |
|
#endif |
| 1657 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1658 |
{ |
{ |
| 1659 |
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
log_error("semop(index = %d, lock read) error %d\n", index, errno); |
| 1660 |
} |
} |
| 1661 |
|
|
| 1662 |
return ret; |
return ret; |
| 1666 |
{ |
{ |
| 1667 |
int index; |
int index; |
| 1668 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
| 1669 |
|
#ifndef __CYGWIN__ |
| 1670 |
struct timespec timeout; |
struct timespec timeout; |
| 1671 |
|
#endif |
| 1672 |
int ret; |
int ret; |
| 1673 |
|
|
| 1674 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1689 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1690 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 1691 |
|
|
| 1692 |
|
#ifdef __CYGWIN__ |
| 1693 |
|
ret = semop(p_section_list_pool->semid, sops, 3); |
| 1694 |
|
#else |
| 1695 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1696 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1697 |
|
|
| 1698 |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1699 |
|
#endif |
| 1700 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1701 |
{ |
{ |
| 1702 |
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
log_error("semop(index = %d, lock write) error %d\n", index, errno); |
| 1703 |
} |
} |
| 1704 |
|
|
| 1705 |
return ret; |
return ret; |