| 22 |
#include "common.h" |
#include "common.h" |
| 23 |
#include "log.h" |
#include "log.h" |
| 24 |
#include "io.h" |
#include "io.h" |
| 25 |
|
#include "init.h" |
| 26 |
#include "fork.h" |
#include "fork.h" |
| 27 |
#include "menu.h" |
#include "menu.h" |
| 28 |
|
#include "file_loader.h" |
| 29 |
|
#include "section_list_loader.h" |
| 30 |
#include <errno.h> |
#include <errno.h> |
| 31 |
#include <fcntl.h> |
#include <fcntl.h> |
| 32 |
#include <string.h> |
#include <string.h> |
| 38 |
#include <sys/wait.h> |
#include <sys/wait.h> |
| 39 |
#include <sys/epoll.h> |
#include <sys/epoll.h> |
| 40 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
| 41 |
|
#include <netinet/in.h> |
| 42 |
#include <systemd/sd-daemon.h> |
#include <systemd/sd-daemon.h> |
| 43 |
|
|
| 44 |
|
struct process_sockaddr_t |
| 45 |
|
{ |
| 46 |
|
pid_t pid; |
| 47 |
|
in_addr_t s_addr; |
| 48 |
|
}; |
| 49 |
|
typedef struct process_sockaddr_t PROCESS_SOCKADDR; |
| 50 |
|
|
| 51 |
|
static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; |
| 52 |
|
|
| 53 |
int net_server(const char *hostaddr, in_port_t port) |
int net_server(const char *hostaddr, in_port_t port) |
| 54 |
{ |
{ |
| 55 |
unsigned int namelen; |
unsigned int namelen; |
| 60 |
int nfds, epollfd; |
int nfds, epollfd; |
| 61 |
siginfo_t siginfo; |
siginfo_t siginfo; |
| 62 |
int sd_notify_stopping = 0; |
int sd_notify_stopping = 0; |
| 63 |
|
MENU_SET *p_bbs_menu_new; |
| 64 |
|
int i, j; |
| 65 |
|
pid_t pid; |
| 66 |
|
|
| 67 |
socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 68 |
|
|
| 106 |
port_server = ntohs(sin.sin_port); |
port_server = ntohs(sin.sin_port); |
| 107 |
namelen = sizeof(sin); |
namelen = sizeof(sin); |
| 108 |
|
|
| 109 |
log_std("Listening at %s:%d\n", hostaddr_server, port_server); |
log_common("Listening at %s:%d\n", hostaddr_server, port_server); |
| 110 |
|
|
| 111 |
epollfd = epoll_create1(0); |
epollfd = epoll_create1(0); |
| 112 |
if (epollfd < 0) |
if (epollfd < 0) |
| 132 |
|
|
| 133 |
// Startup complete |
// Startup complete |
| 134 |
sd_notifyf(0, "READY=1\n" |
sd_notifyf(0, "READY=1\n" |
| 135 |
"STATUS=Accepting incoming connections...\n" |
"STATUS=Listening at %s:%d\n" |
| 136 |
"MAINPID=%d", |
"MAINPID=%d", |
| 137 |
getpid()); |
hostaddr_server, port_server, getpid()); |
| 138 |
|
|
| 139 |
while (!SYS_server_exit || SYS_child_process_count > 0) |
while (!SYS_server_exit || SYS_child_process_count > 0) |
| 140 |
{ |
{ |
| 143 |
sd_notify(0, "STOPPING=1"); |
sd_notify(0, "STOPPING=1"); |
| 144 |
sd_notify_stopping = 1; |
sd_notify_stopping = 1; |
| 145 |
} |
} |
| 146 |
|
|
| 147 |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
| 148 |
{ |
{ |
| 149 |
SYS_child_exit = 0; |
SYS_child_exit = 0; |
| 155 |
SYS_child_exit = 1; // Retry waitid |
SYS_child_exit = 1; // Retry waitid |
| 156 |
|
|
| 157 |
SYS_child_process_count--; |
SYS_child_process_count--; |
| 158 |
log_std("Child process (%d) exited\n", siginfo.si_pid); |
log_common("Child process (%d) exited\n", siginfo.si_pid); |
| 159 |
|
|
| 160 |
|
if (siginfo.si_pid != section_list_loader_pid) |
| 161 |
|
{ |
| 162 |
|
i = 0; |
| 163 |
|
for (; i < BBS_max_client; i++) |
| 164 |
|
{ |
| 165 |
|
if (process_sockaddr_pool[i].pid == siginfo.si_pid) |
| 166 |
|
{ |
| 167 |
|
process_sockaddr_pool[i].pid = 0; |
| 168 |
|
break; |
| 169 |
|
} |
| 170 |
|
} |
| 171 |
|
if (i >= BBS_max_client) |
| 172 |
|
{ |
| 173 |
|
log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid); |
| 174 |
|
} |
| 175 |
|
} |
| 176 |
} |
} |
| 177 |
else if (ret == 0) |
else if (ret == 0) |
| 178 |
{ |
{ |
| 187 |
|
|
| 188 |
if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) |
if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) |
| 189 |
{ |
{ |
| 190 |
log_std("Notify %d child process to exit\n", SYS_child_process_count); |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
| 191 |
if (kill(0, SIGTERM) < 0) |
if (kill(0, SIGTERM) < 0) |
| 192 |
{ |
{ |
| 193 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
| 196 |
sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count); |
| 197 |
} |
} |
| 198 |
|
|
| 199 |
if (SYS_menu_reload && !SYS_server_exit) |
if (SYS_conf_reload && !SYS_server_exit) |
| 200 |
{ |
{ |
| 201 |
SYS_menu_reload = 0; |
SYS_conf_reload = 0; |
| 202 |
|
sd_notify(0, "RELOADING=1"); |
| 203 |
|
|
| 204 |
|
// Reload configuration |
| 205 |
|
if (load_conf(CONF_BBSD) < 0) |
| 206 |
|
{ |
| 207 |
|
log_error("Reload conf failed\n"); |
| 208 |
|
} |
| 209 |
|
|
| 210 |
if (reload_menu(&bbs_menu) < 0) |
p_bbs_menu_new = calloc(1, sizeof(MENU_SET)); |
| 211 |
|
if (p_bbs_menu_new == NULL) |
| 212 |
{ |
{ |
| 213 |
|
log_error("OOM: calloc(MENU_SET)\n"); |
| 214 |
|
} |
| 215 |
|
else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0) |
| 216 |
|
{ |
| 217 |
|
unload_menu(p_bbs_menu_new); |
| 218 |
|
free(p_bbs_menu_new); |
| 219 |
|
p_bbs_menu_new = NULL; |
| 220 |
|
|
| 221 |
log_error("Reload menu failed\n"); |
log_error("Reload menu failed\n"); |
| 222 |
} |
} |
| 223 |
else |
else |
| 224 |
{ |
{ |
| 225 |
log_std("Reload menu successfully\n"); |
unload_menu(p_bbs_menu); |
| 226 |
|
free(p_bbs_menu); |
| 227 |
|
|
| 228 |
|
p_bbs_menu = p_bbs_menu_new; |
| 229 |
|
p_bbs_menu_new = NULL; |
| 230 |
|
|
| 231 |
|
log_common("Reload menu successfully\n"); |
| 232 |
|
} |
| 233 |
|
|
| 234 |
|
sd_notify(0, "READY=1"); |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
if (SYS_data_file_reload && !SYS_server_exit) |
| 238 |
|
{ |
| 239 |
|
SYS_data_file_reload = 0; |
| 240 |
|
sd_notify(0, "RELOADING=1"); |
| 241 |
|
|
| 242 |
|
for (int i = 0; i < data_files_load_startup_count; i++) |
| 243 |
|
{ |
| 244 |
|
if (load_file(data_files_load_startup[i]) < 0) |
| 245 |
|
{ |
| 246 |
|
log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); |
| 247 |
|
} |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
log_common("Reload data files successfully\n"); |
| 251 |
|
sd_notify(0, "READY=1"); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
|
if (SYS_section_list_reload && !SYS_server_exit) |
| 255 |
|
{ |
| 256 |
|
SYS_section_list_reload = 0; |
| 257 |
|
|
| 258 |
|
if (section_list_loader_reload() < 0) |
| 259 |
|
{ |
| 260 |
|
log_error("ksection_list_loader_reload() failed\n"); |
| 261 |
} |
} |
| 262 |
} |
} |
| 263 |
|
|
| 308 |
|
|
| 309 |
port_client = ntohs(sin.sin_port); |
port_client = ntohs(sin.sin_port); |
| 310 |
|
|
| 311 |
log_std("Accept connection from %s:%d\n", hostaddr_client, port_client); |
log_common("Accept connection from %s:%d\n", hostaddr_client, port_client); |
| 312 |
|
|
| 313 |
|
if (SYS_child_process_count - 1 < BBS_max_client) |
| 314 |
|
{ |
| 315 |
|
j = 0; |
| 316 |
|
for (i = 0; i < BBS_max_client; i++) |
| 317 |
|
{ |
| 318 |
|
if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr) |
| 319 |
|
{ |
| 320 |
|
j++; |
| 321 |
|
if (j >= BBS_max_client_per_ip) |
| 322 |
|
{ |
| 323 |
|
log_error("Too many client connections (%d) from %s\n", j, hostaddr_client); |
| 324 |
|
break; |
| 325 |
|
} |
| 326 |
|
} |
| 327 |
|
} |
| 328 |
|
|
| 329 |
if (fork_server() < 0) |
if (j < BBS_max_client_per_ip) |
| 330 |
|
{ |
| 331 |
|
if ((pid = fork_server()) < 0) |
| 332 |
|
{ |
| 333 |
|
log_error("fork_server() error\n"); |
| 334 |
|
} |
| 335 |
|
else if (pid > 0) |
| 336 |
|
{ |
| 337 |
|
i = 0; |
| 338 |
|
for (; i < BBS_max_client; i++) |
| 339 |
|
{ |
| 340 |
|
if (process_sockaddr_pool[i].pid == 0) |
| 341 |
|
{ |
| 342 |
|
break; |
| 343 |
|
} |
| 344 |
|
} |
| 345 |
|
|
| 346 |
|
if (i >= BBS_max_client) |
| 347 |
|
{ |
| 348 |
|
log_error("Process sockaddr pool depleted\n"); |
| 349 |
|
} |
| 350 |
|
else |
| 351 |
|
{ |
| 352 |
|
process_sockaddr_pool[i].pid = pid; |
| 353 |
|
process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr; |
| 354 |
|
} |
| 355 |
|
} |
| 356 |
|
} |
| 357 |
|
} |
| 358 |
|
else |
| 359 |
{ |
{ |
| 360 |
log_error("fork_server() error\n"); |
log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1); |
| 361 |
} |
} |
| 362 |
|
|
| 363 |
if (close(socket_client) == -1) |
if (close(socket_client) == -1) |