/[LeafOK_CVS]/lbbs/src/user_list.c
ViewVC logotype

Diff of /lbbs/src/user_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.26 by sysadm, Tue Nov 4 13:49:51 2025 UTC Revision 1.40 by sysadm, Thu Nov 20 02:56:46 2025 UTC
# Line 3  Line 3 
3   * user_list   * user_list
4   *   - data model and basic operations of (online) user list   *   - data model and basic operations of (online) user list
5   *   *
6   * Copyright (C) 2004-2025 by 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"
# Line 13  Line 17 
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 */
# Line 31  union semun Line 35  union semun
35          struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
36                                                            (Linux-specific) */                                                            (Linux-specific) */
37  };  };
38  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #if defined(_SEM_SEMUN_UNDEFINED)
39    
40  #define USER_LIST_TRY_LOCK_WAIT_TIME 1 // second  enum _user_list_constant_t
41  #define USER_LIST_TRY_LOCK_TIMES 10  {
42            USER_LIST_TRY_LOCK_WAIT_TIME = 1, // second
43            USER_LIST_TRY_LOCK_TIMES = 10,
44    };
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_PATH_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    
# Line 78  const USER_ACTION_MAP user_action_map[] Line 86  const USER_ACTION_MAP user_action_map[]
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);
# Line 181  int user_list_load(MYSQL *db, USER_LIST Line 189  int user_list_load(MYSQL *db, USER_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                  {                  {
# Line 335  int user_online_list_load(MYSQL *db, USE Line 343  int user_online_list_load(MYSQL *db, USE
343                  }                  }
344    
345                  qsort(p_online_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);                  qsort(p_online_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
   
 #ifdef _DEBUG  
                 log_error("Rebuild index of %d online users\n", user_cnt);  
 #endif  
346          }          }
347    
348          p_online_list->user_count = user_cnt;          p_online_list->user_count = user_cnt;
349          p_online_list->guest_count = guest_cnt;          p_online_list->guest_count = guest_cnt;
350    
 #ifdef _DEBUG  
         log_error("Loaded %d online users and %d guest users\n", p_list->user_count, p_list->guest_count);  
 #endif  
   
