/[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.70 by sysadm, Sun Nov 23 01:47:47 2025 UTC Revision 1.77 by sysadm, Thu Dec 18 11:18:29 2025 UTC
# Line 64  static int stdout_conv_len = 0; Line 64  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  {  {
# Line 88  int io_init(void) Line 88  int io_init(void)
88                          log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                          log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
89                          if (close(stdin_epollfd) < 0)                          if (close(stdin_epollfd) < 0)
90                          {                          {
91                                  log_error("close(stdin_epollfd) error (%d)\n");                                  log_error("close(stdin_epollfd) error (%d)\n", errno);
92                          }                          }
93                          stdin_epollfd = -1;                          stdin_epollfd = -1;
94                          return -1;                          return -1;
95                  }                  }
96    
97                  stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
98                  fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);                  {
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)          if (stdout_epollfd == -1)
# Line 120  int io_init(void) Line 138  int io_init(void)
138                          return -1;                          return -1;
139                  }                  }
140    
141                  stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);                  if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
142                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);                  {
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  #else
163          if (stdin_flags == 0)          if (stdin_flags == 0)
164          {          {
165                  stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
166                  fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);                  {
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)          if (stdout_flags == 0)
178          {          {
179                  stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);                  if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
180                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);                  {
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  #endif
191    
# Line 150  void io_cleanup(void) Line 202  void io_cleanup(void)
202    
203                  if (close(stdin_epollfd) < 0)                  if (close(stdin_epollfd) < 0)
204                  {                  {
205                          log_error("close(stdin_epollfd) error (%d)\n");                          log_error("close(stdin_epollfd) error (%d)\n", errno);
206                  }                  }
207                  stdin_epollfd = -1;                  stdin_epollfd = -1;
208          }          }
# Line 193  int prints(const char *format, ...) Line 245  int prints(const char *format, ...)
245    
246          if (ret > 0)          if (ret > 0)
247          {          {
248                  if (stdout_buf_len + ret > OUTPUT_BUF_SIZE)                  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 <= OUTPUT_BUF_SIZE)                  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                  }                  }
260                  else                  else
261                  {                  {
262                          errno = EAGAIN;                          errno = EAGAIN;
263                          ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret);                          int need = stdout_buf_len + ret - OUTPUT_BUF_SIZE;
264                          log_error("Output buffer is full, additional %d is required\n", ret);                          log_error("Output buffer is full, additional %d is required\n", need);
265                  }                  }
266          }          }
267    
# Line 216  int prints(const char *format, ...) Line 270  int prints(const char *format, ...)
270    
271  int outc(char c)  int outc(char c)
272  {  {
273          int ret;          int ret = 0;
274    
275          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)
276          {          {
# Line 246  int iflush(void) Line 300  int iflush(void)
300  #endif  #endif
301    
302          int nfds;          int nfds;
         int retry;  
303          int ret = 0;          int ret = 0;
304    
305          // Retry wait / flush for at most 3 times          // Retry wait / flush for at most 3 times
306          retry = 3;          for (int retry = 3; retry > 0 && !SYS_server_exit; retry--)
         while (retry > 0 && !SYS_server_exit)  
307          {          {
                 retry--;  
   
308  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
309                  nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
310                  ret = nfds;                  ret = nfds;
# Line 286  int iflush(void) Line 336  int iflush(void)
336                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
337                  {                  {
338  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
339                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
340    #else
341                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
342    #endif
343                            {
344    #ifdef HAVE_SYS_EPOLL_H
345                                    log_debug("STDOUT error events (%d)\n", events[i].events);
346    #else
347                                    log_debug("STDOUT error events (%d)\n", pfds[i].revents);
348    #endif
349                                    retry = 0;
350                                    break;
351                            }
352    
353    #ifdef HAVE_SYS_EPOLL_H
354                            if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
355  #else  #else
356                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
357  #endif  #endif
# Line 308  int iflush(void) Line 373  int iflush(void)
373                                                  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));
374                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
375                                                  {                                                  {
376                                                          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));
377                                                          retry = 0;                                                          retry = 0;
378                                                          break;                                                          break;
379                                                  }                                                  }
# Line 329  int iflush(void) Line 394  int iflush(void)
394                                                  }                                                  }
395                                                  else                                                  else
396                                                  {                                                  {
397  #ifdef _DEBUG                                                          log_debug("write(STDOUT) error (%d)\n", errno);
                                                         log_error("write(STDOUT) error (%d)\n", errno);  
 #endif  
398                                                          retry = 0;                                                          retry = 0;
399                                                          break;                                                          break;
400                                                  }                                                  }
# Line 392  int igetch(int timeout) Line 455  int igetch(int timeout)
455                  {                  {
456                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
457                          {                          {
458                                  log_error("SSH channel is closed\n");                                  log_debug("SSH channel is closed\n");
459                                  loop = 0;                                  loop = 0;
460                                  break;                                  break;
461                          }                          }
# Line 431  int igetch(int timeout) Line 494  int igetch(int timeout)
494                                  for (int i = 0; i < nfds; i++)                                  for (int i = 0; i < nfds; i++)
495                                  {                                  {
496  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
497                                          if (events[i].data.fd == STDIN_FILENO)                                          if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
498    #else
499                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
500    #endif
501                                            {
502    #ifdef HAVE_SYS_EPOLL_H
503                                                    log_debug("STDIN error events (%d)\n", events[i].events);
504    #else
505                                                    log_debug("STDIN error events (%d)\n", pfds[i].revents);
506    #endif
507                                                    loop = 0;
508                                                    break;
509                                            }
510    
511    #ifdef HAVE_SYS_EPOLL_H
512                                            if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
513  #else  #else
514                                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))                                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
515  #endif  #endif
# Line 450  int igetch(int timeout) Line 528  int igetch(int timeout)
528                                                  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);
529                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
530                                                  {                                                  {
531                                                          log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));                                                          log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
532                                                          loop = 0;                                                          loop = 0;
533                                                          break;                                                          break;
534                                                  }                                                  }
# Line 487  int igetch(int timeout) Line 565  int igetch(int timeout)
565                                                  }                                                  }
566                                                  else                                                  else
567                                                  {                                                  {
568  #ifdef _DEBUG                                                          log_debug("read(STDIN) error (%d)\n", errno);
                                                         log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
569                                                          loop = 0;                                                          loop = 0;
570                                                          break;                                                          break;
571                                                  }                                                  }
# Line 512  int igetch(int timeout) Line 588  int igetch(int timeout)
588  #ifdef _DEBUG  #ifdef _DEBUG
589                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
590                          {                          {
591                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);                                  log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
592                          }                          }
593  #endif  #endif
594                  }                  }
# Line 530  int igetch(int timeout) Line 606  int igetch(int timeout)
606  #ifdef _DEBUG  #ifdef _DEBUG
607                          for (int j = stdin_conv_offset; j < stdin_conv_len; j++)                          for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
608                          {                          {
609                                  log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);                                  log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
610                          }                          }
611  #endif  #endif
612                  }                  }
# Line 996  int igetch(int timeout) Line 1072  int igetch(int timeout)
1072  #ifdef _DEBUG  #ifdef _DEBUG
1073          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
1074          {          {
1075                  log_error("Debug: -->[0x %x]\n", out);                  log_debug("output: -->[0x %x]\n", out);
1076          }          }
1077  #endif  #endif
1078    
# Line 1036  int io_buf_conv(iconv_t cd, char *p_buf, Line 1112  int io_buf_conv(iconv_t cd, char *p_buf,
1112          size_t i = 0;          size_t i = 0;
1113          int skip_current = 0;          int skip_current = 0;
1114    
1115          if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)          if (cd == (iconv_t)(-1) || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
1116          {          {
1117                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
1118                  return -1;                  return -1;
# Line 1090  int io_buf_conv(iconv_t cd, char *p_buf, Line 1166  int io_buf_conv(iconv_t cd, char *p_buf,
1166                  {                  {
1167                          if (errno == EINVAL) // Incomplete                          if (errno == EINVAL) // Incomplete
1168                          {                          {
1169  #ifdef _DEBUG                                  log_debug("iconv(inbytes=%ld, outbytes=%ld) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
                                 log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);  
 #endif  
1170                                  if (p_buf != in_buf)                                  if (p_buf != in_buf)
1171                                  {                                  {
1172                                          *p_buf_len -= (int)(in_buf - p_buf);                                          *p_buf_len -= (int)(in_buf - p_buf);
# Line 1105  int io_buf_conv(iconv_t cd, char *p_buf, Line 1179  int io_buf_conv(iconv_t cd, char *p_buf,
1179                          }                          }
1180                          else if (errno == E2BIG)                          else if (errno == E2BIG)
1181                          {                          {
1182                                  log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);                                  log_error("iconv(inbytes=%ld, outbytes=%ld) error: E2BIG\n", in_bytes, out_bytes);
1183                                  return -1;                                  return -1;
1184                          }                          }
1185                          else if (errno == EILSEQ)                          else if (errno == EILSEQ)
1186                          {                          {
1187                                  if (in_bytes > out_bytes || out_bytes <= 0)                                  if (in_bytes > out_bytes || out_bytes <= 0)
1188                                  {                                  {
1189                                          log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);                                          log_error("iconv(inbytes=%ld, outbytes=%ld) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
1190                                          return -2;                                          return -2;
1191                                  }                                  }
1192    
# Line 1120  int io_buf_conv(iconv_t cd, char *p_buf, Line 1194  int io_buf_conv(iconv_t cd, char *p_buf,
1194                                  if (in_bytes == 0)                                  if (in_bytes == 0)
1195                                  {                                  {
1196                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1197                                            log_debug("Reset in_bytes from 0 to %ld\n", in_bytes);
1198                                  }                                  }
1199    
1200                                  *out_buf = *in_buf;                                  log_debug("iconv(in_bytes=%ld, out_bytes=%ld) error: EILSEQ, in_buf[0]=%d\n",
1201                                  in_buf++;                                                    in_bytes, out_bytes, in_buf[0]);
1202                                  out_buf++;                                  skip_current = 1;
                                 in_bytes--;  
                                 out_bytes--;  
   
                                 (*p_buf_offset)++;  
                                 (*p_conv_len)++;  
   
                                 continue;  
1203                          }                          }
1204                          else // something strange                          else // something strange
1205                          {                          {
1206  #ifdef _DEBUG                                  log_debug("iconv(in_bytes=%ld, out_bytes=%ld) error: %d, in_buf[0]=%d\n",
                                 log_error("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n",  
1207                                                    in_bytes, out_bytes, errno, in_buf[0]);                                                    in_bytes, out_bytes, errno, in_buf[0]);
 #endif  
   
1208                                  *p_buf_offset = (int)(in_buf - p_buf);                                  *p_buf_offset = (int)(in_buf - p_buf);
1209                                  *p_conv_len = (int)(conv_size - out_bytes);                                  *p_conv_len = (int)(conv_size - out_bytes);
1210                                  skip_current = 1;                                  skip_current = 1;
   
                                 continue;  
1211                          }                          }
1212                  }                  }
1213                  else                  else
1214                  {                  {
1215                          *p_buf_len = 0;                          *p_buf_offset = (int)(in_buf - p_buf);
                         *p_buf_offset = 0;  
1216                          *p_conv_len = (int)(conv_size - out_bytes);                          *p_conv_len = (int)(conv_size - out_bytes);
   
                         break;  
1217                  }                  }
1218          }          }
1219    
# Line 1197  int io_conv_init(const char *charset) Line 1257  int io_conv_init(const char *charset)
1257          {          {
1258                  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);
1259                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1260                    stdin_cd = (iconv_t)(-1);
1261                  return -2;                  return -2;
1262          }          }
1263    
# Line 1205  int io_conv_init(const char *charset) Line 1266  int io_conv_init(const char *charset)
1266    
1267  int io_conv_cleanup(void)  int io_conv_cleanup(void)
1268  {  {
1269          if (stdin_cd != NULL)          if (stdin_cd != (iconv_t)(-1))
1270          {          {
1271                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1272                  stdin_cd = NULL;                  stdin_cd = (iconv_t)(-1);
1273          }          }
1274          if (stdout_cd != NULL)          if (stdout_cd != (iconv_t)(-1))
1275          {          {
1276                  iconv_close(stdout_cd);                  iconv_close(stdout_cd);
1277                  stdout_cd = NULL;                  stdout_cd = (iconv_t)(-1);
1278          }          }
1279    
1280          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