/[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.4 by sysadm, Tue Oct 21 12:35:03 2025 UTC Revision 1.10 by sysadm, Wed Oct 22 10:38:23 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                           user_list.c  -  description                                                   user_list.c  -  description
3                               -------------------                                                           -------------------
4      Copyright            : (C) 2004-2025 by Leaflet          Copyright            : (C) 2004-2025 by Leaflet
5      Email                : leaflet@leafok.com          Email                : leaflet@leafok.com
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
# Line 17  Line 17 
17  #include "common.h"  #include "common.h"
18  #include "database.h"  #include "database.h"
19  #include "log.h"  #include "log.h"
20    #include "trie_dict.h"
21  #include "user_list.h"  #include "user_list.h"
22  #include <errno.h>  #include <errno.h>
23  #include <stdlib.h>  #include <stdlib.h>
# Line 31  Line 32 
32  #ifdef _SEM_SEMUN_UNDEFINED  #ifdef _SEM_SEMUN_UNDEFINED
33  union semun  union semun
34  {  {
35      int val;               /* Value for SETVAL */          int val;                           /* Value for SETVAL */
36      struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
37      unsigned short *array; /* Array for GETALL, SETALL */          unsigned short *array; /* Array for GETALL, SETALL */
38      struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
39                                (Linux-specific) */                                                            (Linux-specific) */
40  };  };
41  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
42    
# Line 44  union semun Line 45  union semun
45    
46  struct user_list_pool_t  struct user_list_pool_t
47  {  {
48      int shmid;          int shmid;
49      int semid;          int semid;
50      USER_LIST user_list[2];          USER_LIST user_list[2];
51      USER_LIST *p_current;          USER_LIST *p_current;
52      USER_LIST *p_new;          USER_LIST *p_new;
53            USER_ONLINE_LIST user_online_list[2];
54            USER_ONLINE_LIST *p_online_current;
55            USER_ONLINE_LIST *p_online_new;
56  };  };
57  typedef struct user_list_pool_t USER_LIST_POOL;  typedef struct user_list_pool_t USER_LIST_POOL;
58    
59  static USER_LIST_POOL *p_user_list_pool = NULL;  static USER_LIST_POOL *p_user_list_pool = NULL;
60    static TRIE_NODE *p_trie_action_dict = NULL;
61    
62    typedef struct user_action_map_t
63    {
64            char name[BBS_current_action_max_len + 1];
65            char title[BBS_current_action_max_len + 1];
66    } USER_ACTION_MAP;
67    
68    const USER_ACTION_MAP user_action_map[] =
69            {
70                    {"ARTICLE_FAVOR", "浏览收藏"},
71                    {"BBS_NET", "站点穿梭"},
72                    {"CHICKEN", "电子小鸡"},
73                    {"EDIT_ARTICLE", "修改文章"},
74                    {"LOGIN", "进入大厅"},
75                    {"MENU", "菜单选择"},
76                    {"POST_ARTICLE", "撰写文章"},
77                    {"REPLY_ARTICLE", "回复文章"},
78                    {"USER_LIST", "查花名册"},
79                    {"USER_ONLINE", "环顾四周"},
80                    {"VIEW_ARTICLE", "阅读文章"},
81                    {"VIEW_FILE", "查看文档"},
82                    {"WWW", "Web浏览"}};
83    
84    const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP);
85    
86    static int user_list_try_rd_lock(int semid, int wait_sec);
87    static int user_list_try_rw_lock(int semid, int wait_sec);
88    static int user_list_rd_unlock(int semid);
89    static int user_list_rw_unlock(int semid);
90    static int user_list_rd_lock(int semid);
91    static int user_list_rw_lock(int semid);
92    
93    static int user_list_load(MYSQL *db, USER_LIST *p_list);
94    static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list);
95    
96    static int user_info_index_uid_comp(const void *ptr1, const void *ptr2)
97    {
98            const USER_INFO_INDEX_UID *p1 = ptr1;
99            const USER_INFO_INDEX_UID *p2 = ptr2;
100    
101            if (p1->uid < p2->uid)
102            {
103                    return -1;
104            }
105            else if (p1->uid > p2->uid)
106            {
107                    return 1;
108            }
109            else if (p1->id < p2->id)
110            {
111                    return -1;
112            }
113            else if (p1->id > p2->id)
114            {
115                    return 1;
116            }
117            return 0;
118    }
119    
120  int user_list_load(MYSQL *db, USER_LIST *p_list)  int user_list_load(MYSQL *db, USER_LIST *p_list)
121  {  {
122      MYSQL_RES *rs = NULL;          MYSQL_RES *rs = NULL;
123      MYSQL_ROW row;          MYSQL_ROW row;
124      char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
125      int ret = 0;          int ret = 0;
126      int i;          int i;
127            int32_t last_uid = -1;
128      if (db == NULL || p_list == NULL)  
129      {          if (db == NULL || p_list == NULL)
130          log_error("NULL pointer error\n");          {
131          return -1;                  log_error("NULL pointer error\n");
132      }                  return -1;
133            }
134      snprintf(sql, sizeof(sql),  
135               "SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, "          if (p_list->user_count > 0)
136               "UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday) "          {
137               "FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "                  last_uid = p_list->users[p_list->user_count - 1].uid;
138               "INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID "          }
139               "WHERE enable ORDER BY UID");  
140            snprintf(sql, sizeof(sql),
141      if (mysql_query(db, sql) != 0)                           "SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, "
142      {                           "UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday) "
143          log_error("Query user info error: %s\n", mysql_error(db));                           "FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
144          ret = -1;                           "INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID "
145          goto cleanup;                           "WHERE enable ORDER BY username");
146      }  
147            if (mysql_query(db, sql) != 0)
148      if ((rs = mysql_use_result(db)) == NULL)          {
149      {                  log_error("Query user info error: %s\n", mysql_error(db));
150          log_error("Get user info data failed\n");                  ret = -1;
151          ret = -1;                  goto cleanup;
152          goto cleanup;          }
153      }  
154            if ((rs = mysql_use_result(db)) == NULL)
155      i = 0;          {
156      while ((row = mysql_fetch_row(rs)))                  log_error("Get user info data failed\n");
157      {                  ret = -1;
158          p_list->users[i].uid = atoi(row[0]);                  goto cleanup;
159          strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1);          }
160          p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0';  
161          strncpy(p_list->users[i].nickname, row[2], sizeof(p_list->users[i].nickname) - 1);          i = 0;
162          p_list->users[i].nickname[sizeof(p_list->users[i].nickname) - 1] = '\0';          while ((row = mysql_fetch_row(rs)))
163          p_list->users[i].gender = row[3][0];          {
164          p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4]));                  // record
165          p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5]));                  p_list->users[i].id = i;
166          p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6]));                  p_list->users[i].uid = atoi(row[0]);
167          p_list->users[i].signup_dt = (row[7] == NULL ? 0 : atol(row[7]));                  strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1);
168          p_list->users[i].last_login_dt = (row[8] == NULL ? 0 : atol(row[8]));                  p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0';
169          p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9]));                  strncpy(p_list->users[i].nickname, row[2], sizeof(p_list->users[i].nickname) - 1);
170                    p_list->users[i].nickname[sizeof(p_list->users[i].nickname) - 1] = '\0';
171          i++;                  p_list->users[i].gender = row[3][0];
172          if (i >= BBS_max_user_count)                  p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4]));
173          {                  p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5]));
174              log_error("Too many users, exceed limit %d\n", BBS_max_user_count);                  p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6]));
175              break;                  p_list->users[i].signup_dt = (row[7] == NULL ? 0 : atol(row[7]));
176          }                  p_list->users[i].last_login_dt = (row[8] == NULL ? 0 : atol(row[8]));
177      }                  p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9]));
178      mysql_free_result(rs);  
179      rs = NULL;                  // index
180                    p_list->index_uid[i].uid = p_list->users[i].uid;
181                    p_list->index_uid[i].id = i;
182    
183                    i++;
184                    if (i >= BBS_max_user_count)
185                    {
186                            log_error("Too many users, exceed limit %d\n", BBS_max_user_count);
187                            break;
188                    }
189            }
190            mysql_free_result(rs);
191            rs = NULL;
192    
193            // Sort index
194            if (i != p_list->user_count || p_list->users[i - 1].uid != last_uid) // Count of users changed
195            {
196                    qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
197    #ifdef _DEBUG
198                    log_error("Rebuild index of %d users, last_uid=%d\n", i, p_list->users[i - 1].uid);
199    #endif
200            }
201    
202      p_list->user_count = i;          p_list->user_count = i;
203    
204  #ifdef _DEBUG  #ifdef _DEBUG
205      log_error("Loaded %d users\n", p_list->user_count);          log_error("Loaded %d users\n", p_list->user_count);
206  #endif  #endif
207    
208  cleanup:  cleanup:
209      mysql_free_result(rs);          mysql_free_result(rs);
   
     return ret;  
 }  
   
 int user_list_pool_init(void)  
 {  
     int shmid;  
     int semid;  
     int proj_id;  
     key_t key;  
     size_t size;  
     void *p_shm;  
     union semun arg;  
     int i;  
   
     if (p_user_list_pool != NULL)  
     {  
         log_error("p_user_list_pool already initialized\n");  
         return -1;  
     }  
   
     // Allocate shared memory  
     proj_id = (int)(time(NULL) % getpid());  
     key = ftok(VAR_USER_LIST_SHM, proj_id);  
     if (key == -1)  
     {  
         log_error("ftok(%s %d) error (%d)\n", VAR_USER_LIST_SHM, proj_id, errno);  
         return -2;  
     }  
   
     size = sizeof(USER_LIST_POOL);  
     shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
     if (shmid == -1)  
     {  
         log_error("shmget(size = %d) error (%d)\n", size, errno);  
         return -3;  
     }  
     p_shm = shmat(shmid, NULL, 0);  
     if (p_shm == (void *)-1)  
     {  
         log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);  
         return -3;  
     }  
   
     p_user_list_pool = p_shm;  
     p_user_list_pool->shmid = shmid;  
   
     // Allocate semaphore as user list pool lock  
     size = 2; // r_sem and w_sem  
     semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);  
     if (semid == -1)  
     {  
         log_error("semget(user_list_pool_sem, size = %d) error (%d)\n", size, errno);  
         return -3;  
     }  
   
     // Initialize sem value to 0  
     arg.val = 0;  
     for (i = 0; i < size; i++)  
     {  
         if (semctl(semid, i, SETVAL, arg) == -1)  
         {  
             log_error("semctl(user_list_pool_sem, SETVAL) error (%d)\n", errno);  
             return -3;  
         }  
     }  
   
     p_user_list_pool->semid = semid;  
   
     // Set user counts to 0  
     p_user_list_pool->user_list[0].user_count = 0;  
     p_user_list_pool->user_list[1].user_count = 0;  
   
     p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);  
     p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);  
   
     return 0;  
 }  
   
 void user_list_pool_cleanup(void)  
 {  
     int shmid;  
210    
211      if (p_user_list_pool == NULL)          return ret;
     {  
         return;  
     }  
   
     shmid = p_user_list_pool->shmid;  
   
     if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)  
     {  
         log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_user_list_pool->semid, errno);  
     }  
   
     if (shmdt(p_user_list_pool) == -1)  
     {  
         log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);  
     }  
   
     if (shmctl(shmid, IPC_RMID, NULL) == -1)  
     {  
         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);  
     }  
   
     p_user_list_pool = NULL;  
 }  
   
 int set_user_list_pool_shm_readonly(void)  
 {  
     int shmid;  
     void *p_shm;  
   
     if (p_user_list_pool == NULL)  
     {  
         log_error("p_user_list_pool not initialized\n");  
         return -1;  
     }  
   
     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;  
   
     return 0;  
212  }  }
213    
214  int detach_user_list_pool_shm(void)  int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list)
 {  
     if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)  
     {  
         log_error("shmdt(user_list_pool) error (%d)\n", errno);  
         return -1;  
     }  
   
     p_user_list_pool = NULL;  
   
     return 0;  
 }  
   
 int user_list_pool_reload(void)  
