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


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

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