/[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.6 by sysadm, Fri Oct 22 15:20:32 2004 UTC Revision 1.36 by sysadm, Fri Dec 19 06:16:27 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                            init.c  -  description  /*
3                               -------------------   * init
4      begin                : Mon Oct 18 2004   *   - initializer of server daemon
5      copyright            : (C) 2004 by Leaflet   *
6      email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
8    
9  /***************************************************************************  #ifdef HAVE_CONFIG_H
10   *                                                                         *  #include "config.h"
11   *   This program is free software; you can redistribute it and/or modify  *  #endif
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
15    #include "database.h"
16    #include "init.h"
17  #include "io.h"  #include "io.h"
18  #include <unistd.h>  #include "log.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 <sys/types.h>
26    
27  void  static const char CONF_DELIM_WITH_SPACE[] = " \t\r\n";
 init_daemon (void)  
 {  
   int pid;  
   int i;  
   
   if (pid = fork ())  
     exit (0);  
   else if (pid < 0)  
     exit (1);  
   
   setsid ();  
   
   if (pid = fork ())  
     exit (0);  
   else if (pid < 0)  
     exit (1);  
   
   for (i = 0; i < NOFILE; ++i)  
     close (i);  
   chdir (app_home_dir);  
   umask (0);  
   
   signal (SIGCHLD, SIG_IGN);  
   
   return;  
 }  
28    
29  int  int init_daemon(void)
 load_conf (const char *conf_file)  
30  {  {
31    char temp[256];          int pid;
   
   // Load configuration  
   char c_name[256];  
   FILE *fin;  
32    
33    if ((fin = fopen (conf_file, "r")) == NULL)          pid = fork();
     {  
       log_error ("Open bbsd.conf failed");  
       return -1;  
     }  
34    
35    while (fscanf (fin, "%s", c_name) != EOF)          if (pid > 0) // Parent process
     {  
       if (c_name[0] == '#')  
       {  
         fgets(temp, 256, fin);  
         continue;  
       }  
       fscanf (fin, "%*c");  
       if (strcmp (c_name, "bbs_id") == 0)  
         {  
           fscanf (fin, "%s", BBS_id);  
         }  
       if (strcmp (c_name, "bbs_name") == 0)  
         {  
           fscanf (fin, "%s", BBS_name);  
         }  
       if (strcmp (c_name, "bbs_server") == 0)  
         {  
           fscanf (fin, "%s", BBS_server);  
         }  
       if (strcmp (c_name, "bbs_address") == 0)  
36          {          {
37            fscanf (fin, "%s", BBS_address);                  _exit(0);
38          }          }
39        if (strcmp (c_name, "bbs_port") == 0)          else if (pid < 0) // error
40          {          {
41            fscanf (fin, "%ud", &BBS_port);                  _exit(pid);
42          }          }
43        if (strcmp (c_name, "bbs_max_client") == 0)  
44          {          // Child process
45            fscanf (fin, "%d", &BBS_max_client);          setsid();
46          }  
47        if (strcmp (c_name, "bbs_max_user") == 0)          pid = fork();
48          {  
49            fscanf (fin, "%d", &BBS_max_user);          if (pid > 0) // Parent process
         }  
       if (strcmp (c_name, "bbs_start_dt") == 0)  
         {  
           int y = 0, m = 0, d = 0;  
           fscanf (fin, "%d-%d-%d", &y, &m, &d);  
           sprintf (BBS_start_dt, "%4dÄê%2dÔÂ%2dÈÕ",y,m,d);  
         }  
       if (strcmp (c_name, "db_host") == 0)  
         {  
           fscanf (fin, "%s", DB_host);  
         }  
       if (strcmp (c_name, "db_username") == 0)  
50          {          {
51            fscanf (fin, "%s", DB_username);                  _exit(0);
52          }          }
53        if (strcmp (c_name, "db_password") == 0)          else if (pid < 0) // error
54          {          {
55            fscanf (fin, "%s", DB_password);                  _exit(pid);
56          }          }
57        if (strcmp (c_name, "db_database") == 0)  
58          {          // Child process
59            fscanf (fin, "%s", DB_database);          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);          fclose(fin);
209      
210    return 0;          return 0;
211  }  }


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

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