/[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.82 by sysadm, Fri Nov 14 08:45:25 2025 UTC Revision 1.96 by sysadm, Fri Nov 21 10:34:10 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
53  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
54    #endif
55    
56  enum _net_server_constant_t  enum _net_server_constant_t
57  {  {
# Line 53  enum _net_server_constant_t Line 61  enum _net_server_constant_t
61          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
62  };  };
63    
 static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";  
   
64  /* A userdata struct for session. */  /* A userdata struct for session. */
65  struct session_data_struct  struct session_data_struct
66  {  {
# Line 81  struct channel_data_struct Line 87  struct channel_data_struct
87          struct winsize *winsize;          struct winsize *winsize;
88  };  };
89    
90    static int socket_server[2];
91    static int socket_client;
92    
93    #ifdef HAVE_SYS_EPOLL_H
94    static int epollfd_server = -1;
95    #endif
96    
97    static ssh_bind sshbind;
98    
99    static HASH_DICT *hash_dict_pid_sockaddr = NULL;
100    static HASH_DICT *hash_dict_sockaddr_count = NULL;
101    
102    static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
103    
104  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
105                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
106  {  {
# Line 307  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)
332            {
333                    log_error("close(epollfd_server) error (%d)\n");
334            }
335    #endif
336    
337          if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)          for (i = 0; i < 2; i++)
338          {          {
339                  log_error("Close server socket failed\n");                  if (close(socket_server[i]) == -1)
340                    {
341                            log_error("Close server socket failed\n");
342                    }
343          }          }
344    
345            hash_dict_destroy(hash_dict_pid_sockaddr);
346            hash_dict_destroy(hash_dict_sockaddr_count);
347    
348          SSH_session = ssh_new();          SSH_session = ssh_new();
349    
350          if (SSH_v2)          if (SSH_v2)
# Line 393  static int fork_server(void) Line 425  static int fork_server(void)
425          }          }
426    
427          // Redirect Input          // Redirect Input
         close(STDIN_FILENO);  
428          if (dup2(socket_client, STDIN_FILENO) == -1)          if (dup2(socket_client, STDIN_FILENO) == -1)
429          {          {
430                  log_error("Redirect stdin to client socket failed\n");                  log_error("Redirect stdin to client socket failed\n");
# Line 401  static int fork_server(void) Line 432  static int fork_server(void)
432          }          }
433    
434          // Redirect Output          // Redirect Output
         close(STDOUT_FILENO);  
