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


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

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