351  cleanup:  cleanup:
352          mysql_free_result(rs);          mysql_free_result(rs);
353    
# Line 389  int user_login_count_load(MYSQL *db) Line 389  int user_login_count_load(MYSQL *db)
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    
# Line 420  int user_list_pool_init(const char *file Line 421  int user_list_pool_init(const char *file
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)
# Line 471  int user_list_pool_init(const char *file Line 496  int user_list_pool_init(const char *file
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    
507          return 0;          return 0;
508  }  }
509    
510  void user_list_pool_cleanup(void)  int 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 -1;
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 (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          {          {
# Line 516  void user_list_pool_cleanup(void) Line 534  void user_list_pool_cleanup(void)
534    
535                  p_trie_action_dict = NULL;                  p_trie_action_dict = NULL;
536          }          }
537    
538            return 0;
539  }  }
540    
541  int set_user_list_pool_shm_readonly(void)  int set_user_list_pool_shm_readonly(void)
542  {  {
543          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)  
544          {          {
545                  log_error("p_user_list_pool not initialized\n");                  log_error("mprotect() error (%d)\n", errno);
546                  return -1;                  return -1;
547          }          }
548    
         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;  
   
549          return 0;          return 0;
550  }  }
551    
552  int detach_user_list_pool_shm(void)  int detach_user_list_pool_shm(void)
553  {  {
554          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)
555          {          {
556                  log_error("shmdt(user_list_pool) error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
557                  return -1;                  return -1;
558          }          }
559    
# Line 560  int detach_user_list_pool_shm(void) Line 565  int detach_user_list_pool_shm(void)
565  int user_list_pool_reload(int online_user)  int user_list_pool_reload(int online_user)
566  {  {
567          MYSQL *db = NULL;          MYSQL *db = NULL;
568          USER_LIST *p_tmp;          int tmp;
         USER_ONLINE_LIST *p_online_tmp;  
569          int ret = 0;          int ret = 0;
570    
571          if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
# Line 579  int user_list_pool_reload(int online_use Line 583  int user_list_pool_reload(int online_use
583    
584          if (online_user)          if (online_user)
585          {          {
586                  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)
587                  {                  {
588                          log_error("user_online_list_load() error\n");                          log_error("user_online_list_load() error\n");
589                          ret = -2;                          ret = -2;
# Line 595  int user_list_pool_reload(int online_use Line 599  int user_list_pool_reload(int online_use
599          }          }
600          else          else
601          {          {
602                  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)
603                  {                  {
604                          log_error("user_list_load() error\n");                          log_error("user_list_load() error\n");
605                          ret = -2;                          ret = -2;
# Line 606  int user_list_pool_reload(int online_use Line 610  int user_list_pool_reload(int online_use
610          mysql_close(db);          mysql_close(db);
611          db = NULL;          db = NULL;
612    
613          if (user_list_rw_lock(p_user_list_pool->semid) < 0)          if (user_list_rw_lock() < 0)
614          {          {
615                  log_error("user_list_rw_lock() error\n");                  log_error("user_list_rw_lock() error\n");
616                  ret = -3;                  ret = -3;
# Line 616  int user_list_pool_reload(int online_use Line 620  int user_list_pool_reload(int online_use
620          if (online_user)          if (online_user)
621          {          {
622                  // Swap p_online_current and p_online_new                  // Swap p_online_current and p_online_new
623                  p_online_tmp = p_user_list_pool->p_online_current;                  tmp = p_user_list_pool->user_online_list_index_current;
624                  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;
625                  p_user_list_pool->p_online_new = p_online_tmp;                  p_user_list_pool->user_online_list_index_new = tmp;
626          }          }
627          else          else
628          {          {
629                  // Swap p_current and p_new                  // Swap index_current and index_new
630                  p_tmp = p_user_list_pool->p_current;                  tmp = p_user_list_pool->user_list_index_current;
631                  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;
632                  p_user_list_pool->p_new = p_tmp;                  p_user_list_pool->user_list_index_new = tmp;
633          }          }
634    
635          if (user_list_rw_unlock(p_user_list_pool->semid) < 0)          if (user_list_rw_unlock() < 0)
636          {          {
637                  log_error("user_list_rw_unlock() error\n");                  log_error("user_list_rw_unlock() error\n");
638                  ret = -3;                  ret = -3;
# Line 641  cleanup: Line 645  cleanup:
645          return ret;          return ret;
646  }  }
647    
648  int user_list_try_rd_lock(int semid, int wait_sec)  int user_list_try_rd_lock(int wait_sec)
649  {  {
650          struct sembuf sops[2];          struct sembuf sops[2];
651    #ifndef __CYGWIN__
652          struct timespec timeout;          struct timespec timeout;
653    #endif
654          int ret;          int ret;
655    
656            if (p_user_list_pool == NULL)
657            {
658                    log_error("p_user_list_pool not initialized\n");
659                    return -1;
660            }
661    
662          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
663          sops[0].sem_op = 0;      // wait until unlocked          sops[0].sem_op = 0;      // wait until unlocked
664          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 655  int user_list_try_rd_lock(int semid, int Line 667  int user_list_try_rd_lock(int semid, int
667          sops[1].sem_op = 1;                     // lock          sops[1].sem_op = 1;                     // lock
668          sops[1].sem_flg = SEM_UNDO; // undo on terminate          sops[1].sem_flg = SEM_UNDO; // undo on terminate
669    
670    #ifdef __CYGWIN__
671            ret = semop(p_user_list_pool->semid, sops, 2);
672    #else
673          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
674          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
675    
676          ret = semtimedop(semid, sops, 2, &timeout);          ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);
677    #endif
678          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
679          {          {
680                  log_error("semtimedop(lock read) error %d\n", errno);                  log_error("semop(lock read) error %d\n", errno);
681          }          }
682    
683          return ret;          return ret;
684  }  }
685    
686  int user_list_try_rw_lock(int semid, int wait_sec)  int user_list_try_rw_lock(int wait_sec)
687  {  {
688          struct sembuf sops[3];          struct sembuf sops[3];
689    #ifndef __CYGWIN__
690          struct timespec timeout;          struct timespec timeout;
691    #endif
692          int ret;          int ret;
693    
694            if (p_user_list_pool == NULL)
695            {
696                    log_error("p_user_list_pool not initialized\n");
697                    return -1;
698            }
699    
700          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
701          sops[0].sem_op = 0;      // wait until unlocked          sops[0].sem_op = 0;      // wait until unlocked
702          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 685  int user_list_try_rw_lock(int semid, int Line 709  int user_list_try_rw_lock(int semid, int
709          sops[2].sem_op = 0;      // wait until unlocked          sops[2].sem_op = 0;      // wait until unlocked
710          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
711    
712    #ifdef __CYGWIN__
713            ret = semop(p_user_list_pool->semid, sops, 3);
714    #else
715          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
716          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
717    
718          ret = semtimedop(semid, sops, 3, &timeout);          ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);
719    #endif
720          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
721          {          {
722                  log_error("semtimedop(lock write) error %d\n", errno);                  log_error("semop(lock write) error %d\n", errno);
723          }          }
724    
725          return ret;          return ret;
726  }  }
727    
728  int user_list_rd_unlock(int semid)  int user_list_rd_unlock(void)
729  {  {
730          struct sembuf sops[2];          struct sembuf sops[2];
731          int ret;          int ret;
732    
733            if (p_user_list_pool == NULL)
734            {
735                    log_error("p_user_list_pool not initialized\n");
736                    return -1;
737            }
738    
739          sops[0].sem_num = 0;                                     // r_sem          sops[0].sem_num = 0;                                     // r_sem
740          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                     // unlock
741          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
742    
743          ret = semop(semid, sops, 1);          ret = semop(p_user_list_pool->semid, sops, 1);
744          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
745          {          {
746                  log_error("semop(unlock read) error %d\n", errno);                  log_error("semop(unlock read) error %d\n", errno);
# Line 715  int user_list_rd_unlock(int semid) Line 749  int user_list_rd_unlock(int semid)
749          return ret;          return ret;
750  }  }
751    
752  int user_list_rw_unlock(int semid)  int user_list_rw_unlock(void)
753  {  {
754          struct sembuf sops[1];          struct sembuf sops[1];
755          int ret;          int ret;
756    
757            if (p_user_list_pool == NULL)
758            {
759                    log_error("p_user_list_pool not initialized\n");
760                    return -1;
761            }
762    
763          sops[0].sem_num = 1;                                     // w_sem          sops[0].sem_num = 1;                                     // w_sem
764          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                     // unlock
765          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
766    
767          ret = semop(semid, sops, 1);          ret = semop(p_user_list_pool->semid, sops, 1);
768          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
769          {          {
770                  log_error("semop(unlock write) error %d\n", errno);                  log_error("semop(unlock write) error %d\n", errno);
# Line 733  int user_list_rw_unlock(int semid) Line 773  int user_list_rw_unlock(int semid)
773          return ret;          return ret;
774  }  }
775    
776  int user_list_rd_lock(int semid)  int user_list_rd_lock(void)
777  {  {
778          int timer = 0;          int timer = 0;
779          int ret = -1;          int ret = -1;
780    
781            if (p_user_list_pool == NULL)
782            {
783                    log_error("p_user_list_pool not initialized\n");
784                    return -1;
785            }
786    
787          while (!SYS_server_exit)          while (!SYS_server_exit)
788          {          {
789                  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);
790                  if (ret == 0) // success                  if (ret == 0) // success
791                  {                  {
792                          break;                          break;
# Line 763  int user_list_rd_lock(int semid) Line 809  int user_list_rd_lock(int semid)
809          return ret;          return ret;
810  }  }
811    
812  int user_list_rw_lock(int semid)  int user_list_rw_lock(void)
813  {  {
814          int timer = 0;          int timer = 0;
815          int ret = -1;          int ret = -1;
816    
817            if (p_user_list_pool == NULL)
818            {
819                    log_error("p_user_list_pool not initialized\n");
820                    return -1;
821            }
822    
823          while (!SYS_server_exit)          while (!SYS_server_exit)
824          {          {
825                  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);
826                  if (ret == 0) // success                  if (ret == 0) // success
827                  {                  {
828                          break;                          break;
# Line 807  int query_user_list(int page_id, USER_IN Line 859  int query_user_list(int page_id, USER_IN
859          *p_page_count = 0;          *p_page_count = 0;
860    
861          // acquire lock of user list          // acquire lock of user list
862          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
863          {          {
864                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
865                  return -2;                  return -2;
866          }          }
867    
868          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)
869          {          {
870                  // empty list                  // empty list
871                  ret = 0;                  ret = 0;
872                  goto cleanup;                  goto cleanup;
873          }          }
874    
875          *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) /
876                                          (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);                                          BBS_user_limit_per_page;
877    
878          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
879          {          {
# Line 831  int query_user_list(int page_id, USER_IN Line 883  int query_user_list(int page_id, USER_IN
883          }          }
884    
885          *p_user_count = MIN(BBS_user_limit_per_page,          *p_user_count = MIN(BBS_user_limit_per_page,
886                                                  p_user_list_pool->p_current->user_count -                                                  p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count -
887                                                          page_id * BBS_user_limit_per_page);                                                          page_id * BBS_user_limit_per_page);
888    
889          memcpy(p_users,          memcpy(p_users,
890                     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,
891                     sizeof(USER_INFO) * (size_t)(*p_user_count));                     sizeof(USER_INFO) * (size_t)(*p_user_count));
892    
893  cleanup:  cleanup:
894          // release lock of user list          // release lock of user list
895          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
896          {          {
897                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
898                  ret = -1;                  ret = -1;
# Line 863  int query_user_online_list(int page_id, Line 915  int query_user_online_list(int page_id,
915          *p_page_count = 0;          *p_page_count = 0;
916    
917          // acquire lock of user list          // acquire lock of user list
918          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
919          {          {
920                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
921                  return -2;                  return -2;
922          }          }
923    
924          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)
925          {          {
926                  // empty list                  // empty list
927                  ret = 0;                  ret = 0;
928                  goto cleanup;                  goto cleanup;
929          }          }
930    
931          *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);  
932    
933          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
934          {          {
# Line 887  int query_user_online_list(int page_id, Line 938  int query_user_online_list(int page_id,
938          }          }
939    
940          *p_user_count = MIN(BBS_user_limit_per_page,          *p_user_count = MIN(BBS_user_limit_per_page,
941                                                  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 -
942                                                          page_id * BBS_user_limit_per_page);                                                          page_id * BBS_user_limit_per_page);
943    
944          memcpy(p_online_users,          memcpy(p_online_users,
945                     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,
946                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));
947    
948  cleanup:  cleanup:
949          // release lock of user list          // release lock of user list
950          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
951          {          {
952                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
953                  ret = -1;                  ret = -1;
# Line 914  int get_user_list_count(int *p_user_cnt) Line 965  int get_user_list_count(int *p_user_cnt)
965          }          }
966    
967          // acquire lock of user list          // acquire lock of user list
968          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
969          {          {
970                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
971                  return -2;                  return -2;
972          }          }
973    
974          *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;
975    
976          // release lock of user list          // release lock of user list
977          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
978          {          {
979                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
980                  return -2;                  return -2;
# Line 941  int get_user_online_list_count(int *p_us Line 992  int get_user_online_list_count(int *p_us
992          }          }
993    
994          // acquire lock of user list          // acquire lock of user list
995          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
996          {          {
997                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
998                  return -2;                  return -2;
999          }          }
1000    
1001          *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;
1002          *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;
1003    
1004          // release lock of user list          // release lock of user list
1005          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1006          {          {
1007                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1008                  return -2;                  return -2;
# Line 984  int query_user_info(int32_t id, USER_INF Line 1035  int query_user_info(int32_t id, USER_INF
1035          }          }
1036    
1037          // acquire lock of user list          // acquire lock of user list
1038          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1039          {          {
1040                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1041                  return -2;                  return -2;
1042          }          }
1043    
1044          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
1045          {          {
1046                  *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];
1047                  ret = 1;                  ret = 1;
1048          }          }
1049    
1050          // release lock of user list          // release lock of user list
1051          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1052          {          {
1053                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1054                  ret = -1;                  ret = -1;
# Line 1021  int query_user_info_by_uid(int32_t uid, Line 1072  int query_user_info_by_uid(int32_t uid,
1072          }          }
1073    
1074          // acquire lock of user list          // acquire lock of user list
1075          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1076          {          {
1077                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1078                  return -2;                  return -2;
1079          }          }
1080    
1081          left = 0;          left = 0;
1082          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;
1083    
1084          while (left < right)          while (left < right)
1085          {          {
1086                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1087                  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)
1088                  {                  {
1089                          right = mid - 1;                          right = mid - 1;
1090                  }                  }
1091                  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)
1092                  {                  {
1093                          left = mid + 1;                          left = mid + 1;
1094                  }                  }
1095                  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)
1096                  {                  {
1097                          left = mid;                          left = mid;
1098                          break;                          break;
1099                  }                  }
1100          }          }
1101    
1102          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
1103          {          {
1104                  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;
1105                  *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];
1106                  ret = 1;                  ret = 1;
1107    
1108                  if (p_intro_buf != NULL)                  if (p_intro_buf != NULL)
1109                  {                  {
1110                          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);
1111                          p_intro_buf[intro_buf_len - 1] = '\0';                          p_intro_buf[intro_buf_len - 1] = '\0';
1112                          p_user->intro = p_intro_buf;                          p_user->intro = p_intro_buf;
1113                  }                  }
1114          }          }
1115    
1116          // release lock of user list          // release lock of user list
1117          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1118          {          {
1119                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1120                  ret = -1;                  ret = -1;
# Line 1093  int query_user_info_by_username(const ch Line 1144  int query_user_info_by_username(const ch
1144          prefix_len = strlen(username_prefix);          prefix_len = strlen(username_prefix);
1145    
1146          // acquire lock of user list          // acquire lock of user list
1147          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1148          {          {
1149                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1150                  return -2;                  return -2;
1151          }          }
1152    
1153          left = 0;          left = 0;
1154          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;
1155    
1156          while (left < right)          while (left < right)
1157          {          {
1158                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1159                  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);
1160                  if (comp < 0)                  if (comp < 0)
1161                  {                  {
1162                          right = mid - 1;                          right = mid - 1;
# Line 1121  int query_user_info_by_username(const ch Line 1172  int query_user_info_by_username(const ch
1172                  }                  }
1173          }          }
1174    
1175          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
1176          {          {
1177  #ifdef _DEBUG  #ifdef _DEBUG
1178                  log_error("Debug: match found, pos=%d\n", left);                  log_error("Debug: match found, pos=%d\n", left);
# Line 1134  int query_user_info_by_username(const ch Line 1185  int query_user_info_by_username(const ch
1185                  while (left < right)                  while (left < right)
1186                  {                  {
1187                          mid = (left + right) / 2;                          mid = (left + right) / 2;
1188                          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);
1189                          if (comp > 0)                          if (comp > 0)
1190                          {                          {
1191                                  left = mid + 1;                                  left = mid + 1;
# Line 1157  int query_user_info_by_username(const ch Line 1208  int query_user_info_by_username(const ch
1208    
1209                  left = left_save;                  left = left_save;
1210                  left_save = right;                  left_save = right;
1211                  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;
1212    
1213                  while (left < right)                  while (left < right)
1214                  {                  {
1215                          mid = (left + right) / 2 + (left + right) % 2;                          mid = (left + right) / 2 + (left + right) % 2;
1216                          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);
1217                          if (comp < 0)                          if (comp < 0)
1218                          {                          {
1219                                  right = mid - 1;                                  right = mid - 1;
# Line 1188  int query_user_info_by_username(const ch Line 1239  int query_user_info_by_username(const ch
1239    
1240                  for (i = 0; i < max_user_cnt && left + i <= right; i++)                  for (i = 0; i < max_user_cnt && left + i <= right; i++)
1241                  {                  {
1242                          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;
1243                          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);
1244                          username_list[i][sizeof(username_list[i]) - 1] = '\0';                          username_list[i][sizeof(username_list[i]) - 1] = '\0';
1245                  }                  }
1246                  ret = i;                  ret = i;
# Line 1197  int query_user_info_by_username(const ch Line 1248  int query_user_info_by_username(const ch
1248    
1249  cleanup:  cleanup:
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;
# Line 1217  int query_user_online_info(int32_t id, U Line 1268  int query_user_online_info(int32_t id, U
1268          }          }
1269    
1270          // acquire lock of user list          // acquire lock of user list
1271          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1272          {          {
1273                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1274                  return -2;                  return -2;
1275          }          }
1276    
1277          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
1278          {          {
1279                  *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];
1280                  ret = 1;                  ret = 1;
1281          }          }
1282    
1283          // release lock of user list          // release lock of user list
1284          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1285          {          {
1286                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1287                  ret = -1;                  ret = -1;
# Line 1259  int query_user_online_info_by_uid(int32_ Line 1310  int query_user_online_info_by_uid(int32_
1310          *p_user_cnt = 0;          *p_user_cnt = 0;
1311    
1312          // acquire lock of user list          // acquire lock of user list
1313          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1314          {          {
1315                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1316                  return -2;                  return -2;
1317          }          }
1318    
1319          left = start_id;          left = start_id;
1320          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;
1321    
1322          while (left < right)          while (left < right)
1323          {          {
1324                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1325                  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)
1326                  {                  {
1327                          right = mid - 1;                          right = mid - 1;
1328                  }                  }
1329                  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)
1330                  {                  {
1331                          left = mid + 1;                          left = mid + 1;
1332                  }                  }
1333                  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)
1334                  {                  {
1335                          left = mid;                          left = mid;
1336                          break;                          break;
1337                  }                  }
1338          }          }
1339    
1340          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)
1341          {          {
1342                  right = left;                  right = left;
1343                  left = start_id;                  left = start_id;
# Line 1294  int query_user_online_info_by_uid(int32_ Line 1345  int query_user_online_info_by_uid(int32_
1345                  while (left < right)                  while (left < right)
1346                  {                  {
1347                          mid = (left + right) / 2;                          mid = (left + right) / 2;
1348                          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)
1349                          {                          {
1350                                  right = mid;                                  right = mid;
1351                          }                          }
1352                          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)
1353                          {                          {
1354                                  left = mid + 1;                                  left = mid + 1;
1355                          }                          }
1356                  }                  }
1357    
1358                  for (i = 0;                  for (i = 0;
1359                           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 &&
1360                           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;
1361                           left++, i++)                           left++, i++)
1362                  {                  {
1363                          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;
1364                          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];
1365                  }                  }
1366    
1367                  if (i > 0)                  if (i > 0)
# Line 1321  int query_user_online_info_by_uid(int32_ Line 1372  int query_user_online_info_by_uid(int32_
1372          }          }
1373    
1374          // release lock of user list          // release lock of user list
1375          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1376          {          {
1377                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1378                  ret = -1;                  ret = -1;
# Line 1345  int get_user_id_list(int32_t *p_uid_list Line 1396  int get_user_id_list(int32_t *p_uid_list
1396          }          }
1397    
1398          // acquire lock of user list          // acquire lock of user list
1399          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1400          {          {
1401                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1402                  return -2;                  return -2;
1403          }          }
1404    
1405          left = 0;          left = 0;
1406          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;
1407    
1408          while (left < right)          while (left < right)
1409          {          {
1410                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1411                  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)
1412                  {                  {
1413                          right = mid - 1;                          right = mid - 1;
1414                  }                  }
1415                  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)
1416                  {                  {
1417                          left = mid + 1;                          left = mid + 1;
1418                  }                  }
1419                  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)
1420                  {                  {
1421                          left = mid;                          left = mid;
1422                          break;                          break;
1423                  }                  }
1424          }          }
1425    
1426          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++)
1427          {          {
1428                  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;
1429          }          }
1430          *p_user_cnt = i;          *p_user_cnt = i;
1431    
1432          // release lock of user list          // release lock of user list
1433          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1434          {          {
1435                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1436                  ret = -1;                  ret = -1;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1