| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#include "section_list.h" |
|
| 17 |
#include "log.h" |
#include "log.h" |
| 18 |
|
#include "section_list.h" |
| 19 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 20 |
|
#include <errno.h> |
| 21 |
|
#include <signal.h> |
| 22 |
#include <stdio.h> |
#include <stdio.h> |
| 23 |
|
#include <stdlib.h> |
| 24 |
#include <string.h> |
#include <string.h> |
|
#include <signal.h> |
|
| 25 |
#include <unistd.h> |
#include <unistd.h> |
| 26 |
#include <stdlib.h> |
#include <sys/ipc.h> |
|
#include <errno.h> |
|
| 27 |
#include <sys/param.h> |
#include <sys/param.h> |
| 28 |
|
#include <sys/sem.h> |
| 29 |
#include <sys/shm.h> |
#include <sys/shm.h> |
|
#include <sys/ipc.h> |
|
| 30 |
|
|
| 31 |
#define ARTICLE_BLOCK_PER_SHM 400 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
#ifdef _SEM_SEMUN_UNDEFINED |
| 32 |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
union semun |
| 33 |
|
{ |
| 34 |
|
int val; /* Value for SETVAL */ |
| 35 |
|
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
| 36 |
|
unsigned short *array; /* Array for GETALL, SETALL */ |
| 37 |
|
struct seminfo *__buf; /* Buffer for IPC_INFO |
| 38 |
|
(Linux-specific) */ |
| 39 |
|
}; |
| 40 |
|
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
| 41 |
|
|
| 42 |
|
#define SECTION_TRY_LOCK_WAIT_TIME 1 // second |
| 43 |
|
#define SECTION_TRY_LOCK_TIMES 10 |
| 44 |
|
|
| 45 |
|
#define ARTICLE_BLOCK_PER_SHM 1000 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
| 46 |
|
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
| 47 |
#define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT) |
#define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT) |
| 48 |
|
|
| 49 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections |
| 50 |
|
|
| 51 |
|
#define SID_STR_LEN 5 // 32-bit + NULL |
| 52 |
|
|
| 53 |
struct article_block_t |
struct article_block_t |
| 54 |
{ |
{ |
| 55 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
| 56 |
int32_t article_count; |
int article_count; |
| 57 |
struct article_block_t *p_next_block; |
struct article_block_t *p_next_block; |
| 58 |
}; |
}; |
| 59 |
typedef struct article_block_t ARTICLE_BLOCK; |
typedef struct article_block_t ARTICLE_BLOCK; |
| 60 |
|
|
|
struct article_block_shm_t |
|
|
{ |
|
|
int shmid; |
|
|
void *p_shm; |
|
|
}; |
|
|
typedef struct article_block_shm_t ARTICLE_BLOCK_SHM; |
|
|
|
|
| 61 |
struct article_block_pool_t |
struct article_block_pool_t |
| 62 |
{ |
{ |
| 63 |
ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
int shmid; |
| 64 |
|
struct |
| 65 |
|
{ |
| 66 |
|
int shmid; |
| 67 |
|
void *p_shm; |
| 68 |
|
} shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
| 69 |
int shm_count; |
int shm_count; |
| 70 |
ARTICLE_BLOCK *p_block_free_list; |
ARTICLE_BLOCK *p_block_free_list; |
| 71 |
ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL]; |
ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL]; |
| 72 |
int32_t block_count; |
int block_count; |
| 73 |
}; |
}; |
| 74 |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
| 75 |
|
|
| 76 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 77 |
|
|
| 78 |
static SECTION_LIST *p_section_list_pool = NULL; |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
|
static int section_list_count = 0; |
|
|
static TRIE_NODE *p_trie_dict_section_list = NULL; |
|
| 79 |
|
|
| 80 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 81 |
{ |
{ |
| 95 |
return -1; |
return -1; |
| 96 |
} |
} |
| 97 |
|
|
| 98 |
if (block_count > ARTICLE_BLOCK_PER_POOL) |
if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL) |
| 99 |
{ |
{ |
| 100 |
log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL); |
log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL); |
| 101 |
return -2; |
return -2; |
| 102 |
} |
} |
| 103 |
|
|
| 104 |
p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL)); |
// Allocate shared memory |
| 105 |
if (p_article_block_pool == NULL) |
proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm |
| 106 |
|
key = ftok(filename, proj_id); |
| 107 |
|
if (key == -1) |
| 108 |
{ |
{ |
| 109 |
log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n"); |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 110 |
return -2; |
return -3; |
| 111 |
} |
} |
| 112 |
|
|
| 113 |
// Allocate shared memory |
size = sizeof(ARTICLE_BLOCK_POOL); |
| 114 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 115 |
|
if (shmid == -1) |
| 116 |
|
{ |
| 117 |
|
log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno); |
| 118 |
|
return -3; |
| 119 |
|
} |
| 120 |
|
p_shm = shmat(shmid, NULL, 0); |
| 121 |
|
if (p_shm == (void *)-1) |
| 122 |
|
{ |
| 123 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 124 |
|
return -3; |
| 125 |
|
} |
| 126 |
|
|
| 127 |
|
p_article_block_pool = p_shm; |
| 128 |
|
p_article_block_pool->shmid = shmid; |
| 129 |
|
|
| 130 |
p_article_block_pool->shm_count = 0; |
p_article_block_pool->shm_count = 0; |
| 131 |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
| 132 |
|
|
| 135 |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
| 136 |
block_count -= block_count_in_shm; |
block_count -= block_count_in_shm; |
| 137 |
|
|
| 138 |
proj_id = getpid() + p_article_block_pool->shm_count; |
proj_id = p_article_block_pool->shm_count; |
| 139 |
key = ftok(filename, proj_id); |
key = ftok(filename, proj_id); |
| 140 |
if (key == -1) |
if (key == -1) |
| 141 |
{ |
{ |
| 144 |
return -3; |
return -3; |
| 145 |
} |
} |
| 146 |
|
|
| 147 |
size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
| 148 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 149 |
if (shmid == -1) |
if (shmid == -1) |
| 150 |
{ |
{ |
| 188 |
|
|
| 189 |
void article_block_cleanup(void) |
void article_block_cleanup(void) |
| 190 |
{ |
{ |
| 191 |
if (p_article_block_pool != NULL) |
int shmid; |
| 192 |
|
|
| 193 |
|
if (p_article_block_pool == NULL) |
| 194 |
|
{ |
| 195 |
|
return; |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 199 |
{ |
{ |
| 200 |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
| 201 |
{ |
{ |
| 202 |
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
| 203 |
{ |
} |
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
|
|
} |
|
| 204 |
|
|
| 205 |
if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1) |
if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1) |
| 206 |
{ |
{ |
| 207 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
| 208 |
} |
} |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
shmid = p_article_block_pool->shmid; |
| 212 |
|
|
| 213 |
|
if (shmdt(p_article_block_pool) == -1) |
| 214 |
|
{ |
| 215 |
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
| 219 |
|
{ |
| 220 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 221 |
|
} |
| 222 |
|
|
| 223 |
|
p_article_block_pool = NULL; |
| 224 |
|
} |
| 225 |
|
|
| 226 |
|
int set_article_block_shm_readonly(void) |
| 227 |
|
{ |
| 228 |
|
int shmid; |
| 229 |
|
void *p_shm; |
| 230 |
|
int i; |
| 231 |
|
|
| 232 |
|
if (p_article_block_pool == NULL) |
| 233 |
|
{ |
| 234 |
|
log_error("article_block_pool not initialized\n"); |
| 235 |
|
return -1; |
| 236 |
|
} |
| 237 |
|
|
| 238 |
|
for (i = 0; i < p_article_block_pool->shm_count; i++) |
| 239 |
|
{ |
| 240 |
|
shmid = (p_article_block_pool->shm_pool + i)->shmid; |
| 241 |
|
|
| 242 |
|
// Remap shared memory in read-only mode |
| 243 |
|
p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP); |
| 244 |
|
if (p_shm == (void *)-1) |
| 245 |
|
{ |
| 246 |
|
log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno); |
| 247 |
|
return -2; |
| 248 |
} |
} |
| 249 |
|
} |
| 250 |
|
|
| 251 |
|
return 0; |
| 252 |
|
} |
| 253 |
|
|
| 254 |
free(p_article_block_pool); |
int detach_article_block_shm(void) |
| 255 |
p_article_block_pool = NULL; |
{ |
| 256 |
|
int shmid; |
| 257 |
|
|
| 258 |
|
if (p_article_block_pool == NULL) |
| 259 |
|
{ |
| 260 |
|
return -1; |
| 261 |
} |
} |
| 262 |
|
|
| 263 |
|
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 264 |
|
{ |
| 265 |
|
if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
| 266 |
|
{ |
| 267 |
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
| 268 |
|
return -2; |
| 269 |
|
} |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
shmid = p_article_block_pool->shmid; |
| 273 |
|
|
| 274 |
|
if (shmdt(p_article_block_pool) == -1) |
| 275 |
|
{ |
| 276 |
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 277 |
|
return -3; |
| 278 |
|
} |
| 279 |
|
|
| 280 |
|
p_article_block_pool = NULL; |
| 281 |
|
|
| 282 |
|
return 0; |
| 283 |
} |
} |
| 284 |
|
|
| 285 |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
| 386 |
} |
} |
| 387 |
} |
} |
| 388 |
|
|
| 389 |
return (p_block->articles + left); |
if (aid != p_block->articles[left].aid) // not found |
| 390 |
|
{ |
| 391 |
|
return NULL; |
| 392 |
|
} |
| 393 |
|
|
| 394 |
|
return (p_block->articles + left); // found |
| 395 |
} |
} |
| 396 |
|
|
| 397 |
ARTICLE *article_block_find_by_index(int index) |
ARTICLE *article_block_find_by_index(int index) |
| 406 |
|
|
| 407 |
if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count) |
if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count) |
| 408 |
{ |
{ |
| 409 |
log_error("section_data_find_article_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); |
| 410 |
return NULL; |
return NULL; |
| 411 |
} |
} |
| 412 |
|
|
| 414 |
|
|
| 415 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
| 416 |
{ |
{ |
| 417 |
log_error("section_data_find_article_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); |
| 418 |
return NULL; |
return NULL; |
| 419 |
} |
} |
| 420 |
|
|
| 421 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
| 422 |
} |
} |
| 423 |
|
|
| 424 |
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name) |
extern int section_list_init(const char *filename) |
| 425 |
{ |
{ |
| 426 |
SECTION_LIST *p_section; |
int semid; |
| 427 |
|
int shmid; |
| 428 |
|
int proj_id; |
| 429 |
|
key_t key; |
| 430 |
|
size_t size; |
| 431 |
|
void *p_shm; |
| 432 |
|
union semun arg; |
| 433 |
|
int i; |
| 434 |
|
|
| 435 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool != NULL) |
| 436 |
{ |
{ |
| 437 |
p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST)); |
section_list_cleanup(); |
| 438 |
if (p_section_list_pool == NULL) |
} |
|
{ |
|
|
log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section); |
|
|
return NULL; |
|
|
} |
|
| 439 |
|
|
| 440 |
section_list_count = 0; |
proj_id = (int)(time(NULL) % getpid()); |
| 441 |
|
key = ftok(filename, proj_id); |
| 442 |
|
if (key == -1) |
| 443 |
|
{ |
| 444 |
|
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 445 |
|
return -3; |
| 446 |
} |
} |
| 447 |
|
|
| 448 |
if (p_trie_dict_section_list == NULL) |
// Allocate shared memory |
| 449 |
|
size = sizeof(SECTION_LIST_POOL); |
| 450 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 451 |
|
if (shmid == -1) |
| 452 |
{ |
{ |
| 453 |
p_trie_dict_section_list = trie_dict_create(); |
log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno); |
| 454 |
if (p_trie_dict_section_list == NULL) |
return -3; |
| 455 |
|
} |
| 456 |
|
p_shm = shmat(shmid, NULL, 0); |
| 457 |
|
if (p_shm == (void *)-1) |
| 458 |
|
{ |
| 459 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 460 |
|
return -3; |
| 461 |
|
} |
| 462 |
|
|
| 463 |
|
p_section_list_pool = p_shm; |
| 464 |
|
p_section_list_pool->shmid = shmid; |
| 465 |
|
p_section_list_pool->section_count = 0; |
| 466 |
|
|
| 467 |
|
// Allocate semaphore as section locks |
| 468 |
|
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
| 469 |
|
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 470 |
|
if (semid == -1) |
| 471 |
|
{ |
| 472 |
|
log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno); |
| 473 |
|
return -3; |
| 474 |
|
} |
| 475 |
|
|
| 476 |
|
// Initialize sem value to 0 |
| 477 |
|
arg.val = 0; |
| 478 |
|
for (i = 0; i < size; i++) |
| 479 |
|
{ |
| 480 |
|
if (semctl(semid, i, SETVAL, arg) == -1) |
| 481 |
{ |
{ |
| 482 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno); |
| 483 |
return NULL; |
return -3; |
| 484 |
} |
} |
| 485 |
} |
} |
| 486 |
|
|
| 487 |
if (section_list_count >= BBS_max_section) |
p_section_list_pool->semid = semid; |
| 488 |
|
|
| 489 |
|
p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create(); |
| 490 |
|
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
| 491 |
{ |
{ |
| 492 |
log_error("section_list_count exceed limit %d\n", BBS_max_section); |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 493 |
|
return -2; |
| 494 |
|
} |
| 495 |
|
|
| 496 |
|
p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create(); |
| 497 |
|
if (p_section_list_pool->p_trie_dict_section_by_sid == NULL) |
| 498 |
|
{ |
| 499 |
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 500 |
|
return -2; |
| 501 |
|
} |
| 502 |
|
|
| 503 |
|
return 0; |
| 504 |
|
} |
| 505 |
|
|
| 506 |
|
void section_list_cleanup(void) |
| 507 |
|
{ |
| 508 |
|
int shmid; |
| 509 |
|
|
| 510 |
|
if (p_section_list_pool == NULL) |
| 511 |
|
{ |
| 512 |
|
return; |
| 513 |
|
} |
| 514 |
|
|
| 515 |
|
if (p_section_list_pool->p_trie_dict_section_by_name != NULL) |
| 516 |
|
{ |
| 517 |
|
trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name); |
| 518 |
|
p_section_list_pool->p_trie_dict_section_by_name = NULL; |
| 519 |
|
} |
| 520 |
|
|
| 521 |
|
if (p_section_list_pool->p_trie_dict_section_by_sid != NULL) |
| 522 |
|
{ |
| 523 |
|
trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid); |
| 524 |
|
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
| 525 |
|
} |
| 526 |
|
|
| 527 |
|
shmid = p_section_list_pool->shmid; |
| 528 |
|
|
| 529 |
|
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
| 530 |
|
{ |
| 531 |
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno); |
| 532 |
|
} |
| 533 |
|
|
| 534 |
|
if (shmdt(p_section_list_pool) == -1) |
| 535 |
|
{ |
| 536 |
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 537 |
|
} |
| 538 |
|
|
| 539 |
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
| 540 |
|
{ |
| 541 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 542 |
|
} |
| 543 |
|
|
| 544 |
|
p_section_list_pool = NULL; |
| 545 |
|
} |
| 546 |
|
|
| 547 |
|
int set_section_list_shm_readonly(void) |
| 548 |
|
{ |
| 549 |
|
int shmid; |
| 550 |
|
void *p_shm; |
| 551 |
|
|
| 552 |
|
if (p_section_list_pool == NULL) |
| 553 |
|
{ |
| 554 |
|
log_error("p_section_list_pool not initialized\n"); |
| 555 |
|
return -1; |
| 556 |
|
} |
| 557 |
|
|
| 558 |
|
shmid = p_section_list_pool->shmid; |
| 559 |
|
|
| 560 |
|
// Remap shared memory in read-only mode |
| 561 |
|
p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP); |
| 562 |
|
if (p_shm == (void *)-1) |
| 563 |
|
{ |
| 564 |
|
log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno); |
| 565 |
|
return -3; |
| 566 |
|
} |
| 567 |
|
|
| 568 |
|
p_section_list_pool = p_shm; |
| 569 |
|
|
| 570 |
|
return 0; |
| 571 |
|
} |
| 572 |
|
|
| 573 |
|
int detach_section_list_shm(void) |
| 574 |
|
{ |
| 575 |
|
if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1) |
| 576 |
|
{ |
| 577 |
|
log_error("shmdt(section_list_pool) error (%d)\n", errno); |
| 578 |
|
return -1; |
| 579 |
|
} |
| 580 |
|
|
| 581 |
|
p_section_list_pool = NULL; |
| 582 |
|
|
| 583 |
|
return 0; |
| 584 |
|
} |
| 585 |
|
|
| 586 |
|
inline static void sid_to_str(int32_t sid, char *p_sid_str) |
| 587 |
|
{ |
| 588 |
|
uint32_t u_sid; |
| 589 |
|
int i; |
| 590 |
|
|
| 591 |
|
u_sid = (uint32_t)sid; |
| 592 |
|
for (i = 0; i < SID_STR_LEN - 1; i++) |
| 593 |
|
{ |
| 594 |
|
p_sid_str[i] = (char)(u_sid % 255 + 1); |
| 595 |
|
u_sid /= 255; |
| 596 |
|
} |
| 597 |
|
p_sid_str[i] = '\0'; |
| 598 |
|
} |
| 599 |
|
|
| 600 |
|
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list) |
| 601 |
|
{ |
| 602 |
|
SECTION_LIST *p_section; |
| 603 |
|
char sid_str[SID_STR_LEN]; |
| 604 |
|
|
| 605 |
|
if (p_section_list_pool == NULL) |
| 606 |
|
{ |
| 607 |
|
log_error("session_list_pool not initialized\n"); |
| 608 |
|
return NULL; |
| 609 |
|
} |
| 610 |
|
|
| 611 |
|
if (p_section_list_pool->section_count >= BBS_max_section) |
| 612 |
|
{ |
| 613 |
|
log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section); |
| 614 |
return NULL; |
return NULL; |
| 615 |
} |
} |
| 616 |
|
|
| 617 |
p_section = p_section_list_pool + section_list_count; |
sid_to_str(sid, sid_str); |
| 618 |
|
|
| 619 |
|
p_section = p_section_list_pool->sections + p_section_list_pool->section_count; |
| 620 |
|
|
| 621 |
p_section->sid = sid; |
p_section->sid = sid; |
| 622 |
|
p_section->ex_menu_tm = 0; |
| 623 |
|
|
| 624 |
strncpy(p_section->sname, sname, sizeof(p_section->sname - 1)); |
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 625 |
p_section->sname[sizeof(p_section->sname - 1)] = '\0'; |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 626 |
|
|
| 627 |
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1)); |
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1); |
| 628 |
p_section->stitle[sizeof(p_section->stitle - 1)] = '\0'; |
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
| 629 |
|
|
| 630 |
strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1)); |
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
| 631 |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
| 632 |
|
|
| 633 |
|
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1) |
| 634 |
|
{ |
| 635 |
|
log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count); |
| 636 |
|
return NULL; |
| 637 |
|
} |
| 638 |
|
|
| 639 |
if (trie_dict_set(p_trie_dict_section_list, sname, section_list_count) != 1) |
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1) |
| 640 |
{ |
{ |
| 641 |
log_error("trie_dict_set(section_data, %s, %d) error\n", sname, section_list_count); |
log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count); |
| 642 |
return NULL; |
return NULL; |
| 643 |
} |
} |
| 644 |
|
|
| 645 |
section_list_reset_articles(p_section); |
section_list_reset_articles(p_section); |
| 646 |
|
|
| 647 |
section_list_count++; |
p_section_list_pool->section_count++; |
| 648 |
|
|
| 649 |
return p_section; |
return p_section; |
| 650 |
} |
} |
| 651 |
|
|
| 652 |
|
int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list) |
| 653 |
|
{ |
| 654 |
|
if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL) |
| 655 |
|
{ |
| 656 |
|
log_error("NULL pointer error\n"); |
| 657 |
|
return -1; |
| 658 |
|
} |
| 659 |
|
|
| 660 |
|
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 661 |
|
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 662 |
|
|
| 663 |
|
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1); |
| 664 |
|
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
| 665 |
|
|
| 666 |
|
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
| 667 |
|
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
| 668 |
|
|
| 669 |
|
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1) |
| 670 |
|
{ |
| 671 |
|
log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count); |
| 672 |
|
return -2; |
| 673 |
|
} |
| 674 |
|
|
| 675 |
|
return 0; |
| 676 |
|
} |
| 677 |
|
|
| 678 |
void section_list_reset_articles(SECTION_LIST *p_section) |
void section_list_reset_articles(SECTION_LIST *p_section) |
| 679 |
{ |
{ |
| 680 |
p_section->article_count = 0; |
p_section->article_count = 0; |
| 686 |
|
|
| 687 |
p_section->page_count = 0; |
p_section->page_count = 0; |
| 688 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 689 |
|
|
| 690 |
|
p_section->ontop_article_count = 0; |
| 691 |
} |
} |
| 692 |
|
|
| 693 |
void section_list_cleanup(void) |
SECTION_LIST *section_list_find_by_name(const char *sname) |
| 694 |
{ |
{ |
| 695 |
if (p_trie_dict_section_list != NULL) |
int64_t index; |
| 696 |
|
int ret; |
| 697 |
|
|
| 698 |
|
if (p_section_list_pool == NULL) |
| 699 |
{ |
{ |
| 700 |
trie_dict_destroy(p_trie_dict_section_list); |
log_error("section_list not initialized\n"); |
| 701 |
p_trie_dict_section_list = NULL; |
return NULL; |
| 702 |
} |
} |
| 703 |
|
|
| 704 |
if (p_section_list_pool != NULL) |
ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index); |
| 705 |
|
if (ret < 0) |
| 706 |
{ |
{ |
| 707 |
free(p_section_list_pool); |
log_error("trie_dict_get(section, %s) error\n", sname); |
| 708 |
p_section_list_pool = NULL; |
return NULL; |
| 709 |
|
} |
| 710 |
|
else if (ret == 0) |
| 711 |
|
{ |
| 712 |
|
return NULL; |
| 713 |
} |
} |
| 714 |
|
|
| 715 |
section_list_count = 0; |
return (p_section_list_pool->sections + index); |
| 716 |
} |
} |
| 717 |
|
|
| 718 |
SECTION_LIST *section_list_find_by_name(const char *sname) |
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 719 |
{ |
{ |
| 720 |
int64_t index; |
int64_t index; |
| 721 |
|
int ret; |
| 722 |
|
char sid_str[SID_STR_LEN]; |
| 723 |
|
|
| 724 |
if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL) |
if (p_section_list_pool == NULL) |
| 725 |
{ |
{ |
| 726 |
log_error("section_list not initialized\n"); |
log_error("section_list not initialized\n"); |
| 727 |
return NULL; |
return NULL; |
| 728 |
} |
} |
| 729 |
|
|
| 730 |
if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1) |
sid_to_str(sid, sid_str); |
| 731 |
|
|
| 732 |
|
ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index); |
| 733 |
|
if (ret < 0) |
| 734 |
|
{ |
| 735 |
|
log_error("trie_dict_get(section, %d) error\n", sid); |
| 736 |
|
return NULL; |
| 737 |
|
} |
| 738 |
|
else if (ret == 0) |
| 739 |
{ |
{ |
|
log_error("trie_dict_get(section_data, %s) error\n", sname); |
|
| 740 |
return NULL; |
return NULL; |
| 741 |
} |
} |
| 742 |
|
|
| 743 |
return (p_section_list_pool + index); |
return (p_section_list_pool->sections + index); |
| 744 |
} |
} |
| 745 |
|
|
| 746 |
int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src) |
int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src) |
| 753 |
|
|
| 754 |
if (p_section == NULL || p_article_src == NULL) |
if (p_section == NULL || p_article_src == NULL) |
| 755 |
{ |
{ |
| 756 |
log_error("section_list_append_article() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 757 |
return -1; |
return -1; |
| 758 |
} |
} |
| 759 |
|
|
| 763 |
return -1; |
return -1; |
| 764 |
} |
} |
| 765 |
|
|
| 766 |
|
if (p_section->sid != p_article_src->sid) |
| 767 |
|
{ |
| 768 |
|
log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid); |
| 769 |
|
return -2; |
| 770 |
|
} |
| 771 |
|
|
| 772 |
if (p_section->article_count >= BBS_article_limit_per_section) |
if (p_section->article_count >= BBS_article_limit_per_section) |
| 773 |
{ |
{ |
| 774 |
log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid); |
log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid); |
| 801 |
// AID of articles should be strictly ascending |
// AID of articles should be strictly ascending |
| 802 |
if (p_article_src->aid <= last_aid) |
if (p_article_src->aid <= last_aid) |
| 803 |
{ |
{ |
| 804 |
log_error("section_data_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid); |
log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid); |
| 805 |
return -3; |
return -3; |
| 806 |
} |
} |
| 807 |
|
|
| 878 |
p_section->last_page_visible_article_count++; |
p_section->last_page_visible_article_count++; |
| 879 |
} |
} |
| 880 |
|
|
| 881 |
|
if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0) |
| 882 |
|
{ |
| 883 |
|
log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n", |
| 884 |
|
p_section->sid, p_article->aid); |
| 885 |
|
return -5; |
| 886 |
|
} |
| 887 |
|
|
| 888 |
return 0; |
return 0; |
| 889 |
} |
} |
| 890 |
|
|
| 896 |
|
|
| 897 |
if (p_section == NULL) |
if (p_section == NULL) |
| 898 |
{ |
{ |
| 899 |
log_error("section_list_set_article_visible() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 900 |
return -2; |
return -1; |
| 901 |
} |
} |
| 902 |
|
|
| 903 |
p_article = article_block_find_by_aid(aid); |
p_article = article_block_find_by_aid(aid); |
| 906 |
return -1; // Not found |
return -1; // Not found |
| 907 |
} |
} |
| 908 |
|
|
| 909 |
|
if (p_section->sid != p_article->sid) |
| 910 |
|
{ |
| 911 |
|
log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 912 |
|
return -2; |
| 913 |
|
} |
| 914 |
|
|
| 915 |
if (p_article->visible == visible) |
if (p_article->visible == visible) |
| 916 |
{ |
{ |
| 917 |
return 0; // Already set |
return 0; // Already set |
| 959 |
return affected_count; |
return affected_count; |
| 960 |
} |
} |
| 961 |
|
|
| 962 |
|
int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article) |
| 963 |
|
{ |
| 964 |
|
int i; |
| 965 |
|
|
| 966 |
|
if (p_section == NULL || p_article == NULL) |
| 967 |
|
{ |
| 968 |
|
log_error("NULL pointer error\n"); |
| 969 |
|
return -1; |
| 970 |
|
} |
| 971 |
|
|
| 972 |
|
if (p_section->sid != p_article->sid) |
| 973 |
|
{ |
| 974 |
|
log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 975 |
|
return -2; |
| 976 |
|
} |
| 977 |
|
|
| 978 |
|
if (p_article->ontop) |
| 979 |
|
{ |
| 980 |
|
for (i = 0; i < p_section->ontop_article_count; i++) |
| 981 |
|
{ |
| 982 |
|
if (p_section->p_ontop_articles[i]->aid == p_article->aid) |
| 983 |
|
{ |
| 984 |
|
log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid); |
| 985 |
|
return 0; |
| 986 |
|
} |
| 987 |
|
else if (p_section->p_ontop_articles[i]->aid > p_article->aid) |
| 988 |
|
{ |
| 989 |
|
break; |
| 990 |
|
} |
| 991 |
|
} |
| 992 |
|
|
| 993 |
|
// Remove the oldest one if the array of ontop articles is full |
| 994 |
|
if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section) |
| 995 |
|
{ |
| 996 |
|
if (i == 0) // p_article is the oldest one |
| 997 |
|
{ |
| 998 |
|
return 0; |
| 999 |
|
} |
| 1000 |
|
memmove((void *)(p_section->p_ontop_articles), |
| 1001 |
|
(void *)(p_section->p_ontop_articles + 1), |
| 1002 |
|
sizeof(ARTICLE *) * (size_t)(i - 1)); |
| 1003 |
|
p_section->ontop_article_count--; |
| 1004 |
|
i--; |
| 1005 |
|
} |
| 1006 |
|
else |
| 1007 |
|
{ |
| 1008 |
|
memmove((void *)(p_section->p_ontop_articles + i + 1), |
| 1009 |
|
(void *)(p_section->p_ontop_articles + i), |
| 1010 |
|
sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i)); |
| 1011 |
|
} |
| 1012 |
|
|
| 1013 |
|
p_section->p_ontop_articles[i] = p_article; |
| 1014 |
|
p_section->ontop_article_count++; |
| 1015 |
|
|
| 1016 |
|
// TODO: debug |
| 1017 |
|
} |
| 1018 |
|
else // ontop == 0 |
| 1019 |
|
{ |
| 1020 |
|
for (i = 0; i < p_section->ontop_article_count; i++) |
| 1021 |
|
{ |
| 1022 |
|
if (p_section->p_ontop_articles[i]->aid == p_article->aid) |
| 1023 |
|
{ |
| 1024 |
|
break; |
| 1025 |
|
} |
| 1026 |
|
} |
| 1027 |
|
if (i == p_section->ontop_article_count) // not found |
| 1028 |
|
{ |
| 1029 |
|
log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid); |
| 1030 |
|
return 0; |
| 1031 |
|
} |
| 1032 |
|
|
| 1033 |
|
memmove((void *)(p_section->p_ontop_articles + i), |
| 1034 |
|
(void *)(p_section->p_ontop_articles + i + 1), |
| 1035 |
|
sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1)); |
| 1036 |
|
p_section->ontop_article_count--; |
| 1037 |
|
} |
| 1038 |
|
|
| 1039 |
|
return 0; |
| 1040 |
|
} |
| 1041 |
|
|
| 1042 |
|
int section_list_page_count_with_ontop(SECTION_LIST *p_section) |
| 1043 |
|
{ |
| 1044 |
|
int page_count; |
| 1045 |
|
|
| 1046 |
|
if (p_section == NULL) |
| 1047 |
|
{ |
| 1048 |
|
log_error("NULL pointer error\n"); |
| 1049 |
|
return -1; |
| 1050 |
|
} |
| 1051 |
|
|
| 1052 |
|
page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) + |
| 1053 |
|
(p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page + |
| 1054 |
|
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1); |
| 1055 |
|
|
| 1056 |
|
return page_count; |
| 1057 |
|
} |
| 1058 |
|
|
| 1059 |
|
int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id) |
| 1060 |
|
{ |
| 1061 |
|
if (p_section == NULL) |
| 1062 |
|
{ |
| 1063 |
|
log_error("NULL pointer error\n"); |
| 1064 |
|
return -1; |
| 1065 |
|
} |
| 1066 |
|
|
| 1067 |
|
if (page_id < p_section->page_count - 1) |
| 1068 |
|
{ |
| 1069 |
|
return BBS_article_limit_per_page; |
| 1070 |
|
} |
| 1071 |
|
else // if (page_id >= p_section->page_count - 1) |
| 1072 |
|
{ |
| 1073 |
|
return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count - |
| 1074 |
|
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))); |
| 1075 |
|
} |
| 1076 |
|
} |
| 1077 |
|
|
| 1078 |
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next) |
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next) |
| 1079 |
{ |
{ |
| 1080 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1088 |
|
|
| 1089 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1090 |
{ |
{ |
| 1091 |
log_error("section_list_find_article_with_offset() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1092 |
return NULL; |
return NULL; |
| 1093 |
} |
} |
| 1094 |
|
|
| 1180 |
|
|
| 1181 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1182 |
{ |
{ |
| 1183 |
log_error("section_list_calculate_page() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1184 |
return -1; |
return -1; |
| 1185 |
} |
} |
| 1186 |
|
|
| 1194 |
|
|
| 1195 |
if (start_aid > 0) |
if (start_aid > 0) |
| 1196 |
{ |
{ |
| 1197 |
|
p_article = article_block_find_by_aid(start_aid); |
| 1198 |
|
if (p_article == NULL) |
| 1199 |
|
{ |
| 1200 |
|
return -1; // Not found |
| 1201 |
|
} |
| 1202 |
|
|
| 1203 |
|
if (p_section->sid != p_article->sid) |
| 1204 |
|
{ |
| 1205 |
|
log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid); |
| 1206 |
|
return -2; |
| 1207 |
|
} |
| 1208 |
|
|
| 1209 |
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next); |
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next); |
| 1210 |
if (p_article == NULL) |
if (p_article == NULL) |
| 1211 |
{ |
{ |
| 1274 |
return 0; |
return 0; |
| 1275 |
} |
} |
| 1276 |
|
|
| 1277 |
int section_list_count_of_topic_articles(int32_t aid) |
int32_t article_block_last_aid(void) |
| 1278 |
|
{ |
| 1279 |
|
ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]; |
| 1280 |
|
int32_t last_aid = p_block->articles[p_block->article_count - 1].aid; |
| 1281 |
|
|
| 1282 |
|
return last_aid; |
| 1283 |
|
} |
| 1284 |
|
|
| 1285 |
|
int article_block_article_count(void) |
| 1286 |
|
{ |
| 1287 |
|
int ret; |
| 1288 |
|
|
| 1289 |
|
if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0) |
| 1290 |
|
{ |
| 1291 |
|
return -1; |
| 1292 |
|
} |
| 1293 |
|
|
| 1294 |
|
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
| 1295 |
|
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count; |
| 1296 |
|
|
| 1297 |
|
return ret; |
| 1298 |
|
} |
| 1299 |
|
|
| 1300 |
|
int article_count_of_topic(int32_t aid) |
| 1301 |
{ |
{ |
| 1302 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1303 |
int article_count; |
int article_count; |
| 1312 |
|
|
| 1313 |
do |
do |
| 1314 |
{ |
{ |
| 1315 |
|
if (p_article->tid != 0 && p_article->tid != aid) |
| 1316 |
|
{ |
| 1317 |
|
log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid); |
| 1318 |
|
break; |
| 1319 |
|
} |
| 1320 |
|
|
| 1321 |
article_count++; |
article_count++; |
| 1322 |
p_article = p_article->p_topic_next; |
p_article = p_article->p_topic_next; |
| 1323 |
} while (p_article->aid != aid); |
} while (p_article->aid != aid); |
| 1337 |
int32_t first_inserted_aid_dest; |
int32_t first_inserted_aid_dest; |
| 1338 |
int move_counter; |
int move_counter; |
| 1339 |
|
|
| 1340 |
if (p_section_dest == NULL) |
if (p_section_src == NULL || p_section_dest == NULL) |
| 1341 |
{ |
{ |
| 1342 |
log_error("section_list_move_topic() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1343 |
return -1; |
return -1; |
| 1344 |
} |
} |
| 1345 |
|
|
| 1346 |
if ((p_article = section_list_find_article_with_offset(p_section_src, aid, &page, &offset, &p_next)) == NULL) |
if (p_section_src->sid == p_section_dest->sid) |
| 1347 |
|
{ |
| 1348 |
|
log_error("section_list_move_topic() src and dest section are the same\n"); |
| 1349 |
|
return -1; |
| 1350 |
|
} |
| 1351 |
|
|
| 1352 |
|
if ((p_article = article_block_find_by_aid(aid)) == NULL) |
| 1353 |
|
{ |
| 1354 |
|
log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid); |
| 1355 |
|
return -2; |
| 1356 |
|
} |
| 1357 |
|
|
| 1358 |
|
if (p_section_src->sid != p_article->sid) |
| 1359 |
{ |
{ |
| 1360 |
log_error("section_list_move_topic() error: article %d not found in section %d\n", aid, p_section_src->sid); |
log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n", |
| 1361 |
|
p_section_src->sid, p_article->aid, p_article->sid); |
| 1362 |
return -2; |
return -2; |
| 1363 |
} |
} |
| 1364 |
|
|
| 1370 |
|
|
| 1371 |
last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid); |
last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid); |
| 1372 |
|
|
| 1373 |
move_article_count = section_list_count_of_topic_articles(aid); |
move_article_count = article_count_of_topic(aid); |
| 1374 |
if (move_article_count <= 0) |
if (move_article_count <= 0) |
| 1375 |
{ |
{ |
| 1376 |
log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid); |
log_error("article_count_of_topic(aid = %d) <= 0\n", aid); |
| 1377 |
return -2; |
return -2; |
| 1378 |
} |
} |
| 1379 |
|
|
| 1390 |
|
|
| 1391 |
do |
do |
| 1392 |
{ |
{ |
| 1393 |
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
if (p_section_src->sid != p_article->sid) |
| 1394 |
{ |
{ |
| 1395 |
log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n", |
| 1396 |
return -4; |
p_section_src->sid, p_article->aid, p_article->sid); |
| 1397 |
|
p_article = p_article->p_topic_next; |
| 1398 |
|
continue; |
| 1399 |
} |
} |
| 1400 |
|
|
| 1401 |
// Remove from bi-directional article list of src section |
// Remove from bi-directional article list of src section |
| 1416 |
p_article->p_prior->p_next = p_article->p_next; |
p_article->p_prior->p_next = p_article->p_next; |
| 1417 |
p_article->p_next->p_prior = p_article->p_prior; |
p_article->p_next->p_prior = p_article->p_prior; |
| 1418 |
|
|
| 1419 |
|
// Update sid of article |
| 1420 |
|
p_article->sid = p_section_dest->sid; |
| 1421 |
|
|
| 1422 |
|
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
| 1423 |
|
{ |
| 1424 |
|
log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
| 1425 |
|
p_article = p_article->p_topic_next; |
| 1426 |
|
continue; |
| 1427 |
|
} |
| 1428 |
|
|
| 1429 |
// Insert into bi-directional article list of dest section |
// Insert into bi-directional article list of dest section |
| 1430 |
if (p_next == NULL) // empty section |
if (p_next == NULL) // empty section |
| 1431 |
{ |
{ |
| 1491 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1492 |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
| 1493 |
{ |
{ |
| 1494 |
log_error("section_list_calculate_page(section = %d, aid = %d) error\n", |
log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n", |
| 1495 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1496 |
} |
} |
| 1497 |
|
|
| 1501 |
|
|
| 1502 |
if (p_section_dest->article_count - dest_article_count_old != move_article_count) |
if (p_section_dest->article_count - dest_article_count_old != move_article_count) |
| 1503 |
{ |
{ |
| 1504 |
log_error("section_list_move_topic() error: count of moved articles %d != %d\n", |
log_error("section_list_move_topic() warning: count of moved articles %d != %d\n", |
| 1505 |
p_section_dest->article_count - dest_article_count_old, move_article_count); |
p_section_dest->article_count - dest_article_count_old, move_article_count); |
| 1506 |
} |
} |
| 1507 |
|
|
| 1508 |
// Re-calculate pages of src section |
// Re-calculate pages of src section |
| 1509 |
if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0) |
if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0) |
| 1510 |
{ |
{ |
| 1511 |
log_error("section_list_calculate_page(section = %d, aid = %d) error at aid = %d\n", |
log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n", |
| 1512 |
p_section_src->sid, last_unaffected_aid_src, aid); |
p_section_src->sid, last_unaffected_aid_src, aid); |
| 1513 |
} |
} |
| 1514 |
|
|
| 1517 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1518 |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
| 1519 |
{ |
{ |
| 1520 |
log_error("section_list_calculate_page(section = %d, aid = %d) error\n", |
log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n", |
| 1521 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1522 |
} |
} |
| 1523 |
} |
} |
| 1524 |
|
|
| 1525 |
return move_article_count; |
return move_article_count; |
| 1526 |
} |
} |
| 1527 |
|
|
| 1528 |
|
int get_section_index(SECTION_LIST *p_section) |
| 1529 |
|
{ |
| 1530 |
|
int index; |
| 1531 |
|
|
| 1532 |
|
if (p_section_list_pool == NULL) |
| 1533 |
|
{ |
| 1534 |
|
log_error("get_section_index() error: uninitialized\n"); |
| 1535 |
|
return -1; |
| 1536 |
|
} |
| 1537 |
|
|
| 1538 |
|
if (p_section == NULL) |
| 1539 |
|
{ |
| 1540 |
|
index = BBS_max_section; |
| 1541 |
|
} |
| 1542 |
|
else |
| 1543 |
|
{ |
| 1544 |
|
index = (int)(p_section - p_section_list_pool->sections); |
| 1545 |
|
if (index < 0 || index >= BBS_max_section) |
| 1546 |
|
{ |
| 1547 |
|
log_error("get_section_index(%d) error: index out of range\n", index); |
| 1548 |
|
return -2; |
| 1549 |
|
} |
| 1550 |
|
} |
| 1551 |
|
|
| 1552 |
|
return index; |
| 1553 |
|
} |
| 1554 |
|
|
| 1555 |
|
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1556 |
|
{ |
| 1557 |
|
int index; |
| 1558 |
|
struct sembuf sops[4]; |
| 1559 |
|
struct timespec timeout; |
| 1560 |
|
int ret; |
| 1561 |
|
|
| 1562 |
|
index = get_section_index(p_section); |
| 1563 |
|
if (index < 0) |
| 1564 |
|
{ |
| 1565 |
|
return -2; |
| 1566 |
|
} |
| 1567 |
|
|
| 1568 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1569 |
|
sops[0].sem_op = 0; // wait until unlocked |
| 1570 |
|
sops[0].sem_flg = 0; |
| 1571 |
|
|
| 1572 |
|
sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1573 |
|
sops[1].sem_op = 1; // lock |
| 1574 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1575 |
|
|
| 1576 |
|
// Read lock on any specific section will also acquire single read lock on "all section" |
| 1577 |
|
// so that write lock on all section only need to acquire single write on on "all section" |
| 1578 |
|
// rather than to acquire multiple write locks on all the available sections. |
| 1579 |
|
if (index != BBS_max_section) |
| 1580 |
|
{ |
| 1581 |
|
sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section |
| 1582 |
|
sops[2].sem_op = 0; // wait until unlocked |
| 1583 |
|
sops[2].sem_flg = 0; |
| 1584 |
|
|
| 1585 |
|
sops[3].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1586 |
|
sops[3].sem_op = 1; // lock |
| 1587 |
|
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1588 |
|
} |
| 1589 |
|
|
| 1590 |
|
timeout.tv_sec = wait_sec; |
| 1591 |
|
timeout.tv_nsec = 0; |
| 1592 |
|
|
| 1593 |
|
ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout); |
| 1594 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1595 |
|
{ |
| 1596 |
|
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
| 1597 |
|
} |
| 1598 |
|
|
| 1599 |
|
return ret; |
| 1600 |
|
} |
| 1601 |
|
|
| 1602 |
|
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
| 1603 |
|
{ |
| 1604 |
|
int index; |
| 1605 |
|
struct sembuf sops[3]; |
| 1606 |
|
struct timespec timeout; |
| 1607 |
|
int ret; |
| 1608 |
|
|
| 1609 |
|
index = get_section_index(p_section); |
| 1610 |
|
if (index < 0) |
| 1611 |
|
{ |
| 1612 |
|
return -2; |
| 1613 |
|
} |
| 1614 |
|
|
| 1615 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1616 |
|
sops[0].sem_op = 0; // wait until unlocked |
| 1617 |
|
sops[0].sem_flg = 0; |
| 1618 |
|
|
| 1619 |
|
sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1620 |
|
sops[1].sem_op = 1; // lock |
| 1621 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1622 |
|
|
| 1623 |
|
sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1624 |
|
sops[2].sem_op = 0; // wait until unlocked |
| 1625 |
|
sops[2].sem_flg = 0; |
| 1626 |
|
|
| 1627 |
|
timeout.tv_sec = wait_sec; |
| 1628 |
|
timeout.tv_nsec = 0; |
| 1629 |
|
|
| 1630 |
|
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1631 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1632 |
|
{ |
| 1633 |
|
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
| 1634 |
|
} |
| 1635 |
|
|
| 1636 |
|
return ret; |
| 1637 |
|
} |
| 1638 |
|
|
| 1639 |
|
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1640 |
|
{ |
| 1641 |
|
int index; |
| 1642 |
|
struct sembuf sops[2]; |
| 1643 |
|
int ret; |
| 1644 |
|
|
| 1645 |
|
index = get_section_index(p_section); |
| 1646 |
|
if (index < 0) |
| 1647 |
|
{ |
| 1648 |
|
return -2; |
| 1649 |
|
} |
| 1650 |
|
|
| 1651 |
|
sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1652 |
|
sops[0].sem_op = -1; // unlock |
| 1653 |
|
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1654 |
|
|
| 1655 |
|
if (index != BBS_max_section) |
| 1656 |
|
{ |
| 1657 |
|
sops[1].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1658 |
|
sops[1].sem_op = -1; // unlock |
| 1659 |
|
sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1660 |
|
} |
| 1661 |
|
|
| 1662 |
|
ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2)); |
| 1663 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1664 |
|
{ |
| 1665 |
|
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1666 |
|
} |
| 1667 |
|
|
| 1668 |
|
return ret; |
| 1669 |
|
} |
| 1670 |
|
|
| 1671 |
|
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1672 |
|
{ |
| 1673 |
|
int index; |
| 1674 |
|
struct sembuf sops[1]; |
| 1675 |
|
int ret; |
| 1676 |
|
|
| 1677 |
|
index = get_section_index(p_section); |
| 1678 |
|
if (index < 0) |
| 1679 |
|
{ |
| 1680 |
|
return -2; |
| 1681 |
|
} |
| 1682 |
|
|
| 1683 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1684 |
|
sops[0].sem_op = -1; // unlock |
| 1685 |
|
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1686 |
|
|
| 1687 |
|
ret = semop(p_section_list_pool->semid, sops, 1); |
| 1688 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1689 |
|
{ |
| 1690 |
|
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1691 |
|
} |
| 1692 |
|
|
| 1693 |
|
return ret; |
| 1694 |
|
} |
| 1695 |
|
|
| 1696 |
|
int section_list_rd_lock(SECTION_LIST *p_section) |
| 1697 |
|
{ |
| 1698 |
|
int timer = 0; |
| 1699 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1700 |
|
int ret = -1; |
| 1701 |
|
|
| 1702 |
|
while (!SYS_server_exit) |
| 1703 |
|
{ |
| 1704 |
|
ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1705 |
|
if (ret == 0) // success |
| 1706 |
|
{ |
| 1707 |
|
break; |
| 1708 |
|
} |
| 1709 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1710 |
|
{ |
| 1711 |
|
timer++; |
| 1712 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1713 |
|
{ |
| 1714 |
|
log_error("section_list_try_rd_lock() tried %d times on section %d\n", sid, timer); |
| 1715 |
|
} |
| 1716 |
|
} |
| 1717 |
|
else // failed |
| 1718 |
|
{ |
| 1719 |
|
log_error("section_list_try_rd_lock() failed on section %d\n", sid); |
| 1720 |
|
break; |
| 1721 |
|
} |
| 1722 |
|
} |
| 1723 |
|
|
| 1724 |
|
return ret; |
| 1725 |
|
} |
| 1726 |
|
|
| 1727 |
|
int section_list_rw_lock(SECTION_LIST *p_section) |
| 1728 |
|
{ |
| 1729 |
|
int timer = 0; |
| 1730 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1731 |
|
int ret = -1; |
| 1732 |
|
|
| 1733 |
|
while (!SYS_server_exit) |
| 1734 |
|
{ |
| 1735 |
|
ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1736 |
|
if (ret == 0) // success |
| 1737 |
|
{ |
| 1738 |
|
break; |
| 1739 |
|
} |
| 1740 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1741 |
|
{ |
| 1742 |
|
timer++; |
| 1743 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1744 |
|
{ |
| 1745 |
|
log_error("section_list_try_rw_lock() tried %d times on section %d\n", sid, timer); |
| 1746 |
|
} |
| 1747 |
|
} |
| 1748 |
|
else // failed |
| 1749 |
|
{ |
| 1750 |
|
log_error("section_list_try_rw_lock() failed on section %d\n", sid); |
| 1751 |
|
break; |
| 1752 |
|
} |
| 1753 |
|
} |
| 1754 |
|
|
| 1755 |
|
return ret; |
| 1756 |
|
} |