215  {  {
216      MYSQL *db = NULL;          MYSQL_RES *rs = NULL;
217      USER_LIST *p_tmp;          MYSQL_ROW row;
218            char sql[SQL_BUFFER_LEN];
219      if (p_user_list_pool == NULL)          int ret = 0;
220      {          int i;
221          log_error("p_user_list_pool not initialized\n");  
222          return -1;          if (db == NULL || p_list == NULL)
223      }          {
224                    log_error("NULL pointer error\n");
225      db = db_open();                  return -1;
226      if (db == NULL)          }
227      {  
228          log_error("db_open() error: %s\n", mysql_error(db));          snprintf(sql, sizeof(sql),
229          return -1;                           "SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), "
230      }                           "UNIX_TIMESTAMP(last_tm) FROM user_online "
231                             "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) AND UID <> 0 "
232      if (user_list_load(db, p_user_list_pool->p_new) < 0)                           "ORDER BY last_tm DESC",
233      {                           BBS_user_off_line);
234          log_error("user_list_rw_lock() error\n");  
235          return -2;          if (mysql_query(db, sql) != 0)
236      }          {
237                    log_error("Query user online error: %s\n", mysql_error(db));
238      mysql_close(db);                  ret = -1;
239                    goto cleanup;
240            }
241    
242            if ((rs = mysql_use_result(db)) == NULL)
243            {
244                    log_error("Get user online data failed\n");
245                    ret = -1;
246                    goto cleanup;
247            }
248    
249            i = 0;
250            while ((row = mysql_fetch_row(rs)))
251            {
252                    p_list->users[i].id = i;
253                    strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1);
254                    p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0';
255    
256                    if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_list->users[i].user_info))) < 0)
257                    {
258                            log_error("query_user_info(%d) error\n", atoi(row[1]));
259                            continue;
260                    }
261                    else if (ret == 0) // skip Guest
262                    {
263                            continue;
264                    }
265    
266                    strncpy(p_list->users[i].ip, row[2], sizeof(p_list->users[i].ip) - 1);
267                    p_list->users[i].ip[sizeof(p_list->users[i].ip) - 1] = '\0';
268    
269                    strncpy(p_list->users[i].current_action, row[3], sizeof(p_list->users[i].current_action) - 1);
270                    p_list->users[i].current_action[sizeof(p_list->users[i].current_action) - 1] = '\0';
271                    p_list->users[i].current_action_title = NULL;
272                    if (p_list->users[i].current_action[0] == '\0')
273                    {
274                            p_list->users[i].current_action_title = "";
275                    }
276                    else if (trie_dict_get(p_trie_action_dict, p_list->users[i].current_action, (int64_t *)(&(p_list->users[i].current_action_title))) < 0)
277                    {
278                            log_error("trie_dict_get(p_trie_action_dict, %s) error on session_id=%s\n",
279                                              p_list->users[i].current_action, p_list->users[i].session_id);
280                            continue;
281                    }
282    
283                    p_list->users[i].login_tm = (row[4] == NULL ? 0 : atol(row[4]));
284                    p_list->users[i].last_tm = (row[5] == NULL ? 0 : atol(row[5]));
285    
286                    // index
287                    p_list->index_uid[i].uid = p_list->users[i].user_info.uid;
288                    p_list->index_uid[i].id = i;
289    
290                    i++;
291                    if (i >= BBS_max_user_online_count)
292                    {
293                            log_error("Too many online users, exceed limit %d\n", BBS_max_user_online_count);
294                            break;
295                    }
296            }
297            mysql_free_result(rs);
298            rs = NULL;
299    
300            // Sort index
301            if (i > 0)
302            {
303                    qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
304    #ifdef _DEBUG
305                    log_error("Rebuild index of %d online users\n", i);
306    #endif
307            }
308    
309      if (user_list_rw_lock() < 0)          p_list->user_count = i;
     {  
         log_error("user_list_rw_lock() error\n");  
         return -3;  
     }  
