| 38 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 39 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 40 |
#include <netinet/in.h> |
#include <netinet/in.h> |
| 41 |
#include <sys/epoll.h> |
#include <sys/ioctl.h> |
| 42 |
#include <sys/socket.h> |
#include <sys/socket.h> |
|
#include <sys/syscall.h> |
|
| 43 |
#include <sys/types.h> |
#include <sys/types.h> |
| 44 |
#include <sys/wait.h> |
#include <sys/wait.h> |
| 45 |
|
|
| 46 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 47 |
|
#include <sys/epoll.h> |
| 48 |
|
#else |
| 49 |
|
#include <poll.h> |
| 50 |
|
#endif |
| 51 |
|
|
| 52 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 53 |
#include <systemd/sd-daemon.h> |
#include <systemd/sd-daemon.h> |
| 54 |
|
#endif |
| 55 |
|
|
| 56 |
enum _net_server_constant_t |
enum _net_server_constant_t |
| 57 |
{ |
{ |
| 61 |
SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds |
SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds |
| 62 |
}; |
}; |
| 63 |
|
|
|
static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; |
|
|
|
|
| 64 |
/* A userdata struct for session. */ |
/* A userdata struct for session. */ |
| 65 |
struct session_data_struct |
struct session_data_struct |
| 66 |
{ |
{ |
| 87 |
struct winsize *winsize; |
struct winsize *winsize; |
| 88 |
}; |
}; |
| 89 |
|
|
| 90 |
|
static int socket_server[2]; |
| 91 |
|
static int socket_client; |
| 92 |
|
|
| 93 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 94 |
|
static int epollfd_server = -1; |
| 95 |
|
#endif |
| 96 |
|
|
| 97 |
|
static ssh_bind sshbind; |
| 98 |
|
|
| 99 |
|
static HASH_DICT *hash_dict_pid_sockaddr = NULL; |
| 100 |
|
static HASH_DICT *hash_dict_sockaddr_count = NULL; |
| 101 |
|
|
| 102 |
|
static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; |
| 103 |
|
|
| 104 |
static int auth_password(ssh_session session, const char *user, |
static int auth_password(ssh_session session, const char *user, |
| 105 |
const char *password, void *userdata) |
const char *password, void *userdata) |
| 106 |
{ |
{ |
| 327 |
} |
} |
| 328 |
|
|
| 329 |
// Child process |
// Child process |
| 330 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 331 |
|
if (close(epollfd_server) < 0) |
| 332 |
|
{ |
| 333 |
|
log_error("close(epollfd_server) error (%d)\n"); |
| 334 |
|
} |
| 335 |
|
#endif |
| 336 |
|
|
| 337 |
if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1) |
for (i = 0; i < 2; i++) |
| 338 |
{ |
{ |
| 339 |
log_error("Close server socket failed\n"); |
if (close(socket_server[i]) == -1) |
| 340 |
|
{ |
| 341 |
|
log_error("Close server socket failed\n"); |
| 342 |
|
} |
| 343 |
} |
} |
| 344 |
|
|
| 345 |
|
hash_dict_destroy(hash_dict_pid_sockaddr); |
| 346 |
|
hash_dict_destroy(hash_dict_sockaddr_count); |
| 347 |
|
|
| 348 |
SSH_session = ssh_new(); |
SSH_session = ssh_new(); |
| 349 |
|
|
| 350 |
if (SSH_v2) |
if (SSH_v2) |
| 438 |
goto cleanup; |
goto cleanup; |
| 439 |
} |
} |
| 440 |
|
|
| 441 |
|
if (io_init() < 0) |
| 442 |
|
{ |
| 443 |
|
log_error("io_init() error\n"); |
| 444 |
|
goto cleanup; |
| 445 |
|
} |
| 446 |
|
|
| 447 |
SYS_child_process_count = 0; |
SYS_child_process_count = 0; |
| 448 |
|
|
| 449 |
|
// BWF compile |
| 450 |
|
if (bwf_compile() < 0) |
| 451 |
|
{ |
| 452 |
|
log_error("bwf_compile() error\n"); |
| 453 |
|
goto cleanup; |
| 454 |
|
} |
| 455 |
|
|
| 456 |
bbs_main(); |
bbs_main(); |
| 457 |
|
|
| 458 |
cleanup: |
cleanup: |
| 461 |
|
|
| 462 |
if (SSH_v2) |
if (SSH_v2) |
| 463 |
{ |
{ |
| 464 |
close(cdata.pty_master); |
if (cdata.pty_master != -1) |
| 465 |
close(cdata.child_stdin); |
{ |
| 466 |
close(cdata.child_stdout); |
close(cdata.pty_master); |
| 467 |
close(cdata.child_stderr); |
} |
| 468 |
|
if (cdata.child_stdin != -1) |
| 469 |
|
{ |
| 470 |
|
close(cdata.child_stdin); |
| 471 |
|
} |
| 472 |
|
if (cdata.child_stdout != -1) |
| 473 |
|
{ |
| 474 |
|
close(cdata.child_stdout); |
| 475 |
|
} |
| 476 |
|
if (cdata.child_stderr != -1) |
| 477 |
|
{ |
| 478 |
|
close(cdata.child_stderr); |
| 479 |
|
} |
| 480 |
|
|
| 481 |
ssh_channel_free(SSH_channel); |
ssh_channel_free(SSH_channel); |
| 482 |
ssh_disconnect(SSH_session); |
ssh_disconnect(SSH_session); |
| 489 |
ssh_free(SSH_session); |
ssh_free(SSH_session); |
| 490 |
ssh_finalize(); |
ssh_finalize(); |
| 491 |
|
|
| 492 |
|
// BWF cleanup |
| 493 |
|
bwf_cleanup(); |
| 494 |
|
|
| 495 |
// Close Input and Output for client |
// Close Input and Output for client |
| 496 |
|
io_cleanup(); |
| 497 |
close(STDIN_FILENO); |
close(STDIN_FILENO); |
| 498 |
close(STDOUT_FILENO); |
close(STDOUT_FILENO); |
| 499 |
|
|
| 507 |
|
|
| 508 |
int net_server(const char *hostaddr, in_port_t port[]) |
int net_server(const char *hostaddr, in_port_t port[]) |
| 509 |
{ |
{ |
|
HASH_DICT *hash_dict_pid_sockaddr = NULL; |
|
|
HASH_DICT *hash_dict_sockaddr_count = NULL; |
|
|
|
|
| 510 |
unsigned int addrlen; |
unsigned int addrlen; |
| 511 |
int ret; |
int ret; |
| 512 |
int flags[2]; |
int flags_server[2]; |
| 513 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 514 |
|
|
| 515 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 516 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 517 |
int nfds, epollfd; |
#else |
| 518 |
siginfo_t siginfo; |
struct pollfd pfds[2]; |
| 519 |
|
#endif |
| 520 |
|
|
| 521 |
|
int nfds; |
| 522 |
int notify_child_exit = 0; |
int notify_child_exit = 0; |
| 523 |
time_t tm_notify_child_exit = time(NULL); |
time_t tm_notify_child_exit = time(NULL); |
|
int sd_notify_stopping = 0; |
|
|
MENU_SET bbs_menu_new; |
|
|
MENU_SET top10_menu_new; |
|
| 524 |
int i, j; |
int i, j; |
| 525 |
pid_t pid; |
pid_t pid; |
| 526 |
|
int ssh_key_valid = 0; |
| 527 |
int ssh_log_level = SSH_LOG_NOLOG; |
int ssh_log_level = SSH_LOG_NOLOG; |
| 528 |
|
|
| 529 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 530 |
|
int sd_notify_stopping = 0; |
| 531 |
|
#endif |
| 532 |
|
|
| 533 |
ssh_init(); |
ssh_init(); |
| 534 |
|
|
| 535 |
sshbind = ssh_bind_new(); |
sshbind = ssh_bind_new(); |
| 536 |
|
|
| 537 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0) |
| 538 |
|
{ |
| 539 |
|
log_error("Error setting SSH RSA key: %s\n", SSH_HOST_RSA_KEY_FILE); |
| 540 |
|
} |
| 541 |
|
else |
| 542 |
|
{ |
| 543 |
|
ssh_key_valid = 1; |
| 544 |
|
} |
| 545 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0) |
| 546 |
|
{ |
| 547 |
|
log_error("Error setting SSH ED25519 key: %s\n", SSH_HOST_ED25519_KEY_FILE); |
| 548 |
|
} |
| 549 |
|
else |
| 550 |
|
{ |
| 551 |
|
ssh_key_valid = 1; |
| 552 |
|
} |
| 553 |
|
|
| 554 |
|
if (!ssh_key_valid) |
| 555 |
|
{ |
| 556 |
|
log_error("Error: no valid SSH host key\n"); |
| 557 |
|
ssh_bind_free(sshbind); |
| 558 |
|
return -1; |
| 559 |
|
} |
| 560 |
|
|
| 561 |
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
| 562 |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
| 563 |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 || |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-ed25519") < 0 || |
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 || |
|
| 564 |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
| 565 |
{ |
{ |
| 566 |
log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); |
log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); |
| 568 |
return -1; |
return -1; |
| 569 |
} |
} |
| 570 |
|
|
| 571 |
epollfd = epoll_create1(0); |
#ifdef HAVE_SYS_EPOLL_H |
| 572 |
if (epollfd < 0) |
epollfd_server = epoll_create1(0); |
| 573 |
|
if (epollfd_server == -1) |
| 574 |
{ |
{ |
| 575 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)\n", errno); |
| 576 |
return -1; |
return -1; |
| 577 |
} |
} |
| 578 |
|
#endif |
| 579 |
|
|
| 580 |
// Server socket |
// Server socket |
| 581 |
for (i = 0; i < 2; i++) |
for (i = 0; i < 2; i++) |
| 593 |
sin.sin_port = htons(port[i]); |
sin.sin_port = htons(port[i]); |
| 594 |
|
|
| 595 |
// Reuse address and port |
// Reuse address and port |
| 596 |
flags[i] = 1; |
flags_server[i] = 1; |
| 597 |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0) |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags_server[i], sizeof(flags_server[i])) < 0) |
| 598 |
{ |
{ |
| 599 |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
| 600 |
} |
} |
| 601 |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0) |
#if defined(SO_REUSEPORT) |
| 602 |
|
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0) |
| 603 |
{ |
{ |
| 604 |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
| 605 |
} |
} |
| 606 |
|
#endif |
| 607 |
|
|
| 608 |
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 609 |
{ |
{ |
| 620 |
|
|
| 621 |
log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
| 622 |
|
|
| 623 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 624 |
ev.events = EPOLLIN; |
ev.events = EPOLLIN; |
| 625 |
ev.data.fd = socket_server[i]; |
ev.data.fd = socket_server[i]; |
| 626 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
| 627 |
{ |
{ |
| 628 |
log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno); |
log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno); |
| 629 |
if (close(epollfd) < 0) |
if (close(epollfd_server) < 0) |
| 630 |
{ |
{ |
| 631 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)\n"); |
| 632 |
} |
} |
| 633 |
return -1; |
return -1; |
| 634 |
} |
} |
| 635 |
|
#endif |
| 636 |
|
|
| 637 |
flags[i] = fcntl(socket_server[i], F_GETFL, 0); |
flags_server[i] = fcntl(socket_server[i], F_GETFL, 0); |
| 638 |
fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK); |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
| 639 |
} |
} |
| 640 |
|
|
| 641 |
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
| 652 |
} |
} |
| 653 |
|
|
| 654 |
// Startup complete |
// Startup complete |
| 655 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 656 |
sd_notifyf(0, "READY=1\n" |
sd_notifyf(0, "READY=1\n" |
| 657 |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
| 658 |
"MAINPID=%d", |
"MAINPID=%d", |
| 659 |
hostaddr, port[0], hostaddr, port[1], getpid()); |
hostaddr, port[0], hostaddr, port[1], getpid()); |
| 660 |
|
#endif |
| 661 |
|
|
| 662 |
while (!SYS_server_exit || SYS_child_process_count > 0) |
while (!SYS_server_exit || SYS_child_process_count > 0) |
| 663 |
{ |
{ |
| 664 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 665 |
if (SYS_server_exit && !sd_notify_stopping) |
if (SYS_server_exit && !sd_notify_stopping) |
| 666 |
{ |
{ |
| 667 |
sd_notify(0, "STOPPING=1"); |
sd_notify(0, "STOPPING=1"); |
| 668 |
sd_notify_stopping = 1; |
sd_notify_stopping = 1; |
| 669 |
} |
} |
| 670 |
|
#endif |
| 671 |
|
|
| 672 |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
| 673 |
{ |
{ |
| 674 |
SYS_child_exit = 0; |
SYS_child_exit = 0; |
| 675 |
|
|
| 676 |
siginfo.si_pid = 0; |
pid = waitpid(-1, &ret, WNOHANG); |
| 677 |
ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG); |
if (pid > 0) |
|
if (ret == 0 && siginfo.si_pid > 0) |
|
| 678 |
{ |
{ |
| 679 |
SYS_child_exit = 1; // Retry waitid |
SYS_child_exit = 1; // Retry waitid |
|
|
|
| 680 |
SYS_child_process_count--; |
SYS_child_process_count--; |
|
log_common("Child process (%d) exited\n", siginfo.si_pid); |
|
| 681 |
|
|
| 682 |
if (siginfo.si_pid != section_list_loader_pid) |
if (WIFEXITED(ret)) |
| 683 |
|
{ |
| 684 |
|
log_common("Child process (%d) exited, status=%d\n", pid, WEXITSTATUS(ret)); |
| 685 |
|
} |
| 686 |
|
else if (WIFSIGNALED(ret)) |
| 687 |
|
{ |
| 688 |
|
log_common("Child process (%d) is killed, status=%d\n", pid, WTERMSIG(ret)); |
| 689 |
|
} |
| 690 |
|
else |
| 691 |
|
{ |
| 692 |
|
log_common("Child process (%d) exited abnormally, status=%d\n", pid, ret); |
| 693 |
|
} |
| 694 |
|
|
| 695 |
|
if (pid != section_list_loader_pid) |
| 696 |
{ |
{ |
| 697 |
j = 0; |
j = 0; |
| 698 |
ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j); |
ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)pid, (int64_t *)&j); |
| 699 |
if (ret < 0) |
if (ret < 0) |
| 700 |
{ |
{ |
| 701 |
log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid); |
log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", pid); |
| 702 |
} |
} |
| 703 |
else |
else |
| 704 |
{ |
{ |
| 708 |
log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j); |
log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j); |
| 709 |
} |
} |
| 710 |
|
|
| 711 |
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid); |
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid); |
| 712 |
if (ret < 0) |
if (ret < 0) |
| 713 |
{ |
{ |
| 714 |
log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid); |
log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", pid); |
| 715 |
} |
} |
| 716 |
} |
} |
| 717 |
} |
} |
| 718 |
} |
} |
| 719 |
else if (ret == 0) |
else if (pid == 0) |
| 720 |
{ |
{ |
| 721 |
break; |
break; |
| 722 |
} |
} |
| 723 |
else if (ret < 0) |
else if (pid < 0) |
| 724 |
{ |
{ |
| 725 |
log_error("Error in waitid: %d\n", errno); |
log_error("Error in waitpid(): %d\n", errno); |
| 726 |
break; |
break; |
| 727 |
} |
} |
| 728 |
} |
} |
| 731 |
{ |
{ |
| 732 |
if (notify_child_exit == 0) |
if (notify_child_exit == 0) |
| 733 |
{ |
{ |
| 734 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 735 |
sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count); |
| 736 |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
| 737 |
|
#endif |
| 738 |
|
|
| 739 |
if (kill(-getpid(), SIGTERM) < 0) |
if (kill(0, SIGTERM) < 0) |
| 740 |
{ |
{ |
| 741 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
| 742 |
} |
} |
| 746 |
} |
} |
| 747 |
else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT) |
else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT) |
| 748 |
{ |
{ |
| 749 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 750 |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
| 751 |
|
#endif |
| 752 |
|
|
| 753 |
if (kill(-getpid(), SIGKILL) < 0) |
if (kill(0, SIGKILL) < 0) |
| 754 |
{ |
{ |
| 755 |
log_error("Send SIGKILL signal failed (%d)\n", errno); |
log_error("Send SIGKILL signal failed (%d)\n", errno); |
| 756 |
} |
} |
| 768 |
if (SYS_conf_reload && !SYS_server_exit) |
if (SYS_conf_reload && !SYS_server_exit) |
| 769 |
{ |
{ |
| 770 |
SYS_conf_reload = 0; |
SYS_conf_reload = 0; |
| 771 |
|
|
| 772 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 773 |
sd_notify(0, "RELOADING=1"); |
sd_notify(0, "RELOADING=1"); |
| 774 |
|
#endif |
| 775 |
|
|
| 776 |
// Restart log |
// Restart log |
| 777 |
if (log_restart() < 0) |
if (log_restart() < 0) |
| 791 |
log_error("Reload BWF conf failed\n"); |
log_error("Reload BWF conf failed\n"); |
| 792 |
} |
} |
| 793 |
|
|
| 794 |
if (load_menu(&bbs_menu_new, CONF_MENU) < 0) |
if (detach_menu_shm(&bbs_menu) < 0) |
| 795 |
{ |
{ |
| 796 |
unload_menu(&bbs_menu_new); |
log_error("detach_menu_shm(bbs_menu) error\n"); |
|
log_error("Reload bbs menu failed\n"); |
|
| 797 |
} |
} |
| 798 |
else |
if (load_menu(&bbs_menu, CONF_MENU) < 0) |
| 799 |
{ |
{ |
| 800 |
|
log_error("load_menu(bbs_menu) error\n"); |
| 801 |
unload_menu(&bbs_menu); |
unload_menu(&bbs_menu); |
|
memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new)); |
|
|
log_common("Reload bbs menu successfully\n"); |
|
| 802 |
} |
} |
| 803 |
|
|
| 804 |
if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0) |
if (detach_menu_shm(&top10_menu) < 0) |
| 805 |
{ |
{ |
| 806 |
unload_menu(&top10_menu_new); |
log_error("detach_menu_shm(top10_menu) error\n"); |
|
log_error("Reload top10 menu failed\n"); |
|
| 807 |
} |
} |
| 808 |
else |
if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0) |
| 809 |
{ |
{ |
| 810 |
|
log_error("load_menu(top10_menu) error\n"); |
| 811 |
unload_menu(&top10_menu); |
unload_menu(&top10_menu); |
|
top10_menu_new.allow_exit = 1; |
|
|
memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new)); |
|
|
log_common("Reload top10 menu successfully\n"); |
|
| 812 |
} |
} |
| 813 |
|
|
| 814 |
for (int i = 0; i < data_files_load_startup_count; i++) |
for (int i = 0; i < data_files_load_startup_count; i++) |
| 818 |
log_error("load_file(%s) error\n", data_files_load_startup[i]); |
log_error("load_file(%s) error\n", data_files_load_startup[i]); |
| 819 |
} |
} |
| 820 |
} |
} |
|
log_common("Reload data files successfully\n"); |
|
| 821 |
|
|
| 822 |
// Load section config and gen_ex |
// Load section config and gen_ex |
| 823 |
if (load_section_config_from_db(1) < 0) |
if (load_section_config_from_db(1) < 0) |
| 824 |
{ |
{ |
| 825 |
log_error("load_section_config_from_db(1) error\n"); |
log_error("load_section_config_from_db(1) error\n"); |
| 826 |
} |
} |
|
else |
|
|
{ |
|
|
log_common("Reload section config and gen_ex successfully\n"); |
|
|
} |
|
| 827 |
|
|
| 828 |
// Notify child processes to reload configuration |
// Notify child processes to reload configuration |
| 829 |
if (kill(-getpid(), SIGUSR1) < 0) |
if (kill(0, SIGUSR1) < 0) |
| 830 |
{ |
{ |
| 831 |
log_error("Send SIGUSR1 signal failed (%d)\n", errno); |
log_error("Send SIGUSR1 signal failed (%d)\n", errno); |
| 832 |
} |
} |
| 833 |
|
|
| 834 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 835 |
sd_notify(0, "READY=1"); |
sd_notify(0, "READY=1"); |
| 836 |
|
#endif |
| 837 |
} |
} |
| 838 |
|
|
| 839 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
#ifdef HAVE_SYS_EPOLL_H |
| 840 |
|
nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second |
| 841 |
if (nfds < 0) |
ret = nfds; |
| 842 |
|
#else |
| 843 |
|
pfds[0].fd = socket_server[0]; |
| 844 |
|
pfds[0].events = POLLIN; |
| 845 |
|
pfds[1].fd = socket_server[1]; |
| 846 |
|
pfds[1].events = POLLIN; |
| 847 |
|
nfds = 2; |
| 848 |
|
ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second |
| 849 |
|
#endif |
| 850 |
|
if (ret < 0) |
| 851 |
{ |
{ |
| 852 |
if (errno != EINTR) |
if (errno != EINTR) |
| 853 |
{ |
{ |
| 854 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 855 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 856 |
|
#else |
| 857 |
|
log_error("poll() error (%d)\n", errno); |
| 858 |
|
#endif |
| 859 |
break; |
break; |
| 860 |
} |
} |
| 861 |
continue; |
continue; |
| 869 |
|
|
| 870 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 871 |
{ |
{ |
| 872 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 873 |
if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1]) |
if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1]) |
| 874 |
|
#else |
| 875 |
|
if ((pfds[i].fd == socket_server[0] || pfds[i].fd == socket_server[1]) && (pfds[i].revents & POLLIN)) |
| 876 |
|
#endif |
| 877 |
{ |
{ |
| 878 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 879 |
SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0); |
SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0); |
| 880 |
|
#else |
| 881 |
|
SSH_v2 = (pfds[i].fd == socket_server[1] ? 1 : 0); |
| 882 |
|
#endif |
| 883 |
|
|
| 884 |
while (!SYS_server_exit) // Accept all incoming connections until error |
while (!SYS_server_exit) // Accept all incoming connections until error |
| 885 |
{ |
{ |
| 886 |
addrlen = sizeof(sin); |
addrlen = sizeof(sin); |
| 887 |
socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen); |
socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, (socklen_t *)&addrlen); |
| 888 |
if (socket_client < 0) |
if (socket_client < 0) |
| 889 |
{ |
{ |
| 890 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 958 |
} |
} |
| 959 |
} |
} |
| 960 |
|
|
| 961 |
if (close(epollfd) < 0) |
#ifdef HAVE_SYS_EPOLL_H |
| 962 |
|
if (close(epollfd_server) < 0) |
| 963 |
{ |
{ |
| 964 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epollfd_server) error (%d)\n"); |
| 965 |
} |
} |
| 966 |
|
#endif |
| 967 |
|
|
| 968 |
for (i = 0; i < 2; i++) |
for (i = 0; i < 2; i++) |
| 969 |
{ |
{ |
|
fcntl(socket_server[i], F_SETFL, flags[i]); |
|
|
|
|
| 970 |
if (close(socket_server[i]) == -1) |
if (close(socket_server[i]) == -1) |
| 971 |
{ |
{ |
| 972 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed\n"); |