| 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> |
| 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_CLIENT_LIMIT]; |
| 53 |
|
|
| 54 |
int net_server(const char *hostaddr, in_port_t port) |
int net_server(const char *hostaddr, in_port_t port) |
| 55 |
{ |
{ |
| 56 |
|
ssh_bind sshbind; |
| 57 |
unsigned int namelen; |
unsigned int namelen; |
| 58 |
int ret; |
int ret; |
| 59 |
int flags; |
int flags; |
| 61 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 62 |
int nfds, epollfd; |
int nfds, epollfd; |
| 63 |
siginfo_t siginfo; |
siginfo_t siginfo; |
| 64 |
|
int sd_notify_stopping = 0; |
| 65 |
|
MENU_SET *p_bbs_menu_new; |
| 66 |
|
int i, j; |
| 67 |
|
pid_t pid; |
| 68 |
|
int ssh_log_level = SSH_LOG_NOLOG; |
| 69 |
|
|
| 70 |
|
ssh_init(); |
| 71 |
|
|
| 72 |
|
sshbind = ssh_bind_new(); |
| 73 |
|
|
| 74 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
| 75 |
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
| 76 |
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 || |
| 77 |
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
| 78 |
|
{ |
| 79 |
|
log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); |
| 80 |
|
ssh_bind_free(sshbind); |
| 81 |
|
return -1; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 85 |
|
|
| 86 |
if (socket_server < 0) |
if (socket_server < 0) |
| 123 |
port_server = ntohs(sin.sin_port); |
port_server = ntohs(sin.sin_port); |
| 124 |
namelen = sizeof(sin); |
namelen = sizeof(sin); |
| 125 |
|
|
| 126 |
log_std("Listening at %s:%d\n", hostaddr_server, port_server); |
log_common("Listening at %s:%d\n", hostaddr_server, port_server); |
| 127 |
|
|
| 128 |
epollfd = epoll_create1(0); |
epollfd = epoll_create1(0); |
| 129 |
if (epollfd < 0) |
if (epollfd < 0) |
| 147 |
flags = fcntl(socket_server, F_GETFL, 0); |
flags = fcntl(socket_server, F_GETFL, 0); |
| 148 |
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
| 149 |
|
|
| 150 |
|
// Startup complete |
| 151 |
|
sd_notifyf(0, "READY=1\n" |
| 152 |
|
"STATUS=Listening at %s:%d\n" |
| 153 |
|
"MAINPID=%d", |
| 154 |
|
hostaddr_server, port_server, getpid()); |
| 155 |
|
|
| 156 |
while (!SYS_server_exit || SYS_child_process_count > 0) |
while (!SYS_server_exit || SYS_child_process_count > 0) |
| 157 |
{ |
{ |
| 158 |
|
if (SYS_server_exit && !sd_notify_stopping) |
| 159 |
|
{ |
| 160 |
|
sd_notify(0, "STOPPING=1"); |
| 161 |
|
sd_notify_stopping = 1; |
| 162 |
|
} |
| 163 |
|
|
| 164 |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
| 165 |
{ |
{ |
| 166 |
SYS_child_exit = 0; |
SYS_child_exit = 0; |
| 172 |
SYS_child_exit = 1; // Retry waitid |
SYS_child_exit = 1; // Retry waitid |
| 173 |
|
|
| 174 |
SYS_child_process_count--; |
SYS_child_process_count--; |
| 175 |
log_std("Child process (%d) exited\n", siginfo.si_pid); |
log_common("Child process (%d) exited\n", siginfo.si_pid); |
| 176 |
|
|
| 177 |
|
if (siginfo.si_pid != section_list_loader_pid) |
| 178 |
|
{ |
| 179 |
|
i = 0; |
| 180 |
|
for (; i < BBS_max_client; i++) |
| 181 |
|
{ |
| 182 |
|
if (process_sockaddr_pool[i].pid == siginfo.si_pid) |
| 183 |
|
{ |
| 184 |
|
process_sockaddr_pool[i].pid = 0; |
| 185 |
|
break; |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
|
if (i >= BBS_max_client) |
| 189 |
|
{ |
| 190 |
|
log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid); |
| 191 |
|
} |
| 192 |
|
} |
| 193 |
} |
} |
| 194 |
else if (ret == 0) |
else if (ret == 0) |
| 195 |
{ |
{ |
| 204 |
|
|
| 205 |
if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) |
if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) |
| 206 |
{ |
{ |
| 207 |
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); |
| 208 |
if (kill(0, SIGTERM) < 0) |
if (kill(0, SIGTERM) < 0) |
| 209 |
{ |
{ |
| 210 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
| 211 |
} |
} |
| 212 |
|
|
| 213 |
|
sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count); |
| 214 |
} |
} |
| 215 |
|
|
| 216 |
if (SYS_menu_reload && !SYS_server_exit) |
if (SYS_conf_reload && !SYS_server_exit) |
| 217 |
{ |
{ |
| 218 |
SYS_menu_reload = 0; |
SYS_conf_reload = 0; |
| 219 |
|
sd_notify(0, "RELOADING=1"); |
| 220 |
|
|
| 221 |
|
// Reload configuration |
| 222 |
|
if (load_conf(CONF_BBSD) < 0) |
| 223 |
|
{ |
| 224 |
|
log_error("Reload conf failed\n"); |
| 225 |
|
} |
| 226 |
|
|
| 227 |
if (reload_menu(&bbs_menu) < 0) |
p_bbs_menu_new = calloc(1, sizeof(MENU_SET)); |
| 228 |
|
if (p_bbs_menu_new == NULL) |
| 229 |
|
{ |
| 230 |
|
log_error("OOM: calloc(MENU_SET)\n"); |
| 231 |
|
} |
| 232 |
|
else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0) |
| 233 |
{ |
{ |
| 234 |
|
unload_menu(p_bbs_menu_new); |
| 235 |
|
free(p_bbs_menu_new); |
| 236 |
|
p_bbs_menu_new = NULL; |
| 237 |
|
|
| 238 |
log_error("Reload menu failed\n"); |
log_error("Reload menu failed\n"); |
| 239 |
} |
} |
| 240 |
else |
else |
| 241 |
{ |
{ |
| 242 |
log_std("Reload menu successfully\n"); |
unload_menu(p_bbs_menu); |
| 243 |
|
free(p_bbs_menu); |
| 244 |
|
|
| 245 |
|
p_bbs_menu = p_bbs_menu_new; |
| 246 |
|
p_bbs_menu_new = NULL; |
| 247 |
|
|
| 248 |
|
log_common("Reload menu successfully\n"); |
| 249 |
|
} |
| 250 |
|
|
| 251 |
|
sd_notify(0, "READY=1"); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
|
if (SYS_data_file_reload && !SYS_server_exit) |
| 255 |
|
{ |
| 256 |
|
SYS_data_file_reload = 0; |
| 257 |
|
sd_notify(0, "RELOADING=1"); |
| 258 |
|
|
| 259 |
|
for (int i = 0; i < data_files_load_startup_count; i++) |
| 260 |
|
{ |
| 261 |
|
if (load_file(data_files_load_startup[i]) < 0) |
| 262 |
|
{ |
| 263 |
|
log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); |
| 264 |
|
} |
| 265 |
|
} |
| 266 |
|
|
| 267 |
|
log_common("Reload data files successfully\n"); |
| 268 |
|
sd_notify(0, "READY=1"); |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
if (SYS_section_list_reload && !SYS_server_exit) |
| 272 |
|
{ |
| 273 |
|
SYS_section_list_reload = 0; |
| 274 |
|
|
| 275 |
|
if (section_list_loader_reload() < 0) |
| 276 |
|
{ |
| 277 |
|
log_error("ksection_list_loader_reload() failed\n"); |
| 278 |
} |
} |
| 279 |
} |
} |
| 280 |
|
|
| 325 |
|
|
| 326 |
port_client = ntohs(sin.sin_port); |
port_client = ntohs(sin.sin_port); |
| 327 |
|
|
| 328 |
log_std("Accept connection from %s:%d\n", hostaddr_client, port_client); |
log_common("Accept connection from %s:%d\n", hostaddr_client, port_client); |
| 329 |
|
|
| 330 |
if (fork_server() < 0) |
if (SYS_child_process_count - 1 < BBS_max_client) |
| 331 |
{ |
{ |
| 332 |
log_error("fork_server() error\n"); |
j = 0; |
| 333 |
|
for (i = 0; i < BBS_max_client; i++) |
| 334 |
|
{ |
| 335 |
|
if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr) |
| 336 |
|
{ |
| 337 |
|
j++; |
| 338 |
|
if (j >= BBS_max_client_per_ip) |
| 339 |
|
{ |
| 340 |
|
log_common("Too many client connections (%d) from %s\n", j, hostaddr_client); |
| 341 |
|
break; |
| 342 |
|
} |
| 343 |
|
} |
| 344 |
|
} |
| 345 |
|
|
| 346 |
|
if (j < BBS_max_client_per_ip) |
| 347 |
|
{ |
| 348 |
|
if ((pid = fork_server(sshbind)) < 0) |
| 349 |
|
{ |
| 350 |
|
log_error("fork_server() error\n"); |
| 351 |
|
} |
| 352 |
|
else if (pid > 0) |
| 353 |
|
{ |
| 354 |
|
i = 0; |
| 355 |
|
for (; i < BBS_max_client; i++) |
| 356 |
|
{ |
| 357 |
|
if (process_sockaddr_pool[i].pid == 0) |
| 358 |
|
{ |
| 359 |
|
break; |
| 360 |
|
} |
| 361 |
|
} |
| 362 |
|
|
| 363 |
|
if (i >= BBS_max_client) |
| 364 |
|
{ |
| 365 |
|
log_error("Process sockaddr pool depleted\n"); |
| 366 |
|
} |
| 367 |
|
else |
| 368 |
|
{ |
| 369 |
|
process_sockaddr_pool[i].pid = pid; |
| 370 |
|
process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr; |
| 371 |
|
} |
| 372 |
|
} |
| 373 |
|
} |
| 374 |
|
} |
| 375 |
|
else |
| 376 |
|
{ |
| 377 |
|
log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1); |
| 378 |
} |
} |
| 379 |
|
|
| 380 |
if (close(socket_client) == -1) |
if (close(socket_client) == -1) |
| 398 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed\n"); |
| 399 |
} |
} |
| 400 |
|
|
| 401 |
|
ssh_bind_free(sshbind); |
| 402 |
|
ssh_finalize(); |
| 403 |
|
|
| 404 |
return 0; |
return 0; |
| 405 |
} |
} |