/[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.9 by sysadm, Wed Oct 22 07:39:02 2025 UTC Revision 1.44 by sysadm, Thu Nov 20 11:31:56 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                   user_list.c  -  description  /*
3                                                           -------------------   * user_list
4          Copyright            : (C) 2004-2025 by Leaflet   *   - data model and basic operations of (online) user list
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "common.h"  #include "common.h"
14  #include "database.h"  #include "database.h"
15  #include "log.h"  #include "log.h"
16  #include "trie_dict.h"  #include "trie_dict.h"
17  #include "user_list.h"  #include "user_list.h"
18    #include "user_stat.h"
19  #include <errno.h>  #include <errno.h>
20    #include <fcntl.h>
21  #include <stdlib.h>  #include <stdlib.h>
22  #include <string.h>  #include <string.h>
23  #include <time.h>  #include <time.h>
 #include <sys/ipc.h>  
24  #include <sys/mman.h>  #include <sys/mman.h>
25  #include <sys/param.h>  #include <sys/param.h>
26    #include <sys/stat.h>
27    
28    #ifdef HAVE_SYSTEM_V
29  #include <sys/sem.h>  #include <sys/sem.h>
 #include <sys/shm.h>  
30    
31  #ifdef _SEM_SEMUN_UNDEFINED  #ifdef _SEM_SEMUN_UNDEFINED
32  union semun  union semun
# Line 36  union semun Line 35  union semun
35          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
36          unsigned short *array; /* Array for GETALL, SETALL */          unsigned short *array; /* Array for GETALL, SETALL */
37          struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
38                                                            (Linux-specific) */                                                          (Linux-specific) */
39  };  };
40  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
41    
42  #define USER_LIST_TRY_LOCK_WAIT_TIME 1 // second  #else
43  #define USER_LIST_TRY_LOCK_TIMES 10  #include <semaphore.h>
44    #endif
45    
46    enum _user_list_constant_t
47    {
48            USER_LIST_TRY_LOCK_WAIT_TIME = 1, // second
49            USER_LIST_TRY_LOCK_TIMES = 10,
50            USER_LIST_DEAD_LOCK_TIMEOUT = 15, // second
51    };
52    
53  struct user_list_pool_t  struct user_list_pool_t
54  {  {
55          int shmid;          size_t shm_size;
56    #ifndef HAVE_SYSTEM_V
57            sem_t sem;
58            uint16_t read_lock_count;
59            uint16_t write_lock_count;
60    #else
61          int semid;          int semid;
62    #endif
63          USER_LIST user_list[2];          USER_LIST user_list[2];
64          USER_LIST *p_current;          int user_list_index_current;
65          USER_LIST *p_new;          int user_list_index_new;
66          USER_ONLINE_LIST user_online_list[2];          USER_ONLINE_LIST user_online_list[2];
67          USER_ONLINE_LIST *p_online_current;          int user_online_list_index_current;
68          USER_ONLINE_LIST *p_online_new;          int user_online_list_index_new;
69            USER_STAT_MAP user_stat_map;
70            int user_login_count;
71  };  };
72  typedef struct user_list_pool_t USER_LIST_POOL;  typedef struct user_list_pool_t USER_LIST_POOL;
73    
74    static char user_list_shm_name[FILE_NAME_LEN];
75  static USER_LIST_POOL *p_user_list_pool = NULL;  static USER_LIST_POOL *p_user_list_pool = NULL;
76  static TRIE_NODE *p_trie_action_dict = NULL;  static TRIE_NODE *p_trie_action_dict = NULL;
77    
# Line 83  const USER_ACTION_MAP user_action_map[] Line 99  const USER_ACTION_MAP user_action_map[]
99    
100  const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP);  const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP);
101    
102  static int user_list_try_rd_lock(int semid, int wait_sec);  static int user_list_try_rd_lock(int wait_sec);
103  static int user_list_try_rw_lock(int semid, int wait_sec);  static int user_list_try_rw_lock(int wait_sec);
104  static int user_list_rd_unlock(int semid);  static int user_list_rd_unlock(void);
105  static int user_list_rw_unlock(int semid);  static int user_list_rw_unlock(void);
106  static int user_list_rd_lock(int semid);  static int user_list_rd_lock(void);
107  static int user_list_rw_lock(int semid);  static int user_list_rw_lock(void);
108    #ifndef HAVE_SYSTEM_V
109    static int user_list_reset_lock(void);
110    #endif
111    
112  static int user_list_load(MYSQL *db, USER_LIST *p_list);  static int user_list_load(MYSQL *db, USER_LIST *p_list);
113  static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list);  static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list);
114    static int user_login_count_load(MYSQL *db);
115    
116  static int user_info_index_uid_comp(const void *ptr1, const void *ptr2)  static int user_info_index_uid_comp(const void *ptr1, const void *ptr2)
117  {  {
# Line 106  static int user_info_index_uid_comp(cons Line 126  static int user_info_index_uid_comp(cons
126          {          {
127                  return 1;                  return 1;
128          }          }
129            else if (p1->id < p2->id)
130            {
131                    return -1;
132            }
133            else if (p1->id > p2->id)
134            {
135                    return 1;
136            }
137          return 0;          return 0;
138  }  }
139    
# Line 116  int user_list_load(MYSQL *db, USER_LIST Line 144  int user_list_load(MYSQL *db, USER_LIST
144          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
145          int ret = 0;          int ret = 0;
146          int i;          int i;
147            int j;
148            int32_t last_uid;
149            size_t intro_buf_offset;
150            size_t intro_len;
151    
152          if (db == NULL || p_list == NULL)          if (db == NULL || p_list == NULL)
153          {          {
# Line 123  int user_list_load(MYSQL *db, USER_LIST Line 155  int user_list_load(MYSQL *db, USER_LIST
155                  return -1;                  return -1;
156          }          }
157    
158            if (p_list->user_count > 0)
159            {
160                    last_uid = p_list->users[p_list->user_count - 1].uid;
161            }
162            else
163            {
164                    last_uid = -1;
165            }
166    
167          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
168                           "SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, "                           "SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, visit_count, "
169                           "UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday) "                           "UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(last_logout_dt), "
170                             "UNIX_TIMESTAMP(birthday), `introduction` "
171                           "FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "                           "FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
172                           "INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID "                           "INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID "
173                           "WHERE enable ORDER BY username");                           "WHERE enable ORDER BY username");
# Line 144  int user_list_load(MYSQL *db, USER_LIST Line 186  int user_list_load(MYSQL *db, USER_LIST
186                  goto cleanup;                  goto cleanup;
187          }          }
188    
189            intro_buf_offset = 0;
190          i = 0;          i = 0;
191          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
192          {          {
# Line 158  int user_list_load(MYSQL *db, USER_LIST Line 201  int user_list_load(MYSQL *db, USER_LIST
201                  p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4]));                  p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4]));
202                  p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5]));                  p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5]));
203                  p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6]));                  p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6]));
204                  p_list->users[i].signup_dt = (row[7] == NULL ? 0 : atol(row[7]));                  p_list->users[i].visit_count = (row[7] == NULL ? 0 : atoi(row[7]));
205                  p_list->users[i].last_login_dt = (row[8] == NULL ? 0 : atol(row[8]));                  p_list->users[i].signup_dt = (row[8] == NULL ? 0 : atol(row[8]));
206                  p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9]));                  p_list->users[i].last_login_dt = (row[9] == NULL ? 0 : atol(row[9]));
207                    p_list->users[i].last_logout_dt = (row[10] == NULL ? 0 : atol(row[10]));
208                  // index                  p_list->users[i].birthday = (row[11] == NULL ? 0 : atol(row[11]));
209                  p_list->index_uid[i].uid = p_list->users[i].uid;                  intro_len = strlen((row[12] == NULL ? "" : row[12]));
210                  p_list->index_uid[i].id = i;                  if (intro_len >= sizeof(p_list->user_intro_buf) - 1 - intro_buf_offset)
211                    {
212                            log_error("OOM for user introduction: len=%d, i=%d\n", intro_len, i);
213                            break;
214                    }
215                    memcpy(p_list->user_intro_buf + intro_buf_offset,
216                               (row[12] == NULL ? "" : row[12]),
217                               intro_len + 1);
218                    p_list->users[i].intro = p_list->user_intro_buf + intro_buf_offset;
219                    intro_buf_offset += (intro_len + 1);
220    
221                  i++;                  i++;
222                  if (i >= BBS_max_user_count)                  if (i >= BBS_max_user_count)
# Line 176  int user_list_load(MYSQL *db, USER_LIST Line 228  int user_list_load(MYSQL *db, USER_LIST
228          mysql_free_result(rs);          mysql_free_result(rs);
229          rs = NULL;          rs = NULL;
230    
231          p_list->user_count = i;          if (i != p_list->user_count || p_list->users[i - 1].uid != last_uid) // Count of users changed
232            {
233                    // Rebuild index
234                    for (j = 0; j < i; j++)
235                    {
236                            p_list->index_uid[j].uid = p_list->users[j].uid;
237                            p_list->index_uid[j].id = j;
238                    }
239    
240                    qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
241    
242    #ifdef _DEBUG
243                    log_error("Rebuild index of %d users, last_uid=%d\n", i, p_list->users[i - 1].uid);
244    #endif
245            }
246    
247          // Sort index          p_list->user_count = i;
         qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);  