435          if (dup2(socket_client, STDOUT_FILENO) == -1)          if (dup2(socket_client, STDOUT_FILENO) == -1)
436          {          {
437                  log_error("Redirect stdout to client socket failed\n");                  log_error("Redirect stdout to client socket failed\n");
438                  goto cleanup;                  goto cleanup;
439          }          }
440    
441            if (io_init() < 0)
442            {
443                    log_error("io_init() error\n");
444                    goto cleanup;
445            }
446    
447          SYS_child_process_count = 0;          SYS_child_process_count = 0;
448    
449            // BWF compile
450            if (bwf_compile() < 0)
451            {
452                    log_error("bwf_compile() error\n");
453                    goto cleanup;
454            }
455    
456          bbs_main();          bbs_main();
457    
458  cleanup:  cleanup:
# Line 418  cleanup: Line 461  cleanup:
461    
462          if (SSH_v2)          if (SSH_v2)
463          {          {
464                  close(cdata.pty_master);                  if (cdata.pty_master != -1)
465                  close(cdata.child_stdin);                  {
466                  close(cdata.child_stdout);                          close(cdata.pty_master);
467                  close(cdata.child_stderr);                  }
468                    if (cdata.child_stdin != -1)
469                    {
470                            close(cdata.child_stdin);
471                    }
472                    if (cdata.child_stdout != -1)
473                    {
474                            close(cdata.child_stdout);
475                    }
476                    if (cdata.child_stderr != -1)
477                    {
478                            close(cdata.child_stderr);
479                    }
480    
481                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
482                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
# Line 434  cleanup: Line 489  cleanup:
489          ssh_free(SSH_session);          ssh_free(SSH_session);
490          ssh_finalize();          ssh_finalize();
491    
492            // BWF cleanup
493            bwf_cleanup();
494    
495          // Close Input and Output for client          // Close Input and Output for client
496            io_cleanup();
497          close(STDIN_FILENO);          close(STDIN_FILENO);
498          close(STDOUT_FILENO);          close(STDOUT_FILENO);
499    
# Line 448  cleanup: Line 507  cleanup:
507    
508  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
509  {  {
         HASH_DICT *hash_dict_pid_sockaddr = NULL;  
         HASH_DICT *hash_dict_sockaddr_count = NULL;  
   
510          unsigned int addrlen;          unsigned int addrlen;
511          int ret;          int ret;
512          int flags[2];          int flags_server[2];
513          struct sockaddr_in sin;          struct sockaddr_in sin;
514    
515    #ifdef HAVE_SYS_EPOLL_H
516          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
517          int nfds, epollfd;  #else
518          siginfo_t siginfo;          struct pollfd pfds[2];
519    #endif
520    
521            int nfds;
522          int notify_child_exit = 0;          int notify_child_exit = 0;
523          time_t tm_notify_child_exit = time(NULL);          time_t tm_notify_child_exit = time(NULL);
         int sd_notify_stopping = 0;  
         MENU_SET bbs_menu_new;  
         MENU_SET top10_menu_new;  
524          int i, j;          int i, j;
525          pid_t pid;          pid_t pid;
526          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
527    
528    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
529            int sd_notify_stopping = 0;
530    #endif
531    
532          ssh_init();          ssh_init();
533    
534          sshbind = ssh_bind_new();          sshbind = ssh_bind_new();
# Line 482  int net_server(const char *hostaddr, in_ Line 544  int net_server(const char *hostaddr, in_
544                  return -1;                  return -1;
545          }          }
546    
547          epollfd = epoll_create1(0);  #ifdef HAVE_SYS_EPOLL_H
548          if (epollfd < 0)          epollfd_server = epoll_create1(0);
549            if (epollfd_server == -1)
550          {          {
551                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)\n", errno);
552                  return -1;                  return -1;
553          }          }
554    #endif
555    
556          // Server socket          // Server socket
557          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
# Line 505  int net_server(const char *hostaddr, in_ Line 569  int net_server(const char *hostaddr, in_
569                  sin.sin_port = htons(port[i]);                  sin.sin_port = htons(port[i]);
570    
571                  // Reuse address and port                  // Reuse address and port
572                  flags[i] = 1;                  flags_server[i] = 1;
573                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags_server[i], sizeof(flags_server[i])) < 0)
574                  {                  {
575                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
576                  }                  }
577                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)  #if defined(SO_REUSEPORT)
578                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)
579                  {                  {
580                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
581                  }                  }
582    #endif
583    
584                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
585                  {                  {
# Line 530  int net_server(const char *hostaddr, in_ Line 596  int net_server(const char *hostaddr, in_
596    
597                  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));
598    
599    #ifdef HAVE_SYS_EPOLL_H
600                  ev.events = EPOLLIN;                  ev.events = EPOLLIN;
601                  ev.data.fd = socket_server[i];                  ev.data.fd = socket_server[i];
602                  if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)                  if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
603                  {                  {
604                          log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);                          log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
605                          if (close(epollfd) < 0)                          if (close(epollfd_server) < 0)
606                          {                          {
607                                  log_error("close(epoll) error (%d)\n");                                  log_error("close(epoll) error (%d)\n");
608                          }                          }
609                          return -1;                          return -1;
610                  }                  }
611    #endif
612    
613                  flags[i] = fcntl(socket_server[i], F_GETFL, 0);                  flags_server[i] = fcntl(socket_server[i], F_GETFL, 0);
614                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);
615          }          }
616    
617          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
# Line 560  int net_server(const char *hostaddr, in_ Line 628  int net_server(const char *hostaddr, in_
628          }          }
629    
630          // Startup complete          // Startup complete
631    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
632          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
633                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
634                                    "MAINPID=%d",                                    "MAINPID=%d",
635                             hostaddr, port[0], hostaddr, port[1], getpid());                             hostaddr, port[0], hostaddr, port[1], getpid());
636    #endif
637    
638          while (!SYS_server_exit || SYS_child_process_count > 0)          while (!SYS_server_exit || SYS_child_process_count > 0)
639          {          {
640    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
641                  if (SYS_server_exit && !sd_notify_stopping)                  if (SYS_server_exit && !sd_notify_stopping)
642                  {                  {
643                          sd_notify(0, "STOPPING=1");                          sd_notify(0, "STOPPING=1");
644                          sd_notify_stopping = 1;                          sd_notify_stopping = 1;
645                  }                  }
646    #endif
647    
648                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
649                  {                  {
650                          SYS_child_exit = 0;                          SYS_child_exit = 0;
651    
652                          siginfo.si_pid = 0;                          pid = waitpid(-1, &ret, WNOHANG);
653                          ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);                          if (pid > 0)
                         if (ret == 0 && siginfo.si_pid > 0)  
654                          {                          {
655                                  SYS_child_exit = 1; // Retry waitid                                  SYS_child_exit = 1; // Retry waitid
   
656                                  SYS_child_process_count--;                                  SYS_child_process_count--;
                                 log_common("Child process (%d) exited\n", siginfo.si_pid);  
657    
658                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (WIFEXITED(ret))
659                                    {
660                                            log_common("Child process (%d) exited, status=%d\n", pid, WEXITSTATUS(ret));
661                                    }
662                                    else if (WIFSIGNALED(ret))
663                                    {
664                                            log_common("Child process (%d) is killed, status=%d\n", pid, WTERMSIG(ret));
665                                    }
666                                    else
667                                    {
668                                            log_common("Child process (%d) exited abnormally, status=%d\n", pid, ret);
669                                    }
670    
671                                    if (pid != section_list_loader_pid)
672                                  {                                  {
673                                          j = 0;                                          j = 0;
674                                          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);
675                                          if (ret < 0)                                          if (ret < 0)
676                                          {                                          {
677                                                  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);
678                                          }                                          }
679                                          else                                          else
680                                          {                                          {
681                                                  sin.sin_addr.s_addr = (uint32_t)j;                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1);
                                                 j = 0;  
                                                 ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, (int64_t *)&j);  
682                                                  if (ret < 0)                                                  if (ret < 0)
683                                                  {                                                  {
684                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %d) error\n", sin.sin_addr.s_addr);                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);
                                                 }  
                                                 else if (ret == 0)  
                                                 {  
                                                         log_error("hash_dict_get(hash_dict_sockaddr_count, %d) not found\n", sin.sin_addr.s_addr);  
                                                         j = 1;  
                                                 }  
   
                                                 j--;  
                                                 ret = hash_dict_set(hash_dict_sockaddr_count, sin.sin_addr.s_addr, j);  
                                                 if (ret < 0)  
                                                 {  
                                                         log_error("hash_dict_set(hash_dict_sockaddr_count, %d, %d) error\n", sin.sin_addr.s_addr, j);  
685                                                  }                                                  }
686    
687                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid);
688                                                  if (ret < 0)                                                  if (ret < 0)
689                                                  {                                                  {
690                                                          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);
691                                                  }                                                  }
692                                          }                                          }
693                                  }                                  }
694                          }                          }
695                          else if (ret == 0)                          else if (pid == 0)
696                          {                          {
697                                  break;                                  break;
698                          }                          }
699                          else if (ret < 0)                          else if (pid < 0)
700                          {                          {
701                                  log_error("Error in waitid: %d\n", errno);                                  log_error("Error in waitpid(): %d\n", errno);
702                                  break;                                  break;
703                          }                          }
704                  }                  }
# Line 639  int net_server(const char *hostaddr, in_ Line 707  int net_server(const char *hostaddr, in_
707                  {                  {
708                          if (notify_child_exit == 0)                          if (notify_child_exit == 0)
709                          {                          {
710    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
711                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
712                                  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);
713    #endif
714    
715                                  if (kill(-getpid(), SIGTERM) < 0)                                  if (kill(0, SIGTERM) < 0)
716                                  {                                  {
717                                          log_error("Send SIGTERM signal failed (%d)\n", errno);                                          log_error("Send SIGTERM signal failed (%d)\n", errno);
718                                  }                                  }
# Line 652  int net_server(const char *hostaddr, in_ Line 722  int net_server(const char *hostaddr, in_
722                          }                          }
723                          else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)                          else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
724                          {                          {
725    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
726                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
727    #endif
728    
729                                  if (kill(-getpid(), SIGKILL) < 0)                                  if (kill(0, SIGKILL) < 0)
730                                  {                                  {
731                                          log_error("Send SIGKILL signal failed (%d)\n", errno);                                          log_error("Send SIGKILL signal failed (%d)\n", errno);
732                                  }                                  }
# Line 672  int net_server(const char *hostaddr, in_ Line 744  int net_server(const char *hostaddr, in_
744                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
745                  {                  {
746                          SYS_conf_reload = 0;                          SYS_conf_reload = 0;
747    
748    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
749                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
750    #endif
751    
752                            // Restart log
753                            if (log_restart() < 0)
754                            {
755                                    log_error("Restart logging failed\n");
756                            }
757    
758                          // Reload configuration                          // Reload configuration
759                          if (load_conf(CONF_BBSD) < 0)                          if (load_conf(CONF_BBSD) < 0)
# Line 686  int net_server(const char *hostaddr, in_ Line 767  int net_server(const char *hostaddr, in_
767                                  log_error("Reload BWF conf failed\n");                                  log_error("Reload BWF conf failed\n");
768                          }                          }
769    
770                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)                          if (detach_menu_shm(&bbs_menu) < 0)
771                          {                          {
772                                  unload_menu(&bbs_menu_new);                                  log_error("detach_menu_shm(bbs_menu) error\n");
                                 log_error("Reload bbs menu failed\n");  
773                          }                          }
774                          else                          if (load_menu(&bbs_menu, CONF_MENU) < 0)
775                          {                          {
776                                    log_error("load_menu(bbs_menu) error\n");
777                                  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");  
778                          }                          }
779    
780                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)                          if (detach_menu_shm(&top10_menu) < 0)
781                          {                          {
782                                  unload_menu(&top10_menu_new);                                  log_error("detach_menu_shm(top10_menu) error\n");
                                 log_error("Reload top10 menu failed\n");  
783                          }                          }
784                          else                          if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0)
785                          {                          {
786                                    log_error("load_menu(top10_menu) error\n");
787                                  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");  
788                          }                          }
789    
790                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
# Line 718  int net_server(const char *hostaddr, in_ Line 794  int net_server(const char *hostaddr, in_
794                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);
795                                  }                                  }
796                          }                          }
                         log_common("Reload data files successfully\n");  
