/[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.1 by sysadm, Tue Oct 21 06:24:51 2025 UTC Revision 1.24 by sysadm, Sun Nov 2 04:30:29 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                           user_list.c  -  description                                                   user_list.c  -  description
3                               -------------------                                                           -------------------
4      Copyright            : (C) 2004-2025 by Leaflet          Copyright            : (C) 2004-2025 by Leaflet
5      Email                : leaflet@leafok.com          Email                : leaflet@leafok.com
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
# Line 17  Line 17 
17  #include "common.h"  #include "common.h"
18  #include "database.h"  #include "database.h"
19  #include "log.h"  #include "log.h"
20    #include "trie_dict.h"
21  #include "user_list.h"  #include "user_list.h"
22    #include "user_stat.h"
23  #include <errno.h>  #include <errno.h>
24  #include <stdlib.h>  #include <stdlib.h>
25  #include <string.h>  #include <string.h>
26  #include <time.h>  #include <time.h>
27  #include <sys/ipc.h>  #include <sys/ipc.h>
28  #include <sys/mman.h>  #include <sys/mman.h>
29    #include <sys/param.h>
30  #include <sys/sem.h>  #include <sys/sem.h>
31  #include <sys/shm.h>  #include <sys/shm.h>
32    
33  #ifdef _SEM_SEMUN_UNDEFINED  #ifdef _SEM_SEMUN_UNDEFINED
34  union semun  union semun
35  {  {
36      int val;               /* Value for SETVAL */          int val;                           /* Value for SETVAL */
37      struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
38      unsigned short *array; /* Array for GETALL, SETALL */          unsigned short *array; /* Array for GETALL, SETALL */
39      struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
40                                (Linux-specific) */                                                            (Linux-specific) */
41  };  };
42  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
43    
# Line 43  union semun Line 46  union semun
46    
47  struct user_list_pool_t  struct user_list_pool_t
48  {  {
49      int shmid;          int shmid;
50      int semid;          int semid;
51      USER_LIST user_list[2];          USER_LIST user_list[2];
52      USER_LIST *p_current;          USER_LIST *p_current;
53      USER_LIST *p_new;          USER_LIST *p_new;
54            USER_ONLINE_LIST user_online_list[2];
55            USER_ONLINE_LIST *p_online_current;
56            USER_ONLINE_LIST *p_online_new;
57            USER_STAT_MAP user_stat_map;
58            int user_login_count;
59  };  };
60  typedef struct user_list_pool_t USER_LIST_POOL;  typedef struct user_list_pool_t USER_LIST_POOL;
61    
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 semid, int wait_sec);
90    static int user_list_try_rw_lock(int semid, int wait_sec);
91    static int user_list_rd_unlock(int semid);
92    static int user_list_rw_unlock(int semid);
93    static int user_list_rd_lock(int semid);
94    static int user_list_rw_lock(int semid);
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_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      }          {
177      mysql_free_result(rs);                  // record
178      rs = NULL;                  p_list->users[i].id = i;
179                    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                    strncpy(p_list->users[i].nickname, row[2], sizeof(p_list->users[i].nickname) - 1);
183                    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[10] == 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_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_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_list->users[i].id = i;
297      for (i = 0; i < size; i++)                  strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1);
298      {                  p_list->users[i].session_id[sizeof(p_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_list->users[i].user_info))) <= 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_list->users[i].ip, row[2], sizeof(p_list->users[i].ip) - 1);
307                    p_list->users[i].ip[sizeof(p_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_list->users[i].current_action, row[3], sizeof(p_list->users[i].current_action) - 1);
310      p_user_list_pool->user_list[1].user_count = 0;                  p_list->users[i].current_action[sizeof(p_list->users[i].current_action) - 1] = '\0';
311                    p_list->users[i].current_action_title = NULL;
312                    if (p_list->users[i].current_action[0] == '\0')
313                    {
314                            p_list->users[i].current_action_title = "";
315                    }
316                    else if (trie_dict_get(p_trie_action_dict, p_list->users[i].current_action, (int64_t *)(&(p_list->users[i].current_action_title))) < 0)
317                    {
318                            log_error("trie_dict_get(p_trie_action_dict, %s) error on session_id=%s\n",
319                                              p_list->users[i].current_action, p_list->users[i].session_id);
320                            continue;
321                    }
322    
323                    p_list->users[i].login_tm = (row[4] == NULL ? 0 : atol(row[4]));
324                    p_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_list->index_uid[j].uid = p_list->users[j].user_info.uid;
342                            p_list->index_uid[j].id = j;
343                    }
344    
345      p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);                  qsort(p_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
     p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);  
