/[LeafOK_CVS]/lbbs/src/io.c
ViewVC logotype

Diff of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.66 by sysadm, Mon Nov 17 02:32:42 2025 UTC Revision 1.73 by sysadm, Wed Dec 17 03:56:39 2025 UTC
# Line 20  Line 20 
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    #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";  const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
41  char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";  char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
42    
43    #ifdef HAVE_SYS_EPOLL_H
44  // epoll for STDIO  // epoll for STDIO
45  static int stdin_epollfd = -1;  static int stdin_epollfd = -1;
46  static int stdout_epollfd = -1;  static int stdout_epollfd = -1;
47  static int stdin_flags;  #endif
48  static int stdout_flags;  
49    static int stdin_flags = 0;
50    static int stdout_flags = 0;
51    
52  // static input / output buffer  // static input / output buffer
53  static char stdin_buf[LINE_BUFFER_LEN];  static char stdin_buf[LINE_BUFFER_LEN];
54  static char stdout_buf[BUFSIZ];  static char stdout_buf[OUTPUT_BUF_SIZE];
55  static int stdin_buf_len = 0;  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;  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];  static char stdin_conv[LINE_BUFFER_LEN * 2];
61  static char stdout_conv[BUFSIZ * 2];  static char stdout_conv[OUTPUT_BUF_SIZE * 2];
62  static int stdin_conv_len = 0;  static int stdin_conv_len = 0;
63  static int stdout_conv_len = 0;  static int stdout_conv_len = 0;
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  {  {
72    #ifdef HAVE_SYS_EPOLL_H
73          struct epoll_event ev;          struct epoll_event ev;
74    
75          if (stdin_epollfd == -1)          if (stdin_epollfd == -1)
# Line 106  int io_init(void) Line 120  int io_init(void)
120                          return -1;                          return -1;
121                  }                  }
122    
                 // Set STDOUT as non-blocking  
