/[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.41 by sysadm, Thu Nov 20 03:22:35 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                           user_list.c  -  description  /*
3                               -------------------   * user_list
4      Copyright            : (C) 2004-2025 by Leaflet   *   - data model and basic operations of (online) user list
5      Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "common.h"  #include "common.h"
14  #include "database.h"  #include "database.h"
15  #include "log.h"  #include "log.h"
16    #include "trie_dict.h"
17  #include "user_list.h"  #include "user_list.h"
18    #include "user_stat.h"
19  #include <errno.h>  #include <errno.h>
20    #include <fcntl.h>
21  #include <stdlib.h>  #include <stdlib.h>
22  #include <string.h>  #include <string.h>
23  #include <time.h>  #include <time.h>
 #include <sys/ipc.h>  
24  #include <sys/mman.h>  #include <sys/mman.h>
25  #include <sys/param.h>  #include <sys/param.h>
26  #include <sys/sem.h>  #include <sys/sem.h>
27  #include <sys/shm.h>  #include <sys/stat.h>
28    
29  #ifdef _SEM_SEMUN_UNDEFINED  #if defined(_SEM_SEMUN_UNDEFINED) || defined(__CYGWIN__)
30  union semun  union semun
31  {  {
32      int val;               /* Value for SETVAL */          int val;                           /* Value for SETVAL */
33      struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
34      unsigned short *array; /* Array for GETALL, SETALL */          unsigned short *array; /* Array for GETALL, SETALL */
35      struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
36                                (Linux-specific) */                                                            (Linux-specific) */
37  };  };
38  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #if defined(_SEM_SEMUN_UNDEFINED)
39    
40  #define USER_LIST_TRY_LOCK_WAIT_TIME 1 // second  enum _user_list_constant_t
41  #define USER_LIST_TRY_LOCK_TIMES 10  {
42            USER_LIST_TRY_LOCK_WAIT_TIME = 1, // second
43            USER_LIST_TRY_LOCK_TIMES = 10,
44    };
45    
46  struct user_list_pool_t  struct user_list_pool_t
47  {  {
48      int shmid;          size_t shm_size;
49      int semid;          int semid;
50      USER_LIST user_list[2];          USER_LIST user_list[2];
51      USER_LIST *p_current;          int user_list_index_current;
52      USER_LIST *p_new;          int user_list_index_new;
53            USER_ONLINE_LIST user_online_list[2];
54            int user_online_list_index_current;
55            int user_online_list_index_new;
56            USER_STAT_MAP user_stat_map;
57            int user_login_count;
58  };  };
59  typedef struct user_list_pool_t USER_LIST_POOL;  typedef struct user_list_pool_t USER_LIST_POOL;
60    
61    static char user_list_shm_name[FILE_NAME_LEN];
62  static USER_LIST_POOL *p_user_list_pool = NULL;  static USER_LIST_POOL *p_user_list_pool = NULL;
63    static TRIE_NODE *p_trie_action_dict = NULL;
64    
65    typedef struct user_action_map_t
66    {
67            char name[BBS_current_action_max_len + 1];
68            char title[BBS_current_action_max_len + 1];
69    } USER_ACTION_MAP;
70    
71    const USER_ACTION_MAP user_action_map[] =
72            {
73                    {"ARTICLE_FAVOR", "浏览收藏"},
74                    {"BBS_NET", "站点穿梭"},
75                    {"CHICKEN", "电子小鸡"},
76                    {"EDIT_ARTICLE", "修改文章"},
77                    {"LOGIN", "进入大厅"},
78                    {"MENU", "菜单选择"},
79                    {"POST_ARTICLE", "撰写文章"},
80                    {"REPLY_ARTICLE", "回复文章"},
81                    {"USER_LIST", "查花名册"},
82                    {"USER_ONLINE", "环顾四周"},
83                    {"VIEW_ARTICLE", "阅读文章"},
84                    {"VIEW_FILE", "查看文档"},
85                    {"WWW", "Web浏览"}};
86    
87    const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP);
88    
89    static int user_list_try_rd_lock(int wait_sec);
90    static int user_list_try_rw_lock(int wait_sec);
91    static int user_list_rd_unlock(void);
92    static int user_list_rw_unlock(void);
93    static int user_list_rd_lock(void);
94    static int user_list_rw_lock(void);
95    
96    static int user_list_load(MYSQL *db, USER_LIST *p_list);
97    static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list);
98    static int user_login_count_load(MYSQL *db);
99    
100    static int user_info_index_uid_comp(const void *ptr1, const void *ptr2)
101    {
102            const USER_INFO_INDEX_UID *p1 = ptr1;
103            const USER_INFO_INDEX_UID *p2 = ptr2;
104    
105            if (p1->uid < p2->uid)
106            {
107                    return -1;
108            }
109            else if (p1->uid > p2->uid)
110            {
111                    return 1;
112            }
113            else if (p1->id < p2->id)
114            {
115                    return -1;
116            }
117            else if (p1->id > p2->id)
118            {
119                    return 1;
120            }
121            return 0;
122    }
123    
124  int user_list_load(MYSQL *db, USER_LIST *p_list)  int user_list_load(MYSQL *db, USER_LIST *p_list)
125  {  {
126      MYSQL_RES *rs = NULL;          MYSQL_RES *rs = NULL;
127      MYSQL_ROW row;          MYSQL_ROW row;
128      char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
129      int ret = 0;          int ret = 0;
130      int i;          int i;
131            int j;
132      if (db == NULL || p_list == NULL)          int32_t last_uid;
133      {          size_t intro_buf_offset;
134          log_error("NULL pointer error\n");          size_t intro_len;
135          return -1;  
136      }          if (db == NULL || p_list == NULL)
137            {
138      snprintf(sql, sizeof(sql),                  log_error("NULL pointer error\n");
139               "SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, "                  return -1;
140               "UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday) "          }
141               "FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "  
142               "INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID "          if (p_list->user_count > 0)
143               "WHERE enable ORDER BY UID");          {
144                    last_uid = p_list->users[p_list->user_count - 1].uid;
145      if (mysql_query(db, sql) != 0)          }
146      {          else
147          log_error("Query user info error: %s\n", mysql_error(db));          {
148          ret = -1;                  last_uid = -1;
149          goto cleanup;          }
150      }  
151            snprintf(sql, sizeof(sql),
152      if ((rs = mysql_use_result(db)) == NULL)                           "SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, visit_count, "
153      {                           "UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(last_logout_dt), "
154          log_error("Get user info data failed\n");                           "UNIX_TIMESTAMP(birthday), `introduction` "
155          ret = -1;                           "FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
156          goto cleanup;                           "INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID "
157      }                           "WHERE enable ORDER BY username");
158    
159      i = 0;          if (mysql_query(db, sql) != 0)
160      while ((row = mysql_fetch_row(rs)))          {
161      {                  log_error("Query user info error: %s\n", mysql_error(db));
162          p_list->users[i].uid = atoi(row[0]);                  ret = -1;
163          strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1);                  goto cleanup;
164          p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0';          }
165          strncpy(p_list->users[i].nickname, row[2], sizeof(p_list->users[i].nickname) - 1);  
166          p_list->users[i].nickname[sizeof(p_list->users[i].nickname) - 1] = '\0';          if ((rs = mysql_use_result(db)) == NULL)
167          p_list->users[i].gender = row[3][0];          {
168          p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4]));                  log_error("Get user info data failed\n");
169          p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5]));                  ret = -1;
170          p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6]));                  goto cleanup;
171          p_list->users[i].signup_dt = (row[7] == NULL ? 0 : atol(row[7]));          }
172          p_list->users[i].last_login_dt = (row[8] == NULL ? 0 : atol(row[8]));  
173          p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9]));          intro_buf_offset = 0;
174            i = 0;
175          i++;          while ((row = mysql_fetch_row(rs)))
176          if (i >= BBS_max_user_count)          {
177          {                  // record
178              log_error("Too many users, exceed limit %d\n", BBS_max_user_count);                  p_list->users[i].id = i;
179              break;                  p_list->users[i].uid = atoi(row[0]);
180          }                  strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1);
181      }                  p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0';
182      mysql_free_result(rs);                  strncpy(p_list->users[i].nickname, row[2], sizeof(p_list->users[i].nickname) - 1);
183      rs = NULL;                  p_list->users[i].nickname[sizeof(p_list->users[i].nickname) - 1] = '\0';
184                    p_list->users[i].gender = row[3][0];
185                    p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4]));
186                    p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5]));
187                    p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6]));
188                    p_list->users[i].visit_count = (row[7] == NULL ? 0 : atoi(row[7]));
189                    p_list->users[i].signup_dt = (row[8] == NULL ? 0 : atol(row[8]));
190                    p_list->users[i].last_login_dt = (row[9] == NULL ? 0 : atol(row[9]));
191                    p_list->users[i].last_logout_dt = (row[10] == NULL ? 0 : atol(row[10]));
192                    p_list->users[i].birthday = (row[11] == NULL ? 0 : atol(row[11]));
193                    intro_len = strlen((row[12] == NULL ? "" : row[12]));
194                    if (intro_len >= sizeof(p_list->user_intro_buf) - 1 - intro_buf_offset)
195                    {
196                            log_error("OOM for user introduction: len=%d, i=%d\n", intro_len, i);
197                            break;
198                    }
199                    memcpy(p_list->user_intro_buf + intro_buf_offset,
200                               (row[12] == NULL ? "" : row[12]),
201                               intro_len + 1);
202                    p_list->users[i].intro = p_list->user_intro_buf + intro_buf_offset;
203                    intro_buf_offset += (intro_len + 1);
204    
205                    i++;
206                    if (i >= BBS_max_user_count)
207                    {
208                            log_error("Too many users, exceed limit %d\n", BBS_max_user_count);
209                            break;
210                    }
211            }
212            mysql_free_result(rs);
213            rs = NULL;
214    
215            if (i != p_list->user_count || p_list->users[i - 1].uid != last_uid) // Count of users changed
216            {
217                    // Rebuild index
218                    for (j = 0; j < i; j++)
219                    {
220                            p_list->index_uid[j].uid = p_list->users[j].uid;
221                            p_list->index_uid[j].id = j;
222                    }
223    
224      p_list->user_count = i;                  qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
225    
226  #ifdef _DEBUG  #ifdef _DEBUG
227      log_error("Loaded %d users\n", p_list->user_count);                  log_error("Rebuild index of %d users, last_uid=%d\n", i, p_list->users[i - 1].uid);
228    #endif
229            }
230    
231            p_list->user_count = i;
232    
233    #ifdef _DEBUG
234            log_error("Loaded %d users\n", p_list->user_count);
235  #endif  #endif
236    
237  cleanup:  cleanup:
238      mysql_free_result(rs);          mysql_free_result(rs);
239    
240      return ret;          return ret;
241  }  }
242    
243  int user_list_pool_init(void)  int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list)
244  {  {
245      int shmid;          MYSQL_RES *rs = NULL;
246      int semid;          MYSQL_ROW row;
247      int proj_id;          char sql[SQL_BUFFER_LEN];
248      key_t key;          int ret = 0;
249      size_t size;          int i;
250      void *p_shm;          int j;
251      union semun arg;          int user_cnt;
252      int i;          int guest_cnt;
253    
254      if (p_user_list_pool != NULL)          if (db == NULL || p_online_list == NULL)
255      {          {
256          log_error("p_user_list_pool already initialized\n");                  log_error("NULL pointer error\n");
257          return -1;                  return -1;
258      }          }
259    
260      // Allocate shared memory          snprintf(sql, sizeof(sql),
261      proj_id = (int)(time(NULL) % getpid());                           "SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), "
262      key = ftok(VAR_USER_LIST_SHM, proj_id);                           "UNIX_TIMESTAMP(last_tm) FROM user_online "
263      if (key == -1)                           "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) "
264      {                           "ORDER BY last_tm DESC",
265          log_error("ftok(%s %d) error (%d)\n", VAR_USER_LIST_SHM, proj_id, errno);                           BBS_user_off_line);
266          return -2;  
267      }          if (mysql_query(db, sql) != 0)
268            {
269      size = sizeof(USER_LIST_POOL);                  log_error("Query user online error: %s\n", mysql_error(db));
270      shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  ret = -1;
271      if (shmid == -1)                  goto cleanup;
272      {          }
273          log_error("shmget(size = %d) error (%d)\n", size, errno);  
274          return -3;          if ((rs = mysql_use_result(db)) == NULL)
275      }          {
276      p_shm = shmat(shmid, NULL, 0);                  log_error("Get user online data failed\n");
277      if (p_shm == (void *)-1)                  ret = -1;
278      {                  goto cleanup;
279          log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);          }
280          return -3;  
281      }          i = 0;
282            user_cnt = 0;
283      p_user_list_pool = p_shm;          guest_cnt = 0;
284      p_user_list_pool->shmid = shmid;          while ((row = mysql_fetch_row(rs)))
285            {
286      // Allocate semaphore as user list pool lock                  if (atoi(row[1]) == 0) // guest
287      size = 2; // r_sem and w_sem                  {
288      semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);                          guest_cnt++;
289      if (semid == -1)                          continue;
290      {                  }
291          log_error("semget(user_list_pool_sem, size = %d) error (%d)\n", size, errno);                  else
292          return -3;                  {
293      }                          user_cnt++;
294                    }
295      // Initialize sem value to 0  
296      arg.val = 0;                  p_online_list->users[i].id = i;
297      for (i = 0; i < size; i++)                  strncpy(p_online_list->users[i].session_id, row[0], sizeof(p_online_list->users[i].session_id) - 1);
298      {                  p_online_list->users[i].session_id[sizeof(p_online_list->users[i].session_id) - 1] = '\0';
299          if (semctl(semid, i, SETVAL, arg) == -1)  
300          {                  if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_online_list->users[i].user_info), NULL, 0)) <= 0)
301              log_error("semctl(user_list_pool_sem, SETVAL) error (%d)\n", errno);                  {
302              return -3;                          log_error("query_user_info_by_uid(%d) error\n", atoi(row[1]));
303          }                          continue;
304      }                  }
305    
306      p_user_list_pool->semid = semid;                  strncpy(p_online_list->users[i].ip, row[2], sizeof(p_online_list->users[i].ip) - 1);
307                    p_online_list->users[i].ip[sizeof(p_online_list->users[i].ip) - 1] = '\0';
308      // Set user counts to 0  
309      p_user_list_pool->user_list[0].user_count = 0;                  strncpy(p_online_list->users[i].current_action, row[3], sizeof(p_online_list->users[i].current_action) - 1);
310      p_user_list_pool->user_list[1].user_count = 0;                  p_online_list->users[i].current_action[sizeof(p_online_list->users[i].current_action) - 1] = '\0';
311                    p_online_list->users[i].current_action_title = NULL;
312                    if (p_online_list->users[i].current_action[0] == '\0')
313                    {
314                            p_online_list->users[i].current_action_title = "";
315                    }
316                    else if (trie_dict_get(p_trie_action_dict, p_online_list->users[i].current_action, (int64_t *)(&(p_online_list->users[i].current_action_title))) < 0)
317                    {
318                            log_error("trie_dict_get(p_trie_action_dict, %s) error on session_id=%s\n",
319                                              p_online_list->users[i].current_action, p_online_list->users[i].session_id);
320                            continue;
321                    }
322    
323                    p_online_list->users[i].login_tm = (row[4] == NULL ? 0 : atol(row[4]));
324                    p_online_list->users[i].last_tm = (row[5] == NULL ? 0 : atol(row[5]));
325    
326                    i++;
327                    if (i >= BBS_max_user_online_count)
328                    {
329                            log_error("Too many online users, exceed limit %d\n", BBS_max_user_online_count);
330                            break;
331                    }
332            }
333            mysql_free_result(rs);
334            rs = NULL;
335    
336            if (user_cnt > 0)
337            {
338                    // Rebuild index
339                    for (j = 0; j < user_cnt; j++)
340                    {
341                            p_online_list->index_uid[j].uid = p_online_list->users[j].user_info.uid;
342                            p_online_list->index_uid[j].id = j;
343                    }
344    
345                    qsort(p_online_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
346            }
347    
348      p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);          p_online_list->user_count = user_cnt;
349      p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);          p_online_list->guest_count = guest_cnt;
350    
351      return 0;  cleanup:
352            mysql_free_result(rs);
353    
354            return ret;
355  }  }
356    
357  void user_list_pool_cleanup(void)  int user_login_count_load(MYSQL *db)
358    {
359            MYSQL_RES *rs = NULL;
360            MYSQL_ROW row;
361            char sql[SQL_BUFFER_LEN];
362    
363            if (db == NULL)
364            {
365                    log_error("NULL pointer error\n");
366                    return -1;
367            }
368    
369            snprintf(sql, sizeof(sql),
370                             "SELECT ID FROM user_login_log ORDER BY ID DESC LIMIT 1");
371            if (mysql_query(db, sql) != 0)
372            {
373                    log_error("Query user_login_log error: %s\n", mysql_error(db));
374                    return -2;
375            }
376            if ((rs = mysql_store_result(db)) == NULL)
377            {
378                    log_error("Get user_login_log data failed\n");
379                    return -2;
380            }
381            if ((row = mysql_fetch_row(rs)))
382            {
383                    p_user_list_pool->user_login_count = atoi(row[0]);
384            }
385            mysql_free_result(rs);
386    
387            return 0;
388    }
389    
390    int user_list_pool_init(const char *filename)
391  {  {
392      int shmid;          char filepath[FILE_PATH_LEN];
393            int fd;
394            size_t size;
395            void *p_shm;
396            int proj_id;
397            key_t key;
398            int semid;
399            union semun arg;
400            int i;
401    
402            if (p_user_list_pool != NULL || p_trie_action_dict != NULL)
403            {
404                    log_error("p_user_list_pool already initialized\n");
405                    return -1;
406            }
407    
408            p_trie_action_dict = trie_dict_create();
409            if (p_trie_action_dict == NULL)
410            {
411                    log_error("trie_dict_create() error\n");
412                    return -1;
413            }
414    
415            for (i = 0; i < user_action_map_size; i++)
416            {
417                    if (trie_dict_set(p_trie_action_dict, user_action_map[i].name, (int64_t)(user_action_map[i].title)) < 0)
418                    {
419                            log_error("trie_dict_set(p_trie_action_dict, %s) error\n", user_action_map[i].name);
420                    }
421            }
422    
423            // Allocate shared memory
424            size = sizeof(USER_LIST_POOL);
425    
426            strncpy(filepath, filename, sizeof(filepath) - 1);
427            filepath[sizeof(filepath) - 1] = '\0';
428            snprintf(user_list_shm_name, sizeof(user_list_shm_name), "/USER_LIST_SHM_%s", basename(filepath));
429    
430            if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT)
431            {
432                    log_error("shm_unlink(%s) error (%d)\n", user_list_shm_name, errno);
433                    return -2;
434            }
435    
436            if ((fd = shm_open(user_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
437            {
438                    log_error("shm_open(%s) error (%d)\n", user_list_shm_name, errno);
439                    return -2;
440            }
441            if (ftruncate(fd, (off_t)size) == -1)
442            {
443                    log_error("ftruncate(size=%d) error (%d)\n", size, errno);
444                    close(fd);
445                    return -2;
446            }
447    
448            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
449            if (p_shm == MAP_FAILED)
450            {
451                    log_error("mmap() error (%d)\n", errno);
452                    close(fd);
453                    return -2;
454            }
455    
456            if (close(fd) < 0)
457            {
458                    log_error("close(fd) error (%d)\n", errno);
459                    return -1;
460            }
461    
462            p_user_list_pool = p_shm;
463            p_user_list_pool->shm_size = size;
464    
465            // Allocate semaphore as user list pool lock
466            proj_id = (int)(time(NULL) % getpid());
467            key = ftok(filename, proj_id);
468            if (key == -1)
469            {
470                    log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
471                    return -2;
472            }
473    
474            size = 2; // r_sem and w_sem
475            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
476            if (semid == -1)
477            {
478                    log_error("semget(user_list_pool_sem, size = %d) error (%d)\n", size, errno);
479                    return -3;
480            }
481    
482            // Initialize sem value to 0
483            arg.val = 0;
484            for (i = 0; i < size; i++)
485            {
486                    if (semctl(semid, i, SETVAL, arg) == -1)
487                    {
488                            log_error("semctl(user_list_pool_sem, SETVAL) error (%d)\n", errno);
489                            return -3;
490                    }
491            }
492    
493            p_user_list_pool->semid = semid;
494    
495            // Set user counts to 0
496            p_user_list_pool->user_list[0].user_count = 0;
497            p_user_list_pool->user_list[1].user_count = 0;
498    
499            p_user_list_pool->user_list_index_current = 0;
500            p_user_list_pool->user_list_index_new = 1;
501    
502      if (p_user_list_pool == NULL)          p_user_list_pool->user_online_list_index_current = 0;
503      {          p_user_list_pool->user_online_list_index_new = 1;
         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);  
     }  
504    
505      p_user_list_pool = NULL;          user_stat_map_init(&(p_user_list_pool->user_stat_map));
506    
507            return 0;
508  }  }
509    
510  int set_user_list_pool_shm_readonly(void)  void user_list_pool_cleanup(void)
511  {  {
512      int shmid;          if (p_user_list_pool == NULL)
513      void *p_shm;          {
514                    return;
515            }
516    
517            if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)
518            {
519                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_user_list_pool->semid, errno);
520            }
521    
522            detach_user_list_pool_shm();
523    
524            if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT)
525            {
526                    log_error("shm_unlink(%s) error (%d)\n", user_list_shm_name, errno);
527            }
528    
529            user_list_shm_name[0] = '\0';
530    
531            if (p_trie_action_dict != NULL)
532            {
533                    trie_dict_destroy(p_trie_action_dict);
534    
535      if (p_user_list_pool == NULL)                  p_trie_action_dict = NULL;
536      {          }
537          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;  
     }  
