--- lbbs/src/io.c 2025/12/17 03:56:39 1.73 +++ lbbs/src/io.c 2025/12/18 11:18:29 1.77 @@ -88,14 +88,32 @@ int io_init(void) log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); if (close(stdin_epollfd) < 0) { - log_error("close(stdin_epollfd) error (%d)\n"); + log_error("close(stdin_epollfd) error (%d)\n", errno); } stdin_epollfd = -1; return -1; } - stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); + if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + if (close(stdin_epollfd) < 0) + { + log_error("close(stdin_epollfd) error (%d)\n", errno); + } + stdin_epollfd = -1; + return -1; + } + if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + if (close(stdin_epollfd) < 0) + { + log_error("close(stdin_epollfd) error (%d)\n", errno); + } + stdin_epollfd = -1; + return -1; + } } if (stdout_epollfd == -1) @@ -120,20 +138,54 @@ int io_init(void) return -1; } - stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); - fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); + if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + if (close(stdout_epollfd) < 0) + { + log_error("close(stdout_epollfd) error (%d)\n", errno); + } + stdout_epollfd = -1; + return -1; + } + if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + if (close(stdout_epollfd) < 0) + { + log_error("close(stdout_epollfd) error (%d)\n", errno); + } + stdout_epollfd = -1; + return -1; + } } #else if (stdin_flags == 0) { - stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); + if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + return -1; + } + if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + return -1; + } } if (stdout_flags == 0) { - stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); - fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); + if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + return -1; + } + if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + return -1; + } } #endif @@ -150,7 +202,7 @@ void io_cleanup(void) if (close(stdin_epollfd) < 0) { - log_error("close(stdin_epollfd) error (%d)\n"); + log_error("close(stdin_epollfd) error (%d)\n", errno); } stdin_epollfd = -1; } @@ -193,21 +245,23 @@ int prints(const char *format, ...) if (ret > 0) { - if (stdout_buf_len + ret > OUTPUT_BUF_SIZE) + int written = (ret >= sizeof(buf) ? (int)sizeof(buf) - 1 : ret); + + if (stdout_buf_len + written > OUTPUT_BUF_SIZE) { iflush(); } - if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE) + if (stdout_buf_len + written <= OUTPUT_BUF_SIZE) { - memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); - stdout_buf_len += ret; + memcpy(stdout_buf + stdout_buf_len, buf, (size_t)written); + stdout_buf_len += written; } else { errno = EAGAIN; - ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret); - log_error("Output buffer is full, additional %d is required\n", ret); + int need = stdout_buf_len + ret - OUTPUT_BUF_SIZE; + log_error("Output buffer is full, additional %d is required\n", need); } } @@ -216,7 +270,7 @@ int prints(const char *format, ...) int outc(char c) { - int ret; + int ret = 0; if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE) { @@ -246,15 +300,11 @@ int iflush(void) #endif int nfds; - int retry; 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--; - #ifdef HAVE_SYS_EPOLL_H nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second ret = nfds; @@ -286,7 +336,22 @@ int iflush(void) for (int i = 0; i < nfds; i++) { #ifdef HAVE_SYS_EPOLL_H - if (events[i].data.fd == STDOUT_FILENO) + 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 @@ -308,9 +373,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) { -#ifdef _DEBUG - log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); -#endif + log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); retry = 0; break; } @@ -331,9 +394,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; } @@ -394,9 +455,7 @@ int igetch(int timeout) { if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) { -#ifdef _DEBUG - log_error("SSH channel is closed\n"); -#endif + log_debug("SSH channel is closed\n"); loop = 0; break; } @@ -435,7 +494,22 @@ int igetch(int timeout) for (int i = 0; i < nfds; i++) { #ifdef HAVE_SYS_EPOLL_H - if (events[i].data.fd == STDIN_FILENO) + 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 @@ -454,9 +528,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) { -#ifdef _DEBUG - log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); -#endif + log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); loop = 0; break; } @@ -493,9 +565,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; } @@ -518,7 +588,7 @@ 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 } @@ -536,7 +606,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 } @@ -1002,7 +1072,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 @@ -1042,7 +1112,7 @@ int io_buf_conv(iconv_t cd, char *p_buf, 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) + if (cd == (iconv_t)(-1) || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) { log_error("NULL pointer error\n"); return -1; @@ -1096,9 +1166,7 @@ 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=%ld, outbytes=%ld) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); if (p_buf != in_buf) { *p_buf_len -= (int)(in_buf - p_buf); @@ -1111,14 +1179,14 @@ int io_buf_conv(iconv_t cd, char *p_buf, } else if (errno == E2BIG) { - log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); + log_error("iconv(inbytes=%ld, outbytes=%ld) error: E2BIG\n", in_bytes, out_bytes); return -1; } else if (errno == EILSEQ) { if (in_bytes > out_bytes || out_bytes <= 0) { - log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); + log_error("iconv(inbytes=%ld, outbytes=%ld) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); return -2; } @@ -1126,23 +1194,17 @@ 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 + log_debug("Reset in_bytes from 0 to %ld\n", in_bytes); } -#ifdef _DEBUG - log_error("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n", + log_debug("iconv(in_bytes=%ld, out_bytes=%ld) 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", + log_debug("iconv(in_bytes=%ld, out_bytes=%ld) 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;