| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
io.c - description |
/* |
| 3 |
------------------- |
* io |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - basic terminal-based user input / output features |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "common.h" |
#include "common.h" |
| 14 |
#include "io.h" |
#include "io.h" |
| 20 |
#include <string.h> |
#include <string.h> |
| 21 |
#include <time.h> |
#include <time.h> |
| 22 |
#include <unistd.h> |
#include <unistd.h> |
|
#include <sys/epoll.h> |
|
| 23 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 24 |
#include <sys/select.h> |
#include <sys/select.h> |
| 25 |
#include <libssh/callbacks.h> |
#include <libssh/callbacks.h> |
| 26 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 27 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 28 |
|
|
| 29 |
static char stdout_buf[BUFSIZ]; |
#ifdef HAVE_SYS_EPOLL_H |
| 30 |
|
#include <sys/epoll.h> |
| 31 |
|
#else |
| 32 |
|
#include <poll.h> |
| 33 |
|
#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"; |
| 41 |
|
char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 42 |
|
|
| 43 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 44 |
|
// epoll for STDIO |
| 45 |
|
static int stdin_epollfd = -1; |
| 46 |
|
static int stdout_epollfd = -1; |
| 47 |
|
#endif |
| 48 |
|
|
| 49 |
|
static int stdin_flags = 0; |
| 50 |
|
static int stdout_flags = 0; |
| 51 |
|
|
| 52 |
|
// static input / output buffer |
| 53 |
|
static char stdin_buf[LINE_BUFFER_LEN]; |
| 54 |
|
static char stdout_buf[OUTPUT_BUF_SIZE]; |
| 55 |
|
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; |
| 58 |
static int stdout_buf_offset = 0; |
static int stdout_buf_offset = 0; |
| 59 |
|
|
| 60 |
|
static char stdin_conv[LINE_BUFFER_LEN * 2]; |
| 61 |
|
static char stdout_conv[OUTPUT_BUF_SIZE * 2]; |
| 62 |
|
static int stdin_conv_len = 0; |
| 63 |
|
static int stdout_conv_len = 0; |
| 64 |
|
static int stdin_conv_offset = 0; |
| 65 |
|
static int stdout_conv_offset = 0; |
| 66 |
|
|
| 67 |
|
static iconv_t stdin_cd = (iconv_t)(-1); |
| 68 |
|
static iconv_t stdout_cd = (iconv_t)(-1); |
| 69 |
|
|
| 70 |
|
int io_init(void) |
| 71 |
|
{ |
| 72 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 73 |
|
struct epoll_event ev; |
| 74 |
|
|
| 75 |
|
if (stdin_epollfd == -1) |
| 76 |
|
{ |
| 77 |
|
stdin_epollfd = epoll_create1(0); |
| 78 |
|
if (stdin_epollfd == -1) |
| 79 |
|
{ |
| 80 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 81 |
|
return -1; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
ev.events = EPOLLIN; |
| 85 |
|
ev.data.fd = STDIN_FILENO; |
| 86 |
|
if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 87 |
|
{ |
| 88 |
|
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 89 |
|
if (close(stdin_epollfd) < 0) |
| 90 |
|
{ |
| 91 |
|
log_error("close(stdin_epollfd) error (%d)\n", errno); |
| 92 |
|
} |
| 93 |
|
stdin_epollfd = -1; |
| 94 |
|
return -1; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) |
| 98 |
|
{ |
| 99 |
|
log_error("fcntl(F_GETFL) error (%d)\n", errno); |
| 100 |
|
if (close(stdin_epollfd) < 0) |
| 101 |
|
{ |
| 102 |
|
log_error("close(stdin_epollfd) error (%d)\n", errno); |
| 103 |
|
} |
| 104 |
|
stdin_epollfd = -1; |
| 105 |
|
return -1; |
| 106 |
|
} |
| 107 |
|
if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1) |
| 108 |
|
{ |
| 109 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 110 |
|
if (close(stdin_epollfd) < 0) |
| 111 |
|
{ |
| 112 |
|
log_error("close(stdin_epollfd) error (%d)\n", errno); |
| 113 |
|
} |
| 114 |
|
stdin_epollfd = -1; |
| 115 |
|
return -1; |
| 116 |
|
} |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
if (stdout_epollfd == -1) |
| 120 |
|
{ |
| 121 |
|
stdout_epollfd = epoll_create1(0); |
| 122 |
|
if (stdout_epollfd == -1) |
| 123 |
|
{ |
| 124 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 125 |
|
return -1; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
ev.events = EPOLLOUT; |
| 129 |
|
ev.data.fd = STDOUT_FILENO; |
| 130 |
|
if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 131 |
|
{ |
| 132 |
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 133 |
|
if (close(stdout_epollfd) < 0) |
| 134 |
|
{ |
| 135 |
|
log_error("close(stdout_epollfd) error (%d)\n", errno); |
| 136 |
|
} |
| 137 |
|
stdout_epollfd = -1; |
| 138 |
|
return -1; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) |
| 142 |
|
{ |
| 143 |
|
log_error("fcntl(F_GETFL) error (%d)\n", errno); |
| 144 |
|
if (close(stdout_epollfd) < 0) |
| 145 |
|
{ |
| 146 |
|
log_error("close(stdout_epollfd) error (%d)\n", errno); |
| 147 |
|
} |
| 148 |
|
stdout_epollfd = -1; |
| 149 |
|
return -1; |
| 150 |
|
} |
| 151 |
|
if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1) |
| 152 |
|
{ |
| 153 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 154 |
|
if (close(stdout_epollfd) < 0) |
| 155 |
|
{ |
| 156 |
|
log_error("close(stdout_epollfd) error (%d)\n", errno); |
| 157 |
|
} |
| 158 |
|
stdout_epollfd = -1; |
| 159 |
|
return -1; |
| 160 |
|
} |
| 161 |
|
} |
| 162 |
|
#else |
| 163 |
|
if (stdin_flags == 0) |
| 164 |
|
{ |
| 165 |
|
if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) |
| 166 |
|
{ |
| 167 |
|
log_error("fcntl(F_GETFL) error (%d)\n", errno); |
| 168 |
|
return -1; |
| 169 |
|
} |
| 170 |
|
if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1) |
| 171 |
|
{ |
| 172 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 173 |
|
return -1; |
| 174 |
|
} |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
if (stdout_flags == 0) |
| 178 |
|
{ |
| 179 |
|
if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) |
| 180 |
|
{ |
| 181 |
|
log_error("fcntl(F_GETFL) error (%d)\n", errno); |
| 182 |
|
return -1; |
| 183 |
|
} |
| 184 |
|
if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1) |
| 185 |
|
{ |
| 186 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 187 |
|
return -1; |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
|
#endif |
| 191 |
|
|
| 192 |
|
return 0; |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
void io_cleanup(void) |
| 196 |
|
{ |
| 197 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 198 |
|
if (stdin_epollfd != -1) |
| 199 |
|
{ |
| 200 |
|
fcntl(STDIN_FILENO, F_SETFL, stdin_flags); |
| 201 |
|
stdin_flags = 0; |
| 202 |
|
|
| 203 |
|
if (close(stdin_epollfd) < 0) |
| 204 |
|
{ |
| 205 |
|
log_error("close(stdin_epollfd) error (%d)\n", errno); |
| 206 |
|
} |
| 207 |
|
stdin_epollfd = -1; |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
if (stdout_epollfd != -1) |
| 211 |
|
{ |
| 212 |
|
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); |
| 213 |
|
stdout_flags = 0; |
| 214 |
|
|
| 215 |
|
if (close(stdout_epollfd) < 0) |
| 216 |
|
{ |
| 217 |
|
log_error("close(stdout_epollfd) error (%d)\n", errno); |
| 218 |
|
} |
| 219 |
|
stdout_epollfd = -1; |
| 220 |
|
} |
| 221 |
|
#else |
| 222 |
|
if (stdin_flags != 0) |
| 223 |
|
{ |
| 224 |
|
fcntl(STDIN_FILENO, F_SETFL, stdin_flags); |
| 225 |
|
stdin_flags = 0; |
| 226 |
|
} |
| 227 |
|
|
| 228 |
|
if (stdout_flags != 0) |
| 229 |
|
{ |
| 230 |
|
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); |
| 231 |
|
stdout_flags = 0; |
| 232 |
|
} |
| 233 |
|
#endif |
| 234 |
|
} |
| 235 |
|
|
| 236 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 237 |
{ |
{ |
| 238 |
char buf[BUFSIZ]; |
char buf[OUTPUT_BUF_SIZE]; |
| 239 |
va_list args; |
va_list args; |
| 240 |
int ret; |
int ret; |
| 241 |
|
|
| 243 |
ret = vsnprintf(buf, sizeof(buf), format, args); |
ret = vsnprintf(buf, sizeof(buf), format, args); |
| 244 |
va_end(args); |
va_end(args); |
| 245 |
|
|
| 246 |
if (ret > 0) |
if (ret >= 0) |
| 247 |
{ |
{ |
| 248 |
if (stdout_buf_len + ret > BUFSIZ) |
int written = (ret >= sizeof(buf) ? (int)sizeof(buf) - 1 : ret); |
| 249 |
|
|
| 250 |
|
if (stdout_buf_len + written > OUTPUT_BUF_SIZE) |
| 251 |
{ |
{ |
| 252 |
iflush(); |
iflush(); |
| 253 |
} |
} |
| 254 |
|
|
| 255 |
if (stdout_buf_len + ret <= BUFSIZ) |
if (stdout_buf_len + written <= OUTPUT_BUF_SIZE) |
| 256 |
{ |
{ |
| 257 |
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); |
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)written); |
| 258 |
stdout_buf_len += ret; |
stdout_buf_len += written; |
| 259 |
|
ret = written; |
| 260 |
} |
} |
| 261 |
else |
else |
| 262 |
{ |
{ |
| 263 |
errno = EAGAIN; |
errno = EAGAIN; |
| 264 |
ret = (BUFSIZ - stdout_buf_len - ret); |
int need = stdout_buf_len + written - OUTPUT_BUF_SIZE; |
| 265 |
log_error("Output buffer is full, additional %d is required\n", ret); |
log_error("Output buffer is full, additional %d bytes are required\n", need); |
| 266 |
|
ret = -need; |
| 267 |
} |
} |
| 268 |
} |
} |
| 269 |
|
|
| 272 |
|
|
| 273 |
int outc(char c) |
int outc(char c) |
| 274 |
{ |
{ |
| 275 |
int ret; |
int ret = 0; |
| 276 |
|
|
| 277 |
if (stdout_buf_len + 1 > BUFSIZ) |
if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE) |
| 278 |
{ |
{ |
| 279 |
iflush(); |
iflush(); |
| 280 |
} |
} |
| 281 |
|
|
| 282 |
if (stdout_buf_len + 1 <= BUFSIZ) |
if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE) |
| 283 |
{ |
{ |
| 284 |
stdout_buf[stdout_buf_len] = c; |
stdout_buf[stdout_buf_len] = c; |
| 285 |
stdout_buf_len++; |
stdout_buf_len++; |
| 295 |
|
|
| 296 |
int iflush(void) |
int iflush(void) |
| 297 |
{ |
{ |
| 298 |
int flags; |
#ifdef HAVE_SYS_EPOLL_H |
| 299 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event events[MAX_EVENTS]; |
| 300 |
int nfds, epollfd; |
#else |
| 301 |
int retry; |
struct pollfd pfds[1]; |
| 302 |
int ret = 0; |
#endif |
|
|
|
|
epollfd = epoll_create1(0); |
|
|
if (epollfd < 0) |
|
|
{ |
|
|
log_error("epoll_create1() error (%d)\n", errno); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
ev.events = EPOLLOUT; |
|
|
ev.data.fd = STDOUT_FILENO; |
|
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
|
|
{ |
|
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
return -1; |
|
|
} |
|
| 303 |
|
|
| 304 |
// Set STDOUT as non-blocking |
int nfds; |
| 305 |
flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
int ret = 0; |
|
fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); |
|
| 306 |
|
|
| 307 |
// Retry wait / flush for at most 3 times |
// Retry wait / flush for at most 3 times |
| 308 |
retry = 3; |
for (int retry = 3; retry > 0 && !SYS_server_exit; retry--) |
|
while (retry > 0 && !SYS_server_exit) |
|
| 309 |
{ |
{ |
| 310 |
retry--; |
#ifdef HAVE_SYS_EPOLL_H |
| 311 |
|
nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 312 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
ret = nfds; |
| 313 |
|
#else |
| 314 |
|
pfds[0].fd = STDOUT_FILENO; |
| 315 |
|
pfds[0].events = POLLOUT; |
| 316 |
|
nfds = 1; |
| 317 |
|
ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second |
| 318 |
|
#endif |
| 319 |
|
|
| 320 |
if (nfds < 0) |
if (ret < 0) |
| 321 |
{ |
{ |
| 322 |
if (errno != EINTR) |
if (errno != EINTR) |
| 323 |
{ |
{ |
| 324 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 325 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 326 |
|
#else |
| 327 |
|
log_error("poll() error (%d)\n", errno); |
| 328 |
|
#endif |
| 329 |
break; |
break; |
| 330 |
} |
} |
| 331 |
continue; |
continue; |
| 332 |
} |
} |
| 333 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 334 |
{ |
{ |
| 335 |
continue; |
continue; |
| 336 |
} |
} |
| 337 |
|
|
| 338 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 339 |
{ |
{ |
| 340 |
if (events[i].data.fd == STDOUT_FILENO) |
#ifdef HAVE_SYS_EPOLL_H |
| 341 |
|
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR))) |
| 342 |
|
#else |
| 343 |
|
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR))) |
| 344 |
|
#endif |
| 345 |
{ |
{ |
| 346 |
while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error |
#ifdef HAVE_SYS_EPOLL_H |
| 347 |
|
log_debug("STDOUT error events (%d)\n", events[i].events); |
| 348 |
|
#else |
| 349 |
|
log_debug("STDOUT error events (%d)\n", pfds[i].revents); |
| 350 |
|
#endif |
| 351 |
|
retry = 0; |
| 352 |
|
break; |
| 353 |
|
} |
| 354 |
|
|
| 355 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 356 |
|
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) |
| 357 |
|
#else |
| 358 |
|
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 359 |
|
#endif |
| 360 |
|
{ |
| 361 |
|
if (stdout_buf_offset < stdout_buf_len) |
| 362 |
|
{ |
| 363 |
|
ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len); |
| 364 |
|
if (ret < 0) |
| 365 |
|
{ |
| 366 |
|
log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len); |
| 367 |
|
stdout_buf_len = stdout_buf_offset; // Discard invalid sequence |
| 368 |
|
} |
| 369 |
|
} |
| 370 |
|
|
| 371 |
|
while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error |
| 372 |
{ |
{ |
| 373 |
if (SSH_v2) |
if (SSH_v2) |
| 374 |
{ |
{ |
| 375 |
ret = ssh_channel_write(SSH_channel, stdout_buf + stdout_buf_offset, (uint32_t)(stdout_buf_len - stdout_buf_offset)); |
ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset)); |
| 376 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 377 |
{ |
{ |
| 378 |
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)); |
| 379 |
retry = 0; |
retry = 0; |
| 380 |
break; |
break; |
| 381 |
} |
} |
| 382 |
} |
} |
| 383 |
else |
else |
| 384 |
{ |
{ |
| 385 |
ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset)); |
ret = (int)write(STDOUT_FILENO, stdout_conv + stdout_conv_offset, (size_t)(stdout_conv_len - stdout_conv_offset)); |
| 386 |
} |
} |
| 387 |
if (ret < 0) |
if (ret < 0) |
| 388 |
{ |
{ |
| 396 |
} |
} |
| 397 |
else |
else |
| 398 |
{ |
{ |
| 399 |
#ifdef _DEBUG |
log_debug("write(STDOUT) error (%d)\n", errno); |
|
log_error("write(STDOUT) error (%d)\n", errno); |
|
|
#endif |
|
| 400 |
retry = 0; |
retry = 0; |
| 401 |
break; |
break; |
| 402 |
} |
} |
| 408 |
} |
} |
| 409 |
else |
else |
| 410 |
{ |
{ |
| 411 |
stdout_buf_offset += ret; |
stdout_conv_offset += ret; |
| 412 |
if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely |
if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely |
| 413 |
{ |
{ |
| 414 |
ret = 0; |
ret = 0; |
| 415 |
stdout_buf_offset = 0; |
stdout_conv_offset = 0; |
| 416 |
stdout_buf_len = 0; |
stdout_conv_len = 0; |
| 417 |
retry = 0; |
retry = 0; |
| 418 |
break; |
break; |
| 419 |
} |
} |
| 424 |
} |
} |
| 425 |
} |
} |
| 426 |
|
|
|
// Restore STDOUT flags |
|
|
fcntl(STDOUT_FILENO, F_SETFL, flags); |
|
|
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
|
|
| 427 |
return ret; |
return ret; |
| 428 |
} |
} |
| 429 |
|
|
| 430 |
int igetch(int timeout) |
int igetch(int timeout) |
| 431 |
{ |
{ |
| 432 |
// static input buffer |
static int stdin_read_wait = 0; |
| 433 |
static unsigned char buf[LINE_BUFFER_LEN]; |
|
| 434 |
static int len = 0; |
#ifdef HAVE_SYS_EPOLL_H |
| 435 |
static int pos = 0; |
struct epoll_event events[MAX_EVENTS]; |
| 436 |
|
#else |
| 437 |
|
struct pollfd pfds[1]; |
| 438 |
|
#endif |
| 439 |
|
|
| 440 |
struct epoll_event ev, events[MAX_EVENTS]; |
int nfds; |
|
int nfds, epollfd; |
|
| 441 |
int ret; |
int ret; |
| 442 |
int loop; |
int loop; |
| 443 |
|
|
| 447 |
int in_ascii = 0; |
int in_ascii = 0; |
| 448 |
int in_control = 0; |
int in_control = 0; |
| 449 |
int i = 0; |
int i = 0; |
|
int flags; |
|
| 450 |
|
|
| 451 |
if (pos >= len) |
if (stdin_conv_offset >= stdin_conv_len) |
| 452 |
{ |
{ |
| 453 |
len = 0; |
stdin_conv_len = 0; |
| 454 |
pos = 0; |
stdin_conv_offset = 0; |
|
|
|
|
epollfd = epoll_create1(0); |
|
|
if (epollfd < 0) |
|
|
{ |
|
|
log_error("epoll_create1() error (%d)\n", errno); |
|
|
return -1; |
|
|
} |
|
| 455 |
|
|
| 456 |
ev.events = EPOLLIN; |
for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) |
|
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); |
|
|
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
return -1; |
|
|
} |
|
|
|
|
|
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
|
|
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
|
|
|
|
|
for (loop = 1; loop && pos >= len && !SYS_server_exit;) |
|
| 457 |
{ |
{ |
| 458 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 459 |
{ |
{ |
| 460 |
log_error("SSH channel is closed\n"); |
log_debug("SSH channel is closed\n"); |
| 461 |
loop = 0; |
loop = 0; |
| 462 |
break; |
break; |
| 463 |
} |
} |
| 464 |
|
|
| 465 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
if (!stdin_read_wait) |
|
|
|
|
if (nfds < 0) |
|
| 466 |
{ |
{ |
| 467 |
if (errno != EINTR) |
#ifdef HAVE_SYS_EPOLL_H |
| 468 |
|
nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout); |
| 469 |
|
ret = nfds; |
| 470 |
|
#else |
| 471 |
|
pfds[0].fd = STDIN_FILENO; |
| 472 |
|
pfds[0].events = POLLIN; |
| 473 |
|
nfds = 1; |
| 474 |
|
ret = poll(pfds, (nfds_t)nfds, timeout); |
| 475 |
|
#endif |
| 476 |
|
|
| 477 |
|
if (ret < 0) |
| 478 |
|
{ |
| 479 |
|
if (errno != EINTR) |
| 480 |
|
{ |
| 481 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 482 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 483 |
|
#else |
| 484 |
|
log_error("poll() error (%d)\n", errno); |
| 485 |
|
#endif |
| 486 |
|
break; |
| 487 |
|
} |
| 488 |
|
continue; |
| 489 |
|
} |
| 490 |
|
else if (ret == 0) // timeout |
| 491 |
{ |
{ |
| 492 |
log_error("epoll_wait() error (%d)\n", errno); |
out = KEY_TIMEOUT; |
| 493 |
break; |
break; |
| 494 |
} |
} |
| 495 |
continue; |
|
| 496 |
} |
for (int i = 0; i < nfds; i++) |
| 497 |
else if (nfds == 0) // timeout |
{ |
| 498 |
{ |
#ifdef HAVE_SYS_EPOLL_H |
| 499 |
out = KEY_TIMEOUT; |
if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR))) |
| 500 |
break; |
#else |
| 501 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR))) |
| 502 |
|
#endif |
| 503 |
|
{ |
| 504 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 505 |
|
log_debug("STDIN error events (%d)\n", events[i].events); |
| 506 |
|
#else |
| 507 |
|
log_debug("STDIN error events (%d)\n", pfds[i].revents); |
| 508 |
|
#endif |
| 509 |
|
loop = 0; |
| 510 |
|
break; |
| 511 |
|
} |
| 512 |
|
|
| 513 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 514 |
|
if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) |
| 515 |
|
#else |
| 516 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 517 |
|
#endif |
| 518 |
|
{ |
| 519 |
|
stdin_read_wait = 1; |
| 520 |
|
} |
| 521 |
|
} |
| 522 |
} |
} |
| 523 |
|
|
| 524 |
for (int i = 0; i < nfds; i++) |
if (stdin_read_wait) |
| 525 |
{ |
{ |
| 526 |
if (events[i].data.fd == STDIN_FILENO) |
while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error |
| 527 |
{ |
{ |
| 528 |
while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error |
if (SSH_v2) |
| 529 |
{ |
{ |
| 530 |
if (SSH_v2) |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
| 531 |
|
if (ret == SSH_ERROR) |
| 532 |
{ |
{ |
| 533 |
ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0); |
log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 534 |
if (ret == SSH_ERROR) |
loop = 0; |
| 535 |
{ |
break; |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else if (ret == SSH_EOF) |
|
|
{ |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else if (ret == 0) |
|
|
{ |
|
|
out = 0; |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 536 |
} |
} |
| 537 |
else |
else if (ret == SSH_EOF) |
| 538 |
{ |
{ |
| 539 |
ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); |
stdin_read_wait = 0; |
| 540 |
|
loop = 0; |
| 541 |
|
break; |
| 542 |
} |
} |
| 543 |
if (ret < 0) |
else if (ret == 0) |
| 544 |
{ |
{ |
| 545 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
out = 0; |
| 546 |
{ |
stdin_read_wait = 0; |
| 547 |
out = 0; |
loop = 0; |
| 548 |
loop = 0; |
break; |
|
break; |
|
|
} |
|
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
|
else |
|
|
{ |
|
|
#ifdef _DEBUG |
|
|
log_error("read(STDIN) error (%d)\n", errno); |
|
|
#endif |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 549 |
} |
} |
| 550 |
else if (ret == 0) // broken pipe |
} |
| 551 |
|
else |
| 552 |
|
{ |
| 553 |
|
ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len); |
| 554 |
|
} |
| 555 |
|
if (ret < 0) |
| 556 |
|
{ |
| 557 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 558 |
{ |
{ |
| 559 |
|
out = 0; |
| 560 |
|
stdin_read_wait = 0; |
| 561 |
loop = 0; |
loop = 0; |
| 562 |
break; |
break; |
| 563 |
} |
} |
| 564 |
else |
else if (errno == EINTR) |
| 565 |
{ |
{ |
|
len += ret; |
|
| 566 |
continue; |
continue; |
| 567 |
} |
} |
| 568 |
|
else |
| 569 |
|
{ |
| 570 |
|
log_debug("read(STDIN) error (%d)\n", errno); |
| 571 |
|
loop = 0; |
| 572 |
|
break; |
| 573 |
|
} |
| 574 |
|
} |
| 575 |
|
else if (ret == 0) // broken pipe |
| 576 |
|
{ |
| 577 |
|
stdin_read_wait = 0; |
| 578 |
|
loop = 0; |
| 579 |
|
break; |
| 580 |
|
} |
| 581 |
|
else |
| 582 |
|
{ |
| 583 |
|
stdin_buf_len += ret; |
| 584 |
|
continue; |
| 585 |
} |
} |
| 586 |
} |
} |
| 587 |
} |
} |
| 588 |
|
|
| 589 |
// For debug |
// For debug |
| 590 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 591 |
for (int j = pos; j < len; j++) |
for (int j = stdin_buf_offset; j < stdin_buf_len; j++) |
| 592 |
{ |
{ |
| 593 |
log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256); |
log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256); |
| 594 |
} |
} |
| 595 |
#endif |
#endif |
| 596 |
} |
} |
| 597 |
|
|
| 598 |
fcntl(STDIN_FILENO, F_SETFL, flags); |
if (stdin_buf_offset < stdin_buf_len) |
|
|
|
|
if (close(epollfd) < 0) |
|
| 599 |
{ |
{ |
| 600 |
log_error("close(epoll) error (%d)\n"); |
ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len); |
| 601 |
|
if (ret < 0) |
| 602 |
|
{ |
| 603 |
|
log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len); |
| 604 |
|
stdin_buf_len = stdin_buf_offset; // Discard invalid sequence |
| 605 |
|
} |
| 606 |
|
|
| 607 |
|
// For debug |
| 608 |
|
#ifdef _DEBUG |
| 609 |
|
for (int j = stdin_conv_offset; j < stdin_conv_len; j++) |
| 610 |
|
{ |
| 611 |
|
log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); |
| 612 |
|
} |
| 613 |
|
#endif |
| 614 |
} |
} |
| 615 |
} |
} |
| 616 |
|
|
| 617 |
while (pos < len) |
while (stdin_conv_offset < stdin_conv_len) |
| 618 |
{ |
{ |
| 619 |
unsigned char c = buf[pos++]; |
unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++]; |
| 620 |
|
|
| 621 |
// Convert \r\n to \r |
// Convert \r\n to \r |
| 622 |
if (c == CR && pos < len && buf[pos] == LF) |
if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF) |
| 623 |
{ |
{ |
| 624 |
pos++; |
stdin_conv_offset++; |
| 625 |
} |
} |
| 626 |
|
|
| 627 |
// Convert single \n to \r |
// Convert single \n to \r |
| 1074 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 1075 |
if (out != KEY_TIMEOUT && out != KEY_NULL) |
if (out != KEY_TIMEOUT && out != KEY_NULL) |
| 1076 |
{ |
{ |
| 1077 |
log_common("Debug: -->[0x %x]\n", out); |
log_debug("output: -->[0x %x]\n", out); |
| 1078 |
} |
} |
| 1079 |
#endif |
#endif |
| 1080 |
|
|
| 1110 |
size_t in_bytes; |
size_t in_bytes; |
| 1111 |
size_t out_bytes; |
size_t out_bytes; |
| 1112 |
int ret; |
int ret; |
| 1113 |
|
int in_control = 0; |
| 1114 |
|
size_t i = 0; |
| 1115 |
|
int skip_current = 0; |
| 1116 |
|
|
| 1117 |
|
if (cd == (iconv_t)(-1) || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) |
| 1118 |
|
{ |
| 1119 |
|
log_error("NULL pointer error\n"); |
| 1120 |
|
return -1; |
| 1121 |
|
} |
| 1122 |
|
|
| 1123 |
in_buf = p_buf + *p_buf_offset; |
in_buf = p_buf + *p_buf_offset; |
| 1124 |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 1127 |
|
|
| 1128 |
while (in_bytes > 0) |
while (in_bytes > 0) |
| 1129 |
{ |
{ |
| 1130 |
|
if ((unsigned char)(*in_buf) == KEY_CONTROL) |
| 1131 |
|
{ |
| 1132 |
|
if (in_control == 0) |
| 1133 |
|
{ |
| 1134 |
|
in_control = 1; |
| 1135 |
|
i = 0; |
| 1136 |
|
} |
| 1137 |
|
} |
| 1138 |
|
|
| 1139 |
|
if (in_control || skip_current) |
| 1140 |
|
{ |
| 1141 |
|
skip_current = 0; |
| 1142 |
|
|
| 1143 |
|
if (out_bytes <= 0) |
| 1144 |
|
{ |
| 1145 |
|
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); |
| 1146 |
|
return -2; |
| 1147 |
|
} |
| 1148 |
|
|
| 1149 |
|
*out_buf = *in_buf; |
| 1150 |
|
in_buf++; |
| 1151 |
|
out_buf++; |
| 1152 |
|
in_bytes--; |
| 1153 |
|
out_bytes--; |
| 1154 |
|
|
| 1155 |
|
(*p_buf_offset)++; |
| 1156 |
|
(*p_conv_len)++; |
| 1157 |
|
|
| 1158 |
|
i++; |
| 1159 |
|
if (i >= 2) |
| 1160 |
|
{ |
| 1161 |
|
in_control = 0; |
| 1162 |
|
} |
| 1163 |
|
continue; |
| 1164 |
|
} |
| 1165 |
|
|
| 1166 |
ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes); |
ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes); |
| 1167 |
if (ret == -1) |
if (ret == -1) |
| 1168 |
{ |
{ |
| 1169 |
if (errno == EINVAL) // Incomplete |
if (errno == EINVAL) // Incomplete |
| 1170 |
{ |
{ |
| 1171 |
#ifdef _DEBUG |
log_debug("iconv(inbytes=%zu, outbytes=%zu) error: EINVAL, in_buf[0]=%d\n", |
| 1172 |
log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes); |
in_bytes, out_bytes, (unsigned char)in_buf[0]); |
| 1173 |
#endif |
if (p_buf != in_buf) |
| 1174 |
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
{ |
| 1175 |
*p_buf_offset = 0; |
*p_buf_len -= (int)(in_buf - p_buf); |
| 1176 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_buf_offset = 0; |
| 1177 |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
*p_conv_len = (int)(conv_size - out_bytes); |
| 1178 |
|
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
| 1179 |
|
} |
| 1180 |
|
|
| 1181 |
break; |
break; |
| 1182 |
} |
} |
| 1183 |
else if (errno == E2BIG) |
else if (errno == E2BIG) |
| 1184 |
{ |
{ |
| 1185 |
log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); |
log_error("iconv(inbytes=%zu, outbytes=%zu) error: E2BIG\n", in_bytes, out_bytes); |
| 1186 |
return -1; |
return -1; |
| 1187 |
} |
} |
| 1188 |
else if (errno == EILSEQ) |
else if (errno == EILSEQ) |
| 1189 |
{ |
{ |
| 1190 |
if (in_bytes > out_bytes || out_bytes <= 0) |
if (in_bytes > out_bytes || out_bytes <= 0) |
| 1191 |
{ |
{ |
| 1192 |
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); |
log_error("iconv(inbytes=%zu, outbytes=%zu) error: EILSEQ\n", in_bytes, out_bytes); |
| 1193 |
return -2; |
return -2; |
| 1194 |
} |
} |
| 1195 |
|
|
| 1196 |
*out_buf = *in_buf; |
// reset in_bytes when "//IGNORE" is applied |
| 1197 |
in_buf++; |
if (in_bytes == 0) |
| 1198 |
out_buf++; |
{ |
| 1199 |
in_bytes--; |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 1200 |
out_bytes--; |
log_debug("Reset in_bytes from 0 to %zu\n", in_bytes); |
| 1201 |
|
} |
| 1202 |
|
|
| 1203 |
continue; |
log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: EILSEQ, in_buf[0]=%d\n", |
| 1204 |
|
in_bytes, out_bytes, (unsigned char)in_buf[0]); |
| 1205 |
|
skip_current = 1; |
| 1206 |
|
} |
| 1207 |
|
else // something strange |
| 1208 |
|
{ |
| 1209 |
|
log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: %d, in_buf[0]=%d\n", |
| 1210 |
|
in_bytes, out_bytes, errno, (unsigned char)in_buf[0]); |
| 1211 |
|
*p_buf_offset = (int)(in_buf - p_buf); |
| 1212 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 1213 |
|
skip_current = 1; |
| 1214 |
} |
} |
| 1215 |
} |
} |
| 1216 |
else |
else |
| 1217 |
{ |
{ |
| 1218 |
*p_buf_len = 0; |
*p_buf_offset = (int)(in_buf - p_buf); |
|
*p_buf_offset = 0; |
|
| 1219 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
|
|
|
|
break; |
|
| 1220 |
} |
} |
| 1221 |
} |
} |
| 1222 |
|
|
| 1223 |
|
if (*p_buf_offset >= *p_buf_len) |
| 1224 |
|
{ |
| 1225 |
|
*p_buf_len = 0; |
| 1226 |
|
*p_buf_offset = 0; |
| 1227 |
|
} |
| 1228 |
|
|
| 1229 |
|
return 0; |
| 1230 |
|
} |
| 1231 |
|
|
| 1232 |
|
int io_conv_init(const char *charset) |
| 1233 |
|
{ |
| 1234 |
|
char tocode[CHARSET_MAX_LEN + 20]; |
| 1235 |
|
|
| 1236 |
|
if (charset == NULL) |
| 1237 |
|
{ |
| 1238 |
|
log_error("NULL pointer error\n"); |
| 1239 |
|
return -1; |
| 1240 |
|
} |
| 1241 |
|
|
| 1242 |
|
io_conv_cleanup(); |
| 1243 |
|
|
| 1244 |
|
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
| 1245 |
|
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
| 1246 |
|
|
| 1247 |
|
snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset, |
| 1248 |
|
(strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE")); |
| 1249 |
|
stdin_cd = iconv_open(tocode, stdio_charset); |
| 1250 |
|
if (stdin_cd == (iconv_t)(-1)) |
| 1251 |
|
{ |
| 1252 |
|
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
| 1253 |
|
return -2; |
| 1254 |
|
} |
| 1255 |
|
|
| 1256 |
|
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 1257 |
|
(strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 1258 |
|
stdout_cd = iconv_open(tocode, BBS_default_charset); |
| 1259 |
|
if (stdout_cd == (iconv_t)(-1)) |
| 1260 |
|
{ |
| 1261 |
|
log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno); |
| 1262 |
|
iconv_close(stdin_cd); |
| 1263 |
|
stdin_cd = (iconv_t)(-1); |
| 1264 |
|
return -2; |
| 1265 |
|
} |
| 1266 |
|
|
| 1267 |
|
return 0; |
| 1268 |
|
} |
| 1269 |
|
|
| 1270 |
|
int io_conv_cleanup(void) |
| 1271 |
|
{ |
| 1272 |
|
if (stdin_cd != (iconv_t)(-1)) |
| 1273 |
|
{ |
| 1274 |
|
iconv_close(stdin_cd); |
| 1275 |
|
stdin_cd = (iconv_t)(-1); |
| 1276 |
|
} |
| 1277 |
|
if (stdout_cd != (iconv_t)(-1)) |
| 1278 |
|
{ |
| 1279 |
|
iconv_close(stdout_cd); |
| 1280 |
|
stdout_cd = (iconv_t)(-1); |
| 1281 |
|
} |
| 1282 |
|
|
| 1283 |
return 0; |
return 0; |
| 1284 |
} |
} |