/[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.3 by sysadm, Tue Oct 21 12:10:20 2025 UTC Revision 1.20 by sysadm, Thu Oct 23 09:54:54 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>
# Line 31  Line 33 
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 44  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  };  };
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);
89    static int user_list_try_rw_lock(int semid, int wait_sec);
90    static int user_list_rd_unlock(int semid);
91    static int user_list_rw_unlock(int semid);
92    static int user_list_rd_lock(int semid);
93    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_list);
97    
98    static int user_info_index_uid_comp(const void *ptr1, const void *ptr2)
99    {
100            const USER_INFO_INDEX_UID *p1 = ptr1;
101            const USER_INFO_INDEX_UID *p2 = ptr2;
102    
103            if (p1->uid < p2->uid)
104            {
105                    return -1;
106            }
107            else if (p1->uid > p2->uid)
108            {
109                    return 1;
110            }
111            else if (p1->id < p2->id)
112            {
113                    return -1;
114            }
115            else if (p1->id > p2->id)
116            {
117                    return 1;
118            }
119            return 0;
120    }
121    
122  int user_list_load(MYSQL *db, USER_LIST *p_list)  int user_list_load(MYSQL *db, USER_LIST *p_list)
123  {  {
124      MYSQL_RES *rs = NULL;          MYSQL_RES *rs = NULL;
125      MYSQL_ROW row;          MYSQL_ROW row;
126      char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
127      int ret = 0;          int ret = 0;
128      int i;          int i;
129            int j;
130      if (db == NULL || p_list == NULL)          int32_t last_uid;
131      {          size_t intro_buf_offset;
132          log_error("NULL pointer error\n");          size_t intro_len;
133          return -1;  
134      }          if (db == NULL || p_list == NULL)
135            {
136      snprintf(sql, sizeof(sql),                  log_error("NULL pointer error\n");
137               "SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, "                  return -1;
138               "UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday) "          }
139               "FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "  
140               "INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID "          if (p_list->user_count > 0)
141               "WHERE enable ORDER BY UID");          {
142                    last_uid = p_list->users[p_list->user_count - 1].uid;
143      if (mysql_query(db, sql) != 0)          }
144      {          else
145          log_error("Query user info error: %s\n", mysql_error(db));          {
146          ret = -1;                  last_uid = -1;
147          goto cleanup;          }
148      }  
149            snprintf(sql, sizeof(sql),
150      if ((rs = mysql_use_result(db)) == NULL)                           "SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, visit_count, "
151      {                           "UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday), `introduction` "
152          log_error("Get user info data failed\n");                           "FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
153          ret = -1;                           "INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID "
154          goto cleanup;                           "WHERE enable ORDER BY username");
155      }  
156            if (mysql_query(db, sql) != 0)
157      i = 0;          {
158      while ((row = mysql_fetch_row(rs)))                  log_error("Query user info error: %s\n", mysql_error(db));
159      {                  ret = -1;
160          p_list->users[i].uid = atoi(row[0]);                  goto cleanup;
161          strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1);          }
162          p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0';  
163          strncpy(p_list->users[i].nickname, row[2], sizeof(p_list->users[i].nickname) - 1);          if ((rs = mysql_use_result(db)) == NULL)
164          p_list->users[i].nickname[sizeof(p_list->users[i].nickname) - 1] = '\0';          {
165          p_list->users[i].gender = row[3][0];                  log_error("Get user info data failed\n");
166          p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4]));                  ret = -1;
167          p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5]));                  goto cleanup;
168          p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6]));          }
169          p_list->users[i].signup_dt = (row[7] == NULL ? 0 : atol(row[7]));  
170          p_list->users[i].last_login_dt = (row[8] == NULL ? 0 : atol(row[8]));          intro_buf_offset = 0;
171          p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9]));          i = 0;
172            while ((row = mysql_fetch_row(rs)))
173          i++;          {
174          if (i >= BBS_max_user_count)                  // record
175          {                  p_list->users[i].id = i;
176              log_error("Too many users, exceed limit %d\n", BBS_max_user_count);                  p_list->users[i].uid = atoi(row[0]);
177              break;                  strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1);
178          }                  p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0';
179      }                  strncpy(p_list->users[i].nickname, row[2], sizeof(p_list->users[i].nickname) - 1);
180      mysql_free_result(rs);                  p_list->users[i].nickname[sizeof(p_list->users[i].nickname) - 1] = '\0';
181      rs = NULL;                  p_list->users[i].gender = row[3][0];
182                    p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4]));
183                    p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5]));
184                    p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6]));
185                    p_list->users[i].visit_count = (row[7] == NULL ? 0 : atoi(row[7]));
186                    p_list->users[i].signup_dt = (row[8] == NULL ? 0 : atol(row[8]));
187                    p_list->users[i].last_login_dt = (row[9] == NULL ? 0 : atol(row[9]));
188                    p_list->users[i].birthday = (row[10] == NULL ? 0 : atol(row[10]));
189                    intro_len = strlen((row[11] == NULL ? "" : row[11]));
190                    if (intro_len >= sizeof(p_list->user_intro_buf) - 1 - intro_buf_offset)
191                    {
192                            log_error("OOM for user introduction: len=%d, i=%d\n", intro_len, i);
193                            break;
194                    }
195                    memcpy(p_list->user_intro_buf + intro_buf_offset,
196                               (row[11] == NULL ? "" : row[11]),
197                               intro_len + 1);
198                    p_list->users[i].intro = p_list->user_intro_buf + intro_buf_offset;
199                    intro_buf_offset += (intro_len + 1);
200    
201                    i++;
202                    if (i >= BBS_max_user_count)
203                    {
204                            log_error("Too many users, exceed limit %d\n", BBS_max_user_count);
205                            break;
206                    }
207            }
208            mysql_free_result(rs);
209            rs = NULL;
210    
211            if (i != p_list->user_count || p_list->users[i - 1].uid != last_uid) // Count of users changed
212            {
213                    // Rebuild index
214                    for (j = 0; j < i; j++)
215                    {
216                            p_list->index_uid[j].uid = p_list->users[j].uid;
217                            p_list->index_uid[j].id = j;
218                    }
219    
220      p_list->user_count = i;                  qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
221    
222  #ifdef _DEBUG  #ifdef _DEBUG
223      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);
224    #endif
225            }
226    
227            p_list->user_count = i;
228    
229    #ifdef _DEBUG
230            log_error("Loaded %d users\n", p_list->user_count);
231  #endif  #endif
232    
233  cleanup:  cleanup:
234      mysql_free_result(rs);          mysql_free_result(rs);
235    
236      return ret;          return ret;
237    }
238    
239    int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list)
240    {
241            MYSQL_RES *rs = NULL;
242            MYSQL_ROW row;
243            char sql[SQL_BUFFER_LEN];
244            int ret = 0;
245            int i;
246            int j;
247    
248            if (db == NULL || p_list == NULL)
249            {
250                    log_error("NULL pointer error\n");
251                    return -1;
252            }
253    
254            snprintf(sql, sizeof(sql),
255                             "SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), "
256                             "UNIX_TIMESTAMP(last_tm) FROM user_online "
257                             "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) AND UID <> 0 "
258                             "ORDER BY last_tm DESC",
259                             BBS_user_off_line);
260    
261            if (mysql_query(db, sql) != 0)
262            {
263                    log_error("Query user online error: %s\n", mysql_error(db));
264                    ret = -1;
265                    goto cleanup;
266            }
267    
268            if ((rs = mysql_use_result(db)) == NULL)
269            {
270                    log_error("Get user online data failed\n");
271                    ret = -1;
272                    goto cleanup;
273            }
274    
275            i = 0;
276            while ((row = mysql_fetch_row(rs)))
277            {
278                    p_list->users[i].id = i;
279                    strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1);
280                    p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0';
281    
282                    if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_list->users[i].user_info))) <= 0)
283                    {
284                            log_error("query_user_info_by_uid(%d) error\n", atoi(row[1]));
285                            continue;
286                    }
287    
288                    strncpy(p_list->users[i].ip, row[2], sizeof(p_list->users[i].ip) - 1);
289                    p_list->users[i].ip[sizeof(p_list->users[i].ip) - 1] = '\0';
290    
291                    strncpy(p_list->users[i].current_action, row[3], sizeof(p_list->users[i].current_action) - 1);
292                    p_list->users[i].current_action[sizeof(p_list->users[i].current_action) - 1] = '\0';
293                    p_list->users[i].current_action_title = NULL;
294                    if (p_list->users[i].current_action[0] == '\0')
295                    {
296                            p_list->users[i].current_action_title = "";
297                    }
298                    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)
299                    {
300                            log_error("trie_dict_get(p_trie_action_dict, %s) error on session_id=%s\n",
301                                              p_list->users[i].current_action, p_list->users[i].session_id);
302                            continue;
303                    }
304    
305                    p_list->users[i].login_tm = (row[4] == NULL ? 0 : atol(row[4]));
306                    p_list->users[i].last_tm = (row[5] == NULL ? 0 : atol(row[5]));
307    
308                    i++;
309                    if (i >= BBS_max_user_online_count)
310                    {
311                            log_error("Too many online users, exceed limit %d\n", BBS_max_user_online_count);
312                            break;
313                    }
314            }
315            mysql_free_result(rs);
316            rs = NULL;
317    
318            if (i > 0)
319            {
320                    // Rebuild index
321                    for (j = 0; j < i; j++)
322                    {
323                            p_list->index_uid[j].uid = p_list->users[j].user_info.uid;
324                            p_list->index_uid[j].id = j;
325                    }
326    
327                    qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
328    
329    #ifdef _DEBUG
330                    log_error("Rebuild index of %d online users\n", i);
331    #endif
332            }
333    
334            p_list->user_count = i;
335    
336    #ifdef _DEBUG
337            log_error("Loaded %d online users\n", p_list->user_count);
338    #endif
339    
340    cleanup:
341            mysql_free_result(rs);
342    
343            return ret;
344  }  }
345    
346  int user_list_pool_init(void)  int user_list_pool_init(void)
347  {  {
348      int shmid;          int shmid;
349      int semid;          int semid;
350      int proj_id;          int proj_id;
351      key_t key;          key_t key;
352      size_t size;          size_t size;
353      void *p_shm;          void *p_shm;
354      union semun arg;          union semun arg;
355      int i;          int i;
356    
357      if (p_user_list_pool != NULL)          if (p_user_list_pool != NULL || p_trie_action_dict != NULL)
358      {          {
359          log_error("p_user_list_pool already initialized\n");                  log_error("p_user_list_pool already initialized\n");
360          return -1;                  return -1;
361      }          }
362    
363      // Allocate shared memory          p_trie_action_dict = trie_dict_create();
364      proj_id = (int)(time(NULL) % getpid());          if (p_trie_action_dict == NULL)
365      key = ftok(VAR_USER_LIST_SHM, proj_id);          {
366      if (key == -1)                  log_error("trie_dict_create() error\n");
367      {                  return -1;
368          log_error("ftok(%s %d) error (%d)\n", VAR_USER_LIST_SHM, proj_id, errno);          }
369          return -2;  
370      }          for (i = 0; i < user_action_map_size; i++)
371            {
372      size = sizeof(USER_LIST_POOL);                  if (trie_dict_set(p_trie_action_dict, user_action_map[i].name, (int64_t)(user_action_map[i].title)) < 0)
373      shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  {
374      if (shmid == -1)                          log_error("trie_dict_set(p_trie_action_dict, %s) error\n", user_action_map[i].name);
375      {                  }
376          log_error("shmget(size = %d) error (%d)\n", size, errno);          }
377          return -3;  
378      }          // Allocate shared memory
379      p_shm = shmat(shmid, NULL, 0);          proj_id = (int)(time(NULL) % getpid());
380      if (p_shm == (void *)-1)          key = ftok(VAR_USER_LIST_SHM, proj_id);
381      {          if (key == -1)
382          log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);          {
383          return -3;                  log_error("ftok(%s %d) error (%d)\n", VAR_USER_LIST_SHM, proj_id, errno);
384      }                  return -2;
385            }
386      p_user_list_pool = p_shm;  
387      p_user_list_pool->shmid = shmid;          size = sizeof(USER_LIST_POOL);
388            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
389      // Allocate semaphore as user list pool lock          if (shmid == -1)
390      size = 2; // r_sem and w_sem          {
391      semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);                  log_error("shmget(size = %d) error (%d)\n", size, errno);
392      if (semid == -1)                  return -3;
393      {          }
394          log_error("semget(user_list_pool_sem, size = %d) error (%d)\n", size, errno);          p_shm = shmat(shmid, NULL, 0);
395          return -3;          if (p_shm == (void *)-1)
396      }          {
397                    log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
398      // Initialize sem value to 0                  return -3;
399      arg.val = 0;          }
400      for (i = 0; i < size; i++)  
401      {          p_user_list_pool = p_shm;
402          if (semctl(semid, i, SETVAL, arg) == -1)          p_user_list_pool->shmid = shmid;
403          {  
404              log_error("semctl(user_list_pool_sem, SETVAL) error (%d)\n", errno);          // Allocate semaphore as user list pool lock
405              return -3;          size = 2; // r_sem and w_sem
406          }          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
407      }          if (semid == -1)
408            {
409      p_user_list_pool->semid = semid;                  log_error("semget(user_list_pool_sem, size = %d) error (%d)\n", size, errno);
410                    return -3;
411      // Set user counts to 0          }
412      p_user_list_pool->user_list[0].user_count = 0;  
413      p_user_list_pool->user_list[1].user_count = 0;          // Initialize sem value to 0
414            arg.val = 0;
415            for (i = 0; i < size; i++)
416            {
417                    if (semctl(semid, i, SETVAL, arg) == -1)
418                    {
419                            log_error("semctl(user_list_pool_sem, SETVAL) error (%d)\n", errno);
420                            return -3;
421                    }
422            }
423    
424            p_user_list_pool->semid = semid;
425    
426            // Set user counts to 0
427            p_user_list_pool->user_list[0].user_count = 0;
428            p_user_list_pool->user_list[1].user_count = 0;
429    
430            p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);
431            p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);
432    
433            p_user_list_pool->p_online_current = &(p_user_list_pool->user_online_list[0]);
434            p_user_list_pool->p_online_new = &(p_user_list_pool->user_online_list[1]);
435    
436      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]);  
437    
438      return 0;          return 0;
439  }  }
440    
441  void user_list_pool_cleanup(void)  void user_list_pool_cleanup(void)
442  {  {
443      int shmid;          int shmid;
444    
445      if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
446      {          {
447          return;                  return;
448      }          }
449    
450      shmid = p_user_list_pool->shmid;          shmid = p_user_list_pool->shmid;
451    
452      if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)          if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)
453      {          {
454          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);
455      }          }
456    
457      if (shmdt(p_user_list_pool) == -1)          if (shmdt(p_user_list_pool) == -1)
458      {          {
459          log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
460      }          }
461    
462      if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmctl(shmid, IPC_RMID, NULL) == -1)
463      {          {
464          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
465      }          }
466    
467            p_user_list_pool = NULL;
468    
469            if (p_trie_action_dict != NULL)
470            {
471                    trie_dict_destroy(p_trie_action_dict);
472    
473      p_user_list_pool = NULL;                  p_trie_action_dict = NULL;
474            }
475  }  }
476    
477  int set_user_list_pool_shm_readonly(void)  int set_user_list_pool_shm_readonly(void)
478  {  {
479      int shmid;          int shmid;
480      void *p_shm;          void *p_shm;
481    
482      if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
483      {          {
484          log_error("p_user_list_pool not initialized\n");                  log_error("p_user_list_pool not initialized\n");
485          return -1;                  return -1;
486      }          }
487    
488      shmid = p_user_list_pool->shmid;          shmid = p_user_list_pool->shmid;
489    
490      // Remap shared memory in read-only mode          // Remap shared memory in read-only mode
491      p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);          p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);
492      if (p_shm == (void *)-1)          if (p_shm == (void *)-1)
493      {          {
494          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);
495          return -3;                  return -3;
496      }          }
497    
498      p_user_list_pool = p_shm;          p_user_list_pool = p_shm;
499    
500      return 0;          return 0;
501  }  }
502    
503  int detach_user_list_pool_shm(void)  int detach_user_list_pool_shm(void)
504  {  {
505      if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)          if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)
506      {          {
507          log_error("shmdt(user_list_pool) error (%d)\n", errno);                  log_error("shmdt(user_list_pool) error (%d)\n", errno);
508          return -1;                  return -1;
509      }          }
510    
511      p_user_list_pool = NULL;          p_user_list_pool = NULL;
512    
513      return 0;          return 0;
514  }  }
515    
516  int user_list_pool_reload(void)  int user_list_pool_reload(int online_user)
517  {  {
518      MYSQL *db = NULL;          MYSQL *db = NULL;
519      USER_LIST *p_tmp;          USER_LIST *p_tmp;
520            USER_ONLINE_LIST *p_online_tmp;
521            int ret = 0;
522    
523            if (p_user_list_pool == NULL)
524            {
525                    log_error("p_user_list_pool not initialized\n");
526                    return -1;
527            }
528    
529            db = db_open();
530            if (db == NULL)
531            {
532                    log_error("db_open() error: %s\n", mysql_error(db));
533                    return -1;
534            }
535    
536            if (online_user)
537            {
538                    if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0)
539                    {
540                            log_error("user_online_list_load() error\n");
541                            ret = -2;
542                            goto cleanup;
543                    }
544            }
545            else
546            {
547                    if (user_list_load(db, p_user_list_pool->p_new) < 0)
548                    {
549                            log_error("user_list_load() error\n");
550                            ret = -2;
551                            goto cleanup;
552                    }
553            }
554    
555            mysql_close(db);
556            db = NULL;
557    
558            if (user_list_rw_lock(p_user_list_pool->semid) < 0)
559            {
560                    log_error("user_list_rw_lock() error\n");
561                    ret = -3;
562                    goto cleanup;
563            }
564    
565            if (online_user)
566            {
567                    // Swap p_online_current and p_online_new
568                    p_online_tmp = p_user_list_pool->p_online_current;
569                    p_user_list_pool->p_online_current = p_user_list_pool->p_online_new;
570                    p_user_list_pool->p_online_new = p_online_tmp;
571            }
572            else
573            {
574                    // Swap p_current and p_new
575                    p_tmp = p_user_list_pool->p_current;
576                    p_user_list_pool->p_current = p_user_list_pool->p_new;
577                    p_user_list_pool->p_new = p_tmp;
578            }
579    
580            if (user_list_rw_unlock(p_user_list_pool->semid) < 0)
581            {
582                    log_error("user_list_rw_unlock() error\n");
583                    ret = -3;
584                    goto cleanup;
585            }
586    
587    cleanup:
588            mysql_close(db);
589    
590      if (p_user_list_pool == NULL)          return ret;
591      {  }
         log_error("p_user_list_pool not initialized\n");  
         return -1;  
     }  
