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

Diff of /lbbs/src/section_list_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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


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

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