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

Contents of /lbbs/src/init.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.37 - (show annotations)
Sat Jan 3 10:27:14 2026 UTC (2 months, 1 week ago) by sysadm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.36: +1 -1 lines
Content type: text/x-csrc
Update copyright info

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

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