/[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.65 by sysadm, Tue Jun 24 10:01:24 2025 UTC Revision 1.87 by sysadm, Mon Nov 17 02:32:42 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.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "bbs_main.h"  #include "bbs_main.h"
15    #include "bwf.h"
16  #include "common.h"  #include "common.h"
17  #include "database.h"  #include "database.h"
18  #include "file_loader.h"  #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 "log.h"  #include "log.h"
23  #include "login.h"  #include "login.h"
24  #include "menu.h"  #include "menu.h"
25  #include "net_server.h"  #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 <pty.h>
31  #include <signal.h>  #include <signal.h>
32  #include <stdlib.h>  #include <stdlib.h>
33  #include <string.h>  #include <string.h>
34  #include <unistd.h>  #include <unistd.h>
35    #include <utmp.h>
36  #include <arpa/inet.h>  #include <arpa/inet.h>
37  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
38  #include <libssh/libssh.h>  #include <libssh/libssh.h>
# Line 42  Line 43 
43  #include <sys/syscall.h>  #include <sys/syscall.h>
44  #include <sys/types.h>  #include <sys/types.h>
45  #include <sys/wait.h>  #include <sys/wait.h>
46    
47    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
48  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
49    #endif
50    
51  struct process_sockaddr_t  enum _net_server_constant_t
52  {  {
53          pid_t pid;          WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second
54          in_addr_t s_addr;          WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second
 };  
 typedef struct process_sockaddr_t PROCESS_SOCKADDR;  
55    
56  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
57    };
 #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds  
58    
59  struct ssl_server_cb_data_t  /* A userdata struct for session. */
60    struct session_data_struct
61  {  {
62          int tries;          int tries;
63          int error;          int error;
64  };  };
65    
66    /* A userdata struct for channel. */
67    struct channel_data_struct
68    {
69            /* pid of the child process the channel will spawn. */
70            pid_t pid;
71            /* For PTY allocation */
72            socket_t pty_master;
73            socket_t pty_slave;
74            /* For communication with the child process. */
75            socket_t child_stdin;
76            socket_t child_stdout;
77            /* Only used for subsystem and exec requests. */
78            socket_t child_stderr;
79            /* Event which is used to poll the above descriptors. */
80            ssh_event event;
81            /* Terminal size struct. */
82            struct winsize *winsize;
83    };
84    
85    static int socket_server[2];
86    static int socket_client;
87    static int epollfd_server = -1;
88    static ssh_bind sshbind;
89    
90    static HASH_DICT *hash_dict_pid_sockaddr = NULL;
91    static HASH_DICT *hash_dict_sockaddr_count = NULL;
92    
93    static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
94    
95  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
96                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
97  {  {
98          struct ssl_server_cb_data_t *p_data = userdata;          struct session_data_struct *sdata = (struct session_data_struct *)userdata;
99          int ret;          int ret;
100    
101          if (strcmp(user, "guest") == 0)          if (strcmp(user, "guest") == 0)
# Line 81  static int auth_password(ssh_session ses Line 112  static int auth_password(ssh_session ses
112                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
113          }          }
114    
115          if ((++(p_data->tries)) >= BBS_login_retry_times)          if ((++(sdata->tries)) >= BBS_login_retry_times)
116          {          {
117                  p_data->error = 1;                  sdata->error = 1;
118          }          }
119    
120          return SSH_AUTH_DENIED;          return SSH_AUTH_DENIED;
121  }  }
122    
123  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,
124                                             int x, int y, int px, int py, void *userdata)                                             int cols, int rows, int px, int py, void *userdata)
125  {  {
126          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
127            int rc;
128    
129            (void)session;
130            (void)channel;
131            (void)term;
132    
133            cdata->winsize->ws_row = (unsigned short int)rows;
134            cdata->winsize->ws_col = (unsigned short int)cols;
135            cdata->winsize->ws_xpixel = (unsigned short int)px;
136            cdata->winsize->ws_ypixel = (unsigned short int)py;
137    
138            rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);
139            if (rc != 0)
140            {
141                    log_error("Failed to open pty\n");
142                    return SSH_ERROR;
143            }
144    
145            return SSH_OK;
146    }
147    
148    static int pty_resize(ssh_session session, ssh_channel channel, int cols, int rows,
149                                              int py, int px, void *userdata)
150    {
151            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
152    
153            (void)session;
154            (void)channel;
155    
156            cdata->winsize->ws_row = (unsigned short int)rows;
157            cdata->winsize->ws_col = (unsigned short int)cols;
158            cdata->winsize->ws_xpixel = (unsigned short int)px;
159            cdata->winsize->ws_ypixel = (unsigned short int)py;
160    
161            if (cdata->pty_master != -1)
162            {
163                    return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
164            }
165    
166            return SSH_ERROR;
167    }
168    
169    static int exec_pty(const char *mode, const char *command, struct channel_data_struct *cdata)
170    {
171            (void)cdata;
172    
173            if (command != NULL)
174            {
175                    log_error("Forbid exec /bin/sh %s %s)\n", mode, command);
176            }
177    
178            return SSH_OK;
179    }
180    
181    static int exec_nopty(const char *command, struct channel_data_struct *cdata)
182    {
183            (void)cdata;
184    
185            if (command != NULL)
186            {
187                    log_error("Forbid exec /bin/sh -c %s)\n", command);
188            }
189    
190            return SSH_OK;
191    }
192    
193    static int exec_request(ssh_session session, ssh_channel channel, const char *command, void *userdata)
194    {
195            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
196    
197            (void)session;
198            (void)channel;
199    
200            if (cdata->pid > 0)
201            {
202                    return SSH_ERROR;
203            }
204    
205            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
206            {
207                    return exec_pty("-c", command, cdata);
208            }
209            return exec_nopty(command, cdata);
210  }  }
211    
212  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
213  {  {
214          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
215    
216            (void)session;
217            (void)channel;
218    
219            if (cdata->pid > 0)
220            {
221                    return SSH_ERROR;
222            }
223    
224            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
225            {
226                    return exec_pty("-l", NULL, cdata);
227            }
228            /* Client requested a shell without a pty, let's pretend we allow that */
229            return SSH_OK;
230  }  }
231    
232  static struct ssh_channel_callbacks_struct channel_cb = {  static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
233          .channel_pty_request_function = pty_request,  {
234          .channel_shell_request_function = shell_request};          (void)session;
235            (void)channel;
236    
237            log_error("subsystem_request(subsystem=%s)\n", subsystem);
238    
239  static ssh_channel new_session_channel(ssh_session session, void *userdata)          /* subsystem requests behave similarly to exec requests. */
240            if (strcmp(subsystem, "sftp") == 0)
241            {
242                    return exec_request(session, channel, SFTP_SERVER_PATH, userdata);
243            }
244            return SSH_ERROR;
245    }
246    
247    static ssh_channel channel_open(ssh_session session, void *userdata)
248  {  {
249            (void)userdata;
250    
251          if (SSH_channel != NULL)          if (SSH_channel != NULL)
252          {          {
253                  return NULL;                  return NULL;
254          }          }
255    
256          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);  
257    
258          return SSH_channel;          return SSH_channel;
259  }  }
# Line 121  static ssh_channel new_session_channel(s Line 261  static ssh_channel new_session_channel(s
261  static int fork_server(void)  static int fork_server(void)
262  {  {
263          ssh_event event;          ssh_event event;
264            long int ssh_timeout = 0;
265          int pid;          int pid;
266          int i;          int i;
267          int ret;          int ret;
268    
269          struct ssl_server_cb_data_t cb_data = {          /* Structure for storing the pty size. */
270            struct winsize wsize = {
271                    .ws_row = 0,
272                    .ws_col = 0,
273                    .ws_xpixel = 0,
274                    .ws_ypixel = 0};
275    
276            /* Our struct holding information about the channel. */
277            struct channel_data_struct cdata = {
278                    .pid = 0,
279                    .pty_master = -1,
280                    .pty_slave = -1,
281                    .child_stdin = -1,
282                    .child_stdout = -1,
283                    .child_stderr = -1,
284                    .event = NULL,
285                    .winsize = &wsize};
286    
287            struct session_data_struct cb_data = {
288                  .tries = 0,                  .tries = 0,
289                  .error = 0,                  .error = 0,
290          };          };
291    
292          struct ssh_server_callbacks_struct cb = {          struct ssh_channel_callbacks_struct channel_cb = {
293                    .userdata = &cdata,
294                    .channel_pty_request_function = pty_request,
295                    .channel_pty_window_change_function = pty_resize,
296                    .channel_shell_request_function = shell_request,
297                    .channel_exec_request_function = exec_request,
298                    .channel_subsystem_request_function = subsystem_request};
299    
300            struct ssh_server_callbacks_struct server_cb = {
301                  .userdata = &cb_data,                  .userdata = &cb_data,
302                  .auth_password_function = auth_password,                  .auth_password_function = auth_password,
303                  .channel_open_request_session_function = new_session_channel,                  .channel_open_request_session_function = channel_open,
304          };          };
305    
306          pid = fork();          pid = fork();
# Line 151  static int fork_server(void) Line 318  static int fork_server(void)
318          }          }
319    
320          // Child process          // Child process
321            if (close(epollfd_server) < 0)
322            {
323                    log_error("close(epollfd_server) error (%d)\n");
324            }
325    
326          if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)          for (i = 0; i < 2; i++)
327          {          {
328                  log_error("Close server socket failed\n");                  if (close(socket_server[i]) == -1)
329                    {
330                            log_error("Close server socket failed\n");
331                    }
332          }          }
333    
334            hash_dict_destroy(hash_dict_pid_sockaddr);
335            hash_dict_destroy(hash_dict_sockaddr_count);
336    
337          SSH_session = ssh_new();          SSH_session = ssh_new();
338    
339          if (SSH_v2)          if (SSH_v2)
# Line 169  static int fork_server(void) Line 346  static int fork_server(void)
346    
347                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
348    
349                  ssh_callbacks_init(&cb);                  ssh_timeout = 60; // second
350                  ssh_set_server_callbacks(SSH_session, &cb);                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
351                    {
352                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
353                            goto cleanup;
354                    }
355    
356                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
357    
358                    ssh_callbacks_init(&server_cb);
359                    ssh_callbacks_init(&channel_cb);
360    
361                    ssh_set_server_callbacks(SSH_session, &server_cb);
362    
363                  if (ssh_handle_key_exchange(SSH_session))                  if (ssh_handle_key_exchange(SSH_session))
364                  {                  {
365                          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));
366                          goto cleanup;                          goto cleanup;
367                  }                  }
                 ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);  
