/[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.67 by sysadm, Mon Nov 17 06:41:18 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  const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";  const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
36  char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";  char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
37    
38    #ifdef HAVE_SYS_EPOLL_H
39  // epoll for STDIO  // epoll for STDIO
40  static int stdin_epollfd = -1;  static int stdin_epollfd = -1;
41  static int stdout_epollfd = -1;  static int stdout_epollfd = -1;
42  static int stdin_flags;  #endif
43  static int stdout_flags;  
44    static int stdin_flags = 0;
45    static int stdout_flags = 0;
46    
47  // static input / output buffer  // static input / output buffer
48  static char stdin_buf[LINE_BUFFER_LEN];  static char stdin_buf[LINE_BUFFER_LEN];
# Line 56  static iconv_t stdout_cd = NULL; Line 64  static iconv_t stdout_cd = NULL;
64    
65  int io_init(void)  int io_init(void)
66  {  {
67    #ifdef HAVE_SYS_EPOLL_H
68          struct epoll_event ev;          struct epoll_event ev;
69    
70          if (stdin_epollfd == -1)          if (stdin_epollfd == -1)
# Line 106  int io_init(void) Line 115  int io_init(void)
115                          return -1;                          return -1;
116                  }                  }
117    
                 // Set STDOUT as non-blocking  
118                  stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);                  stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
119                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);
120          }          }
121    #else
122            if (stdin_flags == 0)
123            {
124                    stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
125                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
126            }
127    
128            if (stdout_flags == 0)
129            {
130                    stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
131                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);
132            }
133    #endif
134    
135          return 0;          return 0;
136  }  }
137    
138  void io_cleanup(void)  void io_cleanup(void)
139  {  {
140    #ifdef HAVE_SYS_EPOLL_H
141          if (stdin_epollfd != -1)          if (stdin_epollfd != -1)
142          {          {
143                  fcntl(STDIN_FILENO, F_SETFL, stdin_flags);                  fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
144                    stdin_flags = 0;
145    
146                  if (close(stdin_epollfd) < 0)                  if (close(stdin_epollfd) < 0)
147                  {                  {
# Line 130  void io_cleanup(void) Line 153  void io_cleanup(void)
153          if (stdout_epollfd != -1)          if (stdout_epollfd != -1)
154          {          {
155                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
156                    stdout_flags = 0;
157    
158                  if (close(stdout_epollfd) < 0)                  if (close(stdout_epollfd) < 0)
159                  {                  {
# Line 137  void io_cleanup(void) Line 161  void io_cleanup(void)
161                  }                  }
162                  stdout_epollfd = -1;                  stdout_epollfd = -1;
163          }          }
164    #else
165            if (stdin_flags != 0)
166            {
167                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
168                    stdin_flags = 0;
169            }
170    
171            if (stdout_flags != 0)
172            {
173                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
174                    stdout_flags = 0;
175            }
176    #endif
177  }  }
178    
179  int prints(const char *format, ...)  int prints(const char *format, ...)
# Line 197  int outc(char c) Line 234  int outc(char c)
234    
235  int iflush(void)  int iflush(void)
236  {  {
237    #ifdef HAVE_SYS_EPOLL_H
238          struct epoll_event events[MAX_EVENTS];          struct epoll_event events[MAX_EVENTS];
239    #else
240            struct pollfd pfds[1];
241    #endif
242    
243          int nfds;          int nfds;
244          int retry;          int retry;
245          int ret = 0;          int ret = 0;
# Line 208  int iflush(void) Line 250  int iflush(void)
250          {          {
251                  retry--;                  retry--;
252    
253    #ifdef HAVE_SYS_EPOLL_H
254                  nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
255                    ret = nfds;
256    #else
257                    pfds[0].fd = STDOUT_FILENO;
258                    pfds[0].events = POLLOUT;
259                    nfds = 1;
260                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
261    #endif
262    
263                  if (nfds < 0)                  if (ret < 0)
264                  {                  {
265                          if (errno != EINTR)                          if (errno != EINTR)
266                          {                          {
267    #ifdef HAVE_SYS_EPOLL_H
268                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
269    #else
270                                    log_error("poll() error (%d)\n", errno);
271    #endif
272                                  break;                                  break;
273                          }                          }
274                          continue;                          continue;
275                  }                  }
276                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
277                  {                  {
278                          continue;                          continue;
279                  }                  }
280    
281                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
282                  {                  {
283    #ifdef HAVE_SYS_EPOLL_H
284                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
285    #else
286                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
287    #endif
288                          {                          {
289                                  if (stdout_buf_offset < stdout_buf_len)                                  if (stdout_buf_offset < stdout_buf_len)
290                                  {                                  {
# Line 303  int igetch(int timeout) Line 361  int igetch(int timeout)
361  {  {
362          static int stdin_read_wait = 0;          static int stdin_read_wait = 0;
363    
364    #ifdef HAVE_SYS_EPOLL_H
365          struct epoll_event events[MAX_EVENTS];          struct epoll_event events[MAX_EVENTS];
366    #else
367            struct pollfd pfds[1];
368    #endif
369    
370          int nfds;          int nfds;
371          int ret;          int ret;
372          int loop;          int loop;
# Line 331  int igetch(int timeout) Line 394  int igetch(int timeout)
394    
395                          if (!stdin_read_wait)                          if (!stdin_read_wait)
396                          {                          {
397    #ifdef HAVE_SYS_EPOLL_H
398                                  nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);                                  nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
399                                    ret = nfds;
400    #else
401                                    pfds[0].fd = STDIN_FILENO;
402                                    pfds[0].events = POLLIN;
403                                    nfds = 1;
404                                    ret = poll(pfds, (nfds_t)nfds, timeout);
405    #endif
406    
407                                  if (nfds < 0)                                  if (ret < 0)
408                                  {                                  {
409                                          if (errno != EINTR)                                          if (errno != EINTR)
410                                          {                                          {
411    #ifdef HAVE_SYS_EPOLL_H
412                                                  log_error("epoll_wait() error (%d)\n", errno);                                                  log_error("epoll_wait() error (%d)\n", errno);
413    #else
414                                                    log_error("poll() error (%d)\n", errno);
415    #endif
416                                                  break;                                                  break;
417                                          }                                          }
418                                          continue;                                          continue;
419                                  }                                  }
420                                  else if (nfds == 0) // timeout                                  else if (ret == 0) // timeout
421                                  {                                  {
422                                          out = KEY_TIMEOUT;                                          out = KEY_TIMEOUT;
423                                          break;                                          break;
# Line 350  int igetch(int timeout) Line 425  int igetch(int timeout)
425    
426                                  for (int i = 0; i < nfds; i++)                                  for (int i = 0; i < nfds; i++)
427                                  {                                  {
428    #ifdef HAVE_SYS_EPOLL_H
429                                          if (events[i].data.fd == STDIN_FILENO)                                          if (events[i].data.fd == STDIN_FILENO)
430    #else
431                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
432    #endif
433                                          {                                          {
434                                                  stdin_read_wait = 1;                                                  stdin_read_wait = 1;
435                                          }                                          }


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

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