592    
593      db = db_open();  int user_list_try_rd_lock(int semid, int wait_sec)
594      if (db == NULL)  {
595      {          struct sembuf sops[2];
596          log_error("db_open() error: %s\n", mysql_error(db));          struct timespec timeout;
597          return -1;          int ret;
598      }  
599            sops[0].sem_num = 1; // w_sem
600            sops[0].sem_op = 0;      // wait until unlocked
601            sops[0].sem_flg = 0;
602    
603            sops[1].sem_num = 0;            // r_sem
604            sops[1].sem_op = 1;                     // lock
605            sops[1].sem_flg = SEM_UNDO; // undo on terminate
606    
607            timeout.tv_sec = wait_sec;
608            timeout.tv_nsec = 0;
609    
610            ret = semtimedop(semid, sops, 2, &timeout);
611            if (ret == -1 && errno != EAGAIN && errno != EINTR)
612            {
613                    log_error("semtimedop(lock read) error %d\n", errno);
614            }
615    
616      if (user_list_load(db, p_user_list_pool->p_new) < 0)          return ret;
617      {  }
         log_error("user_list_rw_lock() error\n");  
         return -2;  
     }  
618    
619      mysql_close(db);  int user_list_try_rw_lock(int semid, int wait_sec)
620    {
621            struct sembuf sops[3];
622            struct timespec timeout;
623            int ret;
624    
625            sops[0].sem_num = 1; // w_sem
626            sops[0].sem_op = 0;      // wait until unlocked
627            sops[0].sem_flg = 0;
628    
629            sops[1].sem_num = 1;            // w_sem
630            sops[1].sem_op = 1;                     // lock
631            sops[1].sem_flg = SEM_UNDO; // undo on terminate
632    
633            sops[2].sem_num = 0; // r_sem
634            sops[2].sem_op = 0;      // wait until unlocked
635            sops[2].sem_flg = 0;
636    
637            timeout.tv_sec = wait_sec;
638            timeout.tv_nsec = 0;
639    
640            ret = semtimedop(semid, sops, 3, &timeout);
641            if (ret == -1 && errno != EAGAIN && errno != EINTR)
642            {
643                    log_error("semtimedop(lock write) error %d\n", errno);
644            }
645    
646      if (user_list_rw_lock() < 0)          return ret;
647      {  }
         log_error("user_list_rw_lock() error\n");  
         return -3;  
     }  
