/[LeafOK_CVS]/lbbs/src/user_list.c
ViewVC logotype

Contents of /lbbs/src/user_list.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.39 - (show annotations)
Thu Nov 20 01:54:18 2025 UTC (3 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.38: +138 -102 lines
Content type: text/x-csrc
Use POSIX shared object instead of SysV shared segment in user_list

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

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