/[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.59 by sysadm, Mon Jun 16 14:32:25 2025 UTC Revision 1.71 by sysadm, Mon Sep 22 05:07:22 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _XOPEN_SOURCE 500  
 #define _POSIX_C_SOURCE 200809L  
 #define _GNU_SOURCE  
   
 #include "net_server.h"  
 #include "common.h"  
 #include "bbs_main.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "log.h"  #include "bbs_main.h"
19    #include "common.h"
20    #include "database.h"
21    #include "file_loader.h"
22  #include "io.h"  #include "io.h"
23  #include "init.h"  #include "init.h"
24  #include "menu.h"  #include "log.h"
 #include "database.h"  
25  #include "login.h"  #include "login.h"
26  #include "file_loader.h"  #include "menu.h"
27    #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>
 #include <string.h>  
32  #include <signal.h>  #include <signal.h>
33  #include <stdlib.h>  #include <stdlib.h>
34    #include <string.h>
35  #include <unistd.h>  #include <unistd.h>
 #include <sys/syscall.h>  
 #include <sys/socket.h>  
 #include <sys/wait.h>  
 #include <sys/epoll.h>  
36  #include <arpa/inet.h>  #include <arpa/inet.h>
37  #include <netinet/in.h>  #include <libssh/callbacks.h>
 #include <systemd/sd-daemon.h>  
38  #include <libssh/libssh.h>  #include <libssh/libssh.h>
39  #include <libssh/server.h>  #include <libssh/server.h>
40  #include <libssh/callbacks.h>  #include <netinet/in.h>
41    #include <sys/epoll.h>
42    #include <sys/socket.h>
43    #include <sys/syscall.h>
44    #include <sys/types.h>
45    #include <sys/wait.h>
46    #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  {  {
# Line 124  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 175  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 200  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 260  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          int i, j;          int i, j;
284          pid_t pid;          pid_t pid;
285          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 273  int net_server(const char *hostaddr, in_ Line 291  int net_server(const char *hostaddr, in_
291          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
292                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
293                  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 ||
294                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
295                  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)
296          {          {
297                  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 354  int net_server(const char *hostaddr, in_ Line 373  int net_server(const char *hostaddr, in_
373          {          {
374                  if (SYS_server_exit && !sd_notify_stopping)                  if (SYS_server_exit && !sd_notify_stopping)
375                  {                  {
                         signal(SIGHUP, SIG_IGN);  
   
376                          sd_notify(0, "STOPPING=1");                          sd_notify(0, "STOPPING=1");
377                          sd_notify_stopping = 1;                          sd_notify_stopping = 1;
378                  }                  }
# Line 403  int net_server(const char *hostaddr, in_ Line 420  int net_server(const char *hostaddr, in_
420    
421                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
422                  {                  {
423                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
424                          {                          {
425                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
426                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
427    
428                                    if (kill(0, SIGTERM) < 0)
429                                    {
430                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
431                                    }
432    
433                                    notify_child_exit = 1;
434                                    tm_notify_child_exit = time(NULL);
435                          }                          }
436                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
437                            {
438                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
439    
440                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  for (i = 0; i < BBS_max_client; i++)
441                                    {
442                                            if (process_sockaddr_pool[i].pid != 0)
443                                            {
444                                                    log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);
445                                                    if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)
446                                                    {
447                                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
448                                                    }
449                                            }
450                                    }
451    
452                                    notify_child_exit = 2;
453                                    tm_notify_child_exit = time(NULL);
454                            }
455                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
456                            {
457                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
458                                    SYS_child_process_count = 0;
459                            }
460                  }                  }
461    
462                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
# Line 423  int net_server(const char *hostaddr, in_ Line 470  int net_server(const char *hostaddr, in_
470                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
471                          }                          }
472    
473                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
                         if (p_bbs_menu_new == NULL)  
                         {  
                                 log_error("OOM: calloc(MENU_SET)\n");  
                         }  
                         else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)  
474                          {                          {
475                                  unload_menu(p_bbs_menu_new);                                  unload_menu(&bbs_menu_new);
                                 free(p_bbs_menu_new);  
                                 p_bbs_menu_new = NULL;  
   
476                                  log_error("Reload menu failed\n");                                  log_error("Reload menu failed\n");
477                          }                          }
478                          else                          else
479                          {                          {
480                                  unload_menu(p_bbs_menu);                                  unload_menu(&bbs_menu);
481                                  free(p_bbs_menu);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
   
                                 p_bbs_menu = p_bbs_menu_new;  
                                 p_bbs_menu_new = NULL;  
   
482                                  log_common("Reload menu successfully\n");                                  log_common("Reload menu successfully\n");
483                          }                          }
484    
                         sd_notify(0, "READY=1");  
                 }  
   
                 if (SYS_data_file_reload && !SYS_server_exit)  
                 {  
                         SYS_data_file_reload = 0;  
                         sd_notify(0, "RELOADING=1");  
   
485                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
486                          {                          {
487                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
# Line 462  int net_server(const char *hostaddr, in_ Line 489  int net_server(const char *hostaddr, in_
489                                          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]);
490                                  }                                  }
491                          }                          }
   
492                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
                         sd_notify(0, "READY=1");  
                 }  
493    
494                  if (SYS_section_list_reload && !SYS_server_exit)                          // Load section config and gen_ex
495                  {                          if (load_section_config_from_db(1) < 0)
                         SYS_section_list_reload = 0;  
   
                         if (section_list_loader_reload() < 0)  
496                          {                          {
497                                  log_error("ksection_list_loader_reload() failed\n");                                  log_error("load_section_config_from_db(1) error\n");
498                          }                          }
499                            else
500                            {
501                                    log_common("Reload section config and gen_ex successfully\n");
502                            }
503    
504                            sd_notify(0, "READY=1");
505                  }                  }
506    
507                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 527  int net_server(const char *hostaddr, in_ Line 554  int net_server(const char *hostaddr, in_
554    
555                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
556    
557                                          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);
558    
559                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
560                                          {                                          {


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

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