/[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.56 by sysadm, Sat Jun 7 02:38:28 2025 UTC Revision 1.85 by sysadm, Sun Nov 16 04:40:52 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    net_server.c  -  description  /*
3                                                           -------------------   * net_server
4          Copyright            : (C) 2004-2025 by Leaflet   *   - network server with SSH support
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
   
 #define _XOPEN_SOURCE 500  
 #define _POSIX_C_SOURCE 200809L  
 #define _GNU_SOURCE  
12    
 #include "net_server.h"  
 #include "common.h"  
 #include "bbs_main.h"  
13  #include "bbs.h"  #include "bbs.h"
14  #include "log.h"  #include "bbs_main.h"
15    #include "bwf.h"
16    #include "common.h"
17    #include "database.h"
18    #include "file_loader.h"
19    #include "hash_dict.h"
20  #include "io.h"  #include "io.h"
21  #include "init.h"  #include "init.h"
22  #include "menu.h"  #include "log.h"
 #include "database.h"  
23  #include "login.h"  #include "login.h"
24  #include "file_loader.h"  #include "menu.h"
25    #include "net_server.h"
26    #include "section_list.h"
27  #include "section_list_loader.h"  #include "section_list_loader.h"
28  #include <errno.h>  #include <errno.h>
29  #include <fcntl.h>  #include <fcntl.h>
30  #include <string.h>  #include <pty.h>
31  #include <signal.h>  #include <signal.h>
32  #include <stdlib.h>  #include <stdlib.h>
33    #include <string.h>
34  #include <unistd.h>  #include <unistd.h>
35  #include <sys/syscall.h>  #include <utmp.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  struct process_sockaddr_t  enum _net_server_constant_t
49  {  {
50          pid_t pid;          WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second
51          in_addr_t s_addr;          WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second
 };  
 typedef struct process_sockaddr_t PROCESS_SOCKADDR;  
52    
53  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
54    };
55    
56  #define SSH_AUTH_MAX_DURATION 60 // seconds  static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
57    
58  struct ssl_server_cb_data_t  /* A userdata struct for session. */
59    struct session_data_struct
60  {  {
61          int tries;          int tries;
62          int error;          int error;
63  };  };
64    
65    /* A userdata struct for channel. */
66    struct channel_data_struct
67    {
68            /* pid of the child process the channel will spawn. */
69            pid_t pid;
70            /* For PTY allocation */
71            socket_t pty_master;
72            socket_t pty_slave;
73            /* For communication with the child process. */
74            socket_t child_stdin;
75            socket_t child_stdout;
76            /* Only used for subsystem and exec requests. */
77            socket_t child_stderr;
78            /* Event which is used to poll the above descriptors. */
79            ssh_event event;
80            /* Terminal size struct. */
81            struct winsize *winsize;
82    };
83    
84  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
85                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
86  {  {
87          MYSQL *db;          struct session_data_struct *sdata = (struct session_data_struct *)userdata;
         struct ssl_server_cb_data_t *p_data = userdata;  
88          int ret;          int ret;
89    
         if ((db = db_open()) == NULL)  
         {  
                 return SSH_AUTH_ERROR;  
         }  
   
90          if (strcmp(user, "guest") == 0)          if (strcmp(user, "guest") == 0)
91          {          {
92                  ret = load_guest_info(db);                  ret = load_guest_info();
93          }          }
94          else          else
95          {          {
96                  ret = check_user(db, user, password);                  ret = check_user(user, password);
97          }          }
98    
         mysql_close(db);  
   
99          if (ret == 0)          if (ret == 0)
100          {          {
101                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
102          }          }
103    
104          if ((++(p_data->tries)) >= BBS_login_retry_times)          if ((++(sdata->tries)) >= BBS_login_retry_times)
105          {          {
106                  p_data->error = 1;                  sdata->error = 1;
107          }          }
108    
109          return SSH_AUTH_DENIED;          return SSH_AUTH_DENIED;
110  }  }
111    
112  static int pty_request(ssh_session session, ssh_channel channel, const char *term,  static int pty_request(ssh_session session, ssh_channel channel, const char *term,
113                                             int x, int y, int px, int py, void *userdata)                                             int cols, int rows, int px, int py, void *userdata)
114  {  {
115          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
116            int rc;
117    
118            (void)session;
119            (void)channel;
120            (void)term;
121    
122            cdata->winsize->ws_row = (unsigned short int)rows;
123            cdata->winsize->ws_col = (unsigned short int)cols;
124            cdata->winsize->ws_xpixel = (unsigned short int)px;
125            cdata->winsize->ws_ypixel = (unsigned short int)py;
126    
127            rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);
128            if (rc != 0)
129            {
130                    log_error("Failed to open pty\n");
131                    return SSH_ERROR;
132            }
133    
134            return SSH_OK;
135    }
136    
137    static int pty_resize(ssh_session session, ssh_channel channel, int cols, int rows,
138                                              int py, int px, void *userdata)
139    {
140            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
141    
142            (void)session;
143            (void)channel;
144    
145            cdata->winsize->ws_row = (unsigned short int)rows;
146            cdata->winsize->ws_col = (unsigned short int)cols;
147            cdata->winsize->ws_xpixel = (unsigned short int)px;
148            cdata->winsize->ws_ypixel = (unsigned short int)py;
149    
150            if (cdata->pty_master != -1)
151            {
152                    return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
153            }
154    
155            return SSH_ERROR;
156    }
157    
158    static int exec_pty(const char *mode, const char *command, struct channel_data_struct *cdata)
159    {
160            (void)cdata;
161    
162            if (command != NULL)
163            {
164                    log_error("Forbid exec /bin/sh %s %s)\n", mode, command);
165            }
166    
167            return SSH_OK;
168    }
169    
170    static int exec_nopty(const char *command, struct channel_data_struct *cdata)
171    {
172            (void)cdata;
173    
174            if (command != NULL)
175            {
176                    log_error("Forbid exec /bin/sh -c %s)\n", command);
177            }
178    
179            return SSH_OK;
180    }
181    
182    static int exec_request(ssh_session session, ssh_channel channel, const char *command, void *userdata)
183    {
184            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
185    
186            (void)session;
187            (void)channel;
188    
189            if (cdata->pid > 0)
190            {
191                    return SSH_ERROR;
192            }
193    
194            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
195            {
196                    return exec_pty("-c", command, cdata);
197            }
198            return exec_nopty(command, cdata);
199  }  }
200    
201  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
202  {  {
203          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
204    
205            (void)session;
206            (void)channel;
207    
208            if (cdata->pid > 0)
209            {
210                    return SSH_ERROR;
211            }
212    
213            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
214            {
215                    return exec_pty("-l", NULL, cdata);
216            }
217            /* Client requested a shell without a pty, let's pretend we allow that */
218            return SSH_OK;
219  }  }
220    
221  static struct ssh_channel_callbacks_struct channel_cb = {  static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
222          .channel_pty_request_function = pty_request,  {
223          .channel_shell_request_function = shell_request};          (void)session;
224            (void)channel;
225    
226            log_error("subsystem_request(subsystem=%s)\n", subsystem);
227    
228            /* subsystem requests behave similarly to exec requests. */
229            if (strcmp(subsystem, "sftp") == 0)
230            {
231                    return exec_request(session, channel, SFTP_SERVER_PATH, userdata);
232            }
233            return SSH_ERROR;
234    }
235    
236  static ssh_channel new_session_channel(ssh_session session, void *userdata)  static ssh_channel channel_open(ssh_session session, void *userdata)
237  {  {
238            (void)userdata;
239    
240          if (SSH_channel != NULL)          if (SSH_channel != NULL)
241          {          {
242                  return NULL;                  return NULL;
243          }          }
244    
245          SSH_channel = ssh_channel_new(session);          SSH_channel = ssh_channel_new(session);
         ssh_callbacks_init(&channel_cb);  
         ssh_set_channel_callbacks(SSH_channel, &channel_cb);  
246    
247          return SSH_channel;          return SSH_channel;
248  }  }
# Line 132  static ssh_channel new_session_channel(s Line 250  static ssh_channel new_session_channel(s
250  static int fork_server(void)  static int fork_server(void)
251  {  {
252          ssh_event event;          ssh_event event;
253            long int ssh_timeout = 0;
254          int pid;          int pid;
255          int i;          int i;
256          int ret;          int ret;
257    
258          struct ssl_server_cb_data_t cb_data = {          /* Structure for storing the pty size. */
259            struct winsize wsize = {
260                    .ws_row = 0,
261                    .ws_col = 0,
262                    .ws_xpixel = 0,
263                    .ws_ypixel = 0};
264    
265            /* Our struct holding information about the channel. */
266            struct channel_data_struct cdata = {
267                    .pid = 0,
268                    .pty_master = -1,
269                    .pty_slave = -1,
270                    .child_stdin = -1,
271                    .child_stdout = -1,
272                    .child_stderr = -1,
273                    .event = NULL,
274                    .winsize = &wsize};
275    
276            struct session_data_struct cb_data = {
277                  .tries = 0,                  .tries = 0,
278                  .error = 0,                  .error = 0,
279          };          };
280    
281          struct ssh_server_callbacks_struct cb = {          struct ssh_channel_callbacks_struct channel_cb = {
282                    .userdata = &cdata,
283                    .channel_pty_request_function = pty_request,
284                    .channel_pty_window_change_function = pty_resize,
285                    .channel_shell_request_function = shell_request,
286                    .channel_exec_request_function = exec_request,
287                    .channel_subsystem_request_function = subsystem_request};
288    
289            struct ssh_server_callbacks_struct server_cb = {
290                  .userdata = &cb_data,                  .userdata = &cb_data,
291                  .auth_password_function = auth_password,                  .auth_password_function = auth_password,
292                  .channel_open_request_session_function = new_session_channel,                  .channel_open_request_session_function = channel_open,
293          };          };
294    
295          pid = fork();          pid = fork();
# Line 180  static int fork_server(void) Line 325  static int fork_server(void)
325    
326                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
327    
328                  ssh_callbacks_init(&cb);                  ssh_timeout = 60; // second
329                  ssh_set_server_callbacks(SSH_session, &cb);                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
330                    {
331                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
332                            goto cleanup;
333                    }
334    
335                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
336    
337                    ssh_callbacks_init(&server_cb);
338                    ssh_callbacks_init(&channel_cb);
339    
340                    ssh_set_server_callbacks(SSH_session, &server_cb);
341    
342                  if (ssh_handle_key_exchange(SSH_session))                  if (ssh_handle_key_exchange(SSH_session))
343                  {                  {
344                          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));
345                          goto cleanup;                          goto cleanup;
346                  }                  }
                 ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);  
