| 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 |
{ |
{ |
| 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 _DEBUG |
| 291 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 292 |
|
log_error("STDOUT error events (%d)\n", events[i].events); |
| 293 |
|
#else |
| 294 |
|
log_error("STDOUT error events (%d)\n", pfds[i].revents); |
| 295 |
|
#endif |
| 296 |
|
#endif |
| 297 |
|
retry = 0; |
| 298 |
|
break; |
| 299 |
|
} |
| 300 |
|
|
| 301 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 302 |
|
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) |
| 303 |
#else |
#else |
| 304 |
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 305 |
#endif |
#endif |
| 321 |
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)); |
| 322 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 323 |
{ |
{ |
| 324 |
|
#ifdef _DEBUG |
| 325 |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
| 326 |
|
#endif |
| 327 |
retry = 0; |
retry = 0; |
| 328 |
break; |
break; |
| 329 |
} |
} |
| 407 |
{ |
{ |
| 408 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 409 |
{ |
{ |
| 410 |
|
#ifdef _DEBUG |
| 411 |
log_error("SSH channel is closed\n"); |
log_error("SSH channel is closed\n"); |
| 412 |
|
#endif |
| 413 |
loop = 0; |
loop = 0; |
| 414 |
break; |
break; |
| 415 |
} |
} |
| 448 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 449 |
{ |
{ |
| 450 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 451 |
if (events[i].data.fd == STDIN_FILENO) |
if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR))) |
| 452 |
|
#else |
| 453 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR))) |
| 454 |
|
#endif |
| 455 |
|
{ |
| 456 |
|
#ifdef _DEBUG |
| 457 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 458 |
|
log_error("STDIN error events (%d)\n", events[i].events); |
| 459 |
|
#else |
| 460 |
|
log_error("STDIN error events (%d)\n", pfds[i].revents); |
| 461 |
|
#endif |
| 462 |
|
#endif |
| 463 |
|
loop = 0; |
| 464 |
|
break; |
| 465 |
|
} |
| 466 |
|
|
| 467 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 468 |
|
if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) |
| 469 |
#else |
#else |
| 470 |
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 471 |
#endif |
#endif |
| 484 |
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); |
| 485 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 486 |
{ |
{ |
| 487 |
|
#ifdef _DEBUG |
| 488 |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 489 |
|
#endif |
| 490 |
loop = 0; |
loop = 0; |
| 491 |
break; |
break; |
| 492 |
} |
} |
| 1070 |
int ret; |
int ret; |
| 1071 |
int in_control = 0; |
int in_control = 0; |
| 1072 |
size_t i = 0; |
size_t i = 0; |
| 1073 |
|
int skip_current = 0; |
| 1074 |
|
|
| 1075 |
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) |
| 1076 |
{ |
{ |
| 1094 |
} |
} |
| 1095 |
} |
} |
| 1096 |
|
|
| 1097 |
if (in_control) |
if (in_control || skip_current) |
| 1098 |
{ |
{ |
| 1099 |
|
skip_current = 0; |
| 1100 |
|
|
| 1101 |
if (out_bytes <= 0) |
if (out_bytes <= 0) |
| 1102 |
{ |
{ |
| 1103 |
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); |
| 1111 |
out_bytes--; |
out_bytes--; |
| 1112 |
|
|
| 1113 |
(*p_buf_offset)++; |
(*p_buf_offset)++; |
| 1114 |
*p_conv_len = (int)(conv_size - out_bytes); |
(*p_conv_len)++; |
| 1115 |
|
|
| 1116 |
i++; |
i++; |
| 1117 |
if (i >= 2) |
if (i >= 2) |
| 1131 |
#endif |
#endif |
| 1132 |
if (p_buf != in_buf) |
if (p_buf != in_buf) |
| 1133 |
{ |
{ |
| 1134 |
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
*p_buf_len -= (int)(in_buf - p_buf); |
| 1135 |
*p_buf_offset = 0; |
*p_buf_offset = 0; |
| 1136 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
| 1137 |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
| 1156 |
if (in_bytes == 0) |
if (in_bytes == 0) |
| 1157 |
{ |
{ |
| 1158 |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 1159 |
|
#ifdef _DEBUG |
| 1160 |
|
log_error("Reset in_bytes from 0 to %d\n", in_bytes); |
| 1161 |
|
#endif |
| 1162 |
} |
} |
| 1163 |
|
|
| 1164 |
*out_buf = *in_buf; |
#ifdef _DEBUG |
| 1165 |
in_buf++; |
log_error("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n", |
| 1166 |
out_buf++; |
in_bytes, out_bytes, in_buf[0]); |
| 1167 |
in_bytes--; |
#endif |
| 1168 |
out_bytes--; |
skip_current = 1; |
| 1169 |
|
} |
| 1170 |
continue; |
else // something strange |
| 1171 |
|
{ |
| 1172 |
|
#ifdef _DEBUG |
| 1173 |
|
log_error("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n", |
| 1174 |
|
in_bytes, out_bytes, errno, in_buf[0]); |
| 1175 |
|
#endif |
| 1176 |
|
*p_buf_offset = (int)(in_buf - p_buf); |
| 1177 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 1178 |
|
skip_current = 1; |
| 1179 |
} |
} |
| 1180 |
} |
} |
| 1181 |
else |
else |
| 1182 |
{ |
{ |
| 1183 |
*p_buf_len = 0; |
*p_buf_offset = (int)(in_buf - p_buf); |
|
*p_buf_offset = 0; |
|
| 1184 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
|
|
|
|
break; |
|
| 1185 |
} |
} |
| 1186 |
} |
} |
| 1187 |
|
|
| 1188 |
|
if (*p_buf_offset >= *p_buf_len) |
| 1189 |
|
{ |
| 1190 |
|
*p_buf_len = 0; |
| 1191 |
|
*p_buf_offset = 0; |
| 1192 |
|
} |
| 1193 |
|
|
| 1194 |
return 0; |
return 0; |
| 1195 |
} |
} |
| 1196 |
|
|
| 1225 |
{ |
{ |
| 1226 |
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); |
| 1227 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1228 |
|
stdin_cd = (iconv_t)(-1); |
| 1229 |
return -2; |
return -2; |
| 1230 |
} |
} |
| 1231 |
|
|
| 1234 |
|
|
| 1235 |
int io_conv_cleanup(void) |
int io_conv_cleanup(void) |
| 1236 |
{ |
{ |
| 1237 |
if (stdin_cd != NULL) |
if (stdin_cd != (iconv_t)(-1)) |
| 1238 |
{ |
{ |
| 1239 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1240 |
stdin_cd = NULL; |
stdin_cd = (iconv_t)(-1); |
| 1241 |
} |
} |
| 1242 |
if (stdout_cd != NULL) |
if (stdout_cd != (iconv_t)(-1)) |
| 1243 |
{ |
{ |
| 1244 |
iconv_close(stdout_cd); |
iconv_close(stdout_cd); |
| 1245 |
stdout_cd = NULL; |
stdout_cd = (iconv_t)(-1); |
| 1246 |
} |
} |
| 1247 |
|
|
| 1248 |
return 0; |
return 0; |