797    
798                          // Load section config and gen_ex                          // Load section config and gen_ex
799                          if (load_section_config_from_db(1) < 0)                          if (load_section_config_from_db(1) < 0)
800                          {                          {
801                                  log_error("load_section_config_from_db(1) error\n");                                  log_error("load_section_config_from_db(1) error\n");
802                          }                          }
803                          else  
804                            // Notify child processes to reload configuration
805                            if (kill(0, SIGUSR1) < 0)
806                          {                          {
807                                  log_common("Reload section config and gen_ex successfully\n");                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);
808                          }                          }
809    
810    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
811                          sd_notify(0, "READY=1");                          sd_notify(0, "READY=1");
812    #endif
813                  }                  }
814    
815                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second  #ifdef HAVE_SYS_EPOLL_H
816                    nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second
817                  if (nfds < 0)                  ret = nfds;
818    #else
819                    pfds[0].fd = socket_server[0];
820                    pfds[0].events = POLLIN;
821                    pfds[1].fd = socket_server[1];
822                    pfds[1].events = POLLIN;
823                    nfds = 2;
824                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
825    #endif
826                    if (ret < 0)
827                  {                  {
828                          if (errno != EINTR)                          if (errno != EINTR)
829                          {                          {
830    #ifdef HAVE_SYS_EPOLL_H
831                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
832    #else
833                                    log_error("poll() error (%d)\n", errno);
834    #endif
835                                  break;                                  break;
836                          }                          }
837                          continue;                          continue;
# Line 753  int net_server(const char *hostaddr, in_ Line 845  int net_server(const char *hostaddr, in_
845    
846                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
847                  {                  {
848    #ifdef HAVE_SYS_EPOLL_H
849                          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])
850    #else
851                            if ((pfds[i].fd == socket_server[0] || pfds[i].fd == socket_server[1]) && (pfds[i].revents & POLLIN))
852    #endif
853                          {                          {
854    #ifdef HAVE_SYS_EPOLL_H
855                                  SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);                                  SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
856    #else
857                                    SSH_v2 = (pfds[i].fd == socket_server[1] ? 1 : 0);
858    #endif
859    
860                                  while (!SYS_server_exit) // Accept all incoming connections until error                                  while (!SYS_server_exit) // Accept all incoming connections until error
861                                  {                                  {
862                                          addrlen = sizeof(sin);                                          addrlen = sizeof(sin);
863                                          socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);                                          socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, (socklen_t *)&addrlen);
864                                          if (socket_client < 0)                                          if (socket_client < 0)
865                                          {                                          {
866                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 808  int net_server(const char *hostaddr, in_ Line 908  int net_server(const char *hostaddr, in_
908                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);
909                                                                  }                                                                  }
910    
911                                                                  ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, j + 1);                                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
912                                                                  if (ret < 0)                                                                  if (ret < 0)
913                                                                  {                                                                  {
914                                                                          log_error("hash_dict_set(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, j + 1);                                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);
915                                                                  }                                                                  }
916                                                          }                                                          }
917                                                  }                                                  }
# Line 834  int net_server(const char *hostaddr, in_ Line 934  int net_server(const char *hostaddr, in_
934                  }                  }
935          }          }
936    
937          if (close(epollfd) < 0)  #ifdef HAVE_SYS_EPOLL_H
938            if (close(epollfd_server) < 0)
939          {          {
940                  log_error("close(epoll) error (%d)\n");                  log_error("close(epollfd_server) error (%d)\n");
941          }          }
942    #endif
943    
944          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
945          {          {
                 fcntl(socket_server[i], F_SETFL, flags[i]);  
   
946                  if (close(socket_server[i]) == -1)                  if (close(socket_server[i]) == -1)
947                  {                  {
948                          log_error("Close server socket failed\n");                          log_error("Close server socket failed\n");


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

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