/[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.62 by sysadm, Sat Jun 21 02:15:18 2025 UTC Revision 1.72 by sysadm, Mon Oct 13 07:13:39 2025 UTC
# Line 25  Line 25 
25  #include "login.h"  #include "login.h"
26  #include "menu.h"  #include "menu.h"
27  #include "net_server.h"  #include "net_server.h"
28    #include "section_list.h"
29  #include "section_list_loader.h"  #include "section_list_loader.h"
30  #include <errno.h>  #include <errno.h>
31  #include <fcntl.h>  #include <fcntl.h>
# Line 44  Line 45 
45  #include <sys/wait.h>  #include <sys/wait.h>
46  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
47    
48    #define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second
49    #define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second
50    
51  struct process_sockaddr_t  struct process_sockaddr_t
52  {  {
53          pid_t pid;          pid_t pid;
# Line 121  static ssh_channel new_session_channel(s Line 125  static ssh_channel new_session_channel(s
125  static int fork_server(void)  static int fork_server(void)
126  {  {
127          ssh_event event;          ssh_event event;
128            long int ssh_timeout = 0;
129          int pid;          int pid;
130          int i;          int i;
131          int ret;          int ret;
# Line 172  static int fork_server(void) Line 177  static int fork_server(void)
177                  ssh_callbacks_init(&cb);                  ssh_callbacks_init(&cb);
178                  ssh_set_server_callbacks(SSH_session, &cb);                  ssh_set_server_callbacks(SSH_session, &cb);
179    
180                    ssh_timeout = 60; // second
181                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
182                    {
183                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
184                            goto cleanup;
185                    }
186    
187                  if (ssh_handle_key_exchange(SSH_session))                  if (ssh_handle_key_exchange(SSH_session))
188                  {                  {
189                          log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));                          log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
# Line 197  static int fork_server(void) Line 209  static int fork_server(void)
209                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times\n", cb_data.tries);
210                          goto cleanup;                          goto cleanup;
211                  }                  }
212    
213                    ssh_timeout = 0;
214                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
215                    {
216                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
217                            goto cleanup;
218                    }
219          }          }
220    
221          // Redirect Input          // Redirect Input
# Line 257  int net_server(const char *hostaddr, in_ Line 276  int net_server(const char *hostaddr, in_
276          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
277          int nfds, epollfd;          int nfds, epollfd;
278          siginfo_t siginfo;          siginfo_t siginfo;
279            int notify_child_exit = 0;
280            time_t tm_notify_child_exit = time(NULL);
281          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
282          MENU_SET *p_bbs_menu_new;          MENU_SET bbs_menu_new;
283            MENU_SET top10_menu_new;
284          int i, j;          int i, j;
285          pid_t pid;          pid_t pid;
286          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 270  int net_server(const char *hostaddr, in_ Line 292  int net_server(const char *hostaddr, in_
292          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
293                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
294                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
295                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
296                  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)
297          {          {
298                  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 398  int net_server(const char *hostaddr, in_ Line 421  int net_server(const char *hostaddr, in_
421    
422                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
423                  {                  {
424                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
425                          {                          {
426                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
427                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
428    
429                                    if (kill(0, SIGTERM) < 0)
430                                    {
431                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
432                                    }
433    
434                                    notify_child_exit = 1;
435                                    tm_notify_child_exit = time(NULL);
436                          }                          }
437                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
438                            {
439                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
440    
441                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  for (i = 0; i < BBS_max_client; i++)
442                                    {
443                                            if (process_sockaddr_pool[i].pid != 0)
444                                            {
445                                                    log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);
446                                                    if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)
447                                                    {
448                                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
449                                                    }
450                                            }
451                                    }
452    
453                                    notify_child_exit = 2;
454                                    tm_notify_child_exit = time(NULL);
455                            }
456                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
457                            {
458                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
459                                    SYS_child_process_count = 0;
460                            }
461                  }                  }
462    
463                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
# Line 418  int net_server(const char *hostaddr, in_ Line 471  int net_server(const char *hostaddr, in_
471                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
472                          }                          }
473    
474                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
                         if (p_bbs_menu_new == NULL)  
475                          {                          {
476                                  log_error("OOM: calloc(MENU_SET)\n");                                  unload_menu(&bbs_menu_new);
477                                    log_error("Reload bbs menu failed\n");
478                          }                          }
479                          else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)                          else
480                          {                          {
481                                  unload_menu(p_bbs_menu_new);                                  unload_menu(&bbs_menu);
482                                  free(p_bbs_menu_new);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
483                                  p_bbs_menu_new = NULL;                                  log_common("Reload bbs menu successfully\n");
484                            }
485    
486                                  log_error("Reload menu failed\n");                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
487                            {
488                                    unload_menu(&top10_menu_new);
489                                    log_error("Reload top10 menu failed\n");
490                          }                          }
491                          else                          else
492                          {                          {
493                                  unload_menu(p_bbs_menu);                                  unload_menu(&top10_menu);
494                                  free(p_bbs_menu);                                  top10_menu_new.allow_exit = 1;
495                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
496                                  p_bbs_menu = p_bbs_menu_new;                                  log_common("Reload top10 menu successfully\n");
                                 p_bbs_menu_new = NULL;  
   
                                 log_common("Reload menu successfully\n");  
497                          }                          }
498    
                         sd_notify(0, "READY=1");  
                 }  
   
                 if (SYS_data_file_reload && !SYS_server_exit)  
                 {  
                         SYS_data_file_reload = 0;  
                         sd_notify(0, "RELOADING=1");  
   
499                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
500                          {                          {
501                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
# Line 457  int net_server(const char *hostaddr, in_ Line 503  int net_server(const char *hostaddr, in_
503                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
504                                  }                                  }
505                          }                          }
   
506                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
                         sd_notify(0, "READY=1");  
                 }  
   
                 if (SYS_section_list_reload && !SYS_server_exit)  
                 {  
                         SYS_section_list_reload = 0;  
507    
508                          if (section_list_loader_reload() < 0)                          // Load section config and gen_ex
509                            if (load_section_config_from_db(1) < 0)
510                          {                          {
511                                  log_error("ksection_list_loader_reload() failed\n");                                  log_error("load_section_config_from_db(1) error\n");
512                          }                          }
513                            else
514                            {
515                                    log_common("Reload section config and gen_ex successfully\n");
516                            }
517    
518                            sd_notify(0, "READY=1");
519                  }                  }
520    
521                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 522  int net_server(const char *hostaddr, in_ Line 568  int net_server(const char *hostaddr, in_
568    
569                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
570    
571                                          log_common("Accept %sconnection from %s:%d\n", (SSH_v2 ? "" : "SSH2 "), hostaddr_client, port_client);                                          log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
572    
573                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
574                                          {                                          {


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

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