/[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.10 by sysadm, Sat May 7 12:08:28 2005 UTC Revision 1.33 by sysadm, Tue Nov 4 14:58:56 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   ***************************************************************************/   */
   
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   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.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
11    #include "database.h"
12    #include "init.h"
13  #include "io.h"  #include "io.h"
14    #include "log.h"
15  #include <signal.h>  #include <signal.h>
16    #include <stdlib.h>
17    #include <string.h>
18    #include <unistd.h>
19  #include <sys/param.h>  #include <sys/param.h>
 #include <sys/types.h>  
20  #include <sys/stat.h>  #include <sys/stat.h>
21  #include <unistd.h>  #include <sys/types.h>
   
 void  
 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);  
22    
23    umask (0);  #define CONF_DELIM_WITH_SPACE " \t\r\n"
24    
25    return;  int init_daemon(void)
 }  
   
 int  
 load_conf (const char *conf_file)  
26  {  {
27    char temp[256];          int pid;
   
   // Load configuration  
   char c_name[256];  
   FILE *fin;  
28    
29    if ((fin = fopen (conf_file, "r")) == NULL)          pid = fork();
     {  
       log_error ("Open %s failed", conf_file);  
       return -1;  
     }  
30    
31    while (fscanf (fin, "%s", c_name) != EOF)          if (pid > 0) // Parent process
     {  
       if (c_name[0] == '#')  
32          {          {
33            fgets (temp, 256, fin);                  _exit(0);
           continue;  
34          }          }
35        fscanf (fin, "%*c");          else if (pid < 0) // error
       if (strcmp (c_name, "bbs_id") == 0)  
36          {          {
37            fscanf (fin, "%s", BBS_id);                  _exit(pid);
38          }          }
39        if (strcmp (c_name, "bbs_name") == 0)  
40          {          // Child process
41            fscanf (fin, "%s", BBS_name);          setsid();
42          }  
43        if (strcmp (c_name, "bbs_server") == 0)          pid = fork();
44          {  
45            fscanf (fin, "%s", BBS_server);          if (pid > 0) // Parent process
         }  
       if (strcmp (c_name, "bbs_address") == 0)  
         {  
           fscanf (fin, "%s", BBS_address);  
         }  
       if (strcmp (c_name, "bbs_port") == 0)  
         {  
           fscanf (fin, "%ud", &BBS_port);  
         }  
       if (strcmp (c_name, "bbs_max_client") == 0)  
         {  
           fscanf (fin, "%d", &BBS_max_client);  
         }  
       if (strcmp (c_name, "bbs_max_user") == 0)  
         {  
           fscanf (fin, "%d", &BBS_max_user);  
         }  
       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)  
46          {          {
47            fscanf (fin, "%s", DB_username);                  _exit(0);
48          }          }
49        if (strcmp (c_name, "db_password") == 0)          else if (pid < 0) // error
50          {          {
51            fscanf (fin, "%s", DB_password);                  _exit(pid);
52          }          }
53        if (strcmp (c_name, "db_database") == 0)  
54          {          // Child process
55            fscanf (fin, "%s", DB_database);          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);          fclose(fin);
205    
206    return 0;          return 0;
207  }  }


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

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