648    
649      // Swap p_current and p_new  int user_list_rd_unlock(int semid)
650      p_tmp = p_user_list_pool->p_current;  {
651      p_user_list_pool->p_current = p_user_list_pool->p_new;          struct sembuf sops[2];
652      p_user_list_pool->p_new = p_tmp;          int ret;
653    
654      if (user_list_rw_unlock() < 0)          sops[0].sem_num = 0;                                     // r_sem
655      {          sops[0].sem_op = -1;                                     // unlock
656          log_error("user_list_rw_unlock() error\n");          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
657          return -3;  
658      }          ret = semop(semid, sops, 1);
659            if (ret == -1 && errno != EAGAIN && errno != EINTR)
660            {
661                    log_error("semop(unlock read) error %d\n", errno);
662            }
663    
664      return 0;          return ret;
665  }  }
666    
667  int user_list_try_rd_lock(int wait_sec)  int user_list_rw_unlock(int semid)
668  {  {
669      struct sembuf sops[2];          struct sembuf sops[1];
670      struct timespec timeout;          int ret;
     int ret;  
671    
672      sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1;                                     // w_sem
673      sops[0].sem_op = 0;  // wait until unlocked          sops[0].sem_op = -1;                                     // unlock
674      sops[0].sem_flg = 0;          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
675    
676            ret = semop(semid, sops, 1);
677            if (ret == -1 && errno != EAGAIN && errno != EINTR)
678            {
679                    log_error("semop(unlock write) error %d\n", errno);
680            }
681    
682      sops[1].sem_num = 0;        // r_sem          return ret;
683      sops[1].sem_op = 1;         // lock  }
     sops[1].sem_flg = SEM_UNDO; // undo on terminate  
684    
685      timeout.tv_sec = wait_sec;  int user_list_rd_lock(int semid)
686      timeout.tv_nsec = 0;  {
687            int timer = 0;
688            int ret = -1;
689    
690      ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);          while (!SYS_server_exit)
691      if (ret == -1 && errno != EAGAIN && errno != EINTR)          {
692      {                  ret = user_list_try_rd_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);
693          log_error("semtimedop(lock read) error %d\n", errno);                  if (ret == 0) // success
694      }                  {
695                            break;
696                    }
697                    else if (errno == EAGAIN || errno == EINTR) // retry
698                    {
699                            timer++;
700                            if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
701                            {
702                                    log_error("user_list_try_rd_lock() tried %d times\n", timer);
703                            }
704                    }
705                    else // failed
706                    {
707                            log_error("user_list_try_rd_lock() failed\n");
708                            break;
709                    }
710            }
711    
712      return ret;          return ret;
713  }  }
714    
715  int user_list_try_rw_lock(int wait_sec)  int user_list_rw_lock(int semid)
716  {  {
717      struct sembuf sops[3];          int timer = 0;
718      struct timespec timeout;          int ret = -1;
     int ret;  
719    
720      sops[0].sem_num = 1; // w_sem          while (!SYS_server_exit)
721      sops[0].sem_op = 0;  // wait until unlocked          {
722      sops[0].sem_flg = 0;                  ret = user_list_try_rw_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);
723                    if (ret == 0) // success
724                    {
725                            break;
726                    }
727                    else if (errno == EAGAIN || errno == EINTR) // retry
728                    {
729                            timer++;
730                            if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
731                            {
732                                    log_error("user_list_try_rw_lock() tried %d times\n", timer);
733                            }
734                    }
735                    else // failed
736                    {
737                            log_error("user_list_try_rw_lock() failed\n");
738                            break;
739                    }
740            }
741    
742      sops[1].sem_num = 1;        // w_sem          return ret;
743      sops[1].sem_op = 1;         // lock  }
     sops[1].sem_flg = SEM_UNDO; // undo on terminate  
