| 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 && !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) |
| 280 |
{ |
{ |
| 281 |
if (errno != EINTR) |
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 |
log_error("epoll_wait() error (%d)\n", errno); |
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 |
|
break; // Check whether channel is still open |
| 318 |
|
} |
| 319 |
} |
} |
| 320 |
else if (ret == SSH_EOF) |
else |
| 321 |
{ |
{ |
| 322 |
loop = 0; |
ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); |
|
break; |
|
| 323 |
} |
} |
| 324 |
else if (ret == 0) |
if (ret < 0) |
| 325 |
{ |
{ |
| 326 |
out = 0; |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 327 |
break; // Check whether channel is still open |
{ |
| 328 |
|
out = 0; |
| 329 |
|
loop = 0; |
| 330 |
|
break; |
| 331 |
|
} |
| 332 |
|
else if (errno == EINTR) |
| 333 |
|
{ |
| 334 |
|
continue; |
| 335 |
|
} |
| 336 |
|
else |
| 337 |
|
{ |
| 338 |
|
#ifdef _DEBUG |
| 339 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 340 |
|
#endif |
| 341 |
|
loop = 0; |
| 342 |
|
break; |
| 343 |
|
} |
| 344 |
} |
} |
| 345 |
} |
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) |
|
| 346 |
{ |
{ |
|
out = 0; |
|
| 347 |
loop = 0; |
loop = 0; |
| 348 |
break; |
break; |
| 349 |
} |
} |
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
| 350 |
else |
else |
| 351 |
{ |
{ |
| 352 |
#ifdef _DEBUG |
len += ret; |
| 353 |
log_error("read(STDIN) error (%d)\n", errno); |
continue; |
|
#endif |
|
|
loop = 0; |
|
|
break; |
|
| 354 |
} |
} |
| 355 |
} |
} |
|
else if (ret == 0) // broken pipe |
|
|
{ |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else |
|
|
{ |
|
|
len += ret; |
|
|
continue; |
|
|
} |
|
| 356 |
} |
} |
| 357 |
} |
} |
|
} |
|
| 358 |
|
|
| 359 |
// For debug |
// For debug |
| 360 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 361 |
for (int j = pos; j < len; j++) |
for (int j = pos; j < len; j++) |
| 362 |
|
{ |
| 363 |
|
log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256); |
| 364 |
|
} |
| 365 |
|
#endif |
| 366 |
|
} |
| 367 |
|
|
| 368 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 369 |
|
|
| 370 |
|
if (close(epollfd) < 0) |
| 371 |
{ |
{ |
| 372 |
log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256); |
log_error("close(epoll) error (%d)\n"); |
| 373 |
} |
} |
|
#endif |
|
| 374 |
} |
} |
| 375 |
|
|
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
|
|
|
|
| 376 |
while (pos < len) |
while (pos < len) |
| 377 |
{ |
{ |
| 378 |
unsigned char c = buf[pos++]; |
unsigned char c = buf[pos++]; |
| 811 |
break; |
break; |
| 812 |
} |
} |
| 813 |
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
|
|
| 814 |
// For ESC key |
// For ESC key |
| 815 |
if (out == 0 && in_esc) |
if (out == 0 && in_esc) |
| 816 |
{ |
{ |