| 17 |
#include "user_list.h" |
#include "user_list.h" |
| 18 |
#include "user_stat.h" |
#include "user_stat.h" |
| 19 |
#include <errno.h> |
#include <errno.h> |
| 20 |
|
#include <fcntl.h> |
| 21 |
#include <stdlib.h> |
#include <stdlib.h> |
| 22 |
#include <string.h> |
#include <string.h> |
| 23 |
#include <time.h> |
#include <time.h> |
|
#include <sys/ipc.h> |
|
| 24 |
#include <sys/mman.h> |
#include <sys/mman.h> |
| 25 |
#include <sys/param.h> |
#include <sys/param.h> |
| 26 |
|
#include <sys/stat.h> |
| 27 |
|
|
| 28 |
|
#ifdef HAVE_SYSTEM_V |
| 29 |
#include <sys/sem.h> |
#include <sys/sem.h> |
|
#include <sys/shm.h> |
|
| 30 |
|
|
| 31 |
#if defined(_SEM_SEMUN_UNDEFINED) || defined(__CYGWIN__) |
#ifdef _SEM_SEMUN_UNDEFINED |
| 32 |
union semun |
union semun |
| 33 |
{ |
{ |
| 34 |
int val; /* Value for SETVAL */ |
int val; /* Value for SETVAL */ |
| 35 |
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
| 36 |
unsigned short *array; /* Array for GETALL, SETALL */ |
unsigned short *array; /* Array for GETALL, SETALL */ |
| 37 |
struct seminfo *__buf; /* Buffer for IPC_INFO |
struct seminfo *__buf; /* Buffer for IPC_INFO |
| 38 |
(Linux-specific) */ |
(Linux-specific) */ |
| 39 |
}; |
}; |
| 40 |
#endif // #if defined(_SEM_SEMUN_UNDEFINED) |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
| 41 |
|
|
| 42 |
|
#else |
| 43 |
|
#include <semaphore.h> |
| 44 |
|
#endif |
| 45 |
|
|
| 46 |
enum _user_list_constant_t |
enum _user_list_constant_t |
| 47 |
{ |
{ |
| 51 |
|
|
| 52 |
struct user_list_pool_t |
struct user_list_pool_t |
| 53 |
{ |
{ |
| 54 |
int shmid; |
size_t shm_size; |
| 55 |
|
#ifndef HAVE_SYSTEM_V |
| 56 |
|
sem_t sem; |
| 57 |
|
uint16_t read_lock_count; |
| 58 |
|
uint16_t write_lock_count; |
| 59 |
|
#else |
| 60 |
int semid; |
int semid; |
| 61 |
|
#endif |
| 62 |
USER_LIST user_list[2]; |
USER_LIST user_list[2]; |
| 63 |
int user_list_index_current; |
int user_list_index_current; |
| 64 |
int user_list_index_new; |
int user_list_index_new; |
| 70 |
}; |
}; |
| 71 |
typedef struct user_list_pool_t USER_LIST_POOL; |
typedef struct user_list_pool_t USER_LIST_POOL; |
| 72 |
|
|
| 73 |
|
static char user_list_shm_name[FILE_NAME_LEN]; |
| 74 |
static USER_LIST_POOL *p_user_list_pool = NULL; |
static USER_LIST_POOL *p_user_list_pool = NULL; |
| 75 |
static TRIE_NODE *p_trie_action_dict = NULL; |
static TRIE_NODE *p_trie_action_dict = NULL; |
| 76 |
|
|
| 98 |
|
|
| 99 |
const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP); |
const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP); |
| 100 |
|
|
| 101 |
static int user_list_try_rd_lock(int semid, int wait_sec); |
static int user_list_try_rd_lock(int wait_sec); |
| 102 |
static int user_list_try_rw_lock(int semid, int wait_sec); |
static int user_list_try_rw_lock(int wait_sec); |
| 103 |
static int user_list_rd_unlock(int semid); |
static int user_list_rd_unlock(void); |
| 104 |
static int user_list_rw_unlock(int semid); |
static int user_list_rw_unlock(void); |
| 105 |
static int user_list_rd_lock(int semid); |
static int user_list_rd_lock(void); |
| 106 |
static int user_list_rw_lock(int semid); |
static int user_list_rw_lock(void); |
| 107 |
|
|
| 108 |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
| 109 |
static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list); |
static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list); |
| 401 |
|
|
| 402 |
int user_list_pool_init(const char *filename) |
int user_list_pool_init(const char *filename) |
| 403 |
{ |
{ |
| 404 |
int shmid; |
char filepath[FILE_PATH_LEN]; |
| 405 |
int semid; |
int fd; |
|
int proj_id; |
|
|
key_t key; |
|
| 406 |
size_t size; |
size_t size; |
| 407 |
void *p_shm; |
void *p_shm; |
| 408 |
|
#ifdef HAVE_SYSTEM_V |
| 409 |
|
int proj_id; |
| 410 |
|
key_t key; |
| 411 |
|
int semid; |
| 412 |
union semun arg; |
union semun arg; |
| 413 |
|
#endif |
| 414 |
int i; |
int i; |
| 415 |
|
|
| 416 |
if (p_user_list_pool != NULL || p_trie_action_dict != NULL) |
if (p_user_list_pool != NULL || p_trie_action_dict != NULL) |
| 435 |
} |
} |
| 436 |
|
|
| 437 |
// Allocate shared memory |
// Allocate shared memory |
| 438 |
proj_id = (int)(time(NULL) % getpid()); |
size = sizeof(USER_LIST_POOL); |
| 439 |
key = ftok(filename, proj_id); |
|
| 440 |
if (key == -1) |
strncpy(filepath, filename, sizeof(filepath) - 1); |
| 441 |
|
filepath[sizeof(filepath) - 1] = '\0'; |
| 442 |
|
snprintf(user_list_shm_name, sizeof(user_list_shm_name), "/USER_LIST_SHM_%s", basename(filepath)); |
| 443 |
|
|
| 444 |
|
if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT) |
| 445 |
{ |
{ |
| 446 |
log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno); |
log_error("shm_unlink(%s) error (%d)\n", user_list_shm_name, errno); |
| 447 |
return -2; |
return -2; |
| 448 |
} |
} |
| 449 |
|
|
| 450 |
size = sizeof(USER_LIST_POOL); |
if ((fd = shm_open(user_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
|
|
if (shmid == -1) |
|
| 451 |
{ |
{ |
| 452 |
log_error("shmget(size = %d) error (%d)\n", size, errno); |
log_error("shm_open(%s) error (%d)\n", user_list_shm_name, errno); |
| 453 |
return -3; |
return -2; |
| 454 |
} |
} |
| 455 |
p_shm = shmat(shmid, NULL, 0); |
if (ftruncate(fd, (off_t)size) == -1) |
|
if (p_shm == (void *)-1) |
|
| 456 |
{ |
{ |
| 457 |
log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); |
log_error("ftruncate(size=%d) error (%d)\n", size, errno); |
| 458 |
return -3; |
close(fd); |
| 459 |
|
return -2; |
| 460 |
|
} |
| 461 |
|
|
| 462 |
|
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 463 |
|
if (p_shm == MAP_FAILED) |
| 464 |
|
{ |
| 465 |
|
log_error("mmap() error (%d)\n", errno); |
| 466 |
|
close(fd); |
| 467 |
|
return -2; |
| 468 |
|
} |
| 469 |
|
|
| 470 |
|
if (close(fd) < 0) |
| 471 |
|
{ |
| 472 |
|
log_error("close(fd) error (%d)\n", errno); |
| 473 |
|
return -1; |
| 474 |
} |
} |
| 475 |
|
|
| 476 |
p_user_list_pool = p_shm; |
p_user_list_pool = p_shm; |
| 477 |
p_user_list_pool->shmid = shmid; |
p_user_list_pool->shm_size = size; |
| 478 |
|
|
| 479 |
// Allocate semaphore as user list pool lock |
// Allocate semaphore as user list pool lock |
| 480 |
|
#ifndef HAVE_SYSTEM_V |
| 481 |
|
if (sem_init(&(p_user_list_pool->sem), 1, 1) == -1) |
| 482 |
|
{ |
| 483 |
|
log_error("sem_init() error (%d)\n", errno); |
| 484 |
|
return -3; |
| 485 |
|
} |
| 486 |
|
|
| 487 |
|
p_user_list_pool->read_lock_count = 0; |
| 488 |
|
p_user_list_pool->write_lock_count = 0; |
| 489 |
|
#else |
| 490 |
|
proj_id = (int)(time(NULL) % getpid()); |
| 491 |
|
key = ftok(filename, proj_id); |
| 492 |
|
if (key == -1) |
| 493 |
|
{ |
| 494 |
|
log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno); |
| 495 |
|
return -2; |
| 496 |
|
} |
| 497 |
|
|
| 498 |
size = 2; // r_sem and w_sem |
size = 2; // r_sem and w_sem |
| 499 |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 500 |
if (semid == -1) |
if (semid == -1) |
| 515 |
} |
} |
| 516 |
|
|
| 517 |
p_user_list_pool->semid = semid; |
p_user_list_pool->semid = semid; |
| 518 |
|
#endif |
| 519 |
|
|
| 520 |
// Set user counts to 0 |
// Set user counts to 0 |
| 521 |
p_user_list_pool->user_list[0].user_count = 0; |
p_user_list_pool->user_list[0].user_count = 0; |
| 534 |
|
|
| 535 |
void user_list_pool_cleanup(void) |
void user_list_pool_cleanup(void) |
| 536 |
{ |
{ |
|
int shmid; |
|
|
|
|
| 537 |
if (p_user_list_pool == NULL) |
if (p_user_list_pool == NULL) |
| 538 |
{ |
{ |
| 539 |
return; |
return; |
| 540 |
} |
} |
| 541 |
|
|
| 542 |
shmid = p_user_list_pool->shmid; |
#ifdef HAVE_SYSTEM_V |
|
|
|
| 543 |
if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1) |
if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1) |
| 544 |
{ |
{ |
| 545 |
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_user_list_pool->semid, errno); |
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_user_list_pool->semid, errno); |
| 546 |
} |
} |
| 547 |
|
#else |
| 548 |
if (shmdt(p_user_list_pool) == -1) |
if (sem_destroy(&(p_user_list_pool->sem)) == -1) |
| 549 |
{ |
{ |
| 550 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("sem_destroy() error (%d)\n", errno); |
| 551 |
} |
} |
| 552 |
|
#endif |
| 553 |
|
|
| 554 |
|
detach_user_list_pool_shm(); |
| 555 |
|
|
| 556 |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT) |
| 557 |
{ |
{ |
| 558 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shm_unlink(%s) error (%d)\n", user_list_shm_name, errno); |
| 559 |
} |
} |
| 560 |
|
|
| 561 |
p_user_list_pool = NULL; |
user_list_shm_name[0] = '\0'; |
| 562 |
|
|
| 563 |
if (p_trie_action_dict != NULL) |
if (p_trie_action_dict != NULL) |
| 564 |
{ |
{ |
| 570 |
|
|
| 571 |
int set_user_list_pool_shm_readonly(void) |
int set_user_list_pool_shm_readonly(void) |
| 572 |
{ |
{ |
| 573 |
int shmid; |
if (p_user_list_pool != NULL && mprotect(p_user_list_pool, p_user_list_pool->shm_size, PROT_READ) < 0) |
|
void *p_shm; |
|
|
|
|
|
if (p_user_list_pool == NULL) |
|
| 574 |
{ |
{ |
| 575 |
log_error("p_user_list_pool not initialized\n"); |
log_error("mprotect() error (%d)\n", errno); |
| 576 |
return -1; |
return -1; |
| 577 |
} |
} |
| 578 |
|
|
|
shmid = p_user_list_pool->shmid; |
|
|
|
|
|
// Remap shared memory in read-only mode |
|
|
#if defined(__CYGWIN__) |
|
|
if (shmdt(p_user_list_pool) == -1) |
|
|
{ |
|
|
log_error("shmdt(user_list_pool) error (%d)\n", errno); |
|
|
return -1; |
|
|
} |
|
|
p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY); |
|
|
#else |
|
|
p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP); |
|
|
#endif |
|
|
if (p_shm == (void *)-1) |
|
|
{ |
|
|
log_error("shmat(user_list_pool shmid = %d) error (%d)\n", shmid, errno); |
|
|
return -3; |
|
|
} |
|
|
|
|
|
p_user_list_pool = p_shm; |
|
|
|
|
| 579 |
return 0; |
return 0; |
| 580 |
} |
} |
| 581 |
|
|
| 582 |
int detach_user_list_pool_shm(void) |
int detach_user_list_pool_shm(void) |
| 583 |
{ |
{ |
| 584 |
if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1) |
if (p_user_list_pool != NULL && munmap(p_user_list_pool, p_user_list_pool->shm_size) < 0) |
| 585 |
{ |
{ |
| 586 |
log_error("shmdt(user_list_pool) error (%d)\n", errno); |
log_error("munmap() error (%d)\n", errno); |
| 587 |
return -1; |
return -1; |
| 588 |
} |
} |
| 589 |
|
|
| 640 |
mysql_close(db); |
mysql_close(db); |
| 641 |
db = NULL; |
db = NULL; |
| 642 |
|
|
| 643 |
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
if (user_list_rw_lock() < 0) |
| 644 |
{ |
{ |
| 645 |
log_error("user_list_rw_lock() error\n"); |
log_error("user_list_rw_lock() error\n"); |
| 646 |
ret = -3; |
ret = -3; |
| 662 |
p_user_list_pool->user_list_index_new = tmp; |
p_user_list_pool->user_list_index_new = tmp; |
| 663 |
} |
} |
| 664 |
|
|
| 665 |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rw_unlock() < 0) |
| 666 |
{ |
{ |
| 667 |
log_error("user_list_rw_unlock() error\n"); |
log_error("user_list_rw_unlock() error\n"); |
| 668 |
ret = -3; |
ret = -3; |
| 675 |
return ret; |
return ret; |
| 676 |
} |
} |
| 677 |
|
|
| 678 |
int user_list_try_rd_lock(int semid, int wait_sec) |
int user_list_try_rd_lock(int wait_sec) |
| 679 |
{ |
{ |
| 680 |
|
#ifdef HAVE_SYSTEM_V |
| 681 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
|
#if !defined(__CYGWIN__) |
|
|
struct timespec timeout; |
|
| 682 |
#endif |
#endif |
| 683 |
int ret; |
struct timespec timeout; |
| 684 |
|
int ret = 0; |
| 685 |
|
|
| 686 |
|
if (p_user_list_pool == NULL) |
| 687 |
|
{ |
| 688 |
|
log_error("p_user_list_pool not initialized\n"); |
| 689 |
|
return -1; |
| 690 |
|
} |
| 691 |
|
|
| 692 |
|
timeout.tv_sec = wait_sec; |
| 693 |
|
timeout.tv_nsec = 0; |
| 694 |
|
|
| 695 |
|
#ifdef HAVE_SYSTEM_V |
| 696 |
sops[0].sem_num = 1; // w_sem |
sops[0].sem_num = 1; // w_sem |
| 697 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 698 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 701 |
sops[1].sem_op = 1; // lock |
sops[1].sem_op = 1; // lock |
| 702 |
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 703 |
|
|
| 704 |
#if defined(__CYGWIN__) |
ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout); |
|
ret = semop(semid, sops, 2); |
|
|
#else |
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
|
ret = semtimedop(semid, sops, 2, &timeout); |
|
|
#endif |
|
| 705 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 706 |
{ |
{ |
| 707 |
log_error("semop(lock read) error %d\n", errno); |
log_error("semop(lock read) error %d\n", errno); |
| 708 |
} |
} |
| 709 |
|
#else |
| 710 |
|
if (sem_timedwait(&(p_user_list_pool->sem), &timeout) == -1) |
| 711 |
|
{ |
| 712 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 713 |
|
{ |
| 714 |
|
log_error("sem_timedwait() error %d\n", errno); |
| 715 |
|
} |
| 716 |
|
return -1; |
| 717 |
|
} |
| 718 |
|
|
| 719 |
|
if (p_user_list_pool->write_lock_count == 0) |
| 720 |
|
{ |
| 721 |
|
p_user_list_pool->read_lock_count++; |
| 722 |
|
} |
| 723 |
|
else |
| 724 |
|
{ |
| 725 |
|
errno = EAGAIN; |
| 726 |
|
ret = -1; |
| 727 |
|
} |
| 728 |
|
|
| 729 |
|
if (sem_post(&(p_user_list_pool->sem)) == -1) |
| 730 |
|
{ |
| 731 |
|
log_error("sem_post() error %d\n", errno); |
| 732 |
|
return -1; |
| 733 |
|
} |
| 734 |
|
#endif |
| 735 |
|
|
| 736 |
return ret; |
return ret; |
| 737 |
} |
} |
| 738 |
|
|
| 739 |
int user_list_try_rw_lock(int semid, int wait_sec) |
int user_list_try_rw_lock(int wait_sec) |
| 740 |
{ |
{ |
| 741 |
|
#ifdef HAVE_SYSTEM_V |
| 742 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
|
#if !defined(__CYGWIN__) |
|
|
struct timespec timeout; |
|
| 743 |
#endif |
#endif |
| 744 |
int ret; |
struct timespec timeout; |
| 745 |
|
int ret = 0; |
| 746 |
|
|
| 747 |
|
if (p_user_list_pool == NULL) |
| 748 |
|
{ |
| 749 |
|
log_error("p_user_list_pool not initialized\n"); |
| 750 |
|
return -1; |
| 751 |
|
} |
| 752 |
|
|
| 753 |
|
timeout.tv_sec = wait_sec; |
| 754 |
|
timeout.tv_nsec = 0; |
| 755 |
|
|
| 756 |
|
#ifdef HAVE_SYSTEM_V |
| 757 |
sops[0].sem_num = 1; // w_sem |
sops[0].sem_num = 1; // w_sem |
| 758 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 759 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 766 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 767 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 768 |
|
|
| 769 |
#if defined(__CYGWIN__) |
ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout); |
|
ret = semop(semid, sops, 3); |
|
|
#else |
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
|
ret = semtimedop(semid, sops, 3, &timeout); |
|
|
#endif |
|
| 770 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 771 |
{ |
{ |
| 772 |
log_error("semop(lock write) error %d\n", errno); |
log_error("semop(lock write) error %d\n", errno); |
| 773 |
} |
} |
| 774 |
|
#else |
| 775 |
|
if (sem_timedwait(&(p_user_list_pool->sem), &timeout) == -1) |
| 776 |
|
{ |
| 777 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 778 |
|
{ |
| 779 |
|
log_error("sem_timedwait() error %d\n", errno); |
| 780 |
|
} |
| 781 |
|
return -1; |
| 782 |
|
} |
| 783 |
|
|
| 784 |
|
if (p_user_list_pool->read_lock_count == 0 && p_user_list_pool->write_lock_count == 0) |
| 785 |
|
{ |
| 786 |
|
p_user_list_pool->write_lock_count++; |
| 787 |
|
} |
| 788 |
|
else |
| 789 |
|
{ |
| 790 |
|
errno = EAGAIN; |
| 791 |
|
ret = -1; |
| 792 |
|
} |
| 793 |
|
|
| 794 |
|
if (sem_post(&(p_user_list_pool->sem)) == -1) |
| 795 |
|
{ |
| 796 |
|
log_error("sem_post() error %d\n", errno); |
| 797 |
|
return -1; |
| 798 |
|
} |
| 799 |
|
#endif |
| 800 |
|
|
| 801 |
return ret; |
return ret; |
| 802 |
} |
} |
| 803 |
|
|
| 804 |
int user_list_rd_unlock(int semid) |
int user_list_rd_unlock(void) |
| 805 |
{ |
{ |
| 806 |
|
#ifdef HAVE_SYSTEM_V |
| 807 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 808 |
int ret; |
#endif |
| 809 |
|
int ret = 0; |
| 810 |
|
|
| 811 |
|
if (p_user_list_pool == NULL) |
| 812 |
|
{ |
| 813 |
|
log_error("p_user_list_pool not initialized\n"); |
| 814 |
|
return -1; |
| 815 |
|
} |
| 816 |
|
|
| 817 |
|
#ifdef HAVE_SYSTEM_V |
| 818 |
sops[0].sem_num = 0; // r_sem |
sops[0].sem_num = 0; // r_sem |
| 819 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 820 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 821 |
|
|
| 822 |
ret = semop(semid, sops, 1); |
ret = semop(p_user_list_pool->semid, sops, 1); |
| 823 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 824 |
{ |
{ |
| 825 |
log_error("semop(unlock read) error %d\n", errno); |
log_error("semop(unlock read) error %d\n", errno); |
| 826 |
} |
} |
| 827 |
|
#else |
| 828 |
|
if (sem_wait(&(p_user_list_pool->sem)) == -1) |
| 829 |
|
{ |
| 830 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 831 |
|
{ |
| 832 |
|
log_error("sem_wait() error %d\n", errno); |
| 833 |
|
} |
| 834 |
|
return -1; |
| 835 |
|
} |
| 836 |
|
|
| 837 |
|
if (p_user_list_pool->read_lock_count > 0) |
| 838 |
|
{ |
| 839 |
|
p_user_list_pool->read_lock_count--; |
| 840 |
|
} |
| 841 |
|
else |
| 842 |
|
{ |
| 843 |
|
log_error("read_lock_count already 0\n"); |
| 844 |
|
} |
| 845 |
|
|
| 846 |
|
if (sem_post(&(p_user_list_pool->sem)) == -1) |
| 847 |
|
{ |
| 848 |
|
log_error("sem_post() error %d\n", errno); |
| 849 |
|
return -1; |
| 850 |
|
} |
| 851 |
|
#endif |
| 852 |
|
|
| 853 |
return ret; |
return ret; |
| 854 |
} |
} |
| 855 |
|
|
| 856 |
int user_list_rw_unlock(int semid) |
int user_list_rw_unlock(void) |
| 857 |
{ |
{ |
| 858 |
|
#ifdef HAVE_SYSTEM_V |
| 859 |
struct sembuf sops[1]; |
struct sembuf sops[1]; |
| 860 |
int ret; |
#endif |
| 861 |
|
int ret = 0; |
| 862 |
|
|
| 863 |
|
if (p_user_list_pool == NULL) |
| 864 |
|
{ |
| 865 |
|
log_error("p_user_list_pool not initialized\n"); |
| 866 |
|
return -1; |
| 867 |
|
} |
| 868 |
|
|
| 869 |
|
#ifdef HAVE_SYSTEM_V |
| 870 |
sops[0].sem_num = 1; // w_sem |
sops[0].sem_num = 1; // w_sem |
| 871 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 872 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 873 |
|
|
| 874 |
ret = semop(semid, sops, 1); |
ret = semop(p_user_list_pool->semid, sops, 1); |
| 875 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 876 |
{ |
{ |
| 877 |
log_error("semop(unlock write) error %d\n", errno); |
log_error("semop(unlock write) error %d\n", errno); |
| 878 |
} |
} |
| 879 |
|
#else |
| 880 |
|
if (sem_wait(&(p_user_list_pool->sem)) == -1) |
| 881 |
|
{ |
| 882 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 883 |
|
{ |
| 884 |
|
log_error("sem_wait() error %d\n", errno); |
| 885 |
|
} |
| 886 |
|
return -1; |
| 887 |
|
} |
| 888 |
|
|
| 889 |
|
if (p_user_list_pool->write_lock_count > 0) |
| 890 |
|
{ |
| 891 |
|
p_user_list_pool->write_lock_count--; |
| 892 |
|
} |
| 893 |
|
else |
| 894 |
|
{ |
| 895 |
|
log_error("write_lock_count already 0\n"); |
| 896 |
|
} |
| 897 |
|
|
| 898 |
|
if (sem_post(&(p_user_list_pool->sem)) == -1) |
| 899 |
|
{ |
| 900 |
|
log_error("sem_post() error %d\n", errno); |
| 901 |
|
return -1; |
| 902 |
|
} |
| 903 |
|
#endif |
| 904 |
|
|
| 905 |
return ret; |
return ret; |
| 906 |
} |
} |
| 907 |
|
|
| 908 |
int user_list_rd_lock(int semid) |
int user_list_rd_lock(void) |
| 909 |
{ |
{ |
| 910 |
int timer = 0; |
int timer = 0; |
| 911 |
int ret = -1; |
int ret = -1; |
| 912 |
|
|
| 913 |
|
if (p_user_list_pool == NULL) |
| 914 |
|
{ |
| 915 |
|
log_error("p_user_list_pool not initialized\n"); |
| 916 |
|
return -1; |
| 917 |
|
} |
| 918 |
|
|
| 919 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 920 |
{ |
{ |
| 921 |
ret = user_list_try_rd_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME); |
ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME); |
| 922 |
if (ret == 0) // success |
if (ret == 0) // success |
| 923 |
{ |
{ |
| 924 |
break; |
break; |
| 930 |
{ |
{ |
| 931 |
log_error("user_list_try_rd_lock() tried %d times\n", timer); |
log_error("user_list_try_rd_lock() tried %d times\n", timer); |
| 932 |
} |
} |
| 933 |
|
usleep(100 * 1000); // 0.1 second |
| 934 |
} |
} |
| 935 |
else // failed |
else // failed |
| 936 |
{ |
{ |
| 942 |
return ret; |
return ret; |
| 943 |
} |
} |
| 944 |
|
|
| 945 |
int user_list_rw_lock(int semid) |
int user_list_rw_lock(void) |
| 946 |
{ |
{ |
| 947 |
int timer = 0; |
int timer = 0; |
| 948 |
int ret = -1; |
int ret = -1; |
| 949 |
|
|
| 950 |
|
if (p_user_list_pool == NULL) |
| 951 |
|
{ |
| 952 |
|
log_error("p_user_list_pool not initialized\n"); |
| 953 |
|
return -1; |
| 954 |
|
} |
| 955 |
|
|
| 956 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 957 |
{ |
{ |
| 958 |
ret = user_list_try_rw_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME); |
ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME); |
| 959 |
if (ret == 0) // success |
if (ret == 0) // success |
| 960 |
{ |
{ |
| 961 |
break; |
break; |
| 967 |
{ |
{ |
| 968 |
log_error("user_list_try_rw_lock() tried %d times\n", timer); |
log_error("user_list_try_rw_lock() tried %d times\n", timer); |
| 969 |
} |
} |
| 970 |
|
usleep(100 * 1000); // 0.1 second |
| 971 |
} |
} |
| 972 |
else // failed |
else // failed |
| 973 |
{ |
{ |
| 993 |
*p_page_count = 0; |
*p_page_count = 0; |
| 994 |
|
|
| 995 |
// acquire lock of user list |
// acquire lock of user list |
| 996 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 997 |
{ |
{ |
| 998 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 999 |
return -2; |
return -2; |
| 1026 |
|
|
| 1027 |
cleanup: |
cleanup: |
| 1028 |
// release lock of user list |
// release lock of user list |
| 1029 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1030 |
{ |
{ |
| 1031 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1032 |
ret = -1; |
ret = -1; |
| 1049 |
*p_page_count = 0; |
*p_page_count = 0; |
| 1050 |
|
|
| 1051 |
// acquire lock of user list |
// acquire lock of user list |
| 1052 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1053 |
{ |
{ |
| 1054 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1055 |
return -2; |
return -2; |
| 1081 |
|
|
| 1082 |
cleanup: |
cleanup: |
| 1083 |
// release lock of user list |
// release lock of user list |
| 1084 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1085 |
{ |
{ |
| 1086 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1087 |
ret = -1; |
ret = -1; |
| 1099 |
} |
} |
| 1100 |
|
|
| 1101 |
// acquire lock of user list |
// acquire lock of user list |
| 1102 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1103 |
{ |
{ |
| 1104 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1105 |
return -2; |
return -2; |
| 1108 |
*p_user_cnt = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count; |
*p_user_cnt = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count; |
| 1109 |
|
|
| 1110 |
// release lock of user list |
// release lock of user list |
| 1111 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1112 |
{ |
{ |
| 1113 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1114 |
return -2; |
return -2; |
| 1126 |
} |
} |
| 1127 |
|
|
| 1128 |
// acquire lock of user list |
// acquire lock of user list |
| 1129 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1130 |
{ |
{ |
| 1131 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1132 |
return -2; |
return -2; |
| 1136 |
*p_guest_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].guest_count; |
*p_guest_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].guest_count; |
| 1137 |
|
|
| 1138 |
// release lock of user list |
// release lock of user list |
| 1139 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1140 |
{ |
{ |
| 1141 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1142 |
return -2; |
return -2; |
| 1169 |
} |
} |
| 1170 |
|
|
| 1171 |
// acquire lock of user list |
// acquire lock of user list |
| 1172 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1173 |
{ |
{ |
| 1174 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1175 |
return -2; |
return -2; |
| 1182 |
} |
} |
| 1183 |
|
|
| 1184 |
// release lock of user list |
// release lock of user list |
| 1185 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1186 |
{ |
{ |
| 1187 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1188 |
ret = -1; |
ret = -1; |
| 1206 |
} |
} |
| 1207 |
|
|
| 1208 |
// acquire lock of user list |
// acquire lock of user list |
| 1209 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1210 |
{ |
{ |
| 1211 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1212 |
return -2; |
return -2; |
| 1248 |
} |
} |
| 1249 |
|
|
| 1250 |
// release lock of user list |
// release lock of user list |
| 1251 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1252 |
{ |
{ |
| 1253 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1254 |
ret = -1; |
ret = -1; |
| 1278 |
prefix_len = strlen(username_prefix); |
prefix_len = strlen(username_prefix); |
| 1279 |
|
|
| 1280 |
// acquire lock of user list |
// acquire lock of user list |
| 1281 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1282 |
{ |
{ |
| 1283 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1284 |
return -2; |
return -2; |
| 1382 |
|
|
| 1383 |
cleanup: |
cleanup: |
| 1384 |
// release lock of user list |
// release lock of user list |
| 1385 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1386 |
{ |
{ |
| 1387 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1388 |
ret = -1; |
ret = -1; |
| 1402 |
} |
} |
| 1403 |
|
|
| 1404 |
// acquire lock of user list |
// acquire lock of user list |
| 1405 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1406 |
{ |
{ |
| 1407 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1408 |
return -2; |
return -2; |
| 1415 |
} |
} |
| 1416 |
|
|
| 1417 |
// release lock of user list |
// release lock of user list |
| 1418 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1419 |
{ |
{ |
| 1420 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1421 |
ret = -1; |
ret = -1; |
| 1444 |
*p_user_cnt = 0; |
*p_user_cnt = 0; |
| 1445 |
|
|
| 1446 |
// acquire lock of user list |
// acquire lock of user list |
| 1447 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1448 |
{ |
{ |
| 1449 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1450 |
return -2; |
return -2; |
| 1506 |
} |
} |
| 1507 |
|
|
| 1508 |
// release lock of user list |
// release lock of user list |
| 1509 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1510 |
{ |
{ |
| 1511 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1512 |
ret = -1; |
ret = -1; |
| 1530 |
} |
} |
| 1531 |
|
|
| 1532 |
// acquire lock of user list |
// acquire lock of user list |
| 1533 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1534 |
{ |
{ |
| 1535 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1536 |
return -2; |
return -2; |
| 1564 |
*p_user_cnt = i; |
*p_user_cnt = i; |
| 1565 |
|
|
| 1566 |
// release lock of user list |
// release lock of user list |
| 1567 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1568 |
{ |
{ |
| 1569 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1570 |
ret = -1; |
ret = -1; |