310    
311      // Swap p_current and p_new  #ifdef _DEBUG
312      p_tmp = p_user_list_pool->p_current;          log_error("Loaded %d online users\n", p_list->user_count);
313      p_user_list_pool->p_current = p_user_list_pool->p_new;  #endif
     p_user_list_pool->p_new = p_tmp;  
314    
315      if (user_list_rw_unlock() < 0)  cleanup:
316      {          mysql_free_result(rs);
         log_error("user_list_rw_unlock() error\n");  
         return -3;  
     }  
317    
318      return 0;          return ret;
319  }  }
320    
321  int user_list_try_rd_lock(int wait_sec)  int user_list_pool_init(void)
322  {  {
323      struct sembuf sops[2];          int shmid;
324      struct timespec timeout;          int semid;
325      int ret;          int proj_id;
326            key_t key;
327            size_t size;
328            void *p_shm;
329            union semun arg;
330            int i;
331    
332            if (p_user_list_pool != NULL || p_trie_action_dict != NULL)
333            {
334                    log_error("p_user_list_pool already initialized\n");
335                    return -1;
336            }
337    
338            p_trie_action_dict = trie_dict_create();
339            if (p_trie_action_dict == NULL)
340            {
341                    log_error("trie_dict_create() error\n");
342                    return -1;
343            }
344    
345            for (i = 0; i < user_action_map_size; i++)
346            {
347                    if (trie_dict_set(p_trie_action_dict, user_action_map[i].name, (int64_t)(user_action_map[i].title)) < 0)
348                    {
349                            log_error("trie_dict_set(p_trie_action_dict, %s) error\n", user_action_map[i].name);
350                    }
351            }
352    
353            // Allocate shared memory
354            proj_id = (int)(time(NULL) % getpid());
355            key = ftok(VAR_USER_LIST_SHM, proj_id);
356            if (key == -1)
357            {
358                    log_error("ftok(%s %d) error (%d)\n", VAR_USER_LIST_SHM, proj_id, errno);
359                    return -2;
360            }
361    
362            size = sizeof(USER_LIST_POOL);
363            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
364            if (shmid == -1)
365            {
366                    log_error("shmget(size = %d) error (%d)\n", size, errno);
367                    return -3;
368            }
369            p_shm = shmat(shmid, NULL, 0);
370            if (p_shm == (void *)-1)
371            {
372                    log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
373                    return -3;
374            }
375    
376            p_user_list_pool = p_shm;
377            p_user_list_pool->shmid = shmid;
378    
379            // Allocate semaphore as user list pool lock
380            size = 2; // r_sem and w_sem
381            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
382            if (semid == -1)
383            {
384                    log_error("semget(user_list_pool_sem, size = %d) error (%d)\n", size, errno);
385                    return -3;
386            }
387    
388            // Initialize sem value to 0
389            arg.val = 0;
390            for (i = 0; i < size; i++)
391            {
392                    if (semctl(semid, i, SETVAL, arg) == -1)
393                    {
394                            log_error("semctl(user_list_pool_sem, SETVAL) error (%d)\n", errno);
395                            return -3;
396                    }
397            }
398    
399            p_user_list_pool->semid = semid;
400    
401            // Set user counts to 0
402            p_user_list_pool->user_list[0].user_count = 0;
403            p_user_list_pool->user_list[1].user_count = 0;
404    
405      sops[0].sem_num = 1; // w_sem          p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);
406      sops[0].sem_op = 0;  // wait until unlocked          p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);
     sops[0].sem_flg = 0;  
407    
408      sops[1].sem_num = 0;        // r_sem          p_user_list_pool->p_online_current = &(p_user_list_pool->user_online_list[0]);
409      sops[1].sem_op = 1;         // lock          p_user_list_pool->p_online_new = &(p_user_list_pool->user_online_list[1]);
     sops[1].sem_flg = SEM_UNDO; // undo on terminate  
410    
411      timeout.tv_sec = wait_sec;          return 0;
     timeout.tv_nsec = 0;  
   
     ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);  
     if (ret == -1 && errno != EAGAIN && errno != EINTR)  
     {  
         log_error("semtimedop(lock read) error %d\n", errno);  
     }  
   
     return ret;  
412  }  }
413    
414  int user_list_try_rw_lock(int wait_sec)  void user_list_pool_cleanup(void)
 {  
     struct sembuf sops[3];  
     struct timespec timeout;  
     int ret;  
   
     sops[0].sem_num = 1; // w_sem  
     sops[0].sem_op = 0;  // wait until unlocked  
     sops[0].sem_flg = 0;  
   
     sops[1].sem_num = 1;        // w_sem  
     sops[1].sem_op = 1;         // lock  
     sops[1].sem_flg = SEM_UNDO; // undo on terminate  
   
     sops[2].sem_num = 0; // r_sem  
     sops[2].sem_op = 0;  // wait until unlocked  
     sops[2].sem_flg = 0;  
   
     timeout.tv_sec = wait_sec;  
     timeout.tv_nsec = 0;  
   
     ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);  
     if (ret == -1 && errno != EAGAIN && errno != EINTR)  
     {  
         log_error("semtimedop(lock write) error %d\n", errno);  
     }  
   
     return ret;  
 }  
   
 int user_list_rd_unlock(void)  