346    
347      return 0;  #ifdef _DEBUG
348                    log_error("Rebuild index of %d online users\n", user_cnt);
349    #endif
350            }
351    
352            p_list->user_count = user_cnt;
353            p_list->guest_count = guest_cnt;
354    
355    #ifdef _DEBUG
356            log_error("Loaded %d online users and %d guest users\n", p_list->user_count, p_list->guest_count);
357    #endif
358    
359    cleanup:
360            mysql_free_result(rs);
361    
362            return ret;
363    }
364    
365    int user_login_count_load(MYSQL *db)
366    {
367            MYSQL_RES *rs = NULL;
368            MYSQL_ROW row;
369            char sql[SQL_BUFFER_LEN];
370    
371            if (db == NULL)
372            {
373                    log_error("NULL pointer error\n");
374                    return -1;
375            }
376    
377            snprintf(sql, sizeof(sql),
378                             "SELECT ID FROM user_login_log ORDER BY ID DESC LIMIT 1");
379            if (mysql_query(db, sql) != 0)
380            {
381                    log_error("Query user_login_log error: %s\n", mysql_error(db));
382                    return -2;
383            }
384            if ((rs = mysql_store_result(db)) == NULL)
385            {
386                    log_error("Get user_login_log data failed\n");
387                    return -2;
388            }
389            if ((row = mysql_fetch_row(rs)))
390            {
391                    p_user_list_pool->user_login_count = atoi(row[0]);
392            }
393            mysql_free_result(rs);
394    
395            return 0;
396    }
397    
398    int user_list_pool_init(const char *filename)
399    {
400            int shmid;
401            int semid;
402            int proj_id;
403            key_t key;
404            size_t size;
405            void *p_shm;
406            union semun arg;
407            int i;
408    
409            if (p_user_list_pool != NULL || p_trie_action_dict != NULL)
410            {
411                    log_error("p_user_list_pool already initialized\n");
412                    return -1;
413            }
414    
415            p_trie_action_dict = trie_dict_create();
416            if (p_trie_action_dict == NULL)
417            {
418                    log_error("trie_dict_create() error\n");
419                    return -1;
420            }
421    
422            for (i = 0; i < user_action_map_size; i++)
423            {
424                    if (trie_dict_set(p_trie_action_dict, user_action_map[i].name, (int64_t)(user_action_map[i].title)) < 0)
425                    {
426                            log_error("trie_dict_set(p_trie_action_dict, %s) error\n", user_action_map[i].name);
427                    }
428            }
429    
430            // Allocate shared memory
431            proj_id = (int)(time(NULL) % getpid());
432            key = ftok(filename, proj_id);
433            if (key == -1)
434            {
435                    log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
436                    return -2;
437            }
438    
439            size = sizeof(USER_LIST_POOL);
440            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
441            if (shmid == -1)
442            {
443                    log_error("shmget(size = %d) error (%d)\n", size, errno);
444                    return -3;
445            }
446            p_shm = shmat(shmid, NULL, 0);
447            if (p_shm == (void *)-1)
448            {
449                    log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
450                    return -3;
451            }
452    
453            p_user_list_pool = p_shm;
454            p_user_list_pool->shmid = shmid;
455    
456            // Allocate semaphore as user list pool lock
457            size = 2; // r_sem and w_sem
458            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
459            if (semid == -1)
460            {
461                    log_error("semget(user_list_pool_sem, size = %d) error (%d)\n", size, errno);
462                    return -3;
463            }
464    
465            // Initialize sem value to 0
466            arg.val = 0;
467            for (i = 0; i < size; i++)
468            {
469                    if (semctl(semid, i, SETVAL, arg) == -1)
470                    {
471                            log_error("semctl(user_list_pool_sem, SETVAL) error (%d)\n", errno);
472                            return -3;
473                    }
474            }
475    
476            p_user_list_pool->semid = semid;
477    
478            // Set user counts to 0
479            p_user_list_pool->user_list[0].user_count = 0;
480            p_user_list_pool->user_list[1].user_count = 0;
481    
482            p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);
483            p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);
484    
485            p_user_list_pool->p_online_current = &(p_user_list_pool->user_online_list[0]);
486            p_user_list_pool->p_online_new = &(p_user_list_pool->user_online_list[1]);
487    
488            user_stat_map_init(&(p_user_list_pool->user_stat_map));
489    
490            return 0;
491  }  }
492    
493  void user_list_pool_cleanup(void)  void user_list_pool_cleanup(void)
494  {  {
495      int shmid;          int shmid;
496    
497      if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
498      {          {
499          return;                  return;
500      }          }
501    
502      shmid = p_user_list_pool->shmid;          shmid = p_user_list_pool->shmid;
503    
504      if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)          if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)
505      {          {
506          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);
507      }          }
508    
509      if (shmdt(p_user_list_pool) == -1)          if (shmdt(p_user_list_pool) == -1)
510      {          {
511          log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
512      }          }
513    
514      if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmctl(shmid, IPC_RMID, NULL) == -1)
515      {          {
516          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
517      }          }
518    
519            p_user_list_pool = NULL;
520    
521            if (p_trie_action_dict != NULL)
522            {
523                    trie_dict_destroy(p_trie_action_dict);
524    
525      p_user_list_pool = NULL;                  p_trie_action_dict = NULL;
526            }
527  }  }
528    
529  int set_user_list_pool_shm_readonly(void)  int set_user_list_pool_shm_readonly(void)
530  {  {
531      int shmid;          int shmid;
532      void *p_shm;          void *p_shm;
533    
534      if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
535      {          {
536          log_error("p_user_list_pool not initialized\n");                  log_error("p_user_list_pool not initialized\n");
537          return -1;                  return -1;
538      }          }
539    
540      shmid = p_user_list_pool->shmid;          shmid = p_user_list_pool->shmid;
541    
542      // Remap shared memory in read-only mode          // Remap shared memory in read-only mode
543      p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);          p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);
544      if (p_shm == (void *)-1)          if (p_shm == (void *)-1)
545      {          {
546          log_error("shmat(user_list_pool shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmat(user_list_pool shmid = %d) error (%d)\n", shmid, errno);
547          return -3;                  return -3;
548      }          }
549    
550      p_user_list_pool = p_shm;          p_user_list_pool = p_shm;
551    
552      return 0;          return 0;
553  }  }
554    
555  int detach_user_list_pool_shm(void)  int detach_user_list_pool_shm(void)
556  {  {
557      if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)          if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)
558      {          {
559          log_error("shmdt(user_list_pool) error (%d)\n", errno);                  log_error("shmdt(user_list_pool) error (%d)\n", errno);
560          return -1;                  return -1;
561      }          }
562    
563      p_user_list_pool = NULL;          p_user_list_pool = NULL;
564    
565      return 0;          return 0;
566  }  }
567    
568  int user_list_pool_reload(void)  int user_list_pool_reload(int online_user)
569  {  {
570      MYSQL *db = NULL;          MYSQL *db = NULL;
571      USER_LIST *p_tmp;          USER_LIST *p_tmp;
572      int ret = 0;          USER_ONLINE_LIST *p_online_tmp;
573            int ret = 0;
574      if (p_user_list_pool == NULL)  
575      {          if (p_user_list_pool == NULL)
576          log_error("p_user_list_pool not initialized\n");          {
577          return -1;                  log_error("p_user_list_pool not initialized\n");
578      }                  return -1;
579            }
580      db = db_open();  
581      if (db == NULL)          db = db_open();
582      {          if (db == NULL)
583          log_error("db_open() error: %s\n", mysql_error(db));          {
584          return -1;                  log_error("db_open() error: %s\n", mysql_error(db));
585      }                  return -1;
586            }
587      if (user_list_rw_lock() < 0)  
588      {          if (online_user)
589          log_error("user_list_rw_lock() error\n");          {
590          return -2;                  if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0)
591      }                  {
592                            log_error("user_online_list_load() error\n");
593      if (user_list_load(db, p_user_list_pool->p_new) < 0)                          ret = -2;
594      {                          goto cleanup;
595          log_error("user_list_rw_lock() error\n");                  }
596          ret = -3;  
597          goto cleanup;                  if (user_login_count_load(db) < 0)
598      }                  {
599                            log_error("user_login_count_load() error\n");
600      // Swap p_current and p_new                          ret = -2;
601      p_tmp = p_user_list_pool->p_current;                          goto cleanup;
602      p_user_list_pool->p_current = p_user_list_pool->p_new;                  }
603      p_user_list_pool->p_new = p_tmp;          }
604            else
605            {
606                    if (user_list_load(db, p_user_list_pool->p_new) < 0)
607                    {
608                            log_error("user_list_load() error\n");
609                            ret = -2;
610                            goto cleanup;
611                    }
612            }
613    
614            mysql_close(db);
615            db = NULL;
616    
617            if (user_list_rw_lock(p_user_list_pool->semid) < 0)
618            {
619                    log_error("user_list_rw_lock() error\n");
620                    ret = -3;
621                    goto cleanup;
622            }
623    
624            if (online_user)
625            {
626                    // Swap p_online_current and p_online_new
627                    p_online_tmp = p_user_list_pool->p_online_current;
628                    p_user_list_pool->p_online_current = p_user_list_pool->p_online_new;
629                    p_user_list_pool->p_online_new = p_online_tmp;
630            }
631            else
632            {
633                    // Swap p_current and p_new
634                    p_tmp = p_user_list_pool->p_current;
635                    p_user_list_pool->p_current = p_user_list_pool->p_new;
636                    p_user_list_pool->p_new = p_tmp;
637            }
638    
639            if (user_list_rw_unlock(p_user_list_pool->semid) < 0)
640            {
641                    log_error("user_list_rw_unlock() error\n");
642                    ret = -3;
643                    goto cleanup;
644            }
645    
646  cleanup:  cleanup:
647      if (user_list_rw_unlock() < 0)          mysql_close(db);
648      {  
649          log_error("user_list_rw_unlock() error\n");          return ret;
650          ret = -2;  }
651      }  
652    int user_list_try_rd_lock(int semid, int wait_sec)
653    {
654            struct sembuf sops[2];
655            struct timespec timeout;
656            int ret;
657    
658            sops[0].sem_num = 1; // w_sem
659            sops[0].sem_op = 0;      // wait until unlocked
660            sops[0].sem_flg = 0;
661    
662            sops[1].sem_num = 0;            // r_sem
663            sops[1].sem_op = 1;                     // lock
664            sops[1].sem_flg = SEM_UNDO; // undo on terminate
665    
666            timeout.tv_sec = wait_sec;
667            timeout.tv_nsec = 0;
668    
669            ret = semtimedop(semid, sops, 2, &timeout);
670            if (ret == -1 && errno != EAGAIN && errno != EINTR)
671            {
672                    log_error("semtimedop(lock read) error %d\n", errno);
673            }
674    
675            return ret;
676    }
677    
678    int user_list_try_rw_lock(int semid, int wait_sec)
679    {
680            struct sembuf sops[3];
681            struct timespec timeout;
682            int ret;
683    
684            sops[0].sem_num = 1; // w_sem
685            sops[0].sem_op = 0;      // wait until unlocked
686            sops[0].sem_flg = 0;
687    
688            sops[1].sem_num = 1;            // w_sem
689            sops[1].sem_op = 1;                     // lock
690            sops[1].sem_flg = SEM_UNDO; // undo on terminate
691    
692            sops[2].sem_num = 0; // r_sem
693            sops[2].sem_op = 0;      // wait until unlocked
694            sops[2].sem_flg = 0;
695    
696            timeout.tv_sec = wait_sec;
697            timeout.tv_nsec = 0;
698    
699            ret = semtimedop(semid, sops, 3, &timeout);
700            if (ret == -1 && errno != EAGAIN && errno != EINTR)
701            {
702                    log_error("semtimedop(lock write) error %d\n", errno);
703            }
704    
705            return ret;
706    }
707    
708      mysql_close(db);  int user_list_rd_unlock(int semid)
709    {
710            struct sembuf sops[2];
711            int ret;
712    
713            sops[0].sem_num = 0;                                     // r_sem
714            sops[0].sem_op = -1;                                     // unlock
715            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
716    
717            ret = semop(semid, sops, 1);
718            if (ret == -1 && errno != EAGAIN && errno != EINTR)
719            {
720                    log_error("semop(unlock read) error %d\n", errno);
721            }
722    
723      return ret;          return ret;
724  }  }
725    
726  int user_list_try_rd_lock(int wait_sec)  int user_list_rw_unlock(int semid)
727  {  {
728      struct sembuf sops[2];          struct sembuf sops[1];
729      struct timespec timeout;          int ret;
     int ret;  
730    
731      sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1;                                     // w_sem
732      sops[0].sem_op = 0;  // wait until unlocked          sops[0].sem_op = -1;                                     // unlock
733      sops[0].sem_flg = 0;          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
734    
735            ret = semop(semid, sops, 1);
736            if (ret == -1 && errno != EAGAIN && errno != EINTR)
737            {
738                    log_error("semop(unlock write) error %d\n", errno);
739            }
740    
741      sops[1].sem_num = 0;        // r_sem          return ret;
742      sops[1].sem_op = 1;         // lock  }
     sops[1].sem_flg = SEM_UNDO; // undo on terminate  
743    
744      timeout.tv_sec = wait_sec;  int user_list_rd_lock(int semid)
745      timeout.tv_nsec = 0;  {
746            int timer = 0;
747            int ret = -1;
748    
749      ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);          while (!SYS_server_exit)
750      if (ret == -1 && errno != EAGAIN && errno != EINTR)          {
751      {                  ret = user_list_try_rd_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);
752          log_error("semtimedop(lock read) error %d\n", errno);                  if (ret == 0) // success
753      }                  {
754                            break;
755                    }
756                    else if (errno == EAGAIN || errno == EINTR) // retry
757                    {
758                            timer++;
759                            if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
760                            {
761                                    log_error("user_list_try_rd_lock() tried %d times\n", timer);
762                            }
763                    }
764                    else // failed
765                    {
766                            log_error("user_list_try_rd_lock() failed\n");
767                            break;
768                    }
769            }
770    
771      return ret;          return ret;
772  }  }
773    
774  int user_list_try_rw_lock(int wait_sec)  int user_list_rw_lock(int semid)
775  {  {
776      struct sembuf sops[3];          int timer = 0;
777      struct timespec timeout;          int ret = -1;
     int ret;  
778    
779      sops[0].sem_num = 1; // w_sem          while (!SYS_server_exit)
780      sops[0].sem_op = 0;  // wait until unlocked          {
781      sops[0].sem_flg = 0;                  ret = user_list_try_rw_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);
782                    if (ret == 0) // success
783                    {
784                            break;
785                    }
786                    else if (errno == EAGAIN || errno == EINTR) // retry
787                    {
788                            timer++;
789                            if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
790                            {
791                                    log_error("user_list_try_rw_lock() tried %d times\n", timer);
792                            }
793                    }
794                    else // failed
795                    {
796                            log_error("user_list_try_rw_lock() failed\n");
797                            break;
798                    }
799            }
800    
801      sops[1].sem_num = 1;        // w_sem          return ret;
802      sops[1].sem_op = 1;         // lock  }
     sops[1].sem_flg = SEM_UNDO; // undo on terminate  