123                  stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);                  stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
124                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);
125          }          }
126    #else
127            if (stdin_flags == 0)
128            {
129                    stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
130                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
131            }
132    
133            if (stdout_flags == 0)
134            {
135                    stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
136                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);
137            }
138    #endif
139    
140          return 0;          return 0;
141  }  }
142    
143  void io_cleanup(void)  void io_cleanup(void)
144  {  {
145    #ifdef HAVE_SYS_EPOLL_H
146          if (stdin_epollfd != -1)          if (stdin_epollfd != -1)
147          {          {
148                  fcntl(STDIN_FILENO, F_SETFL, stdin_flags);                  fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
149                    stdin_flags = 0;
150    
151                  if (close(stdin_epollfd) < 0)                  if (close(stdin_epollfd) < 0)
152                  {                  {
# Line 130  void io_cleanup(void) Line 158  void io_cleanup(void)
158          if (stdout_epollfd != -1)          if (stdout_epollfd != -1)
159          {          {
160                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
161                    stdout_flags = 0;
162    
163                  if (close(stdout_epollfd) < 0)                  if (close(stdout_epollfd) < 0)
164                  {                  {
# Line 137  void io_cleanup(void) Line 166  void io_cleanup(void)
166                  }                  }
167                  stdout_epollfd = -1;                  stdout_epollfd = -1;
168          }          }
169    #else
170            if (stdin_flags != 0)
171            {
172                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
173                    stdin_flags = 0;
174            }
175    
176            if (stdout_flags != 0)
177            {
178                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
179                    stdout_flags = 0;
180            }
181    #endif
182  }  }
183    
184  int prints(const char *format, ...)  int prints(const char *format, ...)
185  {  {
186          char buf[BUFSIZ];          char buf[OUTPUT_BUF_SIZE];
187          va_list args;          va_list args;
188          int ret;          int ret;
189    
# Line 151  int prints(const char *format, ...) Line 193  int prints(const char *format, ...)
193    
194          if (ret > 0)          if (ret > 0)
195          {          {
196                  if (stdout_buf_len + ret > BUFSIZ)                  if (stdout_buf_len + ret > OUTPUT_BUF_SIZE)
197                  {                  {
198                          iflush();                          iflush();
199                  }                  }
200    
201                  if (stdout_buf_len + ret <= BUFSIZ)                  if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE)
202                  {                  {
203                          memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret);                          memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret);
204                          stdout_buf_len += ret;                          stdout_buf_len += ret;
# Line 164  int prints(const char *format, ...) Line 206  int prints(const char *format, ...)
206                  else                  else
207                  {                  {
208                          errno = EAGAIN;                          errno = EAGAIN;
209                          ret = (BUFSIZ - stdout_buf_len - ret);                          ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret);
210                          log_error("Output buffer is full, additional %d is required\n", ret);                          log_error("Output buffer is full, additional %d is required\n", ret);
211                  }                  }
212          }          }
# Line 176  int outc(char c) Line 218  int outc(char c)
218  {  {
219          int ret;          int ret;
220    
221          if (stdout_buf_len + 1 > BUFSIZ)          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)
222          {          {
223                  iflush();                  iflush();
224          }          }
225    
226          if (stdout_buf_len + 1 <= BUFSIZ)          if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE)
227          {          {
228                  stdout_buf[stdout_buf_len] = c;                  stdout_buf[stdout_buf_len] = c;
229                  stdout_buf_len++;                  stdout_buf_len++;
# Line 197  int outc(char c) Line 239  int outc(char c)
239    
240  int iflush(void)  int iflush(void)
241  {  {
242    #ifdef HAVE_SYS_EPOLL_H
243          struct epoll_event events[MAX_EVENTS];          struct epoll_event events[MAX_EVENTS];
244    #else
245            struct pollfd pfds[1];
246    #endif
247    
248          int nfds;          int nfds;
249          int retry;          int retry;
250          int ret = 0;          int ret = 0;
# Line 208  int iflush(void) Line 255  int iflush(void)
255          {          {
256                  retry--;                  retry--;
257    
258    #ifdef HAVE_SYS_EPOLL_H
259                  nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
260                    ret = nfds;
261    #else
262                    pfds[0].fd = STDOUT_FILENO;
263                    pfds[0].events = POLLOUT;
264                    nfds = 1;
265                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
266    #endif
267    
268                  if (nfds < 0)                  if (ret < 0)
269                  {                  {
270                          if (errno != EINTR)                          if (errno != EINTR)
271                          {                          {
272    #ifdef HAVE_SYS_EPOLL_H
273                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
274    #else
275                                    log_error("poll() error (%d)\n", errno);
276    #endif
277                                  break;                                  break;
278                          }                          }
279                          continue;                          continue;
280                  }                  }
281                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
282                  {                  {
283                          continue;                          continue;
284                  }                  }
285    
286                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
287                  {                  {
288    #ifdef HAVE_SYS_EPOLL_H
289                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
290    #else
291                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
292    #endif
293                          {                          {
294                                  if (stdout_buf_offset < stdout_buf_len)                                  if (stdout_buf_offset < stdout_buf_len)
295                                  {                                  {
# Line 245  int iflush(void) Line 308  int iflush(void)
308                                                  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));
309                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
310                                                  {                                                  {
311    #ifdef _DEBUG
312                                                          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));
313    #endif
314                                                          retry = 0;                                                          retry = 0;
315                                                          break;                                                          break;
316                                                  }                                                  }
# Line 303  int igetch(int timeout) Line 368  int igetch(int timeout)
368  {  {
369          static int stdin_read_wait = 0;          static int stdin_read_wait = 0;
370    
371    #ifdef HAVE_SYS_EPOLL_H
372          struct epoll_event events[MAX_EVENTS];          struct epoll_event events[MAX_EVENTS];
373    #else
374            struct pollfd pfds[1];
375    #endif
376    
377          int nfds;          int nfds;
378          int ret;          int ret;
379          int loop;          int loop;
# Line 324  int igetch(int timeout) Line 394  int igetch(int timeout)
394                  {                  {
395                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
396                          {                          {
397    #ifdef _DEBUG
398                                  log_error("SSH channel is closed\n");                                  log_error("SSH channel is closed\n");
399    #endif
400                                  loop = 0;                                  loop = 0;
401                                  break;                                  break;
402                          }                          }
403    
404                          if (!stdin_read_wait)                          if (!stdin_read_wait)
405                          {                          {
406    #ifdef HAVE_SYS_EPOLL_H
407                                  nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);                                  nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
408                                    ret = nfds;
409    #else
410                                    pfds[0].fd = STDIN_FILENO;
411                                    pfds[0].events = POLLIN;
412                                    nfds = 1;
413                                    ret = poll(pfds, (nfds_t)nfds, timeout);
414    #endif
415    
416                                  if (nfds < 0)                                  if (ret < 0)
417                                  {                                  {
418                                          if (errno != EINTR)                                          if (errno != EINTR)
419                                          {                                          {
420    #ifdef HAVE_SYS_EPOLL_H
421                                                  log_error("epoll_wait() error (%d)\n", errno);                                                  log_error("epoll_wait() error (%d)\n", errno);
422    #else
423                                                    log_error("poll() error (%d)\n", errno);
424    #endif
425                                                  break;                                                  break;
426                                          }                                          }
427                                          continue;                                          continue;
428                                  }                                  }
429                                  else if (nfds == 0) // timeout                                  else if (ret == 0) // timeout
430                                  {                                  {
431                                          out = KEY_TIMEOUT;                                          out = KEY_TIMEOUT;
432                                          break;                                          break;
# Line 350  int igetch(int timeout) Line 434  int igetch(int timeout)
434    
435                                  for (int i = 0; i < nfds; i++)                                  for (int i = 0; i < nfds; i++)
436                                  {                                  {
437    #ifdef HAVE_SYS_EPOLL_H
438                                          if (events[i].data.fd == STDIN_FILENO)                                          if (events[i].data.fd == STDIN_FILENO)
439    #else
440                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
441    #endif
442                                          {                                          {
443                                                  stdin_read_wait = 1;                                                  stdin_read_wait = 1;
444                                          }                                          }
# Line 366  int igetch(int timeout) Line 454  int igetch(int timeout)
454                                                  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);
455                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
456                                                  {                                                  {
457    #ifdef _DEBUG
458                                                          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));
459    #endif
460                                                          loop = 0;                                                          loop = 0;
461                                                          break;                                                          break;
462                                                  }                                                  }
# Line 950  int io_buf_conv(iconv_t cd, char *p_buf, Line 1040  int io_buf_conv(iconv_t cd, char *p_buf,
1040          int ret;          int ret;
1041          int in_control = 0;          int in_control = 0;
1042          size_t i = 0;          size_t i = 0;
1043            int skip_current = 0;
1044    
1045          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)
1046          {          {
# Line 973  int io_buf_conv(iconv_t cd, char *p_buf, Line 1064  int io_buf_conv(iconv_t cd, char *p_buf,
1064                          }                          }
1065                  }                  }
1066    
1067                  if (in_control)                  if (in_control || skip_current)
1068                  {                  {
1069                            skip_current = 0;
1070    
1071                          if (out_bytes <= 0)                          if (out_bytes <= 0)
1072                          {                          {
1073                                  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);
# Line 988  int io_buf_conv(iconv_t cd, char *p_buf, Line 1081  int io_buf_conv(iconv_t cd, char *p_buf,
1081                          out_bytes--;                          out_bytes--;
1082    
1083                          (*p_buf_offset)++;                          (*p_buf_offset)++;
1084                          *p_conv_len = (int)(conv_size - out_bytes);                          (*p_conv_len)++;
1085    
1086                          i++;                          i++;
1087                          if (i >= 2)                          if (i >= 2)
# Line 1008  int io_buf_conv(iconv_t cd, char *p_buf, Line 1101  int io_buf_conv(iconv_t cd, char *p_buf,
1101  #endif  #endif
1102                                  if (p_buf != in_buf)                                  if (p_buf != in_buf)
1103                                  {                                  {
1104                                          *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);                                          *p_buf_len -= (int)(in_buf - p_buf);
1105                                          *p_buf_offset = 0;                                          *p_buf_offset = 0;
1106                                          *p_conv_len = (int)(conv_size - out_bytes);                                          *p_conv_len = (int)(conv_size - out_bytes);
1107                                          memmove(p_buf, in_buf, (size_t)(*p_buf_len));                                          memmove(p_buf, in_buf, (size_t)(*p_buf_len));
# Line 1033  int io_buf_conv(iconv_t cd, char *p_buf, Line 1126  int io_buf_conv(iconv_t cd, char *p_buf,
1126                                  if (in_bytes == 0)                                  if (in_bytes == 0)
1127                                  {                                  {
1128                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1129    #ifdef _DEBUG
1130                                            log_error("Reset in_bytes from 0 to %d\n", in_bytes);
1131    #endif
1132                                  }                                  }
1133    
1134                                  *out_buf = *in_buf;  #ifdef _DEBUG
1135                                  in_buf++;                                  log_error("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n",
1136                                  out_buf++;                                                    in_bytes, out_bytes, in_buf[0]);
1137                                  in_bytes--;  #endif
1138                                  out_bytes--;                                  skip_current = 1;
1139                            }
1140                                  continue;                          else // something strange
1141                            {
1142    #ifdef _DEBUG
1143                                    log_error("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n",
1144                                                      in_bytes, out_bytes, errno, in_buf[0]);
1145    #endif
1146                                    *p_buf_offset = (int)(in_buf - p_buf);
1147                                    *p_conv_len = (int)(conv_size - out_bytes);
1148                                    skip_current = 1;
1149                          }                          }
1150                  }                  }
1151                  else                  else
1152                  {                  {
1153                          *p_buf_len = 0;                          *p_buf_offset = (int)(in_buf - p_buf);
                         *p_buf_offset = 0;  
1154                          *p_conv_len = (int)(conv_size - out_bytes);                          *p_conv_len = (int)(conv_size - out_bytes);
   
                         break;  
1155                  }                  }
1156          }          }
1157    
1158            if (*p_buf_offset >= *p_buf_len)
1159            {
1160                    *p_buf_len = 0;
1161                    *p_buf_offset = 0;
1162            }
1163    
1164          return 0;          return 0;
1165  }  }
1166    
# Line 1088  int io_conv_init(const char *charset) Line 1195  int io_conv_init(const char *charset)
1195          {          {
1196                  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);
1197                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1198                    stdin_cd = (iconv_t)(-1);
1199                  return -2;                  return -2;
1200          }          }
1201    
# Line 1096  int io_conv_init(const char *charset) Line 1204  int io_conv_init(const char *charset)
1204    
1205  int io_conv_cleanup(void)  int io_conv_cleanup(void)
1206  {  {
1207          if (stdin_cd != NULL)          if (stdin_cd != (iconv_t)(-1))
1208          {          {
1209                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1210                  stdin_cd = NULL;                  stdin_cd = (iconv_t)(-1);
1211          }          }
1212          if (stdout_cd != NULL)          if (stdout_cd != (iconv_t)(-1))
1213          {          {
1214                  iconv_close(stdout_cd);                  iconv_close(stdout_cd);
1215                  stdout_cd = NULL;                  stdout_cd = (iconv_t)(-1);
1216          }          }
1217    
1218          return 0;          return 0;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1