/[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.44 by sysadm, Thu Nov 20 11:31:56 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/stat.h>
27    
28    #ifdef HAVE_SYSTEM_V
29  #include <sys/sem.h>  #include <sys/sem.h>
 #include <sys/shm.h>  
30    
31  #ifdef _SEM_SEMUN_UNDEFINED  #ifdef _SEM_SEMUN_UNDEFINED
32  union semun  union semun
# Line 29  union semun Line 35  union semun
35          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
36          unsigned short *array; /* Array for GETALL, SETALL */          unsigned short *array; /* Array for GETALL, SETALL */
37          struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
38                                                            (Linux-specific) */                                                          (Linux-specific) */
39  };  };
40  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
41    
42  #define USER_LIST_TRY_LOCK_WAIT_TIME 1 // second  #else
43  #define USER_LIST_TRY_LOCK_TIMES 10  #include <semaphore.h>
44    #endif
45    
46    enum _user_list_constant_t
47    {
48            USER_LIST_TRY_LOCK_WAIT_TIME = 1, // second
49            USER_LIST_TRY_LOCK_TIMES = 10,
50            USER_LIST_DEAD_LOCK_TIMEOUT = 15, // second
51    };
52    
53  struct user_list_pool_t  struct user_list_pool_t
54  {  {
55          int shmid;          size_t shm_size;
56    #ifndef HAVE_SYSTEM_V
57            sem_t sem;
58            uint16_t read_lock_count;
59            uint16_t write_lock_count;
60    #else
61          int semid;          int semid;
62    #endif
63          USER_LIST user_list[2];          USER_LIST user_list[2];
64          USER_LIST *p_current;          int user_list_index_current;
65          USER_LIST *p_new;          int user_list_index_new;
66          USER_ONLINE_LIST user_online_list[2];          USER_ONLINE_LIST user_online_list[2];
67          USER_ONLINE_LIST *p_online_current;          int user_online_list_index_current;
68          USER_ONLINE_LIST *p_online_new;          int user_online_list_index_new;
69          USER_STAT_MAP user_stat_map;          USER_STAT_MAP user_stat_map;
70          int user_login_count;          int user_login_count;
71  };  };
72  typedef struct user_list_pool_t USER_LIST_POOL;  typedef struct user_list_pool_t USER_LIST_POOL;
73    
74    static char user_list_shm_name[FILE_NAME_LEN];
75  static USER_LIST_POOL *p_user_list_pool = NULL;  static USER_LIST_POOL *p_user_list_pool = NULL;
76  static TRIE_NODE *p_trie_action_dict = NULL;  static TRIE_NODE *p_trie_action_dict = NULL;
77    
# Line 78  const USER_ACTION_MAP user_action_map[] Line 99  const USER_ACTION_MAP user_action_map[]
99    
100  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);
101    
102  static int user_list_try_rd_lock(int semid, int wait_sec);  static int user_list_try_rd_lock(int wait_sec);
103  static int user_list_try_rw_lock(int semid, int wait_sec);  static int user_list_try_rw_lock(int wait_sec);
104  static int user_list_rd_unlock(int semid);  static int user_list_rd_unlock(void);
105  static int user_list_rw_unlock(int semid);  static int user_list_rw_unlock(void);
106  static int user_list_rd_lock(int semid);  static int user_list_rd_lock(void);
107  static int user_list_rw_lock(int semid);  static int user_list_rw_lock(void);
108    #ifndef HAVE_SYSTEM_V
109    static int user_list_reset_lock(void);
110    #endif
111    
112  static int user_list_load(MYSQL *db, USER_LIST *p_list);  static int user_list_load(MYSQL *db, USER_LIST *p_list);
113  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 205  int user_list_load(MYSQL *db, USER_LIST
205                  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]));
206                  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]));
207                  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]));
208                  p_list->users[i].birthday = (row[10] == NULL ? 0 : atol(row[11]));                  p_list->users[i].birthday = (row[11] == NULL ? 0 : atol(row[11]));
209                  intro_len = strlen((row[12] == NULL ? "" : row[12]));                  intro_len = strlen((row[12] == NULL ? "" : row[12]));
210                  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)
211                  {                  {
# Line 335  int user_online_list_load(MYSQL *db, USE Line 359  int user_online_list_load(MYSQL *db, USE
359                  }                  }
360    
361                  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  
362          }          }
363    
364          p_online_list->user_count = user_cnt;          p_online_list->user_count = user_cnt;
365          p_online_list->guest_count = guest_cnt;          p_online_list->guest_count = guest_cnt;
366    
 #ifdef _DEBUG  
         log_error("Loaded %d online users and %d guest users\n", p_list->user_count, p_list->guest_count);  
 #endif  
   
