/[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.100 by sysadm, Tue Dec 2 08:04:29 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>
43  #include <sys/syscall.h>  #include <sys/stat.h>
44  #include <sys/types.h>  #include <sys/types.h>
45  #include <sys/wait.h>  #include <sys/wait.h>
46    
47    #ifdef HAVE_SYS_EPOLL_H
48    #include <sys/epoll.h>
49    #else
50    #include <poll.h>
51    #endif
52    
53  #ifdef HAVE_SYSTEMD_SD_DAEMON_H  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
54  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
55  #endif  #endif
# Line 84  struct channel_data_struct Line 90  struct channel_data_struct
90    
91  static int socket_server[2];  static int socket_server[2];
92  static int socket_client;  static int socket_client;
93    
94    #ifdef HAVE_SYS_EPOLL_H
95  static int epollfd_server = -1;  static int epollfd_server = -1;
96    #endif
97    
98  static ssh_bind sshbind;  static ssh_bind sshbind;
99    
100  static HASH_DICT *hash_dict_pid_sockaddr = NULL;  static HASH_DICT *hash_dict_pid_sockaddr = NULL;
# Line 318  static int fork_server(void) Line 328  static int fork_server(void)
328          }          }
329    
330          // Child process          // Child process
331    #ifdef HAVE_SYS_EPOLL_H
332          if (close(epollfd_server) < 0)          if (close(epollfd_server) < 0)
333          {          {
334                  log_error("close(epollfd_server) error (%d)\n");                  log_error("close(epollfd_server) error (%d)\n");
335          }          }
336    #endif
337    
338          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
339          {          {
# Line 411  static int fork_server(void) Line 423  static int fork_server(void)
423                          log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));                          log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
424                          goto cleanup;                          goto cleanup;
425                  }                  }
426    
427                    ssh_set_blocking(SSH_session, 0);
428          }          }
429    
430          // Redirect Input          // Redirect Input
# Line 435  static int fork_server(void) Line 449  static int fork_server(void)
449    
450          SYS_child_process_count = 0;          SYS_child_process_count = 0;
451    
452            // BWF compile
453            if (bwf_compile() < 0)
454            {
455                    log_error("bwf_compile() error\n");
456                    goto cleanup;
457            }
458    
459          bbs_main();          bbs_main();
460    
461  cleanup:  cleanup:
# Line 471  cleanup: Line 492  cleanup:
492          ssh_free(SSH_session);          ssh_free(SSH_session);
493          ssh_finalize();          ssh_finalize();
494    
495            // BWF cleanup
496            bwf_cleanup();
497    
498          // Close Input and Output for client          // Close Input and Output for client
499          io_cleanup();          io_cleanup();
500          close(STDIN_FILENO);          close(STDIN_FILENO);
# Line 486  cleanup: Line 510  cleanup:
510    
511  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
512  {  {
513            struct stat file_stat;
514          unsigned int addrlen;          unsigned int addrlen;
515          int ret;          int ret;
516          int flags_server[2];          int flags_server[2];
517          struct sockaddr_in sin;          struct sockaddr_in sin;
518    
519    #ifdef HAVE_SYS_EPOLL_H
520          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
521    #else
522            struct pollfd pfds[2];
523    #endif
524    
525          int nfds;          int nfds;
         siginfo_t siginfo;  
526          int notify_child_exit = 0;          int notify_child_exit = 0;
527          time_t tm_notify_child_exit = time(NULL);          time_t tm_notify_child_exit = time(NULL);
         MENU_SET bbs_menu_new;  
         MENU_SET top10_menu_new;  
528          int i, j;          int i, j;
529          pid_t pid;          pid_t pid;
530            int ssh_key_valid = 0;
531          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
532    
533  #ifdef HAVE_SYSTEMD_SD_DAEMON_H  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
534          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
535  #endif  #endif
# Line 508  int net_server(const char *hostaddr, in_ Line 538  int net_server(const char *hostaddr, in_
538    
539          sshbind = ssh_bind_new();          sshbind = ssh_bind_new();
540    
541            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0)
542            {
543                    log_error("Error loading SSH RSA key: %s\n", SSH_HOST_RSA_KEY_FILE);
544            }
545            else
546            {
547                    ssh_key_valid = 1;
548            }
549            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0)
550            {
551                    log_error("Error loading SSH ED25519 key: %s\n", SSH_HOST_ED25519_KEY_FILE);
552            }
553            else
554            {
555                    ssh_key_valid = 1;
556            }
557            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ECDSA_KEY_FILE) < 0)
558            {
559                    log_error("Error loading SSH ECDSA key: %s\n", SSH_HOST_ECDSA_KEY_FILE);
560            }
561            else
562            {
563                    ssh_key_valid = 1;
564            }
565    
566            if (!ssh_key_valid)
567            {
568                    log_error("Error: no valid SSH host key\n");
569                    ssh_bind_free(sshbind);
570                    return -1;
571            }
572    
573          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
574                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
575                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "+ssh-rsa") < 0 ||
                 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||  
