| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
section_list.c - description |
/* |
| 3 |
------------------- |
* section_list |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - data models and basic operations of section and article |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "log.h" |
#include "log.h" |
| 14 |
#include "section_list.h" |
#include "section_list.h" |
| 15 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 16 |
|
#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_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 |
(p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shmid = shmid; |
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); |
|
|
|
|
if (shmdt(p_article_block_pool) == -1) |
|
|
{ |
|
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
|
|
} |
|
| 243 |
|
|
| 244 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shm_unlink(filepath) == -1 && errno != ENOENT) |
| 245 |
{ |
{ |
| 246 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shm_unlink(%s) error (%d)\n", filepath, errno); |
| 247 |
} |
} |
| 248 |
|
|
| 249 |
p_article_block_pool = NULL; |
detach_article_block_shm(); |
| 250 |
} |
} |
| 251 |
|
|
| 252 |
int set_article_block_shm_readonly(void) |
int set_article_block_shm_readonly(void) |
| 253 |
{ |
{ |
|
int shmid; |
|
|
void *p_shm; |
|
| 254 |
int i; |
int i; |
| 255 |
|
|
| 256 |
if (p_article_block_pool == NULL) |
if (p_article_block_pool == NULL) |
| 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) |
|
// 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) |
|
| 266 |
{ |
{ |
| 267 |
log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno); |
log_error("mprotect() error (%d)\n", errno); |
| 268 |
return -2; |
return -2; |
| 269 |
} |
} |
| 270 |
} |
} |
| 271 |
|
|
| 272 |
|
if (p_article_block_pool != NULL && |
| 273 |
|
mprotect(p_article_block_pool, p_article_block_pool->shm_size, PROT_READ) < 0) |
| 274 |
|
{ |
| 275 |
|
log_error("mprotect() error (%d)\n", errno); |
| 276 |
|
return -3; |
| 277 |
|
} |
| 278 |
|
|
| 279 |
return 0; |
return 0; |
| 280 |
} |
} |
| 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 |
|
|
| 367 |
} |
} |
| 368 |
|
|
| 369 |
left = 0; |
left = 0; |
| 370 |
right = p_article_block_pool->block_count; |
right = p_article_block_pool->block_count - 1; |
| 371 |
|
|
| 372 |
// aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ] |
// aid in the range [ head aid of blocks[left], tail aid of blocks[right] ] |
| 373 |
while (left < right - 1) |
while (left < right) |
| 374 |
{ |
{ |
| 375 |
// get block offset no less than mid value of left and right block offsets |
// get block offset no less than mid value of left and right block offsets |
| 376 |
mid = (left + right) / 2 + (right - left) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
|
|
|
|
if (mid >= p_article_block_pool->block_count) |
|
|
{ |
|
|
log_error("block(mid = %d) is out of boundary\n", mid); |
|
|
return NULL; |
|
|
} |
|
| 377 |
|
|
| 378 |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
| 379 |
{ |
{ |
| 380 |
right = mid; |
right = mid - 1; |
| 381 |
} |
} |
| 382 |
else |
else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid) |
| 383 |
{ |
{ |
| 384 |
left = mid; |
left = mid; |
| 385 |
} |
} |
| 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 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
detach_section_list_shm(); |
| 601 |
|
|
| 602 |
|
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 |
|
|
| 702 |
return p_section; |
return p_section; |
| 703 |
} |
} |
| 704 |
|
|
| 705 |
|
int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list) |
| 706 |
|
{ |
| 707 |
|
int64_t index; |
| 708 |
|
|
| 709 |
|
if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL) |
| 710 |
|
{ |
| 711 |
|
log_error("NULL pointer error\n"); |
| 712 |
|
return -1; |
| 713 |
|
} |
| 714 |
|
|
| 715 |
|
index = get_section_index(p_section); |
| 716 |
|
|
| 717 |
|
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 718 |
|
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 719 |
|
|
| 720 |
|
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1); |
| 721 |
|
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
| 722 |
|
|
| 723 |
|
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
| 724 |
|
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
| 725 |
|
|
| 726 |
|
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0) |
| 727 |
|
{ |
| 728 |
|
log_error("trie_dict_set(section, %s, %d) error\n", sname, index); |
| 729 |
|
return -2; |
| 730 |
|
} |
| 731 |
|
|
| 732 |
|
return 0; |
| 733 |
|
} |
| 734 |
|
|
| 735 |
void section_list_reset_articles(SECTION_LIST *p_section) |
void section_list_reset_articles(SECTION_LIST *p_section) |
| 736 |
{ |
{ |
| 737 |
p_section->article_count = 0; |
p_section->article_count = 0; |
| 747 |
p_section->ontop_article_count = 0; |
p_section->ontop_article_count = 0; |
| 748 |
} |
} |
| 749 |
|
|
| 750 |
SECTION_LIST *section_list_find_by_name(const char *sname, int64_t *p_index) |
SECTION_LIST *section_list_find_by_name(const char *sname) |
| 751 |
{ |
{ |
| 752 |
int64_t index; |
int64_t index; |
| 753 |
int ret; |
int ret; |
| 769 |
return NULL; |
return NULL; |
| 770 |
} |
} |
| 771 |
|
|
|
if (p_index != NULL) |
|
|
{ |
|
|
*p_index = index; |
|
|
} |
|
|
|
|
| 772 |
return (p_section_list_pool->sections + index); |
return (p_section_list_pool->sections + index); |
| 773 |
} |
} |
| 774 |
|
|
| 775 |
SECTION_LIST *section_list_find_by_sid(int32_t sid, int64_t *p_index) |
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 776 |
{ |
{ |
| 777 |
int64_t index; |
int64_t index; |
| 778 |
int ret; |
int ret; |
| 797 |
return NULL; |
return NULL; |
| 798 |
} |
} |
| 799 |
|
|
|
if (p_index != NULL) |
|
|
{ |
|
|
*p_index = index; |
|
|
} |
|
|
|
|
| 800 |
return (p_section_list_pool->sections + index); |
return (p_section_list_pool->sections + index); |
| 801 |
} |
} |
| 802 |
|
|
| 810 |
|
|
| 811 |
if (p_section == NULL || p_article_src == NULL) |
if (p_section == NULL || p_article_src == NULL) |
| 812 |
{ |
{ |
| 813 |
log_error("section_list_append_article() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 814 |
return -1; |
return -1; |
| 815 |
} |
} |
| 816 |
|
|
| 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; |
| 922 |
p_section->p_article_tail = p_article; |
p_section->p_article_tail = p_article; |
| 923 |
|
|
| 924 |
// Update page |
// Update page |
| 925 |
if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) || |
if ((p_article->visible && p_section->last_page_visible_article_count == BBS_article_limit_per_page) || |
| 926 |
p_section->article_count == 1) |
p_section->page_count == 0) |
| 927 |
{ |
{ |
| 928 |
p_section->p_page_first_article[p_section->page_count] = p_article; |
p_section->p_page_first_article[p_section->page_count] = p_article; |
| 929 |
p_section->page_count++; |
p_section->page_count++; |
| 978 |
{ |
{ |
| 979 |
p_section->visible_article_count--; |
p_section->visible_article_count--; |
| 980 |
|
|
| 981 |
|
if (user_article_cnt_inc(p_article->uid, -1) < 0) |
| 982 |
|
{ |
| 983 |
|
log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid); |
| 984 |
|
} |
| 985 |
|
|
| 986 |
if (p_article->tid == 0) |
if (p_article->tid == 0) |
| 987 |
{ |
{ |
| 988 |
p_section->visible_topic_count--; |
p_section->visible_topic_count--; |
| 1001 |
p_reply->visible = 0; |
p_reply->visible = 0; |
| 1002 |
p_section->visible_article_count--; |
p_section->visible_article_count--; |
| 1003 |
affected_count++; |
affected_count++; |
| 1004 |
|
|
| 1005 |
|
if (user_article_cnt_inc(p_reply->uid, -1) < 0) |
| 1006 |
|
{ |
| 1007 |
|
log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid); |
| 1008 |
|
} |
| 1009 |
} |
} |
| 1010 |
} |
} |
| 1011 |
} |
} |
| 1018 |
{ |
{ |
| 1019 |
p_section->visible_topic_count++; |
p_section->visible_topic_count++; |
| 1020 |
} |
} |
| 1021 |
|
|
| 1022 |
|
if (user_article_cnt_inc(p_article->uid, 1) < 0) |
| 1023 |
|
{ |
| 1024 |
|
log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid); |
| 1025 |
|
} |
| 1026 |
} |
} |
| 1027 |
|
|
| 1028 |
p_article->visible = visible; |
p_article->visible = visible; |
| 1121 |
return -1; |
return -1; |
| 1122 |
} |
} |
| 1123 |
|
|
| 1124 |
page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) + |
page_count = p_section->page_count - 1 + |
| 1125 |
(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) / |
| 1126 |
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1); |
BBS_article_limit_per_page; |
| 1127 |
|
|
| 1128 |
|
if (page_count < 0) |
| 1129 |
|
{ |
| 1130 |
|
page_count = 0; |
| 1131 |
|
} |
| 1132 |
|
|
| 1133 |
return page_count; |
return page_count; |
| 1134 |
} |
} |
| 1147 |
} |
} |
| 1148 |
else // if (page_id >= p_section->page_count - 1) |
else // if (page_id >= p_section->page_count - 1) |
| 1149 |
{ |
{ |
| 1150 |
return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count - |
return MIN(MAX(0, |
| 1151 |
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))); |
(p_section->last_page_visible_article_count + p_section->ontop_article_count - |
| 1152 |
|
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))), |
| 1153 |
|
BBS_article_limit_per_page); |
| 1154 |
} |
} |
| 1155 |
} |
} |
| 1156 |
|
|
| 1167 |
|
|
| 1168 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1169 |
{ |
{ |
| 1170 |
log_error("section_list_find_article_with_offset() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1171 |
return NULL; |
return NULL; |
| 1172 |
} |
} |
| 1173 |
|
|
| 1179 |
} |
} |
| 1180 |
|
|
| 1181 |
left = 0; |
left = 0; |
| 1182 |
right = p_section->page_count; |
right = p_section->page_count - 1; |
| 1183 |
|
|
| 1184 |
// aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ] |
// aid in the range [ head aid of pages[left], tail aid of pages[right] ] |
| 1185 |
while (left < right - 1) |
while (left < right) |
| 1186 |
{ |
{ |
| 1187 |
// get page id no less than mid value of left page id and right page id |
// get page id no less than mid value of left page id and right page id |
| 1188 |
mid = (left + right) / 2 + (right - left) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
|
|
|
|
if (mid >= p_section->page_count) |
|
|
{ |
|
|
log_error("page id (mid = %d) is out of boundary\n", mid); |
|
|
return NULL; |
|
|
} |
|
| 1189 |
|
|
| 1190 |
if (aid < p_section->p_page_first_article[mid]->aid) |
if (aid < p_section->p_page_first_article[mid]->aid) |
| 1191 |
{ |
{ |
| 1192 |
right = mid; |
right = mid - 1; |
| 1193 |
} |
} |
| 1194 |
else |
else // if (aid < p_section->p_page_first_article[mid]->aid) |
| 1195 |
{ |
{ |
| 1196 |
left = mid; |
left = mid; |
| 1197 |
} |
} |
| 1253 |
|
|
| 1254 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1255 |
{ |
{ |
| 1256 |
log_error("section_list_calculate_page() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1257 |
return -1; |
return -1; |
| 1258 |
} |
} |
| 1259 |
|
|
| 1342 |
} while (p_article != p_section->p_article_head); |
} while (p_article != p_section->p_article_head); |
| 1343 |
|
|
| 1344 |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
| 1345 |
p_section->last_page_visible_article_count = visible_article_count; |
p_section->last_page_visible_article_count = (visible_article_count > 0 |
| 1346 |
|
? visible_article_count |
| 1347 |
|
: (page > 0 ? BBS_article_limit_per_page : 0)); |
| 1348 |
|
|
| 1349 |
return 0; |
return 0; |
| 1350 |
} |
} |
| 1366 |
return -1; |
return -1; |
| 1367 |
} |
} |
| 1368 |
|
|
| 1369 |
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block + |
| 1370 |
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; |
| 1371 |
|
|
| 1372 |
return ret; |
return ret; |
| 1414 |
|
|
| 1415 |
if (p_section_src == NULL || p_section_dest == NULL) |
if (p_section_src == NULL || p_section_dest == NULL) |
| 1416 |
{ |
{ |
| 1417 |
log_error("section_list_move_topic() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1418 |
return -1; |
return -1; |
| 1419 |
} |
} |
| 1420 |
|
|
| 1627 |
return index; |
return index; |
| 1628 |
} |
} |
| 1629 |
|
|
| 1630 |
|
int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list) |
| 1631 |
|
{ |
| 1632 |
|
if (p_section == NULL) |
| 1633 |
|
{ |
| 1634 |
|
log_error("NULL pointer error\n"); |
| 1635 |
|
return -1; |
| 1636 |
|
} |
| 1637 |
|
|
| 1638 |
|
if (section_list_rd_lock(p_section) < 0) |
| 1639 |
|
{ |
| 1640 |
|
log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid); |
| 1641 |
|
return -2; |
| 1642 |
|
} |
| 1643 |
|
|
| 1644 |
|
if (sname != NULL) |
| 1645 |
|
{ |
| 1646 |
|
memcpy(sname, p_section->sname, sizeof(p_section->sname)); |
| 1647 |
|
} |
| 1648 |
|
if (stitle != NULL) |
| 1649 |
|
{ |
| 1650 |
|
memcpy(stitle, p_section->stitle, sizeof(p_section->stitle)); |
| 1651 |
|
} |
| 1652 |
|
if (master_list != NULL) |
| 1653 |
|
{ |
| 1654 |
|
memcpy(master_list, p_section->master_list, sizeof(p_section->master_list)); |
| 1655 |
|
} |
| 1656 |
|
|
| 1657 |
|
// release lock of section |
| 1658 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1659 |
|
{ |
| 1660 |
|
log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid); |
| 1661 |
|
return -2; |
| 1662 |
|
} |
| 1663 |
|
|
| 1664 |
|
return 0; |
| 1665 |
|
} |
| 1666 |
|
|
| 1667 |
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) |
| 1668 |
{ |
{ |
| 1669 |
int index; |
int index; |
| 1670 |
|
#ifdef HAVE_SYSTEM_V |
| 1671 |
struct sembuf sops[4]; |
struct sembuf sops[4]; |
| 1672 |
|
#endif |
| 1673 |
struct timespec timeout; |
struct timespec timeout; |
| 1674 |
int ret; |
int ret = 0; |
| 1675 |
|
|
| 1676 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1677 |
if (index < 0) |
if (index < 0) |
| 1679 |
return -2; |
return -2; |
| 1680 |
} |
} |
| 1681 |
|
|
| 1682 |
|
timeout.tv_sec = wait_sec; |
| 1683 |
|
timeout.tv_nsec = 0; |
| 1684 |
|
|
| 1685 |
|
#ifdef HAVE_SYSTEM_V |
| 1686 |
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 |
| 1687 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1688 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1705 |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1706 |
} |
} |
| 1707 |
|
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
| 1708 |
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); |
| 1709 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1710 |
{ |
{ |
| 1711 |
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
log_error("semop(index = %d, lock read) error %d\n", index, errno); |
| 1712 |
|
} |
| 1713 |
|
#else |
| 1714 |
|
if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1) |
| 1715 |
|
{ |
| 1716 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1717 |
|
{ |
| 1718 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", index, errno); |
| 1719 |
|
} |
| 1720 |
|
return -1; |
| 1721 |
|
} |
| 1722 |
|
|
| 1723 |
|
if (index != BBS_max_section) |
| 1724 |
|
{ |
| 1725 |
|
if (sem_timedwait(&(p_section_list_pool->sem[BBS_max_section]), &timeout) == -1) |
| 1726 |
|
{ |
| 1727 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1728 |
|
{ |
| 1729 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1730 |
|
} |
| 1731 |
|
// release previously acquired lock |
| 1732 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1733 |
|
{ |
| 1734 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1735 |
|
} |
| 1736 |
|
return -1; |
| 1737 |
|
} |
| 1738 |
|
} |
| 1739 |
|
|
| 1740 |
|
if (p_section_list_pool->write_lock_count[index] == 0 && |
| 1741 |
|
(index != BBS_max_section && p_section_list_pool->write_lock_count[BBS_max_section] == 0)) |
| 1742 |
|
{ |
| 1743 |
|
p_section_list_pool->read_lock_count[index]++; |
| 1744 |
|
if (index != BBS_max_section) |
| 1745 |
|
{ |
| 1746 |
|
p_section_list_pool->read_lock_count[BBS_max_section]++; |
| 1747 |
|
} |
| 1748 |
|
} |
| 1749 |
|
else |
| 1750 |
|
{ |
| 1751 |
|
errno = EAGAIN; |
| 1752 |
|
ret = -1; |
| 1753 |
|
} |
| 1754 |
|
|
| 1755 |
|
if (index != BBS_max_section) |
| 1756 |
|
{ |
| 1757 |
|
// release lock on "all section" |
| 1758 |
|
if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1759 |
|
{ |
| 1760 |
|
log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1761 |
|
ret = -1; |
| 1762 |
|
} |
| 1763 |
} |
} |
| 1764 |
|
|
| 1765 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1766 |
|
{ |
| 1767 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1768 |
|
return -1; |
| 1769 |
|
} |
| 1770 |
|
#endif |
| 1771 |
|
|
| 1772 |
return ret; |
return ret; |
| 1773 |
} |
} |
| 1774 |
|
|
| 1775 |
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) |
| 1776 |
{ |
{ |
| 1777 |
int index; |
int index; |
| 1778 |
|
#ifdef HAVE_SYSTEM_V |
| 1779 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
| 1780 |
|
#endif |
| 1781 |
struct timespec timeout; |
struct timespec timeout; |
| 1782 |
int ret; |
int ret = 0; |
| 1783 |
|
|
| 1784 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1785 |
if (index < 0) |
if (index < 0) |
| 1787 |
return -2; |
return -2; |
| 1788 |
} |
} |
| 1789 |
|
|
| 1790 |
|
timeout.tv_sec = wait_sec; |
| 1791 |
|
timeout.tv_nsec = 0; |
| 1792 |
|
|
| 1793 |
|
#ifdef HAVE_SYSTEM_V |
| 1794 |
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 |
| 1795 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1796 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1803 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1804 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 1805 |
|
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
| 1806 |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1807 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1808 |
{ |
{ |
| 1809 |
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
log_error("semop(index = %d, lock write) error %d\n", index, errno); |
| 1810 |
} |
} |
| 1811 |
|
#else |
| 1812 |
|
if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1) |
| 1813 |
|
{ |
| 1814 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1815 |
|
{ |
| 1816 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", index, errno); |
| 1817 |
|
} |
| 1818 |
|
return -1; |
| 1819 |
|
} |
| 1820 |
|
|
| 1821 |
|
if (p_section_list_pool->read_lock_count[index] == 0 && p_section_list_pool->write_lock_count[index] == 0) |
| 1822 |
|
{ |
| 1823 |
|
p_section_list_pool->write_lock_count[index]++; |
| 1824 |
|
} |
| 1825 |
|
else |
| 1826 |
|
{ |
| 1827 |
|
errno = EAGAIN; |
| 1828 |
|
ret = -1; |
| 1829 |
|
} |
| 1830 |
|
|
| 1831 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1832 |
|
{ |
| 1833 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1834 |
|
return -1; |
| 1835 |
|
} |
| 1836 |
|
#endif |
| 1837 |
|
|
| 1838 |
return ret; |
return ret; |
| 1839 |
} |
} |
| 1841 |
int section_list_rd_unlock(SECTION_LIST *p_section) |
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1842 |
{ |
{ |
| 1843 |
int index; |
int index; |
| 1844 |
|
#ifdef HAVE_SYSTEM_V |
| 1845 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 1846 |
int ret; |
#endif |
| 1847 |
|
int ret = 0; |
| 1848 |
|
|
| 1849 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1850 |
if (index < 0) |
if (index < 0) |
| 1852 |
return -2; |
return -2; |
| 1853 |
} |
} |
| 1854 |
|
|
| 1855 |
|
#ifdef HAVE_SYSTEM_V |
| 1856 |
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 |
| 1857 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1858 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1869 |
{ |
{ |
| 1870 |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1871 |
} |
} |
| 1872 |
|
#else |
| 1873 |
|
if (sem_wait(&(p_section_list_pool->sem[index])) == -1) |
| 1874 |
|
{ |
| 1875 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1876 |
|
{ |
| 1877 |
|
log_error("sem_wait(sem[%d]) error %d\n", index, errno); |
| 1878 |
|
} |
| 1879 |
|
return -1; |
| 1880 |
|
} |
| 1881 |
|
|
| 1882 |
|
if (index != BBS_max_section) |
| 1883 |
|
{ |
| 1884 |
|
if (sem_wait(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1885 |
|
{ |
| 1886 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1887 |
|
{ |
| 1888 |
|
log_error("sem_wait(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1889 |
|
} |
| 1890 |
|
// release previously acquired lock |
| 1891 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1892 |
|
{ |
| 1893 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1894 |
|
} |
| 1895 |
|
return -1; |
| 1896 |
|
} |
| 1897 |
|
} |
| 1898 |
|
|
| 1899 |
|
if (p_section_list_pool->read_lock_count[index] > 0) |
| 1900 |
|
{ |
| 1901 |
|
p_section_list_pool->read_lock_count[index]--; |
| 1902 |
|
} |
| 1903 |
|
else |
| 1904 |
|
{ |
| 1905 |
|
log_error("read_lock_count[%d] already 0\n", index); |
| 1906 |
|
} |
| 1907 |
|
|
| 1908 |
|
if (index != BBS_max_section && p_section_list_pool->read_lock_count[BBS_max_section] > 0) |
| 1909 |
|
{ |
| 1910 |
|
p_section_list_pool->read_lock_count[BBS_max_section]--; |
| 1911 |
|
} |
| 1912 |
|
else |
| 1913 |
|
{ |
| 1914 |
|
log_error("read_lock_count[%d] already 0\n", BBS_max_section); |
| 1915 |
|
} |
| 1916 |
|
|
| 1917 |
|
if (index != BBS_max_section) |
| 1918 |
|
{ |
| 1919 |
|
// release lock on "all section" |
| 1920 |
|
if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1921 |
|
{ |
| 1922 |
|
log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1923 |
|
ret = -1; |
| 1924 |
|
} |
| 1925 |
|
} |
| 1926 |
|
|
| 1927 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1928 |
|
{ |
| 1929 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1930 |
|
return -1; |
| 1931 |
|
} |
| 1932 |
|
#endif |
| 1933 |
|
|
| 1934 |
return ret; |
return ret; |
| 1935 |
} |
} |
| 1937 |
int section_list_rw_unlock(SECTION_LIST *p_section) |
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1938 |
{ |
{ |
| 1939 |
int index; |
int index; |
| 1940 |
|
#ifdef HAVE_SYSTEM_V |
| 1941 |
struct sembuf sops[1]; |
struct sembuf sops[1]; |
| 1942 |
int ret; |
#endif |
| 1943 |
|
int ret = 0; |
| 1944 |
|
|
| 1945 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1946 |
if (index < 0) |
if (index < 0) |
| 1948 |
return -2; |
return -2; |
| 1949 |
} |
} |
| 1950 |
|
|
| 1951 |
|
#ifdef HAVE_SYSTEM_V |
| 1952 |
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 |
| 1953 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1954 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1958 |
{ |
{ |
| 1959 |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1960 |
} |
} |
| 1961 |
|
#else |
| 1962 |
|
if (sem_wait(&(p_section_list_pool->sem[index])) == -1) |
| 1963 |
|
{ |
| 1964 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1965 |
|
{ |
| 1966 |
|
log_error("sem_wait(sem[%d]) error %d\n", index, errno); |
| 1967 |
|
} |
| 1968 |
|
return -1; |
| 1969 |
|
} |
| 1970 |
|
|
| 1971 |
|
if (p_section_list_pool->write_lock_count[index] > 0) |
| 1972 |
|
{ |
| 1973 |
|
p_section_list_pool->write_lock_count[index]--; |
| 1974 |
|
} |
| 1975 |
|
else |
| 1976 |
|
{ |
| 1977 |
|
log_error("write_lock_count[%d] already 0\n", index); |
| 1978 |
|
} |
| 1979 |
|
|
| 1980 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1981 |
|
{ |
| 1982 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1983 |
|
return -1; |
| 1984 |
|
} |
| 1985 |
|
#endif |
| 1986 |
|
|
| 1987 |
return ret; |
return ret; |
| 1988 |
} |
} |
| 1992 |
int timer = 0; |
int timer = 0; |
| 1993 |
int sid = (p_section == NULL ? 0 : p_section->sid); |
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1994 |
int ret = -1; |
int ret = -1; |
| 1995 |
|
time_t tm_first_failure = 0; |
| 1996 |
|
|
| 1997 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 1998 |
{ |
{ |
| 2003 |
} |
} |
| 2004 |
else if (errno == EAGAIN || errno == EINTR) // retry |
else if (errno == EAGAIN || errno == EINTR) // retry |
| 2005 |
{ |
{ |
| 2006 |
|
// Dead lock detection |
| 2007 |
|
if (tm_first_failure == 0) |
| 2008 |
|
{ |
| 2009 |
|
time(&tm_first_failure); |
| 2010 |
|
} |
| 2011 |
|
|
| 2012 |
timer++; |
timer++; |
| 2013 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 2014 |
{ |
{ |
| 2015 |
log_error("section_list_try_rd_lock() tried %d times on section %d\n", sid, timer); |
log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid); |
| 2016 |
|
if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT) |
| 2017 |
|
{ |
| 2018 |
|
log_error("Unable to acquire rd_lock for %d seconds\n", time(NULL) - tm_first_failure); |
| 2019 |
|
#ifndef HAVE_SYSTEM_V |
| 2020 |
|
section_list_reset_lock(p_section); |
| 2021 |
|
log_error("Reset POSIX semaphore to resolve dead lock\n"); |
| 2022 |
|
#endif |
| 2023 |
|
break; |
| 2024 |
|
} |
| 2025 |
} |
} |
| 2026 |
|
usleep(100 * 1000); // 0.1 second |
| 2027 |
} |
} |
| 2028 |
else // failed |
else // failed |
| 2029 |
{ |
{ |
| 2040 |
int timer = 0; |
int timer = 0; |
| 2041 |
int sid = (p_section == NULL ? 0 : p_section->sid); |
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 2042 |
int ret = -1; |
int ret = -1; |
| 2043 |
|
time_t tm_first_failure = 0; |
| 2044 |
|
|
| 2045 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 2046 |
{ |
{ |
| 2051 |
} |
} |
| 2052 |
else if (errno == EAGAIN || errno == EINTR) // retry |
else if (errno == EAGAIN || errno == EINTR) // retry |
| 2053 |
{ |
{ |
| 2054 |
|
// Dead lock detection |
| 2055 |
|
if (tm_first_failure == 0) |
| 2056 |
|
{ |
| 2057 |
|
time(&tm_first_failure); |
| 2058 |
|
} |
| 2059 |
|
|
| 2060 |
timer++; |
timer++; |
| 2061 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 2062 |
{ |
{ |
| 2063 |
log_error("section_list_try_rw_lock() tried %d times on section %d\n", sid, timer); |
log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid); |
| 2064 |
|
if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT) |
| 2065 |
|
{ |
| 2066 |
|
log_error("Unable to acquire rw_lock for %d seconds\n", time(NULL) - tm_first_failure); |
| 2067 |
|
#ifndef HAVE_SYSTEM_V |
| 2068 |
|
section_list_reset_lock(p_section); |
| 2069 |
|
log_error("Reset POSIX semaphore to resolve dead lock\n"); |
| 2070 |
|
#endif |
| 2071 |
|
break; |
| 2072 |
|
} |
| 2073 |
} |
} |
| 2074 |
|
usleep(100 * 1000); // 0.1 second |
| 2075 |
} |
} |
| 2076 |
else // failed |
else // failed |
| 2077 |
{ |
{ |
| 2082 |
|
|
| 2083 |
return ret; |
return ret; |
| 2084 |
} |
} |
| 2085 |
|
|
| 2086 |
|
#ifndef HAVE_SYSTEM_V |
| 2087 |
|
int section_list_reset_lock(SECTION_LIST *p_section) |
| 2088 |
|
{ |
| 2089 |
|
int index; |
| 2090 |
|
|
| 2091 |
|
if (p_section == NULL) |
| 2092 |
|
{ |
| 2093 |
|
log_error("NULL pointer error\n"); |
| 2094 |
|
return -1; |
| 2095 |
|
} |
| 2096 |
|
|
| 2097 |
|
index = get_section_index(p_section); |
| 2098 |
|
if (index < 0) |
| 2099 |
|
{ |
| 2100 |
|
return -2; |
| 2101 |
|
} |
| 2102 |
|
|
| 2103 |
|
if (sem_destroy(&(p_section_list_pool->sem[index])) == -1) |
| 2104 |
|
{ |
| 2105 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", index, errno); |
| 2106 |
|
} |
| 2107 |
|
|
| 2108 |
|
p_section_list_pool->read_lock_count[index] = 0; |
| 2109 |
|
p_section_list_pool->write_lock_count[index] = 0; |
| 2110 |
|
|
| 2111 |
|
if (sem_init(&(p_section_list_pool->sem[index]), 1, 1) == -1) |
| 2112 |
|
{ |
| 2113 |
|
log_error("sem_init(sem[%d]) error (%d)\n", index, errno); |
| 2114 |
|
} |
| 2115 |
|
|
| 2116 |
|
if (index != BBS_max_section) |
| 2117 |
|
{ |
| 2118 |
|
if (sem_destroy(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 2119 |
|
{ |
| 2120 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", BBS_max_section, errno); |
| 2121 |
|
} |
| 2122 |
|
|
| 2123 |
|
p_section_list_pool->read_lock_count[BBS_max_section] = 0; |
| 2124 |
|
p_section_list_pool->write_lock_count[BBS_max_section] = 0; |
| 2125 |
|
|
| 2126 |
|
if (sem_init(&(p_section_list_pool->sem[BBS_max_section]), 1, 1) == -1) |
| 2127 |
|
{ |
| 2128 |
|
log_error("sem_init(sem[%d]) error (%d)\n", BBS_max_section, errno); |
| 2129 |
|
} |
| 2130 |
|
} |
| 2131 |
|
|
| 2132 |
|
return 0; |
| 2133 |
|
} |
| 2134 |
|
#endif |