347    
348                  event = ssh_event_new();                  event = ssh_event_new();
349                  ssh_event_add_session(event, SSH_session);                  ssh_event_add_session(event, SSH_session);
350    
351                  for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i++)                  for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
352                  {                  {
353                          ret = ssh_event_dopoll(event, 1000); // 1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
354                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
355                          {                          {
356    #ifdef _DEBUG
357                                  log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));                                  log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
358    #endif
359                                  goto cleanup;                                  goto cleanup;
360                          }                          }
361                  }                  }
# Line 208  static int fork_server(void) Line 365  static int fork_server(void)
365                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times\n", cb_data.tries);
366                          goto cleanup;                          goto cleanup;
367                  }                  }
368    
369                    ssh_set_channel_callbacks(SSH_channel, &channel_cb);
370    
371                    do
372                    {
373                            ret = ssh_event_dopoll(event, 100); // 0.1 second
374                            if (ret == SSH_ERROR)
375                            {
376                                    ssh_channel_close(SSH_channel);
377                            }
378    
379                            if (ret == SSH_AGAIN) // loop until SSH connection is fully established
380                            {
381                                    /* Executed only once, once the child process starts. */
382                                    cdata.event = event;
383                                    break;
384                            }
385                    } while (ssh_channel_is_open(SSH_channel));
386    
387                    ssh_timeout = 0;
388                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
389                    {
390                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
391                            goto cleanup;
392                    }
393          }          }
394    
395          // Redirect Input          // Redirect Input
         close(STDIN_FILENO);  
