| 23 |
#include "log.h" |
#include "log.h" |
| 24 |
#include "io.h" |
#include "io.h" |
| 25 |
#include "fork.h" |
#include "fork.h" |
|
#include "tcplib.h" |
|
| 26 |
#include "menu.h" |
#include "menu.h" |
| 27 |
|
#include <errno.h> |
| 28 |
|
#include <fcntl.h> |
| 29 |
|
#include <string.h> |
| 30 |
#include <signal.h> |
#include <signal.h> |
| 31 |
|
#include <stdlib.h> |
| 32 |
#include <unistd.h> |
#include <unistd.h> |
| 33 |
#include <sys/syscall.h> |
#include <sys/syscall.h> |
| 34 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 56 |
} |
} |
| 57 |
|
|
| 58 |
sin.sin_family = AF_INET; |
sin.sin_family = AF_INET; |
| 59 |
sin.sin_addr.s_addr = |
sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY); |
|
(strnlen(hostaddr, sizeof(hostaddr)) > 0 ? inet_addr(hostaddr) : INADDR_ANY); |
|
| 60 |
sin.sin_port = htons(port); |
sin.sin_port = htons(port); |
| 61 |
|
|
| 62 |
|
// Reuse address and port |
| 63 |
|
flags = 1; |
| 64 |
|
if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) < 0) |
| 65 |
|
{ |
| 66 |
|
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
| 67 |
|
} |
| 68 |
|
if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(flags)) < 0) |
| 69 |
|
{ |
| 70 |
|
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 74 |
{ |
{ |
| 75 |
log_error("Bind address %s:%u failed\n", |
log_error("Bind address %s:%u failed (%d)\n", |
| 76 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
| 77 |
exit(2); |
exit(2); |
| 78 |
} |
} |
| 79 |
|
|
| 80 |
if (listen(socket_server, 10) < 0) |
if (listen(socket_server, 10) < 0) |
| 81 |
{ |
{ |
| 82 |
log_error("Socket listen failed\n"); |
log_error("Socket listen failed (%d)\n", errno); |
| 83 |
exit(3); |
exit(3); |
| 84 |
} |
} |
| 85 |
|
|
| 100 |
{ |
{ |
| 101 |
sigprocmask(SIG_BLOCK, &nsigset, &osigset); |
sigprocmask(SIG_BLOCK, &nsigset, &osigset); |
| 102 |
|
|
| 103 |
while (SYS_child_exit_count > 0) |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
| 104 |
{ |
{ |
| 105 |
siginfo.si_pid = 0; |
siginfo.si_pid = 0; |
| 106 |
ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG); |
ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG); |
| 107 |
if (ret == 0 && siginfo.si_pid > 0) |
if (ret == 0 && siginfo.si_pid > 0) |
| 108 |
{ |
{ |
|
SYS_child_exit_count--; |
|
| 109 |
SYS_child_process_count--; |
SYS_child_process_count--; |
| 110 |
log_std("Child process (%d) exited\n", siginfo.si_pid); |
log_std("Child process (%d) exited\n", siginfo.si_pid); |
| 111 |
} |
} |
| 112 |
else if (ret == 0) |
else if (ret == 0) |
| 113 |
{ |
{ |
| 114 |
|
SYS_child_exit = 0; |
| 115 |
break; |
break; |
| 116 |
} |
} |
| 117 |
else if (ret < 0) |
else if (ret < 0) |
| 121 |
} |
} |
| 122 |
} |
} |
| 123 |
|
|
| 124 |
if (SYS_menu_reload) |
if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) |
| 125 |
|
{ |
| 126 |
|
log_std("Notify %d child process to exit\n", SYS_child_process_count); |
| 127 |
|
if (kill(0, SIGTERM) < 0) |
| 128 |
|
{ |
| 129 |
|
log_error("Send SIGTERM signal failed (%d)\n", errno); |
| 130 |
|
} |
| 131 |
|
} |
| 132 |
|
|
| 133 |
|
if (SYS_menu_reload && !SYS_server_exit) |
| 134 |
{ |
{ |
| 135 |
if (reload_menu(&bbs_menu) < 0) |
if (reload_menu(&bbs_menu) < 0) |
| 136 |
{ |
{ |
| 148 |
FD_ZERO(&testfds); |
FD_ZERO(&testfds); |
| 149 |
FD_SET(socket_server, &testfds); |
FD_SET(socket_server, &testfds); |
| 150 |
|
|
| 151 |
timeout.tv_sec = 1; |
timeout.tv_sec = 0; |
| 152 |
timeout.tv_usec = 0; |
timeout.tv_usec = 100 * 1000; // 0.1 second |
| 153 |
|
|
| 154 |
ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout); |
ret = select(socket_server + 1, &testfds, NULL, NULL, &timeout); |
| 155 |
|
|
| 156 |
if (ret < 0) |
if (ret < 0) |
| 157 |
{ |
{ |
| 176 |
{ |
{ |
| 177 |
flags = fcntl(socket_server, F_GETFL, 0); |
flags = fcntl(socket_server, F_GETFL, 0); |
| 178 |
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
| 179 |
|
|
| 180 |
while ((socket_client = |
while ((socket_client = |
| 181 |
accept(socket_server, (struct sockaddr *)&sin, &namelen)) < 0) |
accept(socket_server, (struct sockaddr *)&sin, &namelen)) < 0) |
| 182 |
{ |
{ |
| 186 |
break; |
break; |
| 187 |
} |
} |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
fcntl(socket_server, F_SETFL, flags); |
fcntl(socket_server, F_SETFL, flags); |
| 191 |
} |
} |
| 192 |
|
|