248    
249  #ifdef _DEBUG  #ifdef _DEBUG
250          log_error("Loaded %d users\n", p_list->user_count);          log_error("Loaded %d users\n", p_list->user_count);
# Line 191  cleanup: Line 256  cleanup:
256          return ret;          return ret;
257  }  }
258    
259  int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list)  int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list)
260  {  {
261          MYSQL_RES *rs = NULL;          MYSQL_RES *rs = NULL;
262          MYSQL_ROW row;          MYSQL_ROW row;
263          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
264          int ret = 0;          int ret = 0;
265          int i;          int i;
266            int j;
267            int user_cnt;
268            int guest_cnt;
269    
270          if (db == NULL || p_list == NULL)          if (db == NULL || p_online_list == NULL)
271          {          {
272                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
273                  return -1;                  return -1;
# Line 208  int user_online_list_load(MYSQL *db, USE Line 276  int user_online_list_load(MYSQL *db, USE
276          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
277                           "SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), "                           "SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), "
278                           "UNIX_TIMESTAMP(last_tm) FROM user_online "                           "UNIX_TIMESTAMP(last_tm) FROM user_online "
279                           "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) AND UID <> 0 "                           "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) "
280                           "ORDER BY last_tm DESC",                           "ORDER BY last_tm DESC",
281                           BBS_user_off_line);                           BBS_user_off_line);
282    
# Line 227  int user_online_list_load(MYSQL *db, USE Line 295  int user_online_list_load(MYSQL *db, USE
295          }          }
296    
297          i = 0;          i = 0;
298            user_cnt = 0;
299            guest_cnt = 0;
300          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
301          {          {
302                  p_list->users[i].id = i;                  if (atoi(row[1]) == 0) // guest
                 strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1);  
                 p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0';  
   
                 if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_list->users[i].user_info))) < 0)  
