| 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(__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 |
enum _section_list_constant_t |
enum _section_list_constant_t |
| 40 |
{ |
{ |
| 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); |
| 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); |
| 1081 |
} |
} |
| 1082 |
|
|
| 1083 |
page_count = p_section->page_count - 1 + |
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 ? 1 : 0); |
BBS_article_limit_per_page; |
| 1086 |
|
|
| 1087 |
if (page_count < 0) |
if (page_count < 0) |
| 1088 |
{ |
{ |
| 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; |