744    
745      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)
746      sops[2].sem_op = 0;  // wait until unlocked  {
747      sops[2].sem_flg = 0;          int ret = 0;
748    
749      timeout.tv_sec = wait_sec;          if (p_users == NULL || p_user_count == NULL || p_page_count == NULL)
750      timeout.tv_nsec = 0;          {
751                    log_error("NULL pointer error\n");
752                    return -1;
753            }
754    
755            *p_user_count = 0;
756            *p_page_count = 0;
757    
758            // acquire lock of user list
759            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
760            {
761                    log_error("user_list_rd_lock() error\n");
762                    return -2;
763            }
764    
765            if (p_user_list_pool->p_current->user_count == 0)
766            {
767                    // empty list
768                    ret = 0;
769                    goto cleanup;
770            }
771    
772            *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +
773                                            (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);
774    
775            if (page_id < 0 || page_id >= *p_page_count)
776            {
777                    log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
778                    ret = -3;
779                    goto cleanup;
780            }
781    
782            *p_user_count = MIN(BBS_user_limit_per_page,
783                                                    p_user_list_pool->p_current->user_count -
784                                                            page_id * BBS_user_limit_per_page);
785    
786            memcpy(p_users,
787                       p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,
788                       sizeof(USER_INFO) * (size_t)(*p_user_count));
789    
790      ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);  cleanup:
791      if (ret == -1 && errno != EAGAIN && errno != EINTR)          // release lock of user list
792      {          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
793          log_error("semtimedop(lock write) error %d\n", errno);          {
794      }                  log_error("user_list_rd_unlock() error\n");
795                    ret = -1;
796            }
797    
798      return ret;          return ret;
799  }  }
800    
801  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)
802  {  {
803      struct sembuf sops[2];          int ret = 0;
     int ret;  
804    
805      sops[0].sem_num = 0;                     // r_sem          if (p_online_users == NULL || p_user_count == NULL || p_page_count == NULL)
806      sops[0].sem_op = -1;                     // unlock          {
807      sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  log_error("NULL pointer error\n");
808                    return -1;
809            }
810    
811            *p_user_count = 0;
812            *p_page_count = 0;
813    
814            // acquire lock of user list
815            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
816            {
817                    log_error("user_list_rd_lock() error\n");
818                    return -2;
819            }
820    
821            if (p_user_list_pool->p_online_current->user_count == 0)
822            {
823                    // empty list
824                    ret = 0;
825                    goto cleanup;
826            }
827    
828            *p_page_count = p_user_list_pool->p_online_current->user_count / BBS_user_limit_per_page +
829                                            (p_user_list_pool->p_online_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);
830    
831            if (page_id < 0 || page_id >= *p_page_count)
832            {
833                    log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
834                    ret = -3;
835                    goto cleanup;
836            }
837    
838            *p_user_count = MIN(BBS_user_limit_per_page,
839                                                    p_user_list_pool->p_online_current->user_count -
840                                                            page_id * BBS_user_limit_per_page);
841    
842            memcpy(p_online_users,
843                       p_user_list_pool->p_online_current->users + page_id * BBS_user_limit_per_page,
844                       sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));
845    
846      ret = semop(p_user_list_pool->semid, sops, 1);  cleanup:
847      if (ret == -1 && errno != EAGAIN && errno != EINTR)          // release lock of user list
848      {          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
849          log_error("semop(unlock read) error %d\n", errno);          {
850      }                  log_error("user_list_rd_unlock() error\n");
851                    ret = -1;
852            }
853    
854      return ret;          return ret;
855  }  }
856    
857  int user_list_rw_unlock(void)  int query_user_info(int32_t id, USER_INFO *p_user)
858  {  {
859      struct sembuf sops[1];          int ret = 0;
     int ret;  
860    
861      sops[0].sem_num = 1;                     // w_sem          if (p_user == NULL)
862      sops[0].sem_op = -1;                     // unlock          {
863      sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  log_error("NULL pointer error\n");
864                    return -1;
865            }
866    
867            // acquire lock of user list
868            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
869            {
870                    log_error("user_list_rd_lock() error\n");
871                    return -2;
872            }
873    
874            if (id >= 0 && id < p_user_list_pool->p_current->user_count) // Found
875            {
876                    *p_user = p_user_list_pool->p_current->users[id];
877                    ret = 1;
878            }
879    
880            // release lock of user list
881            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
882            {
883                    log_error("user_list_rd_unlock() error\n");
884                    ret = -1;
885            }
886    
887      ret = semop(p_user_list_pool->semid, sops, 1);          return ret;
888      if (ret == -1 && errno != EAGAIN && errno != EINTR)  }
889      {  
890          log_error("semop(unlock write) error %d\n", errno);  int query_user_info_by_uid(int32_t uid, USER_INFO *p_user)
891      }  {
892            int left;
893            int right;
894            int mid;
895            int32_t id;
896            int ret = 0;
897    
898            if (p_user == NULL)
899            {
900                    log_error("NULL pointer error\n");
901                    return -1;
902            }
903    
904            // acquire lock of user list
905            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
906            {
907                    log_error("user_list_rd_lock() error\n");
908                    return -2;
909            }
910    
911            left = 0;
912            right = p_user_list_pool->p_current->user_count - 1;
913    
914            while (left < right)
915            {
916                    mid = (left + right) / 2;
917                    if (uid < p_user_list_pool->p_current->index_uid[mid].uid)
918                    {
919                            right = mid;
920                    }
921                    else if (uid > p_user_list_pool->p_current->index_uid[mid].uid)
922                    {
923                            left = mid + 1;
924                    }
925                    else // if (uid == p_user_list_pool->p_current->index_uid[mid].uid)
926                    {
927                            left = mid;
928                            break;
929                    }
930            }
931    
932            if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found
933            {
934                    id = p_user_list_pool->p_current->index_uid[left].id;
935                    *p_user = p_user_list_pool->p_current->users[id];
936                    ret = 1;
937            }
938    
939            // release lock of user list
940            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
941            {
942                    log_error("user_list_rd_unlock() error\n");
943                    ret = -1;
944            }
945    
946      return ret;          return ret;
947  }  }
948    
949  int user_list_rd_lock(void)  int query_user_online_info(int32_t id, USER_ONLINE_INFO *p_user)
950  {  {
951      int timer = 0;          int ret = 0;
     int ret = -1;  
952    
953      while (!SYS_server_exit)          if (p_user == NULL)
954      {          {
955          ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME);                  log_error("NULL pointer error\n");
956          if (ret == 0) // success                  return -1;
957          {          }
958              break;  
959          }          // acquire lock of user list
960          else if (errno == EAGAIN || errno == EINTR) // retry          if (user_list_rd_lock(p_user_list_pool->semid) < 0)
961          {          {
962              timer++;                  log_error("user_list_rd_lock() error\n");
963              if (timer % USER_LIST_TRY_LOCK_TIMES == 0)                  return -2;
964              {          }
965                  log_error("user_list_try_rd_lock() tried %d times\n", timer);  
966              }          if (id >= 0 && id < p_user_list_pool->p_online_current->user_count) // Found
967          }          {
968          else // failed                  *p_user = p_user_list_pool->p_online_current->users[id];
969          {                  ret = 1;
970              log_error("user_list_try_rd_lock() failed\n");          }
971              break;  
972          }          // release lock of user list
973      }          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
974            {
975                    log_error("user_list_rd_unlock() error\n");
976                    ret = -1;
977            }
978    
979      return ret;          return ret;
980  }  }
981    
982  int user_list_rw_lock(void)  int query_user_online_info_by_uid(int32_t uid, USER_ONLINE_INFO *p_users, int *p_user_cnt, int start_id)
983  {  {
984      int timer = 0;          int left;
985      int ret = -1;          int right;
986            int mid;
987            int32_t id;
988            int ret = 0;
989            int i;
990            int user_cnt;
991    
992            if (p_users == NULL || p_user_cnt == NULL)
993            {
994                    log_error("NULL pointer error\n");
995                    return -1;
996            }
997    
998            user_cnt = *p_user_cnt;
999            *p_user_cnt = 0;
1000    
1001            // acquire lock of user list
1002            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
1003            {
1004                    log_error("user_list_rd_lock() error\n");
1005                    return -2;
1006            }
1007    
1008            left = start_id;
1009            right = p_user_list_pool->p_online_current->user_count - 1;
1010    
1011            while (left < right)
1012            {
1013                    mid = (left + right) / 2;
1014                    if (uid < p_user_list_pool->p_online_current->index_uid[mid].uid)
1015                    {
1016                            right = mid;
1017                    }
1018                    else if (uid > p_user_list_pool->p_online_current->index_uid[mid].uid)
1019                    {
1020                            left = mid + 1;
1021                    }
1022                    else // if (uid == p_user_list_pool->p_online_current->index_uid[mid].uid)
1023                    {
1024                            left = mid;
1025                            break;
1026                    }
1027            }
1028    
1029            if (uid == p_user_list_pool->p_online_current->index_uid[left].uid)
1030            {
1031                    right = left;
1032                    left = start_id;
1033    
1034                    while (left < right)
1035                    {
1036                            mid = (left + right) / 2;
1037                            if (uid - 1 < p_user_list_pool->p_online_current->index_uid[mid].uid)
1038                            {
1039                                    right = mid;
1040                            }
1041                            else // if (uid - 1 >= p_user_list_pool->p_online_current->index_uid[mid].uid)
1042                            {
1043                                    left = mid + 1;
1044                            }
1045                    }
1046    
1047                    for (i = 0;
1048                             left < p_user_list_pool->p_online_current->user_count && i < user_cnt &&
1049                             uid == p_user_list_pool->p_online_current->index_uid[left].uid;
1050                             left++, i++)
1051                    {
1052                            id = p_user_list_pool->p_online_current->index_uid[left].id;
1053                            p_users[i] = p_user_list_pool->p_online_current->users[id];
1054                    }
1055    
1056                    if (i > 0)
1057                    {
1058                            *p_user_cnt = i;
1059                            ret = 1;
1060                    }
1061            }
1062    
1063            // release lock of user list
1064            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
1065            {
1066                    log_error("user_list_rd_unlock() error\n");
1067                    ret = -1;
1068            }
1069    
1070      while (!SYS_server_exit)          return ret;
1071      {  }
         ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME);  
         if (ret == 0) // success  
         {  
             break;  
         }  
         else if (errno == EAGAIN || errno == EINTR) // retry  
         {  
             timer++;  
             if (timer % USER_LIST_TRY_LOCK_TIMES == 0)  
             {  
                 log_error("user_list_try_rw_lock() tried %d times\n", timer);  
             }  
         }  
         else // failed  
         {  
             log_error("user_list_try_rw_lock() failed\n");  
             break;  
         }  
     }  
