/[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.34 by sysadm, Thu May 15 06:24:11 2025 UTC Revision 1.46 by sysadm, Tue Jun 3 05:33:06 2025 UTC
# Line 22  Line 22 
22  #include "common.h"  #include "common.h"
23  #include "log.h"  #include "log.h"
24  #include "io.h"  #include "io.h"
25    #include "init.h"
26  #include "fork.h"  #include "fork.h"
27  #include "menu.h"  #include "menu.h"
28    #include "file_loader.h"
29    #include "section_list_loader.h"
30  #include <errno.h>  #include <errno.h>
31  #include <fcntl.h>  #include <fcntl.h>
32  #include <string.h>  #include <string.h>
# Line 36  Line 39 
39  #include <sys/epoll.h>  #include <sys/epoll.h>
40  #include <arpa/inet.h>  #include <arpa/inet.h>
41  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
42    #include <libssh/libssh.h>
43    #include <libssh/server.h>
44    
45    struct process_sockaddr_t
46    {
47            pid_t pid;
48            in_addr_t s_addr;
49    };
50    typedef struct process_sockaddr_t PROCESS_SOCKADDR;
51    
52    static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENTS_LIMIT];
53    
54  int net_server(const char *hostaddr, in_port_t port)  int net_server(const char *hostaddr, in_port_t port)
55  {  {
# Line 48  int net_server(const char *hostaddr, in_ Line 62  int net_server(const char *hostaddr, in_
62          siginfo_t siginfo;          siginfo_t siginfo;
63          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
64          MENU_SET *p_bbs_menu_new;          MENU_SET *p_bbs_menu_new;
65            int i, j;
66            pid_t pid;
67    
68          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
69    
# Line 128  int net_server(const char *hostaddr, in_ Line 144  int net_server(const char *hostaddr, in_
144                          sd_notify(0, "STOPPING=1");                          sd_notify(0, "STOPPING=1");
145                          sd_notify_stopping = 1;                          sd_notify_stopping = 1;
146                  }                  }
147    
148                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
149                  {                  {
150                          SYS_child_exit = 0;                          SYS_child_exit = 0;
# Line 140  int net_server(const char *hostaddr, in_ Line 157  int net_server(const char *hostaddr, in_
157    
158                                  SYS_child_process_count--;                                  SYS_child_process_count--;
159                                  log_std("Child process (%d) exited\n", siginfo.si_pid);                                  log_std("Child process (%d) exited\n", siginfo.si_pid);
160    
161                                    if (siginfo.si_pid != section_list_loader_pid)
162                                    {
163                                            i = 0;
164                                            for (; i < BBS_max_client; i++)
165                                            {
166                                                    if (process_sockaddr_pool[i].pid == siginfo.si_pid)
167                                                    {
168                                                            process_sockaddr_pool[i].pid = 0;
169                                                            break;
170                                                    }
171                                            }
172                                            if (i >= BBS_max_client)
173                                            {
174                                                    log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);
175                                            }
176                                    }
177                          }                          }
178                          else if (ret == 0)                          else if (ret == 0)
179                          {                          {
# Line 163  int net_server(const char *hostaddr, in_ Line 197  int net_server(const char *hostaddr, in_
197                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);
198                  }                  }
199    
200                  if (SYS_menu_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
201                  {                  {
202                          SYS_menu_reload = 0;                          SYS_conf_reload = 0;
203                            sd_notify(0, "RELOADING=1");
204    
205                            // Reload configuration
206                            if (load_conf(CONF_BBSD) < 0)
207                            {
208                                    log_error("Reload conf failed\n");
209                            }
210    
211                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));
212                          if (p_bbs_menu_new == NULL)                          if (p_bbs_menu_new == NULL)
# Line 176  int net_server(const char *hostaddr, in_ Line 217  int net_server(const char *hostaddr, in_
217                          {                          {
218                                  unload_menu(p_bbs_menu_new);                                  unload_menu(p_bbs_menu_new);
219                                  free(p_bbs_menu_new);                                  free(p_bbs_menu_new);
220                                    p_bbs_menu_new = NULL;
221    
222                                  log_error("Reload menu failed\n");                                  log_error("Reload menu failed\n");
223                          }                          }
224                          else                          else
225                          {                          {
                                 unload_menu_shm(p_bbs_menu_new);  
   
226                                  unload_menu(p_bbs_menu);                                  unload_menu(p_bbs_menu);
227                                  free(p_bbs_menu);                                  free(p_bbs_menu);
228    
# Line 191  int net_server(const char *hostaddr, in_ Line 231  int net_server(const char *hostaddr, in_
231    
232                                  log_std("Reload menu successfully\n");                                  log_std("Reload menu successfully\n");
233                          }                          }
234    
235                            sd_notify(0, "READY=1");
236                    }
237    
238                    if (SYS_data_file_reload && !SYS_server_exit)
239                    {
240                            SYS_data_file_reload = 0;
241                            sd_notify(0, "RELOADING=1");
242    
243                            for (int i = 0; i < data_files_load_startup_count; i++)
244                            {
245                                    if (load_file(data_files_load_startup[i]) < 0)
246                                    {
247                                            log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
248                                    }
249                            }
250    
251                            log_std("Reload data files successfully\n");
252                            sd_notify(0, "READY=1");
253                    }
254    
255                    if (SYS_section_list_reload && !SYS_server_exit)
256                    {
257                            SYS_section_list_reload = 0;
258    
259                            if (section_list_loader_reload() < 0)
260                            {
261                                    log_error("ksection_list_loader_reload() failed\n");
262                            }
263                  }                  }
264    
265                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 242  int net_server(const char *hostaddr, in_ Line 311  int net_server(const char *hostaddr, in_
311    
312                                          log_std("Accept connection from %s:%d\n", hostaddr_client, port_client);                                          log_std("Accept connection from %s:%d\n", hostaddr_client, port_client);
313    
314                                          if (fork_server() < 0)                                          if (SYS_child_process_count - 1 < BBS_max_client)
315                                            {
316                                                    j = 0;
317                                                    for (i = 0; i < BBS_max_client; i++)
318                                                    {
319                                                            if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)
320                                                            {
321                                                                    j++;
322                                                                    if (j >= BBS_max_client_per_ip)
323                                                                    {
324                                                                            log_error("Too many client connections (%d) from %s\n", j, hostaddr_client);
325                                                                            break;
326                                                                    }
327                                                            }
328                                                    }
329    
330                                                    if (j < BBS_max_client_per_ip)
331                                                    {
332                                                            if ((pid = fork_server()) < 0)
333                                                            {
334                                                                    log_error("fork_server() error\n");
335                                                            }
336                                                            else if (pid > 0)
337                                                            {
338                                                                    i = 0;
339                                                                    for (; i < BBS_max_client; i++)
340                                                                    {
341                                                                            if (process_sockaddr_pool[i].pid == 0)
342                                                                            {
343                                                                                    break;
344                                                                            }
345                                                                    }
346    
347                                                                    if (i >= BBS_max_client)
348                                                                    {
349                                                                            log_error("Process sockaddr pool depleted\n");
350                                                                    }
351                                                                    else
352                                                                    {
353                                                                            process_sockaddr_pool[i].pid = pid;
354                                                                            process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;
355                                                                    }
356                                                            }
357                                                    }
358                                            }
359                                            else
360                                          {                                          {
361                                                  log_error("fork_server() error\n");                                                  log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
362                                          }                                          }
363    
364                                          if (close(socket_client) == -1)                                          if (close(socket_client) == -1)


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

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