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

Diff of /lbbs/src/init.c

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

Revision 1.20 by sysadm, Tue May 13 11:32:01 2025 UTC Revision 1.37 by sysadm, Sat Jan 3 10:27:14 2026 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    init.c  -  description  /*
3                                                           -------------------   * init
4          Copyright            : (C) 2004-2025 by Leaflet   *   - initializer of server daemon
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2026  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    
 #include "init.h"  
 #include "database.h"  
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
15  #include "log.h"  #include "database.h"
16    #include "init.h"
17  #include "io.h"  #include "io.h"
18  #include <string.h>  #include "log.h"
 #include <stdlib.h>  
19  #include <signal.h>  #include <signal.h>
20    #include <stdlib.h>
21    #include <string.h>
22    #include <unistd.h>
23  #include <sys/param.h>  #include <sys/param.h>
 #include <sys/types.h>  
24  #include <sys/stat.h>  #include <sys/stat.h>
25  #include <unistd.h>  #include <sys/types.h>
26    
27    static const char CONF_DELIM_WITH_SPACE[] = " \t\r\n";
28    
29  int init_daemon(void)  int init_daemon(void)
30  {  {
# Line 65  int init_daemon(void) Line 63  int init_daemon(void)
63    
64  int load_conf(const char *conf_file)  int load_conf(const char *conf_file)
65  {  {
66          char temp[256];          char buffer[LINE_BUFFER_LEN];
67            char *saveptr = NULL;
68          // Load configuration          char *p, *q, *r;
69          char c_name[256];          char *y, *m, *d;
70            int v;
71          FILE *fin;          FILE *fin;
72    
73            // Load configuration
74          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
75          {          {
76                  log_error("Open %s failed", conf_file);                  log_error("Open %s failed", conf_file);
77                  return -1;                  return -1;
78          }          }
79    
80          while (fscanf(fin, "%s", c_name) != EOF)          while (fgets(buffer, sizeof(buffer), fin) != NULL)
81          {          {
82                  if (c_name[0] == '#')                  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                          fgets(temp, 256, fin);                          log_error("Skip empty value of config item: %s", p);
97                          continue;                          continue;
98                  }                  }
99                  fscanf(fin, "%*c");  
100                  if (strcmp(c_name, "bbs_id") == 0)                  // Check syntax
101                    r = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr);
102                    if (r != NULL && r[0] != '#')
103                  {                  {
104                          fscanf(fin, "%s", BBS_id);                          log_error("Skip config line with extra value %s = %s %s", p, q, r);
105                            continue;
106                  }                  }
107                  if (strcmp(c_name, "bbs_name") == 0)  
108                    if (strcasecmp(p, "bbs_id") == 0)
109                  {                  {
110                          fscanf(fin, "%s", BBS_name);                          strncpy(BBS_id, q, sizeof(BBS_id) - 1);
111                            BBS_id[sizeof(BBS_id) - 1] = '\0';
112                  }                  }
113                  if (strcmp(c_name, "bbs_server") == 0)                  else if (strcasecmp(p, "bbs_name") == 0)
114                  {                  {
115                          fscanf(fin, "%s", BBS_server);                          strncpy(BBS_name, q, sizeof(BBS_name) - 1);
116                            BBS_name[sizeof(BBS_name) - 1] = '\0';
117                  }                  }
118                  if (strcmp(c_name, "bbs_address") == 0)                  else if (strcasecmp(p, "bbs_server") == 0)
119                  {                  {
120                          fscanf(fin, "%s", BBS_address);                          strncpy(BBS_server, q, sizeof(BBS_server) - 1);
121                            BBS_server[sizeof(BBS_server) - 1] = '\0';
122                  }                  }
123                  if (strcmp(c_name, "bbs_port") == 0)                  else if (strcasecmp(p, "bbs_address") == 0)
124                  {                  {
125                          fscanf(fin, "%hu", &BBS_port);                          strncpy(BBS_address, q, sizeof(BBS_address) - 1);
126                            BBS_address[sizeof(BBS_address) - 1] = '\0';
127                  }                  }
128                  if (strcmp(c_name, "bbs_max_client") == 0)                  else if (strcasecmp(p, "bbs_port") == 0)
129                  {                  {
130                          fscanf(fin, "%d", &BBS_max_client);                          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                  if (strcmp(c_name, "bbs_max_user") == 0)                  else if (strcasecmp(p, "bbs_sys_id") == 0)
168                  {                  {
169                          fscanf(fin, "%d", &BBS_max_user);                          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                  if (strcmp(c_name, "bbs_start_dt") == 0)                  else if (strcasecmp(p, "db_host") == 0)
178                  {                  {
179                          int y = 0, m = 0, d = 0;                          strncpy(DB_host, q, sizeof(DB_host) - 1);
180                          fscanf(fin, "%d-%d-%d", &y, &m, &d);                          DB_host[sizeof(DB_host) - 1] = '\0';
                         snprintf(BBS_start_dt, sizeof(BBS_start_dt), "%4dÄê%2dÔÂ%2dÈÕ", y, m, d);  
181                  }                  }
182                  if (strcmp(c_name, "db_host") == 0)                  else if (strcasecmp(p, "db_username") == 0)
183                  {                  {
184                          fscanf(fin, "%s", DB_host);                          strncpy(DB_username, q, sizeof(DB_username) - 1);
185                            DB_username[sizeof(DB_username) - 1] = '\0';
186                  }                  }
187                  if (strcmp(c_name, "db_username") == 0)                  else if (strcasecmp(p, "db_password") == 0)
188                  {                  {
189                          fscanf(fin, "%s", DB_username);                          strncpy(DB_password, q, sizeof(DB_password) - 1);
190                            DB_password[sizeof(DB_password) - 1] = '\0';
191                  }                  }
192                  if (strcmp(c_name, "db_password") == 0)                  else if (strcasecmp(p, "db_database") == 0)
193                  {                  {
194                          fscanf(fin, "%s", DB_password);                          strncpy(DB_database, q, sizeof(DB_database) - 1);
195                            DB_database[sizeof(DB_database) - 1] = '\0';
196                  }                  }
197                  if (strcmp(c_name, "db_database") == 0)                  else if (strcasecmp(p, "db_timezone") == 0)
198                  {                  {
199                          fscanf(fin, "%s", DB_database);                          strncpy(DB_timezone, q, sizeof(DB_timezone) - 1);
200                            DB_timezone[sizeof(DB_timezone) - 1] = '\0';
201                  }                  }
202                  if (strcmp(c_name, "db_timezone") == 0)                  else
203                  {                  {
204                          fscanf(fin, "%s", DB_timezone);                          log_error("Unknown config %s = %s", p, q);
205                  }                  }
206          }          }
207    


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

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