| 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 |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 53 |
#include <systemd/sd-daemon.h> |
#include <systemd/sd-daemon.h> |
| 54 |
#endif |
#endif |
| 89 |
|
|
| 90 |
static int socket_server[2]; |
static int socket_server[2]; |
| 91 |
static int socket_client; |
static int socket_client; |
| 92 |
|
|
| 93 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 94 |
static int epollfd_server = -1; |
static int epollfd_server = -1; |
| 95 |
|
#endif |
| 96 |
|
|
| 97 |
static ssh_bind sshbind; |
static ssh_bind sshbind; |
| 98 |
|
|
| 99 |
static HASH_DICT *hash_dict_pid_sockaddr = NULL; |
static HASH_DICT *hash_dict_pid_sockaddr = NULL; |
| 327 |
} |
} |
| 328 |
|
|
| 329 |
// Child process |
// Child process |
| 330 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 331 |
if (close(epollfd_server) < 0) |
if (close(epollfd_server) < 0) |
| 332 |
{ |
{ |
| 333 |
log_error("close(epollfd_server) error (%d)\n"); |
log_error("close(epollfd_server) error (%d)\n"); |
| 334 |
} |
} |
| 335 |
|
#endif |
| 336 |
|
|
| 337 |
for (i = 0; i < 2; i++) |
for (i = 0; i < 2; i++) |
| 338 |
{ |
{ |
| 501 |
int ret; |
int ret; |
| 502 |
int flags_server[2]; |
int flags_server[2]; |
| 503 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 504 |
|
|
| 505 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 506 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 507 |
|
#else |
| 508 |
|
struct pollfd pfds[2]; |
| 509 |
|
#endif |
| 510 |
|
|
| 511 |
int nfds; |
int nfds; |
|
siginfo_t siginfo; |
|
| 512 |
int notify_child_exit = 0; |
int notify_child_exit = 0; |
| 513 |
time_t tm_notify_child_exit = time(NULL); |
time_t tm_notify_child_exit = time(NULL); |
| 514 |
MENU_SET bbs_menu_new; |
MENU_SET bbs_menu_new; |
| 516 |
int i, j; |
int i, j; |
| 517 |
pid_t pid; |
pid_t pid; |
| 518 |
int ssh_log_level = SSH_LOG_NOLOG; |
int ssh_log_level = SSH_LOG_NOLOG; |
| 519 |
|
|
| 520 |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 521 |
int sd_notify_stopping = 0; |
int sd_notify_stopping = 0; |
| 522 |
#endif |
#endif |
| 536 |
return -1; |
return -1; |
| 537 |
} |
} |
| 538 |
|
|
| 539 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 540 |
epollfd_server = epoll_create1(0); |
epollfd_server = epoll_create1(0); |
| 541 |
if (epollfd_server == -1) |
if (epollfd_server == -1) |
| 542 |
{ |
{ |
| 543 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)\n", errno); |
| 544 |
return -1; |
return -1; |
| 545 |
} |
} |
| 546 |
|
#endif |
| 547 |
|
|
| 548 |
// Server socket |
// Server socket |
| 549 |
for (i = 0; i < 2; i++) |
for (i = 0; i < 2; i++) |
| 566 |
{ |
{ |
| 567 |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
| 568 |
} |
} |
| 569 |
|
#if defined(SO_REUSEPORT) |
| 570 |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0) |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0) |
| 571 |
{ |
{ |
| 572 |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
| 573 |
} |
} |
| 574 |
|
#endif |
| 575 |
|
|
| 576 |
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 577 |
{ |
{ |
| 588 |
|
|
| 589 |
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)); |
| 590 |
|
|
| 591 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 592 |
ev.events = EPOLLIN; |
ev.events = EPOLLIN; |
| 593 |
ev.data.fd = socket_server[i]; |
ev.data.fd = socket_server[i]; |
| 594 |
if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
| 600 |
} |
} |
| 601 |
return -1; |
return -1; |
| 602 |
} |
} |
| 603 |
|
#endif |
| 604 |
|
|
| 605 |
flags_server[i] = fcntl(socket_server[i], F_GETFL, 0); |
flags_server[i] = fcntl(socket_server[i], F_GETFL, 0); |
| 606 |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
| 641 |
{ |
{ |
| 642 |
SYS_child_exit = 0; |
SYS_child_exit = 0; |
| 643 |
|
|
| 644 |
siginfo.si_pid = 0; |
pid = waitpid(-1, &ret, WNOHANG); |
| 645 |
ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG); |
if (pid > 0) |
|
if (ret == 0 && siginfo.si_pid > 0) |
|
| 646 |
{ |
{ |
| 647 |
SYS_child_exit = 1; // Retry waitid |
SYS_child_exit = 1; // Retry waitid |
|
|
|
| 648 |
SYS_child_process_count--; |
SYS_child_process_count--; |
|
log_common("Child process (%d) exited\n", siginfo.si_pid); |
|
| 649 |
|
|
| 650 |
if (siginfo.si_pid != section_list_loader_pid) |
if (WIFEXITED(ret)) |
| 651 |
|
{ |
| 652 |
|
log_common("Child process (%d) exited, status=%d\n", pid, WEXITSTATUS(ret)); |
| 653 |
|
} |
| 654 |
|
else if (WIFSIGNALED(ret)) |
| 655 |
|
{ |
| 656 |
|
log_common("Child process (%d) is killed, status=%d\n", pid, WTERMSIG(ret)); |
| 657 |
|
} |
| 658 |
|
else |
| 659 |
|
{ |
| 660 |
|
log_common("Child process (%d) exited abnormally, status=%d\n", pid, ret); |
| 661 |
|
} |
| 662 |
|
|
| 663 |
|
if (pid != section_list_loader_pid) |
| 664 |
{ |
{ |
| 665 |
j = 0; |
j = 0; |
| 666 |
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); |
| 667 |
if (ret < 0) |
if (ret < 0) |
| 668 |
{ |
{ |
| 669 |
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); |
| 670 |
} |
} |
| 671 |
else |
else |
| 672 |
{ |
{ |
| 676 |
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); |
| 677 |
} |
} |
| 678 |
|
|
| 679 |
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid); |
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid); |
| 680 |
if (ret < 0) |
if (ret < 0) |
| 681 |
{ |
{ |
| 682 |
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); |
| 683 |
} |
} |
| 684 |
} |
} |
| 685 |
} |
} |
| 686 |
} |
} |
| 687 |
else if (ret == 0) |
else if (pid == 0) |
| 688 |
{ |
{ |
| 689 |
break; |
break; |
| 690 |
} |
} |
| 691 |
else if (ret < 0) |
else if (pid < 0) |
| 692 |
{ |
{ |
| 693 |
log_error("Error in waitid: %d\n", errno); |
log_error("Error in waitpid(): %d\n", errno); |
| 694 |
break; |
break; |
| 695 |
} |
} |
| 696 |
} |
} |
| 814 |
#endif |
#endif |
| 815 |
} |
} |
| 816 |
|
|
| 817 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 818 |
nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second |
| 819 |
|
ret = nfds; |
| 820 |
if (nfds < 0) |
#else |
| 821 |
|
pfds[0].fd = socket_server[0]; |
| 822 |
|
pfds[0].events = POLLIN; |
| 823 |
|
pfds[1].fd = socket_server[1]; |
| 824 |
|
pfds[1].events = POLLIN; |
| 825 |
|
nfds = 2; |
| 826 |
|
ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second |
| 827 |
|
#endif |
| 828 |
|
if (ret < 0) |
| 829 |
{ |
{ |
| 830 |
if (errno != EINTR) |
if (errno != EINTR) |
| 831 |
{ |
{ |
| 832 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 833 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 834 |
|
#else |
| 835 |
|
log_error("poll() error (%d)\n", errno); |
| 836 |
|
#endif |
| 837 |
break; |
break; |
| 838 |
} |
} |
| 839 |
continue; |
continue; |
| 847 |
|
|
| 848 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 849 |
{ |
{ |
| 850 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 851 |
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]) |
| 852 |
|
#else |
| 853 |
|
if ((pfds[i].fd == socket_server[0] || pfds[i].fd == socket_server[1]) && (pfds[i].revents & POLLIN)) |
| 854 |
|
#endif |
| 855 |
{ |
{ |
| 856 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 857 |
SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0); |
SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0); |
| 858 |
|
#else |
| 859 |
|
SSH_v2 = (pfds[i].fd == socket_server[1] ? 1 : 0); |
| 860 |
|
#endif |
| 861 |
|
|
| 862 |
while (!SYS_server_exit) // Accept all incoming connections until error |
while (!SYS_server_exit) // Accept all incoming connections until error |
| 863 |
{ |
{ |
| 864 |
addrlen = sizeof(sin); |
addrlen = sizeof(sin); |
| 865 |
socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen); |
socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, (socklen_t *)&addrlen); |
| 866 |
if (socket_client < 0) |
if (socket_client < 0) |
| 867 |
{ |
{ |
| 868 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 936 |
} |
} |
| 937 |
} |
} |
| 938 |
|
|
| 939 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 940 |
if (close(epollfd_server) < 0) |
if (close(epollfd_server) < 0) |
| 941 |
{ |
{ |
| 942 |
log_error("close(epollfd_server) error (%d)\n"); |
log_error("close(epollfd_server) error (%d)\n"); |
| 943 |
} |
} |
| 944 |
|
#endif |
| 945 |
|
|
| 946 |
for (i = 0; i < 2; i++) |
for (i = 0; i < 2; i++) |
| 947 |
{ |
{ |