| 32 |
#include <poll.h> |
#include <poll.h> |
| 33 |
#endif |
#endif |
| 34 |
|
|
| 35 |
|
enum _io_constant_t |
| 36 |
|
{ |
| 37 |
|
OUTPUT_BUF_SIZE = 8192, |
| 38 |
|
}; |
| 39 |
|
|
| 40 |
const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 41 |
char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 42 |
|
|
| 51 |
|
|
| 52 |
// static input / output buffer |
// static input / output buffer |
| 53 |
static char stdin_buf[LINE_BUFFER_LEN]; |
static char stdin_buf[LINE_BUFFER_LEN]; |
| 54 |
static char stdout_buf[BUFSIZ]; |
static char stdout_buf[OUTPUT_BUF_SIZE]; |
| 55 |
static int stdin_buf_len = 0; |
static int stdin_buf_len = 0; |
| 56 |
static int stdout_buf_len = 0; |
static int stdout_buf_len = 0; |
| 57 |
static int stdin_buf_offset = 0; |
static int stdin_buf_offset = 0; |
| 58 |
static int stdout_buf_offset = 0; |
static int stdout_buf_offset = 0; |
| 59 |
|
|
| 60 |
static char stdin_conv[LINE_BUFFER_LEN * 2]; |
static char stdin_conv[LINE_BUFFER_LEN * 2]; |
| 61 |
static char stdout_conv[BUFSIZ * 2]; |
static char stdout_conv[OUTPUT_BUF_SIZE * 2]; |
| 62 |
static int stdin_conv_len = 0; |
static int stdin_conv_len = 0; |
| 63 |
static int stdout_conv_len = 0; |
static int stdout_conv_len = 0; |
| 64 |
static int stdin_conv_offset = 0; |
static int stdin_conv_offset = 0; |
| 65 |
static int stdout_conv_offset = 0; |
static int stdout_conv_offset = 0; |
| 66 |
|
|
| 67 |
static iconv_t stdin_cd = NULL; |
static iconv_t stdin_cd = (iconv_t)(-1); |
| 68 |
static iconv_t stdout_cd = NULL; |
static iconv_t stdout_cd = (iconv_t)(-1); |
| 69 |
|
|
| 70 |
int io_init(void) |
int io_init(void) |
| 71 |
{ |
{ |
| 183 |
|
|
| 184 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 185 |
{ |
{ |
| 186 |
char buf[BUFSIZ]; |
char buf[OUTPUT_BUF_SIZE]; |
| 187 |
va_list args; |
va_list args; |
| 188 |
int ret; |
int ret; |
| 189 |
|
|
| 193 |
|
|
| 194 |
if (ret > 0) |
if (ret > 0) |
| 195 |
{ |
{ |
| 196 |
if (stdout_buf_len + ret > BUFSIZ) |
if (stdout_buf_len + ret > OUTPUT_BUF_SIZE) |
| 197 |
{ |
{ |
| 198 |
iflush(); |
iflush(); |
| 199 |
} |
} |
| 200 |
|
|
| 201 |
if (stdout_buf_len + ret <= BUFSIZ) |
if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE) |
| 202 |
{ |
{ |
| 203 |
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); |
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); |
| 204 |
stdout_buf_len += ret; |
stdout_buf_len += ret; |
| 206 |
else |
else |
| 207 |
{ |
{ |
| 208 |
errno = EAGAIN; |
errno = EAGAIN; |
| 209 |
ret = (BUFSIZ - stdout_buf_len - ret); |
ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret); |
| 210 |
log_error("Output buffer is full, additional %d is required\n", ret); |
log_error("Output buffer is full, additional %d is required\n", ret); |
| 211 |
} |
} |
| 212 |
} |
} |
| 218 |
{ |
{ |
| 219 |
int ret; |
int ret; |
| 220 |
|
|
| 221 |
if (stdout_buf_len + 1 > BUFSIZ) |
if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE) |
| 222 |
{ |
{ |
| 223 |
iflush(); |
iflush(); |
| 224 |
} |
} |
| 225 |
|
|
| 226 |
if (stdout_buf_len + 1 <= BUFSIZ) |
if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE) |
| 227 |
{ |
{ |
| 228 |
stdout_buf[stdout_buf_len] = c; |
stdout_buf[stdout_buf_len] = c; |
| 229 |
stdout_buf_len++; |
stdout_buf_len++; |
| 246 |
#endif |
#endif |
| 247 |
|
|
| 248 |
int nfds; |
int nfds; |
|
int retry; |
|
| 249 |
int ret = 0; |
int ret = 0; |
| 250 |
|
|
| 251 |
// Retry wait / flush for at most 3 times |
// Retry wait / flush for at most 3 times |
| 252 |
retry = 3; |
for (int retry = 3; retry > 0 && !SYS_server_exit; retry--) |
|
while (retry > 0 && !SYS_server_exit) |
|
| 253 |
{ |
{ |
|
retry--; |
|
|
|
|
| 254 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 255 |
nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 256 |
ret = nfds; |
ret = nfds; |
| 282 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 283 |
{ |
{ |
| 284 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 285 |
if (events[i].data.fd == STDOUT_FILENO) |
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR))) |
| 286 |
|
#else |
| 287 |
|
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR))) |
| 288 |
|
#endif |
| 289 |
|
{ |
| 290 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 291 |
|
log_debug("STDOUT error events (%d)\n", events[i].events); |
| 292 |
|
#else |
| 293 |
|
log_debug("STDOUT error events (%d)\n", pfds[i].revents); |
| 294 |
|
#endif |
| 295 |
|
retry = 0; |
| 296 |
|
break; |
| 297 |
|
} |
| 298 |
|
|
| 299 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 300 |
|
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) |
| 301 |
#else |
#else |
| 302 |
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 303 |
#endif |
#endif |
| 319 |
ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset)); |
ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset)); |
| 320 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 321 |
{ |
{ |
| 322 |
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)); |
| 323 |
retry = 0; |
retry = 0; |
| 324 |
break; |
break; |
| 325 |
} |
} |
| 340 |
} |
} |
| 341 |
else |
else |
| 342 |
{ |
{ |
| 343 |
#ifdef _DEBUG |
log_debug("write(STDOUT) error (%d)\n", errno); |
|
log_error("write(STDOUT) error (%d)\n", errno); |
|
|
#endif |
|
| 344 |
retry = 0; |
retry = 0; |
| 345 |
break; |
break; |
| 346 |
} |
} |
| 401 |
{ |
{ |
| 402 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 403 |
{ |
{ |
| 404 |
log_error("SSH channel is closed\n"); |
log_debug("SSH channel is closed\n"); |
| 405 |
loop = 0; |
loop = 0; |
| 406 |
break; |
break; |
| 407 |
} |
} |
| 440 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 441 |
{ |
{ |
| 442 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 443 |
if (events[i].data.fd == STDIN_FILENO) |
if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR))) |
| 444 |
|
#else |
| 445 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR))) |
| 446 |
|
#endif |
| 447 |
|
{ |
| 448 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 449 |
|
log_debug("STDIN error events (%d)\n", events[i].events); |
| 450 |
|
#else |
| 451 |
|
log_debug("STDIN error events (%d)\n", pfds[i].revents); |
| 452 |
|
#endif |
| 453 |
|
loop = 0; |
| 454 |
|
break; |
| 455 |
|
} |
| 456 |
|
|
| 457 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 458 |
|
if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) |
| 459 |
#else |
#else |
| 460 |
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 461 |
#endif |
#endif |
| 474 |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
| 475 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 476 |
{ |
{ |
| 477 |
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)); |
| 478 |
loop = 0; |
loop = 0; |
| 479 |
break; |
break; |
| 480 |
} |
} |
| 511 |
} |
} |
| 512 |
else |
else |
| 513 |
{ |
{ |
| 514 |
#ifdef _DEBUG |
log_debug("read(STDIN) error (%d)\n", errno); |
|
log_error("read(STDIN) error (%d)\n", errno); |
|
|
#endif |
|
| 515 |
loop = 0; |
loop = 0; |
| 516 |
break; |
break; |
| 517 |
} |
} |
| 534 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 535 |
for (int j = stdin_buf_offset; j < stdin_buf_len; j++) |
for (int j = stdin_buf_offset; j < stdin_buf_len; j++) |
| 536 |
{ |
{ |
| 537 |
log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256); |
log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256); |
| 538 |
} |
} |
| 539 |
#endif |
#endif |
| 540 |
} |
} |
| 552 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 553 |
for (int j = stdin_conv_offset; j < stdin_conv_len; j++) |
for (int j = stdin_conv_offset; j < stdin_conv_len; j++) |
| 554 |
{ |
{ |
| 555 |
log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); |
log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); |
| 556 |
} |
} |
| 557 |
#endif |
#endif |
| 558 |
} |
} |
| 1018 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 1019 |
if (out != KEY_TIMEOUT && out != KEY_NULL) |
if (out != KEY_TIMEOUT && out != KEY_NULL) |
| 1020 |
{ |
{ |
| 1021 |
log_error("Debug: -->[0x %x]\n", out); |
log_debug("output: -->[0x %x]\n", out); |
| 1022 |
} |
} |
| 1023 |
#endif |
#endif |
| 1024 |
|
|
| 1056 |
int ret; |
int ret; |
| 1057 |
int in_control = 0; |
int in_control = 0; |
| 1058 |
size_t i = 0; |
size_t i = 0; |
| 1059 |
|
int skip_current = 0; |
| 1060 |
|
|
| 1061 |
if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) |
if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) |
| 1062 |
{ |
{ |
| 1080 |
} |
} |
| 1081 |
} |
} |
| 1082 |
|
|
| 1083 |
if (in_control) |
if (in_control || skip_current) |
| 1084 |
{ |
{ |
| 1085 |
|
skip_current = 0; |
| 1086 |
|
|
| 1087 |
if (out_bytes <= 0) |
if (out_bytes <= 0) |
| 1088 |
{ |
{ |
| 1089 |
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); |
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); |
| 1097 |
out_bytes--; |
out_bytes--; |
| 1098 |
|
|
| 1099 |
(*p_buf_offset)++; |
(*p_buf_offset)++; |
| 1100 |
*p_conv_len = (int)(conv_size - out_bytes); |
(*p_conv_len)++; |
| 1101 |
|
|
| 1102 |
i++; |
i++; |
| 1103 |
if (i >= 2) |
if (i >= 2) |
| 1112 |
{ |
{ |
| 1113 |
if (errno == EINVAL) // Incomplete |
if (errno == EINVAL) // Incomplete |
| 1114 |
{ |
{ |
| 1115 |
#ifdef _DEBUG |
log_debug("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); |
|
|
#endif |
|
| 1116 |
if (p_buf != in_buf) |
if (p_buf != in_buf) |
| 1117 |
{ |
{ |
| 1118 |
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
*p_buf_len -= (int)(in_buf - p_buf); |
| 1119 |
*p_buf_offset = 0; |
*p_buf_offset = 0; |
| 1120 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
| 1121 |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
| 1140 |
if (in_bytes == 0) |
if (in_bytes == 0) |
| 1141 |
{ |
{ |
| 1142 |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 1143 |
|
log_debug("Reset in_bytes from 0 to %d\n", in_bytes); |
| 1144 |
} |
} |
| 1145 |
|
|
| 1146 |
*out_buf = *in_buf; |
log_debug("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n", |
| 1147 |
in_buf++; |
in_bytes, out_bytes, in_buf[0]); |
| 1148 |
out_buf++; |
skip_current = 1; |
| 1149 |
in_bytes--; |
} |
| 1150 |
out_bytes--; |
else // something strange |
| 1151 |
|
{ |
| 1152 |
continue; |
log_debug("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n", |
| 1153 |
|
in_bytes, out_bytes, errno, in_buf[0]); |
| 1154 |
|
*p_buf_offset = (int)(in_buf - p_buf); |
| 1155 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 1156 |
|
skip_current = 1; |
| 1157 |
} |
} |
| 1158 |
} |
} |
| 1159 |
else |
else |
| 1160 |
{ |
{ |
| 1161 |
*p_buf_len = 0; |
*p_buf_offset = (int)(in_buf - p_buf); |
|
*p_buf_offset = 0; |
|
| 1162 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
|
|
|
|
break; |
|
| 1163 |
} |
} |
| 1164 |
} |
} |
| 1165 |
|
|
| 1166 |
|
if (*p_buf_offset >= *p_buf_len) |
| 1167 |
|
{ |
| 1168 |
|
*p_buf_len = 0; |
| 1169 |
|
*p_buf_offset = 0; |
| 1170 |
|
} |
| 1171 |
|
|
| 1172 |
return 0; |
return 0; |
| 1173 |
} |
} |
| 1174 |
|
|
| 1203 |
{ |
{ |
| 1204 |
log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno); |
log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno); |
| 1205 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1206 |
|
stdin_cd = (iconv_t)(-1); |
| 1207 |
return -2; |
return -2; |
| 1208 |
} |
} |
| 1209 |
|
|
| 1212 |
|
|
| 1213 |
int io_conv_cleanup(void) |
int io_conv_cleanup(void) |
| 1214 |
{ |
{ |
| 1215 |
if (stdin_cd != NULL) |
if (stdin_cd != (iconv_t)(-1)) |
| 1216 |
{ |
{ |
| 1217 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1218 |
stdin_cd = NULL; |
stdin_cd = (iconv_t)(-1); |
| 1219 |
} |
} |
| 1220 |
if (stdout_cd != NULL) |
if (stdout_cd != (iconv_t)(-1)) |
| 1221 |
{ |
{ |
| 1222 |
iconv_close(stdout_cd); |
iconv_close(stdout_cd); |
| 1223 |
stdout_cd = NULL; |
stdout_cd = (iconv_t)(-1); |
| 1224 |
} |
} |
| 1225 |
|
|
| 1226 |
return 0; |
return 0; |