396          if (dup2(socket_client, STDIN_FILENO) == -1)          if (dup2(socket_client, STDIN_FILENO) == -1)
397          {          {
398                  log_error("Redirect stdin to client socket failed\n");                  log_error("Redirect stdin to client socket failed\n");
# Line 219  static int fork_server(void) Line 400  static int fork_server(void)
400          }          }
401    
402          // Redirect Output          // Redirect Output
         close(STDOUT_FILENO);  
403          if (dup2(socket_client, STDOUT_FILENO) == -1)          if (dup2(socket_client, STDOUT_FILENO) == -1)
404          {          {
405                  log_error("Redirect stdout to client socket failed\n");                  log_error("Redirect stdout to client socket failed\n");
# Line 236  cleanup: Line 416  cleanup:
416    
417          if (SSH_v2)          if (SSH_v2)
418          {          {
419                    close(cdata.pty_master);
420                    close(cdata.child_stdin);
421                    close(cdata.child_stdout);
422                    close(cdata.child_stderr);
423    
424                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
425                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
426          }          }
# Line 261  cleanup: Line 446  cleanup:
446    
447  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
448  {  {
449            HASH_DICT *hash_dict_pid_sockaddr = NULL;
450            HASH_DICT *hash_dict_sockaddr_count = NULL;
451    
452          unsigned int addrlen;          unsigned int addrlen;
453          int ret;          int ret;
454          int flags[2];          int flags[2];
# Line 268  int net_server(const char *hostaddr, in_ Line 456  int net_server(const char *hostaddr, in_
456          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
457          int nfds, epollfd;          int nfds, epollfd;
458          siginfo_t siginfo;          siginfo_t siginfo;
459            int notify_child_exit = 0;
460            time_t tm_notify_child_exit = time(NULL);
461          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
462          MENU_SET *p_bbs_menu_new;          MENU_SET bbs_menu_new;
463            MENU_SET top10_menu_new;
464          int i, j;          int i, j;
465          pid_t pid;          pid_t pid;
466          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 281  int net_server(const char *hostaddr, in_ Line 472  int net_server(const char *hostaddr, in_
472          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
473                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
474                  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 ||
475                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
476                  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)
477          {          {
478                  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 352  int net_server(const char *hostaddr, in_ Line 544  int net_server(const char *hostaddr, in_
544                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
545          }          }
546    
547            hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
548            if (hash_dict_pid_sockaddr == NULL)
549            {
550                    log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n");
551                    return -1;
552            }
553            hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT);
554            if (hash_dict_sockaddr_count == NULL)
555            {
556                    log_error("hash_dict_create(hash_dict_sockaddr_count) error\n");
557                    return -1;
558            }
559    
560          // Startup complete          // Startup complete
561          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
562                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
# Line 381  int net_server(const char *hostaddr, in_ Line 586  int net_server(const char *hostaddr, in_
586    
587                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (siginfo.si_pid != section_list_loader_pid)
588                                  {                                  {
589                                          i = 0;                                          j = 0;
590                                          for (; i < BBS_max_client; i++)                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);
591                                            if (ret < 0)
592                                          {                                          {
593                                                  if (process_sockaddr_pool[i].pid == siginfo.si_pid)                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
                                                 {  
                                                         process_sockaddr_pool[i].pid = 0;  
                                                         break;  
                                                 }  
594                                          }                                          }
595                                          if (i >= BBS_max_client)                                          else
596                                          {                                          {
597                                                  log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1);
598                                                    if (ret < 0)
599                                                    {
600                                                            log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);
601                                                    }
602    
603                                                    ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);
604                                                    if (ret < 0)
605                                                    {
606                                                            log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
607                                                    }
608                                          }                                          }
609                                  }                                  }
610                          }                          }
# Line 409  int net_server(const char *hostaddr, in_ Line 621  int net_server(const char *hostaddr, in_
621    
622                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
623                  {                  {
624                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
625                          {                          {
626                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
627                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
628    
629                                    if (kill(-getpid(), SIGTERM) < 0)
630                                    {
631                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
632                                    }
633    
634                                    notify_child_exit = 1;
635                                    tm_notify_child_exit = time(NULL);
636                          }                          }
637                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
638                            {
639                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
640    
641                                    if (kill(-getpid(), SIGKILL) < 0)
642                                    {
643                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
644                                    }
645    
646                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  notify_child_exit = 2;
647                                    tm_notify_child_exit = time(NULL);
648                            }
649                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
650                            {
651                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
652                                    SYS_child_process_count = 0;
653                            }
654                  }                  }
655    
656                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
# Line 423  int net_server(const char *hostaddr, in_ Line 658  int net_server(const char *hostaddr, in_
658                          SYS_conf_reload = 0;                          SYS_conf_reload = 0;
659                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
660    
661                            // Restart log
662                            if (log_restart() < 0)
663                            {
664                                    log_error("Restart logging failed\n");
665                            }
666    
667                          // Reload configuration                          // Reload configuration
668                          if (load_conf(CONF_BBSD) < 0)                          if (load_conf(CONF_BBSD) < 0)
669                          {                          {
670                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
671                          }                          }
672    
673                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          // Reload BWF config
674                          if (p_bbs_menu_new == NULL)                          if (bwf_load(CONF_BWF) < 0)
675                          {                          {
676                                  log_error("OOM: calloc(MENU_SET)\n");                                  log_error("Reload BWF conf failed\n");
677                          }                          }
                         else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)  
                         {  
                                 unload_menu(p_bbs_menu_new);  
                                 free(p_bbs_menu_new);  
                                 p_bbs_menu_new = NULL;  
678    
679                                  log_error("Reload menu failed\n");                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
680                            {
681                                    unload_menu(&bbs_menu_new);
682                                    log_error("Reload bbs menu failed\n");
683                          }                          }
684                          else                          else
685                          {                          {
686                                  unload_menu(p_bbs_menu);                                  unload_menu(&bbs_menu);
687                                  free(p_bbs_menu);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
688                                    log_common("Reload bbs menu successfully\n");
                                 p_bbs_menu = p_bbs_menu_new;  
                                 p_bbs_menu_new = NULL;  
   
                                 log_common("Reload menu successfully\n");  
689                          }                          }
690    
691                          sd_notify(0, "READY=1");                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
692                  }                          {
693                                    unload_menu(&top10_menu_new);
694                  if (SYS_data_file_reload && !SYS_server_exit)                                  log_error("Reload top10 menu failed\n");
695                  {                          }
696                          SYS_data_file_reload = 0;                          else
697                          sd_notify(0, "RELOADING=1");                          {
698                                    unload_menu(&top10_menu);
699                                    top10_menu_new.allow_exit = 1;
700                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
701                                    log_common("Reload top10 menu successfully\n");
702                            }
703    
704                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
705                          {                          {
706                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
707                                  {                                  {
708                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);
709                                  }                                  }
710                          }                          }
   