367  cleanup:  cleanup:
368          mysql_free_result(rs);          mysql_free_result(rs);
369    
# Line 389  int user_login_count_load(MYSQL *db) Line 405  int user_login_count_load(MYSQL *db)
405    
406  int user_list_pool_init(const char *filename)  int user_list_pool_init(const char *filename)
407  {  {
408          int shmid;          char filepath[FILE_PATH_LEN];
409          int semid;          int fd;
         int proj_id;  
         key_t key;  
410          size_t size;          size_t size;
411          void *p_shm;          void *p_shm;
412    #ifdef HAVE_SYSTEM_V
413            int proj_id;
414            key_t key;
415            int semid;
416          union semun arg;          union semun arg;
417    #endif
418          int i;          int i;
419    
420          if (p_user_list_pool != NULL || p_trie_action_dict != NULL)          if (p_user_list_pool != NULL || p_trie_action_dict != NULL)
# Line 420  int user_list_pool_init(const char *file Line 439  int user_list_pool_init(const char *file
439          }          }
440    
441          // Allocate shared memory          // Allocate shared memory
442          proj_id = (int)(time(NULL) % getpid());          size = sizeof(USER_LIST_POOL);
443          key = ftok(filename, proj_id);  
444          if (key == -1)          strncpy(filepath, filename, sizeof(filepath) - 1);
445            filepath[sizeof(filepath) - 1] = '\0';
446            snprintf(user_list_shm_name, sizeof(user_list_shm_name), "/USER_LIST_SHM_%s", basename(filepath));
447    
448            if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT)
449          {          {
450                  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);
451                  return -2;                  return -2;
452          }          }
453    
454          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)  
455          {          {
456                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  log_error("shm_open(%s) error (%d)\n", user_list_shm_name, errno);
457                  return -3;                  return -2;
458          }          }
459          p_shm = shmat(shmid, NULL, 0);          if (ftruncate(fd, (off_t)size) == -1)
         if (p_shm == (void *)-1)  
