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

Diff of /lbbs/src/net_server.c

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

Revision 1.87 by sysadm, Mon Nov 17 02:32:42 2025 UTC Revision 1.89 by sysadm, Mon Nov 17 09:33:58 2025 UTC
# Line 38  Line 38 
38  #include <libssh/libssh.h>  #include <libssh/libssh.h>
39  #include <libssh/server.h>  #include <libssh/server.h>
40  #include <netinet/in.h>  #include <netinet/in.h>
41  #include <sys/epoll.h>  #include <sys/ioctl.h>
42  #include <sys/socket.h>  #include <sys/socket.h>
 #include <sys/syscall.h>  
43  #include <sys/types.h>  #include <sys/types.h>
44  #include <sys/wait.h>  #include <sys/wait.h>
45    
46    #ifdef HAVE_SYS_EPOLL_H
47    #include <sys/epoll.h>
48    #else
49    #include <poll.h>
50    #endif
51    
52  #ifdef HAVE_SYSTEMD_SD_DAEMON_H  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
53  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
54  #endif  #endif
# Line 84  struct channel_data_struct Line 89  struct channel_data_struct
89    
90  static int socket_server[2];  static int socket_server[2];
91  static int socket_client;  static int socket_client;
92    
93    #ifdef HAVE_SYS_EPOLL_H
94  static int epollfd_server = -1;  static int epollfd_server = -1;
95    #endif
96    
97  static ssh_bind sshbind;  static ssh_bind sshbind;
98    
99  static HASH_DICT *hash_dict_pid_sockaddr = NULL;  static HASH_DICT *hash_dict_pid_sockaddr = NULL;
# Line 318  static int fork_server(void) Line 327  static int fork_server(void)
327          }          }
328    
329          // Child process          // Child process
330    #ifdef HAVE_SYS_EPOLL_H
331          if (close(epollfd_server) < 0)          if (close(epollfd_server) < 0)
332          {          {
333                  log_error("close(epollfd_server) error (%d)\n");                  log_error("close(epollfd_server) error (%d)\n");
334          }          }
335    #endif
336    
337          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
338          {          {
# Line 490  int net_server(const char *hostaddr, in_ Line 501  int net_server(const char *hostaddr, in_
501          int ret;          int ret;
502          int flags_server[2];          int flags_server[2];
503          struct sockaddr_in sin;          struct sockaddr_in sin;
504    
505    #ifdef HAVE_SYS_EPOLL_H
506          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
507    #else
508            struct pollfd pfds[2];
509    #endif
510    
511          int nfds;          int nfds;
         siginfo_t siginfo;  
512          int notify_child_exit = 0;          int notify_child_exit = 0;
513          time_t tm_notify_child_exit = time(NULL);          time_t tm_notify_child_exit = time(NULL);
514          MENU_SET bbs_menu_new;          MENU_SET bbs_menu_new;
# Line 500  int net_server(const char *hostaddr, in_ Line 516  int net_server(const char *hostaddr, in_
516          int i, j;          int i, j;
517          pid_t pid;          pid_t pid;
518          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
519    
520  #ifdef HAVE_SYSTEMD_SD_DAEMON_H  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
521          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
522  #endif  #endif
# Line 519  int net_server(const char *hostaddr, in_ Line 536  int net_server(const char *hostaddr, in_
536                  return -1;                  return -1;
537          }          }
538    
539    #ifdef HAVE_SYS_EPOLL_H
540          epollfd_server = epoll_create1(0);          epollfd_server = epoll_create1(0);
541          if (epollfd_server == -1)          if (epollfd_server == -1)
542          {          {
543                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)\n", errno);
544                  return -1;                  return -1;
545          }          }
546    #endif
547    
548          // Server socket          // Server socket
549          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
# Line 567  int net_server(const char *hostaddr, in_ Line 586  int net_server(const char *hostaddr, in_
586    
587                  log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));                  log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
588    
589    #ifdef HAVE_SYS_EPOLL_H
590                  ev.events = EPOLLIN;                  ev.events = EPOLLIN;
591                  ev.data.fd = socket_server[i];                  ev.data.fd = socket_server[i];
592                  if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)                  if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
# Line 578  int net_server(const char *hostaddr, in_ Line 598  int net_server(const char *hostaddr, in_
598                          }                          }
599                          return -1;                          return -1;
600                  }                  }
601    #endif
602    
603                  flags_server[i] = fcntl(socket_server[i], F_GETFL, 0);                  flags_server[i] = fcntl(socket_server[i], F_GETFL, 0);
604                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);
# Line 618  int net_server(const char *hostaddr, in_ Line 639  int net_server(const char *hostaddr, in_
639                  {                  {
640                          SYS_child_exit = 0;                          SYS_child_exit = 0;
641    
642                          siginfo.si_pid = 0;                          pid = waitpid(-1, NULL, WNOHANG);
643                          ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);                          if (pid > 0)
                         if (ret == 0 && siginfo.si_pid > 0)  
644                          {                          {
645                                  SYS_child_exit = 1; // Retry waitid                                  SYS_child_exit = 1; // Retry waitid
646    
647                                  SYS_child_process_count--;                                  SYS_child_process_count--;
648                                  log_common("Child process (%d) exited\n", siginfo.si_pid);                                  log_common("Child process (%d) exited\n", pid);
649    
650                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (pid != section_list_loader_pid)
651                                  {                                  {
652                                          j = 0;                                          j = 0;
653                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)pid, (int64_t *)&j);
654                                          if (ret < 0)                                          if (ret < 0)
655                                          {                                          {
656                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", pid);
657                                          }                                          }
658                                          else                                          else
659                                          {                                          {
# Line 643  int net_server(const char *hostaddr, in_ Line 663  int net_server(const char *hostaddr, in_
663                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);
664                                                  }                                                  }
665    
666                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid);
667                                                  if (ret < 0)                                                  if (ret < 0)
668                                                  {                                                  {
669                                                          log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);                                                          log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", pid);
670                                                  }                                                  }
671                                          }                                          }
672                                  }                                  }
673                          }                          }
674                          else if (ret == 0)                          else if (pid == 0)
675                          {                          {
676                                  break;                                  break;
677                          }                          }
678                          else if (ret < 0)                          else if (pid < 0)
679                          {                          {
680                                  log_error("Error in waitid: %d\n", errno);                                  log_error("Error in waitpid(): %d\n", errno);
681                                  break;                                  break;
682                          }                          }
683                  }                  }
# Line 781  int net_server(const char *hostaddr, in_ Line 801  int net_server(const char *hostaddr, in_
801  #endif  #endif
802                  }                  }
803    
804    #ifdef HAVE_SYS_EPOLL_H
805                  nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second
806                    ret = nfds;
807                  if (nfds < 0)  #else
808                    pfds[0].fd = socket_server[0];
809                    pfds[0].events = POLLIN;
810                    pfds[1].fd = socket_server[1];
811                    pfds[1].events = POLLIN;
812                    nfds = 2;
813                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
814    #endif
815                    if (ret < 0)
816                  {                  {
817                          if (errno != EINTR)                          if (errno != EINTR)
818                          {                          {
819    #ifdef HAVE_SYS_EPOLL_H
820                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
821    #else
822                                    log_error("poll() error (%d)\n", errno);
823    #endif
824                                  break;                                  break;
825                          }                          }
826                          continue;                          continue;
# Line 801  int net_server(const char *hostaddr, in_ Line 834  int net_server(const char *hostaddr, in_
834    
835                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
836                  {                  {
837    #ifdef HAVE_SYS_EPOLL_H
838                          if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])                          if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
839    #else
840                            if ((pfds[i].fd == socket_server[0] || pfds[i].fd == socket_server[1]) && (pfds[i].revents & POLLIN))
841    #endif
842                          {                          {
843    #ifdef HAVE_SYS_EPOLL_H
844                                  SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);                                  SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
845    #else
846                                    SSH_v2 = (pfds[i].fd == socket_server[1] ? 1 : 0);
847    #endif
848    
849                                  while (!SYS_server_exit) // Accept all incoming connections until error                                  while (!SYS_server_exit) // Accept all incoming connections until error
850                                  {                                  {
# Line 882  int net_server(const char *hostaddr, in_ Line 923  int net_server(const char *hostaddr, in_
923                  }                  }
924          }          }
925    
926    #ifdef HAVE_SYS_EPOLL_H
927          if (close(epollfd_server) < 0)          if (close(epollfd_server) < 0)
928          {          {
929                  log_error("close(epollfd_server) error (%d)\n");                  log_error("close(epollfd_server) error (%d)\n");
930          }          }
931    #endif
932    
933          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
934          {          {


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

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