368    
369                  event = ssh_event_new();                  event = ssh_event_new();
370                  ssh_event_add_session(event, SSH_session);                  ssh_event_add_session(event, SSH_session);
# Line 187  static int fork_server(void) Line 374  static int fork_server(void)
374                          ret = ssh_event_dopoll(event, 100); // 0.1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
375                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
376                          {                          {
377    #ifdef _DEBUG
378                                  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));
379    #endif
380                                  goto cleanup;                                  goto cleanup;
381                          }                          }
382                  }                  }
# Line 197  static int fork_server(void) Line 386  static int fork_server(void)
386                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times\n", cb_data.tries);
387                          goto cleanup;                          goto cleanup;
388                  }                  }
389    
390                    ssh_set_channel_callbacks(SSH_channel, &channel_cb);
391    
392                    do
393                    {
394                            ret = ssh_event_dopoll(event, 100); // 0.1 second
395                            if (ret == SSH_ERROR)
396                            {
397                                    ssh_channel_close(SSH_channel);
398                            }
399    
400                            if (ret == SSH_AGAIN) // loop until SSH connection is fully established
401                            {
402                                    /* Executed only once, once the child process starts. */
403                                    cdata.event = event;
404                                    break;
405                            }
406                    } while (ssh_channel_is_open(SSH_channel));
407    
408                    ssh_timeout = 0;
409                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
410                    {
411                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
412                            goto cleanup;
413                    }
414          }          }
415    
416          // Redirect Input          // Redirect Input
         close(STDIN_FILENO);  
