| 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> |
| 36 |
#include <sys/syscall.h> |
#include <sys/syscall.h> |
| 37 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 38 |
#include <sys/wait.h> |
#include <sys/wait.h> |
| 39 |
|
#include <sys/epoll.h> |
| 40 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
| 41 |
|
#include <systemd/sd-daemon.h> |
| 42 |
|
#include <libssh/libssh.h> |
| 43 |
|
#include <libssh/server.h> |
| 44 |
|
|
| 45 |
|
struct process_sockaddr_t |
| 46 |
|
{ |
| 47 |
|
pid_t pid; |
| 48 |
|
in_addr_t s_addr; |
| 49 |
|
}; |
| 50 |
|
typedef struct process_sockaddr_t PROCESS_SOCKADDR; |
| 51 |
|
|
| 52 |
|
static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENTS_LIMIT]; |
| 53 |
|
|
| 54 |
int net_server(const char *hostaddr, in_port_t port) |
int net_server(const char *hostaddr, in_port_t port) |
| 55 |
{ |
{ |
| 57 |
int ret; |
int ret; |
| 58 |
int flags; |
int flags; |
| 59 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 60 |
fd_set testfds; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 61 |
struct timeval timeout; |
int nfds, epollfd; |
|
sigset_t nsigset; |
|
|
sigset_t osigset; |
|
| 62 |
siginfo_t siginfo; |
siginfo_t siginfo; |
| 63 |
|
int sd_notify_stopping = 0; |
| 64 |
|
MENU_SET *p_bbs_menu_new; |
| 65 |
|
int i, j; |
| 66 |
|
pid_t pid; |
| 67 |
|
|
| 68 |
socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 69 |
|
|
| 70 |
if (socket_server < 0) |
if (socket_server < 0) |
| 71 |
{ |
{ |
| 72 |
log_error("Create socket failed\n"); |
log_error("Create socket failed\n"); |
| 73 |
exit(1); |
return -1; |
| 74 |
} |
} |
| 75 |
|
|
| 76 |
sin.sin_family = AF_INET; |
sin.sin_family = AF_INET; |
| 77 |
sin.sin_addr.s_addr = |
sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY); |
|
(strnlen(hostaddr, sizeof(hostaddr)) > 0 ? inet_addr(hostaddr) : INADDR_ANY); |
|
| 78 |
sin.sin_port = htons(port); |
sin.sin_port = htons(port); |
| 79 |
|
|
| 80 |
|
// Reuse address and port |
| 81 |
|
flags = 1; |
| 82 |
|
if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) < 0) |
| 83 |
|
{ |
| 84 |
|
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
| 85 |
|
} |
| 86 |
|
if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(flags)) < 0) |
| 87 |
|
{ |
| 88 |
|
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 92 |
{ |
{ |
| 93 |
log_error("Bind address %s:%u failed\n", |
log_error("Bind address %s:%u failed (%d)\n", |
| 94 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
| 95 |
exit(2); |
return -1; |
| 96 |
} |
} |
| 97 |
|
|
| 98 |
if (listen(socket_server, 10) < 0) |
if (listen(socket_server, 10) < 0) |
| 99 |
{ |
{ |
| 100 |
log_error("Socket listen failed\n"); |
log_error("Socket listen failed (%d)\n", errno); |
| 101 |
exit(3); |
return -1; |
| 102 |
} |
} |
| 103 |
|
|
| 104 |
strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1); |
strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1); |
| 109 |
|
|
| 110 |
log_std("Listening at %s:%d\n", hostaddr_server, port_server); |
log_std("Listening at %s:%d\n", hostaddr_server, port_server); |
| 111 |
|
|
| 112 |
sigemptyset(&nsigset); |
epollfd = epoll_create1(0); |
| 113 |
sigaddset(&nsigset, SIGHUP); |
if (epollfd < 0) |
| 114 |
sigaddset(&nsigset, SIGCHLD); |
{ |
| 115 |
sigaddset(&nsigset, SIGTERM); |
log_error("epoll_create1() error (%d)\n", errno); |
| 116 |
|
return -1; |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
ev.events = EPOLLIN; |
| 120 |
|
ev.data.fd = socket_server; |
| 121 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server, &ev) == -1) |
| 122 |
|
{ |
| 123 |
|
log_error("epoll_ctl(socket_server) error (%d)\n", errno); |
| 124 |
|
if (close(epollfd) < 0) |
| 125 |
|
{ |
| 126 |
|
log_error("close(epoll) error (%d)\n"); |
| 127 |
|
} |
| 128 |
|
return -1; |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
flags = fcntl(socket_server, F_GETFL, 0); |
| 132 |
|
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
| 133 |
|
|
| 134 |
|
// Startup complete |
| 135 |
|
sd_notifyf(0, "READY=1\n" |
| 136 |
|
"STATUS=Listening at %s:%d\n" |
| 137 |
|
"MAINPID=%d", |
| 138 |
|
hostaddr_server, port_server, getpid()); |
| 139 |
|
|
| 140 |
while (!SYS_server_exit || SYS_child_process_count > 0) |
while (!SYS_server_exit || SYS_child_process_count > 0) |
| 141 |
{ |
{ |
| 142 |
sigprocmask(SIG_BLOCK, &nsigset, &osigset); |
if (SYS_server_exit && !sd_notify_stopping) |
| 143 |
|
{ |
| 144 |
|
sd_notify(0, "STOPPING=1"); |
| 145 |
|
sd_notify_stopping = 1; |
| 146 |
|
} |
| 147 |
|
|
| 148 |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
| 149 |
{ |
{ |
| 150 |
|
SYS_child_exit = 0; |
| 151 |
|
|
| 152 |
siginfo.si_pid = 0; |
siginfo.si_pid = 0; |
| 153 |
ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG); |
ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG); |
| 154 |
if (ret == 0 && siginfo.si_pid > 0) |
if (ret == 0 && siginfo.si_pid > 0) |
| 155 |
{ |
{ |
| 156 |
|
SYS_child_exit = 1; // Retry waitid |
| 157 |
|
|
| 158 |
SYS_child_process_count--; |
SYS_child_process_count--; |
| 159 |
log_std("Child process (%d) exited\n", siginfo.si_pid); |
log_std("Child process (%d) exited\n", siginfo.si_pid); |
| 160 |
|
|
| 161 |
|
if (siginfo.si_pid != section_list_loader_pid) |
| 162 |
|
{ |
| 163 |
|
i = 0; |
| 164 |
|
for (; i < BBS_max_client; i++) |
| 165 |
|
{ |
| 166 |
|
if (process_sockaddr_pool[i].pid == siginfo.si_pid) |
| 167 |
|
{ |
| 168 |
|
process_sockaddr_pool[i].pid = 0; |
| 169 |
|
break; |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
if (i >= BBS_max_client) |
| 173 |
|
{ |
| 174 |
|
log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid); |
| 175 |
|
} |
| 176 |
|
} |
| 177 |
} |
} |
| 178 |
else if (ret == 0) |
else if (ret == 0) |
| 179 |
{ |
{ |
|
SYS_child_exit = 0; |
|
| 180 |
break; |
break; |
| 181 |
} |
} |
| 182 |
else if (ret < 0) |
else if (ret < 0) |
| 193 |
{ |
{ |
| 194 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
| 195 |
} |
} |
| 196 |
|
|
| 197 |
|
sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count); |
| 198 |
} |
} |
| 199 |
|
|
| 200 |
if (SYS_menu_reload && !SYS_server_exit) |
if (SYS_conf_reload && !SYS_server_exit) |
| 201 |
{ |
{ |
| 202 |
if (reload_menu(&bbs_menu) < 0) |
SYS_conf_reload = 0; |
| 203 |
|
sd_notify(0, "RELOADING=1"); |
| 204 |
|
|
| 205 |
|
// Reload configuration |
| 206 |
|
if (load_conf(CONF_BBSD) < 0) |
| 207 |
|
{ |
| 208 |
|
log_error("Reload conf failed\n"); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
p_bbs_menu_new = calloc(1, sizeof(MENU_SET)); |
| 212 |
|
if (p_bbs_menu_new == NULL) |
| 213 |
{ |
{ |
| 214 |
|
log_error("OOM: calloc(MENU_SET)\n"); |
| 215 |
|
} |
| 216 |
|
else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0) |
| 217 |
|
{ |
| 218 |
|
unload_menu(p_bbs_menu_new); |
| 219 |
|
free(p_bbs_menu_new); |
| 220 |
|
p_bbs_menu_new = NULL; |
| 221 |
|
|
| 222 |
log_error("Reload menu failed\n"); |
log_error("Reload menu failed\n"); |
| 223 |
} |
} |
| 224 |
else |
else |
| 225 |
{ |
{ |
| 226 |
|
unload_menu(p_bbs_menu); |
| 227 |
|
free(p_bbs_menu); |
| 228 |
|
|
| 229 |
|
p_bbs_menu = p_bbs_menu_new; |
| 230 |
|
p_bbs_menu_new = NULL; |
| 231 |
|
|
| 232 |
log_std("Reload menu successfully\n"); |
log_std("Reload menu successfully\n"); |
| 233 |
} |
} |
|
SYS_menu_reload = 0; |
|
|
} |
|
| 234 |
|
|
| 235 |
sigprocmask(SIG_SETMASK, &osigset, NULL); |
sd_notify(0, "READY=1"); |
| 236 |
|
} |
| 237 |
|
|
| 238 |
FD_ZERO(&testfds); |
if (SYS_data_file_reload && !SYS_server_exit) |
| 239 |
FD_SET(socket_server, &testfds); |
{ |
| 240 |
|
SYS_data_file_reload = 0; |
| 241 |
|
sd_notify(0, "RELOADING=1"); |
| 242 |
|
|
| 243 |
timeout.tv_sec = 0; |
for (int i = 0; i < data_files_load_startup_count; i++) |
| 244 |
timeout.tv_usec = 100 * 1000; // 0.1 second |
{ |
| 245 |
|
if (load_file(data_files_load_startup[i]) < 0) |
| 246 |
|
{ |
| 247 |
|
log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); |
| 248 |
|
} |
| 249 |
|
} |
| 250 |
|
|
| 251 |
ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout); |
log_std("Reload data files successfully\n"); |
| 252 |
|
sd_notify(0, "READY=1"); |
| 253 |
|
} |
| 254 |
|
|
| 255 |
if (ret < 0) |
if (SYS_section_list_reload && !SYS_server_exit) |
| 256 |
{ |
{ |
| 257 |
if (errno != EINTR) |
SYS_section_list_reload = 0; |
| 258 |
|
|
| 259 |
|
if (section_list_loader_reload() < 0) |
| 260 |
{ |
{ |
| 261 |
log_error("Accept connection error: %d\n", errno); |
log_error("ksection_list_loader_reload() failed\n"); |
| 262 |
} |
} |
|
continue; |
|
| 263 |
} |
} |
| 264 |
else if (ret == 0) // timeout |
|
| 265 |
|
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 266 |
|
|
| 267 |
|
if (nfds < 0) |
| 268 |
{ |
{ |
| 269 |
|
if (errno != EINTR) |
| 270 |
|
{ |
| 271 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 272 |
|
break; |
| 273 |
|
} |
| 274 |
continue; |
continue; |
| 275 |
} |
} |
| 276 |
|
|
| 280 |
continue; |
continue; |
| 281 |
} |
} |
| 282 |
|
|
| 283 |
if (FD_ISSET(socket_server, &testfds)) |
for (int i = 0; i < nfds; i++) |
| 284 |
{ |
{ |
| 285 |
flags = fcntl(socket_server, F_GETFL, 0); |
if (events[i].data.fd == socket_server) |
|
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
|
|
while ((socket_client = |
|
|
accept(socket_server, (struct sockaddr *)&sin, &namelen)) < 0) |
|
| 286 |
{ |
{ |
| 287 |
if (errno != EWOULDBLOCK && errno != ECONNABORTED && errno != EINTR) |
while (!SYS_server_exit) // Accept all incoming connections until error |
| 288 |
{ |
{ |
| 289 |
log_error("Accept connection error\n"); |
socket_client = accept(socket_server, (struct sockaddr *)&sin, &namelen); |
| 290 |
break; |
if (socket_client < 0) |
| 291 |
|
{ |
| 292 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 293 |
|
{ |
| 294 |
|
break; |
| 295 |
|
} |
| 296 |
|
else if (errno == EINTR) |
| 297 |
|
{ |
| 298 |
|
continue; |
| 299 |
|
} |
| 300 |
|
else |
| 301 |
|
{ |
| 302 |
|
log_error("accept(socket_server) error (%d)\n", errno); |
| 303 |
|
break; |
| 304 |
|
} |
| 305 |
|
} |
| 306 |
|
|
| 307 |
|
strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1); |
| 308 |
|
hostaddr_client[sizeof(hostaddr_client) - 1] = '\0'; |
| 309 |
|
|
| 310 |
|
port_client = ntohs(sin.sin_port); |
| 311 |
|
|
| 312 |
|
log_std("Accept connection from %s:%d\n", hostaddr_client, port_client); |
| 313 |
|
|
| 314 |
|
if (SYS_child_process_count - 1 < BBS_max_client) |
| 315 |
|
{ |
| 316 |
|
j = 0; |
| 317 |
|
for (i = 0; i < BBS_max_client; i++) |
| 318 |
|
{ |
| 319 |
|
if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr) |
| 320 |
|
{ |
| 321 |
|
j++; |
| 322 |
|
if (j >= BBS_max_client_per_ip) |
| 323 |
|
{ |
| 324 |
|
log_error("Too many client connections (%d) from %s\n", j, hostaddr_client); |
| 325 |
|
break; |
| 326 |
|
} |
| 327 |
|
} |
| 328 |
|
} |
| 329 |
|
|
| 330 |
|
if (j < BBS_max_client_per_ip) |
| 331 |
|
{ |
| 332 |
|
if ((pid = fork_server()) < 0) |
| 333 |
|
{ |
| 334 |
|
log_error("fork_server() error\n"); |
| 335 |
|
} |
| 336 |
|
else if (pid > 0) |
| 337 |
|
{ |
| 338 |
|
i = 0; |
| 339 |
|
for (; i < BBS_max_client; i++) |
| 340 |
|
{ |
| 341 |
|
if (process_sockaddr_pool[i].pid == 0) |
| 342 |
|
{ |
| 343 |
|
break; |
| 344 |
|
} |
| 345 |
|
} |
| 346 |
|
|
| 347 |
|
if (i >= BBS_max_client) |
| 348 |
|
{ |
| 349 |
|
log_error("Process sockaddr pool depleted\n"); |
| 350 |
|
} |
| 351 |
|
else |
| 352 |
|
{ |
| 353 |
|
process_sockaddr_pool[i].pid = pid; |
| 354 |
|
process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr; |
| 355 |
|
} |
| 356 |
|
} |
| 357 |
|
} |
| 358 |
|
} |
| 359 |
|
else |
| 360 |
|
{ |
| 361 |
|
log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1); |
| 362 |
|
} |
| 363 |
|
|
| 364 |
|
if (close(socket_client) == -1) |
| 365 |
|
{ |
| 366 |
|
log_error("close(socket_lient) error (%d)\n", errno); |
| 367 |
|
} |
| 368 |
} |
} |
| 369 |
} |
} |
|
fcntl(socket_server, F_SETFL, flags); |
|
|
} |
|
|
|
|
|
if (socket_client < 0) |
|
|
{ |
|
|
log_error("Accept connection error\n"); |
|
|
continue; |
|
|
} |
|
|
|
|
|
strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1); |
|
|
hostaddr_client[sizeof(hostaddr_client) - 1] = '\0'; |
|
|
|
|
|
port_client = ntohs(sin.sin_port); |
|
|
|
|
|
log_std("Accept connection from %s:%d\n", hostaddr_client, |
|
|
port_client); |
|
|
|
|
|
if (fork_server() < 0) |
|
|
{ |
|
|
log_error("Fork error\n"); |
|
| 370 |
} |
} |
| 371 |
|
} |
| 372 |
|
|
| 373 |
if (close(socket_client) == -1) |
if (close(epollfd) < 0) |
| 374 |
{ |
{ |
| 375 |
log_error("Close client socket failed\n"); |
log_error("close(epoll) error (%d)\n"); |
|
} |
|
| 376 |
} |
} |
| 377 |
|
|
| 378 |
|
fcntl(socket_server, F_SETFL, flags); |
| 379 |
|
|
| 380 |
if (close(socket_server) == -1) |
if (close(socket_server) == -1) |
| 381 |
{ |
{ |
| 382 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed\n"); |