576                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
577          {          {
578                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
# Line 519  int net_server(const char *hostaddr, in_ Line 580  int net_server(const char *hostaddr, in_
580                  return -1;                  return -1;
581          }          }
582    
583    #ifdef HAVE_SYS_EPOLL_H
584          epollfd_server = epoll_create1(0);          epollfd_server = epoll_create1(0);
585          if (epollfd_server == -1)          if (epollfd_server == -1)
586          {          {
587                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)\n", errno);
588                  return -1;                  return -1;
589          }          }
590    #endif
591    
592          // Server socket          // Server socket
593          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
# Line 547  int net_server(const char *hostaddr, in_ Line 610  int net_server(const char *hostaddr, in_
610                  {                  {
611                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
612                  }                  }
613    #if defined(SO_REUSEPORT)
614                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)
615                  {                  {
616                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
617                  }                  }
618    #endif
619    
620                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
621                  {                  {
# Line 567  int net_server(const char *hostaddr, in_ Line 632  int net_server(const char *hostaddr, in_
632    
633                  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));
634    
635    #ifdef HAVE_SYS_EPOLL_H
636                  ev.events = EPOLLIN;                  ev.events = EPOLLIN;
637                  ev.data.fd = socket_server[i];                  ev.data.fd = socket_server[i];
638                  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 644  int net_server(const char *hostaddr, in_
644                          }                          }
645                          return -1;                          return -1;
646                  }                  }
647    #endif
648    
649                  flags_server[i] = fcntl(socket_server[i], F_GETFL, 0);                  flags_server[i] = fcntl(socket_server[i], F_GETFL, 0);
650                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);
651          }          }
652    
653            ssh_bind_set_blocking(sshbind, 0);
654    
655          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
656          if (hash_dict_pid_sockaddr == NULL)          if (hash_dict_pid_sockaddr == NULL)
657          {          {
# Line 618  int net_server(const char *hostaddr, in_ Line 687  int net_server(const char *hostaddr, in_
687                  {                  {
688                          SYS_child_exit = 0;                          SYS_child_exit = 0;
689    
690                          siginfo.si_pid = 0;                          pid = waitpid(-1, &ret, WNOHANG);
691                          ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);                          if (pid > 0)
                         if (ret == 0 && siginfo.si_pid > 0)  
692                          {                          {
693                                  SYS_child_exit = 1; // Retry waitid                                  SYS_child_exit = 1; // Retry waitid
   
694                                  SYS_child_process_count--;                                  SYS_child_process_count--;
                                 log_common("Child process (%d) exited\n", siginfo.si_pid);  
695    
696                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (WIFEXITED(ret))
697                                    {
698                                            log_common("Child process (%d) exited, status=%d\n", pid, WEXITSTATUS(ret));
699                                    }
700                                    else if (WIFSIGNALED(ret))
701                                    {
702                                            log_common("Child process (%d) is killed, status=%d\n", pid, WTERMSIG(ret));
703                                    }
704                                    else
705                                    {
706                                            log_common("Child process (%d) exited abnormally, status=%d\n", pid, ret);
707                                    }
708    
709                                    if (pid != section_list_loader_pid)
710                                  {                                  {
711                                          j = 0;                                          j = 0;
712                                          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);
713                                          if (ret < 0)                                          if (ret < 0)
714                                          {                                          {
715                                                  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);
716                                          }                                          }
717                                          else                                          else
718                                          {                                          {
# Line 643  int net_server(const char *hostaddr, in_ Line 722  int net_server(const char *hostaddr, in_
722                                                          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);
723                                                  }                                                  }
724    
725                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid);
726                                                  if (ret < 0)                                                  if (ret < 0)
727                                                  {                                                  {
728                                                          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);
729                                                  }                                                  }
730                                          }                                          }
731                                  }                                  }
732                          }                          }
733                          else if (ret == 0)                          else if (pid == 0)
734                          {                          {
735                                  break;                                  break;
736                          }                          }
737                          else if (ret < 0)                          else if (pid < 0)
738                          {                          {
739                                  log_error("Error in waitid: %d\n", errno);                                  log_error("Error in waitpid(): %d\n", errno);
740                                  break;                                  break;
741                          }                          }
742                  }                  }
# Line 671  int net_server(const char *hostaddr, in_ Line 750  int net_server(const char *hostaddr, in_
750                                  log_common("Notify %d child process to exit\n", SYS_child_process_count);                                  log_common("Notify %d child process to exit\n", SYS_child_process_count);
751  #endif  #endif
752    
753                                  if (kill(-getpid(), SIGTERM) < 0)                                  if (kill(0, SIGTERM) < 0)
754                                  {                                  {
755                                          log_error("Send SIGTERM signal failed (%d)\n", errno);                                          log_error("Send SIGTERM signal failed (%d)\n", errno);
756                                  }                                  }
# Line 685  int net_server(const char *hostaddr, in_ Line 764  int net_server(const char *hostaddr, in_
764                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
765  #endif  #endif
766    
767                                  if (kill(-getpid(), SIGKILL) < 0)                                  if (kill(0, SIGKILL) < 0)
768                                  {                                  {
769                                          log_error("Send SIGKILL signal failed (%d)\n", errno);                                          log_error("Send SIGKILL signal failed (%d)\n", errno);
770                                  }                                  }
# Line 726  int net_server(const char *hostaddr, in_ Line 805  int net_server(const char *hostaddr, in_
805                                  log_error("Reload BWF conf failed\n");                                  log_error("Reload BWF conf failed\n");
806                          }                          }
807    
808                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)                          // Get EULA modification tm
809                            if (stat(DATA_EULA, &file_stat) == -1)
810                          {                          {
811                                  unload_menu(&bbs_menu_new);                                  log_error("stat(%s) error\n", DATA_EULA, errno);
                                 log_error("Reload bbs menu failed\n");  
812                          }                          }
813                          else                          else
814                          {                          {
815                                    BBS_eula_tm = file_stat.st_mtim.tv_sec;
816                            }
817    
818                            if (detach_menu_shm(&bbs_menu) < 0)
819                            {
820                                    log_error("detach_menu_shm(bbs_menu) error\n");
821                            }
822                            if (load_menu(&bbs_menu, CONF_MENU) < 0)
823                            {
824                                    log_error("load_menu(bbs_menu) error\n");
825                                  unload_menu(&bbs_menu);                                  unload_menu(&bbs_menu);
                                 memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));  
                                 log_common("Reload bbs menu successfully\n");  