417          if (dup2(socket_client, STDIN_FILENO) == -1)          if (dup2(socket_client, STDIN_FILENO) == -1)
418          {          {
419                  log_error("Redirect stdin to client socket failed\n");                  log_error("Redirect stdin to client socket failed\n");
# Line 208  static int fork_server(void) Line 421  static int fork_server(void)
421          }          }
422    
423          // Redirect Output          // Redirect Output
         close(STDOUT_FILENO);  
424          if (dup2(socket_client, STDOUT_FILENO) == -1)          if (dup2(socket_client, STDOUT_FILENO) == -1)
425          {          {
426                  log_error("Redirect stdout to client socket failed\n");                  log_error("Redirect stdout to client socket failed\n");
427                  goto cleanup;                  goto cleanup;
428          }          }
429    
430            if (io_init() < 0)
431            {
432                    log_error("io_init() error\n");
433                    goto cleanup;
434            }
435    
436          SYS_child_process_count = 0;          SYS_child_process_count = 0;
437    
438          bbs_main();          bbs_main();
# Line 225  cleanup: Line 443  cleanup:
443    
444          if (SSH_v2)          if (SSH_v2)
445          {          {
446                    if (cdata.pty_master != -1)
447                    {
448                            close(cdata.pty_master);
449                    }
450                    if (cdata.child_stdin != -1)
451                    {
452                            close(cdata.child_stdin);
453                    }
454                    if (cdata.child_stdout != -1)
455                    {
456                            close(cdata.child_stdout);
457                    }
458                    if (cdata.child_stderr != -1)
459                    {
460                            close(cdata.child_stderr);
461                    }
462    
463                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
464                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
465          }          }
# Line 237  cleanup: Line 472  cleanup:
472          ssh_finalize();          ssh_finalize();
473    
474          // Close Input and Output for client          // Close Input and Output for client
475            io_cleanup();
476          close(STDIN_FILENO);          close(STDIN_FILENO);
477          close(STDOUT_FILENO);          close(STDOUT_FILENO);
478    
# Line 252  int net_server(const char *hostaddr, in_ Line 488  int net_server(const char *hostaddr, in_
488  {  {
489          unsigned int addrlen;          unsigned int addrlen;
490          int ret;          int ret;
491          int flags[2];          int flags_server[2];
492          struct sockaddr_in sin;          struct sockaddr_in sin;
493          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
494          int nfds, epollfd;          int nfds;
495          siginfo_t siginfo;          siginfo_t siginfo;
496          int sd_notify_stopping = 0;          int notify_child_exit = 0;
497          MENU_SET *p_bbs_menu_new;          time_t tm_notify_child_exit = time(NULL);
498            MENU_SET bbs_menu_new;
499            MENU_SET top10_menu_new;
500          int i, j;          int i, j;
501          pid_t pid;          pid_t pid;
502          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
503    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
504            int sd_notify_stopping = 0;
505    #endif
506    
507          ssh_init();          ssh_init();
508    
# Line 278  int net_server(const char *hostaddr, in_ Line 519  int net_server(const char *hostaddr, in_
519                  return -1;                  return -1;
520          }          }
521    
522          epollfd = epoll_create1(0);          epollfd_server = epoll_create1(0);
523          if (epollfd < 0)          if (epollfd_server == -1)
524          {          {
525                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)\n", errno);
526                  return -1;                  return -1;
# Line 301  int net_server(const char *hostaddr, in_ Line 542  int net_server(const char *hostaddr, in_
542                  sin.sin_port = htons(port[i]);                  sin.sin_port = htons(port[i]);
543    
544                  // Reuse address and port                  // Reuse address and port
545                  flags[i] = 1;                  flags_server[i] = 1;
546                  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)
547                  {                  {
548                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
549                  }                  }
550                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)
551                  {                  {
552                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
553                  }                  }
# Line 328  int net_server(const char *hostaddr, in_ Line 569  int net_server(const char *hostaddr, in_
569    
570                  ev.events = EPOLLIN;                  ev.events = EPOLLIN;
571                  ev.data.fd = socket_server[i];                  ev.data.fd = socket_server[i];
572                  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)
573                  {                  {
574                          log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);                          log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
575                          if (close(epollfd) < 0)                          if (close(epollfd_server) < 0)
576                          {                          {
577                                  log_error("close(epoll) error (%d)\n");                                  log_error("close(epoll) error (%d)\n");
578                          }                          }
579                          return -1;                          return -1;
580                  }                  }
581    
582                  flags[i] = fcntl(socket_server[i], F_GETFL, 0);                  flags_server[i] = fcntl(socket_server[i], F_GETFL, 0);
583                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);
584            }
585    
586            hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
587            if (hash_dict_pid_sockaddr == NULL)
588            {
589                    log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n");
590                    return -1;
591            }
592            hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT);
593            if (hash_dict_sockaddr_count == NULL)
594            {
595                    log_error("hash_dict_create(hash_dict_sockaddr_count) error\n");
596                    return -1;
597          }          }
598    
599          // Startup complete          // Startup complete
600    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
601          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
602                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
603                                    "MAINPID=%d",                                    "MAINPID=%d",
604                             hostaddr, port[0], hostaddr, port[1], getpid());                             hostaddr, port[0], hostaddr, port[1], getpid());
605    #endif
606    
607          while (!SYS_server_exit || SYS_child_process_count > 0)          while (!SYS_server_exit || SYS_child_process_count > 0)
608          {          {
609    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
610                  if (SYS_server_exit && !sd_notify_stopping)                  if (SYS_server_exit && !sd_notify_stopping)
611                  {                  {
612                          sd_notify(0, "STOPPING=1");                          sd_notify(0, "STOPPING=1");
613                          sd_notify_stopping = 1;                          sd_notify_stopping = 1;
614                  }                  }
615    #endif
616    
617                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
618                  {                  {
# Line 371  int net_server(const char *hostaddr, in_ Line 629  int net_server(const char *hostaddr, in_
629    
630                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (siginfo.si_pid != section_list_loader_pid)
631                                  {                                  {
632                                          i = 0;                                          j = 0;
633                                          for (; i < BBS_max_client; i++)                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);
634                                            if (ret < 0)
635                                          {                                          {
636                                                  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;  
                                                 }  
637                                          }                                          }
638                                          if (i >= BBS_max_client)                                          else
639                                          {                                          {
640                                                  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);
641                                                    if (ret < 0)
642                                                    {
643                                                            log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);
644                                                    }
645    
646                                                    ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);
647                                                    if (ret < 0)
648                                                    {
649                                                            log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
650                                                    }
651                                          }                                          }
652                                  }                                  }
653                          }                          }
# Line 399  int net_server(const char *hostaddr, in_ Line 664  int net_server(const char *hostaddr, in_
664    
665                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
666                  {                  {
667                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
668                          {                          {
669                                  log_error("Send SIGTERM signal failed (%d)\n", errno);  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
670                                    sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
671                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
672    #endif
673    
674                                    if (kill(-getpid(), SIGTERM) < 0)
675                                    {
676                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
677                                    }
678    
679                                    notify_child_exit = 1;
680                                    tm_notify_child_exit = time(NULL);
681                          }                          }
682                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
683                            {
684    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
685                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
686    #endif
687    
688                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  if (kill(-getpid(), SIGKILL) < 0)
689                                    {
690                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
691                                    }
692    
693                                    notify_child_exit = 2;
694                                    tm_notify_child_exit = time(NULL);
695                            }
696                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
697                            {
698                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
699                                    SYS_child_process_count = 0;
700                            }
701                  }                  }
702    
703                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
704                  {                  {
705                          SYS_conf_reload = 0;                          SYS_conf_reload = 0;
706    
707    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
708                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
709    #endif
710    
711                            // Restart log
712                            if (log_restart() < 0)
713                            {
714                                    log_error("Restart logging failed\n");
715                            }
716    
717                          // Reload configuration                          // Reload configuration
718                          if (load_conf(CONF_BBSD) < 0)                          if (load_conf(CONF_BBSD) < 0)
# Line 419  int net_server(const char *hostaddr, in_ Line 720  int net_server(const char *hostaddr, in_
720                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
721                          }                          }
722    
723                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          // Reload BWF config
724                          if (p_bbs_menu_new == NULL)                          if (bwf_load(CONF_BWF) < 0)
725                          {                          {
726                                  log_error("OOM: calloc(MENU_SET)\n");                                  log_error("Reload BWF conf failed\n");
727                          }                          }
                         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;  
