| 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 |
#include "bbs.h" |
#include "bbs.h" |
| 18 |
#include "common.h" |
#include "common.h" |
| 19 |
|
#include "database.h" |
| 20 |
|
#include "init.h" |
| 21 |
#include "io.h" |
#include "io.h" |
| 22 |
#include <unistd.h> |
#include "log.h" |
| 23 |
#include <signal.h> |
#include <signal.h> |
| 24 |
|
#include <stdlib.h> |
| 25 |
|
#include <string.h> |
| 26 |
|
#include <unistd.h> |
| 27 |
#include <sys/param.h> |
#include <sys/param.h> |
|
#include <sys/types.h> |
|
| 28 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 29 |
|
#include <sys/types.h> |
| 30 |
|
|
| 31 |
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); |
|
|
|
|
|
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; |
|
|
} |
|
| 32 |
|
|
| 33 |
int |
int init_daemon(void) |
|
load_conf (const char *conf_file) |
|
| 34 |
{ |
{ |
| 35 |
char temp[256]; |
int pid; |
|
|
|
|
// Load configuration |
|
|
char c_name[256]; |
|
|
FILE *fin; |
|
| 36 |
|
|
| 37 |
if ((fin = fopen (conf_file, "r")) == NULL) |
pid = fork(); |
|
{ |
|
|
log_error ("Open %s failed", conf_file); |
|
|
return -1; |
|
|
} |
|
| 38 |
|
|
| 39 |
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) |
|
| 40 |
{ |
{ |
| 41 |
fscanf (fin, "%s", BBS_address); |
_exit(0); |
| 42 |
} |
} |
| 43 |
if (strcmp (c_name, "bbs_port") == 0) |
else if (pid < 0) // error |
| 44 |
{ |
{ |
| 45 |
fscanf (fin, "%ud", &BBS_port); |
_exit(pid); |
| 46 |
} |
} |
| 47 |
if (strcmp (c_name, "bbs_max_client") == 0) |
|
| 48 |
{ |
// Child process |
| 49 |
fscanf (fin, "%d", &BBS_max_client); |
setsid(); |
| 50 |
} |
|
| 51 |
if (strcmp (c_name, "bbs_max_user") == 0) |
pid = fork(); |
| 52 |
{ |
|
| 53 |
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) |
|
| 54 |
{ |
{ |
| 55 |
fscanf (fin, "%s", DB_username); |
_exit(0); |
| 56 |
} |
} |
| 57 |
if (strcmp (c_name, "db_password") == 0) |
else if (pid < 0) // error |
| 58 |
{ |
{ |
| 59 |
fscanf (fin, "%s", DB_password); |
_exit(pid); |
| 60 |
} |
} |
| 61 |
if (strcmp (c_name, "db_database") == 0) |
|
| 62 |
{ |
// Child process |
| 63 |
fscanf (fin, "%s", DB_database); |
umask(022); |
| 64 |
|
|
| 65 |
|
return 0; |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
int load_conf(const char *conf_file) |
| 69 |
|
{ |
| 70 |
|
char buffer[LINE_BUFFER_LEN]; |
| 71 |
|
char *saveptr = NULL; |
| 72 |
|
char *p, *q, *r; |
| 73 |
|
char *y, *m, *d; |
| 74 |
|
int v; |
| 75 |
|
FILE *fin; |
| 76 |
|
|
| 77 |
|
// Load configuration |
| 78 |
|
if ((fin = fopen(conf_file, "r")) == NULL) |
| 79 |
|
{ |
| 80 |
|
log_error("Open %s failed", conf_file); |
| 81 |
|
return -1; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
while (fgets(buffer, sizeof(buffer), fin) != NULL) |
| 85 |
|
{ |
| 86 |
|
p = strtok_r(buffer, CONF_DELIM_WITH_SPACE, &saveptr); |
| 87 |
|
if (p == NULL) // Blank line |
| 88 |
|
{ |
| 89 |
|
continue; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line |
| 93 |
|
{ |
| 94 |
|
continue; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
q = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr); |
| 98 |
|
if (q == NULL) // Empty value |
| 99 |
|
{ |
| 100 |
|
log_error("Skip empty value of config item: %s\n", p); |
| 101 |
|
continue; |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
// Check syntax |
| 105 |
|
r = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr); |
| 106 |
|
if (r != NULL && r[0] != '#') |
| 107 |
|
{ |
| 108 |
|
log_error("Skip config line with extra value %s = %s %s\n", p, q, r); |
| 109 |
|
continue; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
if (strcasecmp(p, "bbs_id") == 0) |
| 113 |
|
{ |
| 114 |
|
strncpy(BBS_id, q, sizeof(BBS_id) - 1); |
| 115 |
|
BBS_id[sizeof(BBS_id) - 1] = '\0'; |
| 116 |
|
} |
| 117 |
|
else if (strcasecmp(p, "bbs_name") == 0) |
| 118 |
|
{ |
| 119 |
|
strncpy(BBS_name, q, sizeof(BBS_name) - 1); |
| 120 |
|
BBS_name[sizeof(BBS_name) - 1] = '\0'; |
| 121 |
|
} |
| 122 |
|
else if (strcasecmp(p, "bbs_server") == 0) |
| 123 |
|
{ |
| 124 |
|
strncpy(BBS_server, q, sizeof(BBS_server) - 1); |
| 125 |
|
BBS_server[sizeof(BBS_server) - 1] = '\0'; |
| 126 |
|
} |
| 127 |
|
else if (strcasecmp(p, "bbs_address") == 0) |
| 128 |
|
{ |
| 129 |
|
strncpy(BBS_address, q, sizeof(BBS_address) - 1); |
| 130 |
|
BBS_address[sizeof(BBS_address) - 1] = '\0'; |
| 131 |
|
} |
| 132 |
|
else if (strcasecmp(p, "bbs_port") == 0) |
| 133 |
|
{ |
| 134 |
|
BBS_port[0] = (in_port_t)atoi(q); |
| 135 |
|
} |
| 136 |
|
else if (strcasecmp(p, "bbs_ssh_port") == 0) |
| 137 |
|
{ |
| 138 |
|
BBS_port[1] = (in_port_t)atoi(q); |
| 139 |
|
} |
| 140 |
|
else if (strcasecmp(p, "bbs_max_client") == 0) |
| 141 |
|
{ |
| 142 |
|
BBS_max_client = atoi(q); |
| 143 |
|
if (BBS_max_client <= 0 || BBS_max_client > MAX_CLIENT_LIMIT) |
| 144 |
|
{ |
| 145 |
|
log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_client); |
| 146 |
|
BBS_max_client = MAX_CLIENT_LIMIT; |
| 147 |
|
} |
| 148 |
|
} |
| 149 |
|
else if (strcasecmp(p, "bbs_max_client_per_ip") == 0) |
| 150 |
|
{ |
| 151 |
|
BBS_max_client_per_ip = atoi(q); |
| 152 |
|
if (BBS_max_client_per_ip <= 0 || BBS_max_client_per_ip > MAX_CLIENT_PER_IP_LIMIT) |
| 153 |
|
{ |
| 154 |
|
log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_client_per_ip); |
| 155 |
|
BBS_max_client_per_ip = MAX_CLIENT_PER_IP_LIMIT; |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
|
else if (strcasecmp(p, "bbs_max_user") == 0) |
| 159 |
|
{ |
| 160 |
|
BBS_max_user = atoi(q); |
| 161 |
|
if (BBS_max_user <= 0 || BBS_max_user > BBS_MAX_USER_LIMIT) |
| 162 |
|
{ |
| 163 |
|
log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_user); |
| 164 |
|
BBS_max_user = BBS_MAX_USER_LIMIT; |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
|
else if (strcasecmp(p, "bbs_start_dt") == 0) |
| 168 |
|
{ |
| 169 |
|
y = strtok_r(q, "-", &saveptr); |
| 170 |
|
m = strtok_r(NULL, "-", &saveptr); |
| 171 |
|
d = strtok_r(NULL, "-", &saveptr); |
| 172 |
|
|
| 173 |
|
if (y == NULL || m == NULL || d == NULL) |
| 174 |
|
{ |
| 175 |
|
log_error("Ignore config bbs_start_dt with incorrect value\n"); |
| 176 |
|
continue; |
| 177 |
|
} |
| 178 |
|
snprintf(BBS_start_dt, sizeof(BBS_start_dt), "%4s年%2s月%2s日", y, m, d); |
| 179 |
|
} |
| 180 |
|
else if (strcasecmp(p, "bbs_sys_id") == 0) |
| 181 |
|
{ |
| 182 |
|
v = atoi(q); |
| 183 |
|
if (v <= 0 || v > BBS_MAX_USER_LIMIT) |
| 184 |
|
{ |
| 185 |
|
log_error("Ignore config bbs_sys_id with incorrect value %d\n", v); |
| 186 |
|
continue; |
| 187 |
|
} |
| 188 |
|
BBS_sys_id = v; |
| 189 |
|
} |
| 190 |
|
else if (strcasecmp(p, "db_host") == 0) |
| 191 |
|
{ |
| 192 |
|
strncpy(DB_host, q, sizeof(DB_host) - 1); |
| 193 |
|
DB_host[sizeof(DB_host) - 1] = '\0'; |
| 194 |
|
} |
| 195 |
|
else if (strcasecmp(p, "db_username") == 0) |
| 196 |
|
{ |
| 197 |
|
strncpy(DB_username, q, sizeof(DB_username) - 1); |
| 198 |
|
DB_username[sizeof(DB_username) - 1] = '\0'; |
| 199 |
|
} |
| 200 |
|
else if (strcasecmp(p, "db_password") == 0) |
| 201 |
|
{ |
| 202 |
|
strncpy(DB_password, q, sizeof(DB_password) - 1); |
| 203 |
|
DB_password[sizeof(DB_password) - 1] = '\0'; |
| 204 |
|
} |
| 205 |
|
else if (strcasecmp(p, "db_database") == 0) |
| 206 |
|
{ |
| 207 |
|
strncpy(DB_database, q, sizeof(DB_database) - 1); |
| 208 |
|
DB_database[sizeof(DB_database) - 1] = '\0'; |
| 209 |
|
} |
| 210 |
|
else if (strcasecmp(p, "db_timezone") == 0) |
| 211 |
|
{ |
| 212 |
|
strncpy(DB_timezone, q, sizeof(DB_timezone) - 1); |
| 213 |
|
DB_timezone[sizeof(DB_timezone) - 1] = '\0'; |
| 214 |
|
} |
| 215 |
|
else |
| 216 |
|
{ |
| 217 |
|
log_error("Unknown config %s = %s\n", p, q); |
| 218 |
|
} |
| 219 |
} |
} |
|
} |
|
| 220 |
|
|
| 221 |
fclose (fin); |
fclose(fin); |
| 222 |
|
|
| 223 |
return 0; |
return 0; |
| 224 |
} |
} |