415  {  {
416      struct sembuf sops[2];          int shmid;
     int ret;  
417    
418      sops[0].sem_num = 0;                     // r_sem          if (p_user_list_pool == NULL)
419      sops[0].sem_op = -1;                     // unlock          {
420      sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  return;
421            }
422    
423            shmid = p_user_list_pool->shmid;
424    
425            if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)
426            {
427                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_user_list_pool->semid, errno);
428            }
429    
430            if (shmdt(p_user_list_pool) == -1)
431            {
432                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
433            }
434    
435            if (shmctl(shmid, IPC_RMID, NULL) == -1)
436            {
437                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
438            }
439    
440            p_user_list_pool = NULL;
441    
442            if (p_trie_action_dict != NULL)
443            {
444                    trie_dict_destroy(p_trie_action_dict);
445    
446      ret = semop(p_user_list_pool->semid, sops, 1);                  p_trie_action_dict = NULL;
447      if (ret == -1 && errno != EAGAIN && errno != EINTR)          }
     {  
         log_error("semop(unlock read) error %d\n", errno);  
     }  
   
     return ret;  
448  }  }
449    
450  int user_list_rw_unlock(void)  int set_user_list_pool_shm_readonly(void)
451  {  {
452      struct sembuf sops[1];          int shmid;
453      int ret;          void *p_shm;
454    
455      sops[0].sem_num = 1;                     // w_sem          if (p_user_list_pool == NULL)
456      sops[0].sem_op = -1;                     // unlock          {
457      sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  log_error("p_user_list_pool not initialized\n");
458                    return -1;
459            }
460    
461            shmid = p_user_list_pool->shmid;
462    
463            // Remap shared memory in read-only mode
464            p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);
465            if (p_shm == (void *)-1)
466            {
467                    log_error("shmat(user_list_pool shmid = %d) error (%d)\n", shmid, errno);
468                    return -3;
469            }
470    
471      ret = semop(p_user_list_pool->semid, sops, 1);          p_user_list_pool = p_shm;
     if (ret == -1 && errno != EAGAIN && errno != EINTR)  
     {  
         log_error("semop(unlock write) error %d\n", errno);  
     }  