728    
729                                  log_error("Reload menu failed\n");                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
730                            {
731                                    unload_menu(&bbs_menu_new);
732                                    log_error("Reload bbs menu failed\n");
733                          }                          }
734                          else                          else
735                          {                          {
736                                  unload_menu(p_bbs_menu);                                  unload_menu(&bbs_menu);
737                                  free(p_bbs_menu);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
738                                    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");  
739                          }                          }
740    
741                          sd_notify(0, "READY=1");                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
742                  }                          {
743                                    unload_menu(&top10_menu_new);
744                  if (SYS_data_file_reload && !SYS_server_exit)                                  log_error("Reload top10 menu failed\n");
745                  {                          }
746                          SYS_data_file_reload = 0;                          else
747                          sd_notify(0, "RELOADING=1");                          {
748                                    unload_menu(&top10_menu);
749                                    top10_menu_new.allow_exit = 1;
750                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
751                                    log_common("Reload top10 menu successfully\n");
752                            }
753    
754                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
755                          {                          {
756                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
757                                  {                                  {
758                                          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]);
759                                  }                                  }
760                          }                          }
   
761                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
                         sd_notify(0, "READY=1");  
                 }  
762    
763                  if (SYS_section_list_reload && !SYS_server_exit)                          // Load section config and gen_ex
764                  {                          if (load_section_config_from_db(1) < 0)
765                          SYS_section_list_reload = 0;                          {
766                                    log_error("load_section_config_from_db(1) error\n");
767                            }
768                            else
769                            {
770                                    log_common("Reload section config and gen_ex successfully\n");
771                            }
772    
773                          if (section_list_loader_reload() < 0)                          // Notify child processes to reload configuration
774                            if (kill(-getpid(), SIGUSR1) < 0)
775                          {                          {
776                                  log_error("section_list_loader_reload() failed\n");                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);
777                          }                          }
778    
779    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
780                            sd_notify(0, "READY=1");
781    #endif
782                  }                  }
783    
784                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second
785    
786                  if (nfds < 0)                  if (nfds < 0)
787                  {                  {
# Line 528  int net_server(const char *hostaddr, in_ Line 836  int net_server(const char *hostaddr, in_
836                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
837                                          {                                          {
838                                                  j = 0;                                                  j = 0;
839                                                  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);
840                                                    if (ret < 0)
841                                                  {                                                  {
842                                                          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;  
                                                                 }  
                                                         }  
843                                                  }                                                  }
844    
845                                                  if (j < BBS_max_client_per_ip)                                                  if (j < BBS_max_client_per_ip)
# Line 549  int net_server(const char *hostaddr, in_ Line 850  int net_server(const char *hostaddr, in_
850                                                          }                                                          }
851                                                          else if (pid > 0)                                                          else if (pid > 0)
852                                                          {                                                          {
853                                                                  i = 0;                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
854                                                                  for (; i < BBS_max_client; i++)                                                                  if (ret < 0)
855                                                                  {                                                                  {
856                                                                          if (process_sockaddr_pool[i].pid == 0)                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);
                                                                         {  
                                                                                 break;  
                                                                         }  
857                                                                  }                                                                  }
858    
859                                                                  if (i >= BBS_max_client)                                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
860                                                                  {                                                                  if (ret < 0)
                                                                         log_error("Process sockaddr pool depleted\n");  
                                                                 }  
                                                                 else  
861                                                                  {                                                                  {
862                                                                          process_sockaddr_pool[i].pid = pid;                                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);
                                                                         process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;  
863                                                                  }                                                                  }
864                                                          }                                                          }
865                                                  }                                                  }
866                                                    else
867                                                    {
868                                                            log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip);
869                                                    }
870                                          }                                          }
871                                          else                                          else
872                                          {                                          {
# Line 584  int net_server(const char *hostaddr, in_ Line 882  int net_server(const char *hostaddr, in_
882                  }                  }
883          }          }
884    
885          if (close(epollfd) < 0)          if (close(epollfd_server) < 0)
886          {          {
887                  log_error("close(epoll) error (%d)\n");                  log_error("close(epollfd_server) error (%d)\n");
888          }          }
889    
890          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
891          {          {
                 fcntl(socket_server[i], F_SETFL, flags[i]);  
   
892                  if (close(socket_server[i]) == -1)                  if (close(socket_server[i]) == -1)
893                  {                  {
894                          log_error("Close server socket failed\n");                          log_error("Close server socket failed\n");
895                  }                  }
896          }          }
897    
898            hash_dict_destroy(hash_dict_pid_sockaddr);
899            hash_dict_destroy(hash_dict_sockaddr_count);
900    
901          ssh_bind_free(sshbind);          ssh_bind_free(sshbind);
902          ssh_finalize();          ssh_finalize();
903    


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

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