| 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 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "common.h" |
#include "common.h" |
| 10 |
#include "database.h" |
#include "database.h" |
| 33 |
}; |
}; |
| 34 |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
| 35 |
|
|
| 36 |
#define USER_LIST_TRY_LOCK_WAIT_TIME 1 // second |
enum _user_list_constant_t |
| 37 |
#define USER_LIST_TRY_LOCK_TIMES 10 |
{ |
| 38 |
|
USER_LIST_TRY_LOCK_WAIT_TIME = 1, // second |
| 39 |
|
USER_LIST_TRY_LOCK_TIMES = 10, |
| 40 |
|
}; |
| 41 |
|
|
| 42 |
struct user_list_pool_t |
struct user_list_pool_t |
| 43 |
{ |
{ |
| 50 |
USER_ONLINE_LIST *p_online_current; |
USER_ONLINE_LIST *p_online_current; |
| 51 |
USER_ONLINE_LIST *p_online_new; |
USER_ONLINE_LIST *p_online_new; |
| 52 |
USER_STAT_MAP user_stat_map; |
USER_STAT_MAP user_stat_map; |
| 53 |
|
int user_login_count; |
| 54 |
}; |
}; |
| 55 |
typedef struct user_list_pool_t USER_LIST_POOL; |
typedef struct user_list_pool_t USER_LIST_POOL; |
| 56 |
|
|
| 89 |
static int user_list_rw_lock(int semid); |
static int user_list_rw_lock(int semid); |
| 90 |
|
|
| 91 |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
| 92 |
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); |
| 93 |
|
static int user_login_count_load(MYSQL *db); |
| 94 |
|
|
| 95 |
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) |
| 96 |
{ |
{ |
| 235 |
return ret; |
return ret; |
| 236 |
} |
} |
| 237 |
|
|
| 238 |
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) |
| 239 |
{ |
{ |
| 240 |
MYSQL_RES *rs = NULL; |
MYSQL_RES *rs = NULL; |
| 241 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 246 |
int user_cnt; |
int user_cnt; |
| 247 |
int guest_cnt; |
int guest_cnt; |
| 248 |
|
|
| 249 |
if (db == NULL || p_list == NULL) |
if (db == NULL || p_online_list == NULL) |
| 250 |
{ |
{ |
| 251 |
log_error("NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 252 |
return -1; |
return -1; |
| 288 |
user_cnt++; |
user_cnt++; |
| 289 |
} |
} |
| 290 |
|
|
| 291 |
p_list->users[i].id = i; |
p_online_list->users[i].id = i; |
| 292 |
strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1); |
strncpy(p_online_list->users[i].session_id, row[0], sizeof(p_online_list->users[i].session_id) - 1); |
| 293 |
p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0'; |
p_online_list->users[i].session_id[sizeof(p_online_list->users[i].session_id) - 1] = '\0'; |
| 294 |
|
|
| 295 |
if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_list->users[i].user_info))) <= 0) |
if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_online_list->users[i].user_info), NULL, 0)) <= 0) |
| 296 |
{ |
{ |
| 297 |
log_error("query_user_info_by_uid(%d) error\n", atoi(row[1])); |
log_error("query_user_info_by_uid(%d) error\n", atoi(row[1])); |
| 298 |
continue; |
continue; |
| 299 |
} |
} |
| 300 |
|
|
| 301 |
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); |
| 302 |
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'; |
| 303 |
|
|
| 304 |
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); |
| 305 |
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'; |
| 306 |
p_list->users[i].current_action_title = NULL; |
p_online_list->users[i].current_action_title = NULL; |
| 307 |
if (p_list->users[i].current_action[0] == '\0') |
if (p_online_list->users[i].current_action[0] == '\0') |
| 308 |
{ |
{ |
| 309 |
p_list->users[i].current_action_title = ""; |
p_online_list->users[i].current_action_title = ""; |
| 310 |
} |
} |
| 311 |
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) |
| 312 |
{ |
{ |
| 313 |
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", |
| 314 |
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); |
| 315 |
continue; |
continue; |
| 316 |
} |
} |
| 317 |
|
|
| 318 |
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])); |
| 319 |
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])); |
| 320 |
|
|
| 321 |
i++; |
i++; |
| 322 |
if (i >= BBS_max_user_online_count) |
if (i >= BBS_max_user_online_count) |
| 333 |
// Rebuild index |
// Rebuild index |
| 334 |
for (j = 0; j < user_cnt; j++) |
for (j = 0; j < user_cnt; j++) |
| 335 |
{ |
{ |
| 336 |
p_list->index_uid[j].uid = p_list->users[j].user_info.uid; |
p_online_list->index_uid[j].uid = p_online_list->users[j].user_info.uid; |
| 337 |
p_list->index_uid[j].id = j; |
p_online_list->index_uid[j].id = j; |
| 338 |
} |
} |
| 339 |
|
|
| 340 |
qsort(p_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
qsort(p_online_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
|
|
|
|
#ifdef _DEBUG |
|
|
log_error("Rebuild index of %d online users\n", user_cnt); |
|
|
#endif |
|
| 341 |
} |
} |
| 342 |
|
|
| 343 |
p_list->user_count = user_cnt; |
p_online_list->user_count = user_cnt; |
| 344 |
p_list->guest_count = guest_cnt; |
p_online_list->guest_count = guest_cnt; |
|
|
|
|
#ifdef _DEBUG |
|
|
log_error("Loaded %d online users and %d guest users\n", p_list->user_count, p_list->guest_count); |
|
|
#endif |
|
| 345 |
|
|
| 346 |
cleanup: |
cleanup: |
| 347 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 349 |
return ret; |
return ret; |
| 350 |
} |
} |
| 351 |
|
|
| 352 |
|
int user_login_count_load(MYSQL *db) |
| 353 |
|
{ |
| 354 |
|
MYSQL_RES *rs = NULL; |
| 355 |
|
MYSQL_ROW row; |
| 356 |
|
char sql[SQL_BUFFER_LEN]; |
| 357 |
|
|
| 358 |
|
if (db == NULL) |
| 359 |
|
{ |
| 360 |
|
log_error("NULL pointer error\n"); |
| 361 |
|
return -1; |
| 362 |
|
} |
| 363 |
|
|
| 364 |
|
snprintf(sql, sizeof(sql), |
| 365 |
|
"SELECT ID FROM user_login_log ORDER BY ID DESC LIMIT 1"); |
| 366 |
|
if (mysql_query(db, sql) != 0) |
| 367 |
|
{ |
| 368 |
|
log_error("Query user_login_log error: %s\n", mysql_error(db)); |
| 369 |
|
return -2; |
| 370 |
|
} |
| 371 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 372 |
|
{ |
| 373 |
|
log_error("Get user_login_log data failed\n"); |
| 374 |
|
return -2; |
| 375 |
|
} |
| 376 |
|
if ((row = mysql_fetch_row(rs))) |
| 377 |
|
{ |
| 378 |
|
p_user_list_pool->user_login_count = atoi(row[0]); |
| 379 |
|
} |
| 380 |
|
mysql_free_result(rs); |
| 381 |
|
|
| 382 |
|
return 0; |
| 383 |
|
} |
| 384 |
|
|
| 385 |
int user_list_pool_init(const char *filename) |
int user_list_pool_init(const char *filename) |
| 386 |
{ |
{ |
| 387 |
int shmid; |
int shmid; |
| 498 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 499 |
} |
} |
| 500 |
|
|
| 501 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 502 |
{ |
{ |
| 503 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 504 |
} |
} |
| 580 |
ret = -2; |
ret = -2; |
| 581 |
goto cleanup; |
goto cleanup; |
| 582 |
} |
} |
| 583 |
|
|
| 584 |
|
if (user_login_count_load(db) < 0) |
| 585 |
|
{ |
| 586 |
|
log_error("user_login_count_load() error\n"); |
| 587 |
|
ret = -2; |
| 588 |
|
goto cleanup; |
| 589 |
|
} |
| 590 |
} |
} |
| 591 |
else |
else |
| 592 |
{ |
{ |
| 955 |
return 0; |
return 0; |
| 956 |
} |
} |
| 957 |
|
|
| 958 |
|
int get_user_login_count(int *p_login_cnt) |
| 959 |
|
{ |
| 960 |
|
if (p_login_cnt == NULL) |
| 961 |
|
{ |
| 962 |
|
log_error("NULL pointer error\n"); |
| 963 |
|
return -1; |
| 964 |
|
} |
| 965 |
|
|
| 966 |
|
*p_login_cnt = p_user_list_pool->user_login_count; |
| 967 |
|
|
| 968 |
|
return 0; |
| 969 |
|
} |
| 970 |
|
|
| 971 |
int query_user_info(int32_t id, USER_INFO *p_user) |
int query_user_info(int32_t id, USER_INFO *p_user) |
| 972 |
{ |
{ |
| 973 |
int ret = 0; |
int ret = 0; |
| 1001 |
return ret; |
return ret; |
| 1002 |
} |
} |
| 1003 |
|
|
| 1004 |
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) |
| 1005 |
{ |
{ |
| 1006 |
int left; |
int left; |
| 1007 |
int right; |
int right; |
| 1048 |
id = p_user_list_pool->p_current->index_uid[left].id; |
id = p_user_list_pool->p_current->index_uid[left].id; |
| 1049 |
*p_user = p_user_list_pool->p_current->users[id]; |
*p_user = p_user_list_pool->p_current->users[id]; |
| 1050 |
ret = 1; |
ret = 1; |
| 1051 |
|
|
| 1052 |
|
if (p_intro_buf != NULL) |
| 1053 |
|
{ |
| 1054 |
|
strncpy(p_intro_buf, p_user_list_pool->p_current->users[id].intro, intro_buf_len - 1); |
| 1055 |
|
p_intro_buf[intro_buf_len - 1] = '\0'; |
| 1056 |
|
p_user->intro = p_intro_buf; |
| 1057 |
|
} |
| 1058 |
} |
} |
| 1059 |
|
|
| 1060 |
// release lock of user list |
// release lock of user list |
| 1061 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1062 |
{ |
{ |
| 1063 |
|
log_error("user_list_rd_unlock() error\n"); |
| 1064 |
|
ret = -1; |
| 1065 |
|
} |
| 1066 |
|
|
| 1067 |
|
return ret; |
| 1068 |
|
} |
| 1069 |
|
|
| 1070 |
|
int query_user_info_by_username(const char *username_prefix, int max_user_cnt, |
| 1071 |
|
int32_t uid_list[], char username_list[][BBS_username_max_len + 1]) |
| 1072 |
|
{ |
| 1073 |
|
int left; |
| 1074 |
|
int right; |
| 1075 |
|
int mid; |
| 1076 |
|
int left_save; |
| 1077 |
|
int ret = 0; |
| 1078 |
|
size_t prefix_len; |
| 1079 |
|
int comp; |
| 1080 |
|
int i; |
| 1081 |
|
|
| 1082 |
|
if (username_prefix == NULL || uid_list == NULL || username_list == NULL) |
| 1083 |
|
{ |
| 1084 |
|
log_error("NULL pointer error\n"); |
| 1085 |
|
return -1; |
| 1086 |
|
} |
| 1087 |
|
|
| 1088 |
|
prefix_len = strlen(username_prefix); |
| 1089 |
|
|
| 1090 |
|
// acquire lock of user list |
| 1091 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 1092 |
|
{ |
| 1093 |
|
log_error("user_list_rd_lock() error\n"); |
| 1094 |
|
return -2; |
| 1095 |
|
} |
| 1096 |
|
|
| 1097 |
|
left = 0; |
| 1098 |
|
right = p_user_list_pool->p_current->user_count - 1; |
| 1099 |
|
|
| 1100 |
|
while (left < right) |
| 1101 |
|
{ |
| 1102 |
|
mid = (left + right) / 2; |
| 1103 |
|
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
| 1104 |
|
if (comp < 0) |
| 1105 |
|
{ |
| 1106 |
|
right = mid - 1; |
| 1107 |
|
} |
| 1108 |
|
else if (comp > 0) |
| 1109 |
|
{ |
| 1110 |
|
left = mid + 1; |
| 1111 |
|
} |
| 1112 |
|
else // if (comp == 0) |
| 1113 |
|
{ |
| 1114 |
|
left = mid; |
| 1115 |
|
break; |
| 1116 |
|
} |
| 1117 |
|
} |
| 1118 |
|
|
| 1119 |
|
if (strncasecmp(username_prefix, p_user_list_pool->p_current->users[left].username, prefix_len) == 0) // Found |
| 1120 |
|
{ |
| 1121 |
|
#ifdef _DEBUG |
| 1122 |
|
log_error("Debug: match found, pos=%d\n", left); |
| 1123 |
|
#endif |
| 1124 |
|
|
| 1125 |
|
left_save = left; |
| 1126 |
|
right = left; |
| 1127 |
|
left = 0; |
| 1128 |
|
|
| 1129 |
|
while (left < right) |
| 1130 |
|
{ |
| 1131 |
|
mid = (left + right) / 2; |
| 1132 |
|
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
| 1133 |
|
if (comp > 0) |
| 1134 |
|
{ |
| 1135 |
|
left = mid + 1; |
| 1136 |
|
} |
| 1137 |
|
else if (comp == 0) |
| 1138 |
|
{ |
| 1139 |
|
right = mid; |
| 1140 |
|
} |
| 1141 |
|
else // if (comp < 0) |
| 1142 |
|
{ |
| 1143 |
|
log_error("Bug: left=%d right=%d mid=%d"); |
| 1144 |
|
ret = -2; |
| 1145 |
|
goto cleanup; |
| 1146 |
|
} |
| 1147 |
|
} |
| 1148 |
|
|
| 1149 |
|
#ifdef _DEBUG |
| 1150 |
|
log_error("Debug: first match found, pos=%d\n", right); |
| 1151 |
|
#endif |
| 1152 |
|
|
| 1153 |
|
left = left_save; |
| 1154 |
|
left_save = right; |
| 1155 |
|
right = p_user_list_pool->p_current->user_count - 1; |
| 1156 |
|
|
| 1157 |
|
while (left < right) |
| 1158 |
|
{ |
| 1159 |
|
mid = (left + right) / 2 + (left + right) % 2; |
| 1160 |
|
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
| 1161 |
|
if (comp < 0) |
| 1162 |
|
{ |
| 1163 |
|
right = mid - 1; |
| 1164 |
|
} |
| 1165 |
|
else if (comp == 0) |
| 1166 |
|
{ |
| 1167 |
|
left = mid; |
| 1168 |
|
} |
| 1169 |
|
else // if (comp > 0) |
| 1170 |
|
{ |
| 1171 |
|
log_error("Bug: left=%d right=%d mid=%d"); |
| 1172 |
|
ret = -2; |
| 1173 |
|
goto cleanup; |
| 1174 |
|
} |
| 1175 |
|
} |
| 1176 |
|
|
| 1177 |
|
#ifdef _DEBUG |
| 1178 |
|
log_error("Debug: last match found, pos=%d\n", left); |
| 1179 |
|
#endif |
| 1180 |
|
|
| 1181 |
|
right = left; |
| 1182 |
|
left = left_save; |
| 1183 |
|
|
| 1184 |
|
for (i = 0; i < max_user_cnt && left + i <= right; i++) |
| 1185 |
|
{ |
| 1186 |
|
uid_list[i] = p_user_list_pool->p_current->users[left + i].uid; |
| 1187 |
|
strncpy(username_list[i], p_user_list_pool->p_current->users[left + i].username, sizeof(username_list[i]) - 1); |
| 1188 |
|
username_list[i][sizeof(username_list[i]) - 1] = '\0'; |
| 1189 |
|
} |
| 1190 |
|
ret = i; |
| 1191 |
|
} |
| 1192 |
|
|
| 1193 |
|
cleanup: |
| 1194 |
|
// release lock of user list |
| 1195 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1196 |
|
{ |
| 1197 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |
| 1198 |
ret = -1; |
ret = -1; |
| 1199 |
} |
} |