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

Diff of /lbbs/src/main.c

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

Revision 1.44 by sysadm, Tue May 27 01:56:16 2025 UTC Revision 1.92 by sysadm, Tue Dec 23 13:08:13 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    main.c  -  description  /*
3                                                           -------------------   * main
4          Copyright            : (C) 2004-2025 by Leaflet   *   - entry of server program
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "init.h"  #include "bwf.h"
15  #include "common.h"  #include "common.h"
16  #include "net_server.h"  #include "file_loader.h"
17  #include "log.h"  #include "init.h"
18  #include "io.h"  #include "io.h"
19    #include "lml.h"
20    #include "log.h"
21  #include "menu.h"  #include "menu.h"
22  #include "file_loader.h"  #include "net_server.h"
23  #include "section_list_loader.h"  #include "section_list_loader.h"
24  #include <stdlib.h>  #include "user_list.h"
25    #include <errno.h>
26    #include <libgen.h>
27    #include <locale.h>
28  #include <signal.h>  #include <signal.h>
29    #include <stdio.h>
30    #include <stdlib.h>
31  #include <string.h>  #include <string.h>
32  #include <unistd.h>  #include <unistd.h>
 #include <libgen.h>  
 #include <sys/types.h>  
33  #include <sys/stat.h>  #include <sys/stat.h>
34    #include <sys/types.h>
35    #include <sys/wait.h>
36    
37    typedef void (*arg_option_handler_t)(void);
38    
39  void app_help(void)  struct arg_option_t
40  {  {
41          prints("Usage: bbsd [-fhv] [...]\n\n"          const char *short_arg;
42                     "-f\t--foreground\t\tForce program run in foreground\n"          const char *long_arg;
43                     "-h\t--help\t\t\tDisplay this help message\n"          const char *description;
44                     "-v\t--version\t\tDisplay version information\n"          arg_option_handler_t handler;
45                     "\t--display-log\t\tDisplay standard log information\n"  };
46                     "\t--display-error-log\tDisplay error log information\n"  typedef struct arg_option_t ARG_OPTION;
47                     "\n    If meet any bug, please report to <leaflet@leafok.com>\n\n");  
48    static void arg_foreground(void);
49    static void arg_help(void);
50    static void arg_version(void);
51    static void arg_display_log(void);
52    static void arg_display_error_log(void);
53    static void arg_compile_info(void);
54    static void arg_error(void);
55    
56    static const ARG_OPTION arg_options[] = {
57            {"-f", "--foreground", "Run in foreground", arg_foreground},
58            {"-h", "--help", "Display help message", arg_help},
59            {"-v", "--version", "Display version information", arg_version},
60            {NULL, "--display-log", "Display standard log information", arg_display_log},
61            {NULL, "--display-error-log", "Display error log information", arg_display_error_log},
62            {"-C", "--compile-config", "Display compile configuration", arg_compile_info}};
63    
64    static const int arg_options_count = sizeof(arg_options) / sizeof(ARG_OPTION);
65    
66    static int daemon_service = 1;
67    static int std_log_redir = 0;
68    static int error_log_redir = 0;
69    
70    static void arg_foreground(void)
71    {
72            daemon_service = 0;
73  }  }
74    
75  void arg_error(void)  static void arg_help(void)
76  {  {
77          prints("Invalid arguments\n");          fprintf(stderr, "Usage: bbsd [-fhv] [...]\n\n");
78          app_help();  
79            for (int i = 0; i < arg_options_count; i++)
80            {
81                    fprintf(stderr, "%s%*s%s%*s%s\n",
82                                    arg_options[i].short_arg ? arg_options[i].short_arg : "",
83                                    8 - (arg_options[i].short_arg ? (int)strlen(arg_options[i].short_arg) : 0), "",
84                                    arg_options[i].long_arg ? arg_options[i].long_arg : "",
85                                    24 - (arg_options[i].long_arg ? (int)strlen(arg_options[i].long_arg) : 0), "",
86                                    arg_options[i].description ? arg_options[i].description : "");
87            }
88    
89            fprintf(stderr, "\n    If meet any bug, please report to <leaflet@leafok.com>\n\n");
90    }
91    
92    static void arg_version(void)
93    {
94            printf("%s\n", APP_INFO);
95            printf("%s\n", COPYRIGHT_INFO);
96    }
97    
98    static void arg_display_log(void)
99    {
100            std_log_redir = 1;
101    }
102    
103    static void arg_display_error_log(void)
104    {
105            error_log_redir = 1;
106    }
107    
108    static void arg_compile_info(void)
109    {
110            printf("%s\n"
111                       "--enable-shared\t\t[%s]\n"
112                       "--enable-systemd\t[%s]\n"
113                       "--with-debug\t\t[%s]\n"
114                       "--with-epoll\t\t[%s]\n"
115                       "--with-mariadb\t\t[%s]\n"
116                       "--with-sysv\t\t[%s]\n",
117                       APP_INFO,
118    #ifdef _ENABLE_SHARED
119                       "yes",
120    #else
121                       "no",
122    #endif
123    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
124                       "yes",
125    #else
126                       "no",
127    #endif
128    #ifdef _DEBUG
129                       "yes",
130    #else
131                       "no",
132    #endif
133    #ifdef HAVE_SYS_EPOLL_H
134                       "yes",
135    #else
136                       "no",
137    #endif
138    #ifdef HAVE_MARIADB_CLIENT
139                       "yes",
140    #else
141                       "no",
142    #endif
143    #ifdef HAVE_SYSTEM_V
144                       "yes"
145    #else
146                       "no"
147    #endif
148            );
149    }
150    
151    static void arg_error(void)
152    {
153            fprintf(stderr, "Invalid arguments\n");
154            arg_help();
155  }  }
156    
157  int main(int argc, char *argv[])  int main(int argc, char *argv[])
158  {  {
159          char file_path_temp[FILE_PATH_LEN];          char file_path_temp[FILE_PATH_LEN];
         int daemon = 1;  
         int std_log_redir = 0;  
         int error_log_redir = 0;  
160          FILE *fp;          FILE *fp;
161            int ret;
162            int last_aid;
163            struct sigaction act = {0};
164            int i;
165            int j;
166            struct stat file_stat;
167    
168          // Parse args          // Parse args
169          for (int i = 1; i < argc; i++)          for (i = 1; i < argc; i++)
170          {          {
171                  switch (argv[i][0])                  for (j = 0; j < arg_options_count; j++)
172                  {                  {
173                  case '-':                          if (arg_options[j].short_arg && strcmp(argv[i], arg_options[j].short_arg) == 0)
                         if (argv[i][1] != '-')  
174                          {                          {
175                                  for (int j = 1; j < strlen(argv[i]); j++)                                  arg_options[j].handler();
176                                  {                                  break;
                                         switch (argv[i][j])  
                                         {  
                                         case 'f':  
                                                 daemon = 0;  
                                                 break;  
                                         case 'h':  
                                                 app_help();  
                                                 return 0;  
                                         case 'v':  
                                                 puts(app_version);  
                                                 return 0;  
                                         default:  
                                                 arg_error();  
                                                 return 1;  
                                         }  
                                 }  
177                          }                          }
178                          else                          else if (arg_options[j].long_arg && strcmp(argv[i], arg_options[j].long_arg) == 0)
179                          {                          {
180                                  if (strcmp(argv[i] + 2, "foreground") == 0)                                  arg_options[j].handler();
181                                  {                                  break;
                                         daemon = 0;  
                                         break;  
                                 }  
                                 if (strcmp(argv[i] + 2, "help") == 0)  
                                 {  
                                         app_help();  
                                         return 0;  
                                 }  
                                 if (strcmp(argv[i] + 2, "version") == 0)  
                                 {  
                                         puts(app_version);  
                                         return 0;  
                                 }  
                                 if (strcmp(argv[i] + 2, "display-log") == 0)  
                                 {  
                                         std_log_redir = 1;  
                                 }  
                                 if (strcmp(argv[i] + 2, "display-error-log") == 0)  
                                 {  
                                         error_log_redir = 1;  
                                 }  
182                          }                          }
183                          break;                  }
184                    if (j == arg_options_count)
185                    {
186                            arg_error();
187                            return -1;
188                  }                  }
189          }          }
190    
191          // Initialize daemon          // Initialize daemon
192          if (daemon)          if (daemon_service)
193          {          {
194                  init_daemon();                  init_daemon();
195          }          }
# Line 123  int main(int argc, char *argv[]) Line 198  int main(int argc, char *argv[])
198          strncpy(file_path_temp, argv[0], sizeof(file_path_temp) - 1);          strncpy(file_path_temp, argv[0], sizeof(file_path_temp) - 1);
199          file_path_temp[sizeof(file_path_temp) - 1] = '\0';          file_path_temp[sizeof(file_path_temp) - 1] = '\0';
200    
201          chdir(dirname(file_path_temp));          if (chdir(dirname(file_path_temp)) < 0)
202          chdir("..");          {
203                    fprintf(stderr, "chdir(%s) error: %d\n", dirname(file_path_temp), errno);
204                    return -1;
205            }
206            if (chdir("..") < 0)
207            {
208                    fprintf(stderr, "chdir(..) error: %d\n", errno);
209                    return -1;
210            }
211    
212            // Apply the specified locale
213            if (setlocale(LC_ALL, "en_US.UTF-8") == NULL)
214            {
215                    fprintf(stderr, "setlocale(LC_ALL, en_US.UTF-8) error\n");
216                    return -1;
217            }
218    
219          // Initialize log          // Initialize log
220          if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0)          if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0)
# Line 132  int main(int argc, char *argv[]) Line 222  int main(int argc, char *argv[])
222                  return -1;                  return -1;
223          }          }
224    
225          if ((!daemon) && std_log_redir)          if ((!daemon_service) && std_log_redir)
226          {          {
227                  log_std_redirect(STDERR_FILENO);                  log_common_redir(STDERR_FILENO);
228          }          }
229          if ((!daemon) && error_log_redir)          if ((!daemon_service) && error_log_redir)
230          {          {
231                  log_err_redirect(STDERR_FILENO);                  log_error_redir(STDERR_FILENO);
232          }          }
233    
234            log_common("Starting %s", APP_INFO);
235    
236          // Load configuration          // Load configuration
237          if (load_conf(CONF_BBSD) < 0)          if (load_conf(CONF_BBSD) < 0)
238          {          {
239                  return -2;                  return -2;
240          }          }
241    
242          // Initialize data pools          // Load BWF config
243          if ((fp = fopen(VAR_TRIE_DICT_SHM, "w")) == NULL)          if (bwf_load(CONF_BWF) < 0)
244          {          {
245                  log_error("fopen(%s) error\n", VAR_TRIE_DICT_SHM);                  return -2;
246            }
247    
248            // Get EULA modification tm
249            if (stat(DATA_EULA, &file_stat) == -1)
250            {
251                    log_error("stat(%s) error", DATA_EULA, errno);
252                  goto cleanup;                  goto cleanup;
253          }          }
254          fclose(fp);          BBS_eula_tm = file_stat.st_mtim.tv_sec;
255    
256            // Check article cache dir
257            ret = mkdir(VAR_ARTICLE_CACHE_DIR, 0750);
258            if (ret == -1 && errno != EEXIST)
259            {
260                    log_error("mkdir(%s) error (%d)", VAR_ARTICLE_CACHE_DIR, errno);
261                    goto cleanup;
262            }
263    
264            // Check section aid location dir
265            ret = mkdir(VAR_SECTION_AID_LOC_DIR, 0750);
266            if (ret == -1 && errno != EEXIST)
267            {
268                    log_error("mkdir(%s) error (%d)", VAR_SECTION_AID_LOC_DIR, errno);
269                    goto cleanup;
270            }
271    
272            // Initialize data pools
273          if ((fp = fopen(VAR_ARTICLE_BLOCK_SHM, "w")) == NULL)          if ((fp = fopen(VAR_ARTICLE_BLOCK_SHM, "w")) == NULL)
274          {          {
275                  log_error("fopen(%s) error\n", VAR_ARTICLE_BLOCK_SHM);                  log_error("fopen(%s) error", VAR_ARTICLE_BLOCK_SHM);
276                  goto cleanup;                  goto cleanup;
277          }          }
278          fclose(fp);          fclose(fp);
279          if ((fp = fopen(VAR_SECTION_LIST_SHM, "w")) == NULL)          if ((fp = fopen(VAR_SECTION_LIST_SHM, "w")) == NULL)
280          {          {
281                  log_error("fopen(%s) error\n", VAR_SECTION_LIST_SHM);                  log_error("fopen(%s) error", VAR_SECTION_LIST_SHM);
282                    goto cleanup;
283            }
284            fclose(fp);
285            if ((fp = fopen(VAR_TRIE_DICT_SHM, "w")) == NULL)
286            {
287                    log_error("fopen(%s) error", VAR_TRIE_DICT_SHM);
288                    goto cleanup;
289            }
290            fclose(fp);
291            if ((fp = fopen(VAR_USER_LIST_SHM, "w")) == NULL)
292            {
293                    log_error("fopen(%s) error", VAR_USER_LIST_SHM);
294                  goto cleanup;                  goto cleanup;
295          }          }
296          fclose(fp);          fclose(fp);
297    
298          if (trie_dict_init(VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL) < 0)          if (trie_dict_init(VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL) < 0)
299          {          {
300                  printf("trie_dict_init failed\n");                  log_error("trie_dict_init(%s, %d) error", VAR_TRIE_DICT_SHM, TRIE_NODE_PER_POOL);
301                  goto cleanup;                  goto cleanup;
302          }          }
303          if (article_block_init(VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK) < 0)          if (article_block_init(VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block) < 0)
304          {          {
305                  log_error("article_block_init(%s, %d) error\n", VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK);                  log_error("article_block_init(%s, %d) error", VAR_ARTICLE_BLOCK_SHM, BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block);
306                  goto cleanup;                  goto cleanup;
307          }          }
308          if (section_list_init(VAR_SECTION_LIST_SHM) < 0)          if (section_list_init(VAR_SECTION_LIST_SHM) < 0)
309          {          {
310                  log_error("section_list_pool_init(%s) error\n", VAR_SECTION_LIST_SHM);                  log_error("section_list_pool_init(%s) error", VAR_SECTION_LIST_SHM);
311                    goto cleanup;
312            }
313    
314            // Init LML module
315            if (lml_init() < 0)
316            {
317                    log_error("lml_init() error");
318                  goto cleanup;                  goto cleanup;
319          }          }
320    
321          // Load BBS cmd          // Load BBS cmd
322          if (load_cmd() < 0)          if (load_cmd() < 0)
323          {          {
324                    log_error("load_cmd() error");
325                  goto cleanup;                  goto cleanup;
326          }          }
327    
328          // Load menus          // Load menus
329          p_bbs_menu = calloc(1, sizeof(MENU_SET));          if (load_menu(&bbs_menu, CONF_MENU) < 0)
         if (p_bbs_menu == NULL)  
330          {          {
331                  log_error("OOM: calloc(MENU_SET)\n");                  log_error("load_menu(bbs_menu) error");
332                  goto cleanup;                  goto cleanup;
333          }          }
334          if (load_menu(p_bbs_menu, CONF_MENU) < 0)          if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0)
335          {          {
336                    log_error("load_menu(top10_menu) error");
337                  goto cleanup;                  goto cleanup;
338          }          }
339            top10_menu.allow_exit = 1;
340    
341          // Load data files          // Load data files
         if (file_loader_init() < 0)  
         {  
                 log_error("file_loader_init() error\n");  
                 goto cleanup;  
         }  
342          for (int i = 0; i < data_files_load_startup_count; i++)          for (int i = 0; i < data_files_load_startup_count; i++)
343          {          {
344                  if (load_file(data_files_load_startup[i]) < 0)                  if (load_file(data_files_load_startup[i]) < 0)
345                  {                  {
346                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                          log_error("load_file(%s) error", data_files_load_startup[i]);
347                  }                  }
348          }          }
349    
350          // Load section config          // Load user_list and online_user_list
351          if (load_section_config_from_db() < 0)          if (user_list_pool_init(VAR_USER_LIST_SHM) < 0)
352          {          {
353                  log_error("load_section_config_from_db() error\n");                  log_error("user_list_pool_init(%s) error", VAR_USER_LIST_SHM);
354                    goto cleanup;
355            }
356            if (user_list_pool_reload(0) < 0)
357            {
358                    log_error("user_list_pool_reload(all_user) error");
359                    goto cleanup;
360            }
361            if (user_list_pool_reload(1) < 0)
362            {
363                    log_error("user_list_pool_reload(online_user) error");
364                  goto cleanup;                  goto cleanup;
365          }          }
366    
367          // Load section config          // Load section config and gen_ex
368          if (append_articles_from_db(0, 1) < 0)          if (load_section_config_from_db(1) < 0)
369          {          {
370                  log_error("append_articles_from_db(0, 1) error\n");                  log_error("load_section_config_from_db(0) error");
371                  goto cleanup;                  goto cleanup;
372          }          }
373    
374          log_std("Debug: loaded %d articles, last_aid = %d\n", article_block_article_count(), article_block_last_aid());          set_last_article_op_log_from_db();
375            last_aid = 0;
376    
377            // Load section articles
378            do
379            {
380                    if ((ret = append_articles_from_db(last_aid + 1, 1, LOAD_ARTICLE_COUNT_LIMIT)) < 0)
381                    {
382                            log_error("append_articles_from_db(0, 1, %d) error", LOAD_ARTICLE_COUNT_LIMIT);
383                            goto cleanup;
384                    }
385    
386                    last_aid = article_block_last_aid();
387            } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
388    
389            log_common("Initially load %d articles, last_aid = %d", article_block_article_count(), article_block_last_aid());
390    
391            if ((ret = user_stat_update()) < 0)
392            {
393                    log_error("user_stat_update() error");
394                    goto cleanup;
395            }
396    
397          // Set signal handler          // Set signal handler
398          signal(SIGHUP, sig_hup_handler);          act.sa_handler = sig_hup_handler;
399          signal(SIGCHLD, sig_chld_handler);          if (sigaction(SIGHUP, &act, NULL) == -1)
400          signal(SIGTERM, sig_term_handler);          {
401                    log_error("set signal action of SIGHUP error: %d", errno);
402                    goto cleanup;
403            }
404            act.sa_handler = sig_chld_handler;
405            if (sigaction(SIGCHLD, &act, NULL) == -1)
406            {
407                    log_error("set signal action of SIGCHLD error: %d", errno);
408                    goto cleanup;
409            }
410            act.sa_handler = sig_term_handler;
411            if (sigaction(SIGTERM, &act, NULL) == -1)
412            {
413                    log_error("set signal action of SIGTERM error: %d", errno);
414                    goto cleanup;
415            }
416            act.sa_handler = SIG_IGN;
417            if (sigaction(SIGPIPE, &act, NULL) == -1)
418            {
419                    log_error("set signal action of SIGPIPE error: %d", errno);
420                    goto cleanup;
421            }
422            act.sa_handler = SIG_IGN;
423            if (sigaction(SIGUSR1, &act, NULL) == -1)
424            {
425                    log_error("set signal action of SIGUSR1 error: %d", errno);
426                    goto cleanup;
427            }
428    
429            // Launch section_list_loader process
430            if (section_list_loader_launch() < 0)
431            {
432                    log_error("section_list_loader_launch() error");
433                    goto cleanup;
434            }
435    
436          // Initialize socket server          // Initialize socket server
437          net_server(BBS_address, BBS_port);          net_server(BBS_address, BBS_port);
438    
439  cleanup:  cleanup:
440            // Cleanup loader process if still running
441            if (SYS_child_process_count > 0)
442            {
443                    SYS_child_exit = 0;
444    
445                    if (kill(section_list_loader_pid, SIGTERM) < 0)
446                    {
447                            log_error("Send SIGTERM signal failed (%d)", errno);
448                    }
449    
450                    for (i = 0; SYS_child_exit == 0 && i < 5; i++)
451                    {
452                            sleep(1); // second
453                    }
454    
455                    if ((ret = waitpid(section_list_loader_pid, NULL, WNOHANG)) < 0)
456                    {
457                            log_error("waitpid(%d) error (%d)", section_list_loader_pid, errno);
458                    }
459                    else if (ret == 0)
460                    {
461                            log_common("Force kill section_list_loader process (%d)", section_list_loader_pid);
462                            if (kill(section_list_loader_pid, SIGKILL) < 0)
463                            {
464                                    log_error("Send SIGKILL signal failed (%d)", errno);
465                            }
466                    }
467            }
468    
469          // Cleanup loaded data files          // Cleanup loaded data files
470          file_loader_cleanup();          for (int i = 0; i < data_files_load_startup_count; i++)
471            {
472                    if (unload_file(data_files_load_startup[i]) < 0)
473                    {
474                            log_error("unload_file(%s) error", data_files_load_startup[i]);
475                    }
476            }
477    
478          // Cleanup menu          // Cleanup menu
479          unload_menu(p_bbs_menu);          unload_menu(&bbs_menu);
480          free(p_bbs_menu);          unload_menu(&top10_menu);
481          p_bbs_menu = NULL;  
482            // Cleanup LML module
483            lml_cleanup();
484    
485          // Cleanup data pools          // Cleanup data pools
486          section_list_cleanup();          section_list_cleanup();
487          article_block_cleanup();          article_block_cleanup();
488          trie_dict_cleanup();          trie_dict_cleanup();
489            user_list_pool_cleanup();
490    
         if (unlink(VAR_TRIE_DICT_SHM) < 0)  
         {  
                 log_error("unlink(%s) error\n", VAR_TRIE_DICT_SHM);  
         }  
491          if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0)          if (unlink(VAR_ARTICLE_BLOCK_SHM) < 0)
492          {          {
493                  log_error("unlink(%s) error\n", VAR_ARTICLE_BLOCK_SHM);                  log_error("unlink(%s) error", VAR_ARTICLE_BLOCK_SHM);
494          }          }
495          if (unlink(VAR_SECTION_LIST_SHM) < 0)          if (unlink(VAR_SECTION_LIST_SHM) < 0)
496          {          {
497                  log_error("unlink(%s) error\n", VAR_SECTION_LIST_SHM);                  log_error("unlink(%s) error", VAR_SECTION_LIST_SHM);
498          }          }
499            if (unlink(VAR_TRIE_DICT_SHM) < 0)
500            {
501                    log_error("unlink(%s) error", VAR_TRIE_DICT_SHM);
502            }
503            if (unlink(VAR_USER_LIST_SHM) < 0)
504            {
505                    log_error("unlink(%s) error", VAR_SECTION_LIST_SHM);
506            }
507    
508            log_common("Main process exit normally");
509    
510          log_std("Main process exit normally\n");          log_end();
511    
512          return 0;          return 0;
513  }  }


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

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