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

Annotation of /lbbs/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (hide annotations)
Wed Apr 30 09:18:19 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.19: +9 -2 lines
Content type: text/x-csrc
Add missing header files
Update compile dependency

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.20 #include "init.h"
20 sysadm 1.1 #include "common.h"
21 sysadm 1.20 #include "net_server.h"
22     #include "log.h"
23 sysadm 1.7 #include "io.h"
24 sysadm 1.9 #include "menu.h"
25 sysadm 1.20 #include <stdlib.h>
26 sysadm 1.14 #include <signal.h>
27 sysadm 1.7 #include <string.h>
28 sysadm 1.20 #include <unistd.h>
29 sysadm 1.15 #include <sys/types.h>
30 sysadm 1.20 #include <sys/stat.h>
31 sysadm 1.1
32 sysadm 1.18 void app_help(void)
33 sysadm 1.6 {
34 sysadm 1.18 prints("Usage: bbsd [-fhv] [...]\n\n"
35     "-f\t--foreground\t\tForce program run in foreground\n"
36     "-h\t--help\t\t\tDisplay this help message\n"
37     "-v\t--version\t\tDisplay version information\n"
38     "\t--display-log\t\tDisplay standard log information\n"
39     "\t--display-error-log\tDisplay error log information\n"
40     "\n If meet any bug, please report to <leaflet@leafok.com>\n\n");
41 sysadm 1.6 }
42    
43 sysadm 1.18 void arg_error(void)
44 sysadm 1.6 {
45 sysadm 1.18 prints("Invalid arguments\n");
46     app_help();
47 sysadm 1.6 }
48    
49 sysadm 1.18 int main(int argc, char *argv[])
50 sysadm 1.1 {
51 sysadm 1.18 char log_dir[256], file_log_std[256], file_log_error[256], file_config[256];
52     int i, j;
53     int daemon = 1, std_log_redir = 0, error_log_redir = 0;
54    
55     // Parse args
56     for (i = 1; i < argc; i++)
57 sysadm 1.12 {
58 sysadm 1.18 switch (argv[i][0])
59 sysadm 1.12 {
60 sysadm 1.18 case '-':
61     if (argv[i][1] != '-')
62     {
63     for (j = 1; j < strlen(argv[i]); j++)
64     {
65     switch (argv[i][j])
66     {
67     case 'f':
68     daemon = 0;
69     break;
70     case 'h':
71     app_help();
72     exit(0);
73     case 'v':
74     puts(app_version);
75     exit(0);
76     default:
77     arg_error();
78     exit(1);
79     }
80     }
81     }
82     else
83     {
84     if (strcmp(argv[i] + 2, "foreground") == 0)
85     {
86     daemon = 0;
87     break;
88     }
89     if (strcmp(argv[i] + 2, "help") == 0)
90     {
91     app_help();
92     exit(0);
93     }
94     if (strcmp(argv[i] + 2, "version") == 0)
95     {
96     puts(app_version);
97     exit(0);
98     }
99     if (strcmp(argv[i] + 2, "display-log") == 0)
100     {
101     std_log_redir = 1;
102     }
103     if (strcmp(argv[i] + 2, "display-error-log") == 0)
104     {
105     error_log_redir = 1;
106     }
107     }
108     break;
109 sysadm 1.12 }
110     }
111    
112 sysadm 1.18 // Initialize daemon
113     if (daemon)
114 sysadm 1.20 {
115 sysadm 1.18 init_daemon();
116 sysadm 1.20 }
117 sysadm 1.18
118     // Change current dir
119 sysadm 1.20 strncpy(app_home_dir, argv[0], strrchr(argv[0], '/') - argv[0] + 1);
120 sysadm 1.18 strcat(app_home_dir, "../");
121     chdir(app_home_dir);
122    
123     // Initialize log
124 sysadm 1.19 strcpy(app_temp_dir, "var/");
125     mkdir(app_temp_dir, 0750);
126 sysadm 1.18 strcpy(log_dir, app_home_dir);
127     strcat(log_dir, "log/");
128     strcpy(file_log_std, log_dir);
129     strcpy(file_log_error, log_dir);
130     strcat(file_log_std, "bbsd.log");
131     strcat(file_log_error, "error.log");
132     mkdir(log_dir, 0750);
133     if (log_begin(file_log_std, file_log_error) < 0)
134     exit(-1);
135    
136     if ((!daemon) && std_log_redir)
137     log_std_redirect(2);
138     if ((!daemon) && error_log_redir)
139     log_err_redirect(3);
140    
141     // Load configuration
142     strcpy(file_config, app_home_dir);
143     strcat(file_config, "conf/bbsd.conf");
144     if (load_conf(file_config) < 0)
145     exit(-2);
146    
147     // Load menus
148     strcpy(file_config, app_home_dir);
149     strcat(file_config, "conf/menu.conf");
150     if (load_menu(&bbs_menu, file_config) < 0)
151     exit(-3);
152    
153     // Set signal handler
154     signal(SIGCHLD, child_exit);
155     signal(SIGTERM, system_exit);
156     signal(SIG_RELOAD_MENU, reload_bbs_menu);
157    
158     // Initialize socket server
159     net_server(BBS_address, BBS_port);
160    
161     // Wait for child process exit
162     while (SYS_child_process_count > 0)
163     {
164     log_std(".");
165     sleep(1);
166     }
167    
168     // Cleanup
169     unload_menu(&bbs_menu);
170     rmdir(app_temp_dir);
171 sysadm 1.15
172 sysadm 1.18 return 0;
173 sysadm 1.1 }

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