711                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
                         sd_notify(0, "READY=1");  
                 }  
712    
713                  if (SYS_section_list_reload && !SYS_server_exit)                          // Load section config and gen_ex
714                  {                          if (load_section_config_from_db(1) < 0)
715                          SYS_section_list_reload = 0;                          {
716                                    log_error("load_section_config_from_db(1) error\n");
717                            }
718                            else
719                            {
720                                    log_common("Reload section config and gen_ex successfully\n");
721                            }
722    
723                          if (section_list_loader_reload() < 0)                          // Notify child processes to reload configuration
724                            if (kill(-getpid(), SIGUSR1) < 0)
725                          {                          {
726                                  log_error("ksection_list_loader_reload() failed\n");                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);
727                          }                          }
728    
729                            sd_notify(0, "READY=1");
730                  }                  }
731    
732                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 533  int net_server(const char *hostaddr, in_ Line 779  int net_server(const char *hostaddr, in_
779    
780                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
781    
782                                          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);
783    
784                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
785                                          {                                          {
786                                                  j = 0;                                                  j = 0;
787                                                  for (i = 0; i < BBS_max_client; i++)                                                  ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j);
788                                                    if (ret < 0)
789                                                  {                                                  {
790                                                          if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client);
                                                         {  
                                                                 j++;  
                                                                 if (j >= BBS_max_client_per_ip)  
                                                                 {  
                                                                         log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);  
                                                                         break;  
                                                                 }  
                                                         }  
791                                                  }                                                  }
792    
793                                                  if (j < BBS_max_client_per_ip)                                                  if (j < BBS_max_client_per_ip)
# Line 559  int net_server(const char *hostaddr, in_ Line 798  int net_server(const char *hostaddr, in_
798                                                          }                                                          }
799                                                          else if (pid > 0)                                                          else if (pid > 0)
800                                                          {                                                          {
801                                                                  i = 0;                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
802                                                                  for (; i < BBS_max_client; i++)                                                                  if (ret < 0)
803                                                                  {                                                                  {
804                                                                          if (process_sockaddr_pool[i].pid == 0)                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);
                                                                         {  
                                                                                 break;  
                                                                         }  
805                                                                  }                                                                  }
806    
807                                                                  if (i >= BBS_max_client)                                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
808                                                                    if (ret < 0)
809                                                                  {                                                                  {
810                                                                          log_error("Process sockaddr pool depleted\n");                                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);
                                                                 }  
                                                                 else  
                                                                 {  
                                                                         process_sockaddr_pool[i].pid = pid;  
                                                                         process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;  
811                                                                  }                                                                  }
812                                                          }                                                          }
813                                                  }                                                  }
814                                                    else
815                                                    {
816                                                            log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip);
817                                                    }
818                                          }                                          }
819                                          else                                          else
820                                          {                                          {
# Line 609  int net_server(const char *hostaddr, in_ Line 845  int net_server(const char *hostaddr, in_
845                  }                  }
846          }          }
847    
848            hash_dict_destroy(hash_dict_pid_sockaddr);
849            hash_dict_destroy(hash_dict_sockaddr_count);
850    
851          ssh_bind_free(sshbind);          ssh_bind_free(sshbind);
852          ssh_finalize();          ssh_finalize();
853    


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

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