826                          }                          }
827    
828                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)                          if (detach_menu_shm(&top10_menu) < 0)
829                          {                          {
830                                  unload_menu(&top10_menu_new);                                  log_error("detach_menu_shm(top10_menu) error\n");
                                 log_error("Reload top10 menu failed\n");  
831                          }                          }
832                          else                          if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0)
833                          {                          {
834                                    log_error("load_menu(top10_menu) error\n");
835                                  unload_menu(&top10_menu);                                  unload_menu(&top10_menu);
                                 top10_menu_new.allow_exit = 1;  
                                 memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));  
                                 log_common("Reload top10 menu successfully\n");  
836                          }                          }
837    
838                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
# Line 758  int net_server(const char *hostaddr, in_ Line 842  int net_server(const char *hostaddr, in_
842                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);
843                                  }                                  }
844                          }                          }
                         log_common("Reload data files successfully\n");  
845    
846                          // Load section config and gen_ex                          // Load section config and gen_ex
847                          if (load_section_config_from_db(1) < 0)                          if (load_section_config_from_db(1) < 0)
848                          {                          {
849                                  log_error("load_section_config_from_db(1) error\n");                                  log_error("load_section_config_from_db(1) error\n");
850                          }                          }
                         else  
                         {  
                                 log_common("Reload section config and gen_ex successfully\n");  
                         }  
