/[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.36 by sysadm, Mon Nov 17 14:01:13 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 22  Line 26 
26  #include <sys/sem.h>  #include <sys/sem.h>
27  #include <sys/shm.h>  #include <sys/shm.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  {  {
# Line 181  int user_list_load(MYSQL *db, USER_LIST Line 188  int user_list_load(MYSQL *db, USER_LIST
188                  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]));
189                  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]));
190                  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]));
191                  p_list->users[i].birthday = (row[10] == NULL ? 0 : atol(row[11]));                  p_list->users[i].birthday = (row[11] == NULL ? 0 : atol(row[11]));
192                  intro_len = strlen((row[12] == NULL ? "" : row[12]));                  intro_len = strlen((row[12] == NULL ? "" : row[12]));
193                  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)
194                  {                  {
# Line 335  int user_online_list_load(MYSQL *db, USE Line 342  int user_online_list_load(MYSQL *db, USE
342                  }                  }
343    
344                  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  
345          }          }
346    
347          p_online_list->user_count = user_cnt;          p_online_list->user_count = user_cnt;
348          p_online_list->guest_count = guest_cnt;          p_online_list->guest_count = guest_cnt;
349    
 #ifdef _DEBUG  
         log_error("Loaded %d online users and %d guest users\n", p_list->user_count, p_list->guest_count);  
 #endif  
   
350  cleanup:  cleanup:
351          mysql_free_result(rs);          mysql_free_result(rs);
352    
# Line 503  void user_list_pool_cleanup(void) Line 502  void user_list_pool_cleanup(void)
502                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
503          }          }
504    
505          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
506          {          {
507                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
508          }          }
# Line 532  int set_user_list_pool_shm_readonly(void Line 531  int set_user_list_pool_shm_readonly(void
531          shmid = p_user_list_pool->shmid;          shmid = p_user_list_pool->shmid;
532    
533          // Remap shared memory in read-only mode          // Remap shared memory in read-only mode
534    #if defined(__CYGWIN__)
535            if (shmdt(p_user_list_pool) == -1)
536            {
537                    log_error("shmdt(user_list_pool) error (%d)\n", errno);
538                    return -1;
539            }
540            p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY);
541    #else
542          p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);          p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);
543    #endif
544          if (p_shm == (void *)-1)          if (p_shm == (void *)-1)
545          {          {
546                  log_error("shmat(user_list_pool shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmat(user_list_pool shmid = %d) error (%d)\n", shmid, errno);
# Line 644  cleanup: Line 652  cleanup:
652  int user_list_try_rd_lock(int semid, int wait_sec)  int user_list_try_rd_lock(int semid, int wait_sec)
653  {  {
654          struct sembuf sops[2];          struct sembuf sops[2];
655    #if !defined(__CYGWIN__)
656          struct timespec timeout;          struct timespec timeout;
657    #endif
658          int ret;          int ret;
659    
660          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
# Line 655  int user_list_try_rd_lock(int semid, int Line 665  int user_list_try_rd_lock(int semid, int
665          sops[1].sem_op = 1;                     // lock          sops[1].sem_op = 1;                     // lock
666          sops[1].sem_flg = SEM_UNDO; // undo on terminate          sops[1].sem_flg = SEM_UNDO; // undo on terminate
667    
668    #if defined(__CYGWIN__)
669            ret = semop(semid, sops, 2);
670    #else
671          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
672          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
673    
674          ret = semtimedop(semid, sops, 2, &timeout);          ret = semtimedop(semid, sops, 2, &timeout);
675    #endif
676          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
677          {          {
678                  log_error("semtimedop(lock read) error %d\n", errno);                  log_error("semop(lock read) error %d\n", errno);
679          }          }
680    
681          return ret;          return ret;
# Line 670  int user_list_try_rd_lock(int semid, int Line 684  int user_list_try_rd_lock(int semid, int
684  int user_list_try_rw_lock(int semid, int wait_sec)  int user_list_try_rw_lock(int semid, int wait_sec)
685  {  {
686          struct sembuf sops[3];          struct sembuf sops[3];
687    #if !defined(__CYGWIN__)
688          struct timespec timeout;          struct timespec timeout;
689    #endif
690          int ret;          int ret;
691    
692          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
# Line 685  int user_list_try_rw_lock(int semid, int Line 701  int user_list_try_rw_lock(int semid, int
701          sops[2].sem_op = 0;      // wait until unlocked          sops[2].sem_op = 0;      // wait until unlocked
702          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
703    
704    #if defined(__CYGWIN__)
705            ret = semop(semid, sops, 3);
706    #else
707          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
708          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
709    
710          ret = semtimedop(semid, sops, 3, &timeout);          ret = semtimedop(semid, sops, 3, &timeout);
711    #endif
712          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
713          {          {
714                  log_error("semtimedop(lock write) error %d\n", errno);                  log_error("semop(lock write) error %d\n", errno);
715          }          }
716    
717          return ret;          return ret;
# Line 820  int query_user_list(int page_id, USER_IN Line 840  int query_user_list(int page_id, USER_IN
840                  goto cleanup;                  goto cleanup;
841          }          }
842    
843          *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +          *p_page_count = (p_user_list_pool->p_current->user_count + BBS_user_limit_per_page - 1) /
844                                          (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);                                          BBS_user_limit_per_page;
845    
846          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
847          {          {
# Line 876  int query_user_online_list(int page_id, Line 896  int query_user_online_list(int page_id,
896                  goto cleanup;                  goto cleanup;
897          }          }
898    
899          *p_page_count = p_user_list_pool->p_online_current->user_count / BBS_user_limit_per_page +          *p_page_count = (p_user_list_pool->p_online_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);  
900    
901          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
902          {          {


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

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