| 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> |
| 32 |
|
#include <libssh/server.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; |
| 91 |
return ret; |
return ret; |
| 92 |
} |
} |
| 93 |
|
|
| 94 |
int iflush() |
int iflush(void) |
| 95 |
{ |
{ |
| 96 |
int flags; |
int flags; |
| 97 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 150 |
{ |
{ |
| 151 |
while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error |
while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error |
| 152 |
{ |
{ |
| 153 |
ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset)); |
if (SSH_v2) |
| 154 |
|
{ |
| 155 |
|
ret = ssh_channel_write(SSH_channel, stdout_buf + stdout_buf_offset, (uint32_t)(stdout_buf_len - stdout_buf_offset)); |
| 156 |
|
if (ret == SSH_ERROR) |
| 157 |
|
{ |
| 158 |
|
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
| 159 |
|
retry = 0; |
| 160 |
|
break; |
| 161 |
|
} |
| 162 |
|
} |
| 163 |
|
else |
| 164 |
|
{ |
| 165 |
|
ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset)); |
| 166 |
|
} |
| 167 |
if (ret < 0) |
if (ret < 0) |
| 168 |
{ |
{ |
| 169 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 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 |
loop = 1; |
{ |
| 256 |
|
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 257 |
|
|
| 258 |
while (loop && pos >= len && !SYS_server_exit) |
if (close(epollfd) < 0) |
| 259 |
{ |
{ |
| 260 |
len = 0; |
log_error("close(epoll) error (%d)\n"); |
| 261 |
pos = 0; |
} |
| 262 |
|
return -1; |
| 263 |
|
} |
| 264 |
|
|
| 265 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 266 |
|
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 267 |
|
|
| 268 |
if (nfds < 0) |
for (loop = 1; loop && pos >= len && !SYS_server_exit;) |
| 269 |
{ |
{ |
| 270 |
if (errno != EINTR) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 271 |
{ |
{ |
| 272 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("SSH channel is closed\n"); |
| 273 |
|
loop = 0; |
| 274 |
break; |
break; |
| 275 |
} |
} |
|
continue; |
|
|
} |
|
|
else if (nfds == 0) // timeout |
|
|
{ |
|
|
out = KEY_TIMEOUT; |
|
|
break; |
|
|
} |
|
| 276 |
|
|
| 277 |
for (int i = 0; i < nfds; i++) |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
| 278 |
{ |
|
| 279 |
if (events[i].data.fd == STDIN_FILENO) |
if (nfds < 0) |
| 280 |
{ |
{ |
| 281 |
while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error |
if (errno != EINTR) |
| 282 |
{ |
{ |
| 283 |
ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); |
log_error("epoll_wait() error (%d)\n", errno); |
| 284 |
if (ret < 0) |
break; |
| 285 |
|
} |
| 286 |
|
continue; |
| 287 |
|
} |
| 288 |
|
else if (nfds == 0) // timeout |
| 289 |
|
{ |
| 290 |
|
out = KEY_TIMEOUT; |
| 291 |
|
break; |
| 292 |
|
} |
| 293 |
|
|
| 294 |
|
for (int i = 0; i < nfds; i++) |
| 295 |
|
{ |
| 296 |
|
if (events[i].data.fd == STDIN_FILENO) |
| 297 |
|
{ |
| 298 |
|
while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error |
| 299 |
{ |
{ |
| 300 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (SSH_v2) |
| 301 |
{ |
{ |
| 302 |
out = 0; |
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 (errno == EINTR) |
else |
| 322 |
{ |
{ |
| 323 |
continue; |
ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); |
| 324 |
} |
} |
| 325 |
else |
if (ret < 0) |
| 326 |
|
{ |
| 327 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 328 |
|
{ |
| 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 |
| 347 |
{ |
{ |
|
log_error("read(STDIN) error (%d)\n", errno); |
|
| 348 |
loop = 0; |
loop = 0; |
| 349 |
break; |
break; |
| 350 |
} |
} |
| 351 |
} |
else |
| 352 |
else if (ret == 0) // broken pipe |
{ |
| 353 |
{ |
len += ret; |
| 354 |
loop = 0; |
continue; |
| 355 |
break; |
} |
|
} |
|
|
else |
|
|
{ |
|
|
len += ret; |
|
|
continue; |
|
| 356 |
} |
} |
| 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_std("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) |
| 412 |
continue; |
continue; |
| 413 |
} |
} |
| 414 |
|
|
| 415 |
if (c == ESC_KEY) |
if (c == KEY_ESC) |
| 416 |
{ |
{ |
| 417 |
if (in_esc == 0) |
if (in_esc == 0) |
| 418 |
{ |
{ |
| 423 |
} |
} |
| 424 |
else |
else |
| 425 |
{ |
{ |
| 426 |
out = ESC_KEY; |
out = KEY_CSI; |
| 427 |
in_esc = 0; |
in_esc = 0; |
| 428 |
break; |
break; |
| 429 |
} |
} |
| 559 |
case 49: |
case 49: |
| 560 |
out = KEY_HOME; |
out = KEY_HOME; |
| 561 |
break; |
break; |
| 562 |
|
case 50: |
| 563 |
|
out = KEY_INS; |
| 564 |
|
break; |
| 565 |
case 51: |
case 51: |
| 566 |
out = KEY_DEL; |
out = KEY_DEL; |
| 567 |
break; |
break; |
| 582 |
break; |
break; |
| 583 |
} |
} |
| 584 |
} |
} |
| 585 |
if (i == 4 && tmp[0] == 91 && tmp[1] == 49 && tmp[3] == 126) // Fterm |
if (i == 4 && tmp[0] == 91 && tmp[1] == 49 && tmp[3] == 126) // Fterm |
| 586 |
{ |
{ |
| 587 |
in_ascii = 0; |
in_ascii = 0; |
| 588 |
switch (tmp[2]) |
switch (tmp[2]) |
| 652 |
in_ascii = 0; |
in_ascii = 0; |
| 653 |
switch (tmp[4]) |
switch (tmp[4]) |
| 654 |
{ |
{ |
| 655 |
|
case 65: |
| 656 |
|
out = KEY_CTRL_UP; |
| 657 |
|
break; |
| 658 |
|
case 66: |
| 659 |
|
out = KEY_CTRL_DOWN; |
| 660 |
|
break; |
| 661 |
|
case 67: |
| 662 |
|
out = KEY_CTRL_RIGHT; |
| 663 |
|
break; |
| 664 |
|
case 68: |
| 665 |
|
out = KEY_CTRL_LEFT; |
| 666 |
|
break; |
| 667 |
|
case 70: |
| 668 |
|
out = KEY_CTRL_END; |
| 669 |
|
break; |
| 670 |
|
case 72: |
| 671 |
|
out = KEY_CTRL_HOME; |
| 672 |
|
break; |
| 673 |
case 80: |
case 80: |
| 674 |
out = KEY_CTRL_F1; |
out = KEY_CTRL_F1; |
| 675 |
break; |
break; |
| 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_std ("Debug: -->[0x %x]\n", out); |
{ |
| 837 |
// } |
log_common("Debug: -->[0x %x]\n", out); |
| 838 |
|
} |
| 839 |
|
#endif |
| 840 |
|
|
| 841 |
return out; |
return out; |
| 842 |
} |
} |
| 844 |
int igetch_t(int sec) |
int igetch_t(int sec) |
| 845 |
{ |
{ |
| 846 |
int ch; |
int ch; |
| 847 |
time_t t_begin = time(0); |
time_t t_begin = time(NULL); |
| 848 |
|
|
| 849 |
do |
do |
| 850 |
{ |
{ |
| 851 |
ch = igetch(100); |
ch = igetch(100); |
| 852 |
} while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec)); |
} while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(NULL) - t_begin < sec)); |
| 853 |
|
|
| 854 |
return ch; |
return ch; |
| 855 |
} |
} |
| 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 |
} |
} |