| 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 |
enum _section_list_constant_t |
enum _section_list_constant_t |
| 41 |
{ |
{ |
| 61 |
|
|
| 62 |
struct article_block_pool_t |
struct article_block_pool_t |
| 63 |
{ |
{ |
| 64 |
int shmid; |
size_t shm_size; |
| 65 |
struct |
struct |
| 66 |
{ |
{ |
| 67 |
int shmid; |
size_t shm_size; |
| 68 |
void *p_shm; |
void *p_shm; |
| 69 |
} shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
} shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
| 70 |
int shm_count; |
int shm_count; |
| 74 |
}; |
}; |
| 75 |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
| 76 |
|
|
| 77 |
|
static char article_block_shm_name[FILE_NAME_LEN]; |
| 78 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 79 |
|
|
| 80 |
|
static char section_list_shm_name[FILE_NAME_LEN]; |
| 81 |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
| 82 |
|
|
| 83 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 84 |
{ |
{ |
| 85 |
int shmid; |
char filepath[FILE_PATH_LEN]; |
| 86 |
int proj_id; |
int fd; |
|
key_t key; |
|
| 87 |
size_t size; |
size_t size; |
| 88 |
void *p_shm; |
void *p_shm; |
| 89 |
int i; |
int i; |
| 103 |
return -2; |
return -2; |
| 104 |
} |
} |
| 105 |
|
|
| 106 |
|
strncpy(filepath, filename, sizeof(filepath) - 1); |
| 107 |
|
filepath[sizeof(filepath) - 1] = '\0'; |
| 108 |
|
snprintf(article_block_shm_name, sizeof(article_block_shm_name), "/ARTICLE_BLOCK_SHM_%s", basename(filepath)); |
| 109 |
|
|
| 110 |
// Allocate shared memory |
// Allocate shared memory |
| 111 |
proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm |
size = sizeof(ARTICLE_BLOCK_POOL); |
| 112 |
key = ftok(filename, proj_id); |
snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, ARTICLE_BLOCK_SHM_COUNT_LIMIT); |
| 113 |
if (key == -1) |
|
| 114 |
|
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 115 |
{ |
{ |
| 116 |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
log_error("shm_unlink(%s) error (%d)\n", filepath, errno); |
| 117 |
return -3; |
return -2; |
| 118 |
} |
} |
| 119 |
|
|
| 120 |
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) |
|
| 121 |
{ |
{ |
| 122 |
log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno); |
log_error("shm_open(%s) error (%d)\n", filepath, errno); |
| 123 |
return -3; |
return -2; |
| 124 |
} |
} |
| 125 |
p_shm = shmat(shmid, NULL, 0); |
if (ftruncate(fd, (off_t)size) == -1) |
|
if (p_shm == (void *)-1) |
|
| 126 |
{ |
{ |
| 127 |
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
log_error("ftruncate(size=%d) error (%d)\n", size, errno); |
| 128 |
return -3; |
close(fd); |
| 129 |
|
return -2; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 133 |
|
if (p_shm == MAP_FAILED) |
| 134 |
|
{ |
| 135 |
|
log_error("mmap() error (%d)\n", errno); |
| 136 |
|
close(fd); |
| 137 |
|
return -2; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
if (close(fd) < 0) |
| 141 |
|
{ |
| 142 |
|
log_error("close(fd) error (%d)\n", errno); |
| 143 |
|
return -1; |
| 144 |
} |
} |
| 145 |
|
|
| 146 |
p_article_block_pool = p_shm; |
p_article_block_pool = p_shm; |
| 147 |
p_article_block_pool->shmid = shmid; |
p_article_block_pool->shm_size = size; |
| 148 |
|
|
| 149 |
p_article_block_pool->shm_count = 0; |
p_article_block_pool->shm_count = 0; |
| 150 |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
| 154 |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
| 155 |
block_count -= block_count_in_shm; |
block_count -= block_count_in_shm; |
| 156 |
|
|
| 157 |
proj_id = p_article_block_pool->shm_count; |
size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
| 158 |
key = ftok(filename, proj_id); |
snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, p_article_block_pool->shm_count); |
| 159 |
if (key == -1) |
|
| 160 |
|
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 161 |
{ |
{ |
| 162 |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
log_error("shm_unlink(%s) error (%d)\n", filepath, errno); |
| 163 |
article_block_cleanup(); |
return -2; |
|
return -3; |
|
| 164 |
} |
} |
| 165 |
|
|
| 166 |
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) |
|
| 167 |
{ |
{ |
| 168 |
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); |
| 169 |
article_block_cleanup(); |
return -2; |
|
return -3; |
|
| 170 |
} |
} |
| 171 |
p_shm = shmat(shmid, NULL, 0); |
if (ftruncate(fd, (off_t)size) == -1) |
|
if (p_shm == (void *)-1) |
|
| 172 |
{ |
{ |
| 173 |
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
log_error("ftruncate(size=%d) error (%d)\n", size, errno); |
| 174 |
article_block_cleanup(); |
close(fd); |
| 175 |
return -3; |
return -2; |
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 179 |
|
if (p_shm == MAP_FAILED) |
| 180 |
|
{ |
| 181 |
|
log_error("mmap() error (%d)\n", errno); |
| 182 |
|
close(fd); |
| 183 |
|
return -2; |
| 184 |
} |
} |
| 185 |
|
|
| 186 |
(p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shmid = shmid; |
if (close(fd) < 0) |
| 187 |
|
{ |
| 188 |
|
log_error("close(fd) error (%d)\n", errno); |
| 189 |
|
return -1; |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
(p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shm_size = size; |
| 193 |
(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; |
| 194 |
p_article_block_pool->shm_count++; |
p_article_block_pool->shm_count++; |
| 195 |
|
|
| 217 |
|
|
| 218 |
void article_block_cleanup(void) |
void article_block_cleanup(void) |
| 219 |
{ |
{ |
| 220 |
int shmid; |
char filepath[FILE_PATH_LEN]; |
| 221 |
|
|
| 222 |
if (p_article_block_pool == NULL) |
if (p_article_block_pool == NULL) |
| 223 |
{ |
{ |
| 226 |
|
|
| 227 |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 228 |
{ |
{ |
| 229 |
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); |
|
|
} |
|
| 230 |
|
|
| 231 |
if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1) |
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 232 |
{ |
{ |
| 233 |
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); |
| 234 |
} |
} |
| 235 |
} |
} |
| 236 |
|
|
| 237 |
shmid = p_article_block_pool->shmid; |
snprintf(filepath, sizeof(filepath), "%s_%d", article_block_shm_name, ARTICLE_BLOCK_SHM_COUNT_LIMIT); |
|
|
|
|
if (shmdt(p_article_block_pool) == -1) |
|
|
{ |
|
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
|
|
} |
|
| 238 |
|
|
| 239 |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 240 |
{ |
{ |
| 241 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shm_unlink(%s) error (%d)\n", filepath, errno); |
| 242 |
} |
} |
| 243 |
|
|
| 244 |
p_article_block_pool = NULL; |
detach_article_block_shm(); |
| 245 |
} |
} |
| 246 |
|
|
| 247 |
int set_article_block_shm_readonly(void) |
int set_article_block_shm_readonly(void) |
| 248 |
{ |
{ |
|
int shmid; |
|
|
void *p_shm; |
|
| 249 |
int i; |
int i; |
| 250 |
|
|
| 251 |
if (p_article_block_pool == NULL) |
if (p_article_block_pool == NULL) |
| 256 |
|
|
| 257 |
for (i = 0; i < p_article_block_pool->shm_count; i++) |
for (i = 0; i < p_article_block_pool->shm_count; i++) |
| 258 |
{ |
{ |
| 259 |
shmid = (p_article_block_pool->shm_pool + i)->shmid; |
if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && |
| 260 |
|
mprotect((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size, PROT_READ) < 0) |
|
// Remap shared memory in read-only mode |
|
|
p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP); |
|
|
if (p_shm == (void *)-1) |
|
| 261 |
{ |
{ |
| 262 |
log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno); |
log_error("mprotect() error (%d)\n", errno); |
| 263 |
return -2; |
return -2; |
| 264 |
} |
} |
| 265 |
} |
} |
| 266 |
|
|
| 267 |
|
if (p_article_block_pool != NULL && |
| 268 |
|
mprotect(p_article_block_pool, p_article_block_pool->shm_size, PROT_READ) < 0) |
| 269 |
|
{ |
| 270 |
|
log_error("mprotect() error (%d)\n", errno); |
| 271 |
|
return -3; |
| 272 |
|
} |
| 273 |
|
|
| 274 |
return 0; |
return 0; |
| 275 |
} |
} |
| 276 |
|
|
| 277 |
int detach_article_block_shm(void) |
int detach_article_block_shm(void) |
| 278 |
{ |
{ |
|
int shmid; |
|
|
|
|
| 279 |
if (p_article_block_pool == NULL) |
if (p_article_block_pool == NULL) |
| 280 |
{ |
{ |
| 281 |
return -1; |
return -1; |
| 283 |
|
|
| 284 |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 285 |
{ |
{ |
| 286 |
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 && |
| 287 |
|
munmap((p_article_block_pool->shm_pool + i)->p_shm, (p_article_block_pool->shm_pool + i)->shm_size) < 0) |
| 288 |
{ |
{ |
| 289 |
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
log_error("munmap() error (%d)\n", errno); |
| 290 |
return -2; |
return -2; |
| 291 |
} |
} |
| 292 |
} |
} |
| 293 |
|
|
| 294 |
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) |
|
| 295 |
{ |
{ |
| 296 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("munmap() error (%d)\n", errno); |
| 297 |
return -3; |
return -3; |
| 298 |
} |
} |
| 299 |
|
|
| 437 |
|
|
| 438 |
extern int section_list_init(const char *filename) |
extern int section_list_init(const char *filename) |
| 439 |
{ |
{ |
| 440 |
int semid; |
char filepath[FILE_PATH_LEN]; |
| 441 |
int shmid; |
int fd; |
|
int proj_id; |
|
|
key_t key; |
|
| 442 |
size_t size; |
size_t size; |
| 443 |
void *p_shm; |
void *p_shm; |
| 444 |
|
#ifdef HAVE_SYSTEM_V |
| 445 |
|
int proj_id; |
| 446 |
|
key_t key; |
| 447 |
|
int semid; |
| 448 |
union semun arg; |
union semun arg; |
| 449 |
|
#endif |
| 450 |
int i; |
int i; |
| 451 |
|
|
| 452 |
if (p_section_list_pool != NULL) |
if (p_section_list_pool != NULL) |
| 454 |
section_list_cleanup(); |
section_list_cleanup(); |
| 455 |
} |
} |
| 456 |
|
|
| 457 |
proj_id = (int)(time(NULL) % getpid()); |
// Allocate shared memory |
| 458 |
key = ftok(filename, proj_id); |
size = sizeof(SECTION_LIST_POOL); |
| 459 |
if (key == -1) |
|
| 460 |
|
strncpy(filepath, filename, sizeof(filepath) - 1); |
| 461 |
|
filepath[sizeof(filepath) - 1] = '\0'; |
| 462 |
|
snprintf(section_list_shm_name, sizeof(section_list_shm_name), "/SECTION_LIST_SHM_%s", basename(filepath)); |
| 463 |
|
|
| 464 |
|
if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT) |
| 465 |
{ |
{ |
| 466 |
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); |
| 467 |
return -3; |
return -2; |
| 468 |
} |
} |
| 469 |
|
|
| 470 |
// 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) |
|
| 471 |
{ |
{ |
| 472 |
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); |
| 473 |
return -3; |
return -2; |
| 474 |
} |
} |
| 475 |
p_shm = shmat(shmid, NULL, 0); |
if (ftruncate(fd, (off_t)size) == -1) |
|
if (p_shm == (void *)-1) |
|
| 476 |
{ |
{ |
| 477 |
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
log_error("ftruncate(size=%d) error (%d)\n", size, errno); |
| 478 |
return -3; |
close(fd); |
| 479 |
|
return -2; |
| 480 |
|
} |
| 481 |
|
|
| 482 |
|
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 483 |
|
if (p_shm == MAP_FAILED) |
| 484 |
|
{ |
| 485 |
|
log_error("mmap() error (%d)\n", errno); |
| 486 |
|
close(fd); |
| 487 |
|
return -2; |
| 488 |
|
} |
| 489 |
|
|
| 490 |
|
if (close(fd) < 0) |
| 491 |
|
{ |
| 492 |
|
log_error("close(fd) error (%d)\n", errno); |
| 493 |
|
return -1; |
| 494 |
} |
} |
| 495 |
|
|
| 496 |
p_section_list_pool = p_shm; |
p_section_list_pool = p_shm; |
| 497 |
p_section_list_pool->shmid = shmid; |
p_section_list_pool->shm_size = size; |
| 498 |
p_section_list_pool->section_count = 0; |
p_section_list_pool->section_count = 0; |
| 499 |
|
|
| 500 |
// Allocate semaphore as section locks |
// Allocate semaphore as section locks |
| 501 |
|
#ifndef HAVE_SYSTEM_V |
| 502 |
|
for (i = 0; i <= BBS_max_section; i++) |
| 503 |
|
{ |
| 504 |
|
if (sem_init(&(p_section_list_pool->sem[i]), 1, 1) == -1) |
| 505 |
|
{ |
| 506 |
|
log_error("sem_init(sem[%d]) error (%d)\n", i, errno); |
| 507 |
|
return -3; |
| 508 |
|
} |
| 509 |
|
|
| 510 |
|
p_section_list_pool->read_lock_count[i] = 0; |
| 511 |
|
p_section_list_pool->write_lock_count[i] = 0; |
| 512 |
|
} |
| 513 |
|
#else |
| 514 |
|
proj_id = (int)(time(NULL) % getpid()); |
| 515 |
|
key = ftok(filename, proj_id); |
| 516 |
|
if (key == -1) |
| 517 |
|
{ |
| 518 |
|
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 519 |
|
return -3; |
| 520 |
|
} |
| 521 |
|
|
| 522 |
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 |
| 523 |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 524 |
if (semid == -1) |
if (semid == -1) |
| 539 |
} |
} |
| 540 |
|
|
| 541 |
p_section_list_pool->semid = semid; |
p_section_list_pool->semid = semid; |
| 542 |
|
#endif |
| 543 |
|
|
| 544 |
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(); |
| 545 |
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
| 560 |
|
|
| 561 |
void section_list_cleanup(void) |
void section_list_cleanup(void) |
| 562 |
{ |
{ |
|
int shmid; |
|
|
|
|
| 563 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL) |
| 564 |
{ |
{ |
| 565 |
return; |
return; |
| 577 |
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
| 578 |
} |
} |
| 579 |
|
|
| 580 |
shmid = p_section_list_pool->shmid; |
#ifdef HAVE_SYSTEM_V |
|
|
|
| 581 |
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
| 582 |
{ |
{ |
| 583 |
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); |
| 584 |
} |
} |
| 585 |
|
#else |
| 586 |
if (shmdt(p_section_list_pool) == -1) |
for (int i = 0; i <= BBS_max_section; i++) |
| 587 |
{ |
{ |
| 588 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
if (sem_destroy(&(p_section_list_pool->sem[i])) == -1) |
| 589 |
|
{ |
| 590 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", i, errno); |
| 591 |
|
} |
| 592 |
} |
} |
| 593 |
|
#endif |
| 594 |
|
|
| 595 |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
detach_section_list_shm(); |
| 596 |
|
|
| 597 |
|
if (shm_unlink(section_list_shm_name) == -1 && errno != ENOENT) |
| 598 |
{ |
{ |
| 599 |
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); |
| 600 |
} |
} |
|
|
|
|
p_section_list_pool = NULL; |
|
| 601 |
} |
} |
| 602 |
|
|
| 603 |
int set_section_list_shm_readonly(void) |
int set_section_list_shm_readonly(void) |
| 604 |
{ |
{ |
|
int shmid; |
|
|
void *p_shm; |
|
|
|
|
| 605 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL) |
| 606 |
{ |
{ |
| 607 |
log_error("p_section_list_pool not initialized\n"); |
log_error("p_section_list_pool not initialized\n"); |
| 608 |
return -1; |
return -1; |
| 609 |
} |
} |
| 610 |
|
|
| 611 |
shmid = p_section_list_pool->shmid; |
if (p_section_list_pool != NULL && |
| 612 |
|
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) |
|
| 613 |
{ |
{ |
| 614 |
log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno); |
log_error("mprotect() error (%d)\n", errno); |
| 615 |
return -3; |
return -2; |
| 616 |
} |
} |
| 617 |
|
|
|
p_section_list_pool = p_shm; |
|
|
|
|
| 618 |
return 0; |
return 0; |
| 619 |
} |
} |
| 620 |
|
|
| 621 |
int detach_section_list_shm(void) |
int detach_section_list_shm(void) |
| 622 |
{ |
{ |
| 623 |
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) |
| 624 |
{ |
{ |
| 625 |
log_error("shmdt(section_list_pool) error (%d)\n", errno); |
log_error("munmap() error (%d)\n", errno); |
| 626 |
return -1; |
return -1; |
| 627 |
} |
} |
| 628 |
|
|
| 1117 |
} |
} |
| 1118 |
|
|
| 1119 |
page_count = p_section->page_count - 1 + |
page_count = p_section->page_count - 1 + |
| 1120 |
(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) / |
| 1121 |
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page ? 1 : 0); |
BBS_article_limit_per_page; |
| 1122 |
|
|
| 1123 |
if (page_count < 0) |
if (page_count < 0) |
| 1124 |
{ |
{ |
| 1662 |
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) |
| 1663 |
{ |
{ |
| 1664 |
int index; |
int index; |
| 1665 |
|
#ifdef HAVE_SYSTEM_V |
| 1666 |
struct sembuf sops[4]; |
struct sembuf sops[4]; |
| 1667 |
|
#endif |
| 1668 |
struct timespec timeout; |
struct timespec timeout; |
| 1669 |
int ret; |
int ret = 0; |
| 1670 |
|
|
| 1671 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1672 |
if (index < 0) |
if (index < 0) |
| 1674 |
return -2; |
return -2; |
| 1675 |
} |
} |
| 1676 |
|
|
| 1677 |
|
timeout.tv_sec = wait_sec; |
| 1678 |
|
timeout.tv_nsec = 0; |
| 1679 |
|
|
| 1680 |
|
#ifdef HAVE_SYSTEM_V |
| 1681 |
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 |
| 1682 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1683 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1700 |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1701 |
} |
} |
| 1702 |
|
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
| 1703 |
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); |
| 1704 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1705 |
{ |
{ |
| 1706 |
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
log_error("semop(index = %d, lock read) error %d\n", index, errno); |
| 1707 |
|
} |
| 1708 |
|
#else |
| 1709 |
|
if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1) |
| 1710 |
|
{ |
| 1711 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1712 |
|
{ |
| 1713 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", index, errno); |
| 1714 |
|
} |
| 1715 |
|
return -1; |
| 1716 |
} |
} |
| 1717 |
|
|
| 1718 |
|
if (index != BBS_max_section) |
| 1719 |
|
{ |
| 1720 |
|
if (sem_timedwait(&(p_section_list_pool->sem[BBS_max_section]), &timeout) == -1) |
| 1721 |
|
{ |
| 1722 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1723 |
|
{ |
| 1724 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1725 |
|
} |
| 1726 |
|
// release previously acquired lock |
| 1727 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1728 |
|
{ |
| 1729 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1730 |
|
} |
| 1731 |
|
return -1; |
| 1732 |
|
} |
| 1733 |
|
} |
| 1734 |
|
|
| 1735 |
|
if (p_section_list_pool->write_lock_count[index] == 0 && |
| 1736 |
|
(index != BBS_max_section && p_section_list_pool->write_lock_count[BBS_max_section] == 0)) |
| 1737 |
|
{ |
| 1738 |
|
p_section_list_pool->read_lock_count[index]++; |
| 1739 |
|
if (index != BBS_max_section) |
| 1740 |
|
{ |
| 1741 |
|
p_section_list_pool->read_lock_count[BBS_max_section]++; |
| 1742 |
|
} |
| 1743 |
|
} |
| 1744 |
|
else |
| 1745 |
|
{ |
| 1746 |
|
errno = EAGAIN; |
| 1747 |
|
ret = -1; |
| 1748 |
|
} |
| 1749 |
|
|
| 1750 |
|
if (index != BBS_max_section) |
| 1751 |
|
{ |
| 1752 |
|
// release lock on "all section" |
| 1753 |
|
if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1754 |
|
{ |
| 1755 |
|
log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1756 |
|
ret = -1; |
| 1757 |
|
} |
| 1758 |
|
} |
| 1759 |
|
|
| 1760 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1761 |
|
{ |
| 1762 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1763 |
|
return -1; |
| 1764 |
|
} |
| 1765 |
|
#endif |
| 1766 |
|
|
| 1767 |
return ret; |
return ret; |
| 1768 |
} |
} |
| 1769 |
|
|
| 1770 |
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) |
| 1771 |
{ |
{ |
| 1772 |
int index; |
int index; |
| 1773 |
|
#ifdef HAVE_SYSTEM_V |
| 1774 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
| 1775 |
|
#endif |
| 1776 |
struct timespec timeout; |
struct timespec timeout; |
| 1777 |
int ret; |
int ret = 0; |
| 1778 |
|
|
| 1779 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1780 |
if (index < 0) |
if (index < 0) |
| 1782 |
return -2; |
return -2; |
| 1783 |
} |
} |
| 1784 |
|
|
| 1785 |
|
timeout.tv_sec = wait_sec; |
| 1786 |
|
timeout.tv_nsec = 0; |
| 1787 |
|
|
| 1788 |
|
#ifdef HAVE_SYSTEM_V |
| 1789 |
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 |
| 1790 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1791 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1798 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1799 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 1800 |
|
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
| 1801 |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1802 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1803 |
{ |
{ |
| 1804 |
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
log_error("semop(index = %d, lock write) error %d\n", index, errno); |
| 1805 |
|
} |
| 1806 |
|
#else |
| 1807 |
|
if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1) |
| 1808 |
|
{ |
| 1809 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1810 |
|
{ |
| 1811 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", index, errno); |
| 1812 |
|
} |
| 1813 |
|
return -1; |
| 1814 |
|
} |
| 1815 |
|
|
| 1816 |
|
if (p_section_list_pool->read_lock_count[index] == 0 && p_section_list_pool->write_lock_count[index] == 0) |
| 1817 |
|
{ |
| 1818 |
|
p_section_list_pool->write_lock_count[index]++; |
| 1819 |
|
} |
| 1820 |
|
else |
| 1821 |
|
{ |
| 1822 |
|
errno = EAGAIN; |
| 1823 |
|
ret = -1; |
| 1824 |
|
} |
| 1825 |
|
|
| 1826 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1827 |
|
{ |
| 1828 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1829 |
|
return -1; |
| 1830 |
} |
} |
| 1831 |
|
#endif |
| 1832 |
|
|
| 1833 |
return ret; |
return ret; |
| 1834 |
} |
} |
| 1836 |
int section_list_rd_unlock(SECTION_LIST *p_section) |
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1837 |
{ |
{ |
| 1838 |
int index; |
int index; |
| 1839 |
|
#ifdef HAVE_SYSTEM_V |
| 1840 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 1841 |
int ret; |
#endif |
| 1842 |
|
int ret = 0; |
| 1843 |
|
|
| 1844 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1845 |
if (index < 0) |
if (index < 0) |
| 1847 |
return -2; |
return -2; |
| 1848 |
} |
} |
| 1849 |
|
|
| 1850 |
|
#ifdef HAVE_SYSTEM_V |
| 1851 |
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 |
| 1852 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1853 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1864 |
{ |
{ |
| 1865 |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1866 |
} |
} |
| 1867 |
|
#else |
| 1868 |
|
if (sem_wait(&(p_section_list_pool->sem[index])) == -1) |
| 1869 |
|
{ |
| 1870 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1871 |
|
{ |
| 1872 |
|
log_error("sem_wait(sem[%d]) error %d\n", index, errno); |
| 1873 |
|
} |
| 1874 |
|
return -1; |
| 1875 |
|
} |
| 1876 |
|
|
| 1877 |
|
if (index != BBS_max_section) |
| 1878 |
|
{ |
| 1879 |
|
if (sem_wait(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1880 |
|
{ |
| 1881 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1882 |
|
{ |
| 1883 |
|
log_error("sem_wait(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1884 |
|
} |
| 1885 |
|
// release previously acquired lock |
| 1886 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1887 |
|
{ |
| 1888 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1889 |
|
} |
| 1890 |
|
return -1; |
| 1891 |
|
} |
| 1892 |
|
} |
| 1893 |
|
|
| 1894 |
|
if (p_section_list_pool->read_lock_count[index] > 0) |
| 1895 |
|
{ |
| 1896 |
|
p_section_list_pool->read_lock_count[index]--; |
| 1897 |
|
} |
| 1898 |
|
else |
| 1899 |
|
{ |
| 1900 |
|
log_error("read_lock_count[%d] already 0\n", index); |
| 1901 |
|
} |
| 1902 |
|
|
| 1903 |
|
if (index != BBS_max_section && p_section_list_pool->read_lock_count[BBS_max_section] > 0) |
| 1904 |
|
{ |
| 1905 |
|
p_section_list_pool->read_lock_count[BBS_max_section]--; |
| 1906 |
|
} |
| 1907 |
|
else |
| 1908 |
|
{ |
| 1909 |
|
log_error("read_lock_count[%d] already 0\n", BBS_max_section); |
| 1910 |
|
} |
| 1911 |
|
|
| 1912 |
|
if (index != BBS_max_section) |
| 1913 |
|
{ |
| 1914 |
|
// release lock on "all section" |
| 1915 |
|
if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1916 |
|
{ |
| 1917 |
|
log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1918 |
|
ret = -1; |
| 1919 |
|
} |
| 1920 |
|
} |
| 1921 |
|
|
| 1922 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1923 |
|
{ |
| 1924 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1925 |
|
return -1; |
| 1926 |
|
} |
| 1927 |
|
#endif |
| 1928 |
|
|
| 1929 |
return ret; |
return ret; |
| 1930 |
} |
} |
| 1932 |
int section_list_rw_unlock(SECTION_LIST *p_section) |
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1933 |
{ |
{ |
| 1934 |
int index; |
int index; |
| 1935 |
|
#ifdef HAVE_SYSTEM_V |
| 1936 |
struct sembuf sops[1]; |
struct sembuf sops[1]; |
| 1937 |
int ret; |
#endif |
| 1938 |
|
int ret = 0; |
| 1939 |
|
|
| 1940 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1941 |
if (index < 0) |
if (index < 0) |
| 1943 |
return -2; |
return -2; |
| 1944 |
} |
} |
| 1945 |
|
|
| 1946 |
|
#ifdef HAVE_SYSTEM_V |
| 1947 |
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 |
| 1948 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1949 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1953 |
{ |
{ |
| 1954 |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1955 |
} |
} |
| 1956 |
|
#else |
| 1957 |
|
if (sem_wait(&(p_section_list_pool->sem[index])) == -1) |
| 1958 |
|
{ |
| 1959 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1960 |
|
{ |
| 1961 |
|
log_error("sem_wait(sem[%d]) error %d\n", index, errno); |
| 1962 |
|
} |
| 1963 |
|
return -1; |
| 1964 |
|
} |
| 1965 |
|
|
| 1966 |
|
if (p_section_list_pool->write_lock_count[index] > 0) |
| 1967 |
|
{ |
| 1968 |
|
p_section_list_pool->write_lock_count[index]--; |
| 1969 |
|
} |
| 1970 |
|
else |
| 1971 |
|
{ |
| 1972 |
|
log_error("write_lock_count[%d] already 0\n", index); |
| 1973 |
|
} |
| 1974 |
|
|
| 1975 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1976 |
|
{ |
| 1977 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1978 |
|
return -1; |
| 1979 |
|
} |
| 1980 |
|
#endif |
| 1981 |
|
|
| 1982 |
return ret; |
return ret; |
| 1983 |
} |
} |
| 2002 |
{ |
{ |
| 2003 |
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); |
| 2004 |
} |
} |
| 2005 |
|
usleep(100 * 1000); // 0.1 second |
| 2006 |
} |
} |
| 2007 |
else // failed |
else // failed |
| 2008 |
{ |
{ |
| 2034 |
{ |
{ |
| 2035 |
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); |
| 2036 |
} |
} |
| 2037 |
|
usleep(100 * 1000); // 0.1 second |
| 2038 |
} |
} |
| 2039 |
else // failed |
else // failed |
| 2040 |
{ |
{ |