803    
804      sops[2].sem_num = 0; // r_sem  int query_user_list(int page_id, USER_INFO *p_users, int *p_user_count, int *p_page_count)
805      sops[2].sem_op = 0;  // wait until unlocked  {
806      sops[2].sem_flg = 0;          int ret = 0;
807    
808      timeout.tv_sec = wait_sec;          if (p_users == NULL || p_user_count == NULL || p_page_count == NULL)
809      timeout.tv_nsec = 0;          {
810                    log_error("NULL pointer error\n");
811                    return -1;
812            }
813    
814            *p_user_count = 0;
815            *p_page_count = 0;
816    
817            // acquire lock of user list
818            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
819            {
820                    log_error("user_list_rd_lock() error\n");
821                    return -2;
822            }
823    
824            if (p_user_list_pool->p_current->user_count == 0)
825            {
826                    // empty list
827                    ret = 0;
828                    goto cleanup;
829            }
830    
831            *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +
832                                            (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);
833    
834            if (page_id < 0 || page_id >= *p_page_count)
835            {
836                    log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
837                    ret = -3;
838                    goto cleanup;
839            }
840    
841            *p_user_count = MIN(BBS_user_limit_per_page,
842                                                    p_user_list_pool->p_current->user_count -
843                                                            page_id * BBS_user_limit_per_page);
844    
845            memcpy(p_users,
846                       p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,
847                       sizeof(USER_INFO) * (size_t)(*p_user_count));
848    
849      ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);  cleanup:
850      if (ret == -1 && errno != EAGAIN && errno != EINTR)          // release lock of user list
851      {          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
852          log_error("semtimedop(lock write) error %d\n", errno);          {
853      }                  log_error("user_list_rd_unlock() error\n");
854                    ret = -1;
855            }
856    
857      return ret;          return ret;
858  }  }
859    
860  int user_list_rd_unlock(void)  int query_user_online_list(int page_id, USER_ONLINE_INFO *p_online_users, int *p_user_count, int *p_page_count)
861  {  {
862      struct sembuf sops[2];          int ret = 0;
     int ret;  
863    
864      sops[0].sem_num = 0;                     // r_sem          if (p_online_users == NULL || p_user_count == NULL || p_page_count == NULL)
865      sops[0].sem_op = -1;                     // unlock          {
866      sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  log_error("NULL pointer error\n");
867                    return -1;
868            }
869    
870            *p_user_count = 0;
871            *p_page_count = 0;
872    
873            // acquire lock of user list
874            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
875            {
876                    log_error("user_list_rd_lock() error\n");
877                    return -2;
878            }
879    
880            if (p_user_list_pool->p_online_current->user_count == 0)
881            {
882                    // empty list
883                    ret = 0;
884                    goto cleanup;
885            }
886    
887            *p_page_count = p_user_list_pool->p_online_current->user_count / BBS_user_limit_per_page +
888                                            (p_user_list_pool->p_online_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);
889    
890            if (page_id < 0 || page_id >= *p_page_count)
891            {
892                    log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
893                    ret = -3;
894                    goto cleanup;
895            }
896    
897            *p_user_count = MIN(BBS_user_limit_per_page,
898                                                    p_user_list_pool->p_online_current->user_count -
899                                                            page_id * BBS_user_limit_per_page);
900    
901            memcpy(p_online_users,
902                       p_user_list_pool->p_online_current->users + page_id * BBS_user_limit_per_page,
903                       sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));
904    
905      ret = semop(p_user_list_pool->semid, sops, 1);  cleanup:
906      if (ret == -1 && errno != EAGAIN && errno != EINTR)          // release lock of user list
907      {          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
908          log_error("semop(unlock read) error %d\n", errno);          {
909      }                  log_error("user_list_rd_unlock() error\n");
910                    ret = -1;
911            }
912    
913      return ret;          return ret;
914  }  }
915    
916  int user_list_rw_unlock(void)  int get_user_list_count(int *p_user_cnt)
917  {  {
918      struct sembuf sops[1];          if (p_user_cnt == NULL)
919      int ret;          {
920                    log_error("NULL pointer error\n");
921                    return -1;
922            }
923    
924            // acquire lock of user list
925            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
926            {
927                    log_error("user_list_rd_lock() error\n");
928                    return -2;
929            }
930    
931            *p_user_cnt = p_user_list_pool->p_current->user_count;
932    
933            // release lock of user list
934            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
935            {
936                    log_error("user_list_rd_unlock() error\n");
937                    return -2;
938            }
939    
940      sops[0].sem_num = 1;                     // w_sem          return 0;
941      sops[0].sem_op = -1;                     // unlock  }
942      sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait  
943    int get_user_online_list_count(int *p_user_cnt, int *p_guest_cnt)
944    {
945            if (p_user_cnt == NULL || p_guest_cnt == NULL)
946            {
947                    log_error("NULL pointer error\n");
948                    return -1;
949            }
950    
951            // acquire lock of user list
952            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
953            {
954                    log_error("user_list_rd_lock() error\n");
955                    return -2;
956            }
957    
958            *p_user_cnt = p_user_list_pool->p_online_current->user_count;
959            *p_guest_cnt = p_user_list_pool->p_online_current->guest_count;
960    
961            // release lock of user list
962            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
963            {
964                    log_error("user_list_rd_unlock() error\n");
965                    return -2;
966            }
967    
968            return 0;
969    }
970    
971      ret = semop(p_user_list_pool->semid, sops, 1);  int get_user_login_count(int *p_login_cnt)
972      if (ret == -1 && errno != EAGAIN && errno != EINTR)  {
973      {          if (p_login_cnt == NULL)
974          log_error("semop(unlock write) error %d\n", errno);          {
975      }                  log_error("NULL pointer error\n");
976                    return -1;
977            }
978    
979            *p_login_cnt = p_user_list_pool->user_login_count;
980    
981      return ret;          return 0;
982  }  }
983    
984  int user_list_rd_lock(void)  int query_user_info(int32_t id, USER_INFO *p_user)
985  {  {
986      int timer = 0;          int ret = 0;
     int ret = -1;  
987    
988      while (!SYS_server_exit)          if (p_user == NULL)
989      {          {
990          ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME);                  log_error("NULL pointer error\n");
991          if (ret == 0) // success                  return -1;
992          {          }
993              break;  
994          }          // acquire lock of user list
995          else if (errno == EAGAIN || errno == EINTR) // retry          if (user_list_rd_lock(p_user_list_pool->semid) < 0)
996          {          {
997              timer++;                  log_error("user_list_rd_lock() error\n");
998              if (timer % USER_LIST_TRY_LOCK_TIMES == 0)                  return -2;
999              {          }
1000                  log_error("user_list_try_rd_lock() tried %d times\n", timer);  
1001              }          if (id >= 0 && id < p_user_list_pool->p_current->user_count) // Found
1002          }          {
1003          else // failed                  *p_user = p_user_list_pool->p_current->users[id];
1004          {                  ret = 1;
1005              log_error("user_list_try_rd_lock() failed\n");          }
1006              break;  
1007          }          // release lock of user list
1008      }          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
1009            {
1010                    log_error("user_list_rd_unlock() error\n");
1011                    ret = -1;
1012            }
1013    
1014      return ret;          return ret;
1015    }
1016    
1017    int query_user_info_by_uid(int32_t uid, USER_INFO *p_user)
1018    {
1019            int left;
1020            int right;
1021            int mid;
1022            int32_t id;
1023            int ret = 0;
1024    
1025            if (p_user == NULL)
1026            {
1027                    log_error("NULL pointer error\n");
1028                    return -1;
1029            }
1030    
1031            // acquire lock of user list
1032            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
1033            {
1034                    log_error("user_list_rd_lock() error\n");
1035                    return -2;
1036            }
1037    
1038            left = 0;
1039            right = p_user_list_pool->p_current->user_count - 1;
1040    
1041            while (left < right)
1042            {
1043                    mid = (left + right) / 2;
1044                    if (uid < p_user_list_pool->p_current->index_uid[mid].uid)
1045                    {
1046                            right = mid - 1;
1047                    }
1048                    else if (uid > p_user_list_pool->p_current->index_uid[mid].uid)
1049                    {
1050                            left = mid + 1;
1051                    }
1052                    else // if (uid == p_user_list_pool->p_current->index_uid[mid].uid)
1053                    {
1054                            left = mid;
1055                            break;
1056                    }
1057            }
1058    
1059            if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found
1060            {
1061                    id = p_user_list_pool->p_current->index_uid[left].id;
1062                    *p_user = p_user_list_pool->p_current->users[id];
1063                    ret = 1;
1064            }
1065    
1066            // release lock of user list
1067            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
1068            {
1069                    log_error("user_list_rd_unlock() error\n");
1070                    ret = -1;
1071            }
1072    
1073            return ret;
1074    }
1075    
1076    int query_user_online_info(int32_t id, USER_ONLINE_INFO *p_user)
1077    {
1078            int ret = 0;
1079    
1080            if (p_user == NULL)
1081            {
1082                    log_error("NULL pointer error\n");
1083                    return -1;
1084            }
1085    
1086            // acquire lock of user list
1087            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
1088            {
1089                    log_error("user_list_rd_lock() error\n");
1090                    return -2;
1091            }
1092    
1093            if (id >= 0 && id < p_user_list_pool->p_online_current->user_count) // Found
1094            {
1095                    *p_user = p_user_list_pool->p_online_current->users[id];
1096                    ret = 1;
1097            }
1098    
1099            // release lock of user list
1100            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
1101            {
1102                    log_error("user_list_rd_unlock() error\n");
1103                    ret = -1;
1104            }
1105    
1106            return ret;
1107    }
1108    
1109    int query_user_online_info_by_uid(int32_t uid, USER_ONLINE_INFO *p_users, int *p_user_cnt, int start_id)
1110    {
1111            int left;
1112            int right;
1113            int mid;
1114            int32_t id;
1115            int ret = 0;
1116            int i;
1117            int user_cnt;
1118    
1119            if (p_users == NULL || p_user_cnt == NULL)
1120            {
1121                    log_error("NULL pointer error\n");
1122                    return -1;
1123            }
1124    
1125            user_cnt = *p_user_cnt;
1126            *p_user_cnt = 0;
1127    
1128            // acquire lock of user list
1129            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
1130            {
1131                    log_error("user_list_rd_lock() error\n");
1132                    return -2;
1133            }
1134    
1135            left = start_id;
1136            right = p_user_list_pool->p_online_current->user_count - 1;
1137    
1138            while (left < right)
1139            {
1140                    mid = (left + right) / 2;
1141                    if (uid < p_user_list_pool->p_online_current->index_uid[mid].uid)
1142                    {
1143                            right = mid - 1;
1144                    }
1145                    else if (uid > p_user_list_pool->p_online_current->index_uid[mid].uid)
1146                    {
1147                            left = mid + 1;
1148                    }
1149                    else // if (uid == p_user_list_pool->p_online_current->index_uid[mid].uid)
1150                    {
1151                            left = mid;
1152                            break;
1153                    }
1154            }
1155    
1156            if (uid == p_user_list_pool->p_online_current->index_uid[left].uid)
1157            {
1158                    right = left;
1159                    left = start_id;
1160    
1161                    while (left < right)
1162                    {
1163                            mid = (left + right) / 2;
1164                            if (uid - 1 < p_user_list_pool->p_online_current->index_uid[mid].uid)
1165                            {
1166                                    right = mid;
1167                            }
1168                            else // if (uid - 1 >= p_user_list_pool->p_online_current->index_uid[mid].uid)
1169                            {
1170                                    left = mid + 1;
1171                            }
1172                    }
1173    
1174                    for (i = 0;
1175                             left < p_user_list_pool->p_online_current->user_count && i < user_cnt &&
1176                             uid == p_user_list_pool->p_online_current->index_uid[left].uid;
1177                             left++, i++)
1178                    {
1179                            id = p_user_list_pool->p_online_current->index_uid[left].id;
1180                            p_users[i] = p_user_list_pool->p_online_current->users[id];
1181                    }
1182    
1183                    if (i > 0)
1184                    {
1185                            *p_user_cnt = i;
1186                            ret = 1;
1187                    }
1188            }
1189    
1190            // release lock of user list
1191            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
1192            {
1193                    log_error("user_list_rd_unlock() error\n");
1194                    ret = -1;
1195            }
1196    
1197            return ret;
1198    }
1199    
1200    int get_user_id_list(int32_t *p_uid_list, int *p_user_cnt, int start_uid)
1201    {
1202            int left;
1203            int right;
1204            int mid;
1205            int ret = 0;
1206            int i;
1207    
1208            if (p_uid_list == NULL || p_user_cnt == NULL)
1209            {
1210                    log_error("NULL pointer error\n");
1211                    return -1;
1212            }
1213    
1214            // acquire lock of user list
1215            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
1216            {
1217                    log_error("user_list_rd_lock() error\n");
1218                    return -2;
1219            }
1220    
1221            left = 0;
1222            right = p_user_list_pool->p_current->user_count - 1;
1223    
1224            while (left < right)
1225            {
1226                    mid = (left + right) / 2;
1227                    if (start_uid < p_user_list_pool->p_current->index_uid[mid].uid)
1228                    {
1229                            right = mid - 1;
1230                    }
1231                    else if (start_uid > p_user_list_pool->p_current->index_uid[mid].uid)
1232                    {
1233                            left = mid + 1;
1234                    }
1235                    else // if (start_uid == p_user_list_pool->p_current->index_uid[mid].uid)
1236                    {
1237                            left = mid;
1238                            break;
1239                    }
1240            }
1241    
1242            for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->p_current->user_count; i++)
1243            {
1244                    p_uid_list[i] = p_user_list_pool->p_current->index_uid[left + i].uid;
1245            }
1246            *p_user_cnt = i;
1247    
1248            // release lock of user list
1249            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
1250            {
1251                    log_error("user_list_rd_unlock() error\n");
1252                    ret = -1;
1253            }
1254    
1255            return ret;
1256    }
1257    
1258    int user_stat_update(void)
1259    {
1260            return user_stat_map_update(&(p_user_list_pool->user_stat_map));
1261    }
1262    
1263    int user_article_cnt_inc(int32_t uid, int n)
1264    {
1265            return user_stat_article_cnt_inc(&(p_user_list_pool->user_stat_map), uid, n);
1266  }  }
1267    
1268  int user_list_rw_lock(void)  int get_user_article_cnt(int32_t uid)
1269  {  {
1270      int timer = 0;          const USER_STAT *p_stat;
1271      int ret = -1;          int ret;
1272    
1273      while (!SYS_server_exit)          ret = user_stat_get(&(p_user_list_pool->user_stat_map), uid, &p_stat);
1274      {          if (ret < 0)
1275          ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME);          {
1276          if (ret == 0) // success                  log_error("user_stat_get(uid=%d) error: %d\n", uid, ret);
1277          {                  return -1;
1278              break;          }
1279          }          else if (ret == 0) // user not found
1280          else if (errno == EAGAIN || errno == EINTR) // retry          {
1281          {                  return -1;
1282              timer++;          }
             if (timer % USER_LIST_TRY_LOCK_TIMES == 0)  
             {  
                 log_error("user_list_try_rw_lock() tried %d times\n", timer);  
             }  
         }  
         else // failed  
         {  
             log_error("user_list_try_rw_lock() failed\n");  
             break;  
         }  
     }  
1283    
1284      return ret;          return p_stat->article_count;
1285  }  }


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

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