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


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

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