1072    
1073      return ret;  int get_user_id_list(int32_t *p_uid_list, int *p_user_cnt, int start_uid)
1074    {
1075            int left;
1076            int right;
1077            int mid;
1078            int ret = 0;
1079            int i;
1080    
1081            if (p_uid_list == NULL || p_user_cnt == NULL)
1082            {
1083                    log_error("NULL pointer error\n");
1084                    return -1;
1085            }
1086    
1087            // acquire lock of user list
1088            if (user_list_rd_lock(p_user_list_pool->semid) < 0)
1089            {
1090                    log_error("user_list_rd_lock() error\n");
1091                    return -2;
1092            }
1093    
1094            left = 0;
1095            right = p_user_list_pool->p_current->user_count - 1;
1096    
1097            while (left < right)
1098            {
1099                    mid = (left + right) / 2;
1100                    if (start_uid < p_user_list_pool->p_current->index_uid[mid].uid)
1101                    {
1102                            right = mid;
1103                    }
1104                    else if (start_uid > p_user_list_pool->p_current->index_uid[mid].uid)
1105                    {
1106                            left = mid + 1;
1107                    }
1108                    else // if (start_uid == p_user_list_pool->p_current->index_uid[mid].uid)
1109                    {
1110                            left = mid;
1111                            break;
1112                    }
1113            }
1114    
1115            for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->p_current->user_count; i++)
1116            {
1117                    p_uid_list[i] = p_user_list_pool->p_current->index_uid[left + i].uid;
1118            }
1119            *p_user_cnt = i;
1120    
1121            // release lock of user list
1122            if (user_list_rd_unlock(p_user_list_pool->semid) < 0)
1123            {
1124                    log_error("user_list_rd_unlock() error\n");
1125                    ret = -1;
1126            }
1127    
1128            return ret;
1129  }  }
1130    
1131  int query_user_list(int page_id, USER_INFO *p_users, int *p_user_count, int *p_page_count)  int user_stat_update(void)
1132  {  {
1133      int ret = 0;          return user_stat_map_update(&(p_user_list_pool->user_stat_map));
1134    }
1135    
1136    int user_article_cnt_inc(int32_t uid, int n)
1137    {
1138            return user_stat_article_cnt_inc(&(p_user_list_pool->user_stat_map), uid, n);
1139    }
1140    
1141      if (p_users == NULL || p_user_count == NULL || p_page_count == NULL)  int get_user_article_cnt(int32_t uid)
1142      {  {
1143          log_error("NULL pointer error\n");          const USER_STAT *p_stat;
1144          return -1;          int ret;
     }  
   
     // acquire lock of user list  
     if (user_list_rd_lock() < 0)  
     {  
         log_error("user_list_rd_lock() error\n");  
         return -2;  
     }  
   
     if (p_user_list_pool->p_current->user_count == 0)  
     {  
         // empty list  
         ret = 0;  
         goto cleanup;  
     }  
   
     *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +  
                     (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);  
   
     if (page_id < 0 || page_id >= *p_page_count)  
     {  
         log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);  
         ret = -3;  
         goto cleanup;  
     }  
   
     *p_user_count = MIN(BBS_user_limit_per_page,  
                         p_user_list_pool->p_current->user_count -  
                             page_id * BBS_user_limit_per_page);  
   
     memcpy(p_users,  
            p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,  
            sizeof(USER_INFO) * (size_t)(*p_user_count));  
1145    
1146  cleanup:          ret = user_stat_get(&(p_user_list_pool->user_stat_map), uid, &p_stat);
1147      // release lock of user list          if (ret < 0)
1148      if (user_list_rd_unlock() < 0)          {
1149      {                  log_error("user_stat_get(uid=%d) error: %d\n", uid, ret);
1150          log_error("user_list_rd_unlock() error\n");                  return -1;
1151          ret = -1;          }
1152      }          else if (ret == 0) // user not found
1153            {
1154                    return -1;
1155            }
1156    
1157      return ret;          return p_stat->article_count;
1158  }  }


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

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