| 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 by 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" |
| 16 |
#include "user_list.h" |
#include "user_list.h" |
| 17 |
#include <errno.h> |
#include <errno.h> |
| 18 |
|
#include <fcntl.h> |
| 19 |
#include <signal.h> |
#include <signal.h> |
| 20 |
#include <stdio.h> |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
#include <stdlib.h> |
| 22 |
#include <string.h> |
#include <string.h> |
| 23 |
#include <unistd.h> |
#include <unistd.h> |
| 24 |
#include <sys/ipc.h> |
#include <sys/mman.h> |
| 25 |
#include <sys/param.h> |
#include <sys/param.h> |
| 26 |
#include <sys/sem.h> |
#include <sys/sem.h> |
| 27 |
#include <sys/shm.h> |
#include <sys/stat.h> |
| 28 |
|
|
| 29 |
#ifdef _SEM_SEMUN_UNDEFINED |
#if defined(_SEM_SEMUN_UNDEFINED) || defined(__CYGWIN__) |
| 30 |
union semun |
union semun |
| 31 |
{ |
{ |
| 32 |
int val; /* Value for SETVAL */ |
int val; /* Value for SETVAL */ |
| 35 |
struct seminfo *__buf; /* Buffer for IPC_INFO |
struct seminfo *__buf; /* Buffer for IPC_INFO |
| 36 |
(Linux-specific) */ |
(Linux-specific) */ |
| 37 |
}; |
}; |
| 38 |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
#endif // #if defined(_SEM_SEMUN_UNDEFINED) |
| 39 |
|
|
| 40 |
#define SECTION_TRY_LOCK_WAIT_TIME 1 // second |
enum _section_list_constant_t |
| 41 |
#define SECTION_TRY_LOCK_TIMES 10 |
{ |
| 42 |
|
SECTION_TRY_LOCK_WAIT_TIME = 1, // second |
| 43 |
|
SECTION_TRY_LOCK_TIMES = 10, |
| 44 |
|
SECTION_DEAD_LOCK_TIMEOUT = 15, // second |
| 45 |
|
|
| 46 |
#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 |
| 47 |
#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) |
| 48 |
#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), |
| 49 |
|
|
| 50 |
#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 |
| 51 |
|
|
| 52 |
#define SID_STR_LEN 5 // 32-bit + NULL |
SID_STR_LEN = 5, // 32-bit + NULL |
| 53 |
|
}; |
| 54 |
|
|
| 55 |
struct article_block_t |
struct article_block_t |
| 56 |
{ |
{ |
| 57 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[BBS_article_count_per_block]; |
| 58 |
int article_count; |
int article_count; |
| 59 |
struct article_block_t *p_next_block; |
struct article_block_t *p_next_block; |
| 60 |
}; |
}; |
| 62 |
|
|
| 63 |
struct article_block_pool_t |
struct article_block_pool_t |
| 64 |
{ |
{ |
| 65 |
int shmid; |
size_t shm_size; |
| 66 |
struct |
struct |
| 67 |
{ |
{ |
| 68 |
int shmid; |
size_t shm_size; |
| 69 |
void *p_shm; |
void *p_shm; |
| 70 |
} shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
} shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
| 71 |
int shm_count; |
int shm_count; |
| 75 |
}; |
}; |
| 76 |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
| 77 |
|
|
| 78 |
|
static char article_block_shm_name[FILE_NAME_LEN]; |
| 79 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 80 |
|
|
| 81 |
|
static char section_list_shm_name[FILE_NAME_LEN]; |
| 82 |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
| 83 |
|
|
| 84 |
|
#ifndef HAVE_SYSTEM_V |
| 85 |
|
static int section_list_reset_lock(SECTION_LIST *p_section); |
| 86 |
|
#endif |
| 87 |
|
|
| 88 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 89 |
{ |
{ |
| 90 |
int shmid; |
char filepath[FILE_PATH_LEN]; |
| 91 |
int proj_id; |
int fd; |
|
key_t key; |
|
| 92 |
size_t size; |
size_t size; |
| 93 |
void *p_shm; |
void *p_shm; |
| 94 |
int i; |
int i; |
| 108 |
return -2; |
return -2; |
| 109 |
} |
} |
| 110 |
|
|
| 111 |
|
strncpy(filepath, filename, sizeof(filepath) - 1); |
| 112 |
|
filepath[sizeof(filepath) - 1] = '\0'; |
| 113 |
|
snprintf(article_block_shm_name, sizeof(article_block_shm_name), "/ARTICLE_BLOCK_SHM_%s", basename(filepath)); |
| 114 |
|
|
| 115 |
// Allocate shared memory |
// Allocate shared memory |
| 116 |
proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm |
size = sizeof(ARTICLE_BLOCK_POOL); |
| 117 |
key = ftok(filename, proj_id); |
snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, ARTICLE_BLOCK_SHM_COUNT_LIMIT); |
| 118 |
if (key == -1) |
|
| 119 |
|
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 120 |
{ |
{ |
| 121 |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
log_error("shm_unlink(%s) error (%d)\n", filepath, errno); |
| 122 |
return -3; |
return -2; |
| 123 |
} |
} |
| 124 |
|
|
| 125 |
size = sizeof(ARTICLE_BLOCK_POOL); |
if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
|
|
if (shmid == -1) |
|
| 126 |
{ |
{ |
| 127 |
log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno); |
log_error("shm_open(%s) error (%d)\n", filepath, errno); |
| 128 |
return -3; |
return -2; |
| 129 |
} |
} |
| 130 |
p_shm = shmat(shmid, NULL, 0); |
if (ftruncate(fd, (off_t)size) == -1) |
|
if (p_shm == (void *)-1) |
|
| 131 |
{ |
{ |
| 132 |
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
log_error("ftruncate(size=%d) error (%d)\n", size, errno); |
| 133 |
return -3; |
close(fd); |
| 134 |
|
return -2; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 138 |
|
if (p_shm == MAP_FAILED) |
| 139 |
|
{ |
| 140 |
|
log_error("mmap() error (%d)\n", errno); |
| 141 |
|
close(fd); |
| 142 |
|
return -2; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
if (close(fd) < 0) |
| 146 |
|
{ |
| 147 |
|
log_error("close(fd) error (%d)\n", errno); |
| 148 |
|
return -1; |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
p_article_block_pool = p_shm; |
p_article_block_pool = p_shm; |
| 152 |
p_article_block_pool->shmid = shmid; |
p_article_block_pool->shm_size = size; |
| 153 |
|
|
| 154 |
p_article_block_pool->shm_count = 0; |
p_article_block_pool->shm_count = 0; |
| 155 |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
| 159 |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
| 160 |
block_count -= block_count_in_shm; |
block_count -= block_count_in_shm; |
| 161 |
|
|
| 162 |
proj_id = p_article_block_pool->shm_count; |
size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
| 163 |
key = ftok(filename, proj_id); |
snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, p_article_block_pool->shm_count); |
| 164 |
if (key == -1) |
|
| 165 |
|
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 166 |
{ |
{ |
| 167 |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
log_error("shm_unlink(%s) error (%d)\n", filepath, errno); |
| 168 |
article_block_cleanup(); |
return -2; |
|
return -3; |
|
| 169 |
} |
} |
| 170 |
|
|
| 171 |
size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
if ((fd = shm_open(filepath, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
|
|
if (shmid == -1) |
|
| 172 |
{ |
{ |
| 173 |
log_error("shmget(shm_index = %d, size = %d) error (%d)\n", p_article_block_pool->shm_count, size, errno); |
log_error("shm_open(%s) error (%d)\n", filepath, errno); |
| 174 |
article_block_cleanup(); |
return -2; |
|
return -3; |
|
| 175 |
} |
} |
| 176 |
p_shm = shmat(shmid, NULL, 0); |
if (ftruncate(fd, (off_t)size) == -1) |
|
if (p_shm == (void *)-1) |
|
| 177 |
{ |
{ |
| 178 |
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
log_error("ftruncate(size=%d) error (%d)\n", size, errno); |
| 179 |
article_block_cleanup(); |
close(fd); |
| 180 |
return -3; |
return -2; |
| 181 |
} |
} |
| 182 |
|
|
| 183 |
(p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shmid = shmid; |
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 184 |
|
if (p_shm == MAP_FAILED) |
| 185 |
|
{ |
| 186 |
|
log_error("mmap() error (%d)\n", errno); |
| 187 |
|
close(fd); |
| 188 |
|
return -2; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
if (close(fd) < 0) |
| 192 |
|
{ |
| 193 |
|
log_error("close(fd) error (%d)\n", errno); |
| 194 |
|
return -1; |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
(p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shm_size = size; |
| 198 |
(p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->p_shm = p_shm; |
(p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->p_shm = p_shm; |
| 199 |
p_article_block_pool->shm_count++; |
p_article_block_pool->shm_count++; |
| 200 |
|
|
| 222 |
|
|
| 223 |
void article_block_cleanup(void) |
void article_block_cleanup(void) |
| 224 |
{ |
{ |
| 225 |
int shmid; |
char filepath[FILE_PATH_LEN]; |
| 226 |
|
|
| 227 |
if (p_article_block_pool == NULL) |
if (p_article_block_pool == NULL) |
| 228 |
{ |
{ |
| 231 |
|
|
| 232 |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 233 |
{ |
{ |
| 234 |
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, i); |
|
{ |
|
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
|
|
} |
|
| 235 |
|
|
| 236 |
if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1) |
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 237 |
{ |
{ |
| 238 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
log_error("shm_unlink(%s) error (%d)\n", filepath, errno); |
| 239 |
} |
} |
| 240 |
} |
} |
| 241 |
|
|
| 242 |
shmid = p_article_block_pool->shmid; |
snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, ARTICLE_BLOCK_SHM_COUNT_LIMIT); |
| 243 |
|
|
| 244 |
if (shmdt(p_article_block_pool) == -1) |
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 245 |
{ |
{ |
| 246 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shm_unlink(%s) error (%d)\n", filepath, errno); |
| 247 |
} |
} |
| 248 |
|
|
| 249 |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
detach_article_block_shm(); |
|
{ |
|
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
|
|
} |
|
|
|
|
|
p_article_block_pool = NULL; |
|
| 250 |
} |
} |
| 251 |
|
|
| 252 |
int set_article_block_shm_readonly(void) |
int set_article_block_shm_readonly(void) |
| 253 |
{ |
{ |
| 254 |
int shmid; |
// int i; |
|
void *p_shm; |
|
|
int i; |
|
| 255 |
|
|
| 256 |
if (p_article_block_pool == NULL) |
if (p_article_block_pool == NULL) |
| 257 |
{ |
{ |
| 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 |
shmid = (p_article_block_pool->shm_pool + i)->shmid; |
// 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) |
| 266 |
|
// { |
| 267 |
|
// log_error("mprotect() error (%d)\n", errno); |
| 268 |
|
// return -2; |
| 269 |
|
// } |
| 270 |
|
// } |
| 271 |
|
|
| 272 |
// Remap shared memory in read-only mode |
if (p_article_block_pool != NULL && |
| 273 |
p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP); |
mprotect(p_article_block_pool, p_article_block_pool->shm_size, PROT_READ) < 0) |
| 274 |
if (p_shm == (void *)-1) |
{ |
| 275 |
{ |
log_error("mprotect() error (%d)\n", errno); |
| 276 |
log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno); |
return -3; |
|
return -2; |
|
|
} |
|
| 277 |
} |
} |
| 278 |
|
|
| 279 |
return 0; |
return 0; |
| 281 |
|
|
| 282 |
int detach_article_block_shm(void) |
int detach_article_block_shm(void) |
| 283 |
{ |
{ |
|
int shmid; |
|
|
|
|
| 284 |
if (p_article_block_pool == NULL) |
if (p_article_block_pool == NULL) |
| 285 |
{ |
{ |
| 286 |
return -1; |
return -1; |
| 288 |
|
|
| 289 |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 290 |
{ |
{ |
| 291 |
if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && |
| 292 |
|
munmap((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size) < 0) |
| 293 |
{ |
{ |
| 294 |
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
log_error("munmap() error (%d)\n", errno); |
| 295 |
return -2; |
return -2; |
| 296 |
} |
} |
| 297 |
} |
} |
| 298 |
|
|
| 299 |
shmid = p_article_block_pool->shmid; |
if (p_article_block_pool != NULL && munmap(p_article_block_pool, p_article_block_pool->shm_size) < 0) |
|
|
|
|
if (shmdt(p_article_block_pool) == -1) |
|
| 300 |
{ |
{ |
| 301 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("munmap() error (%d)\n", errno); |
| 302 |
return -3; |
return -3; |
| 303 |
} |
} |
| 304 |
|
|
| 423 |
return NULL; |
return NULL; |
| 424 |
} |
} |
| 425 |
|
|
| 426 |
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) |
| 427 |
{ |
{ |
| 428 |
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); |
| 429 |
return NULL; |
return NULL; |
| 430 |
} |
} |
| 431 |
|
|
| 432 |
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]; |
| 433 |
|
|
| 434 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % BBS_article_count_per_block >= p_block->article_count) |
| 435 |
{ |
{ |
| 436 |
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); |
| 437 |
return NULL; |
return NULL; |
| 438 |
} |
} |
| 439 |
|
|
| 440 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % BBS_article_count_per_block)); |
| 441 |
} |
} |
| 442 |
|
|
| 443 |
extern int section_list_init(const char *filename) |
extern int section_list_init(const char *filename) |
| 444 |
{ |
{ |
| 445 |
int semid; |
char filepath[FILE_PATH_LEN]; |
| 446 |
int shmid; |
int fd; |
|
int proj_id; |
|
|
key_t key; |
|
| 447 |
size_t size; |
size_t size; |
| 448 |
void *p_shm; |
void *p_shm; |
| 449 |
|
#ifdef HAVE_SYSTEM_V |
| 450 |
|
int proj_id; |
| 451 |
|
key_t key; |
| 452 |
|
int semid; |
| 453 |
union semun arg; |
union semun arg; |
| 454 |
|
#endif |
| 455 |
int i; |
int i; |
| 456 |
|
|
| 457 |
if (p_section_list_pool != NULL) |
if (p_section_list_pool != NULL) |
| 459 |
section_list_cleanup(); |
section_list_cleanup(); |
| 460 |
} |
} |
| 461 |
|
|
| 462 |
proj_id = (int)(time(NULL) % getpid()); |
// Allocate shared memory |
| 463 |
key = ftok(filename, proj_id); |
size = sizeof(SECTION_LIST_POOL); |
| 464 |
if (key == -1) |
|
| 465 |
|
strncpy(filepath, filename, sizeof(filepath) - 1); |
| 466 |
|
filepath[sizeof(filepath) - 1] = '\0'; |
| 467 |
|
snprintf(section_list_shm_name, sizeof(section_list_shm_name), "/SECTION_LIST_SHM_%s", basename(filepath)); |
| 468 |
|
|
| 469 |
|
if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT) |
| 470 |
{ |
{ |
| 471 |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
log_error("shm_unlink(%s) error (%d)\n", section_list_shm_name, errno); |
| 472 |
return -3; |
return -2; |
| 473 |
} |
} |
| 474 |
|
|
| 475 |
// Allocate shared memory |
if ((fd = shm_open(section_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) |
|
size = sizeof(SECTION_LIST_POOL); |
|
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
|
|
if (shmid == -1) |
|
| 476 |
{ |
{ |
| 477 |
log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno); |
log_error("shm_open(%s) error (%d)\n", section_list_shm_name, errno); |
| 478 |
return -3; |
return -2; |
| 479 |
} |
} |
| 480 |
p_shm = shmat(shmid, NULL, 0); |
if (ftruncate(fd, (off_t)size) == -1) |
|
if (p_shm == (void *)-1) |
|
| 481 |
{ |
{ |
| 482 |
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
log_error("ftruncate(size=%d) error (%d)\n", size, errno); |
| 483 |
return -3; |
close(fd); |
| 484 |
|
return -2; |
| 485 |
|
} |
| 486 |
|
|
| 487 |
|
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 488 |
|
if (p_shm == MAP_FAILED) |
| 489 |
|
{ |
| 490 |
|
log_error("mmap() error (%d)\n", errno); |
| 491 |
|
close(fd); |
| 492 |
|
return -2; |
| 493 |
|
} |
| 494 |
|
|
| 495 |
|
if (close(fd) < 0) |
| 496 |
|
{ |
| 497 |
|
log_error("close(fd) error (%d)\n", errno); |
| 498 |
|
return -1; |
| 499 |
} |
} |
| 500 |
|
|
| 501 |
p_section_list_pool = p_shm; |
p_section_list_pool = p_shm; |
| 502 |
p_section_list_pool->shmid = shmid; |
p_section_list_pool->shm_size = size; |
| 503 |
p_section_list_pool->section_count = 0; |
p_section_list_pool->section_count = 0; |
| 504 |
|
|
| 505 |
// Allocate semaphore as section locks |
// Allocate semaphore as section locks |
| 506 |
|
#ifndef HAVE_SYSTEM_V |
| 507 |
|
for (i = 0; i <= BBS_max_section; i++) |
| 508 |
|
{ |
| 509 |
|
if (sem_init(&(p_section_list_pool->sem[i]), 1, 1) == -1) |
| 510 |
|
{ |
| 511 |
|
log_error("sem_init(sem[%d]) error (%d)\n", i, errno); |
| 512 |
|
return -3; |
| 513 |
|
} |
| 514 |
|
|
| 515 |
|
p_section_list_pool->read_lock_count[i] = 0; |
| 516 |
|
p_section_list_pool->write_lock_count[i] = 0; |
| 517 |
|
} |
| 518 |
|
#else |
| 519 |
|
proj_id = (int)(time(NULL) % getpid()); |
| 520 |
|
key = ftok(filename, proj_id); |
| 521 |
|
if (key == -1) |
| 522 |
|
{ |
| 523 |
|
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 524 |
|
return -3; |
| 525 |
|
} |
| 526 |
|
|
| 527 |
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
| 528 |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 529 |
if (semid == -1) |
if (semid == -1) |
| 544 |
} |
} |
| 545 |
|
|
| 546 |
p_section_list_pool->semid = semid; |
p_section_list_pool->semid = semid; |
| 547 |
|
#endif |
| 548 |
|
|
| 549 |
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(); |
| 550 |
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
| 565 |
|
|
| 566 |
void section_list_cleanup(void) |
void section_list_cleanup(void) |
| 567 |
{ |
{ |
|
int shmid; |
|
|
|
|
| 568 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL) |
| 569 |
{ |
{ |
| 570 |
return; |
return; |
| 582 |
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
| 583 |
} |
} |
| 584 |
|
|
| 585 |
shmid = p_section_list_pool->shmid; |
#ifdef HAVE_SYSTEM_V |
|
|
|
| 586 |
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
| 587 |
{ |
{ |
| 588 |
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)\n", p_section_list_pool->semid, errno); |
| 589 |
} |
} |
| 590 |
|
#else |
| 591 |
if (shmdt(p_section_list_pool) == -1) |
for (int i = 0; i <= BBS_max_section; i++) |
| 592 |
{ |
{ |
| 593 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
if (sem_destroy(&(p_section_list_pool->sem[i])) == -1) |
| 594 |
|
{ |
| 595 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", i, errno); |
| 596 |
|
} |
| 597 |
} |
} |
| 598 |
|
#endif |
| 599 |
|
|
| 600 |
|
detach_section_list_shm(); |
| 601 |
|
|
| 602 |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT) |
| 603 |
{ |
{ |
| 604 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shm_unlink(%s) error (%d)\n", section_list_shm_name, errno); |
| 605 |
} |
} |
|
|
|
|
p_section_list_pool = NULL; |
|
| 606 |
} |
} |
| 607 |
|
|
| 608 |
int set_section_list_shm_readonly(void) |
int set_section_list_shm_readonly(void) |
| 609 |
{ |
{ |
|
int shmid; |
|
|
void *p_shm; |
|
|
|
|
| 610 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL) |
| 611 |
{ |
{ |
| 612 |
log_error("p_section_list_pool not initialized\n"); |
log_error("p_section_list_pool not initialized\n"); |
| 613 |
return -1; |
return -1; |
| 614 |
} |
} |
| 615 |
|
|
| 616 |
shmid = p_section_list_pool->shmid; |
if (p_section_list_pool != NULL && |
| 617 |
|
mprotect(p_section_list_pool, p_section_list_pool->shm_size, PROT_READ) < 0) |
|
// Remap shared memory in read-only mode |
|
|
p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP); |
|
|
if (p_shm == (void *)-1) |
|
| 618 |
{ |
{ |
| 619 |
log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno); |
log_error("mprotect() error (%d)\n", errno); |
| 620 |
return -3; |
return -2; |
| 621 |
} |
} |
| 622 |
|
|
|
p_section_list_pool = p_shm; |
|
|
|
|
| 623 |
return 0; |
return 0; |
| 624 |
} |
} |
| 625 |
|
|
| 626 |
int detach_section_list_shm(void) |
int detach_section_list_shm(void) |
| 627 |
{ |
{ |
| 628 |
if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1) |
if (p_section_list_pool != NULL && munmap(p_section_list_pool, p_section_list_pool->shm_size) < 0) |
| 629 |
{ |
{ |
| 630 |
log_error("shmdt(section_list_pool) error (%d)\n", errno); |
log_error("munmap() error (%d)\n", errno); |
| 631 |
return -1; |
return -1; |
| 632 |
} |
} |
| 633 |
|
|
| 833 |
} |
} |
| 834 |
|
|
| 835 |
if (p_article_block_pool->block_count == 0 || |
if (p_article_block_pool->block_count == 0 || |
| 836 |
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) |
| 837 |
{ |
{ |
| 838 |
if ((p_block = pop_free_article_block()) == NULL) |
if ((p_block = pop_free_article_block()) == NULL) |
| 839 |
{ |
{ |
| 843 |
|
|
| 844 |
if (p_article_block_pool->block_count > 0) |
if (p_article_block_pool->block_count > 0) |
| 845 |
{ |
{ |
| 846 |
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; |
| 847 |
} |
} |
| 848 |
|
|
| 849 |
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; |
| 1031 |
return affected_count; |
return affected_count; |
| 1032 |
} |
} |
| 1033 |
|
|
| 1034 |
|
int section_list_set_article_excerption(SECTION_LIST *p_section, int32_t aid, int8_t excerption) |
| 1035 |
|
{ |
| 1036 |
|
ARTICLE *p_article; |
| 1037 |
|
|
| 1038 |
|
if (p_section == NULL) |
| 1039 |
|
{ |
| 1040 |
|
log_error("NULL pointer error\n"); |
| 1041 |
|
return -1; |
| 1042 |
|
} |
| 1043 |
|
|
| 1044 |
|
p_article = article_block_find_by_aid(aid); |
| 1045 |
|
if (p_article == NULL) |
| 1046 |
|
{ |
| 1047 |
|
return -1; // Not found |
| 1048 |
|
} |
| 1049 |
|
|
| 1050 |
|
if (p_section->sid != p_article->sid) |
| 1051 |
|
{ |
| 1052 |
|
log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 1053 |
|
return -2; |
| 1054 |
|
} |
| 1055 |
|
|
| 1056 |
|
if (p_article->excerption == excerption) |
| 1057 |
|
{ |
| 1058 |
|
return 0; // Already set |
| 1059 |
|
} |
| 1060 |
|
|
| 1061 |
|
p_article->excerption = excerption; |
| 1062 |
|
|
| 1063 |
|
return 1; |
| 1064 |
|
} |
| 1065 |
|
|
| 1066 |
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) |
| 1067 |
{ |
{ |
| 1068 |
int i; |
int i; |
| 1154 |
} |
} |
| 1155 |
|
|
| 1156 |
page_count = p_section->page_count - 1 + |
page_count = p_section->page_count - 1 + |
| 1157 |
(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) / |
| 1158 |
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page ? 1 : 0); |
BBS_article_limit_per_page; |
| 1159 |
|
|
| 1160 |
if (page_count < 0) |
if (page_count < 0) |
| 1161 |
{ |
{ |
| 1398 |
return -1; |
return -1; |
| 1399 |
} |
} |
| 1400 |
|
|
| 1401 |
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block + |
| 1402 |
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; |
| 1403 |
|
|
| 1404 |
return ret; |
return ret; |
| 1699 |
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) |
| 1700 |
{ |
{ |
| 1701 |
int index; |
int index; |
| 1702 |
|
#ifdef HAVE_SYSTEM_V |
| 1703 |
struct sembuf sops[4]; |
struct sembuf sops[4]; |
| 1704 |
|
#endif |
| 1705 |
struct timespec timeout; |
struct timespec timeout; |
| 1706 |
int ret; |
int ret = 0; |
| 1707 |
|
|
| 1708 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1709 |
if (index < 0) |
if (index < 0) |
| 1711 |
return -2; |
return -2; |
| 1712 |
} |
} |
| 1713 |
|
|
| 1714 |
|
timeout.tv_sec = wait_sec; |
| 1715 |
|
timeout.tv_nsec = 0; |
| 1716 |
|
|
| 1717 |
|
#ifdef HAVE_SYSTEM_V |
| 1718 |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1719 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1720 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1737 |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1738 |
} |
} |
| 1739 |
|
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
| 1740 |
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); |
| 1741 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1742 |
{ |
{ |
| 1743 |
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
log_error("semop(index = %d, lock read) error %d\n", index, errno); |
| 1744 |
|
} |
| 1745 |
|
#else |
| 1746 |
|
if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1) |
| 1747 |
|
{ |
| 1748 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1749 |
|
{ |
| 1750 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", index, errno); |
| 1751 |
|
} |
| 1752 |
|
return -1; |
| 1753 |
|
} |
| 1754 |
|
|
| 1755 |
|
if (index != BBS_max_section) |
| 1756 |
|
{ |
| 1757 |
|
if (sem_timedwait(&(p_section_list_pool->sem[BBS_max_section]), &timeout) == -1) |
| 1758 |
|
{ |
| 1759 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1760 |
|
{ |
| 1761 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1762 |
|
} |
| 1763 |
|
// release previously acquired lock |
| 1764 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1765 |
|
{ |
| 1766 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1767 |
|
} |
| 1768 |
|
return -1; |
| 1769 |
|
} |
| 1770 |
} |
} |
| 1771 |
|
|
| 1772 |
|
if (p_section_list_pool->write_lock_count[index] == 0 && |
| 1773 |
|
(index != BBS_max_section && p_section_list_pool->write_lock_count[BBS_max_section] == 0)) |
| 1774 |
|
{ |
| 1775 |
|
p_section_list_pool->read_lock_count[index]++; |
| 1776 |
|
if (index != BBS_max_section) |
| 1777 |
|
{ |
| 1778 |
|
p_section_list_pool->read_lock_count[BBS_max_section]++; |
| 1779 |
|
} |
| 1780 |
|
} |
| 1781 |
|
else |
| 1782 |
|
{ |
| 1783 |
|
errno = EAGAIN; |
| 1784 |
|
ret = -1; |
| 1785 |
|
} |
| 1786 |
|
|
| 1787 |
|
if (index != BBS_max_section) |
| 1788 |
|
{ |
| 1789 |
|
// release lock on "all section" |
| 1790 |
|
if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1791 |
|
{ |
| 1792 |
|
log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1793 |
|
ret = -1; |
| 1794 |
|
} |
| 1795 |
|
} |
| 1796 |
|
|
| 1797 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1798 |
|
{ |
| 1799 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1800 |
|
return -1; |
| 1801 |
|
} |
| 1802 |
|
#endif |
| 1803 |
|
|
| 1804 |
return ret; |
return ret; |
| 1805 |
} |
} |
| 1806 |
|
|
| 1807 |
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
| 1808 |
{ |
{ |
| 1809 |
int index; |
int index; |
| 1810 |
|
#ifdef HAVE_SYSTEM_V |
| 1811 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
| 1812 |
|
#endif |
| 1813 |
struct timespec timeout; |
struct timespec timeout; |
| 1814 |
int ret; |
int ret = 0; |
| 1815 |
|
|
| 1816 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1817 |
if (index < 0) |
if (index < 0) |
| 1819 |
return -2; |
return -2; |
| 1820 |
} |
} |
| 1821 |
|
|
| 1822 |
|
timeout.tv_sec = wait_sec; |
| 1823 |
|
timeout.tv_nsec = 0; |
| 1824 |
|
|
| 1825 |
|
#ifdef HAVE_SYSTEM_V |
| 1826 |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1827 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1828 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1835 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1836 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 1837 |
|
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
| 1838 |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1839 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1840 |
{ |
{ |
| 1841 |
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
log_error("semop(index = %d, lock write) error %d\n", index, errno); |
| 1842 |
|
} |
| 1843 |
|
#else |
| 1844 |
|
if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1) |
| 1845 |
|
{ |
| 1846 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1847 |
|
{ |
| 1848 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", index, errno); |
| 1849 |
|
} |
| 1850 |
|
return -1; |
| 1851 |
} |
} |
| 1852 |
|
|
| 1853 |
|
if (p_section_list_pool->read_lock_count[index] == 0 && p_section_list_pool->write_lock_count[index] == 0) |
| 1854 |
|
{ |
| 1855 |
|
p_section_list_pool->write_lock_count[index]++; |
| 1856 |
|
} |
| 1857 |
|
else |
| 1858 |
|
{ |
| 1859 |
|
errno = EAGAIN; |
| 1860 |
|
ret = -1; |
| 1861 |
|
} |
| 1862 |
|
|
| 1863 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1864 |
|
{ |
| 1865 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1866 |
|
return -1; |
| 1867 |
|
} |
| 1868 |
|
#endif |
| 1869 |
|
|
| 1870 |
return ret; |
return ret; |
| 1871 |
} |
} |
| 1872 |
|
|
| 1873 |
int section_list_rd_unlock(SECTION_LIST *p_section) |
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1874 |
{ |
{ |
| 1875 |
int index; |
int index; |
| 1876 |
|
#ifdef HAVE_SYSTEM_V |
| 1877 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 1878 |
int ret; |
#endif |
| 1879 |
|
int ret = 0; |
| 1880 |
|
|
| 1881 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1882 |
if (index < 0) |
if (index < 0) |
| 1884 |
return -2; |
return -2; |
| 1885 |
} |
} |
| 1886 |
|
|
| 1887 |
|
#ifdef HAVE_SYSTEM_V |
| 1888 |
sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1889 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1890 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1901 |
{ |
{ |
| 1902 |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1903 |
} |
} |
| 1904 |
|
#else |
| 1905 |
|
if (sem_wait(&(p_section_list_pool->sem[index])) == -1) |
| 1906 |
|
{ |
| 1907 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1908 |
|
{ |
| 1909 |
|
log_error("sem_wait(sem[%d]) error %d\n", index, errno); |
| 1910 |
|
} |
| 1911 |
|
return -1; |
| 1912 |
|
} |
| 1913 |
|
|
| 1914 |
|
if (index != BBS_max_section) |
| 1915 |
|
{ |
| 1916 |
|
if (sem_wait(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1917 |
|
{ |
| 1918 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1919 |
|
{ |
| 1920 |
|
log_error("sem_wait(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1921 |
|
} |
| 1922 |
|
// release previously acquired lock |
| 1923 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1924 |
|
{ |
| 1925 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1926 |
|
} |
| 1927 |
|
return -1; |
| 1928 |
|
} |
| 1929 |
|
} |
| 1930 |
|
|
| 1931 |
|
if (p_section_list_pool->read_lock_count[index] > 0) |
| 1932 |
|
{ |
| 1933 |
|
p_section_list_pool->read_lock_count[index]--; |
| 1934 |
|
} |
| 1935 |
|
else |
| 1936 |
|
{ |
| 1937 |
|
log_error("read_lock_count[%d] already 0\n", index); |
| 1938 |
|
} |
| 1939 |
|
|
| 1940 |
|
if (index != BBS_max_section && p_section_list_pool->read_lock_count[BBS_max_section] > 0) |
| 1941 |
|
{ |
| 1942 |
|
p_section_list_pool->read_lock_count[BBS_max_section]--; |
| 1943 |
|
} |
| 1944 |
|
else |
| 1945 |
|
{ |
| 1946 |
|
log_error("read_lock_count[%d] already 0\n", BBS_max_section); |
| 1947 |
|
} |
| 1948 |
|
|
| 1949 |
|
if (index != BBS_max_section) |
| 1950 |
|
{ |
| 1951 |
|
// release lock on "all section" |
| 1952 |
|
if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1953 |
|
{ |
| 1954 |
|
log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1955 |
|
ret = -1; |
| 1956 |
|
} |
| 1957 |
|
} |
| 1958 |
|
|
| 1959 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1960 |
|
{ |
| 1961 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1962 |
|
return -1; |
| 1963 |
|
} |
| 1964 |
|
#endif |
| 1965 |
|
|
| 1966 |
return ret; |
return ret; |
| 1967 |
} |
} |
| 1969 |
int section_list_rw_unlock(SECTION_LIST *p_section) |
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1970 |
{ |
{ |
| 1971 |
int index; |
int index; |
| 1972 |
|
#ifdef HAVE_SYSTEM_V |
| 1973 |
struct sembuf sops[1]; |
struct sembuf sops[1]; |
| 1974 |
int ret; |
#endif |
| 1975 |
|
int ret = 0; |
| 1976 |
|
|
| 1977 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1978 |
if (index < 0) |
if (index < 0) |
| 1980 |
return -2; |
return -2; |
| 1981 |
} |
} |
| 1982 |
|
|
| 1983 |
|
#ifdef HAVE_SYSTEM_V |
| 1984 |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1985 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1986 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1990 |
{ |
{ |
| 1991 |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1992 |
} |
} |
| 1993 |
|
#else |
| 1994 |
|
if (sem_wait(&(p_section_list_pool->sem[index])) == -1) |
| 1995 |
|
{ |
| 1996 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1997 |
|
{ |
| 1998 |
|
log_error("sem_wait(sem[%d]) error %d\n", index, errno); |
| 1999 |
|
} |
| 2000 |
|
return -1; |
| 2001 |
|
} |
| 2002 |
|
|
| 2003 |
|
if (p_section_list_pool->write_lock_count[index] > 0) |
| 2004 |
|
{ |
| 2005 |
|
p_section_list_pool->write_lock_count[index]--; |
| 2006 |
|
} |
| 2007 |
|
else |
| 2008 |
|
{ |
| 2009 |
|
log_error("write_lock_count[%d] already 0\n", index); |
| 2010 |
|
} |
| 2011 |
|
|
| 2012 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 2013 |
|
{ |
| 2014 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 2015 |
|
return -1; |
| 2016 |
|
} |
| 2017 |
|
#endif |
| 2018 |
|
|
| 2019 |
return ret; |
return ret; |
| 2020 |
} |
} |
| 2024 |
int timer = 0; |
int timer = 0; |
| 2025 |
int sid = (p_section == NULL ? 0 : p_section->sid); |
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 2026 |
int ret = -1; |
int ret = -1; |
| 2027 |
|
time_t tm_first_failure = 0; |
| 2028 |
|
|
| 2029 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 2030 |
{ |
{ |
| 2035 |
} |
} |
| 2036 |
else if (errno == EAGAIN || errno == EINTR) // retry |
else if (errno == EAGAIN || errno == EINTR) // retry |
| 2037 |
{ |
{ |
| 2038 |
|
// Dead lock detection |
| 2039 |
|
if (tm_first_failure == 0) |
| 2040 |
|
{ |
| 2041 |
|
time(&tm_first_failure); |
| 2042 |
|
} |
| 2043 |
|
|
| 2044 |
timer++; |
timer++; |
| 2045 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 2046 |
{ |
{ |
| 2047 |
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\n", timer, sid); |
| 2048 |
|
if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT) |
| 2049 |
|
{ |
| 2050 |
|
log_error("Unable to acquire rd_lock for %d seconds\n", time(NULL) - tm_first_failure); |
| 2051 |
|
#ifndef HAVE_SYSTEM_V |
| 2052 |
|
section_list_reset_lock(p_section); |
| 2053 |
|
log_error("Reset POSIX semaphore to resolve dead lock\n"); |
| 2054 |
|
#endif |
| 2055 |
|
break; |
| 2056 |
|
} |
| 2057 |
} |
} |
| 2058 |
|
usleep(100 * 1000); // 0.1 second |
| 2059 |
} |
} |
| 2060 |
else // failed |
else // failed |
| 2061 |
{ |
{ |
| 2072 |
int timer = 0; |
int timer = 0; |
| 2073 |
int sid = (p_section == NULL ? 0 : p_section->sid); |
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 2074 |
int ret = -1; |
int ret = -1; |
| 2075 |
|
time_t tm_first_failure = 0; |
| 2076 |
|
|
| 2077 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 2078 |
{ |
{ |
| 2083 |
} |
} |
| 2084 |
else if (errno == EAGAIN || errno == EINTR) // retry |
else if (errno == EAGAIN || errno == EINTR) // retry |
| 2085 |
{ |
{ |
| 2086 |
|
// Dead lock detection |
| 2087 |
|
if (tm_first_failure == 0) |
| 2088 |
|
{ |
| 2089 |
|
time(&tm_first_failure); |
| 2090 |
|
} |
| 2091 |
|
|
| 2092 |
timer++; |
timer++; |
| 2093 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 2094 |
{ |
{ |
| 2095 |
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\n", timer, sid); |
| 2096 |
|
if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT) |
| 2097 |
|
{ |
| 2098 |
|
log_error("Unable to acquire rw_lock for %d seconds\n", time(NULL) - tm_first_failure); |
| 2099 |
|
#ifndef HAVE_SYSTEM_V |
| 2100 |
|
section_list_reset_lock(p_section); |
| 2101 |
|
log_error("Reset POSIX semaphore to resolve dead lock\n"); |
| 2102 |
|
#endif |
| 2103 |
|
break; |
| 2104 |
|
} |
| 2105 |
} |
} |
| 2106 |
|
usleep(100 * 1000); // 0.1 second |
| 2107 |
} |
} |
| 2108 |
else // failed |
else // failed |
| 2109 |
{ |
{ |
| 2114 |
|
|
| 2115 |
return ret; |
return ret; |
| 2116 |
} |
} |
| 2117 |
|
|
| 2118 |
|
#ifndef HAVE_SYSTEM_V |
| 2119 |
|
int section_list_reset_lock(SECTION_LIST *p_section) |
| 2120 |
|
{ |
| 2121 |
|
int index; |
| 2122 |
|
|
| 2123 |
|
if (p_section == NULL) |
| 2124 |
|
{ |
| 2125 |
|
log_error("NULL pointer error\n"); |
| 2126 |
|
return -1; |
| 2127 |
|
} |
| 2128 |
|
|
| 2129 |
|
index = get_section_index(p_section); |
| 2130 |
|
if (index < 0) |
| 2131 |
|
{ |
| 2132 |
|
return -2; |
| 2133 |
|
} |
| 2134 |
|
|
| 2135 |
|
if (sem_destroy(&(p_section_list_pool->sem[index])) == -1) |
| 2136 |
|
{ |
| 2137 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", index, errno); |
| 2138 |
|
} |
| 2139 |
|
|
| 2140 |
|
p_section_list_pool->read_lock_count[index] = 0; |
| 2141 |
|
p_section_list_pool->write_lock_count[index] = 0; |
| 2142 |
|
|
| 2143 |
|
if (sem_init(&(p_section_list_pool->sem[index]), 1, 1) == -1) |
| 2144 |
|
{ |
| 2145 |
|
log_error("sem_init(sem[%d]) error (%d)\n", index, errno); |
| 2146 |
|
} |
| 2147 |
|
|
| 2148 |
|
if (index != BBS_max_section) |
| 2149 |
|
{ |
| 2150 |
|
if (sem_destroy(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 2151 |
|
{ |
| 2152 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", BBS_max_section, errno); |
| 2153 |
|
} |
| 2154 |
|
|
| 2155 |
|
p_section_list_pool->read_lock_count[BBS_max_section] = 0; |
| 2156 |
|
p_section_list_pool->write_lock_count[BBS_max_section] = 0; |
| 2157 |
|
|
| 2158 |
|
if (sem_init(&(p_section_list_pool->sem[BBS_max_section]), 1, 1) == -1) |
| 2159 |
|
{ |
| 2160 |
|
log_error("sem_init(sem[%d]) error (%d)\n", BBS_max_section, errno); |
| 2161 |
|
} |
| 2162 |
|
} |
| 2163 |
|
|
| 2164 |
|
return 0; |
| 2165 |
|
} |
| 2166 |
|
#endif |