851    
852                          // Notify child processes to reload configuration                          // Notify child processes to reload configuration
853                          if (kill(-getpid(), SIGUSR1) < 0)                          if (kill(0, SIGUSR1) < 0)
854                          {                          {
855                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);
856                          }                          }
# Line 781  int net_server(const char *hostaddr, in_ Line 860  int net_server(const char *hostaddr, in_
860  #endif  #endif
861                  }                  }
862    
863    #ifdef HAVE_SYS_EPOLL_H
864                  nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second
865                    ret = nfds;
866                  if (nfds < 0)  #else
867                    pfds[0].fd = socket_server[0];
868                    pfds[0].events = POLLIN;
869                    pfds[1].fd = socket_server[1];
870                    pfds[1].events = POLLIN;
871                    nfds = 2;
872                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
873    #endif
874                    if (ret < 0)
875                  {                  {
876                          if (errno != EINTR)                          if (errno != EINTR)
877                          {                          {
878    #ifdef HAVE_SYS_EPOLL_H
879                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
880    #else
881                                    log_error("poll() error (%d)\n", errno);
882    #endif
883                                  break;                                  break;
884                          }                          }
885                          continue;                          continue;
# Line 801  int net_server(const char *hostaddr, in_ Line 893  int net_server(const char *hostaddr, in_
893    
894                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
895                  {                  {
896    #ifdef HAVE_SYS_EPOLL_H
897                          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])
898    #else
899                            if ((pfds[i].fd == socket_server[0] || pfds[i].fd == socket_server[1]) && (pfds[i].revents & POLLIN))
900    #endif
901                          {                          {
902    #ifdef HAVE_SYS_EPOLL_H
903                                  SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);                                  SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
904    #else
905                                    SSH_v2 = (pfds[i].fd == socket_server[1] ? 1 : 0);
906    #endif
907    
908                                  while (!SYS_server_exit) // Accept all incoming connections until error                                  while (!SYS_server_exit) // Accept all incoming connections until error
909                                  {                                  {
910                                          addrlen = sizeof(sin);                                          addrlen = sizeof(sin);
911                                          socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);                                          socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, (socklen_t *)&addrlen);
912                                          if (socket_client < 0)                                          if (socket_client < 0)
913                                          {                                          {
914                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 882  int net_server(const char *hostaddr, in_ Line 982  int net_server(const char *hostaddr, in_
982                  }                  }
983          }          }
984    
985    #ifdef HAVE_SYS_EPOLL_H
986          if (close(epollfd_server) < 0)          if (close(epollfd_server) < 0)
987          {          {
988                  log_error("close(epollfd_server) error (%d)\n");                  log_error("close(epollfd_server) error (%d)\n");
989          }          }
990    #endif
991    
992          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
993          {          {


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

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