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

Annotation of /lbbs/src/section_list_loader.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations)
Sat May 31 08:54:03 2025 UTC (9 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.13: +61 -0 lines
Content type: text/x-csrc
Support article / topic switch with UP/DOWN/PgUp/PgDn in article viewer

1 sysadm 1.1 /***************************************************************************
2     section_list_loader.c - description
3     -------------------
4     Copyright : (C) 2004-2025 by Leaflet
5     Email : leaflet@leafok.com
6     ***************************************************************************/
7    
8     /***************************************************************************
9     * *
10     * This program is free software; you can redistribute it and/or modify *
11     * it under the terms of the GNU General Public License as published by *
12     * the Free Software Foundation; either version 3 of the License, or *
13     * (at your option) any later version. *
14     * *
15     ***************************************************************************/
16    
17     #include "section_list_loader.h"
18 sysadm 1.11 #include "article_cache.h"
19 sysadm 1.1 #include "log.h"
20     #include "database.h"
21 sysadm 1.4 #include "menu.h"
22 sysadm 1.1 #include <stdio.h>
23     #include <string.h>
24     #include <errno.h>
25 sysadm 1.4 #include <signal.h>
26 sysadm 1.1 #include <stdlib.h>
27 sysadm 1.3 #include <strings.h>
28 sysadm 1.4 #include <unistd.h>
29    
30 sysadm 1.8 #define _POSIX_C_SOURCE 200809L
31     #include <string.h>
32    
33 sysadm 1.4 #define SECTION_LIST_LOAD_INTERVAL 10 // second
34    
35 sysadm 1.5 int section_list_loader_pid;
36     int last_article_op_log_mid;
37 sysadm 1.1
38 sysadm 1.2 int load_section_config_from_db(void)
39 sysadm 1.1 {
40 sysadm 1.2 MYSQL *db;
41 sysadm 1.1 MYSQL_RES *rs, *rs2;
42     MYSQL_ROW row, row2;
43     char sql[SQL_BUFFER_LEN];
44     int32_t sid;
45 sysadm 1.8 char master_list[(BBS_username_max_len + 1) * 3 + 1];
46 sysadm 1.1 SECTION_LIST *p_section;
47     int ret;
48    
49 sysadm 1.2 db = db_open();
50     if (db == NULL)
51     {
52     log_error("db_open() error: %s\n", mysql_error(db));
53     return -2;
54     }
55    
56 sysadm 1.1 snprintf(sql, sizeof(sql),
57 sysadm 1.2 "SELECT section_config.SID, sname, section_config.title, section_config.CID, "
58     "read_user_level, write_user_level, section_config.enable * section_class.enable AS enable "
59     "FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID "
60 sysadm 1.1 "ORDER BY section_config.SID");
61    
62     if (mysql_query(db, sql) != 0)
63     {
64     log_error("Query section_list error: %s\n", mysql_error(db));
65 sysadm 1.2 return -3;
66 sysadm 1.1 }
67     if ((rs = mysql_store_result(db)) == NULL)
68     {
69     log_error("Get section_list data failed\n");
70 sysadm 1.2 return -3;
71 sysadm 1.1 }
72 sysadm 1.2
73     ret = 0;
74 sysadm 1.1 while ((row = mysql_fetch_row(rs)))
75     {
76     sid = atoi(row[0]);
77    
78     // Query section master
79     snprintf(sql, sizeof(sql),
80     "SELECT username FROM section_master "
81     "INNER JOIN user_list ON section_master.UID = user_list.UID "
82     "WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) "
83 sysadm 1.8 "ORDER BY major DESC, begin_dt ASC LIMIT 3",
84 sysadm 1.1 sid);
85    
86     if (mysql_query(db, sql) != 0)
87     {
88     log_error("Query section_master error: %s\n", mysql_error(db));
89 sysadm 1.2 ret = -3;
90     break;
91 sysadm 1.1 }
92     if ((rs2 = mysql_store_result(db)) == NULL)
93     {
94     log_error("Get section_master data failed\n");
95 sysadm 1.2 ret = -3;
96     break;
97 sysadm 1.1 }
98 sysadm 1.8
99     master_list[0] = '\0';
100     while ((row2 = mysql_fetch_row(rs2)))
101 sysadm 1.1 {
102 sysadm 1.8 strncat(master_list, row2[0], sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
103     strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
104 sysadm 1.1 }
105     mysql_free_result(rs2);
106    
107     p_section = section_list_find_by_sid(sid);
108    
109     if (p_section == NULL)
110     {
111 sysadm 1.8 p_section = section_list_create(sid, row[1], row[2], master_list);
112 sysadm 1.1 if (p_section == NULL)
113     {
114 sysadm 1.2 log_error("section_list_create() error: load new section sid = %d sname = %s\n", sid, row[1]);
115     ret = -4;
116 sysadm 1.1 break;
117     }
118    
119     // acquire rw lock
120     ret = section_list_rw_lock(p_section);
121     if (ret < 0)
122     {
123     break;
124     }
125     }
126     else
127     {
128     // acquire rw lock
129     ret = section_list_rw_lock(p_section);
130     if (ret < 0)
131     {
132     break;
133     }
134    
135     strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1);
136     p_section->sname[sizeof(p_section->sname) - 1] = '\0';
137     strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);
138     p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
139 sysadm 1.8 strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
140     p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
141 sysadm 1.1 }
142    
143     p_section->class_id = atoi(row[3]);
144     p_section->read_user_level = atoi(row[4]);
145     p_section->write_user_level = atoi(row[5]);
146     p_section->enable = (int8_t)atoi(row[6]);
147    
148     // release rw lock
149     ret = section_list_rw_unlock(p_section);
150     if (ret < 0)
151     {
152     break;
153     }
154     }
155     mysql_free_result(rs);
156    
157 sysadm 1.2 mysql_close(db);
158    
159     return ret;
160 sysadm 1.1 }
161 sysadm 1.3
162 sysadm 1.9 int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit)
163 sysadm 1.3 {
164     MYSQL *db;
165     MYSQL_RES *rs;
166     MYSQL_ROW row;
167     char sql[SQL_BUFFER_LEN];
168     ARTICLE article;
169 sysadm 1.5 ARTICLE *p_topic;
170 sysadm 1.3 SECTION_LIST *p_section = NULL;
171     int32_t last_sid = 0;
172 sysadm 1.9 int article_count = 0;
173 sysadm 1.3 int ret = 0;
174     int i;
175    
176     db = db_open();
177     if (db == NULL)
178     {
179     log_error("db_open() error: %s\n", mysql_error(db));
180     return -2;
181     }
182    
183     snprintf(sql, sizeof(sql),
184 sysadm 1.11 "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
185     "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
186     "bbs_content.content "
187     "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
188     "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
189 sysadm 1.9 start_aid, article_count_limit);
190 sysadm 1.3
191     if (mysql_query(db, sql) != 0)
192     {
193     log_error("Query article list error: %s\n", mysql_error(db));
194     return -3;
195     }
196 sysadm 1.11 if ((rs = mysql_use_result(db)) == NULL)
197 sysadm 1.3 {
198     log_error("Get article list data failed\n");
199     return -3;
200     }
201    
202     // acquire global lock
203     if (global_lock)
204     {
205     if ((ret = section_list_rw_lock(NULL)) < 0)
206     {
207     log_error("section_list_rw_lock(sid = 0) error\n");
208     goto cleanup;
209     }
210     }
211    
212     while ((row = mysql_fetch_row(rs)))
213     {
214     bzero(&article, sizeof(ARTICLE));
215    
216     // copy data of article
217     i = 0;
218    
219     article.aid = atoi(row[i++]);
220     article.tid = atoi(row[i++]);
221     article.sid = atoi(row[i++]);
222     article.cid = atoi(row[i++]);
223     article.uid = atoi(row[i++]);
224     article.visible = (int8_t)atoi(row[i++]);
225     article.excerption = (int8_t)atoi(row[i++]);
226     article.ontop = (int8_t)atoi(row[i++]);
227     article.lock = (int8_t)atoi(row[i++]);
228 sysadm 1.5 article.transship = (int8_t)atoi(row[i++]);
229 sysadm 1.3
230     strncpy(article.username, row[i++], sizeof(article.username) - 1);
231     article.username[sizeof(article.username) - 1] = '\0';
232     strncpy(article.nickname, row[i++], sizeof(article.nickname) - 1);
233     article.nickname[sizeof(article.nickname) - 1] = '\0';
234     strncpy(article.title, row[i++], sizeof(article.title) - 1);
235     article.title[sizeof(article.title) - 1] = '\0';
236    
237     article.sub_dt = atol(row[i++]);
238    
239     // release lock of last section if different from current one
240     if (!global_lock && article.sid != last_sid && last_sid != 0)
241     {
242     if ((ret = section_list_rw_unlock(p_section)) < 0)
243     {
244 sysadm 1.11 log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
245 sysadm 1.3 break;
246     }
247     }
248    
249     if ((p_section = section_list_find_by_sid(article.sid)) == NULL)
250     {
251     log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", article.sid);
252 sysadm 1.4 ret = ERR_UNKNOWN_SECTION; // Unknown section found
253 sysadm 1.3 break;
254     }
255    
256 sysadm 1.5 if (article.visible != 0 && article.tid != 0)
257     {
258     // Check if topic article is visible
259     p_topic = article_block_find_by_aid(article.tid);
260     if (p_topic == NULL || p_topic->visible == 0)
261     {
262     // log_error("Set article (aid = %d) as invisible due to invisible or non-existing topic head\n", article.aid);
263     article.tid = 0;
264     article.visible = 0;
265     }
266     }
267    
268 sysadm 1.3 // acquire lock of current section if different from last one
269     if (!global_lock && article.sid != last_sid)
270     {
271 sysadm 1.4 if ((ret = section_list_rw_lock(p_section)) < 0)
272 sysadm 1.3 {
273 sysadm 1.11 log_error("section_list_rw_lock(sid=0) error\n");
274 sysadm 1.3 break;
275     }
276     }
277    
278     // append article to section list
279     last_sid = article.sid;
280    
281     if (section_list_append_article(p_section, &article) < 0)
282     {
283 sysadm 1.11 log_error("section_list_append_article(sid=%d, aid=%d) error\n",
284 sysadm 1.3 p_section->sid, article.aid);
285     ret = -3;
286     break;
287     }
288 sysadm 1.9
289     article_count++;
290    
291 sysadm 1.13 if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], 0) < 0)
292 sysadm 1.11 {
293     log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);
294     ret = -4;
295     break;
296     }
297 sysadm 1.3 }
298    
299     // release lock of last section
300     if (!global_lock && last_sid != 0)
301     {
302     if ((ret = section_list_rw_unlock(p_section)) < 0)
303     {
304 sysadm 1.11 log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
305 sysadm 1.3 }
306     }
307    
308     // release global lock
309     if (global_lock)
310     {
311     if ((ret = section_list_rw_unlock(NULL)) < 0)
312     {
313 sysadm 1.11 log_error("section_list_rw_unlock(sid=0) error\n");
314 sysadm 1.3 }
315     }
316    
317     cleanup:
318     mysql_free_result(rs);
319    
320     mysql_close(db);
321    
322 sysadm 1.9 return (ret < 0 ? ret : article_count);
323 sysadm 1.3 }
324 sysadm 1.4
325 sysadm 1.5 int set_last_article_op_log_from_db(void)
326     {
327     MYSQL *db;
328     MYSQL_RES *rs;
329     MYSQL_ROW row;
330     char sql[SQL_BUFFER_LEN];
331    
332     db = db_open();
333     if (db == NULL)
334     {
335     log_error("db_open() error: %s\n", mysql_error(db));
336     return -1;
337     }
338    
339     snprintf(sql, sizeof(sql),
340     "SELECT MID FROM bbs_article_op ORDER BY MID DESC LIMIT 1");
341    
342     if (mysql_query(db, sql) != 0)
343     {
344     log_error("Query article op error: %s\n", mysql_error(db));
345     return -2;
346     }
347     if ((rs = mysql_store_result(db)) == NULL)
348     {
349     log_error("Get article op data failed\n");
350     return -2;
351     }
352    
353     if ((row = mysql_fetch_row(rs)))
354     {
355     last_article_op_log_mid = atoi(row[0]);
356     }
357    
358     mysql_free_result(rs);
359    
360     mysql_close(db);
361    
362     return last_article_op_log_mid;
363     }
364    
365 sysadm 1.9 int apply_article_op_log_from_db(int op_count_limit)
366 sysadm 1.5 {
367     MYSQL *db;
368     MYSQL_RES *rs, *rs2;
369     MYSQL_ROW row, row2;
370     char sql[SQL_BUFFER_LEN];
371     ARTICLE *p_article;
372     SECTION_LIST *p_section = NULL;
373     SECTION_LIST *p_section_dest;
374     int32_t last_sid = 0;
375     int32_t sid_dest;
376 sysadm 1.9 int op_count = 0;
377 sysadm 1.5 int ret = 0;
378    
379     db = db_open();
380     if (db == NULL)
381     {
382     log_error("db_open() error: %s\n", mysql_error(db));
383     return -3;
384     }
385    
386     snprintf(sql, sizeof(sql),
387     "SELECT MID, AID, type FROM bbs_article_op "
388 sysadm 1.9 "WHERE MID > %d AND type NOT IN ('A') "
389     "ORDER BY MID LIMIT %d",
390     last_article_op_log_mid, op_count_limit);
391 sysadm 1.5
392     if (mysql_query(db, sql) != 0)
393     {
394     log_error("Query article log error: %s\n", mysql_error(db));
395     return -3;
396     }
397     if ((rs = mysql_store_result(db)) == NULL)
398     {
399     log_error("Get article log data failed\n");
400     return -3;
401     }
402    
403     while ((row = mysql_fetch_row(rs)))
404     {
405     p_article = article_block_find_by_aid(atoi(row[1]));
406     if (p_article == NULL) // related article has not been appended yet
407     {
408     ret = -2;
409     break;
410     }
411    
412     // release lock of last section if different from current one
413     if (p_article->sid != last_sid && last_sid != 0)
414     {
415     if ((ret = section_list_rw_unlock(p_section)) < 0)
416     {
417     log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
418     break;
419     }
420     }
421    
422     if ((p_section = section_list_find_by_sid(p_article->sid)) == NULL)
423     {
424     log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", p_article->sid);
425     ret = ERR_UNKNOWN_SECTION; // Unknown section found
426     break;
427     }
428    
429     // acquire lock of current section if different from last one
430     if (p_article->sid != last_sid)
431     {
432     if ((ret = section_list_rw_lock(p_section)) < 0)
433     {
434     log_error("section_list_rw_lock(sid = 0) error\n");
435     break;
436     }
437     }
438    
439     last_sid = p_article->sid;
440    
441     switch (row[2][0])
442     {
443     case 'A': // Add article
444     log_error("Operation type=A should not be found\n");
445     break;
446     case 'D': // Delete article
447     case 'X': // Delete article by Admin
448     p_article->visible = 0;
449     if (p_article->tid == 0)
450     {
451     // Set articles in the topic to be invisible
452     do
453     {
454     p_article = p_article->p_topic_next;
455     p_article->visible = 0;
456     } while (p_article->tid != 0);
457     }
458     break;
459     case 'S': // Restore article
460     p_article->visible = 1;
461     break;
462     case 'L': // Lock article
463     p_article->lock = 1;
464     break;
465     case 'U': // Unlock article
466     p_article->lock = 0;
467     break;
468     case 'M': // Modify article
469 sysadm 1.9 snprintf(sql, sizeof(sql),
470 sysadm 1.11 "SELECT bbs.CID, bbs_content.content "
471     "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
472     "WHERE bbs.AID = %d",
473 sysadm 1.9 p_article->aid);
474    
475     if (mysql_query(db, sql) != 0)
476     {
477     log_error("Query article error: %s\n", mysql_error(db));
478     ret = -3;
479     break;
480     }
481 sysadm 1.12 if ((rs2 = mysql_use_result(db)) == NULL)
482 sysadm 1.9 {
483     log_error("Get article data failed\n");
484     ret = -3;
485     break;
486     }
487     if ((row2 = mysql_fetch_row(rs2)))
488     {
489     p_article->cid = atoi(row2[0]);
490 sysadm 1.11
491 sysadm 1.13 if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[1], 0) < 0)
492 sysadm 1.11 {
493     log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
494     ret = -4;
495     }
496 sysadm 1.9 }
497     else
498     {
499     p_article->cid = 0;
500     ret = -4;
501     }
502     mysql_free_result(rs2);
503 sysadm 1.5 break;
504     case 'T': // Move article
505     snprintf(sql, sizeof(sql),
506     "SELECT SID FROM bbs WHERE AID = %d",
507     p_article->aid);
508    
509     if (mysql_query(db, sql) != 0)
510     {
511     log_error("Query article error: %s\n", mysql_error(db));
512     ret = -3;
513     break;
514     }
515     if ((rs2 = mysql_store_result(db)) == NULL)
516     {
517     log_error("Get article data failed\n");
518     ret = -3;
519     break;
520     }
521     if ((row2 = mysql_fetch_row(rs2)))
522     {
523     sid_dest = atoi(row2[0]);
524     }
525     else
526     {
527     sid_dest = 0;
528     ret = -4;
529     }
530     mysql_free_result(rs2);
531    
532     if (sid_dest > 0 && sid_dest != p_article->sid)
533     {
534     p_section_dest = section_list_find_by_sid(sid_dest);
535     if (p_section_dest == NULL)
536     {
537     ret = ERR_UNKNOWN_SECTION;
538     break;
539     }
540 sysadm 1.6 // acquire lock of dest section
541     if ((ret = section_list_rw_lock(p_section_dest)) < 0)
542     {
543     log_error("section_list_rw_lock(sid = %d) error\n", p_section_dest);
544     break;
545     }
546 sysadm 1.5 // Move topic
547     if ((ret = section_list_move_topic(p_section, p_section_dest, p_article->aid)) < 0)
548     {
549 sysadm 1.6 log_error("section_list_move_topic(src_sid=%d, dest_sid=%d, aid=%d) error (%d), retry in the next loop\n",
550     p_section->sid, p_section_dest->sid, p_article->aid, ret);
551     }
552     // release lock of dest section
553     if (section_list_rw_unlock(p_section_dest) < 0)
554     {
555     log_error("section_list_rw_unlock(sid = %d) error\n", p_section_dest);
556     ret = -1;
557 sysadm 1.5 }
558     }
559     break;
560     case 'E': // Set article as excerption
561     p_article->excerption = 1;
562     break;
563     case 'O': // Unset article as excerption
564     p_article->excerption = 0;
565     break;
566     case 'F': // Set article on top
567     p_article->ontop = 1;
568     break;
569     case 'V': // Unset article on top
570     p_article->ontop = 0;
571     break;
572     case 'Z': // Set article as trnasship
573     p_article->transship = 1;
574     break;
575     default:
576     // log_error("Operation type=%s unknown, mid=%s\n", row[2], row[0]);
577     break;
578     }
579    
580     if (ret < 0)
581     {
582     break;
583     }
584    
585     // Update MID with last successfully proceeded article_op_log
586     last_article_op_log_mid = atoi(row[0]);
587 sysadm 1.9
588     op_count++;
589 sysadm 1.5 }
590    
591     // release lock of last section
592     if (last_sid != 0)
593     {
594     if ((ret = section_list_rw_unlock(p_section)) < 0)
595     {
596     log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
597     }
598     }
599    
600     mysql_free_result(rs);
601    
602     mysql_close(db);
603    
604 sysadm 1.9 return (ret < 0 ? ret : op_count);
605 sysadm 1.5 }
606    
607 sysadm 1.4 int section_list_loader_launch(void)
608     {
609     int pid;
610     int ret;
611     int32_t last_aid;
612     int article_count;
613     int load_count;
614 sysadm 1.5 int last_mid;
615 sysadm 1.4 int i;
616    
617     if (section_list_loader_pid != 0)
618     {
619     log_error("section_list_loader already running, pid = %d\n", section_list_loader_pid);
620     return -2;
621     }
622    
623     pid = fork();
624    
625     if (pid > 0) // Parent process
626     {
627     SYS_child_process_count++;
628     section_list_loader_pid = pid;
629     log_std("Section list loader process (%d) start\n", pid);
630     return 0;
631     }
632     else if (pid < 0) // Error
633     {
634     log_error("fork() error (%d)\n", errno);
635     return -1;
636     }
637    
638     // Child process
639     SYS_child_process_count = 0;
640    
641     // Detach menu in shared memory
642     detach_menu_shm(p_bbs_menu);
643     free(p_bbs_menu);
644     p_bbs_menu = NULL;
645    
646     // Do section data loader periodically
647     while (!SYS_server_exit)
648     {
649     if (SYS_section_list_reload)
650     {
651     SYS_section_list_reload = 0;
652    
653     // Load section config
654     if (load_section_config_from_db() < 0)
655     {
656     log_error("load_section_config_from_db() error\n");
657     }
658     else
659     {
660     log_error("Reload section config successfully\n");
661     }
662     }
663    
664     // Load section articles
665     article_count = article_block_article_count();
666    
667 sysadm 1.9 do
668 sysadm 1.4 {
669 sysadm 1.9 last_aid = article_block_last_aid();
670 sysadm 1.4
671 sysadm 1.9 if ((ret = append_articles_from_db(last_aid + 1, 0, LOAD_ARTICLE_COUNT_LIMIT)) < 0)
672 sysadm 1.4 {
673 sysadm 1.9 log_error("append_articles_from_db(%d, 0, %d) error\n", last_aid + 1, LOAD_ARTICLE_COUNT_LIMIT);
674    
675     if (ret == ERR_UNKNOWN_SECTION)
676     {
677     SYS_section_list_reload = 1; // Force reload section_list
678     }
679 sysadm 1.4 }
680 sysadm 1.9 } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
681 sysadm 1.5
682     load_count = article_block_article_count() - article_count;
683     if (load_count > 0)
684     {
685     log_std("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());
686     }
687    
688     if (SYS_section_list_reload)
689     {
690     continue;
691     }
692    
693     // Load article_op log
694     last_mid = last_article_op_log_mid;
695    
696 sysadm 1.9 do
697 sysadm 1.4 {
698 sysadm 1.9 if ((ret = apply_article_op_log_from_db(LOAD_ARTICLE_COUNT_LIMIT)) < 0)
699     {
700     log_error("apply_article_op_log_from_db() error\n");
701 sysadm 1.4
702 sysadm 1.9 if (ret == ERR_UNKNOWN_SECTION)
703     {
704     SYS_section_list_reload = 1; // Force reload section_list
705     }
706 sysadm 1.4 }
707 sysadm 1.9 } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
708 sysadm 1.4
709 sysadm 1.5 if (last_article_op_log_mid > last_mid)
710     {
711     log_std("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);
712     }
713    
714     if (SYS_section_list_reload)
715     {
716     continue;
717     }
718    
719     for (i = 0; i < SECTION_LIST_LOAD_INTERVAL && !SYS_server_exit && !SYS_section_list_reload; i++)
720     {
721     sleep(1);
722 sysadm 1.4 }
723     }
724    
725     // Child process exit
726    
727     // Detach data pools shm
728     detach_section_list_shm();
729     detach_article_block_shm();
730     detach_trie_dict_shm();
731    
732     log_std("Section list loader process exit normally\n");
733     log_end();
734    
735     section_list_loader_pid = 0;
736    
737     _exit(0);
738    
739     return 0;
740     }
741    
742     int section_list_loader_reload(void)
743     {
744     if (section_list_loader_pid == 0)
745     {
746     log_error("section_list_loader not running\n");
747     return -2;
748     }
749    
750     if (kill(section_list_loader_pid, SIGHUP) < 0)
751     {
752     log_error("Send SIGTERM signal failed (%d)\n", errno);
753     return -1;
754     }
755    
756     return 0;
757     }
758 sysadm 1.7
759 sysadm 1.10 int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count)
760 sysadm 1.7 {
761     ARTICLE *p_article;
762     ARTICLE *p_next_page_first_article;
763     int ret = 0;
764    
765 sysadm 1.10 if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)
766 sysadm 1.7 {
767     log_error("query_section_articles() NULL pointer error\n");
768     return -1;
769     }
770    
771     // acquire lock of section
772     if ((ret = section_list_rd_lock(p_section)) < 0)
773     {
774     log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
775     return -2;
776     }
777    
778 sysadm 1.10 *p_page_count = p_section->page_count;
779    
780 sysadm 1.9 if (p_section->visible_article_count == 0)
781     {
782     *p_article_count = 0;
783     }
784     else if (page_id < 0 || page_id >= p_section->page_count)
785 sysadm 1.7 {
786 sysadm 1.8 log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);
787 sysadm 1.7 ret = -3;
788     }
789     else
790     {
791     ret = page_id;
792     p_article = p_section->p_page_first_article[page_id];
793     p_next_page_first_article =
794     (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);
795     *p_article_count = 0;
796    
797     do
798     {
799     if (p_article->visible)
800     {
801     p_articles[*p_article_count] = p_article;
802     (*p_article_count)++;
803     }
804     p_article = p_article->p_next;
805     } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);
806 sysadm 1.8
807     if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))
808     {
809     log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);
810     }
811 sysadm 1.7 }
812    
813     // release lock of section
814 sysadm 1.8 if (section_list_rd_unlock(p_section) < 0)
815 sysadm 1.7 {
816     log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
817     ret = -2;
818     }
819    
820     return ret;
821     }
822 sysadm 1.14
823     int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction,
824     int *p_page_id, int *p_offset, int *p_article_count)
825     {
826     ARTICLE *p_article = NULL;
827     ARTICLE *p_next;
828     int32_t aid = 0;
829     int ret = 0;
830    
831     if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_offset == NULL || p_article_count == NULL)
832     {
833     log_error("locate_article_in_section() NULL pointer error\n");
834     return -1;
835     }
836    
837     // acquire lock of section
838     if ((ret = section_list_rd_lock(p_section)) < 0)
839     {
840     log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
841     return -2;
842     }
843    
844     if (direction == 0)
845     {
846     aid = p_article_cur->aid;
847     }
848     else if (direction == 1)
849     {
850     aid = p_article_cur->p_topic_next->aid;
851     if (aid <= p_article_cur->aid)
852     {
853     aid = 0;
854     }
855     }
856     else if (direction == -1)
857     {
858     aid = p_article_cur->p_topic_prior->aid;
859     if (aid >= p_article_cur->aid)
860     {
861     aid = 0;
862     }
863     }
864    
865     if (aid > 0)
866     {
867     p_article = section_list_find_article_with_offset(p_section, aid, p_page_id, p_offset, &p_next);
868     if (p_article != NULL)
869     {
870     *p_article_count = (*p_page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
871     }
872     }
873    
874     // release lock of section
875     if (section_list_rd_unlock(p_section) < 0)
876     {
877     log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
878     ret = -2;
879     }
880    
881     return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
882     }

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