--- lbbs/src/io.c 2025/11/11 00:28:05 1.65 +++ lbbs/src/io.c 2025/11/23 06:43:51 1.71 @@ -20,26 +20,45 @@ #include #include #include -#include #include #include #include #include #include +#ifdef HAVE_SYS_EPOLL_H +#include +#else +#include +#endif + +enum _io_constant_t +{ + OUTPUT_BUF_SIZE = 8192, +}; + const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; +#ifdef HAVE_SYS_EPOLL_H +// epoll for STDIO +static int stdin_epollfd = -1; +static int stdout_epollfd = -1; +#endif + +static int stdin_flags = 0; +static int stdout_flags = 0; + // static input / output buffer static char stdin_buf[LINE_BUFFER_LEN]; -static char stdout_buf[BUFSIZ]; +static char stdout_buf[OUTPUT_BUF_SIZE]; static int stdin_buf_len = 0; static int stdout_buf_len = 0; static int stdin_buf_offset = 0; static int stdout_buf_offset = 0; static char stdin_conv[LINE_BUFFER_LEN * 2]; -static char stdout_conv[BUFSIZ * 2]; +static char stdout_conv[OUTPUT_BUF_SIZE * 2]; static int stdin_conv_len = 0; static int stdout_conv_len = 0; static int stdin_conv_offset = 0; @@ -48,9 +67,123 @@ static int stdout_conv_offset = 0; static iconv_t stdin_cd = NULL; static iconv_t stdout_cd = NULL; +int io_init(void) +{ +#ifdef HAVE_SYS_EPOLL_H + struct epoll_event ev; + + if (stdin_epollfd == -1) + { + stdin_epollfd = epoll_create1(0); + if (stdin_epollfd == -1) + { + log_error("epoll_create1() error (%d)\n", errno); + return -1; + } + + ev.events = EPOLLIN; + ev.data.fd = STDIN_FILENO; + if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) + { + log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); + if (close(stdin_epollfd) < 0) + { + log_error("close(stdin_epollfd) error (%d)\n"); + } + stdin_epollfd = -1; + return -1; + } + + stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); + } + + if (stdout_epollfd == -1) + { + stdout_epollfd = epoll_create1(0); + if (stdout_epollfd == -1) + { + log_error("epoll_create1() error (%d)\n", errno); + return -1; + } + + ev.events = EPOLLOUT; + ev.data.fd = STDOUT_FILENO; + if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) + { + log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); + if (close(stdout_epollfd) < 0) + { + log_error("close(stdout_epollfd) error (%d)\n"); + } + stdout_epollfd = -1; + return -1; + } + + stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); + fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); + } +#else + if (stdin_flags == 0) + { + stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); + } + + if (stdout_flags == 0) + { + stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); + fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); + } +#endif + + return 0; +} + +void io_cleanup(void) +{ +#ifdef HAVE_SYS_EPOLL_H + if (stdin_epollfd != -1) + { + fcntl(STDIN_FILENO, F_SETFL, stdin_flags); + stdin_flags = 0; + + if (close(stdin_epollfd) < 0) + { + log_error("close(stdin_epollfd) error (%d)\n"); + } + stdin_epollfd = -1; + } + + if (stdout_epollfd != -1) + { + fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); + stdout_flags = 0; + + if (close(stdout_epollfd) < 0) + { + log_error("close(stdout_epollfd) error (%d)\n"); + } + stdout_epollfd = -1; + } +#else + if (stdin_flags != 0) + { + fcntl(STDIN_FILENO, F_SETFL, stdin_flags); + stdin_flags = 0; + } + + if (stdout_flags != 0) + { + fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); + stdout_flags = 0; + } +#endif +} + int prints(const char *format, ...) { - char buf[BUFSIZ]; + char buf[OUTPUT_BUF_SIZE]; va_list args; int ret; @@ -60,12 +193,12 @@ int prints(const char *format, ...) if (ret > 0) { - if (stdout_buf_len + ret > BUFSIZ) + if (stdout_buf_len + ret > OUTPUT_BUF_SIZE) { iflush(); } - if (stdout_buf_len + ret <= BUFSIZ) + if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE) { memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); stdout_buf_len += ret; @@ -73,7 +206,7 @@ int prints(const char *format, ...) else { errno = EAGAIN; - ret = (BUFSIZ - stdout_buf_len - ret); + ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret); log_error("Output buffer is full, additional %d is required\n", ret); } } @@ -85,12 +218,12 @@ int outc(char c) { int ret; - if (stdout_buf_len + 1 > BUFSIZ) + if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE) { iflush(); } - if (stdout_buf_len + 1 <= BUFSIZ) + if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE) { stdout_buf[stdout_buf_len] = c; stdout_buf_len++; @@ -106,60 +239,57 @@ int outc(char c) int iflush(void) { - int flags; - struct epoll_event ev, events[MAX_EVENTS]; - int nfds, epollfd; +#ifdef HAVE_SYS_EPOLL_H + struct epoll_event events[MAX_EVENTS]; +#else + struct pollfd pfds[1]; +#endif + + int nfds; int retry; int ret = 0; - epollfd = epoll_create1(0); - if (epollfd < 0) - { - log_error("epoll_create1() error (%d)\n", errno); - return -1; - } - - ev.events = EPOLLOUT; - ev.data.fd = STDOUT_FILENO; - if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) - { - log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); - if (close(epollfd) < 0) - { - log_error("close(epoll) error (%d)\n"); - } - return -1; - } - - // Set STDOUT as non-blocking - flags = fcntl(STDOUT_FILENO, F_GETFL, 0); - fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); - // Retry wait / flush for at most 3 times retry = 3; while (retry > 0 && !SYS_server_exit) { retry--; - nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second +#ifdef HAVE_SYS_EPOLL_H + nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second + ret = nfds; +#else + pfds[0].fd = STDOUT_FILENO; + pfds[0].events = POLLOUT; + nfds = 1; + ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second +#endif - if (nfds < 0) + if (ret < 0) { if (errno != EINTR) { +#ifdef HAVE_SYS_EPOLL_H log_error("epoll_wait() error (%d)\n", errno); +#else + log_error("poll() error (%d)\n", errno); +#endif break; } continue; } - else if (nfds == 0) // timeout + else if (ret == 0) // timeout { continue; } for (int i = 0; i < nfds; i++) { +#ifdef HAVE_SYS_EPOLL_H if (events[i].data.fd == STDOUT_FILENO) +#else + if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) +#endif { if (stdout_buf_offset < stdout_buf_len) { @@ -229,14 +359,6 @@ int iflush(void) } } - // Restore STDOUT flags - fcntl(STDOUT_FILENO, F_SETFL, flags); - - if (close(epollfd) < 0) - { - log_error("close(epoll) error (%d)\n"); - } - return ret; } @@ -244,8 +366,13 @@ int igetch(int timeout) { static int stdin_read_wait = 0; - struct epoll_event ev, events[MAX_EVENTS]; - int nfds, epollfd; +#ifdef HAVE_SYS_EPOLL_H + struct epoll_event events[MAX_EVENTS]; +#else + struct pollfd pfds[1]; +#endif + + int nfds; int ret; int loop; @@ -255,36 +382,12 @@ int igetch(int timeout) int in_ascii = 0; int in_control = 0; int i = 0; - int flags; if (stdin_conv_offset >= stdin_conv_len) { stdin_conv_len = 0; stdin_conv_offset = 0; - epollfd = epoll_create1(0); - if (epollfd < 0) - { - log_error("epoll_create1() error (%d)\n", errno); - return -1; - } - - ev.events = EPOLLIN; - ev.data.fd = STDIN_FILENO; - if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) - { - log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); - - if (close(epollfd) < 0) - { - log_error("close(epoll) error (%d)\n"); - } - return -1; - } - - flags = fcntl(STDIN_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); - for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) { if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) @@ -296,18 +399,30 @@ int igetch(int timeout) if (!stdin_read_wait) { - nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); +#ifdef HAVE_SYS_EPOLL_H + nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout); + ret = nfds; +#else + pfds[0].fd = STDIN_FILENO; + pfds[0].events = POLLIN; + nfds = 1; + ret = poll(pfds, (nfds_t)nfds, timeout); +#endif - if (nfds < 0) + if (ret < 0) { if (errno != EINTR) { +#ifdef HAVE_SYS_EPOLL_H log_error("epoll_wait() error (%d)\n", errno); +#else + log_error("poll() error (%d)\n", errno); +#endif break; } continue; } - else if (nfds == 0) // timeout + else if (ret == 0) // timeout { out = KEY_TIMEOUT; break; @@ -315,7 +430,11 @@ int igetch(int timeout) for (int i = 0; i < nfds; i++) { +#ifdef HAVE_SYS_EPOLL_H if (events[i].data.fd == STDIN_FILENO) +#else + if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) +#endif { stdin_read_wait = 1; } @@ -398,13 +517,6 @@ int igetch(int timeout) #endif } - fcntl(STDIN_FILENO, F_SETFL, flags); - - if (close(epollfd) < 0) - { - log_error("close(epoll) error (%d)\n"); - } - if (stdin_buf_offset < stdin_buf_len) { ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len); @@ -922,6 +1034,7 @@ int io_buf_conv(iconv_t cd, char *p_buf, int ret; int in_control = 0; size_t i = 0; + int skip_current = 0; if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) { @@ -945,8 +1058,10 @@ int io_buf_conv(iconv_t cd, char *p_buf, } } - if (in_control) + if (in_control || skip_current) { + skip_current = 0; + if (out_bytes <= 0) { log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); @@ -960,7 +1075,7 @@ int io_buf_conv(iconv_t cd, char *p_buf, out_bytes--; (*p_buf_offset)++; - *p_conv_len = (int)(conv_size - out_bytes); + (*p_conv_len)++; i++; if (i >= 2) @@ -980,7 +1095,7 @@ int io_buf_conv(iconv_t cd, char *p_buf, #endif if (p_buf != in_buf) { - *p_buf_len = (int)(p_buf + *p_buf_len - in_buf); + *p_buf_len -= (int)(in_buf - p_buf); *p_buf_offset = 0; *p_conv_len = (int)(conv_size - out_bytes); memmove(p_buf, in_buf, (size_t)(*p_buf_len)); @@ -1005,27 +1120,41 @@ int io_buf_conv(iconv_t cd, char *p_buf, if (in_bytes == 0) { in_bytes = (size_t)(*p_buf_len - *p_buf_offset); +#ifdef _DEBUG + log_error("Reset in_bytes from 0 to %d\n", in_bytes); +#endif } - *out_buf = *in_buf; - in_buf++; - out_buf++; - in_bytes--; - out_bytes--; - - continue; +#ifdef _DEBUG + log_error("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n", + in_bytes, out_bytes, in_buf[0]); +#endif + skip_current = 1; + } + else // something strange + { +#ifdef _DEBUG + log_error("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n", + in_bytes, out_bytes, errno, in_buf[0]); +#endif + *p_buf_offset = (int)(in_buf - p_buf); + *p_conv_len = (int)(conv_size - out_bytes); + skip_current = 1; } } else { - *p_buf_len = 0; - *p_buf_offset = 0; + *p_buf_offset = (int)(in_buf - p_buf); *p_conv_len = (int)(conv_size - out_bytes); - - break; } } + if (*p_buf_offset >= *p_buf_len) + { + *p_buf_len = 0; + *p_buf_offset = 0; + } + return 0; }