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