/[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.32 by sysadm, Tue May 13 11:35:17 2025 UTC Revision 1.44 by sysadm, Sun Jun 1 03:07:42 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 37  Line 40 
40  #include <arpa/inet.h>  #include <arpa/inet.h>
41  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
42    
43    struct process_sockaddr_t
44    {
45            pid_t pid;
46            in_addr_t s_addr;
47    };
48    typedef struct process_sockaddr_t PROCESS_SOCKADDR;
49    
50    static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENTS_LIMIT];
51    
52  int net_server(const char *hostaddr, in_port_t port)  int net_server(const char *hostaddr, in_port_t port)
53  {  {
54          unsigned int namelen;          unsigned int namelen;
# Line 47  int net_server(const char *hostaddr, in_ Line 59  int net_server(const char *hostaddr, in_
59          int nfds, epollfd;          int nfds, epollfd;
60          siginfo_t siginfo;          siginfo_t siginfo;
61          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
62            MENU_SET *p_bbs_menu_new;
63            int i, j;
64            pid_t pid;
65    
66          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
67    
# Line 127  int net_server(const char *hostaddr, in_ Line 142  int net_server(const char *hostaddr, in_
142                          sd_notify(0, "STOPPING=1");                          sd_notify(0, "STOPPING=1");
143                          sd_notify_stopping = 1;                          sd_notify_stopping = 1;
144                  }                  }
145    
146                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
147                  {                  {
148                          SYS_child_exit = 0;                          SYS_child_exit = 0;
# Line 139  int net_server(const char *hostaddr, in_ Line 155  int net_server(const char *hostaddr, in_
155    
156                                  SYS_child_process_count--;                                  SYS_child_process_count--;
157                                  log_std("Child process (%d) exited\n", siginfo.si_pid);                                  log_std("Child process (%d) exited\n", siginfo.si_pid);
158    
159                                    i = 0;
160                                    for (; i < BBS_max_client; i++)
161                                    {
162                                            if (process_sockaddr_pool[i].pid == siginfo.si_pid)
163                                            {
164                                                    process_sockaddr_pool[i].pid = 0;
165                                                    break;
166                                            }
167                                    }
168                                    if (i >= BBS_max_client)
169                                    {
170                                            log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);
171                                    }
172                          }                          }
173                          else if (ret == 0)                          else if (ret == 0)
174                          {                          {
# Line 162  int net_server(const char *hostaddr, in_ Line 192  int net_server(const char *hostaddr, in_
192                          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);
193                  }                  }
194    
195                  if (SYS_menu_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
196                  {                  {
197                          SYS_menu_reload = 0;                          SYS_conf_reload = 0;
198                            sd_notify(0, "RELOADING=1");
199    
200                            // Reload configuration
201                            if (load_conf(CONF_BBSD) < 0)
202                            {
203                                    log_error("Reload conf failed\n");
204                            }
205    
206                          if (reload_menu(&bbs_menu) < 0)                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));
207                            if (p_bbs_menu_new == NULL)
208                          {                          {
209                                    log_error("OOM: calloc(MENU_SET)\n");
210                            }
211                            else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)
212                            {
213                                    unload_menu(p_bbs_menu_new);
214                                    free(p_bbs_menu_new);
215                                    p_bbs_menu_new = NULL;
216    
217                                  log_error("Reload menu failed\n");                                  log_error("Reload menu failed\n");
218                          }                          }
219                          else                          else
220                          {                          {
221                                    unload_menu(p_bbs_menu);
222                                    free(p_bbs_menu);
223    
224                                    p_bbs_menu = p_bbs_menu_new;
225                                    p_bbs_menu_new = NULL;
226    
227                                  log_std("Reload menu successfully\n");                                  log_std("Reload menu successfully\n");
228                          }                          }
229    
230                            sd_notify(0, "READY=1");
231                    }
232    
233                    if (SYS_data_file_reload && !SYS_server_exit)
234                    {
235                            SYS_data_file_reload = 0;
236                            sd_notify(0, "RELOADING=1");
237    
238                            for (int i = 0; i < data_files_load_startup_count; i++)
239                            {
240                                    if (load_file(data_files_load_startup[i]) < 0)
241                                    {
242                                            log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
243                                    }
244                            }
245    
246                            log_std("Reload data files successfully\n");
247                            sd_notify(0, "READY=1");
248                    }
249    
250                    if (SYS_section_list_reload && !SYS_server_exit)
251                    {
252                            SYS_section_list_reload = 0;
253    
254                            if (section_list_loader_reload() < 0)
255                            {
256                                    log_error("ksection_list_loader_reload() failed\n");
257                            }
258                  }                  }
259    
260                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 225  int net_server(const char *hostaddr, in_ Line 306  int net_server(const char *hostaddr, in_
306    
307                                          log_std("Accept connection from %s:%d\n", hostaddr_client, port_client);                                          log_std("Accept connection from %s:%d\n", hostaddr_client, port_client);
308    
309                                          if (fork_server() < 0)                                          if (SYS_child_process_count - 1 < BBS_max_client)
310                                            {
311                                                    j = 0;
312                                                    for (i = 0; i < BBS_max_client; i++)
313                                                    {
314                                                            if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)
315                                                            {
316                                                                    j++;
317                                                                    if (j >= BBS_max_client_per_ip)
318                                                                    {
319                                                                            log_error("Too many client connections (%d) from %s\n", j, hostaddr_client);
320                                                                            break;
321                                                                    }
322                                                            }
323                                                    }
324    
325                                                    if (j < BBS_max_client_per_ip)
326                                                    {
327                                                            if ((pid = fork_server()) < 0)
328                                                            {
329                                                                    log_error("fork_server() error\n");
330                                                            }
331                                                            else if (pid > 0)
332                                                            {
333                                                                    i = 0;
334                                                                    for (; i < BBS_max_client; i++)
335                                                                    {
336                                                                            if (process_sockaddr_pool[i].pid == 0)
337                                                                            {
338                                                                                    break;
339                                                                            }
340                                                                    }
341    
342                                                                    if (i >= BBS_max_client)
343                                                                    {
344                                                                            log_error("Process sockaddr pool depleted\n");
345                                                                    }
346                                                                    else
347                                                                    {
348                                                                            process_sockaddr_pool[i].pid = pid;
349                                                                            process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;
350                                                                    }
351                                                            }
352                                                    }
353                                            }
354                                            else
355                                          {                                          {
356                                                  log_error("fork_server() error\n");                                                  log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
357                                          }                                          }
358    
359                                          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