/[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.9 by sysadm, Sun Mar 20 17:37:14 2005 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"
20    #include "database.h"
21  #include "bbs.h"  #include "bbs.h"
22  #include "common.h"  #include "common.h"
23    #include "log.h"
24  #include "io.h"  #include "io.h"
25  #include <unistd.h>  #include <string.h>
26    #include <strings.h>
27    #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>
33    
34  void  #define 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);  
35    
36    setsid ();  int init_daemon(void)
   
   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;  
 }  
   
 int  
 load_conf (const char *conf_file)  
37  {  {
38    char temp[256];          int pid;
   
   // Load configuration  
   char c_name[256];  
   FILE *fin;  
39    
40    if ((fin = fopen (conf_file, "r")) == NULL)          pid = fork();
     {  
       log_error ("Open %s failed", conf_file);  
       return -1;  
     }  
41    
42    while (fscanf (fin, "%s", c_name) != EOF)          if (pid > 0) // Parent process
     {  
       if (c_name[0] == '#')  
43          {          {
44            fgets (temp, 256, fin);                  _exit(0);
           continue;  
45          }          }
46        fscanf (fin, "%*c");          else if (pid < 0) // error
       if (strcmp (c_name, "bbs_id") == 0)  
47          {          {
48            fscanf (fin, "%s", BBS_id);                  _exit(pid);
49          }          }
50        if (strcmp (c_name, "bbs_name") == 0)  
51          {          // Child process
52            fscanf (fin, "%s", BBS_name);          setsid();
53          }  
54        if (strcmp (c_name, "bbs_server") == 0)          pid = fork();
55          {  
56            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)  
57          {          {
58            fscanf (fin, "%s", DB_username);                  _exit(0);
59          }          }
60        if (strcmp (c_name, "db_password") == 0)          else if (pid < 0) // error
61          {          {
62            fscanf (fin, "%s", DB_password);                  _exit(pid);
63          }          }
64        if (strcmp (c_name, "db_database") == 0)  
65          {          // Child process
66            fscanf (fin, "%s", DB_database);          umask(022);
67    
68            return 0;
69    }
70    
71    int load_conf(const char *conf_file)
72    {
73            char buffer[LINE_BUFFER_LEN];
74            char *saveptr = NULL;
75            char *p, *q, *r;
76            char *y, *m, *d;
77            FILE *fin;
78    
79            // Load configuration
80            if ((fin = fopen(conf_file, "r")) == NULL)
81            {
82                    log_error("Open %s failed", conf_file);
83                    return -1;
84            }
85    
86            while (fgets(buffer, sizeof(buffer), fin) != NULL)
87            {
88                    p = strtok_r(buffer, CONF_DELIM_WITH_SPACE, &saveptr);
89                    if (p == NULL) // Blank line
90                    {
91                            continue;
92                    }
93    
94                    if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
95                    {
96                            continue;
97                    }
98    
99                    q = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr);
100                    if (q == NULL) // Empty value
101                    {
102                            log_error("Skip empty value of config item: %s\n", p);
103                            continue;
104                    }
105    
106                    // Check syntax
107                    r = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr);
108                    if (r != NULL && r[0] != '#')
109                    {
110                            log_error("Skip config line with extra value %s = %s %s\n", p, q, r);
111                            continue;
112                    }
113    
114                    if (strcasecmp(p, "bbs_id") == 0)
115                    {
116                            strncpy(BBS_id, q, sizeof(BBS_id) - 1);
117                            BBS_id[sizeof(BBS_id) - 1] = '\0';
118                    }
119                    else if (strcasecmp(p, "bbs_name") == 0)
120                    {
121                            strncpy(BBS_name, q, sizeof(BBS_name) - 1);
122                            BBS_name[sizeof(BBS_name) - 1] = '\0';
123                    }
124                    else if (strcasecmp(p, "bbs_server") == 0)
125                    {
126                            strncpy(BBS_server, q, sizeof(BBS_server) - 1);
127                            BBS_server[sizeof(BBS_server) - 1] = '\0';
128                    }
129                    else if (strcasecmp(p, "bbs_address") == 0)
130                    {
131                            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                    else if (strcasecmp(p, "db_host") == 0)
183                    {
184                            strncpy(DB_host, q, sizeof(DB_host) - 1);
185                            DB_host[sizeof(DB_host) - 1] = '\0';
186                    }
187                    else if (strcasecmp(p, "db_username") == 0)
188                    {
189                            strncpy(DB_username, q, sizeof(DB_username) - 1);
190                            DB_username[sizeof(DB_username) - 1] = '\0';
191                    }
192                    else if (strcasecmp(p, "db_password") == 0)
193                    {
194                            strncpy(DB_password, q, sizeof(DB_password) - 1);
195                            DB_password[sizeof(DB_password) - 1] = '\0';
196                    }
197                    else if (strcasecmp(p, "db_database") == 0)
198                    {
199                            strncpy(DB_database, q, sizeof(DB_database) - 1);
200                            DB_database[sizeof(DB_database) - 1] = '\0';
201                    }
202                    else if (strcasecmp(p, "db_timezone") == 0)
203                    {
204                            strncpy(DB_timezone, q, sizeof(DB_timezone) - 1);
205                            DB_timezone[sizeof(DB_timezone) - 1] = '\0';
206                    }
207                    else
208                    {
209                            log_error("Unknown config %s = %s\n", p, q);
210                    }
211          }          }
     }  
212    
213    fclose (fin);          fclose(fin);
214    
215    return 0;          return 0;
216  }  }


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

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