--- lbbs/src/io.c 2025/11/11 00:28:05 1.65 +++ lbbs/src/io.c 2025/12/18 03:23:48 1.76 @@ -20,37 +20,170 @@ #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; static int stdout_conv_offset = 0; -static iconv_t stdin_cd = NULL; -static iconv_t stdout_cd = NULL; +static iconv_t stdin_cd = (iconv_t)(-1); +static iconv_t stdout_cd = (iconv_t)(-1); + +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,68 @@ int outc(char c) int iflush(void) { - int flags; - struct epoll_event ev, events[MAX_EVENTS]; - int nfds, epollfd; - 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; - } +#ifdef HAVE_SYS_EPOLL_H + struct epoll_event events[MAX_EVENTS]; +#else + struct pollfd pfds[1]; +#endif - // Set STDOUT as non-blocking - flags = fcntl(STDOUT_FILENO, F_GETFL, 0); - fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); + int nfds; + int ret = 0; // Retry wait / flush for at most 3 times - retry = 3; - while (retry > 0 && !SYS_server_exit) + for (int retry = 3; retry > 0 && !SYS_server_exit; retry--) { - 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++) { - if (events[i].data.fd == STDOUT_FILENO) +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR))) +#else + if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR))) +#endif + { +#ifdef HAVE_SYS_EPOLL_H + log_debug("STDOUT error events (%d)\n", events[i].events); +#else + log_debug("STDOUT error events (%d)\n", pfds[i].revents); +#endif + retry = 0; + break; + } + +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) +#else + if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) +#endif { if (stdout_buf_offset < stdout_buf_len) { @@ -178,7 +319,7 @@ int iflush(void) ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset)); if (ret == SSH_ERROR) { - log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); + log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); retry = 0; break; } @@ -199,9 +340,7 @@ int iflush(void) } else { -#ifdef _DEBUG - log_error("write(STDOUT) error (%d)\n", errno); -#endif + log_debug("write(STDOUT) error (%d)\n", errno); retry = 0; break; } @@ -229,14 +368,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 +375,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,59 +391,47 @@ 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)) { - log_error("SSH channel is closed\n"); + log_debug("SSH channel is closed\n"); loop = 0; break; } 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 +439,26 @@ int igetch(int timeout) for (int i = 0; i < nfds; i++) { - if (events[i].data.fd == STDIN_FILENO) +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR))) +#else + if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR))) +#endif + { +#ifdef HAVE_SYS_EPOLL_H + log_debug("STDIN error events (%d)\n", events[i].events); +#else + log_debug("STDIN error events (%d)\n", pfds[i].revents); +#endif + loop = 0; + break; + } + +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) +#else + if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) +#endif { stdin_read_wait = 1; } @@ -331,7 +474,7 @@ int igetch(int timeout) ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); if (ret == SSH_ERROR) { - log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); + log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); loop = 0; break; } @@ -368,9 +511,7 @@ int igetch(int timeout) } else { -#ifdef _DEBUG - log_error("read(STDIN) error (%d)\n", errno); -#endif + log_debug("read(STDIN) error (%d)\n", errno); loop = 0; break; } @@ -393,18 +534,11 @@ int igetch(int timeout) #ifdef _DEBUG for (int j = stdin_buf_offset; j < stdin_buf_len; j++) { - log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256); + log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256); } #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); @@ -418,7 +552,7 @@ int igetch(int timeout) #ifdef _DEBUG for (int j = stdin_conv_offset; j < stdin_conv_len; j++) { - log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); + log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); } #endif } @@ -884,7 +1018,7 @@ int igetch(int timeout) #ifdef _DEBUG if (out != KEY_TIMEOUT && out != KEY_NULL) { - log_error("Debug: -->[0x %x]\n", out); + log_debug("output: -->[0x %x]\n", out); } #endif @@ -922,6 +1056,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 +1080,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 +1097,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) @@ -975,12 +1112,10 @@ int io_buf_conv(iconv_t cd, char *p_buf, { if (errno == EINVAL) // Incomplete { -#ifdef _DEBUG - log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); -#endif + log_debug("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); 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 +1140,35 @@ int io_buf_conv(iconv_t cd, char *p_buf, if (in_bytes == 0) { in_bytes = (size_t)(*p_buf_len - *p_buf_offset); + log_debug("Reset in_bytes from 0 to %d\n", in_bytes); } - *out_buf = *in_buf; - in_buf++; - out_buf++; - in_bytes--; - out_bytes--; - - continue; + log_debug("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n", + in_bytes, out_bytes, in_buf[0]); + skip_current = 1; + } + else // something strange + { + log_debug("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n", + in_bytes, out_bytes, errno, in_buf[0]); + *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; } @@ -1060,6 +1203,7 @@ int io_conv_init(const char *charset) { log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno); iconv_close(stdin_cd); + stdin_cd = (iconv_t)(-1); return -2; } @@ -1068,15 +1212,15 @@ int io_conv_init(const char *charset) int io_conv_cleanup(void) { - if (stdin_cd != NULL) + if (stdin_cd != (iconv_t)(-1)) { iconv_close(stdin_cd); - stdin_cd = NULL; + stdin_cd = (iconv_t)(-1); } - if (stdout_cd != NULL) + if (stdout_cd != (iconv_t)(-1)) { iconv_close(stdout_cd); - stdout_cd = NULL; + stdout_cd = (iconv_t)(-1); } return 0;