460          {          {
461                  log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);
462                  return -3;                  close(fd);
463                    return -2;
464            }
465    
466            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
467            if (p_shm == MAP_FAILED)
468            {
469                    log_error("mmap() error (%d)\n", errno);
470                    close(fd);
471                    return -2;
472            }
473    
474            if (close(fd) < 0)
475            {
476                    log_error("close(fd) error (%d)\n", errno);
477                    return -1;
478          }          }
479    
480          p_user_list_pool = p_shm;          p_user_list_pool = p_shm;
481          p_user_list_pool->shmid = shmid;          p_user_list_pool->shm_size = size;
482    
483          // Allocate semaphore as user list pool lock          // Allocate semaphore as user list pool lock
484    #ifndef HAVE_SYSTEM_V
485            if (sem_init(&(p_user_list_pool->sem), 1, 1) == -1)
486            {
487                    log_error("sem_init() error (%d)\n", errno);
488                    return -3;
489            }
490    
491            p_user_list_pool->read_lock_count = 0;
492            p_user_list_pool->write_lock_count = 0;
493    #else
494            proj_id = (int)(time(NULL) % getpid());
495            key = ftok(filename, proj_id);
496            if (key == -1)
497            {
498                    log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
499                    return -2;
500            }
501    
502          size = 2; // r_sem and w_sem          size = 2; // r_sem and w_sem
503          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
504          if (semid == -1)          if (semid == -1)
# Line 466  int user_list_pool_init(const char *file Line 519  int user_list_pool_init(const char *file
519          }          }
520    
521          p_user_list_pool->semid = semid;          p_user_list_pool->semid = semid;
522    #endif
523    
524          // Set user counts to 0          // Set user counts to 0
525          p_user_list_pool->user_list[0].user_count = 0;          p_user_list_pool->user_list[0].user_count = 0;
526          p_user_list_pool->user_list[1].user_count = 0;          p_user_list_pool->user_list[1].user_count = 0;
527    
528          p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);          p_user_list_pool->user_list_index_current = 0;
529          p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);          p_user_list_pool->user_list_index_new = 1;
530    
531          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;
532          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;
533    
534          user_stat_map_init(&(p_user_list_pool->user_stat_map));          user_stat_map_init(&(p_user_list_pool->user_stat_map));
535    
# Line 484  int user_list_pool_init(const char *file Line 538  int user_list_pool_init(const char *file
538    
539  void user_list_pool_cleanup(void)  void user_list_pool_cleanup(void)
540  {  {
         int shmid;  
   
541          if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
542          {          {
543                  return;                  return;
544          }          }
545    
546          shmid = p_user_list_pool->shmid;  #ifdef HAVE_SYSTEM_V
   
547          if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)          if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)
548          {          {
549                  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);
550          }          }
551    #else
552          if (shmdt(p_user_list_pool) == -1)          if (sem_destroy(&(p_user_list_pool->sem)) == -1)
553          {          {
554                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("sem_destroy() error (%d)\n", errno);
555          }          }
556    #endif
557    
558            detach_user_list_pool_shm();
559    
560          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT)
561          {          {
562                  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);
563          }          }
564    
565          p_user_list_pool = NULL;          user_list_shm_name[0] = '\0';
566    
567          if (p_trie_action_dict != NULL)          if (p_trie_action_dict != NULL)
568          {          {
# Line 520  void user_list_pool_cleanup(void) Line 574  void user_list_pool_cleanup(void)
574    
575  int set_user_list_pool_shm_readonly(void)  int set_user_list_pool_shm_readonly(void)
576  {  {
577          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)  
578          {          {
579                  log_error("p_user_list_pool not initialized\n");                  log_error("mprotect() error (%d)\n", errno);
580                  return -1;                  return -1;
581          }          }
582    
         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;  
   
583          return 0;          return 0;
584  }  }
585    
586  int detach_user_list_pool_shm(void)  int detach_user_list_pool_shm(void)
587  {  {
588          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)
589          {          {
590                  log_error("shmdt(user_list_pool) error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
591                  return -1;                  return -1;
592          }          }
593    
# Line 560  int detach_user_list_pool_shm(void) Line 599  int detach_user_list_pool_shm(void)
599  int user_list_pool_reload(int online_user)  int user_list_pool_reload(int online_user)
600  {  {
601          MYSQL *db = NULL;          MYSQL *db = NULL;
602          USER_LIST *p_tmp;          int tmp;
         USER_ONLINE_LIST *p_online_tmp;  
603          int ret = 0;          int ret = 0;
604    
605          if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
# Line 579  int user_list_pool_reload(int online_use Line 617  int user_list_pool_reload(int online_use
617    
618          if (online_user)          if (online_user)
619          {          {
620                  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)
621                  {                  {
622                          log_error("user_online_list_load() error\n");                          log_error("user_online_list_load() error\n");
623                          ret = -2;                          ret = -2;
# Line 595  int user_list_pool_reload(int online_use Line 633  int user_list_pool_reload(int online_use
633          }          }
634          else          else
635          {          {
636                  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)
637                  {                  {
638                          log_error("user_list_load() error\n");                          log_error("user_list_load() error\n");
639                          ret = -2;                          ret = -2;
# Line 606  int user_list_pool_reload(int online_use Line 644  int user_list_pool_reload(int online_use
644          mysql_close(db);          mysql_close(db);
645          db = NULL;          db = NULL;
646    
647          if (user_list_rw_lock(p_user_list_pool->semid) < 0)          if (user_list_rw_lock() < 0)
648          {          {
649                  log_error("user_list_rw_lock() error\n");                  log_error("user_list_rw_lock() error\n");
650                  ret = -3;                  ret = -3;
# Line 616  int user_list_pool_reload(int online_use Line 654  int user_list_pool_reload(int online_use
654          if (online_user)          if (online_user)
655          {          {
656                  // Swap p_online_current and p_online_new                  // Swap p_online_current and p_online_new
657                  p_online_tmp = p_user_list_pool->p_online_current;                  tmp = p_user_list_pool->user_online_list_index_current;
658                  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;
659                  p_user_list_pool->p_online_new = p_online_tmp;                  p_user_list_pool->user_online_list_index_new = tmp;
660          }          }
661          else          else
662          {          {
663                  // Swap p_current and p_new                  // Swap index_current and index_new
664                  p_tmp = p_user_list_pool->p_current;                  tmp = p_user_list_pool->user_list_index_current;
665                  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;
666                  p_user_list_pool->p_new = p_tmp;                  p_user_list_pool->user_list_index_new = tmp;
667          }          }
668    
669          if (user_list_rw_unlock(p_user_list_pool->semid) < 0)          if (user_list_rw_unlock() < 0)
670          {          {
671                  log_error("user_list_rw_unlock() error\n");                  log_error("user_list_rw_unlock() error\n");
672                  ret = -3;                  ret = -3;
# Line 641  cleanup: Line 679  cleanup:
679          return ret;          return ret;
680  }  }
681    
682  int user_list_try_rd_lock(int semid, int wait_sec)  int user_list_try_rd_lock(int wait_sec)
683  {  {
684    #ifdef HAVE_SYSTEM_V
685          struct sembuf sops[2];          struct sembuf sops[2];
686    #endif
687          struct timespec timeout;          struct timespec timeout;
688          int ret;          int ret = 0;
689    
690            if (p_user_list_pool == NULL)
691            {
692                    log_error("p_user_list_pool not initialized\n");
693                    return -1;
694            }
695    
696            timeout.tv_sec = wait_sec;
697            timeout.tv_nsec = 0;
698    
699    #ifdef HAVE_SYSTEM_V
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 655  int user_list_try_rd_lock(int semid, int Line 705  int user_list_try_rd_lock(int semid, int
705          sops[1].sem_op = 1;                     // lock          sops[1].sem_op = 1;                     // lock
706          sops[1].sem_flg = SEM_UNDO; // undo on terminate          sops[1].sem_flg = SEM_UNDO; // undo on terminate
707    
708          timeout.tv_sec = wait_sec;          ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);
         timeout.tv_nsec = 0;  
   
         ret = semtimedop(semid, sops, 2, &timeout);  
709          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
710          {          {
711                  log_error("semtimedop(lock read) error %d\n", errno);                  log_error("semop(lock read) error %d\n", errno);
712            }
713    #else
714            if (sem_timedwait(&(p_user_list_pool->sem), &timeout) == -1)
715            {
716                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
717                    {
718                            log_error("sem_timedwait() error %d\n", errno);
719                    }
720                    return -1;
721          }          }
722    
723            if (p_user_list_pool->write_lock_count == 0)
724            {
725                    p_user_list_pool->read_lock_count++;
726            }
727            else
728            {
729                    errno = EAGAIN;
730                    ret = -1;
731            }
732    
733            if (sem_post(&(p_user_list_pool->sem)) == -1)
734            {
735                    log_error("sem_post() error %d\n", errno);
736                    return -1;
737            }
738    #endif
739    
740          return ret;          return ret;
741  }  }
742    
743  int user_list_try_rw_lock(int semid, int wait_sec)  int user_list_try_rw_lock(int wait_sec)
744  {  {
745    #ifdef HAVE_SYSTEM_V
746          struct sembuf sops[3];          struct sembuf sops[3];
747    #endif
748          struct timespec timeout;          struct timespec timeout;
749          int ret;          int ret = 0;
750    
751            if (p_user_list_pool == NULL)
752            {
753                    log_error("p_user_list_pool not initialized\n");
754                    return -1;
755            }
756    
757            timeout.tv_sec = wait_sec;
758            timeout.tv_nsec = 0;
759    
760    #ifdef HAVE_SYSTEM_V
761          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
762          sops[0].sem_op = 0;      // wait until unlocked          sops[0].sem_op = 0;      // wait until unlocked
763          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 685  int user_list_try_rw_lock(int semid, int Line 770  int user_list_try_rw_lock(int semid, int
770          sops[2].sem_op = 0;      // wait until unlocked          sops[2].sem_op = 0;      // wait until unlocked
771          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
772    
773          timeout.tv_sec = wait_sec;          ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);
         timeout.tv_nsec = 0;  
   
         ret = semtimedop(semid, sops, 3, &timeout);  
774          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
775          {          {
776                  log_error("semtimedop(lock write) error %d\n", errno);                  log_error("semop(lock write) error %d\n", errno);
777            }
778    #else
779            if (sem_timedwait(&(p_user_list_pool->sem), &timeout) == -1)
780            {
781                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
782                    {
783                            log_error("sem_timedwait() error %d\n", errno);
784                    }
785                    return -1;
786          }          }
787    
788            if (p_user_list_pool->read_lock_count == 0 && p_user_list_pool->write_lock_count == 0)
789            {
790                    p_user_list_pool->write_lock_count++;
791            }
792            else
793            {
794                    errno = EAGAIN;
795                    ret = -1;
796            }
797    
798            if (sem_post(&(p_user_list_pool->sem)) == -1)
799            {
800                    log_error("sem_post() error %d\n", errno);
801                    return -1;
802            }
803    #endif
804    
805          return ret;          return ret;
806  }  }
807    
808  int user_list_rd_unlock(int semid)  int user_list_rd_unlock(void)
809  {  {
810    #ifdef HAVE_SYSTEM_V
811          struct sembuf sops[2];          struct sembuf sops[2];
812          int ret;  #endif
813            int ret = 0;
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    #ifdef HAVE_SYSTEM_V
822          sops[0].sem_num = 0;                                     // r_sem          sops[0].sem_num = 0;                                     // r_sem
823          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                     // unlock
824          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
825    
826          ret = semop(semid, sops, 1);          ret = semop(p_user_list_pool->semid, sops, 1);
827          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
828          {          {
829                  log_error("semop(unlock read) error %d\n", errno);                  log_error("semop(unlock read) error %d\n", errno);
830          }          }
831    #else
832            if (sem_wait(&(p_user_list_pool->sem)) == -1)
833            {
834                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
835                    {
836                            log_error("sem_wait() error %d\n", errno);
837                    }
838                    return -1;
839            }
840    
841            if (p_user_list_pool->read_lock_count > 0)
842            {
843                    p_user_list_pool->read_lock_count--;
844            }
845            else
846            {
847                    log_error("read_lock_count already 0\n");
848            }
849    
850            if (sem_post(&(p_user_list_pool->sem)) == -1)
851            {
852                    log_error("sem_post() error %d\n", errno);
853                    return -1;
854            }
855    #endif
856    
857          return ret;          return ret;
858  }  }
859    
860  int user_list_rw_unlock(int semid)  int user_list_rw_unlock(void)
861  {  {
862    #ifdef HAVE_SYSTEM_V
863          struct sembuf sops[1];          struct sembuf sops[1];
864          int ret;  #endif
865            int ret = 0;
866    
867            if (p_user_list_pool == NULL)
868            {
869                    log_error("p_user_list_pool not initialized\n");
870                    return -1;
871            }
872    
873    #ifdef HAVE_SYSTEM_V
874          sops[0].sem_num = 1;                                     // w_sem          sops[0].sem_num = 1;                                     // w_sem
875          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                     // unlock
876          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
877    
878          ret = semop(semid, sops, 1);          ret = semop(p_user_list_pool->semid, sops, 1);
879          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
880          {          {
881                  log_error("semop(unlock write) error %d\n", errno);                  log_error("semop(unlock write) error %d\n", errno);
882          }          }
883    #else
884            if (sem_wait(&(p_user_list_pool->sem)) == -1)
885            {
886                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
887                    {
888                            log_error("sem_wait() error %d\n", errno);
889                    }
890                    return -1;
891            }
892    
893            if (p_user_list_pool->write_lock_count > 0)
894            {
895                    p_user_list_pool->write_lock_count--;
896            }
897            else
898            {
899                    log_error("write_lock_count already 0\n");
900            }
901    
902            if (sem_post(&(p_user_list_pool->sem)) == -1)
903            {
904                    log_error("sem_post() error %d\n", errno);
905                    return -1;
906            }
907    #endif
908    
909          return ret;          return ret;
910  }  }
911    
912  int user_list_rd_lock(int semid)  int user_list_rd_lock(void)
913  {  {
914          int timer = 0;          int timer = 0;
915          int ret = -1;          int ret = -1;
916            time_t tm_first_failure = 0;
917    
918            if (p_user_list_pool == NULL)
919            {
920                    log_error("p_user_list_pool not initialized\n");
921                    return -1;
922            }
923    
924          while (!SYS_server_exit)          while (!SYS_server_exit)
925          {          {
926                  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);
927                  if (ret == 0) // success                  if (ret == 0) // success
928                  {                  {
929                          break;                          break;
# Line 751  int user_list_rd_lock(int semid) Line 934  int user_list_rd_lock(int semid)
934                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
935                          {                          {
936                                  log_error("user_list_try_rd_lock() tried %d times\n", timer);                                  log_error("user_list_try_rd_lock() tried %d times\n", timer);
937    
938                                    if (time(NULL) - tm_first_failure >= USER_LIST_DEAD_LOCK_TIMEOUT)
939                                    {
940                                            log_error("Unable to acquire rw_lock for %d seconds\n", time(NULL) - tm_first_failure);
941    #ifndef HAVE_SYSTEM_V
942                                            user_list_reset_lock();
943                                            log_error("Reset POSIX semaphore to resolve dead lock\n");
944    #endif
945                                            break;
946                                    }
947                          }                          }
948                            usleep(100 * 1000); // 0.1 second
949                  }                  }
950                  else // failed                  else // failed
951                  {                  {
# Line 763  int user_list_rd_lock(int semid) Line 957  int user_list_rd_lock(int semid)
957          return ret;          return ret;
958  }  }
959    
960  int user_list_rw_lock(int semid)  int user_list_rw_lock(void)
961  {  {
962          int timer = 0;          int timer = 0;
963          int ret = -1;          int ret = -1;
964            time_t tm_first_failure = 0;
965    
966            if (p_user_list_pool == NULL)
967            {
968                    log_error("p_user_list_pool not initialized\n");
969                    return -1;
970            }
971    
972          while (!SYS_server_exit)          while (!SYS_server_exit)
973          {          {
974                  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);
975                  if (ret == 0) // success                  if (ret == 0) // success
976                  {                  {
977                          break;                          break;
# Line 781  int user_list_rw_lock(int semid) Line 982  int user_list_rw_lock(int semid)
982                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
983                          {                          {
984                                  log_error("user_list_try_rw_lock() tried %d times\n", timer);                                  log_error("user_list_try_rw_lock() tried %d times\n", timer);
985    
986                                    if (time(NULL) - tm_first_failure >= USER_LIST_DEAD_LOCK_TIMEOUT)
987                                    {
988                                            log_error("Unable to acquire rw_lock for %d seconds\n", time(NULL) - tm_first_failure);
989    #ifndef HAVE_SYSTEM_V
990                                            user_list_reset_lock();
991                                            log_error("Reset POSIX semaphore to resolve dead lock\n");
992    #endif
993                                            break;
994                                    }
995                          }                          }
996                            usleep(100 * 1000); // 0.1 second
997                  }                  }
998                  else // failed                  else // failed
999                  {                  {
# Line 793  int user_list_rw_lock(int semid) Line 1005  int user_list_rw_lock(int semid)
1005          return ret;          return ret;
1006  }  }
1007    
1008    #ifndef HAVE_SYSTEM_V
1009    int user_list_reset_lock(void)
1010    {
1011            if (p_user_list_pool == NULL)
1012            {
1013                    log_error("p_user_list_pool not initialized\n");
1014                    return -1;
1015            }
1016    
1017            if (sem_destroy(&(p_user_list_pool->sem)) == -1)
1018            {
1019                    log_error("sem_destroy() error (%d)\n", errno);
1020            }
1021    
1022            p_user_list_pool->read_lock_count = 0;
1023            p_user_list_pool->write_lock_count = 0;
1024    
1025            if (sem_init(&(p_user_list_pool->sem), 1, 1) == -1)
1026            {
1027                    log_error("sem_init() error (%d)\n", errno);
1028            }
1029    
1030            return 0;
1031    }
1032    #endif
1033    
1034  int query_user_list(int page_id, USER_INFO *p_users, int *p_user_count, int *p_page_count)  int query_user_list(int page_id, USER_INFO *p_users, int *p_user_count, int *p_page_count)
1035  {  {
1036          int ret = 0;          int ret = 0;
# Line 807  int query_user_list(int page_id, USER_IN Line 1045  int query_user_list(int page_id, USER_IN
1045          *p_page_count = 0;          *p_page_count = 0;
1046    
1047          // acquire lock of user list          // acquire lock of user list
1048          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1049          {          {
1050                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1051                  return -2;                  return -2;
1052          }          }
1053    
1054          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)
1055          {          {
1056                  // empty list                  // empty list
1057                  ret = 0;                  ret = 0;
1058                  goto cleanup;                  goto cleanup;
1059          }          }
1060    
1061          *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) /
1062                                          (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);                                          BBS_user_limit_per_page;
1063    
1064          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
1065          {          {
# Line 831  int query_user_list(int page_id, USER_IN Line 1069  int query_user_list(int page_id, USER_IN
1069          }          }
1070    
1071          *p_user_count = MIN(BBS_user_limit_per_page,          *p_user_count = MIN(BBS_user_limit_per_page,
1072                                                  p_user_list_pool->p_current->user_count -                                                  p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count -
1073                                                          page_id * BBS_user_limit_per_page);                                                          page_id * BBS_user_limit_per_page);
1074    
1075          memcpy(p_users,          memcpy(p_users,
1076                     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,
1077                     sizeof(USER_INFO) * (size_t)(*p_user_count));                     sizeof(USER_INFO) * (size_t)(*p_user_count));
1078    
1079  cleanup:  cleanup:
1080          // release lock of user list          // release lock of user list
1081          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1082          {          {
1083                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1084                  ret = -1;                  ret = -1;
# Line 863  int query_user_online_list(int page_id, Line 1101  int query_user_online_list(int page_id,
1101          *p_page_count = 0;          *p_page_count = 0;
1102    
1103          // acquire lock of user list          // acquire lock of user list
1104          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1105          {          {
1106                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1107                  return -2;                  return -2;
1108          }          }
1109    
1110          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)
1111          {          {
1112                  // empty list                  // empty list
1113                  ret = 0;                  ret = 0;
1114                  goto cleanup;                  goto cleanup;
1115          }          }
1116    
1117          *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);  
1118    
1119          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
1120          {          {
# Line 887  int query_user_online_list(int page_id, Line 1124  int query_user_online_list(int page_id,
1124          }          }
1125    
1126          *p_user_count = MIN(BBS_user_limit_per_page,          *p_user_count = MIN(BBS_user_limit_per_page,
1127                                                  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 -
1128                                                          page_id * BBS_user_limit_per_page);                                                          page_id * BBS_user_limit_per_page);
1129    
1130          memcpy(p_online_users,          memcpy(p_online_users,
1131                     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,
1132                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));
1133    
1134  cleanup:  cleanup:
1135          // release lock of user list          // release lock of user list
1136          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1137          {          {
1138                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1139                  ret = -1;                  ret = -1;
# Line 914  int get_user_list_count(int *p_user_cnt) Line 1151  int get_user_list_count(int *p_user_cnt)
1151          }          }
1152    
1153          // acquire lock of user list          // acquire lock of user list
1154          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1155          {          {
1156                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1157                  return -2;                  return -2;
1158          }          }
1159    
1160          *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;
1161    
1162          // release lock of user list          // release lock of user list
1163          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1164          {          {
1165                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1166                  return -2;                  return -2;
# Line 941  int get_user_online_list_count(int *p_us Line 1178  int get_user_online_list_count(int *p_us
1178          }          }
1179    
1180          // acquire lock of user list          // acquire lock of user list
1181          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1182          {          {
1183                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1184                  return -2;                  return -2;
1185          }          }
1186    
1187          *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;
1188          *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;
1189    
1190          // release lock of user list          // release lock of user list
1191          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1192          {          {
1193                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1194                  return -2;                  return -2;
# Line 984  int query_user_info(int32_t id, USER_INF Line 1221  int query_user_info(int32_t id, USER_INF
1221          }          }
1222    
1223          // acquire lock of user list          // acquire lock of user list
1224          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1225          {          {
1226                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1227                  return -2;                  return -2;
1228          }          }
1229    
1230          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
1231          {          {
1232                  *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];
1233                  ret = 1;                  ret = 1;
1234          }          }
1235    
1236          // release lock of user list          // release lock of user list
1237          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1238          {          {
1239                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1240                  ret = -1;                  ret = -1;
# Line 1021  int query_user_info_by_uid(int32_t uid, Line 1258  int query_user_info_by_uid(int32_t uid,
1258          }          }
1259    
1260          // acquire lock of user list          // acquire lock of user list
1261          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1262          {          {
1263                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1264                  return -2;                  return -2;
1265          }          }
1266    
1267          left = 0;          left = 0;
1268          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;
1269    
1270          while (left < right)          while (left < right)
1271          {          {
1272                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1273                  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)
1274                  {                  {
1275                          right = mid - 1;                          right = mid - 1;
1276                  }                  }
1277                  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)
1278                  {                  {
1279                          left = mid + 1;                          left = mid + 1;
1280                  }                  }
1281                  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)
1282                  {                  {
1283                          left = mid;                          left = mid;
1284                          break;                          break;
1285                  }                  }
1286          }          }
1287    
1288          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
1289          {          {
1290                  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;
1291                  *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];
1292                  ret = 1;                  ret = 1;
1293    
1294                  if (p_intro_buf != NULL)                  if (p_intro_buf != NULL)
1295                  {                  {
1296                          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);
1297                          p_intro_buf[intro_buf_len - 1] = '\0';                          p_intro_buf[intro_buf_len - 1] = '\0';
1298                          p_user->intro = p_intro_buf;                          p_user->intro = p_intro_buf;
1299                  }                  }
1300          }          }
1301    
1302          // release lock of user list          // release lock of user list
1303          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1304          {          {
1305                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1306                  ret = -1;                  ret = -1;
# Line 1093  int query_user_info_by_username(const ch Line 1330  int query_user_info_by_username(const ch
1330          prefix_len = strlen(username_prefix);          prefix_len = strlen(username_prefix);
1331    
1332          // acquire lock of user list          // acquire lock of user list
1333          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1334          {          {
1335                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1336                  return -2;                  return -2;
1337          }          }
1338    
1339          left = 0;          left = 0;
1340          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;
1341    
1342          while (left < right)          while (left < right)
1343          {          {
1344                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1345                  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);
1346                  if (comp < 0)                  if (comp < 0)
1347                  {                  {
1348                          right = mid - 1;                          right = mid - 1;
# Line 1121  int query_user_info_by_username(const ch Line 1358  int query_user_info_by_username(const ch
1358                  }                  }
1359          }          }
1360    
1361          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
1362          {          {
1363  #ifdef _DEBUG  #ifdef _DEBUG
1364                  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 1371  int query_user_info_by_username(const ch
1371                  while (left < right)                  while (left < right)
1372                  {                  {
1373                          mid = (left + right) / 2;                          mid = (left + right) / 2;
1374                          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);
1375                          if (comp > 0)                          if (comp > 0)
1376                          {                          {
1377                                  left = mid + 1;                                  left = mid + 1;
# Line 1157  int query_user_info_by_username(const ch Line 1394  int query_user_info_by_username(const ch
1394    
1395                  left = left_save;                  left = left_save;
1396                  left_save = right;                  left_save = right;
1397                  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;
1398    
1399                  while (left < right)                  while (left < right)
1400                  {                  {
1401                          mid = (left + right) / 2 + (left + right) % 2;                          mid = (left + right) / 2 + (left + right) % 2;
1402                          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);
1403                          if (comp < 0)                          if (comp < 0)
1404                          {                          {
1405                                  right = mid - 1;                                  right = mid - 1;
# Line 1188  int query_user_info_by_username(const ch Line 1425  int query_user_info_by_username(const ch
1425    
1426                  for (i = 0; i < max_user_cnt && left + i <= right; i++)                  for (i = 0; i < max_user_cnt && left + i <= right; i++)
1427                  {                  {
1428                          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;
1429                          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);
1430                          username_list[i][sizeof(username_list[i]) - 1] = '\0';                          username_list[i][sizeof(username_list[i]) - 1] = '\0';
1431                  }                  }
1432                  ret = i;                  ret = i;
# Line 1197  int query_user_info_by_username(const ch Line 1434  int query_user_info_by_username(const ch
1434    
1435  cleanup:  cleanup:
1436          // release lock of user list          // release lock of user list
1437          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1438          {          {
1439                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1440                  ret = -1;                  ret = -1;
# Line 1217  int query_user_online_info(int32_t id, U Line 1454  int query_user_online_info(int32_t id, U
1454          }          }
1455    
1456          // acquire lock of user list          // acquire lock of user list
1457          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1458          {          {
1459                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1460                  return -2;                  return -2;
1461          }          }
1462    
1463          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
1464          {          {
1465                  *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];
1466                  ret = 1;                  ret = 1;
1467          }          }
1468    
1469          // release lock of user list          // release lock of user list
1470          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1471          {          {
1472                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1473                  ret = -1;                  ret = -1;
# Line 1259  int query_user_online_info_by_uid(int32_ Line 1496  int query_user_online_info_by_uid(int32_
1496          *p_user_cnt = 0;          *p_user_cnt = 0;
1497    
1498          // acquire lock of user list          // acquire lock of user list
1499          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1500          {          {
1501                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1502                  return -2;                  return -2;
1503          }          }
1504    
1505          left = start_id;          left = start_id;
1506          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;
1507    
1508          while (left < right)          while (left < right)
1509          {          {
1510                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1511                  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)
1512                  {                  {
1513                          right = mid - 1;                          right = mid - 1;
1514                  }                  }
1515                  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)
1516                  {                  {
1517                          left = mid + 1;                          left = mid + 1;
1518                  }                  }
1519                  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)
1520                  {                  {
1521                          left = mid;                          left = mid;
1522                          break;                          break;
1523                  }                  }
1524          }          }
1525    
1526          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)
1527          {          {
1528                  right = left;                  right = left;
1529                  left = start_id;                  left = start_id;
# Line 1294  int query_user_online_info_by_uid(int32_ Line 1531  int query_user_online_info_by_uid(int32_
1531                  while (left < right)                  while (left < right)
1532                  {                  {
1533                          mid = (left + right) / 2;                          mid = (left + right) / 2;
1534                          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)
1535                          {                          {
1536                                  right = mid;                                  right = mid;
1537                          }                          }
1538                          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)
1539                          {                          {
1540                                  left = mid + 1;                                  left = mid + 1;
1541                          }                          }
1542                  }                  }
1543    
1544                  for (i = 0;                  for (i = 0;
1545                           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 &&
1546                           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;
1547                           left++, i++)                           left++, i++)
1548                  {                  {
1549                          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;
1550                          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];
1551                  }                  }
1552    
1553                  if (i > 0)                  if (i > 0)
# Line 1321  int query_user_online_info_by_uid(int32_ Line 1558  int query_user_online_info_by_uid(int32_
1558          }          }
1559    
1560          // release lock of user list          // release lock of user list
1561          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1562          {          {
1563                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1564                  ret = -1;                  ret = -1;
# Line 1345  int get_user_id_list(int32_t *p_uid_list Line 1582  int get_user_id_list(int32_t *p_uid_list
1582          }          }
1583    
1584          // acquire lock of user list          // acquire lock of user list
1585          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1586          {          {
1587                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1588                  return -2;                  return -2;
1589          }          }
1590    
1591          left = 0;          left = 0;
1592          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;
1593    
1594          while (left < right)          while (left < right)
1595          {          {
1596                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1597                  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)
1598                  {                  {
1599                          right = mid - 1;                          right = mid - 1;
1600                  }                  }
1601                  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)
1602                  {                  {
1603                          left = mid + 1;                          left = mid + 1;
1604                  }                  }
1605                  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)
1606                  {                  {
1607                          left = mid;                          left = mid;
1608                          break;                          break;
1609                  }                  }
1610          }          }
1611    
1612          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++)
1613          {          {
1614                  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;
1615          }          }
1616          *p_user_cnt = i;          *p_user_cnt = i;
1617    
1618          // release lock of user list          // release lock of user list
1619          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1620          {          {
1621                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1622                  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