/[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.15 by sysadm, Sun May 4 14:54:55 2025 UTC Revision 1.22 by sysadm, Sun May 11 01:54:00 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    net_server.c  -  description                                                    net_server.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 11 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _XOPEN_SOURCE 500
18    #define _POSIX_C_SOURCE 200809L
19    #define _GNU_SOURCE
20    
21  #include "net_server.h"  #include "net_server.h"
22  #include "common.h"  #include "common.h"
23  #include "log.h"  #include "log.h"
24  #include "io.h"  #include "io.h"
25  #include "fork.h"  #include "fork.h"
26  #include "tcplib.h"  #include "menu.h"
27    #include <errno.h>
28    #include <fcntl.h>
29    #include <string.h>
30    #include <signal.h>
31    #include <stdlib.h>
32    #include <unistd.h>
33    #include <sys/syscall.h>
34  #include <sys/socket.h>  #include <sys/socket.h>
35  #include <netinet/in.h>  #include <sys/wait.h>
36  #include <arpa/inet.h>  #include <arpa/inet.h>
37    
38  int net_server(const char *hostaddr, unsigned int port)  int net_server(const char *hostaddr, in_port_t port)
39  {  {
40          unsigned int namelen;          unsigned int namelen;
41          int result;          int ret;
42          int flags;          int flags;
43          struct sockaddr_in sin;          struct sockaddr_in sin;
44          fd_set testfds;          fd_set testfds;
45          struct timeval timeout;          struct timeval timeout;
46            sigset_t nsigset;
47            sigset_t osigset;
48            siginfo_t siginfo;
49    
50          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
51    
# Line 44  int net_server(const char *hostaddr, uns Line 57  int net_server(const char *hostaddr, uns
57    
58          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
59          sin.sin_addr.s_addr =          sin.sin_addr.s_addr =
60                  (strlen(hostaddr) > 0 ? inet_addr(hostaddr) : INADDR_ANY);                  (strnlen(hostaddr, sizeof(hostaddr)) > 0 ? inet_addr(hostaddr) : INADDR_ANY);
61          sin.sin_port = htons(port);          sin.sin_port = htons(port);
62    
63          if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)
# Line 60  int net_server(const char *hostaddr, uns Line 73  int net_server(const char *hostaddr, uns
73                  exit(3);                  exit(3);
74          }          }
75    
76          strcpy(hostaddr_server, inet_ntoa(sin.sin_addr));          strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1);
77            hostaddr_server[sizeof(hostaddr_server) - 1] = '\0';
78    
79          port_server = ntohs(sin.sin_port);          port_server = ntohs(sin.sin_port);
80            namelen = sizeof(sin);
81    
82          log_std("Listening at %s:%d\n", hostaddr_server, port_server);          log_std("Listening at %s:%d\n", hostaddr_server, port_server);
83    
84          namelen = sizeof(sin);          sigemptyset(&nsigset);
85          while (!SYS_exit)          sigaddset(&nsigset, SIGHUP);
86            sigaddset(&nsigset, SIGCHLD);
87            sigaddset(&nsigset, SIGTERM);
88    
89            while (!SYS_server_exit || SYS_child_process_count > 0)
90          {          {
91                    sigprocmask(SIG_BLOCK, &nsigset, &osigset);
92    
93                    while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
94                    {
95                            siginfo.si_pid = 0;
96                            ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);
97                            if (ret == 0 && siginfo.si_pid > 0)
98                            {
99                                    SYS_child_process_count--;
100                                    log_std("Child process (%d) exited\n", siginfo.si_pid);
101                            }
102                            else if (ret == 0)
103                            {
104                                    SYS_child_exit = 0;
105                                    break;
106                            }
107                            else if (ret < 0)
108                            {
109                                    log_error("Error in waitid: %d\n", errno);
110                                    break;
111                            }
112                    }
113    
114                    if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
115                    {
116                            log_std("Notify %d child process to exit\n", SYS_child_process_count);
117                            if (kill(0, SIGTERM) < 0)
118                            {
119                                    log_error("Send SIGTERM signal failed (%d)\n", errno);
120                            }
121                    }
122    
123                    if (SYS_menu_reload && !SYS_server_exit)
124                    {
125                            if (reload_menu(&bbs_menu) < 0)
126                            {
127                                    log_error("Reload menu failed\n");
128                            }
129                            else
130                            {
131                                    log_std("Reload menu successfully\n");
132                            }
133                            SYS_menu_reload = 0;
134                    }
135    
136                    sigprocmask(SIG_SETMASK, &osigset, NULL);
137    
138                  FD_ZERO(&testfds);                  FD_ZERO(&testfds);
139                  FD_SET(socket_server, &testfds);                  FD_SET(socket_server, &testfds);
140    
141                  timeout.tv_sec = 1;                  timeout.tv_sec = 0;
142                  timeout.tv_usec = 0;                  timeout.tv_usec = 100 * 1000; // 0.1 second
143    
144                    ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout);
145    
146                  result = SignalSafeSelect(FD_SETSIZE, &testfds, NULL, NULL, &timeout);                  if (ret < 0)
147                  if (result < 0)                  {
148                            if (errno != EINTR)
149                            {
150                                    log_error("Accept connection error: %d\n", errno);
151                            }
152                            continue;
153                    }
154                    else if (ret == 0) // timeout
155                  {                  {
                         log_error("Accept connection error\n");  
156                          continue;                          continue;
157                  }                  }
158    
159                  if (result == 0)                  // Stop accept new connection on exit
160                    if (SYS_server_exit)
161                  {                  {
162                          continue;                          continue;
163                  }                  }
# Line 108  int net_server(const char *hostaddr, uns Line 184  int net_server(const char *hostaddr, uns
184                          continue;                          continue;
185                  }                  }
186    
187                  strcpy(hostaddr_client, (const char *)inet_ntoa(sin.sin_addr));                  strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1);
188                    hostaddr_client[sizeof(hostaddr_client) - 1] = '\0';
189    
190                  port_client = ntohs(sin.sin_port);                  port_client = ntohs(sin.sin_port);
191    
192                  log_std("Accept connection from %s:%d\n", hostaddr_client,                  log_std("Accept connection from %s:%d\n", hostaddr_client,


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

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