303                  {                  {
304                          log_error("query_user_info(%d) error\n", atoi(row[1]));                          guest_cnt++;
305                          continue;                          continue;
306                  }                  }
307                  else if (ret == 0) // skip Guest                  else
308                  {                  {
309                            user_cnt++;
310                    }
311    
312                    p_online_list->users[i].id = i;
313                    strncpy(p_online_list->users[i].session_id, row[0], sizeof(p_online_list->users[i].session_id) - 1);
314                    p_online_list->users[i].session_id[sizeof(p_online_list->users[i].session_id) - 1] = '\0';
315    
316                    if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_online_list->users[i].user_info), NULL, 0)) <= 0)
317                    {
318                            log_error("query_user_info_by_uid(%d) error\n", atoi(row[1]));
319                          continue;                          continue;
320                  }                  }
321    
322                  strncpy(p_list->users[i].ip, row[2], sizeof(p_list->users[i].ip) - 1);                  strncpy(p_online_list->users[i].ip, row[2], sizeof(p_online_list->users[i].ip) - 1);
323                  p_list->users[i].ip[sizeof(p_list->users[i].ip) - 1] = '\0';                  p_online_list->users[i].ip[sizeof(p_online_list->users[i].ip) - 1] = '\0';
324    
325                  strncpy(p_list->users[i].current_action, row[3], sizeof(p_list->users[i].current_action) - 1);                  strncpy(p_online_list->users[i].current_action, row[3], sizeof(p_online_list->users[i].current_action) - 1);
326                  p_list->users[i].current_action[sizeof(p_list->users[i].current_action) - 1] = '\0';                  p_online_list->users[i].current_action[sizeof(p_online_list->users[i].current_action) - 1] = '\0';
327                  p_list->users[i].current_action_title = NULL;                  p_online_list->users[i].current_action_title = NULL;
328                  if (p_list->users[i].current_action[0] == '\0')                  if (p_online_list->users[i].current_action[0] == '\0')
329                  {                  {
330                          p_list->users[i].current_action_title = "";                          p_online_list->users[i].current_action_title = "";
331                  }                  }
332                  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)                  else if (trie_dict_get(p_trie_action_dict, p_online_list->users[i].current_action, (int64_t *)(&(p_online_list->users[i].current_action_title))) < 0)
333                  {                  {
334                          log_error("trie_dict_get(p_trie_action_dict, %s) error on session_id=%s\n",                          log_error("trie_dict_get(p_trie_action_dict, %s) error on session_id=%s\n",
335                                            p_list->users[i].current_action, p_list->users[i].session_id);                                            p_online_list->users[i].current_action, p_online_list->users[i].session_id);
336                          continue;                          continue;
337                  }                  }
338    
339                  p_list->users[i].login_tm = (row[4] == NULL ? 0 : atol(row[4]));                  p_online_list->users[i].login_tm = (row[4] == NULL ? 0 : atol(row[4]));
340                  p_list->users[i].last_tm = (row[5] == NULL ? 0 : atol(row[5]));                  p_online_list->users[i].last_tm = (row[5] == NULL ? 0 : atol(row[5]));
341    
342                  i++;                  i++;
343                  if (i >= BBS_max_user_online_count)                  if (i >= BBS_max_user_online_count)
# Line 273  int user_online_list_load(MYSQL *db, USE Line 349  int user_online_list_load(MYSQL *db, USE
349          mysql_free_result(rs);          mysql_free_result(rs);
350          rs = NULL;          rs = NULL;
351    
352          p_list->user_count = i;          if (user_cnt > 0)
353            {
354                    // Rebuild index
355                    for (j = 0; j < user_cnt; j++)
356                    {
357                            p_online_list->index_uid[j].uid = p_online_list->users[j].user_info.uid;
358                            p_online_list->index_uid[j].id = j;
359                    }
360    
361  #ifdef _DEBUG                  qsort(p_online_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp);
362          log_error("Loaded %d users\n", p_list->user_count);          }
363  #endif  
364            p_online_list->user_count = user_cnt;
365            p_online_list->guest_count = guest_cnt;
366    
367  cleanup:  cleanup:
368          mysql_free_result(rs);          mysql_free_result(rs);
# Line 285  cleanup: Line 370  cleanup:
370          return ret;          return ret;
371  }  }
372    
373  int user_list_pool_init(void)  int user_login_count_load(MYSQL *db)
374  {  {
375          int shmid;          MYSQL_RES *rs = NULL;
376          int semid;          MYSQL_ROW row;
377          int proj_id;          char sql[SQL_BUFFER_LEN];
378          key_t key;  
379            if (db == NULL)
380            {
381                    log_error("NULL pointer error\n");
382                    return -1;
383            }
384    
385            snprintf(sql, sizeof(sql),
386                             "SELECT ID FROM user_login_log ORDER BY ID DESC LIMIT 1");
387            if (mysql_query(db, sql) != 0)
388            {
389                    log_error("Query user_login_log error: %s\n", mysql_error(db));
390                    return -2;
391            }
392            if ((rs = mysql_store_result(db)) == NULL)
393            {
394                    log_error("Get user_login_log data failed\n");
395                    return -2;
396            }
397            if ((row = mysql_fetch_row(rs)))
398            {
399                    p_user_list_pool->user_login_count = atoi(row[0]);
400            }
401            mysql_free_result(rs);
402    
403            return 0;
404    }
405    
406    int user_list_pool_init(const char *filename)
407    {
408            char filepath[FILE_PATH_LEN];
409            int fd;
410          size_t size;          size_t size;
411          void *p_shm;          void *p_shm;
412    #ifdef HAVE_SYSTEM_V
413            int proj_id;
414            key_t key;
415            int semid;
416          union semun arg;          union semun arg;
417    #endif
418          int i;          int i;
419    
420          if (p_user_list_pool != NULL || p_trie_action_dict != NULL)          if (p_user_list_pool != NULL || p_trie_action_dict != NULL)
# Line 318  int user_list_pool_init(void) Line 439  int user_list_pool_init(void)
439          }          }
440    
441          // Allocate shared memory          // Allocate shared memory
442          proj_id = (int)(time(NULL) % getpid());          size = sizeof(USER_LIST_POOL);
443          key = ftok(VAR_USER_LIST_SHM, proj_id);  
444          if (key == -1)          strncpy(filepath, filename, sizeof(filepath) - 1);
445            filepath[sizeof(filepath) - 1] = '\0';
446            snprintf(user_list_shm_name, sizeof(user_list_shm_name), "/USER_LIST_SHM_%s", basename(filepath));
447    
448            if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT)
449          {          {
450                  log_error("ftok(%s %d) error (%d)\n", VAR_USER_LIST_SHM, proj_id, errno);                  log_error("shm_unlink(%s) error (%d)\n", user_list_shm_name, errno);
451                  return -2;                  return -2;
452          }          }
453    
454          size = sizeof(USER_LIST_POOL);          if ((fd = shm_open(user_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
         shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
         if (shmid == -1)  
455          {          {
456                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  log_error("shm_open(%s) error (%d)\n", user_list_shm_name, errno);
457                  return -3;                  return -2;
458          }          }
459          p_shm = shmat(shmid, NULL, 0);          if (ftruncate(fd, (off_t)size) == -1)
         if (p_shm == (void *)-1)  
460          {          {
461                  log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);
462                  return -3;                  close(fd);
463                    return -2;
464            }
465    
466            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
467            if (p_shm == MAP_FAILED)
468            {
469                    log_error("mmap() error (%d)\n", errno);
470                    close(fd);
471                    return -2;
472            }
473    
474            if (close(fd) < 0)
475            {
476                    log_error("close(fd) error (%d)\n", errno);
477                    return -1;
478          }          }
479    
480          p_user_list_pool = p_shm;          p_user_list_pool = p_shm;
481          p_user_list_pool->shmid = shmid;          p_user_list_pool->shm_size = size;
482    
483          // Allocate semaphore as user list pool lock          // Allocate semaphore as user list pool lock
484    #ifndef HAVE_SYSTEM_V
485            if (sem_init(&(p_user_list_pool->sem), 1, 1) == -1)
486            {
487                    log_error("sem_init() error (%d)\n", errno);
488                    return -3;
489            }
490    
491            p_user_list_pool->read_lock_count = 0;
492            p_user_list_pool->write_lock_count = 0;
493    #else
494            proj_id = (int)(time(NULL) % getpid());
495            key = ftok(filename, proj_id);
496            if (key == -1)
497            {
498                    log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
499                    return -2;
500            }
501    
502          size = 2; // r_sem and w_sem          size = 2; // r_sem and w_sem
503          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
504          if (semid == -1)          if (semid == -1)
# Line 364  int user_list_pool_init(void) Line 519  int user_list_pool_init(void)
519          }          }
520    
521          p_user_list_pool->semid = semid;          p_user_list_pool->semid = semid;
522    #endif
523    
524          // Set user counts to 0          // Set user counts to 0
525          p_user_list_pool->user_list[0].user_count = 0;          p_user_list_pool->user_list[0].user_count = 0;
526          p_user_list_pool->user_list[1].user_count = 0;          p_user_list_pool->user_list[1].user_count = 0;
527    
528          p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);          p_user_list_pool->user_list_index_current = 0;
529          p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);          p_user_list_pool->user_list_index_new = 1;
530    
531            p_user_list_pool->user_online_list_index_current = 0;
532            p_user_list_pool->user_online_list_index_new = 1;
533    
534          p_user_list_pool->p_online_current = &(p_user_list_pool->user_online_list[0]);          user_stat_map_init(&(p_user_list_pool->user_stat_map));
         p_user_list_pool->p_online_new = &(p_user_list_pool->user_online_list[1]);  
