| 24 |
#include <time.h> |
#include <time.h> |
| 25 |
#include <sys/ipc.h> |
#include <sys/ipc.h> |
| 26 |
#include <sys/mman.h> |
#include <sys/mman.h> |
| 27 |
|
#include <sys/param.h> |
| 28 |
#include <sys/sem.h> |
#include <sys/sem.h> |
| 29 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 30 |
|
|
| 54 |
|
|
| 55 |
static USER_LIST_POOL *p_user_list_pool = NULL; |
static USER_LIST_POOL *p_user_list_pool = NULL; |
| 56 |
|
|
| 57 |
|
static int user_list_try_rd_lock(int semid, int wait_sec); |
| 58 |
|
static int user_list_try_rw_lock(int semid, int wait_sec); |
| 59 |
|
static int user_list_rd_unlock(int semid); |
| 60 |
|
static int user_list_rw_unlock(int semid); |
| 61 |
|
static int user_list_rd_lock(int semid); |
| 62 |
|
static int user_list_rw_lock(int semid); |
| 63 |
|
|
| 64 |
int user_list_load(MYSQL *db, USER_LIST *p_list) |
int user_list_load(MYSQL *db, USER_LIST *p_list) |
| 65 |
{ |
{ |
| 66 |
MYSQL_RES *rs = NULL; |
MYSQL_RES *rs = NULL; |
| 113 |
p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9])); |
p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9])); |
| 114 |
|
|
| 115 |
i++; |
i++; |
| 116 |
|
if (i >= BBS_max_user_count) |
| 117 |
|
{ |
| 118 |
|
log_error("Too many users, exceed limit %d\n", BBS_max_user_count); |
| 119 |
|
break; |
| 120 |
|
} |
| 121 |
} |
} |
| 122 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 123 |
rs = NULL; |
rs = NULL; |
| 281 |
{ |
{ |
| 282 |
MYSQL *db = NULL; |
MYSQL *db = NULL; |
| 283 |
USER_LIST *p_tmp; |
USER_LIST *p_tmp; |
|
int ret = 0; |
|
| 284 |
|
|
| 285 |
if (p_user_list_pool == NULL) |
if (p_user_list_pool == NULL) |
| 286 |
{ |
{ |
| 295 |
return -1; |
return -1; |
| 296 |
} |
} |
| 297 |
|
|
| 298 |
if (user_list_rw_lock() < 0) |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
| 299 |
{ |
{ |
| 300 |
log_error("user_list_rw_lock() error\n"); |
log_error("user_list_rw_lock() error\n"); |
| 301 |
return -2; |
return -2; |
| 302 |
} |
} |
| 303 |
|
|
| 304 |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
mysql_close(db); |
| 305 |
|
|
| 306 |
|
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
| 307 |
{ |
{ |
| 308 |
log_error("user_list_rw_lock() error\n"); |
log_error("user_list_rw_lock() error\n"); |
| 309 |
ret = -3; |
return -3; |
|
goto cleanup; |
|
| 310 |
} |
} |
| 311 |
|
|
| 312 |
// Swap p_current and p_new |
// Swap p_current and p_new |
| 314 |
p_user_list_pool->p_current = p_user_list_pool->p_new; |
p_user_list_pool->p_current = p_user_list_pool->p_new; |
| 315 |
p_user_list_pool->p_new = p_tmp; |
p_user_list_pool->p_new = p_tmp; |
| 316 |
|
|
| 317 |
cleanup: |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
|
if (user_list_rw_unlock() < 0) |
|
| 318 |
{ |
{ |
| 319 |
log_error("user_list_rw_unlock() error\n"); |
log_error("user_list_rw_unlock() error\n"); |
| 320 |
ret = -2; |
return -3; |
| 321 |
} |
} |
| 322 |
|
|
| 323 |
mysql_close(db); |
return 0; |
|
|
|
|
return ret; |
|
| 324 |
} |
} |
| 325 |
|
|
| 326 |
int user_list_try_rd_lock(int wait_sec) |
int user_list_try_rd_lock(int semid, int wait_sec) |
| 327 |
{ |
{ |
| 328 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 329 |
struct timespec timeout; |
struct timespec timeout; |
| 340 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 341 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 342 |
|
|
| 343 |
ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout); |
ret = semtimedop(semid, sops, 2, &timeout); |
| 344 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 345 |
{ |
{ |
| 346 |
log_error("semtimedop(lock read) error %d\n", errno); |
log_error("semtimedop(lock read) error %d\n", errno); |
| 349 |
return ret; |
return ret; |
| 350 |
} |
} |
| 351 |
|
|
| 352 |
int user_list_try_rw_lock(int wait_sec) |
int user_list_try_rw_lock(int semid, int wait_sec) |
| 353 |
{ |
{ |
| 354 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
| 355 |
struct timespec timeout; |
struct timespec timeout; |
| 370 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 371 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 372 |
|
|
| 373 |
ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout); |
ret = semtimedop(semid, sops, 3, &timeout); |
| 374 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 375 |
{ |
{ |
| 376 |
log_error("semtimedop(lock write) error %d\n", errno); |
log_error("semtimedop(lock write) error %d\n", errno); |
| 379 |
return ret; |
return ret; |
| 380 |
} |
} |
| 381 |
|
|
| 382 |
int user_list_rd_unlock(void) |
int user_list_rd_unlock(int semid) |
| 383 |
{ |
{ |
| 384 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 385 |
int ret; |
int ret; |
| 388 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 389 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 390 |
|
|
| 391 |
ret = semop(p_user_list_pool->semid, sops, 1); |
ret = semop(semid, sops, 1); |
| 392 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 393 |
{ |
{ |
| 394 |
log_error("semop(unlock read) error %d\n", errno); |
log_error("semop(unlock read) error %d\n", errno); |
| 397 |
return ret; |
return ret; |
| 398 |
} |
} |
| 399 |
|
|
| 400 |
int user_list_rw_unlock(void) |
int user_list_rw_unlock(int semid) |
| 401 |
{ |
{ |
| 402 |
struct sembuf sops[1]; |
struct sembuf sops[1]; |
| 403 |
int ret; |
int ret; |
| 406 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 407 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 408 |
|
|
| 409 |
ret = semop(p_user_list_pool->semid, sops, 1); |
ret = semop(semid, sops, 1); |
| 410 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 411 |
{ |
{ |
| 412 |
log_error("semop(unlock write) error %d\n", errno); |
log_error("semop(unlock write) error %d\n", errno); |
| 415 |
return ret; |
return ret; |
| 416 |
} |
} |
| 417 |
|
|
| 418 |
int user_list_rd_lock(void) |
int user_list_rd_lock(int semid) |
| 419 |
{ |
{ |
| 420 |
int timer = 0; |
int timer = 0; |
| 421 |
int ret = -1; |
int ret = -1; |
| 422 |
|
|
| 423 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 424 |
{ |
{ |
| 425 |
ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME); |
ret = user_list_try_rd_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME); |
| 426 |
if (ret == 0) // success |
if (ret == 0) // success |
| 427 |
{ |
{ |
| 428 |
break; |
break; |
| 445 |
return ret; |
return ret; |
| 446 |
} |
} |
| 447 |
|
|
| 448 |
int user_list_rw_lock(void) |
int user_list_rw_lock(int semid) |
| 449 |
{ |
{ |
| 450 |
int timer = 0; |
int timer = 0; |
| 451 |
int ret = -1; |
int ret = -1; |
| 452 |
|
|
| 453 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 454 |
{ |
{ |
| 455 |
ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME); |
ret = user_list_try_rw_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME); |
| 456 |
if (ret == 0) // success |
if (ret == 0) // success |
| 457 |
{ |
{ |
| 458 |
break; |
break; |
| 473 |
} |
} |
| 474 |
|
|
| 475 |
return ret; |
return ret; |
| 476 |
|
} |
| 477 |
|
|
| 478 |
|
int query_user_list(int page_id, USER_INFO *p_users, int *p_user_count, int *p_page_count) |
| 479 |
|
{ |
| 480 |
|
int ret = 0; |
| 481 |
|
|
| 482 |
|
if (p_users == NULL || p_user_count == NULL || p_page_count == NULL) |
| 483 |
|
{ |
| 484 |
|
log_error("NULL pointer error\n"); |
| 485 |
|
return -1; |
| 486 |
|
} |
| 487 |
|
|
| 488 |
|
// acquire lock of user list |
| 489 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 490 |
|
{ |
| 491 |
|
log_error("user_list_rd_lock() error\n"); |
| 492 |
|
return -2; |
| 493 |
|
} |
| 494 |
|
|
| 495 |
|
if (p_user_list_pool->p_current->user_count == 0) |
| 496 |
|
{ |
| 497 |
|
// empty list |
| 498 |
|
ret = 0; |
| 499 |
|
goto cleanup; |
| 500 |
|
} |
| 501 |
|
|
| 502 |
|
*p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page + |
| 503 |
|
(p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1); |
| 504 |
|
|
| 505 |
|
if (page_id < 0 || page_id >= *p_page_count) |
| 506 |
|
{ |
| 507 |
|
log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count); |
| 508 |
|
ret = -3; |
| 509 |
|
goto cleanup; |
| 510 |
|
} |
| 511 |
|
|
| 512 |
|
*p_user_count = MIN(BBS_user_limit_per_page, |
| 513 |
|
p_user_list_pool->p_current->user_count - |
| 514 |
|
page_id * BBS_user_limit_per_page); |
| 515 |
|
|
| 516 |
|
memcpy(p_users, |
| 517 |
|
p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page, |
| 518 |
|
sizeof(USER_INFO) * (size_t)(*p_user_count)); |
| 519 |
|
|
| 520 |
|
cleanup: |
| 521 |
|
// release lock of user list |
| 522 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 523 |
|
{ |
| 524 |
|
log_error("user_list_rd_unlock() error\n"); |
| 525 |
|
ret = -1; |
| 526 |
|
} |
| 527 |
|
|
| 528 |
|
return ret; |
| 529 |
|
} |
| 530 |
|
|
| 531 |
|
int query_user_info(int32_t uid, USER_INFO *p_user) |
| 532 |
|
{ |
| 533 |
|
int left; |
| 534 |
|
int right; |
| 535 |
|
int mid; |
| 536 |
|
int ret = 0; |
| 537 |
|
|
| 538 |
|
if (p_user == NULL) |
| 539 |
|
{ |
| 540 |
|
log_error("NULL pointer error\n"); |
| 541 |
|
return -1; |
| 542 |
|
} |
| 543 |
|
|
| 544 |
|
// acquire lock of user list |
| 545 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 546 |
|
{ |
| 547 |
|
log_error("user_list_rd_lock() error\n"); |
| 548 |
|
return -2; |
| 549 |
|
} |
| 550 |
|
|
| 551 |
|
left = 0; |
| 552 |
|
right = p_user_list_pool->p_current->user_count - 1; |
| 553 |
|
|
| 554 |
|
while (left < right) |
| 555 |
|
{ |
| 556 |
|
mid = (left + right) / 2; |
| 557 |
|
if (uid < p_user_list_pool->p_current->users[mid].uid) |
| 558 |
|
{ |
| 559 |
|
right = mid; |
| 560 |
|
} |
| 561 |
|
else if (uid > p_user_list_pool->p_current->users[mid].uid) |
| 562 |
|
{ |
| 563 |
|
left = mid + 1; |
| 564 |
|
} |
| 565 |
|
else // if (uid == p_user_list_pool->p_current->users[mid].uid) |
| 566 |
|
{ |
| 567 |
|
left = mid; |
| 568 |
|
break; |
| 569 |
|
} |
| 570 |
|
} |
| 571 |
|
|
| 572 |
|
if (uid == p_user_list_pool->p_current->users[left].uid) // Found |
| 573 |
|
{ |
| 574 |
|
*p_user = p_user_list_pool->p_current->users[left]; |
| 575 |
|
ret = 1; |
| 576 |
|
} |
| 577 |
|
|
| 578 |
|
// release lock of user list |
| 579 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 580 |
|
{ |
| 581 |
|
log_error("user_list_rd_unlock() error\n"); |
| 582 |
|
ret = -1; |
| 583 |
|
} |
| 584 |
|
|
| 585 |
|
return ret; |
| 586 |
} |
} |