| 6 |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 12 |
|
|
| 13 |
#include "common.h" |
#include "common.h" |
| 14 |
#include "database.h" |
#include "database.h" |
| 15 |
#include "log.h" |
#include "log.h" |
| 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/sem.h> |
#include <sys/sem.h> |
| 27 |
#include <sys/shm.h> |
#include <sys/stat.h> |
| 28 |
|
|
| 29 |
#ifdef _SEM_SEMUN_UNDEFINED |
#if defined(_SEM_SEMUN_UNDEFINED) || defined(__CYGWIN__) |
| 30 |
union semun |
union semun |
| 31 |
{ |
{ |
| 32 |
int val; /* Value for SETVAL */ |
int val; /* Value for SETVAL */ |
| 35 |
struct seminfo *__buf; /* Buffer for IPC_INFO |
struct seminfo *__buf; /* Buffer for IPC_INFO |
| 36 |
(Linux-specific) */ |
(Linux-specific) */ |
| 37 |
}; |
}; |
| 38 |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
#endif // #if defined(_SEM_SEMUN_UNDEFINED) |
| 39 |
|
|
| 40 |
enum _user_list_constant_t |
enum _user_list_constant_t |
| 41 |
{ |
{ |
| 45 |
|
|
| 46 |
struct user_list_pool_t |
struct user_list_pool_t |
| 47 |
{ |
{ |
| 48 |
int shmid; |
size_t shm_size; |
| 49 |
int semid; |
int semid; |
| 50 |
USER_LIST user_list[2]; |
USER_LIST user_list[2]; |
| 51 |
USER_LIST *p_current; |
int user_list_index_current; |
| 52 |
USER_LIST *p_new; |
int user_list_index_new; |
| 53 |
USER_ONLINE_LIST user_online_list[2]; |
USER_ONLINE_LIST user_online_list[2]; |
| 54 |
USER_ONLINE_LIST *p_online_current; |
int user_online_list_index_current; |
| 55 |
USER_ONLINE_LIST *p_online_new; |
int user_online_list_index_new; |
| 56 |
USER_STAT_MAP user_stat_map; |
USER_STAT_MAP user_stat_map; |
| 57 |
int user_login_count; |
int user_login_count; |
| 58 |
}; |
}; |
| 59 |
typedef struct user_list_pool_t USER_LIST_POOL; |
typedef struct user_list_pool_t USER_LIST_POOL; |
| 60 |
|
|
| 61 |
|
static char user_list_shm_name[FILE_NAME_LEN]; |
| 62 |
static USER_LIST_POOL *p_user_list_pool = NULL; |
static USER_LIST_POOL *p_user_list_pool = NULL; |
| 63 |
static TRIE_NODE *p_trie_action_dict = NULL; |
static TRIE_NODE *p_trie_action_dict = NULL; |
| 64 |
|
|
| 86 |
|
|
| 87 |
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); |
| 88 |
|
|
| 89 |
static int user_list_try_rd_lock(int semid, int wait_sec); |
static int user_list_try_rd_lock(int wait_sec); |
| 90 |
static int user_list_try_rw_lock(int semid, int wait_sec); |
static int user_list_try_rw_lock(int wait_sec); |
| 91 |
static int user_list_rd_unlock(int semid); |
static int user_list_rd_unlock(void); |
| 92 |
static int user_list_rw_unlock(int semid); |
static int user_list_rw_unlock(void); |
| 93 |
static int user_list_rd_lock(int semid); |
static int user_list_rd_lock(void); |
| 94 |
static int user_list_rw_lock(int semid); |
static int user_list_rw_lock(void); |
| 95 |
|
|
| 96 |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
| 97 |
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); |
| 189 |
p_list->users[i].signup_dt = (row[8] == NULL ? 0 : atol(row[8])); |
p_list->users[i].signup_dt = (row[8] == NULL ? 0 : atol(row[8])); |
| 190 |
p_list->users[i].last_login_dt = (row[9] == NULL ? 0 : atol(row[9])); |
p_list->users[i].last_login_dt = (row[9] == NULL ? 0 : atol(row[9])); |
| 191 |
p_list->users[i].last_logout_dt = (row[10] == NULL ? 0 : atol(row[10])); |
p_list->users[i].last_logout_dt = (row[10] == NULL ? 0 : atol(row[10])); |
| 192 |
p_list->users[i].birthday = (row[10] == NULL ? 0 : atol(row[11])); |
p_list->users[i].birthday = (row[11] == NULL ? 0 : atol(row[11])); |
| 193 |
intro_len = strlen((row[12] == NULL ? "" : row[12])); |
intro_len = strlen((row[12] == NULL ? "" : row[12])); |
| 194 |
if (intro_len >= sizeof(p_list->user_intro_buf) - 1 - intro_buf_offset) |
if (intro_len >= sizeof(p_list->user_intro_buf) - 1 - intro_buf_offset) |
| 195 |
{ |
{ |
| 389 |
|
|
| 390 |
int user_list_pool_init(const char *filename) |
int user_list_pool_init(const char *filename) |
| 391 |
{ |
{ |
| 392 |
int shmid; |
char filepath[FILE_PATH_LEN]; |
| 393 |
int semid; |
int fd; |
|
int proj_id; |
|
|
key_t key; |
|
| 394 |
size_t size; |
size_t size; |
| 395 |
void *p_shm; |
void *p_shm; |
| 396 |
|
int proj_id; |
| 397 |
|
key_t key; |
| 398 |
|
int semid; |
| 399 |
union semun arg; |
union semun arg; |
| 400 |
int i; |
int i; |
| 401 |
|
|
| 421 |
} |
} |
| 422 |
|
|
| 423 |
// Allocate shared memory |
// Allocate shared memory |
| 424 |
proj_id = (int)(time(NULL) % getpid()); |
size = sizeof(USER_LIST_POOL); |
| 425 |
key = ftok(filename, proj_id); |
|
| 426 |
if (key == -1) |
strncpy(filepath, filename, sizeof(filepath) - 1); |
| 427 |
|
filepath[sizeof(filepath) - 1] = '\0'; |
| 428 |
|
snprintf(user_list_shm_name, sizeof(user_list_shm_name), "/USER_LIST_SHM_%s", basename(filepath)); |
| 429 |
|
|
| 430 |
|
if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT) |
| 431 |
{ |
{ |
| 432 |
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); |
| 433 |
return -2; |
return -2; |
| 434 |
} |
} |
| 435 |
|
|
| 436 |
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) |
|
| 437 |
{ |
{ |
| 438 |
log_error("shmget(size = %d) error (%d)\n", size, errno); |
log_error("shm_open(%s) error (%d)\n", user_list_shm_name, errno); |
| 439 |
return -3; |
return -2; |
| 440 |
} |
} |
| 441 |
p_shm = shmat(shmid, NULL, 0); |
if (ftruncate(fd, (off_t)size) == -1) |
|
if (p_shm == (void *)-1) |
|
| 442 |
{ |
{ |
| 443 |
log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); |
log_error("ftruncate(size=%d) error (%d)\n", size, errno); |
| 444 |
return -3; |
close(fd); |
| 445 |
|
return -2; |
| 446 |
|
} |
| 447 |
|
|
| 448 |
|
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 449 |
|
if (p_shm == MAP_FAILED) |
| 450 |
|
{ |
| 451 |
|
log_error("mmap() error (%d)\n", errno); |
| 452 |
|
close(fd); |
| 453 |
|
return -2; |
| 454 |
|
} |
| 455 |
|
|
| 456 |
|
if (close(fd) < 0) |
| 457 |
|
{ |
| 458 |
|
log_error("close(fd) error (%d)\n", errno); |
| 459 |
|
return -1; |
| 460 |
} |
} |
| 461 |
|
|
| 462 |
p_user_list_pool = p_shm; |
p_user_list_pool = p_shm; |
| 463 |
p_user_list_pool->shmid = shmid; |
p_user_list_pool->shm_size = size; |
| 464 |
|
|
| 465 |
// Allocate semaphore as user list pool lock |
// Allocate semaphore as user list pool lock |
| 466 |
|
proj_id = (int)(time(NULL) % getpid()); |
| 467 |
|
key = ftok(filename, proj_id); |
| 468 |
|
if (key == -1) |
| 469 |
|
{ |
| 470 |
|
log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno); |
| 471 |
|
return -2; |
| 472 |
|
} |
| 473 |
|
|
| 474 |
size = 2; // r_sem and w_sem |
size = 2; // r_sem and w_sem |
| 475 |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 476 |
if (semid == -1) |
if (semid == -1) |
| 496 |
p_user_list_pool->user_list[0].user_count = 0; |
p_user_list_pool->user_list[0].user_count = 0; |
| 497 |
p_user_list_pool->user_list[1].user_count = 0; |
p_user_list_pool->user_list[1].user_count = 0; |
| 498 |
|
|
| 499 |
p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]); |
p_user_list_pool->user_list_index_current = 0; |
| 500 |
p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]); |
p_user_list_pool->user_list_index_new = 1; |
| 501 |
|
|
| 502 |
p_user_list_pool->p_online_current = &(p_user_list_pool->user_online_list[0]); |
p_user_list_pool->user_online_list_index_current = 0; |
| 503 |
p_user_list_pool->p_online_new = &(p_user_list_pool->user_online_list[1]); |
p_user_list_pool->user_online_list_index_new = 1; |
| 504 |
|
|
| 505 |
user_stat_map_init(&(p_user_list_pool->user_stat_map)); |
user_stat_map_init(&(p_user_list_pool->user_stat_map)); |
| 506 |
|
|
| 509 |
|
|
| 510 |
void user_list_pool_cleanup(void) |
void user_list_pool_cleanup(void) |
| 511 |
{ |
{ |
|
int shmid; |
|
|
|
|
| 512 |
if (p_user_list_pool == NULL) |
if (p_user_list_pool == NULL) |
| 513 |
{ |
{ |
| 514 |
return; |
return; |
| 515 |
} |
} |
| 516 |
|
|
|
shmid = p_user_list_pool->shmid; |
|
|
|
|
| 517 |
if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1) |
if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1) |
| 518 |
{ |
{ |
| 519 |
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); |
| 520 |
} |
} |
| 521 |
|
|
| 522 |
if (shmdt(p_user_list_pool) == -1) |
detach_user_list_pool_shm(); |
|
{ |
|
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
|
|
} |
|
| 523 |
|
|
| 524 |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT) |
| 525 |
{ |
{ |
| 526 |
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); |
| 527 |
} |
} |
| 528 |
|
|
| 529 |
p_user_list_pool = NULL; |
user_list_shm_name[0] = '\0'; |
| 530 |
|
|
| 531 |
if (p_trie_action_dict != NULL) |
if (p_trie_action_dict != NULL) |
| 532 |
{ |
{ |
| 538 |
|
|
| 539 |
int set_user_list_pool_shm_readonly(void) |
int set_user_list_pool_shm_readonly(void) |
| 540 |
{ |
{ |
| 541 |
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) |
|
| 542 |
{ |
{ |
| 543 |
log_error("p_user_list_pool not initialized\n"); |
log_error("mprotect() error (%d)\n", errno); |
| 544 |
return -1; |
return -1; |
| 545 |
} |
} |
| 546 |
|
|
|
shmid = p_user_list_pool->shmid; |
|
|
|
|
|
// Remap shared memory in read-only mode |
|
|
p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP); |
|
|
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; |
|
|
|
|
| 547 |
return 0; |
return 0; |
| 548 |
} |
} |
| 549 |
|
|
| 550 |
int detach_user_list_pool_shm(void) |
int detach_user_list_pool_shm(void) |
| 551 |
{ |
{ |
| 552 |
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) |
| 553 |
{ |
{ |
| 554 |
log_error("shmdt(user_list_pool) error (%d)\n", errno); |
log_error("munmap() error (%d)\n", errno); |
| 555 |
return -1; |
return -1; |
| 556 |
} |
} |
| 557 |
|
|
| 563 |
int user_list_pool_reload(int online_user) |
int user_list_pool_reload(int online_user) |
| 564 |
{ |
{ |
| 565 |
MYSQL *db = NULL; |
MYSQL *db = NULL; |
| 566 |
USER_LIST *p_tmp; |
int tmp; |
|
USER_ONLINE_LIST *p_online_tmp; |
|
| 567 |
int ret = 0; |
int ret = 0; |
| 568 |
|
|
| 569 |
if (p_user_list_pool == NULL) |
if (p_user_list_pool == NULL) |
| 581 |
|
|
| 582 |
if (online_user) |
if (online_user) |
| 583 |
{ |
{ |
| 584 |
if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0) |
if (user_online_list_load(db, &(p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_new])) < 0) |
| 585 |
{ |
{ |
| 586 |
log_error("user_online_list_load() error\n"); |
log_error("user_online_list_load() error\n"); |
| 587 |
ret = -2; |
ret = -2; |
| 597 |
} |
} |
| 598 |
else |
else |
| 599 |
{ |
{ |
| 600 |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
if (user_list_load(db, &(p_user_list_pool->user_list[p_user_list_pool->user_list_index_new])) < 0) |
| 601 |
{ |
{ |
| 602 |
log_error("user_list_load() error\n"); |
log_error("user_list_load() error\n"); |
| 603 |
ret = -2; |
ret = -2; |
| 608 |
mysql_close(db); |
mysql_close(db); |
| 609 |
db = NULL; |
db = NULL; |
| 610 |
|
|
| 611 |
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
if (user_list_rw_lock() < 0) |
| 612 |
{ |
{ |
| 613 |
log_error("user_list_rw_lock() error\n"); |
log_error("user_list_rw_lock() error\n"); |
| 614 |
ret = -3; |
ret = -3; |
| 618 |
if (online_user) |
if (online_user) |
| 619 |
{ |
{ |
| 620 |
// Swap p_online_current and p_online_new |
// Swap p_online_current and p_online_new |
| 621 |
p_online_tmp = p_user_list_pool->p_online_current; |
tmp = p_user_list_pool->user_online_list_index_current; |
| 622 |
p_user_list_pool->p_online_current = p_user_list_pool->p_online_new; |
p_user_list_pool->user_online_list_index_current = p_user_list_pool->user_online_list_index_new; |
| 623 |
p_user_list_pool->p_online_new = p_online_tmp; |
p_user_list_pool->user_online_list_index_new = tmp; |
| 624 |
} |
} |
| 625 |
else |
else |
| 626 |
{ |
{ |
| 627 |
// Swap p_current and p_new |
// Swap index_current and index_new |
| 628 |
p_tmp = p_user_list_pool->p_current; |
tmp = p_user_list_pool->user_list_index_current; |
| 629 |
p_user_list_pool->p_current = p_user_list_pool->p_new; |
p_user_list_pool->user_list_index_current = p_user_list_pool->user_list_index_new; |
| 630 |
p_user_list_pool->p_new = p_tmp; |
p_user_list_pool->user_list_index_new = tmp; |
| 631 |
} |
} |
| 632 |
|
|
| 633 |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rw_unlock() < 0) |
| 634 |
{ |
{ |
| 635 |
log_error("user_list_rw_unlock() error\n"); |
log_error("user_list_rw_unlock() error\n"); |
| 636 |
ret = -3; |
ret = -3; |
| 643 |
return ret; |
return ret; |
| 644 |
} |
} |
| 645 |
|
|
| 646 |
int user_list_try_rd_lock(int semid, int wait_sec) |
int user_list_try_rd_lock(int wait_sec) |
| 647 |
{ |
{ |
| 648 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 649 |
|
#ifndef __CYGWIN__ |
| 650 |
struct timespec timeout; |
struct timespec timeout; |
| 651 |
|
#endif |
| 652 |
int ret; |
int ret; |
| 653 |
|
|
| 654 |
|
if (p_user_list_pool == NULL) |
| 655 |
|
{ |
| 656 |
|
log_error("p_user_list_pool not initialized\n"); |
| 657 |
|
return -1; |
| 658 |
|
} |
| 659 |
|
|
| 660 |
sops[0].sem_num = 1; // w_sem |
sops[0].sem_num = 1; // w_sem |
| 661 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 662 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 665 |
sops[1].sem_op = 1; // lock |
sops[1].sem_op = 1; // lock |
| 666 |
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 667 |
|
|
| 668 |
|
#ifdef __CYGWIN__ |
| 669 |
|
ret = semop(p_user_list_pool->semid, sops, 2); |
| 670 |
|
#else |
| 671 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 672 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 673 |
|
|
| 674 |
ret = semtimedop(semid, sops, 2, &timeout); |
ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout); |
| 675 |
|
#endif |
| 676 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 677 |
{ |
{ |
| 678 |
log_error("semtimedop(lock read) error %d\n", errno); |
log_error("semop(lock read) error %d\n", errno); |
| 679 |
} |
} |
| 680 |
|
|
| 681 |
return ret; |
return ret; |
| 682 |
} |
} |
| 683 |
|
|
| 684 |
int user_list_try_rw_lock(int semid, int wait_sec) |
int user_list_try_rw_lock(int wait_sec) |
| 685 |
{ |
{ |
| 686 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
| 687 |
|
#ifndef __CYGWIN__ |
| 688 |
struct timespec timeout; |
struct timespec timeout; |
| 689 |
|
#endif |
| 690 |
int ret; |
int ret; |
| 691 |
|
|
| 692 |
|
if (p_user_list_pool == NULL) |
| 693 |
|
{ |
| 694 |
|
log_error("p_user_list_pool not initialized\n"); |
| 695 |
|
return -1; |
| 696 |
|
} |
| 697 |
|
|
| 698 |
sops[0].sem_num = 1; // w_sem |
sops[0].sem_num = 1; // w_sem |
| 699 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 700 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 707 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 708 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 709 |
|
|
| 710 |
|
#ifdef __CYGWIN__ |
| 711 |
|
ret = semop(p_user_list_pool->semid, sops, 3); |
| 712 |
|
#else |
| 713 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 714 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 715 |
|
|
| 716 |
ret = semtimedop(semid, sops, 3, &timeout); |
ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout); |
| 717 |
|
#endif |
| 718 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 719 |
{ |
{ |
| 720 |
log_error("semtimedop(lock write) error %d\n", errno); |
log_error("semop(lock write) error %d\n", errno); |
| 721 |
} |
} |
| 722 |
|
|
| 723 |
return ret; |
return ret; |
| 724 |
} |
} |
| 725 |
|
|
| 726 |
int user_list_rd_unlock(int semid) |
int user_list_rd_unlock(void) |
| 727 |
{ |
{ |
| 728 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 729 |
int ret; |
int ret; |
| 730 |
|
|
| 731 |
|
if (p_user_list_pool == NULL) |
| 732 |
|
{ |
| 733 |
|
log_error("p_user_list_pool not initialized\n"); |
| 734 |
|
return -1; |
| 735 |
|
} |
| 736 |
|
|
| 737 |
sops[0].sem_num = 0; // r_sem |
sops[0].sem_num = 0; // r_sem |
| 738 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 739 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 740 |
|
|
| 741 |
ret = semop(semid, sops, 1); |
ret = semop(p_user_list_pool->semid, sops, 1); |
| 742 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 743 |
{ |
{ |
| 744 |
log_error("semop(unlock read) error %d\n", errno); |
log_error("semop(unlock read) error %d\n", errno); |
| 747 |
return ret; |
return ret; |
| 748 |
} |
} |
| 749 |
|
|
| 750 |
int user_list_rw_unlock(int semid) |
int user_list_rw_unlock(void) |
| 751 |
{ |
{ |
| 752 |
struct sembuf sops[1]; |
struct sembuf sops[1]; |
| 753 |
int ret; |
int ret; |
| 754 |
|
|
| 755 |
|
if (p_user_list_pool == NULL) |
| 756 |
|
{ |
| 757 |
|
log_error("p_user_list_pool not initialized\n"); |
| 758 |
|
return -1; |
| 759 |
|
} |
| 760 |
|
|
| 761 |
sops[0].sem_num = 1; // w_sem |
sops[0].sem_num = 1; // w_sem |
| 762 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 763 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 764 |
|
|
| 765 |
ret = semop(semid, sops, 1); |
ret = semop(p_user_list_pool->semid, sops, 1); |
| 766 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 767 |
{ |
{ |
| 768 |
log_error("semop(unlock write) error %d\n", errno); |
log_error("semop(unlock write) error %d\n", errno); |
| 771 |
return ret; |
return ret; |
| 772 |
} |
} |
| 773 |
|
|
| 774 |
int user_list_rd_lock(int semid) |
int user_list_rd_lock(void) |
| 775 |
{ |
{ |
| 776 |
int timer = 0; |
int timer = 0; |
| 777 |
int ret = -1; |
int ret = -1; |
| 778 |
|
|
| 779 |
|
if (p_user_list_pool == NULL) |
| 780 |
|
{ |
| 781 |
|
log_error("p_user_list_pool not initialized\n"); |
| 782 |
|
return -1; |
| 783 |
|
} |
| 784 |
|
|
| 785 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 786 |
{ |
{ |
| 787 |
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); |
| 788 |
if (ret == 0) // success |
if (ret == 0) // success |
| 789 |
{ |
{ |
| 790 |
break; |
break; |
| 807 |
return ret; |
return ret; |
| 808 |
} |
} |
| 809 |
|
|
| 810 |
int user_list_rw_lock(int semid) |
int user_list_rw_lock(void) |
| 811 |
{ |
{ |
| 812 |
int timer = 0; |
int timer = 0; |
| 813 |
int ret = -1; |
int ret = -1; |
| 814 |
|
|
| 815 |
|
if (p_user_list_pool == NULL) |
| 816 |
|
{ |
| 817 |
|
log_error("p_user_list_pool not initialized\n"); |
| 818 |
|
return -1; |
| 819 |
|
} |
| 820 |
|
|
| 821 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 822 |
{ |
{ |
| 823 |
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); |
| 824 |
if (ret == 0) // success |
if (ret == 0) // success |
| 825 |
{ |
{ |
| 826 |
break; |
break; |
| 857 |
*p_page_count = 0; |
*p_page_count = 0; |
| 858 |
|
|
| 859 |
// acquire lock of user list |
// acquire lock of user list |
| 860 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 861 |
{ |
{ |
| 862 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 863 |
return -2; |
return -2; |
| 864 |
} |
} |
| 865 |
|
|
| 866 |
if (p_user_list_pool->p_current->user_count == 0) |
if (p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count == 0) |
| 867 |
{ |
{ |
| 868 |
// empty list |
// empty list |
| 869 |
ret = 0; |
ret = 0; |
| 870 |
goto cleanup; |
goto cleanup; |
| 871 |
} |
} |
| 872 |
|
|
| 873 |
*p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page + |
*p_page_count = (p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count + BBS_user_limit_per_page - 1) / |
| 874 |
(p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1); |
BBS_user_limit_per_page; |
| 875 |
|
|
| 876 |
if (page_id < 0 || page_id >= *p_page_count) |
if (page_id < 0 || page_id >= *p_page_count) |
| 877 |
{ |
{ |
| 881 |
} |
} |
| 882 |
|
|
| 883 |
*p_user_count = MIN(BBS_user_limit_per_page, |
*p_user_count = MIN(BBS_user_limit_per_page, |
| 884 |
p_user_list_pool->p_current->user_count - |
p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - |
| 885 |
page_id * BBS_user_limit_per_page); |
page_id * BBS_user_limit_per_page); |
| 886 |
|
|
| 887 |
memcpy(p_users, |
memcpy(p_users, |
| 888 |
p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page, |
p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users + page_id * BBS_user_limit_per_page, |
| 889 |
sizeof(USER_INFO) * (size_t)(*p_user_count)); |
sizeof(USER_INFO) * (size_t)(*p_user_count)); |
| 890 |
|
|
| 891 |
cleanup: |
cleanup: |
| 892 |
// release lock of user list |
// release lock of user list |
| 893 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 894 |
{ |
{ |
| 895 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 896 |
ret = -1; |
ret = -1; |
| 913 |
*p_page_count = 0; |
*p_page_count = 0; |
| 914 |
|
|
| 915 |
// acquire lock of user list |
// acquire lock of user list |
| 916 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 917 |
{ |
{ |
| 918 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 919 |
return -2; |
return -2; |
| 920 |
} |
} |
| 921 |
|
|
| 922 |
if (p_user_list_pool->p_online_current->user_count == 0) |
if (p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count == 0) |
| 923 |
{ |
{ |
| 924 |
// empty list |
// empty list |
| 925 |
ret = 0; |
ret = 0; |
| 926 |
goto cleanup; |
goto cleanup; |
| 927 |
} |
} |
| 928 |
|
|
| 929 |
*p_page_count = p_user_list_pool->p_online_current->user_count / BBS_user_limit_per_page + |
*p_page_count = (p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count + BBS_user_limit_per_page - 1) / BBS_user_limit_per_page; |
|
(p_user_list_pool->p_online_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1); |
|
| 930 |
|
|
| 931 |
if (page_id < 0 || page_id >= *p_page_count) |
if (page_id < 0 || page_id >= *p_page_count) |
| 932 |
{ |
{ |
| 936 |
} |
} |
| 937 |
|
|
| 938 |
*p_user_count = MIN(BBS_user_limit_per_page, |
*p_user_count = MIN(BBS_user_limit_per_page, |
| 939 |
p_user_list_pool->p_online_current->user_count - |
p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count - |
| 940 |
page_id * BBS_user_limit_per_page); |
page_id * BBS_user_limit_per_page); |
| 941 |
|
|
| 942 |
memcpy(p_online_users, |
memcpy(p_online_users, |
| 943 |
p_user_list_pool->p_online_current->users + page_id * BBS_user_limit_per_page, |
p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users + page_id * BBS_user_limit_per_page, |
| 944 |
sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count)); |
sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count)); |
| 945 |
|
|
| 946 |
cleanup: |
cleanup: |
| 947 |
// release lock of user list |
// release lock of user list |
| 948 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 949 |
{ |
{ |
| 950 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 951 |
ret = -1; |
ret = -1; |
| 963 |
} |
} |
| 964 |
|
|
| 965 |
// acquire lock of user list |
// acquire lock of user list |
| 966 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 967 |
{ |
{ |
| 968 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 969 |
return -2; |
return -2; |
| 970 |
} |
} |
| 971 |
|
|
| 972 |
*p_user_cnt = p_user_list_pool->p_current->user_count; |
*p_user_cnt = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count; |
| 973 |
|
|
| 974 |
// release lock of user list |
// release lock of user list |
| 975 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 976 |
{ |
{ |
| 977 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 978 |
return -2; |
return -2; |
| 990 |
} |
} |
| 991 |
|
|
| 992 |
// acquire lock of user list |
// acquire lock of user list |
| 993 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 994 |
{ |
{ |
| 995 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 996 |
return -2; |
return -2; |
| 997 |
} |
} |
| 998 |
|
|
| 999 |
*p_user_cnt = p_user_list_pool->p_online_current->user_count; |
*p_user_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count; |
| 1000 |
*p_guest_cnt = p_user_list_pool->p_online_current->guest_count; |
*p_guest_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].guest_count; |
| 1001 |
|
|
| 1002 |
// release lock of user list |
// release lock of user list |
| 1003 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1004 |
{ |
{ |
| 1005 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1006 |
return -2; |
return -2; |
| 1033 |
} |
} |
| 1034 |
|
|
| 1035 |
// acquire lock of user list |
// acquire lock of user list |
| 1036 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1037 |
{ |
{ |
| 1038 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1039 |
return -2; |
return -2; |
| 1040 |
} |
} |
| 1041 |
|
|
| 1042 |
if (id >= 0 && id < p_user_list_pool->p_current->user_count) // Found |
if (id >= 0 && id < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count) // Found |
| 1043 |
{ |
{ |
| 1044 |
*p_user = p_user_list_pool->p_current->users[id]; |
*p_user = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id]; |
| 1045 |
ret = 1; |
ret = 1; |
| 1046 |
} |
} |
| 1047 |
|
|
| 1048 |
// release lock of user list |
// release lock of user list |
| 1049 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1050 |
{ |
{ |
| 1051 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1052 |
ret = -1; |
ret = -1; |
| 1070 |
} |
} |
| 1071 |
|
|
| 1072 |
// acquire lock of user list |
// acquire lock of user list |
| 1073 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1074 |
{ |
{ |
| 1075 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1076 |
return -2; |
return -2; |
| 1077 |
} |
} |
| 1078 |
|
|
| 1079 |
left = 0; |
left = 0; |
| 1080 |
right = p_user_list_pool->p_current->user_count - 1; |
right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1; |
| 1081 |
|
|
| 1082 |
while (left < right) |
while (left < right) |
| 1083 |
{ |
{ |
| 1084 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 1085 |
if (uid < p_user_list_pool->p_current->index_uid[mid].uid) |
if (uid < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid) |
| 1086 |
{ |
{ |
| 1087 |
right = mid - 1; |
right = mid - 1; |
| 1088 |
} |
} |
| 1089 |
else if (uid > p_user_list_pool->p_current->index_uid[mid].uid) |
else if (uid > p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid) |
| 1090 |
{ |
{ |
| 1091 |
left = mid + 1; |
left = mid + 1; |
| 1092 |
} |
} |
| 1093 |
else // if (uid == p_user_list_pool->p_current->index_uid[mid].uid) |
else // if (uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid) |
| 1094 |
{ |
{ |
| 1095 |
left = mid; |
left = mid; |
| 1096 |
break; |
break; |
| 1097 |
} |
} |
| 1098 |
} |
} |
| 1099 |
|
|
| 1100 |
if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found |
if (uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left].uid) // Found |
| 1101 |
{ |
{ |
| 1102 |
id = p_user_list_pool->p_current->index_uid[left].id; |
id = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left].id; |
| 1103 |
*p_user = p_user_list_pool->p_current->users[id]; |
*p_user = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id]; |
| 1104 |
ret = 1; |
ret = 1; |
| 1105 |
|
|
| 1106 |
if (p_intro_buf != NULL) |
if (p_intro_buf != NULL) |
| 1107 |
{ |
{ |
| 1108 |
strncpy(p_intro_buf, p_user_list_pool->p_current->users[id].intro, intro_buf_len - 1); |
strncpy(p_intro_buf, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id].intro, intro_buf_len - 1); |
| 1109 |
p_intro_buf[intro_buf_len - 1] = '\0'; |
p_intro_buf[intro_buf_len - 1] = '\0'; |
| 1110 |
p_user->intro = p_intro_buf; |
p_user->intro = p_intro_buf; |
| 1111 |
} |
} |
| 1112 |
} |
} |
| 1113 |
|
|
| 1114 |
// release lock of user list |
// release lock of user list |
| 1115 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1116 |
{ |
{ |
| 1117 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1118 |
ret = -1; |
ret = -1; |
| 1142 |
prefix_len = strlen(username_prefix); |
prefix_len = strlen(username_prefix); |
| 1143 |
|
|
| 1144 |
// acquire lock of user list |
// acquire lock of user list |
| 1145 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1146 |
{ |
{ |
| 1147 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1148 |
return -2; |
return -2; |
| 1149 |
} |
} |
| 1150 |
|
|
| 1151 |
left = 0; |
left = 0; |
| 1152 |
right = p_user_list_pool->p_current->user_count - 1; |
right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1; |
| 1153 |
|
|
| 1154 |
while (left < right) |
while (left < right) |
| 1155 |
{ |
{ |
| 1156 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 1157 |
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len); |
| 1158 |
if (comp < 0) |
if (comp < 0) |
| 1159 |
{ |
{ |
| 1160 |
right = mid - 1; |
right = mid - 1; |
| 1170 |
} |
} |
| 1171 |
} |
} |
| 1172 |
|
|
| 1173 |
if (strncasecmp(username_prefix, p_user_list_pool->p_current->users[left].username, prefix_len) == 0) // Found |
if (strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left].username, prefix_len) == 0) // Found |
| 1174 |
{ |
{ |
| 1175 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 1176 |
log_error("Debug: match found, pos=%d\n", left); |
log_error("Debug: match found, pos=%d\n", left); |
| 1183 |
while (left < right) |
while (left < right) |
| 1184 |
{ |
{ |
| 1185 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 1186 |
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len); |
| 1187 |
if (comp > 0) |
if (comp > 0) |
| 1188 |
{ |
{ |
| 1189 |
left = mid + 1; |
left = mid + 1; |
| 1206 |
|
|
| 1207 |
left = left_save; |
left = left_save; |
| 1208 |
left_save = right; |
left_save = right; |
| 1209 |
right = p_user_list_pool->p_current->user_count - 1; |
right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1; |
| 1210 |
|
|
| 1211 |
while (left < right) |
while (left < right) |
| 1212 |
{ |
{ |
| 1213 |
mid = (left + right) / 2 + (left + right) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
| 1214 |
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len); |
| 1215 |
if (comp < 0) |
if (comp < 0) |
| 1216 |
{ |
{ |
| 1217 |
right = mid - 1; |
right = mid - 1; |
| 1237 |
|
|
| 1238 |
for (i = 0; i < max_user_cnt && left + i <= right; i++) |
for (i = 0; i < max_user_cnt && left + i <= right; i++) |
| 1239 |
{ |
{ |
| 1240 |
uid_list[i] = p_user_list_pool->p_current->users[left + i].uid; |
uid_list[i] = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left + i].uid; |
| 1241 |
strncpy(username_list[i], p_user_list_pool->p_current->users[left + i].username, sizeof(username_list[i]) - 1); |
strncpy(username_list[i], p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left + i].username, sizeof(username_list[i]) - 1); |
| 1242 |
username_list[i][sizeof(username_list[i]) - 1] = '\0'; |
username_list[i][sizeof(username_list[i]) - 1] = '\0'; |
| 1243 |
} |
} |
| 1244 |
ret = i; |
ret = i; |
| 1246 |
|
|
| 1247 |
cleanup: |
cleanup: |
| 1248 |
// release lock of user list |
// release lock of user list |
| 1249 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1250 |
{ |
{ |
| 1251 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1252 |
ret = -1; |
ret = -1; |
| 1266 |
} |
} |
| 1267 |
|
|
| 1268 |
// acquire lock of user list |
// acquire lock of user list |
| 1269 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1270 |
{ |
{ |
| 1271 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1272 |
return -2; |
return -2; |
| 1273 |
} |
} |
| 1274 |
|
|
| 1275 |
if (id >= 0 && id < p_user_list_pool->p_online_current->user_count) // Found |
if (id >= 0 && id < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count) // Found |
| 1276 |
{ |
{ |
| 1277 |
*p_user = p_user_list_pool->p_online_current->users[id]; |
*p_user = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users[id]; |
| 1278 |
ret = 1; |
ret = 1; |
| 1279 |
} |
} |
| 1280 |
|
|
| 1281 |
// release lock of user list |
// release lock of user list |
| 1282 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1283 |
{ |
{ |
| 1284 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1285 |
ret = -1; |
ret = -1; |
| 1308 |
*p_user_cnt = 0; |
*p_user_cnt = 0; |
| 1309 |
|
|
| 1310 |
// acquire lock of user list |
// acquire lock of user list |
| 1311 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1312 |
{ |
{ |
| 1313 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1314 |
return -2; |
return -2; |
| 1315 |
} |
} |
| 1316 |
|
|
| 1317 |
left = start_id; |
left = start_id; |
| 1318 |
right = p_user_list_pool->p_online_current->user_count - 1; |
right = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count - 1; |
| 1319 |
|
|
| 1320 |
while (left < right) |
while (left < right) |
| 1321 |
{ |
{ |
| 1322 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 1323 |
if (uid < p_user_list_pool->p_online_current->index_uid[mid].uid) |
if (uid < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid) |
| 1324 |
{ |
{ |
| 1325 |
right = mid - 1; |
right = mid - 1; |
| 1326 |
} |
} |
| 1327 |
else if (uid > p_user_list_pool->p_online_current->index_uid[mid].uid) |
else if (uid > p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid) |
| 1328 |
{ |
{ |
| 1329 |
left = mid + 1; |
left = mid + 1; |
| 1330 |
} |
} |
| 1331 |
else // if (uid == p_user_list_pool->p_online_current->index_uid[mid].uid) |
else // if (uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid) |
| 1332 |
{ |
{ |
| 1333 |
left = mid; |
left = mid; |
| 1334 |
break; |
break; |
| 1335 |
} |
} |
| 1336 |
} |
} |
| 1337 |
|
|
| 1338 |
if (uid == p_user_list_pool->p_online_current->index_uid[left].uid) |
if (uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].uid) |
| 1339 |
{ |
{ |
| 1340 |
right = left; |
right = left; |
| 1341 |
left = start_id; |
left = start_id; |
| 1343 |
while (left < right) |
while (left < right) |
| 1344 |
{ |
{ |
| 1345 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 1346 |
if (uid - 1 < p_user_list_pool->p_online_current->index_uid[mid].uid) |
if (uid - 1 < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid) |
| 1347 |
{ |
{ |
| 1348 |
right = mid; |
right = mid; |
| 1349 |
} |
} |
| 1350 |
else // if (uid - 1 >= p_user_list_pool->p_online_current->index_uid[mid].uid) |
else // if (uid - 1 >= p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid) |
| 1351 |
{ |
{ |
| 1352 |
left = mid + 1; |
left = mid + 1; |
| 1353 |
} |
} |
| 1354 |
} |
} |
| 1355 |
|
|
| 1356 |
for (i = 0; |
for (i = 0; |
| 1357 |
left < p_user_list_pool->p_online_current->user_count && i < user_cnt && |
left < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count && i < user_cnt && |
| 1358 |
uid == p_user_list_pool->p_online_current->index_uid[left].uid; |
uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].uid; |
| 1359 |
left++, i++) |
left++, i++) |
| 1360 |
{ |
{ |
| 1361 |
id = p_user_list_pool->p_online_current->index_uid[left].id; |
id = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].id; |
| 1362 |
p_users[i] = p_user_list_pool->p_online_current->users[id]; |
p_users[i] = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users[id]; |
| 1363 |
} |
} |
| 1364 |
|
|
| 1365 |
if (i > 0) |
if (i > 0) |
| 1370 |
} |
} |
| 1371 |
|
|
| 1372 |
// release lock of user list |
// release lock of user list |
| 1373 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1374 |
{ |
{ |
| 1375 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1376 |
ret = -1; |
ret = -1; |
| 1394 |
} |
} |
| 1395 |
|
|
| 1396 |
// acquire lock of user list |
// acquire lock of user list |
| 1397 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock() < 0) |
| 1398 |
{ |
{ |
| 1399 |
log_error("user_list_rd_lock() error\n"); |
log_error("user_list_rd_lock() error\n"); |
| 1400 |
return -2; |
return -2; |
| 1401 |
} |
} |
| 1402 |
|
|
| 1403 |
left = 0; |
left = 0; |
| 1404 |
right = p_user_list_pool->p_current->user_count - 1; |
right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1; |
| 1405 |
|
|
| 1406 |
while (left < right) |
while (left < right) |
| 1407 |
{ |
{ |
| 1408 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 1409 |
if (start_uid < p_user_list_pool->p_current->index_uid[mid].uid) |
if (start_uid < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid) |
| 1410 |
{ |
{ |
| 1411 |
right = mid - 1; |
right = mid - 1; |
| 1412 |
} |
} |
| 1413 |
else if (start_uid > p_user_list_pool->p_current->index_uid[mid].uid) |
else if (start_uid > p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid) |
| 1414 |
{ |
{ |
| 1415 |
left = mid + 1; |
left = mid + 1; |
| 1416 |
} |
} |
| 1417 |
else // if (start_uid == p_user_list_pool->p_current->index_uid[mid].uid) |
else // if (start_uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid) |
| 1418 |
{ |
{ |
| 1419 |
left = mid; |
left = mid; |
| 1420 |
break; |
break; |
| 1421 |
} |
} |
| 1422 |
} |
} |
| 1423 |
|
|
| 1424 |
for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->p_current->user_count; i++) |
for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count; i++) |
| 1425 |
{ |
{ |
| 1426 |
p_uid_list[i] = p_user_list_pool->p_current->index_uid[left + i].uid; |
p_uid_list[i] = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left + i].uid; |
| 1427 |
} |
} |
| 1428 |
*p_user_cnt = i; |
*p_user_cnt = i; |
| 1429 |
|
|
| 1430 |
// release lock of user list |
// release lock of user list |
| 1431 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock() < 0) |
| 1432 |
{ |
{ |
| 1433 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1434 |
ret = -1; |
ret = -1; |