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