| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
|
#include "common.h" |
| 18 |
#include "io.h" |
#include "io.h" |
| 19 |
#include "log.h" |
#include "log.h" |
|
#include "common.h" |
|
| 20 |
#include <errno.h> |
#include <errno.h> |
| 21 |
#include <stdio.h> |
#include <fcntl.h> |
| 22 |
#include <stdarg.h> |
#include <stdarg.h> |
| 23 |
|
#include <stdio.h> |
| 24 |
#include <string.h> |
#include <string.h> |
| 25 |
#include <time.h> |
#include <time.h> |
|
#include <fcntl.h> |
|
| 26 |
#include <unistd.h> |
#include <unistd.h> |
|
#include <sys/select.h> |
|
|
#include <sys/ioctl.h> |
|
| 27 |
#include <sys/epoll.h> |
#include <sys/epoll.h> |
| 28 |
|
#include <sys/ioctl.h> |
| 29 |
|
#include <sys/select.h> |
| 30 |
|
#include <libssh/callbacks.h> |
| 31 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 32 |
#include <libssh/server.h> |
#include <libssh/server.h> |
|
#include <libssh/callbacks.h> |
|
| 33 |
|
|
| 34 |
static char stdout_buf[BUFSIZ]; |
static char stdout_buf[BUFSIZ]; |
| 35 |
static int stdout_buf_len = 0; |
static int stdout_buf_len = 0; |
| 176 |
} |
} |
| 177 |
else |
else |
| 178 |
{ |
{ |
| 179 |
|
#ifdef _DEBUG |
| 180 |
log_error("write(STDOUT) error (%d)\n", errno); |
log_error("write(STDOUT) error (%d)\n", errno); |
| 181 |
|
#endif |
| 182 |
retry = 0; |
retry = 0; |
| 183 |
break; |
break; |
| 184 |
} |
} |
| 237 |
int i = 0; |
int i = 0; |
| 238 |
int flags; |
int flags; |
| 239 |
|
|
| 240 |
epollfd = epoll_create1(0); |
if (pos >= len) |
|
if (epollfd < 0) |
|
| 241 |
{ |
{ |
| 242 |
log_error("epoll_create1() error (%d)\n", errno); |
len = 0; |
| 243 |
return -1; |
pos = 0; |
|
} |
|
|
|
|
|
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); |
|
| 244 |
|
|
| 245 |
if (close(epollfd) < 0) |
epollfd = epoll_create1(0); |
| 246 |
|
if (epollfd < 0) |
| 247 |
{ |
{ |
| 248 |
log_error("close(epoll) error (%d)\n"); |
log_error("epoll_create1() error (%d)\n", errno); |
| 249 |
|
return -1; |
| 250 |
} |
} |
|
return -1; |
|
|
} |
|
| 251 |
|
|
| 252 |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
ev.events = EPOLLIN; |
| 253 |
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
ev.data.fd = STDIN_FILENO; |
| 254 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 255 |
|
{ |
| 256 |
|
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 257 |
|
|
| 258 |
loop = 1; |
if (close(epollfd) < 0) |
| 259 |
|
{ |
| 260 |
|
log_error("close(epoll) error (%d)\n"); |
| 261 |
|
} |
| 262 |
|
return -1; |
| 263 |
|
} |
| 264 |
|
|
| 265 |
while (loop && pos >= len && !SYS_server_exit) |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 266 |
{ |
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
|
len = 0; |
|
|
pos = 0; |
|
| 267 |
|
|
| 268 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
for (loop = 1; loop && pos >= len && !SYS_server_exit;) |
| 269 |
{ |
{ |
| 270 |
log_error("SSH channel is closed\n"); |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 271 |
loop = 0; |
{ |
| 272 |
break; |
log_error("SSH channel is closed\n"); |
| 273 |
} |
loop = 0; |
| 274 |
|
break; |
| 275 |
|
} |
| 276 |
|
|
| 277 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
| 278 |
|
|
| 279 |
if (nfds < 0) |
if (nfds < 0) |
|
{ |
|
|
if (errno != EINTR) |
|
| 280 |
{ |
{ |
| 281 |
log_error("epoll_wait() error (%d)\n", errno); |
if (errno != EINTR) |
| 282 |
|
{ |
| 283 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 284 |
|
break; |
| 285 |
|
} |
| 286 |
|
continue; |
| 287 |
|
} |
| 288 |
|
else if (nfds == 0) // timeout |
| 289 |
|
{ |
| 290 |
|
out = KEY_TIMEOUT; |
| 291 |
break; |
break; |
| 292 |
} |
} |
|
continue; |
|
|
} |
|
|
else if (nfds == 0) // timeout |
|
|
{ |
|
|
out = KEY_TIMEOUT; |
|
|
break; |
|
|
} |
|
| 293 |
|
|
| 294 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
|
{ |
|
|
if (events[i].data.fd == STDIN_FILENO) |
|
| 295 |
{ |
{ |
| 296 |
while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error |
if (events[i].data.fd == STDIN_FILENO) |
| 297 |
{ |
{ |
| 298 |
if (SSH_v2) |
while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error |
| 299 |
{ |
{ |
| 300 |
ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0); |
if (SSH_v2) |
|
if (ret == SSH_ERROR) |
|
| 301 |
{ |
{ |
| 302 |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0); |
| 303 |
loop = 0; |
if (ret == SSH_ERROR) |
| 304 |
break; |
{ |
| 305 |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 306 |
|
loop = 0; |
| 307 |
|
break; |
| 308 |
|
} |
| 309 |
|
else if (ret == SSH_EOF) |
| 310 |
|
{ |
| 311 |
|
loop = 0; |
| 312 |
|
break; |
| 313 |
|
} |
| 314 |
|
else if (ret == 0) |
| 315 |
|
{ |
| 316 |
|
out = 0; |
| 317 |
|
loop = 0; |
| 318 |
|
break; |
| 319 |
|
} |
| 320 |
} |
} |
| 321 |
else if (ret == SSH_EOF) |
else |
| 322 |
{ |
{ |
| 323 |
loop = 0; |
ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); |
|
break; |
|
| 324 |
} |
} |
| 325 |
else if (ret == 0) |
if (ret < 0) |
| 326 |
{ |
{ |
| 327 |
out = 0; |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 328 |
break; // Check whether channel is still open |
{ |
| 329 |
|
out = 0; |
| 330 |
|
loop = 0; |
| 331 |
|
break; |
| 332 |
|
} |
| 333 |
|
else if (errno == EINTR) |
| 334 |
|
{ |
| 335 |
|
continue; |
| 336 |
|
} |
| 337 |
|
else |
| 338 |
|
{ |
| 339 |
|
#ifdef _DEBUG |
| 340 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 341 |
|
#endif |
| 342 |
|
loop = 0; |
| 343 |
|
break; |
| 344 |
|
} |
| 345 |
} |
} |
| 346 |
} |
else if (ret == 0) // broken pipe |
|
else |
|
|
{ |
|
|
ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); |
|
|
} |
|
|
if (ret < 0) |
|
|
{ |
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
|
| 347 |
{ |
{ |
|
out = 0; |
|
| 348 |
loop = 0; |
loop = 0; |
| 349 |
break; |
break; |
| 350 |
} |
} |
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
| 351 |
else |
else |
| 352 |
{ |
{ |
| 353 |
log_error("read(STDIN) error (%d)\n", errno); |
len += ret; |
| 354 |
loop = 0; |
continue; |
|
break; |
|
| 355 |
} |
} |
| 356 |
} |
} |
|
else if (ret == 0) // broken pipe |
|
|
{ |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else |
|
|
{ |
|
|
len += ret; |
|
|
continue; |
|
|
} |
|
| 357 |
} |
} |
| 358 |
} |
} |
| 359 |
|
|
| 360 |
|
// For debug |
| 361 |
|
#ifdef _DEBUG |
| 362 |
|
for (int j = pos; j < len; j++) |
| 363 |
|
{ |
| 364 |
|
log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256); |
| 365 |
|
} |
| 366 |
|
#endif |
| 367 |
} |
} |
| 368 |
|
|
| 369 |
// For debug |
fcntl(STDIN_FILENO, F_SETFL, flags); |
|
// for (int j = pos; j < len; j++) |
|
|
// { |
|
|
// log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256); |
|
|
// } |
|
|
} |
|
| 370 |
|
|
| 371 |
fcntl(STDIN_FILENO, F_SETFL, flags); |
if (close(epollfd) < 0) |
| 372 |
|
{ |
| 373 |
|
log_error("close(epoll) error (%d)\n"); |
| 374 |
|
} |
| 375 |
|
} |
| 376 |
|
|
| 377 |
while (pos < len) |
while (pos < len) |
| 378 |
{ |
{ |
| 379 |
unsigned char c = buf[pos++]; |
unsigned char c = buf[pos++]; |
| 380 |
|
|
| 381 |
|
// Convert \r\n to \r |
| 382 |
|
if (c == CR && pos < len && buf[pos] == LF) |
| 383 |
|
{ |
| 384 |
|
pos++; |
| 385 |
|
} |
| 386 |
|
|
| 387 |
|
// Convert single \n to \r |
| 388 |
|
if (c == LF) |
| 389 |
|
{ |
| 390 |
|
c = CR; |
| 391 |
|
} |
| 392 |
|
|
| 393 |
if (c == KEY_CONTROL) |
if (c == KEY_CONTROL) |
| 394 |
{ |
{ |
| 395 |
if (in_control == 0) |
if (in_control == 0) |
| 824 |
break; |
break; |
| 825 |
} |
} |
| 826 |
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
|
|
| 827 |
// For ESC key |
// For ESC key |
| 828 |
if (out == 0 && in_esc) |
if (out == 0 && in_esc) |
| 829 |
{ |
{ |
| 831 |
} |
} |
| 832 |
|
|
| 833 |
// for debug |
// for debug |
| 834 |
// if (out != KEY_TIMEOUT && out != KEY_NULL) |
#ifdef _DEBUG |
| 835 |
// { |
if (out != KEY_TIMEOUT && out != KEY_NULL) |
| 836 |
// log_common("Debug: -->[0x %x]\n", out); |
{ |
| 837 |
// } |
log_common("Debug: -->[0x %x]\n", out); |
| 838 |
|
} |
| 839 |
|
#endif |
| 840 |
|
|
| 841 |
return out; |
return out; |
| 842 |
} |
} |
| 859 |
int ch; |
int ch; |
| 860 |
do |
do |
| 861 |
{ |
{ |
| 862 |
ch = igetch(0); |
ch = igetch(100); |
| 863 |
} while (ch != KEY_NULL && ch != KEY_TIMEOUT); |
} while (ch != KEY_NULL && ch != KEY_TIMEOUT); |
| 864 |
} |
} |
| 865 |
|
|
| 866 |
|
int io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len) |
| 867 |
|
{ |
| 868 |
|
char *in_buf; |
| 869 |
|
char *out_buf; |
| 870 |
|
size_t in_bytes; |
| 871 |
|
size_t out_bytes; |
| 872 |
|
int ret; |
| 873 |
|
|
| 874 |
|
in_buf = p_buf + *p_buf_offset; |
| 875 |
|
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 876 |
|
out_buf = p_conv + *p_conv_len; |
| 877 |
|
out_bytes = conv_size - (size_t)(*p_conv_len); |
| 878 |
|
|
| 879 |
|
while (in_bytes > 0) |
| 880 |
|
{ |
| 881 |
|
ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes); |
| 882 |
|
if (ret == -1) |
| 883 |
|
{ |
| 884 |
|
if (errno == EINVAL) // Incomplete |
| 885 |
|
{ |
| 886 |
|
#ifdef _DEBUG |
| 887 |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes); |
| 888 |
|
#endif |
| 889 |
|
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
| 890 |
|
*p_buf_offset = 0; |
| 891 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 892 |
|
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
| 893 |
|
|
| 894 |
|
break; |
| 895 |
|
} |
| 896 |
|
else if (errno == E2BIG) |
| 897 |
|
{ |
| 898 |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); |
| 899 |
|
return -1; |
| 900 |
|
} |
| 901 |
|
else if (errno == EILSEQ) |
| 902 |
|
{ |
| 903 |
|
if (in_bytes > out_bytes || out_bytes <= 0) |
| 904 |
|
{ |
| 905 |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); |
| 906 |
|
return -2; |
| 907 |
|
} |
| 908 |
|
|
| 909 |
|
*out_buf = *in_buf; |
| 910 |
|
in_buf++; |
| 911 |
|
out_buf++; |
| 912 |
|
in_bytes--; |
| 913 |
|
out_bytes--; |
| 914 |
|
|
| 915 |
|
continue; |
| 916 |
|
} |
| 917 |
|
} |
| 918 |
|
else |
| 919 |
|
{ |
| 920 |
|
*p_buf_len = 0; |
| 921 |
|
*p_buf_offset = 0; |
| 922 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 923 |
|
|
| 924 |
|
break; |
| 925 |
|
} |
| 926 |
|
} |
| 927 |
|
|
| 928 |
|
return 0; |
| 929 |
|
} |