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

Annotation of /lbbs/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (hide annotations)
Mon Apr 28 03:31:00 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.17: +131 -134 lines
Content type: text/x-csrc
Update

1 sysadm 1.1 /***************************************************************************
2 sysadm 1.18 main.c - description
3     -------------------
4     begin : Mon Oct 11 2004
5     copyright : (C) 2004 by Leaflet
6     email : leaflet@leafok.com
7 sysadm 1.1 ***************************************************************************/
8    
9     /***************************************************************************
10     * *
11     * This program is free software; you can redistribute it and/or modify *
12     * it under the terms of the GNU General Public License as published by *
13     * the Free Software Foundation; either version 2 of the License, or *
14     * (at your option) any later version. *
15     * *
16     ***************************************************************************/
17    
18 sysadm 1.4 #include "bbs.h"
19 sysadm 1.1 #include "common.h"
20 sysadm 1.7 #include "io.h"
21 sysadm 1.9 #include "menu.h"
22 sysadm 1.14 #include <signal.h>
23 sysadm 1.7 #include <string.h>
24 sysadm 1.15 #include <sys/types.h>
25 sysadm 1.13 #include <unistd.h>
26 sysadm 1.1
27 sysadm 1.18 void app_help(void)
28 sysadm 1.6 {
29 sysadm 1.18 prints("Usage: bbsd [-fhv] [...]\n\n"
30     "-f\t--foreground\t\tForce program run in foreground\n"
31     "-h\t--help\t\t\tDisplay this help message\n"
32     "-v\t--version\t\tDisplay version information\n"
33     "\t--display-log\t\tDisplay standard log information\n"
34     "\t--display-error-log\tDisplay error log information\n"
35     "\n If meet any bug, please report to <leaflet@leafok.com>\n\n");
36 sysadm 1.6 }
37    
38 sysadm 1.18 void arg_error(void)
39 sysadm 1.6 {
40 sysadm 1.18 prints("Invalid arguments\n");
41     app_help();
42 sysadm 1.6 }
43    
44 sysadm 1.18 int main(int argc, char *argv[])
45 sysadm 1.1 {
46 sysadm 1.18 char log_dir[256], file_log_std[256], file_log_error[256], file_config[256];
47     int i, j;
48     int daemon = 1, std_log_redir = 0, error_log_redir = 0;
49    
50     // Parse args
51     for (i = 1; i < argc; i++)
52 sysadm 1.12 {
53 sysadm 1.18 switch (argv[i][0])
54 sysadm 1.12 {
55 sysadm 1.18 case '-':
56     if (argv[i][1] != '-')
57     {
58     for (j = 1; j < strlen(argv[i]); j++)
59     {
60     switch (argv[i][j])
61     {
62     case 'f':
63     daemon = 0;
64     break;
65     case 'h':
66     app_help();
67     exit(0);
68     case 'v':
69     puts(app_version);
70     exit(0);
71     default:
72     arg_error();
73     exit(1);
74     }
75     }
76     }
77     else
78     {
79     if (strcmp(argv[i] + 2, "foreground") == 0)
80     {
81     daemon = 0;
82     break;
83     }
84     if (strcmp(argv[i] + 2, "help") == 0)
85     {
86     app_help();
87     exit(0);
88     }
89     if (strcmp(argv[i] + 2, "version") == 0)
90     {
91     puts(app_version);
92     exit(0);
93     }
94     if (strcmp(argv[i] + 2, "display-log") == 0)
95     {
96     std_log_redir = 1;
97     }
98     if (strcmp(argv[i] + 2, "display-error-log") == 0)
99     {
100     error_log_redir = 1;
101     }
102     }
103     break;
104 sysadm 1.12 }
105     }
106    
107 sysadm 1.18 // Initialize daemon
108     if (daemon)
109     init_daemon();
110    
111     // Change current dir
112     strncpy(app_home_dir, argv[0], rindex(argv[0], '/') - argv[0] + 1);
113     strcat(app_home_dir, "../");
114     chdir(app_home_dir);
115    
116     // Initialize log
117     strcpy(app_temp_dir, "/tmp/lbbs/");
118     mkdir(app_temp_dir, 0777);
119     strcpy(log_dir, app_home_dir);
120     strcat(log_dir, "log/");
121     strcpy(file_log_std, log_dir);
122     strcpy(file_log_error, log_dir);
123     strcat(file_log_std, "bbsd.log");
124     strcat(file_log_error, "error.log");
125     mkdir(log_dir, 0750);
126     if (log_begin(file_log_std, file_log_error) < 0)
127     exit(-1);
128    
129     if ((!daemon) && std_log_redir)
130     log_std_redirect(2);
131     if ((!daemon) && error_log_redir)
132     log_err_redirect(3);
133    
134     // Load configuration
135     strcpy(file_config, app_home_dir);
136     strcat(file_config, "conf/bbsd.conf");
137     if (load_conf(file_config) < 0)
138     exit(-2);
139    
140     // Load menus
141     strcpy(file_config, app_home_dir);
142     strcat(file_config, "conf/menu.conf");
143     if (load_menu(&bbs_menu, file_config) < 0)
144     exit(-3);
145    
146     // Set signal handler
147     signal(SIGCHLD, child_exit);
148     signal(SIGTERM, system_exit);
149     signal(SIG_RELOAD_MENU, reload_bbs_menu);
150    
151     // Initialize socket server
152     net_server(BBS_address, BBS_port);
153    
154     // Wait for child process exit
155     while (SYS_child_process_count > 0)
156     {
157     log_std(".");
158     sleep(1);
159     }
160    
161     // Cleanup
162     unload_menu(&bbs_menu);
163     rmdir(app_temp_dir);
164 sysadm 1.15
165 sysadm 1.18 return 0;
166 sysadm 1.1 }

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