535    
536          return 0;          return 0;
537  }  }
538    
539  void user_list_pool_cleanup(void)  void user_list_pool_cleanup(void)
540  {  {
         int shmid;  
   
541          if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
542          {          {
543                  return;                  return;
544          }          }
545    
546          shmid = p_user_list_pool->shmid;  #ifdef HAVE_SYSTEM_V
   
547          if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)          if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)
548          {          {
549                  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);
550          }          }
551    #else
552          if (shmdt(p_user_list_pool) == -1)          if (sem_destroy(&(p_user_list_pool->sem)) == -1)
553          {          {
554                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("sem_destroy() error (%d)\n", errno);
555          }          }
556    #endif
557    
558          if (shmctl(shmid, IPC_RMID, NULL) == -1)          detach_user_list_pool_shm();
559    
560            if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT)
561          {          {
562                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shm_unlink(%s) error (%d)\n", user_list_shm_name, errno);
563          }          }
564    
565          p_user_list_pool = NULL;          user_list_shm_name[0] = '\0';
566    
567          if (p_trie_action_dict != NULL)          if (p_trie_action_dict != NULL)
568          {          {
# Line 416  void user_list_pool_cleanup(void) Line 574  void user_list_pool_cleanup(void)
574    
575  int set_user_list_pool_shm_readonly(void)  int set_user_list_pool_shm_readonly(void)
576  {  {
577          int shmid;          if (p_user_list_pool != NULL && mprotect(p_user_list_pool, p_user_list_pool->shm_size, PROT_READ) < 0)
         void *p_shm;  
   
         if (p_user_list_pool == NULL)  
578          {          {
579                  log_error("p_user_list_pool not initialized\n");                  log_error("mprotect() error (%d)\n", errno);
580                  return -1;                  return -1;
581          }          }
582    
         shmid = p_user_list_pool->shmid;  
   
         // Remap shared memory in read-only mode  
         p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);  
         if (p_shm == (void *)-1)  
         {  
                 log_error("shmat(user_list_pool shmid = %d) error (%d)\n", shmid, errno);  
                 return -3;  
         }  
   
         p_user_list_pool = p_shm;  
   
583          return 0;          return 0;
584  }  }
585    
586  int detach_user_list_pool_shm(void)  int detach_user_list_pool_shm(void)
587  {  {
588          if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)          if (p_user_list_pool != NULL && munmap(p_user_list_pool, p_user_list_pool->shm_size) < 0)
589          {          {
590                  log_error("shmdt(user_list_pool) error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
591                  return -1;                  return -1;
592          }          }
593    
# Line 456  int detach_user_list_pool_shm(void) Line 599  int detach_user_list_pool_shm(void)
599  int user_list_pool_reload(int online_user)  int user_list_pool_reload(int online_user)
600  {  {
601          MYSQL *db = NULL;          MYSQL *db = NULL;
602          USER_LIST *p_tmp;          int tmp;
603          USER_ONLINE_LIST *p_online_tmp;          int ret = 0;
604    
605          if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
606          {          {
# Line 474  int user_list_pool_reload(int online_use Line 617  int user_list_pool_reload(int online_use
617    
618          if (online_user)          if (online_user)
619          {          {
620                  if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0)                  if (user_online_list_load(db, &(p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_new])) < 0)
621                  {                  {
622                          log_error("user_online_list_load() error\n");                          log_error("user_online_list_load() error\n");
623                          return -2;                          ret = -2;
624                            goto cleanup;
625                    }
626    
627                    if (user_login_count_load(db) < 0)
628                    {
629                            log_error("user_login_count_load() error\n");
630                            ret = -2;
631                            goto cleanup;
632                  }                  }
633          }          }
634          else          else
635          {          {
636                  if (user_list_load(db, p_user_list_pool->p_new) < 0)                  if (user_list_load(db, &(p_user_list_pool->user_list[p_user_list_pool->user_list_index_new])) < 0)
637                  {                  {
638                          log_error("user_list_load() error\n");                          log_error("user_list_load() error\n");
639                          return -2;                          ret = -2;
640                            goto cleanup;
641                  }                  }
642          }          }
643    
644          mysql_close(db);          mysql_close(db);
645            db = NULL;
646    
647          if (user_list_rw_lock(p_user_list_pool->semid) < 0)          if (user_list_rw_lock() < 0)
648          {          {
649                  log_error("user_list_rw_lock() error\n");                  log_error("user_list_rw_lock() error\n");
650                  return -3;                  ret = -3;
651                    goto cleanup;
652          }          }
653    
654          if (online_user)          if (online_user)
655          {          {
656                  // Swap p_online_current and p_online_new                  // Swap p_online_current and p_online_new
657                  p_online_tmp = p_user_list_pool->p_online_current;                  tmp = p_user_list_pool->user_online_list_index_current;
658                  p_user_list_pool->p_online_current = p_user_list_pool->p_online_new;                  p_user_list_pool->user_online_list_index_current = p_user_list_pool->user_online_list_index_new;
659                  p_user_list_pool->p_online_new = p_online_tmp;                  p_user_list_pool->user_online_list_index_new = tmp;
660          }          }
661          else          else
662          {          {
663                  // Swap p_current and p_new                  // Swap index_current and index_new
664                  p_tmp = p_user_list_pool->p_current;                  tmp = p_user_list_pool->user_list_index_current;
665                  p_user_list_pool->p_current = p_user_list_pool->p_new;                  p_user_list_pool->user_list_index_current = p_user_list_pool->user_list_index_new;
666                  p_user_list_pool->p_new = p_tmp;                  p_user_list_pool->user_list_index_new = tmp;
667          }          }
668    
669          if (user_list_rw_unlock(p_user_list_pool->semid) < 0)          if (user_list_rw_unlock() < 0)
670          {          {
671                  log_error("user_list_rw_unlock() error\n");                  log_error("user_list_rw_unlock() error\n");
672                  return -3;                  ret = -3;
673                    goto cleanup;
674          }          }
675    
676          return 0;  cleanup:
677            mysql_close(db);
678    
679            return ret;
680  }  }
681    
682  int user_list_try_rd_lock(int semid, int wait_sec)  int user_list_try_rd_lock(int wait_sec)
683  {  {
684    #ifdef HAVE_SYSTEM_V
685          struct sembuf sops[2];          struct sembuf sops[2];
686    #endif
687          struct timespec timeout;          struct timespec timeout;
688          int ret;          int ret = 0;
689    
690            if (p_user_list_pool == NULL)
691            {
692                    log_error("p_user_list_pool not initialized\n");
693                    return -1;
694            }
695    
696            timeout.tv_sec = wait_sec;
697            timeout.tv_nsec = 0;
698    
699    #ifdef HAVE_SYSTEM_V
700          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
701          sops[0].sem_op = 0;      // wait until unlocked          sops[0].sem_op = 0;      // wait until unlocked
702          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 535  int user_list_try_rd_lock(int semid, int Line 705  int user_list_try_rd_lock(int semid, int
705          sops[1].sem_op = 1;                     // lock          sops[1].sem_op = 1;                     // lock
706          sops[1].sem_flg = SEM_UNDO; // undo on terminate          sops[1].sem_flg = SEM_UNDO; // undo on terminate
707    
708          timeout.tv_sec = wait_sec;          ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);
         timeout.tv_nsec = 0;  
   
         ret = semtimedop(semid, sops, 2, &timeout);  
709          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
710          {          {
711                  log_error("semtimedop(lock read) error %d\n", errno);                  log_error("semop(lock read) error %d\n", errno);
712            }
713    #else
714            if (sem_timedwait(&(p_user_list_pool->sem), &timeout) == -1)
715            {
716                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
717                    {
718                            log_error("sem_timedwait() error %d\n", errno);
719                    }
720                    return -1;
721          }          }
722    
723            if (p_user_list_pool->write_lock_count == 0)
724            {
725                    p_user_list_pool->read_lock_count++;
726            }
727            else
728            {
729                    errno = EAGAIN;
730                    ret = -1;
731            }
732    
733            if (sem_post(&(p_user_list_pool->sem)) == -1)
734            {
735                    log_error("sem_post() error %d\n", errno);
736                    return -1;
737            }
738    #endif
739    
740          return ret;          return ret;
741  }  }
742    
743  int user_list_try_rw_lock(int semid, int wait_sec)  int user_list_try_rw_lock(int wait_sec)
744  {  {
745    #ifdef HAVE_SYSTEM_V
746          struct sembuf sops[3];          struct sembuf sops[3];
747    #endif
748          struct timespec timeout;          struct timespec timeout;
749          int ret;          int ret = 0;
750    
751            if (p_user_list_pool == NULL)
752            {
753                    log_error("p_user_list_pool not initialized\n");
754                    return -1;
755            }
756    
757            timeout.tv_sec = wait_sec;
758            timeout.tv_nsec = 0;
759    
760    #ifdef HAVE_SYSTEM_V
761          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
762          sops[0].sem_op = 0;      // wait until unlocked          sops[0].sem_op = 0;      // wait until unlocked
763          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 565  int user_list_try_rw_lock(int semid, int Line 770  int user_list_try_rw_lock(int semid, int
770          sops[2].sem_op = 0;      // wait until unlocked          sops[2].sem_op = 0;      // wait until unlocked
771          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
772    
773          timeout.tv_sec = wait_sec;          ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);
         timeout.tv_nsec = 0;  
   
         ret = semtimedop(semid, sops, 3, &timeout);  
774          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
775          {          {
776                  log_error("semtimedop(lock write) error %d\n", errno);                  log_error("semop(lock write) error %d\n", errno);
777            }
778    #else
779            if (sem_timedwait(&(p_user_list_pool->sem), &timeout) == -1)
780            {
781                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
782                    {
783                            log_error("sem_timedwait() error %d\n", errno);
784                    }
785                    return -1;
786            }
787    
788            if (p_user_list_pool->read_lock_count == 0 && p_user_list_pool->write_lock_count == 0)
789            {
790                    p_user_list_pool->write_lock_count++;
791            }
792            else
793            {
794                    errno = EAGAIN;
795                    ret = -1;
796            }
797    
798            if (sem_post(&(p_user_list_pool->sem)) == -1)
799            {
800                    log_error("sem_post() error %d\n", errno);
801                    return -1;
802          }          }
803    #endif
804    
805          return ret;          return ret;
806  }  }
807    
808  int user_list_rd_unlock(int semid)  int user_list_rd_unlock(void)
809  {  {
810    #ifdef HAVE_SYSTEM_V
811          struct sembuf sops[2];          struct sembuf sops[2];
812          int ret;  #endif
813            int ret = 0;
814    
815            if (p_user_list_pool == NULL)
816            {
817                    log_error("p_user_list_pool not initialized\n");
818                    return -1;
819            }
820    
821    #ifdef HAVE_SYSTEM_V
822          sops[0].sem_num = 0;                                     // r_sem          sops[0].sem_num = 0;                                     // r_sem
823          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                     // unlock
824          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
825    
826          ret = semop(semid, sops, 1);          ret = semop(p_user_list_pool->semid, sops, 1);
827          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
828          {          {
829                  log_error("semop(unlock read) error %d\n", errno);                  log_error("semop(unlock read) error %d\n", errno);
830          }          }
831    #else
832            if (sem_wait(&(p_user_list_pool->sem)) == -1)
833            {
834                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
835                    {
836                            log_error("sem_wait() error %d\n", errno);
837                    }
838                    return -1;
839            }
840    
841            if (p_user_list_pool->read_lock_count > 0)
842            {
843                    p_user_list_pool->read_lock_count--;
844            }
845            else
846            {
847                    log_error("read_lock_count already 0\n");
848            }
849    
850            if (sem_post(&(p_user_list_pool->sem)) == -1)
851            {
852                    log_error("sem_post() error %d\n", errno);
853                    return -1;
854            }
855    #endif
856    
857          return ret;          return ret;
858  }  }
859    
860  int user_list_rw_unlock(int semid)  int user_list_rw_unlock(void)
861  {  {
862    #ifdef HAVE_SYSTEM_V
863          struct sembuf sops[1];          struct sembuf sops[1];
864          int ret;  #endif
865            int ret = 0;
866    
867            if (p_user_list_pool == NULL)
868            {
869                    log_error("p_user_list_pool not initialized\n");
870                    return -1;
871            }
872    
873    #ifdef HAVE_SYSTEM_V
874          sops[0].sem_num = 1;                                     // w_sem          sops[0].sem_num = 1;                                     // w_sem
875          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                     // unlock
876          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
877    
878          ret = semop(semid, sops, 1);          ret = semop(p_user_list_pool->semid, sops, 1);
879          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
880          {          {
881                  log_error("semop(unlock write) error %d\n", errno);                  log_error("semop(unlock write) error %d\n", errno);
882          }          }
883    #else
884            if (sem_wait(&(p_user_list_pool->sem)) == -1)
885            {
886                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
887                    {
888                            log_error("sem_wait() error %d\n", errno);
889                    }
890                    return -1;
891            }
892    
893            if (p_user_list_pool->write_lock_count > 0)
894            {
895                    p_user_list_pool->write_lock_count--;
896            }
897            else
898            {
899                    log_error("write_lock_count already 0\n");
900            }
901    
902            if (sem_post(&(p_user_list_pool->sem)) == -1)
903            {
904                    log_error("sem_post() error %d\n", errno);
905                    return -1;
906            }
907    #endif
908    
909          return ret;          return ret;
910  }  }
911    
912  int user_list_rd_lock(int semid)  int user_list_rd_lock(void)
913  {  {
914          int timer = 0;          int timer = 0;
915          int ret = -1;          int ret = -1;
916            time_t tm_first_failure = 0;
917    
918            if (p_user_list_pool == NULL)
919            {
920                    log_error("p_user_list_pool not initialized\n");
921                    return -1;
922            }
923    
924          while (!SYS_server_exit)          while (!SYS_server_exit)
925          {          {
926                  ret = user_list_try_rd_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);                  ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME);
927                  if (ret == 0) // success                  if (ret == 0) // success
928                  {                  {
929                          break;                          break;
# Line 631  int user_list_rd_lock(int semid) Line 934  int user_list_rd_lock(int semid)
934                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
935                          {                          {
936                                  log_error("user_list_try_rd_lock() tried %d times\n", timer);                                  log_error("user_list_try_rd_lock() tried %d times\n", timer);
937    
938                                    if (time(NULL) - tm_first_failure >= USER_LIST_DEAD_LOCK_TIMEOUT)
939                                    {
940                                            log_error("Unable to acquire rw_lock for %d seconds\n", time(NULL) - tm_first_failure);
941    #ifndef HAVE_SYSTEM_V
942                                            user_list_reset_lock();
943                                            log_error("Reset POSIX semaphore to resolve dead lock\n");
944    #endif
945                                            break;
946                                    }
947                          }                          }
948                            usleep(100 * 1000); // 0.1 second
949                  }                  }
950                  else // failed                  else // failed
951                  {                  {
# Line 643  int user_list_rd_lock(int semid) Line 957  int user_list_rd_lock(int semid)
957          return ret;          return ret;
958  }  }
959    
960  int user_list_rw_lock(int semid)  int user_list_rw_lock(void)
961  {  {
962          int timer = 0;          int timer = 0;
963          int ret = -1;          int ret = -1;
964            time_t tm_first_failure = 0;
965    
966            if (p_user_list_pool == NULL)
967            {
968                    log_error("p_user_list_pool not initialized\n");
969                    return -1;
970            }
971    
972          while (!SYS_server_exit)          while (!SYS_server_exit)
973          {          {
974                  ret = user_list_try_rw_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);                  ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME);
975                  if (ret == 0) // success                  if (ret == 0) // success
976                  {                  {
977                          break;                          break;
# Line 661  int user_list_rw_lock(int semid) Line 982  int user_list_rw_lock(int semid)
982                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)                          if (timer % USER_LIST_TRY_LOCK_TIMES == 0)
983                          {                          {
984                                  log_error("user_list_try_rw_lock() tried %d times\n", timer);                                  log_error("user_list_try_rw_lock() tried %d times\n", timer);
985    
986                                    if (time(NULL) - tm_first_failure >= USER_LIST_DEAD_LOCK_TIMEOUT)
987                                    {
988                                            log_error("Unable to acquire rw_lock for %d seconds\n", time(NULL) - tm_first_failure);
989    #ifndef HAVE_SYSTEM_V
990                                            user_list_reset_lock();
991                                            log_error("Reset POSIX semaphore to resolve dead lock\n");
992    #endif
993                                            break;
994                                    }
995                          }                          }
996                            usleep(100 * 1000); // 0.1 second
997                  }                  }
998                  else // failed                  else // failed
999                  {                  {
# Line 673  int user_list_rw_lock(int semid) Line 1005  int user_list_rw_lock(int semid)
1005          return ret;          return ret;
1006  }  }
1007    
1008    #ifndef HAVE_SYSTEM_V
1009    int user_list_reset_lock(void)
1010    {
1011            if (p_user_list_pool == NULL)
1012            {
1013                    log_error("p_user_list_pool not initialized\n");
1014                    return -1;
1015            }
1016    
1017            if (sem_destroy(&(p_user_list_pool->sem)) == -1)
1018            {
1019                    log_error("sem_destroy() error (%d)\n", errno);
1020            }
1021    
1022            p_user_list_pool->read_lock_count = 0;
1023            p_user_list_pool->write_lock_count = 0;
1024    
1025            if (sem_init(&(p_user_list_pool->sem), 1, 1) == -1)
1026            {
1027                    log_error("sem_init() error (%d)\n", errno);
1028            }
1029    
1030            return 0;
1031    }
1032    #endif
1033    
1034  int query_user_list(int page_id, USER_INFO *p_users, int *p_user_count, int *p_page_count)  int query_user_list(int page_id, USER_INFO *p_users, int *p_user_count, int *p_page_count)
1035  {  {
1036          int ret = 0;          int ret = 0;
# Line 683  int query_user_list(int page_id, USER_IN Line 1041  int query_user_list(int page_id, USER_IN
1041                  return -1;                  return -1;
1042          }          }
1043    
1044            *p_user_count = 0;
1045            *p_page_count = 0;
1046    
1047          // acquire lock of user list          // acquire lock of user list
1048          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1049          {          {
1050                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1051                  return -2;                  return -2;
1052          }          }
1053    
1054          if (p_user_list_pool->p_current->user_count == 0)          if (p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count == 0)
1055          {          {
1056                  // empty list                  // empty list
1057                  ret = 0;                  ret = 0;
1058                  goto cleanup;                  goto cleanup;
1059          }          }
1060    
1061          *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +          *p_page_count = (p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count + BBS_user_limit_per_page - 1) /
1062                                          (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);                                          BBS_user_limit_per_page;
1063    
1064          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
1065          {          {
# Line 708  int query_user_list(int page_id, USER_IN Line 1069  int query_user_list(int page_id, USER_IN
1069          }          }
1070    
1071          *p_user_count = MIN(BBS_user_limit_per_page,          *p_user_count = MIN(BBS_user_limit_per_page,
1072                                                  p_user_list_pool->p_current->user_count -                                                  p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count -
1073                                                          page_id * BBS_user_limit_per_page);                                                          page_id * BBS_user_limit_per_page);
1074    
1075          memcpy(p_users,          memcpy(p_users,
1076                     p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,                     p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users + page_id * BBS_user_limit_per_page,
1077                     sizeof(USER_INFO) * (size_t)(*p_user_count));                     sizeof(USER_INFO) * (size_t)(*p_user_count));
1078    
1079  cleanup:  cleanup:
1080          // release lock of user list          // release lock of user list
1081          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1082          {          {
1083                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1084                  ret = -1;                  ret = -1;
# Line 736  int query_user_online_list(int page_id, Line 1097  int query_user_online_list(int page_id,
1097                  return -1;                  return -1;
1098          }          }
1099    
1100            *p_user_count = 0;
1101            *p_page_count = 0;
1102    
1103          // acquire lock of user list          // acquire lock of user list
1104          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1105          {          {
1106                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1107                  return -2;                  return -2;
1108          }          }
1109    
1110          if (p_user_list_pool->p_online_current->user_count == 0)          if (p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count == 0)
1111          {          {
1112                  // empty list                  // empty list
1113                  ret = 0;                  ret = 0;
1114                  goto cleanup;                  goto cleanup;
1115          }          }
1116    
1117          *p_page_count = p_user_list_pool->p_online_current->user_count / BBS_user_limit_per_page +          *p_page_count = (p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count + BBS_user_limit_per_page - 1) / BBS_user_limit_per_page;
                                         (p_user_list_pool->p_online_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);  
1118    
1119          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
1120          {          {
# Line 761  int query_user_online_list(int page_id, Line 1124  int query_user_online_list(int page_id,
1124          }          }
1125    
1126          *p_user_count = MIN(BBS_user_limit_per_page,          *p_user_count = MIN(BBS_user_limit_per_page,
1127                                                  p_user_list_pool->p_online_current->user_count -                                                  p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count -
1128                                                          page_id * BBS_user_limit_per_page);                                                          page_id * BBS_user_limit_per_page);
1129    
1130          memcpy(p_online_users,          memcpy(p_online_users,
1131                     p_user_list_pool->p_online_current->users + page_id * BBS_user_limit_per_page,                     p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users + page_id * BBS_user_limit_per_page,
1132                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));
1133    
1134  cleanup:  cleanup:
1135          // release lock of user list          // release lock of user list
1136          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1137          {          {
1138                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1139                  ret = -1;                  ret = -1;
# Line 779  cleanup: Line 1142  cleanup:
1142          return ret;          return ret;
1143  }  }
1144    
1145    int get_user_list_count(int *p_user_cnt)
1146    {
1147            if (p_user_cnt == NULL)
1148            {
1149                    log_error("NULL pointer error\n");
1150                    return -1;
1151            }
1152    
1153            // acquire lock of user list
1154            if (user_list_rd_lock() < 0)
1155            {
1156                    log_error("user_list_rd_lock() error\n");
1157                    return -2;
1158            }
1159    
1160            *p_user_cnt = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count;
1161    
1162            // release lock of user list
1163            if (user_list_rd_unlock() < 0)
1164            {
1165                    log_error("user_list_rd_unlock() error\n");
1166                    return -2;
1167            }
1168    
1169            return 0;
1170    }
1171    
1172    int get_user_online_list_count(int *p_user_cnt, int *p_guest_cnt)
1173    {
1174            if (p_user_cnt == NULL || p_guest_cnt == NULL)
1175            {
1176                    log_error("NULL pointer error\n");
1177                    return -1;
1178            }
1179    
1180            // acquire lock of user list
1181            if (user_list_rd_lock() < 0)
1182            {
1183                    log_error("user_list_rd_lock() error\n");
1184                    return -2;
1185            }
1186    
1187            *p_user_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count;
1188            *p_guest_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].guest_count;
1189    
1190            // release lock of user list
1191            if (user_list_rd_unlock() < 0)
1192            {
1193                    log_error("user_list_rd_unlock() error\n");
1194                    return -2;
1195            }
1196    
1197            return 0;
1198    }
1199    
1200    int get_user_login_count(int *p_login_cnt)
1201    {
1202            if (p_login_cnt == NULL)
1203            {
1204                    log_error("NULL pointer error\n");
1205                    return -1;
1206            }
1207    
1208            *p_login_cnt = p_user_list_pool->user_login_count;
1209    
1210            return 0;
1211    }
1212    
1213  int query_user_info(int32_t id, USER_INFO *p_user)  int query_user_info(int32_t id, USER_INFO *p_user)
1214  {  {
1215          int ret = 0;          int ret = 0;
# Line 790  int query_user_info(int32_t id, USER_INF Line 1221  int query_user_info(int32_t id, USER_INF
1221          }          }
1222    
1223          // acquire lock of user list          // acquire lock of user list
1224          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1225          {          {
1226                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1227                  return -2;                  return -2;
1228          }          }
1229    
1230          if (id >= 0 && id < p_user_list_pool->p_current->user_count) // Found          if (id >= 0 && id < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count) // Found
1231          {          {
1232                  *p_user = p_user_list_pool->p_current->users[id];                  *p_user = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id];
1233                  ret = 1;                  ret = 1;
1234          }          }
1235    
1236          // release lock of user list          // release lock of user list
1237          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1238          {          {
1239                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1240                  ret = -1;                  ret = -1;
# Line 812  int query_user_info(int32_t id, USER_INF Line 1243  int query_user_info(int32_t id, USER_INF
1243          return ret;          return ret;
1244  }  }
1245    
1246  int query_user_info_by_uid(int32_t uid, USER_INFO *p_user)  int query_user_info_by_uid(int32_t uid, USER_INFO *p_user, char *p_intro_buf, size_t intro_buf_len)
1247  {  {
1248          int left;          int left;
1249          int right;          int right;
# Line 827  int query_user_info_by_uid(int32_t uid, Line 1258  int query_user_info_by_uid(int32_t uid,
1258          }          }
1259    
1260          // acquire lock of user list          // acquire lock of user list
1261          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1262          {          {
1263                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1264                  return -2;                  return -2;
1265          }          }
1266    
1267          left = 0;          left = 0;
1268          right = p_user_list_pool->p_current->user_count - 1;          right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1269    
1270          while (left < right)          while (left < right)
1271          {          {
1272                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1273                  if (uid < p_user_list_pool->p_current->index_uid[mid].uid)                  if (uid < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1274                  {                  {
1275                          right = mid;                          right = mid - 1;
1276                  }                  }
1277                  else if (uid > p_user_list_pool->p_current->index_uid[mid].uid)                  else if (uid > p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1278                  {                  {
1279                          left = mid + 1;                          left = mid + 1;
1280                  }                  }
1281                  else // if (uid == p_user_list_pool->p_current->index_uid[mid].uid)                  else // if (uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1282                  {                  {
1283                          left = mid;                          left = mid;
1284                          break;                          break;
1285                  }                  }
1286          }          }
1287    
1288          if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found          if (uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left].uid) // Found
1289          {          {
1290                  id = p_user_list_pool->p_current->index_uid[left].id;                  id = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left].id;
1291                  if ((ret = query_user_info(id, p_user)) <= 0)                  *p_user = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id];
1292                    ret = 1;
1293    
1294                    if (p_intro_buf != NULL)
1295                  {                  {
1296                          log_error("query_user_info(id=%d) error: %d\n", id, ret);                          strncpy(p_intro_buf, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id].intro, intro_buf_len - 1);
1297                            p_intro_buf[intro_buf_len - 1] = '\0';
1298                            p_user->intro = p_intro_buf;
1299                  }                  }
1300                  else          }
1301    
1302            // release lock of user list
1303            if (user_list_rd_unlock() < 0)
1304            {
1305                    log_error("user_list_rd_unlock() error\n");
1306                    ret = -1;
1307            }
1308    
1309            return ret;
1310    }
1311    
1312    int query_user_info_by_username(const char *username_prefix, int max_user_cnt,
1313                                                                    int32_t uid_list[], char username_list[][BBS_username_max_len + 1])
1314    {
1315            int left;
1316            int right;
1317            int mid;
1318            int left_save;
1319            int ret = 0;
1320            size_t prefix_len;
1321            int comp;
1322            int i;
1323    
1324            if (username_prefix == NULL || uid_list == NULL || username_list == NULL)
1325            {
1326                    log_error("NULL pointer error\n");
1327                    return -1;
1328            }
1329    
1330            prefix_len = strlen(username_prefix);
1331    
1332            // acquire lock of user list
1333            if (user_list_rd_lock() < 0)
1334            {
1335                    log_error("user_list_rd_lock() error\n");
1336                    return -2;
1337            }
1338    
1339            left = 0;
1340            right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1341    
1342            while (left < right)
1343            {
1344                    mid = (left + right) / 2;
1345                    comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1346                    if (comp < 0)
1347                  {                  {
1348                          ret = 1;                          right = mid - 1;
1349                    }
1350                    else if (comp > 0)
1351                    {
1352                            left = mid + 1;
1353                    }
1354                    else // if (comp == 0)
1355                    {
1356                            left = mid;
1357                            break;
1358                    }
1359            }
1360    
1361            if (strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left].username, prefix_len) == 0) // Found
1362            {
1363    #ifdef _DEBUG
1364                    log_error("Debug: match found, pos=%d\n", left);
1365    #endif
1366    
1367                    left_save = left;
1368                    right = left;
1369                    left = 0;
1370    
1371                    while (left < right)
1372                    {
1373                            mid = (left + right) / 2;
1374                            comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1375                            if (comp > 0)
1376                            {
1377                                    left = mid + 1;
1378                            }
1379                            else if (comp == 0)
1380                            {
1381                                    right = mid;
1382                            }
1383                            else // if (comp < 0)
1384                            {
1385                                    log_error("Bug: left=%d right=%d mid=%d");
1386                                    ret = -2;
1387                                    goto cleanup;
1388                            }
1389                    }
1390    
1391    #ifdef _DEBUG
1392                    log_error("Debug: first match found, pos=%d\n", right);
1393    #endif
1394    
1395                    left = left_save;
1396                    left_save = right;
1397                    right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1398    
1399                    while (left < right)
1400                    {
1401                            mid = (left + right) / 2 + (left + right) % 2;
1402                            comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1403                            if (comp < 0)
1404                            {
1405                                    right = mid - 1;
1406                            }
1407                            else if (comp == 0)
1408                            {
1409                                    left = mid;
1410                            }
1411                            else // if (comp > 0)
1412                            {
1413                                    log_error("Bug: left=%d right=%d mid=%d");
1414                                    ret = -2;
1415                                    goto cleanup;
1416                            }
1417                    }
1418    
1419    #ifdef _DEBUG
1420                    log_error("Debug: last match found, pos=%d\n", left);
1421    #endif
1422    
1423                    right = left;
1424                    left = left_save;
1425    
1426                    for (i = 0; i < max_user_cnt && left + i <= right; i++)
1427                    {
1428                            uid_list[i] = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left + i].uid;
1429                            strncpy(username_list[i], p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left + i].username, sizeof(username_list[i]) - 1);
1430                            username_list[i][sizeof(username_list[i]) - 1] = '\0';
1431                  }                  }
1432                    ret = i;
1433          }          }
1434    
1435    cleanup:
1436          // release lock of user list          // release lock of user list
1437          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1438          {          {
1439                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1440                  ret = -1;                  ret = -1;
# Line 888  int query_user_online_info(int32_t id, U Line 1454  int query_user_online_info(int32_t id, U
1454          }          }
1455    
1456          // acquire lock of user list          // acquire lock of user list
1457          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1458          {          {
1459                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1460                  return -2;                  return -2;
1461          }          }
1462    
1463          if (id >= 0 && id < p_user_list_pool->p_online_current->user_count) // Found          if (id >= 0 && id < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count) // Found
1464          {          {
1465                  *p_user = p_user_list_pool->p_online_current->users[id];                  *p_user = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users[id];
1466                  ret = 1;                  ret = 1;
1467          }          }
1468    
1469          // release lock of user list          // release lock of user list
1470          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1471            {
1472                    log_error("user_list_rd_unlock() error\n");
1473                    ret = -1;
1474            }
1475    
1476            return ret;
1477    }
1478    
1479    int query_user_online_info_by_uid(int32_t uid, USER_ONLINE_INFO *p_users, int *p_user_cnt, int start_id)
1480    {
1481            int left;
1482            int right;
1483            int mid;
1484            int32_t id;
1485            int ret = 0;
1486            int i;
1487            int user_cnt;
1488    
1489            if (p_users == NULL || p_user_cnt == NULL)
1490            {
1491                    log_error("NULL pointer error\n");
1492                    return -1;
1493            }
1494    
1495            user_cnt = *p_user_cnt;
1496            *p_user_cnt = 0;
1497    
1498            // acquire lock of user list
1499            if (user_list_rd_lock() < 0)
1500            {
1501                    log_error("user_list_rd_lock() error\n");
1502                    return -2;
1503            }
1504    
1505            left = start_id;
1506            right = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count - 1;
1507    
1508            while (left < right)
1509            {
1510                    mid = (left + right) / 2;
1511                    if (uid < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1512                    {
1513                            right = mid - 1;
1514                    }
1515                    else if (uid > p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1516                    {
1517                            left = mid + 1;
1518                    }
1519                    else // if (uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1520                    {
1521                            left = mid;
1522                            break;
1523                    }
1524            }
1525    
1526            if (uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].uid)
1527            {
1528                    right = left;
1529                    left = start_id;
1530    
1531                    while (left < right)
1532                    {
1533                            mid = (left + right) / 2;
1534                            if (uid - 1 < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1535                            {
1536                                    right = mid;
1537                            }
1538                            else // if (uid - 1 >= p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1539                            {
1540                                    left = mid + 1;
1541                            }
1542                    }
1543    
1544                    for (i = 0;
1545                             left < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count && i < user_cnt &&
1546                             uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].uid;
1547                             left++, i++)
1548                    {
1549                            id = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].id;
1550                            p_users[i] = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users[id];
1551                    }
1552    
1553                    if (i > 0)
1554                    {
1555                            *p_user_cnt = i;
1556                            ret = 1;
1557                    }
1558            }
1559    
1560            // release lock of user list
1561            if (user_list_rd_unlock() < 0)
1562          {          {
1563                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1564                  ret = -1;                  ret = -1;
# Line 909  int query_user_online_info(int32_t id, U Line 1566  int query_user_online_info(int32_t id, U
1566    
1567          return ret;          return ret;
1568  }  }
1569    
1570    int get_user_id_list(int32_t *p_uid_list, int *p_user_cnt, int start_uid)
1571    {
1572            int left;
1573            int right;
1574            int mid;
1575            int ret = 0;
1576            int i;
1577    
1578            if (p_uid_list == NULL || p_user_cnt == NULL)
1579            {
1580                    log_error("NULL pointer error\n");
1581                    return -1;
1582            }
1583    
1584            // acquire lock of user list
1585            if (user_list_rd_lock() < 0)
1586            {
1587                    log_error("user_list_rd_lock() error\n");
1588                    return -2;
1589            }
1590    
1591            left = 0;
1592            right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1593    
1594            while (left < right)
1595            {
1596                    mid = (left + right) / 2;
1597                    if (start_uid < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1598                    {
1599                            right = mid - 1;
1600                    }
1601                    else if (start_uid > p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1602                    {
1603                            left = mid + 1;
1604                    }
1605                    else // if (start_uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1606                    {
1607                            left = mid;
1608                            break;
1609                    }
1610            }
1611    
1612            for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count; i++)
1613            {
1614                    p_uid_list[i] = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left + i].uid;
1615            }
1616            *p_user_cnt = i;
1617    
1618            // release lock of user list
1619            if (user_list_rd_unlock() < 0)
1620            {
1621                    log_error("user_list_rd_unlock() error\n");
1622                    ret = -1;
1623            }
1624    
1625            return ret;
1626    }
1627    
1628    int user_stat_update(void)
1629    {
1630            return user_stat_map_update(&(p_user_list_pool->user_stat_map));
1631    }
1632    
1633    int user_article_cnt_inc(int32_t uid, int n)
1634    {
1635            return user_stat_article_cnt_inc(&(p_user_list_pool->user_stat_map), uid, n);
1636    }
1637    
1638    int get_user_article_cnt(int32_t uid)
1639    {
1640            const USER_STAT *p_stat;
1641            int ret;
1642    
1643            ret = user_stat_get(&(p_user_list_pool->user_stat_map), uid, &p_stat);
1644            if (ret < 0)
1645            {
1646                    log_error("user_stat_get(uid=%d) error: %d\n", uid, ret);
1647                    return -1;
1648            }
1649            else if (ret == 0) // user not found
1650            {
1651                    return -1;
1652            }
1653    
1654            return p_stat->article_count;
1655    }


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

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