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

Contents of /lbbs/src/init.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (show annotations)
Wed Nov 5 04:19:21 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.33: +1 -1 lines
Content type: text/x-csrc
Use enum / const int instead of macro define constant integers
Use const char * instead of macro define for constant strings

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * init
4 * - initializer of server daemon
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #include "bbs.h"
10 #include "common.h"
11 #include "database.h"
12 #include "init.h"
13 #include "io.h"
14 #include "log.h"
15 #include <signal.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/param.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22
23 static const char CONF_DELIM_WITH_SPACE[] = " \t\r\n";
24
25 int init_daemon(void)
26 {
27 int pid;
28
29 pid = fork();
30
31 if (pid > 0) // Parent process
32 {
33 _exit(0);
34 }
35 else if (pid < 0) // error
36 {
37 _exit(pid);
38 }
39
40 // Child process
41 setsid();
42
43 pid = fork();
44
45 if (pid > 0) // Parent process
46 {
47 _exit(0);
48 }
49 else if (pid < 0) // error
50 {
51 _exit(pid);
52 }
53
54 // Child process
55 umask(022);
56
57 return 0;
58 }
59
60 int load_conf(const char *conf_file)
61 {
62 char buffer[LINE_BUFFER_LEN];
63 char *saveptr = NULL;
64 char *p, *q, *r;
65 char *y, *m, *d;
66 int v;
67 FILE *fin;
68
69 // Load configuration
70 if ((fin = fopen(conf_file, "r")) == NULL)
71 {
72 log_error("Open %s failed", conf_file);
73 return -1;
74 }
75
76 while (fgets(buffer, sizeof(buffer), fin) != NULL)
77 {
78 p = strtok_r(buffer, CONF_DELIM_WITH_SPACE, &saveptr);
79 if (p == NULL) // Blank line
80 {
81 continue;
82 }
83
84 if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
85 {
86 continue;
87 }
88
89 q = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr);
90 if (q == NULL) // Empty value
91 {
92 log_error("Skip empty value of config item: %s\n", p);
93 continue;
94 }
95
96 // Check syntax
97 r = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr);
98 if (r != NULL && r[0] != '#')
99 {
100 log_error("Skip config line with extra value %s = %s %s\n", p, q, r);
101 continue;
102 }
103
104 if (strcasecmp(p, "bbs_id") == 0)
105 {
106 strncpy(BBS_id, q, sizeof(BBS_id) - 1);
107 BBS_id[sizeof(BBS_id) - 1] = '\0';
108 }
109 else if (strcasecmp(p, "bbs_name") == 0)
110 {
111 strncpy(BBS_name, q, sizeof(BBS_name) - 1);
112 BBS_name[sizeof(BBS_name) - 1] = '\0';
113 }
114 else if (strcasecmp(p, "bbs_server") == 0)
115 {
116 strncpy(BBS_server, q, sizeof(BBS_server) - 1);
117 BBS_server[sizeof(BBS_server) - 1] = '\0';
118 }
119 else if (strcasecmp(p, "bbs_address") == 0)
120 {
121 strncpy(BBS_address, q, sizeof(BBS_address) - 1);
122 BBS_address[sizeof(BBS_address) - 1] = '\0';
123 }
124 else if (strcasecmp(p, "bbs_port") == 0)
125 {
126 BBS_port[0] = (in_port_t)atoi(q);
127 }
128 else if (strcasecmp(p, "bbs_ssh_port") == 0)
129 {
130 BBS_port[1] = (in_port_t)atoi(q);
131 }
132 else if (strcasecmp(p, "bbs_max_client") == 0)
133 {
134 BBS_max_client = atoi(q);
135 if (BBS_max_client <= 0 || BBS_max_client > MAX_CLIENT_LIMIT)
136 {
137 log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_client);
138 BBS_max_client = MAX_CLIENT_LIMIT;
139 }
140 }
141 else if (strcasecmp(p, "bbs_max_client_per_ip") == 0)
142 {
143 BBS_max_client_per_ip = atoi(q);
144 if (BBS_max_client_per_ip <= 0 || BBS_max_client_per_ip > MAX_CLIENT_PER_IP_LIMIT)
145 {
146 log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_client_per_ip);
147 BBS_max_client_per_ip = MAX_CLIENT_PER_IP_LIMIT;
148 }
149 }
150 else if (strcasecmp(p, "bbs_start_dt") == 0)
151 {
152 y = strtok_r(q, "-", &saveptr);
153 m = strtok_r(NULL, "-", &saveptr);
154 d = strtok_r(NULL, "-", &saveptr);
155
156 if (y == NULL || m == NULL || d == NULL)
157 {
158 log_error("Ignore config bbs_start_dt with incorrect value\n");
159 continue;
160 }
161 snprintf(BBS_start_dt, sizeof(BBS_start_dt), "%4s年%2s月%2s日", y, m, d);
162 }
163 else if (strcasecmp(p, "bbs_sys_id") == 0)
164 {
165 v = atoi(q);
166 if (v <= 0)
167 {
168 log_error("Ignore config bbs_sys_id with incorrect value %d\n", v);
169 continue;
170 }
171 BBS_sys_id = v;
172 }
173 else if (strcasecmp(p, "db_host") == 0)
174 {
175 strncpy(DB_host, q, sizeof(DB_host) - 1);
176 DB_host[sizeof(DB_host) - 1] = '\0';
177 }
178 else if (strcasecmp(p, "db_username") == 0)
179 {
180 strncpy(DB_username, q, sizeof(DB_username) - 1);
181 DB_username[sizeof(DB_username) - 1] = '\0';
182 }
183 else if (strcasecmp(p, "db_password") == 0)
184 {
185 strncpy(DB_password, q, sizeof(DB_password) - 1);
186 DB_password[sizeof(DB_password) - 1] = '\0';
187 }
188 else if (strcasecmp(p, "db_database") == 0)
189 {
190 strncpy(DB_database, q, sizeof(DB_database) - 1);
191 DB_database[sizeof(DB_database) - 1] = '\0';
192 }
193 else if (strcasecmp(p, "db_timezone") == 0)
194 {
195 strncpy(DB_timezone, q, sizeof(DB_timezone) - 1);
196 DB_timezone[sizeof(DB_timezone) - 1] = '\0';
197 }
198 else
199 {
200 log_error("Unknown config %s = %s\n", p, q);
201 }
202 }
203
204 fclose(fin);
205
206 return 0;
207 }

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