538    
539      p_user_list_pool = p_shm;  int set_user_list_pool_shm_readonly(void)
540    {
541            if (p_user_list_pool != NULL && mprotect(p_user_list_pool, p_user_list_pool->shm_size, PROT_READ) < 0)
542            {
543                    log_error("mprotect() error (%d)\n", errno);
544                    return -1;
545            }
546    
547      return 0;          return 0;
548  }  }
549    
550  int detach_user_list_pool_shm(void)  int detach_user_list_pool_shm(void)
551  {  {
552      if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)          if (p_user_list_pool != NULL && munmap(p_user_list_pool, p_user_list_pool->shm_size) < 0)
553      {          {
554          log_error("shmdt(user_list_pool) error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
555          return -1;                  return -1;
556      }          }
557    
558      p_user_list_pool = NULL;          p_user_list_pool = NULL;
559    
560      return 0;          return 0;
561  }  }
562    
563  int user_list_pool_reload(void)  int user_list_pool_reload(int online_user)
564  {  {
565      MYSQL *db = NULL;          MYSQL *db = NULL;
566      USER_LIST *p_tmp;          int tmp;
567            int ret = 0;
568      if (p_user_list_pool == NULL)  
569      {          if (p_user_list_pool == NULL)
570          log_error("p_user_list_pool not initialized\n");          {
571          return -1;                  log_error("p_user_list_pool not initialized\n");
572      }                  return -1;
573            }
574      db = db_open();  
575      if (db == NULL)          db = db_open();
576      {          if (db == NULL)
577          log_error("db_open() error: %s\n", mysql_error(db));          {
578          return -1;                  log_error("db_open() error: %s\n", mysql_error(db));
579      }                  return -1;
580            }
581      if (user_list_load(db, p_user_list_pool->p_new) < 0)  
582      {          if (online_user)
583          log_error("user_list_rw_lock() error\n");          {
584          return -2;                  if (user_online_list_load(db, &(p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_new])) < 0)
585      }                  {
586                            log_error("user_online_list_load() error\n");
587      mysql_close(db);                          ret = -2;
588                            goto cleanup;
589      if (user_list_rw_lock() < 0)                  }
590      {  
591          log_error("user_list_rw_lock() error\n");                  if (user_login_count_load(db) < 0)
592          return -3;                  {
593      }                          log_error("user_login_count_load() error\n");
594                            ret = -2;
595      // Swap p_current and p_new                          goto cleanup;
596      p_tmp = p_user_list_pool->p_current;                  }
597      p_user_list_pool->p_current = p_user_list_pool->p_new;          }
598      p_user_list_pool->p_new = p_tmp;          else
599            {
600      if (user_list_rw_unlock() < 0)                  if (user_list_load(db, &(p_user_list_pool->user_list[p_user_list_pool->user_list_index_new])) < 0)
601      {                  {
602          log_error("user_list_rw_unlock() error\n");                          log_error("user_list_load() error\n");
603          return -3;                          ret = -2;
604      }                          goto cleanup;
605                    }
606            }
607    
608            mysql_close(db);
609            db = NULL;
610    
611            if (user_list_rw_lock() < 0)
612            {
613                    log_error("user_list_rw_lock() error\n");
614                    ret = -3;
615                    goto cleanup;
616            }
617    
618            if (online_user)
619            {
620                    // Swap p_online_current and p_online_new
621                    tmp = p_user_list_pool->user_online_list_index_current;
622                    p_user_list_pool->user_online_list_index_current = p_user_list_pool->user_online_list_index_new;
623                    p_user_list_pool->user_online_list_index_new = tmp;
624            }
625            else
626            {
627                    // Swap index_current and index_new
628                    tmp = p_user_list_pool->user_list_index_current;
629                    p_user_list_pool->user_list_index_current = p_user_list_pool->user_list_index_new;
630                    p_user_list_pool->user_list_index_new = tmp;
631            }
632    
633            if (user_list_rw_unlock() < 0)
634            {
635                    log_error("user_list_rw_unlock() error\n");
636                    ret = -3;
637                    goto cleanup;
638            }
639    
640      return 0;  cleanup:
641            mysql_close(db);
642    
643            return ret;
644  }  }
645    
646  int user_list_try_rd_lock(int wait_sec)  int user_list_try_rd_lock(int wait_sec)
647  {  {
648      struct sembuf sops[2];          struct sembuf sops[2];
649      struct timespec timeout;  #ifndef __CYGWIN__
650      int ret;          struct timespec timeout;
651    #endif
652      sops[0].sem_num = 1; // w_sem          int ret;
653      sops[0].sem_op = 0;  // wait until unlocked  
654      sops[0].sem_flg = 0;          if (p_user_list_pool == NULL)
655            {
656      sops[1].sem_num = 0;        // r_sem                  log_error("p_user_list_pool not initialized\n");
657      sops[1].sem_op = 1;         // lock                  return -1;
658      sops[1].sem_flg = SEM_UNDO; // undo on terminate          }
659    
660      timeout.tv_sec = wait_sec;          sops[0].sem_num = 1; // w_sem
661      timeout.tv_nsec = 0;          sops[0].sem_op = 0;      // wait until unlocked
662            sops[0].sem_flg = 0;
663      ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);  
664      if (ret == -1 && errno != EAGAIN && errno != EINTR)          sops[1].sem_num = 0;            // r_sem
665      {          sops[1].sem_op = 1;                     // lock
666          log_error("semtimedop(lock read) error %d\n", errno);          sops[1].sem_flg = SEM_UNDO; // undo on terminate
667      }  
668    #ifdef __CYGWIN__
669            ret = semop(p_user_list_pool->semid, sops, 2);
670    #else
671            timeout.tv_sec = wait_sec;
672            timeout.tv_nsec = 0;
673    
674      return ret;          ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);
675    #endif
676            if (ret == -1 && errno != EAGAIN && errno != EINTR)
677            {
678                    log_error("semop(lock read) error %d\n", errno);
679            }
680    
681            return ret;
682  }  }
683    
684  int user_list_try_rw_lock(int wait_sec)  int user_list_try_rw_lock(int wait_sec)
685  {  {
686      struct sembuf sops[3];          struct sembuf sops[3];
687      struct timespec timeout;  #ifndef __CYGWIN__
688      int ret;          struct timespec timeout;
689    #endif
690      sops[0].sem_num = 1; // w_sem          int ret;
691      sops[0].sem_op = 0;  // wait until unlocked  
692      sops[0].sem_flg = 0;          if (p_user_list_pool == NULL)
693            {
694      sops[1].sem_num = 1;        // w_sem                  log_error("p_user_list_pool not initialized\n");
695      sops[1].sem_op = 1;         // lock                  return -1;
696      sops[1].sem_flg = SEM_UNDO; // undo on terminate          }
697    
698      sops[2].sem_num = 0; // r_sem          sops[0].sem_num = 1; // w_sem
699      sops[2].sem_op = 0;  // wait until unlocked          sops[0].sem_op = 0;      // wait until unlocked
700      sops[2].sem_flg = 0;          sops[0].sem_flg = 0;
701    
702      timeout.tv_sec = wait_sec;          sops[1].sem_num = 1;            // w_sem
703      timeout.tv_nsec = 0;          sops[1].sem_op = 1;                     // lock
704            sops[1].sem_flg = SEM_UNDO; // undo on terminate
705      ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);  
706      if (ret == -1 && errno != EAGAIN && errno != EINTR)          sops[2].sem_num = 0; // r_sem
707      {          sops[2].sem_op = 0;      // wait until unlocked
708          log_error("semtimedop(lock write) error %d\n", errno);          sops[2].sem_flg = 0;
709      }  
710    #ifdef __CYGWIN__
711            ret = semop(p_user_list_pool->semid, sops, 3);
712    #else
713            timeout.tv_sec = wait_sec;
714            timeout.tv_nsec = 0;
715    
716      return ret;          ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);
717    #endif
718            if (ret == -1 && errno != EAGAIN && errno != EINTR)
719            {
720                    log_error("semop(lock write) error %d\n", errno);
721            }
722    
723            return ret;
724  }  }
725    
726  int user_list_rd_unlock(void)  int user_list_rd_unlock(void)
727  {  {
728      struct sembuf sops[2];          struct sembuf sops[2];
729      int ret;          int ret;
730    
731      sops[0].sem_num = 0;                     // r_sem          if (p_user_list_pool == NULL)
732      sops[0].sem_op = -1;                     // unlock          {
733      sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  log_error("p_user_list_pool not initialized\n");
734                    return -1;
735      ret = semop(p_user_list_pool->semid, sops, 1);          }
736      if (ret == -1 && errno != EAGAIN && errno != EINTR)  
737      {          sops[0].sem_num = 0;                                     // r_sem
738          log_error("semop(unlock read) error %d\n", errno);          sops[0].sem_op = -1;                                     // unlock
739      }          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
740    
741            ret = semop(p_user_list_pool->semid, sops, 1);
742            if (ret == -1 && errno != EAGAIN && errno != EINTR)
743            {
744                    log_error("semop(unlock read) error %d\n", errno);
745            }
746    
747      return ret;          return ret;
748  }  }
749    
750  int user_list_rw_unlock(void)  int user_list_rw_unlock(void)
751  {  {
752      struct sembuf sops[1];          struct sembuf sops[1];
753      int ret;          int ret;
754    
755      sops[0].sem_num = 1;                     // w_sem          if (p_user_list_pool == NULL)
756      sops[0].sem_op = -1;                     // unlock          {
757      sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  log_error("p_user_list_pool not initialized\n");
758                    return -1;
759      ret = semop(p_user_list_pool->semid, sops, 1);          }
760      if (ret == -1 && errno != EAGAIN && errno != EINTR)  
761      {          sops[0].sem_num = 1;                                     // w_sem
762          log_error("semop(unlock write) error %d\n", errno);          sops[0].sem_op = -1;                                     // unlock
763      }          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
764    
765            ret = semop(p_user_list_pool->semid, sops, 1);
766            if (ret == -1 && errno != EAGAIN && errno != EINTR)
767            {
768                    log_error("semop(unlock write) error %d\n", errno);
769            }
770    
771      return ret;          return ret;
772  }  }
773    
774  int user_list_rd_lock(void)  int user_list_rd_lock(void)
775  {  {
776      int timer = 0;          int timer = 0;
777      int ret = -1;          int ret = -1;
778    
779      while (!SYS_server_exit)          if (p_user_list_pool == NULL)
780      {          {
781          ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME);                  log_error("p_user_list_pool not initialized\n");
782          if (ret == 0) // success                  return -1;
783          {          }
784              break;  
785          }          while (!SYS_server_exit)
786          else if (errno == EAGAIN || errno == EINTR) // retry          {
787          {                  ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME);
788              timer++;                  if (ret == 0) // success
789              if (timer % USER_LIST_TRY_LOCK_TIMES == 0)                  {
790              {                          break;
791                  log_error("user_list_try_rd_lock() tried %d times\n", timer);                  }
792              }                  else if (errno == EAGAIN || errno == EINTR) // retry
793          }                  {
794          else // failed                          timer++;
795          {                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
796              log_error("user_list_try_rd_lock() failed\n");                          {
797              break;                                  log_error("user_list_try_rd_lock() tried %d times\n", timer);
798          }                          }
799      }                  }
800                    else // failed
801                    {
802                            log_error("user_list_try_rd_lock() failed\n");
803                            break;
804                    }
805            }
806    
807      return ret;          return ret;
808  }  }
809    
810  int user_list_rw_lock(void)  int user_list_rw_lock(void)
811  {  {
812      int timer = 0;          int timer = 0;
813      int ret = -1;          int ret = -1;
814    
815      while (!SYS_server_exit)          if (p_user_list_pool == NULL)
816      {          {
817          ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME);                  log_error("p_user_list_pool not initialized\n");
818          if (ret == 0) // success                  return -1;
819          {          }
820              break;  
821          }          while (!SYS_server_exit)
822          else if (errno == EAGAIN || errno == EINTR) // retry          {
823          {                  ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME);
824              timer++;                  if (ret == 0) // success
825              if (timer % USER_LIST_TRY_LOCK_TIMES == 0)                  {
826              {                          break;
827                  log_error("user_list_try_rw_lock() tried %d times\n", timer);                  }
828              }                  else if (errno == EAGAIN || errno == EINTR) // retry
829          }                  {
830          else // failed                          timer++;
831          {                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
832              log_error("user_list_try_rw_lock() failed\n");                          {
833              break;                                  log_error("user_list_try_rw_lock() tried %d times\n", timer);
834          }                          }
835      }                  }
836                    else // failed
837                    {
838                            log_error("user_list_try_rw_lock() failed\n");
839                            break;
840                    }
841            }
842    
843      return ret;          return ret;
844  }  }
845    
846  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)
847  {  {
848      int ret = 0;          int ret = 0;
849    
850      if (p_users == NULL || p_user_count == NULL || p_page_count == NULL)          if (p_users == NULL || p_user_count == NULL || p_page_count == NULL)
851      {          {
852          log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
853          return -1;                  return -1;
854      }          }
855    
856      // acquire lock of user list          *p_user_count = 0;
857      if (user_list_rd_lock() < 0)          *p_page_count = 0;
858      {  
859          log_error("user_list_rd_lock() error\n");          // acquire lock of user list
860          return -2;          if (user_list_rd_lock() < 0)
861      }          {
862                    log_error("user_list_rd_lock() error\n");
863      if (p_user_list_pool->p_current->user_count == 0)                  return -2;
864      {          }
865          // empty list  
866          ret = 0;          if (p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count == 0)
867          goto cleanup;          {
868      }                  // empty list
869                    ret = 0;
870      *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +                  goto cleanup;
871                      (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);          }
872    
873      if (page_id < 0 || page_id >= *p_page_count)          *p_page_count = (p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count + BBS_user_limit_per_page - 1) /
874      {                                          BBS_user_limit_per_page;
875          log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);  
876          ret = -3;          if (page_id < 0 || page_id >= *p_page_count)
877          goto cleanup;          {
878      }                  log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
879                    ret = -3;
880      *p_user_count = MIN(BBS_user_limit_per_page,                  goto cleanup;
881                          p_user_list_pool->p_current->user_count -          }
882                              page_id * BBS_user_limit_per_page);  
883            *p_user_count = MIN(BBS_user_limit_per_page,
884      memcpy(p_users,                                                  p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count -
885             p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,                                                          page_id * BBS_user_limit_per_page);
886             sizeof(USER_INFO) * (size_t)(*p_user_count));  
887            memcpy(p_users,
888                       p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users + page_id * BBS_user_limit_per_page,
889                       sizeof(USER_INFO) * (size_t)(*p_user_count));
890    
891  cleanup:  cleanup:
892      // release lock of user list          // release lock of user list
893      if (user_list_rd_unlock() < 0)          if (user_list_rd_unlock() < 0)
894      {          {
895          log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
896          ret = -1;                  ret = -1;
897      }          }
898    
899      return ret;          return ret;
900  }  }
901    
902  int query_user_info(int32_t uid, USER_INFO *p_user)  int query_user_online_list(int page_id, USER_ONLINE_INFO *p_online_users, int *p_user_count, int *p_page_count)
903  {  {
904      int left;          int ret = 0;
905      int right;  
906      int mid;          if (p_online_users == NULL || p_user_count == NULL || p_page_count == NULL)
907      int ret = 0;          {
908                    log_error("NULL pointer error\n");
909      if (p_user == NULL)                  return -1;
910      {          }
911          log_error("NULL pointer error\n");  
912          return -1;          *p_user_count = 0;
913      }          *p_page_count = 0;
914    
915      // acquire lock of user list          // acquire lock of user list
916      if (user_list_rd_lock() < 0)          if (user_list_rd_lock() < 0)
917      {          {
918          log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
919          return -2;                  return -2;
920      }          }
921    
922      left = 0;          if (p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count == 0)
923      right = p_user_list_pool->p_current->user_count - 1;          {
924                    // empty list
925      while (left < right)                  ret = 0;
926      {                  goto cleanup;
927          mid = (left + right) / 2;          }
928          if (uid < p_user_list_pool->p_current->users[mid].uid)  
929          {          *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;
930              right = mid;  
931          }          if (page_id < 0 || page_id >= *p_page_count)
932          else if (uid > p_user_list_pool->p_current->users[mid].uid)          {
933          {                  log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
934              left = mid + 1;                  ret = -3;
935          }                  goto cleanup;
936          else // if (uid == p_user_list_pool->p_current->users[mid].uid)          }
937          {  
938              left = mid;          *p_user_count = MIN(BBS_user_limit_per_page,
939              break;                                                  p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count -
940          }                                                          page_id * BBS_user_limit_per_page);
941      }  
942            memcpy(p_online_users,
943      if (uid == p_user_list_pool->p_current->users[left].uid) // Found                     p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users + page_id * BBS_user_limit_per_page,
944      {                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));
945          *p_user = p_user_list_pool->p_current->users[left];  
946          ret = 1;  cleanup:
947      }          // release lock of user list
948            if (user_list_rd_unlock() < 0)
949      // release lock of user list          {
950      if (user_list_rd_unlock() < 0)                  log_error("user_list_rd_unlock() error\n");
951      {                  ret = -1;
952          log_error("user_list_rd_unlock() error\n");          }
953          ret = -1;  
954      }          return ret;
955    }
956    
957    int get_user_list_count(int *p_user_cnt)
958    {
959            if (p_user_cnt == NULL)
960            {
961                    log_error("NULL pointer error\n");
962                    return -1;
963            }
964    
965            // acquire lock of user list
966            if (user_list_rd_lock() < 0)
967            {
968                    log_error("user_list_rd_lock() error\n");
969                    return -2;
970            }
971    
972            *p_user_cnt = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count;
973    
974            // release lock of user list
975            if (user_list_rd_unlock() < 0)
976            {
977                    log_error("user_list_rd_unlock() error\n");
978                    return -2;
979            }
980    
981            return 0;
982    }
983    
984    int get_user_online_list_count(int *p_user_cnt, int *p_guest_cnt)
985    {
986            if (p_user_cnt == NULL || p_guest_cnt == NULL)
987            {
988                    log_error("NULL pointer error\n");
989                    return -1;
990            }
991    
992            // acquire lock of user list
993            if (user_list_rd_lock() < 0)
994            {
995                    log_error("user_list_rd_lock() error\n");
996                    return -2;
997            }
998    
999            *p_user_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count;
1000            *p_guest_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].guest_count;
1001    
1002            // release lock of user list
1003            if (user_list_rd_unlock() < 0)
1004            {
1005                    log_error("user_list_rd_unlock() error\n");
1006                    return -2;
1007            }
1008    
1009            return 0;
1010    }
1011    
1012    int get_user_login_count(int *p_login_cnt)
1013    {
1014            if (p_login_cnt == NULL)
1015            {
1016                    log_error("NULL pointer error\n");
1017                    return -1;
1018            }
1019    
1020            *p_login_cnt = p_user_list_pool->user_login_count;
1021    
1022            return 0;
1023    }
1024    
1025    int query_user_info(int32_t id, USER_INFO *p_user)
1026    {
1027            int ret = 0;
1028    
1029            if (p_user == NULL)
1030            {
1031                    log_error("NULL pointer error\n");
1032                    return -1;
1033            }
1034    
1035            // acquire lock of user list
1036            if (user_list_rd_lock() < 0)
1037            {
1038                    log_error("user_list_rd_lock() error\n");
1039                    return -2;
1040            }
1041    
1042            if (id >= 0 && id < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count) // Found
1043            {
1044                    *p_user = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id];
1045                    ret = 1;
1046            }
1047    
1048            // release lock of user list
1049            if (user_list_rd_unlock() < 0)
1050            {
1051                    log_error("user_list_rd_unlock() error\n");
1052                    ret = -1;
1053            }
1054    
1055            return ret;
1056    }
1057    
1058    int query_user_info_by_uid(int32_t uid, USER_INFO *p_user, char *p_intro_buf, size_t intro_buf_len)
1059    {
1060            int left;
1061            int right;
1062            int mid;
1063            int32_t id;
1064            int ret = 0;
1065    
1066            if (p_user == NULL)
1067            {
1068                    log_error("NULL pointer error\n");
1069                    return -1;
1070            }
1071    
1072            // acquire lock of user list
1073            if (user_list_rd_lock() < 0)
1074            {
1075                    log_error("user_list_rd_lock() error\n");
1076                    return -2;
1077            }
1078    
1079            left = 0;
1080            right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1081    
1082            while (left < right)
1083            {
1084                    mid = (left + right) / 2;
1085                    if (uid < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1086                    {
1087                            right = mid - 1;
1088                    }
1089                    else if (uid > p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1090                    {
1091                            left = mid + 1;
1092                    }
1093                    else // if (uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1094                    {
1095                            left = mid;
1096                            break;
1097                    }
1098            }
1099    
1100            if (uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left].uid) // Found
1101            {
1102                    id = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left].id;
1103                    *p_user = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id];
1104                    ret = 1;
1105    
1106                    if (p_intro_buf != NULL)
1107                    {
1108                            strncpy(p_intro_buf, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id].intro, intro_buf_len - 1);
1109                            p_intro_buf[intro_buf_len - 1] = '\0';
1110                            p_user->intro = p_intro_buf;
1111                    }
1112            }
1113    
1114            // release lock of user list
1115            if (user_list_rd_unlock() < 0)
1116            {
1117                    log_error("user_list_rd_unlock() error\n");
1118                    ret = -1;
1119            }
1120    
1121            return ret;
1122    }
1123    
1124    int query_user_info_by_username(const char *username_prefix, int max_user_cnt,
1125                                                                    int32_t uid_list[], char username_list[][BBS_username_max_len + 1])
1126    {
1127            int left;
1128            int right;
1129            int mid;
1130            int left_save;
1131            int ret = 0;
1132            size_t prefix_len;
1133            int comp;
1134            int i;
1135    
1136            if (username_prefix == NULL || uid_list == NULL || username_list == NULL)
1137            {
1138                    log_error("NULL pointer error\n");
1139                    return -1;
1140            }
1141    
1142            prefix_len = strlen(username_prefix);
1143    
1144            // acquire lock of user list
1145            if (user_list_rd_lock() < 0)
1146            {
1147                    log_error("user_list_rd_lock() error\n");
1148                    return -2;
1149            }
1150    
1151            left = 0;
1152            right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1153    
1154            while (left < right)
1155            {
1156                    mid = (left + right) / 2;
1157                    comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1158                    if (comp < 0)
1159                    {
1160                            right = mid - 1;
1161                    }
1162                    else if (comp > 0)
1163                    {
1164                            left = mid + 1;
1165                    }
1166                    else // if (comp == 0)
1167                    {
1168                            left = mid;
1169                            break;
1170                    }
1171            }
1172    
1173            if (strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left].username, prefix_len) == 0) // Found
1174            {
1175    #ifdef _DEBUG
1176                    log_error("Debug: match found, pos=%d\n", left);
1177    #endif
1178    
1179                    left_save = left;
1180                    right = left;
1181                    left = 0;
1182    
1183                    while (left < right)
1184                    {
1185                            mid = (left + right) / 2;
1186                            comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1187                            if (comp > 0)
1188                            {
1189                                    left = mid + 1;
1190                            }
1191                            else if (comp == 0)
1192                            {
1193                                    right = mid;
1194                            }
1195                            else // if (comp < 0)
1196                            {
1197                                    log_error("Bug: left=%d right=%d mid=%d");
1198                                    ret = -2;
1199                                    goto cleanup;
1200                            }
1201                    }
1202    
1203    #ifdef _DEBUG
1204                    log_error("Debug: first match found, pos=%d\n", right);
1205    #endif
1206    
1207                    left = left_save;
1208                    left_save = right;
1209                    right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1210    
1211                    while (left < right)
1212                    {
1213                            mid = (left + right) / 2 + (left + right) % 2;
1214                            comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1215                            if (comp < 0)
1216                            {
1217                                    right = mid - 1;
1218                            }
1219                            else if (comp == 0)
1220                            {
1221                                    left = mid;
1222                            }
1223                            else // if (comp > 0)
1224                            {
1225                                    log_error("Bug: left=%d right=%d mid=%d");
1226                                    ret = -2;
1227                                    goto cleanup;
1228                            }
1229                    }
1230    
1231    #ifdef _DEBUG
1232                    log_error("Debug: last match found, pos=%d\n", left);
1233    #endif
1234    
1235                    right = left;
1236                    left = left_save;
1237    
1238                    for (i = 0; i < max_user_cnt && left + i <= right; i++)
1239                    {
1240                            uid_list[i] = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left + i].uid;
1241                            strncpy(username_list[i], p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left + i].username, sizeof(username_list[i]) - 1);
1242                            username_list[i][sizeof(username_list[i]) - 1] = '\0';
1243                    }
1244                    ret = i;
1245            }
1246    
1247    cleanup:
1248            // release lock of user list
1249            if (user_list_rd_unlock() < 0)
1250            {
1251                    log_error("user_list_rd_unlock() error\n");
1252                    ret = -1;
1253            }
1254    
1255            return ret;
1256    }
1257    
1258    int query_user_online_info(int32_t id, USER_ONLINE_INFO *p_user)
1259    {
1260            int ret = 0;
1261    
1262            if (p_user == NULL)
1263            {
1264                    log_error("NULL pointer error\n");
1265                    return -1;
1266            }
1267    
1268            // acquire lock of user list
1269            if (user_list_rd_lock() < 0)
1270            {
1271                    log_error("user_list_rd_lock() error\n");
1272                    return -2;
1273            }
1274    
1275            if (id >= 0 && id < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count) // Found
1276            {
1277                    *p_user = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users[id];
1278                    ret = 1;
1279            }
1280    
1281            // release lock of user list
1282            if (user_list_rd_unlock() < 0)
1283            {
1284                    log_error("user_list_rd_unlock() error\n");
1285                    ret = -1;
1286            }
1287    
1288            return ret;
1289    }
1290    
1291    int query_user_online_info_by_uid(int32_t uid, USER_ONLINE_INFO *p_users, int *p_user_cnt, int start_id)
1292    {
1293            int left;
1294            int right;
1295            int mid;
1296            int32_t id;
1297            int ret = 0;
1298            int i;
1299            int user_cnt;
1300    
1301            if (p_users == NULL || p_user_cnt == NULL)
1302            {
1303                    log_error("NULL pointer error\n");
1304                    return -1;
1305            }
1306    
1307            user_cnt = *p_user_cnt;
1308            *p_user_cnt = 0;
1309    
1310            // acquire lock of user list
1311            if (user_list_rd_lock() < 0)
1312            {
1313                    log_error("user_list_rd_lock() error\n");
1314                    return -2;
1315            }
1316    
1317            left = start_id;
1318            right = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count - 1;
1319    
1320            while (left < right)
1321            {
1322                    mid = (left + right) / 2;
1323                    if (uid < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1324                    {
1325                            right = mid - 1;
1326                    }
1327                    else if (uid > p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1328                    {
1329                            left = mid + 1;
1330                    }
1331                    else // if (uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1332                    {
1333                            left = mid;
1334                            break;
1335                    }
1336            }
1337    
1338            if (uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].uid)
1339            {
1340                    right = left;
1341                    left = start_id;
1342    
1343                    while (left < right)
1344                    {
1345                            mid = (left + right) / 2;
1346                            if (uid - 1 < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1347                            {
1348                                    right = mid;
1349                            }
1350                            else // if (uid - 1 >= p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1351                            {
1352                                    left = mid + 1;
1353                            }
1354                    }
1355    
1356                    for (i = 0;
1357                             left < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count && i < user_cnt &&
1358                             uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].uid;
1359                             left++, i++)
1360                    {
1361                            id = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].id;
1362                            p_users[i] = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users[id];
1363                    }
1364    
1365                    if (i > 0)
1366                    {
1367                            *p_user_cnt = i;
1368                            ret = 1;
1369                    }
1370            }
1371    
1372            // release lock of user list
1373            if (user_list_rd_unlock() < 0)
1374            {
1375                    log_error("user_list_rd_unlock() error\n");
1376                    ret = -1;
1377            }
1378    
1379            return ret;
1380    }
1381    
1382    int get_user_id_list(int32_t *p_uid_list, int *p_user_cnt, int start_uid)
1383    {
1384            int left;
1385            int right;
1386            int mid;
1387            int ret = 0;
1388            int i;
1389    
1390            if (p_uid_list == NULL || p_user_cnt == NULL)
1391            {
1392                    log_error("NULL pointer error\n");
1393                    return -1;
1394            }
1395    
1396            // acquire lock of user list
1397            if (user_list_rd_lock() < 0)
1398            {
1399                    log_error("user_list_rd_lock() error\n");
1400                    return -2;
1401            }
1402    
1403            left = 0;
1404            right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1405    
1406            while (left < right)
1407            {
1408                    mid = (left + right) / 2;
1409                    if (start_uid < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1410                    {
1411                            right = mid - 1;
1412                    }
1413                    else if (start_uid > p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1414                    {
1415                            left = mid + 1;
1416                    }
1417                    else // if (start_uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1418                    {
1419                            left = mid;
1420                            break;
1421                    }
1422            }
1423    
1424            for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count; i++)
1425            {
1426                    p_uid_list[i] = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left + i].uid;
1427            }
1428            *p_user_cnt = i;
1429    
1430            // release lock of user list
1431            if (user_list_rd_unlock() < 0)
1432            {
1433                    log_error("user_list_rd_unlock() error\n");
1434                    ret = -1;
1435            }
1436    
1437            return ret;
1438    }
1439    
1440    int user_stat_update(void)
1441    {
1442            return user_stat_map_update(&(p_user_list_pool->user_stat_map));
1443    }
1444    
1445    int user_article_cnt_inc(int32_t uid, int n)
1446    {
1447            return user_stat_article_cnt_inc(&(p_user_list_pool->user_stat_map), uid, n);
1448    }
1449    
1450    int get_user_article_cnt(int32_t uid)
1451    {
1452            const USER_STAT *p_stat;
1453            int ret;
1454    
1455            ret = user_stat_get(&(p_user_list_pool->user_stat_map), uid, &p_stat);
1456            if (ret < 0)
1457            {
1458                    log_error("user_stat_get(uid=%d) error: %d\n", uid, ret);
1459                    return -1;
1460            }
1461            else if (ret == 0) // user not found
1462            {
1463                    return -1;
1464            }
1465    
1466      return ret;          return p_stat->article_count;
1467  }  }


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

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