472    
473      return ret;          return 0;
474  }  }
475    
476  int user_list_rd_lock(void)  int detach_user_list_pool_shm(void)
 {  
     int timer = 0;  
     int ret = -1;  
   
     while (!SYS_server_exit)  
     {  
         ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME);  
         if (ret == 0) // success  
         {  
             break;  
         }  
         else if (errno == EAGAIN || errno == EINTR) // retry  
         {  
             timer++;  
             if (timer % USER_LIST_TRY_LOCK_TIMES == 0)  
             {  
                 log_error("user_list_try_rd_lock() tried %d times\n", timer);  
             }  
         }  
         else // failed  
         {  
             log_error("user_list_try_rd_lock() failed\n");  
             break;  
         }  
     }  
   
     return ret;  
 }  
   
 int user_list_rw_lock(void)  
477  {  {
478      int timer = 0;          if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)
479      int ret = -1;          {
480                    log_error("shmdt(user_list_pool) error (%d)\n", errno);
481                    return -1;
482            }
483    
484            p_user_list_pool = NULL;
485    
486            return 0;
487    }
488    
489    int user_list_pool_reload(int online_user)
490    {
491            MYSQL *db = NULL;
492            USER_LIST *p_tmp;
493            USER_ONLINE_LIST *p_online_tmp;
494    
495            if (p_user_list_pool == NULL)
496            {
497                    log_error("p_user_list_pool not initialized\n");
498                    return -1;
499            }
500    
501            db = db_open();
502            if (db == NULL)
503            {
504                    log_error("db_open() error: %s\n", mysql_error(db));
505                    return -1;
506            }
507    
508            if (online_user)
509            {
510                    if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0)
511                    {
512                            log_error("user_online_list_load() error\n");
513                            return -2;
514                    }
515    
516                    // No online user, no need to swap lists
517                    if (p_user_list_pool->p_online_current->user_count == 0 && p_user_list_pool->p_online_new->user_count == 0)
518                    {
519                            return 0;
520                    }
521            }
522            else
523            {
524                    if (user_list_load(db, p_user_list_pool->p_new) < 0)
525                    {
526                            log_error("user_list_load() error\n");
527                            return -2;
528                    }
529            }
530    
531            mysql_close(db);
532    
533            if (user_list_rw_lock(p_user_list_pool->semid) < 0)
534            {
535                    log_error("user_list_rw_lock() error\n");
536                    return -3;
537            }
538    
539            if (online_user)
540            {
541                    // Swap p_online_current and p_online_new
542                    p_online_tmp = p_user_list_pool->p_online_current;
543                    p_user_list_pool->p_online_current = p_user_list_pool->p_online_new;
544                    p_user_list_pool->p_online_new = p_online_tmp;
545            }
546            else
547            {
548                    // Swap p_current and p_new
549                    p_tmp = p_user_list_pool->p_current;
550                    p_user_list_pool->p_current = p_user_list_pool->p_new;
551                    p_user_list_pool->p_new = p_tmp;
552            }
553    
554            if (user_list_rw_unlock(p_user_list_pool->semid) < 0)
555            {
556                    log_error("user_list_rw_unlock() error\n");
557                    return -3;
558            }
559    
560            return 0;
561    }
562    
563    int user_list_try_rd_lock(int semid, int wait_sec)
564    {
565            struct sembuf sops[2];
566            struct timespec timeout;
567            int ret;
568    
569            sops[0].sem_num = 1; // w_sem
570            sops[0].sem_op = 0;      // wait until unlocked
571            sops[0].sem_flg = 0;
572    
573            sops[1].sem_num = 0;            // r_sem
574            sops[1].sem_op = 1;                     // lock
575            sops[1].sem_flg = SEM_UNDO; // undo on terminate
576    
577            timeout.tv_sec = wait_sec;
578            timeout.tv_nsec = 0;
579    
580            ret = semtimedop(semid, sops, 2, &timeout);
581            if (ret == -1 && errno != EAGAIN && errno != EINTR)
582            {
583                    log_error("semtimedop(lock read) error %d\n", errno);
584            }
585    
586            return ret;
587    }
588    
589    int user_list_try_rw_lock(int semid, int wait_sec)
590    {
591            struct sembuf sops[3];
592            struct timespec timeout;
593            int ret;
594    
595            sops[0].sem_num = 1; // w_sem
596            sops[0].sem_op = 0;      // wait until unlocked
597            sops[0].sem_flg = 0;
598    
599            sops[1].sem_num = 1;            // w_sem
600            sops[1].sem_op = 1;                     // lock
601            sops[1].sem_flg = SEM_UNDO; // undo on terminate
602    
603            sops[2].sem_num = 0; // r_sem
604            sops[2].sem_op = 0;      // wait until unlocked
605            sops[2].sem_flg = 0;
606    
607            timeout.tv_sec = wait_sec;
608            timeout.tv_nsec = 0;
609    
610            ret = semtimedop(semid, sops, 3, &timeout);
611            if (ret == -1 && errno != EAGAIN && errno != EINTR)
612            {
613                    log_error("semtimedop(lock write) error %d\n", errno);
614            }
615    
616            return ret;
617    }
618    
619    int user_list_rd_unlock(int semid)
620    {
621            struct sembuf sops[2];
622            int ret;
623    
624            sops[0].sem_num = 0;                                     // r_sem
625            sops[0].sem_op = -1;                                     // unlock
626            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
627    
628            ret = semop(semid, sops, 1);
629            if (ret == -1 && errno != EAGAIN && errno != EINTR)
630            {
631                    log_error("semop(unlock read) error %d\n", errno);
632            }
633    
634            return ret;
635    }
636    
637    int user_list_rw_unlock(int semid)
638    {
639            struct sembuf sops[1];
640            int ret;
641    
642            sops[0].sem_num = 1;                                     // w_sem
643            sops[0].sem_op = -1;                                     // unlock
644            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
645    
646            ret = semop(semid, sops, 1);
647            if (ret == -1 && errno != EAGAIN && errno != EINTR)
648            {
649                    log_error("semop(unlock write) error %d\n", errno);
650            }
651    
652            return ret;
653    }
654    
655    int user_list_rd_lock(int semid)
656    {
657            int timer = 0;
658            int ret = -1;
659    
660            while (!SYS_server_exit)
661            {
662                    ret = user_list_try_rd_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);
663                    if (ret == 0) // success
664                    {
665                            break;
666                    }
667                    else if (errno == EAGAIN || errno == EINTR) // retry
668                    {
669                            timer++;
670                            if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
671                            {
672                                    log_error("user_list_try_rd_lock() tried %d times\n", timer);
673                            }
674                    }
675                    else // failed
676                    {
677                            log_error("user_list_try_rd_lock() failed\n");
678                            break;
679                    }
680            }
681    
682            return ret;
683    }
684    
685    int user_list_rw_lock(int semid)
686    {
687            int timer = 0;
688            int ret = -1;
689    
690            while (!SYS_server_exit)
691            {
692                    ret = user_list_try_rw_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);
693                    if (ret == 0) // success
694                    {
695                            break;
696                    }
697                    else if (errno == EAGAIN || errno == EINTR) // retry
698                    {
699                            timer++;
700                            if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
701                            {
702                                    log_error("user_list_try_rw_lock() tried %d times\n", timer);
703                            }
704                    }
705                    else // failed
706                    {
707                            log_error("user_list_try_rw_lock() failed\n");
708                            break;
709                    }
710            }
711    
712      while (!SYS_server_exit)          return ret;
     {  
         ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME);  
         if (ret == 0) // success  
         {  
             break;  
         }  
         else if (errno == EAGAIN || errno == EINTR) // retry  
         {  
             timer++;  
             if (timer % USER_LIST_TRY_LOCK_TIMES == 0)  
             {  
                 log_error("user_list_try_rw_lock() tried %d times\n", timer);  
             }  
         }  
         else // failed  
         {  
             log_error("user_list_try_rw_lock() failed\n");  
             break;  
         }  
     }  
   
     return ret;  
713  }  }
714    
715  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)
716  {  {
717      int ret = 0;          int ret = 0;
718    
719            if (p_users == NULL || p_user_count == NULL || p_page_count == NULL)
720            {
721                    log_error("NULL pointer error\n");
722                    return -1;
723            }
724    
725            // acquire lock of user list
726            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
727            {
728                    log_error("user_list_rd_lock() error\n");
729                    return -2;
730            }
731    
732            if (p_user_list_pool->p_current->user_count == 0)
733            {
734                    // empty list
735                    ret = 0;
736                    goto cleanup;
737            }
738    
739            *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +
740                                            (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);
741    
742            if (page_id < 0 || page_id >= *p_page_count)
743            {
744                    log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
745                    ret = -3;
746                    goto cleanup;
747            }
748    
749            *p_user_count = MIN(BBS_user_limit_per_page,
750                                                    p_user_list_pool->p_current->user_count -
751                                                            page_id * BBS_user_limit_per_page);
752    
753            memcpy(p_users,
754                       p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,
755                       sizeof(USER_INFO) * (size_t)(*p_user_count));
756    
757      if (p_users == NULL || p_user_count == NULL || p_page_count == NULL)  cleanup:
758      {          // release lock of user list
759          log_error("NULL pointer error\n");          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
760          return -1;          {
761      }                  log_error("user_list_rd_unlock() error\n");
762                    ret = -1;
763      // acquire lock of user list          }
764      if (user_list_rd_lock() < 0)  
765      {          return ret;
766          log_error("user_list_rd_lock() error\n");  }
767          return -2;  
768      }  int query_user_online_list(int page_id, USER_ONLINE_INFO *p_online_users, int *p_user_count, int *p_page_count)
769    {
770      if (p_user_list_pool->p_current->user_count == 0)          int ret = 0;
771      {  
772          // empty list          if (p_online_users == NULL || p_user_count == NULL || p_page_count == NULL)
773          ret = 0;          {
774          goto cleanup;                  log_error("NULL pointer error\n");
775      }                  return -1;
776            }
777      *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +  
778                      (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);          // acquire lock of user list
779            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
780      if (page_id < 0 || page_id >= *p_page_count)          {
781      {                  log_error("user_list_rd_lock() error\n");
782          log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);                  return -2;
783          ret = -3;          }
784          goto cleanup;  
785      }          if (p_user_list_pool->p_online_current->user_count == 0)
786            {
787      *p_user_count = MIN(BBS_user_limit_per_page,                  // empty list
788                          p_user_list_pool->p_current->user_count -                  ret = 0;
789                              page_id * BBS_user_limit_per_page);                  goto cleanup;
790            }
791      memcpy(p_users,  
792             p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,          *p_page_count = p_user_list_pool->p_online_current->user_count / BBS_user_limit_per_page +
793             sizeof(USER_INFO) * (size_t)(*p_user_count));                                          (p_user_list_pool->p_online_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);
794    
795            if (page_id < 0 || page_id >= *p_page_count)
796            {
797                    log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
798                    ret = -3;
799                    goto cleanup;
800            }
801    
802            *p_user_count = MIN(BBS_user_limit_per_page,
803                                                    p_user_list_pool->p_online_current->user_count -
804                                                            page_id * BBS_user_limit_per_page);
805    
806            memcpy(p_online_users,
807                       p_user_list_pool->p_online_current->users + page_id * BBS_user_limit_per_page,
808                       sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));
809    
810  cleanup:  cleanup:
811      // release lock of user list          // release lock of user list
812      if (user_list_rd_unlock() < 0)          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
813      {          {
814          log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
815          ret = -1;                  ret = -1;
816      }          }
817    
818      return ret;          return ret;
819  }  }
820    
821  int query_user_info(int32_t uid, USER_INFO *p_user)  int query_user_info(int32_t id, USER_INFO *p_user)
822  {  {
823      int left;          int ret = 0;
824      int right;  
825      int mid;          if (p_user == NULL)
826      int ret = 0;          {
827                    log_error("NULL pointer error\n");
828      if (p_user == NULL)                  return -1;
829      {          }
830          log_error("NULL pointer error\n");  
831          return -1;          // acquire lock of user list
832      }          if (user_list_rd_lock(p_user_list_pool->semid) < 0)
833            {
834      // acquire lock of user list                  log_error("user_list_rd_lock() error\n");
835      if (user_list_rd_lock() < 0)                  return -2;
836      {          }
837          log_error("user_list_rd_lock() error\n");  
838          return -2;          if (id >= 0 && id < p_user_list_pool->p_current->user_count) // Found
839      }          {
840                    *p_user = p_user_list_pool->p_current->users[id];
841      left = 0;                  ret = 1;
842      right = p_user_list_pool->p_current->user_count - 1;          }
843    
844      while (left < right)          // release lock of user list
845      {          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
846          mid = (left + right) / 2;          {
847          if (uid < p_user_list_pool->p_current->users[mid].uid)                  log_error("user_list_rd_unlock() error\n");
848          {                  ret = -1;
849              right = mid;          }
850          }  
851          else if (uid > p_user_list_pool->p_current->users[mid].uid)          return ret;
852          {  }
853              left = mid + 1;  
854          }  int query_user_info_by_uid(int32_t uid, USER_INFO *p_user)
855          else // if (uid == p_user_list_pool->p_current->users[mid].uid)  {
856          {          int left;
857              left = mid;          int right;
858              break;          int mid;
859          }          int32_t id;
860      }          int ret = 0;
861    
862      if (uid == p_user_list_pool->p_current->users[left].uid) // Found          if (p_user == NULL)
863      {          {
864          *p_user = p_user_list_pool->p_current->users[left];                  log_error("NULL pointer error\n");
865          ret = 1;                  return -1;
866      }          }
867    
868      // release lock of user list          // acquire lock of user list
869      if (user_list_rd_unlock() < 0)          if (user_list_rd_lock(p_user_list_pool->semid) < 0)
870      {          {
871          log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_lock() error\n");
872          ret = -1;                  return -2;
873      }          }
874    
875            left = 0;
876            right = p_user_list_pool->p_current->user_count - 1;
877    
878            while (left < right)
879            {
880                    mid = (left + right) / 2;
881                    if (uid < p_user_list_pool->p_current->index_uid[mid].uid)
882                    {
883                            right = mid;
884                    }
885                    else if (uid > p_user_list_pool->p_current->index_uid[mid].uid)
886                    {
887                            left = mid + 1;
888                    }
889                    else // if (uid == p_user_list_pool->p_current->index_uid[mid].uid)
890                    {
891                            left = mid;
892                            break;
893                    }
894            }
895    
896            if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found
897            {
898                    id = p_user_list_pool->p_current->index_uid[left].id;
899                    if ((ret = query_user_info(id, p_user)) <= 0)
900                    {
901                            log_error("query_user_info(id=%d) error: %d\n", id, ret);
902                    }
903                    else
904                    {
905                            ret = 1;
906                    }
907            }
908    
909            // release lock of user list
910            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
911            {
912                    log_error("user_list_rd_unlock() error\n");
913                    ret = -1;
914            }
915    
916            return ret;
917    }
918    
919    int query_user_online_info(int32_t id, USER_ONLINE_INFO *p_user)
920    {
921            int ret = 0;
922    
923            if (p_user == NULL)
924            {
925                    log_error("NULL pointer error\n");
926                    return -1;
927            }
928    
929            // acquire lock of user list
930            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
931            {
932                    log_error("user_list_rd_lock() error\n");
933                    return -2;
934            }
935    
936            if (id >= 0 && id < p_user_list_pool->p_online_current->user_count) // Found
937            {
938                    *p_user = p_user_list_pool->p_online_current->users[id];
939                    ret = 1;
940            }
941    
942            // release lock of user list
943            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
944            {
945                    log_error("user_list_rd_unlock() error\n");
946                    ret = -1;
947            }
948    
949      return ret;          return ret;
950  }  }


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

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