Close socket properly
This commit is contained in:
parent
08ccc8d0e9
commit
f95c634034
29
main.c
29
main.c
@ -31,12 +31,16 @@ extern struct user_record *gUser;
|
|||||||
|
|
||||||
int ssh_pid = -1;
|
int ssh_pid = -1;
|
||||||
int bbs_pid = 0;
|
int bbs_pid = 0;
|
||||||
|
int server_socket = -1;
|
||||||
|
|
||||||
void sigterm_handler(int s)
|
void sigterm_handler(int s)
|
||||||
{
|
{
|
||||||
if (ssh_pid != -1) {
|
if (ssh_pid != -1) {
|
||||||
kill(ssh_pid, SIGTERM);
|
kill(ssh_pid, SIGTERM);
|
||||||
}
|
}
|
||||||
|
if (server_socket != -1) {
|
||||||
|
close(server_socket);
|
||||||
|
}
|
||||||
remove(conf.pid_file);
|
remove(conf.pid_file);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
@ -617,7 +621,8 @@ void serverssh(int port) {
|
|||||||
void server(int port) {
|
void server(int port) {
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
struct sigaction st;
|
struct sigaction st;
|
||||||
int socket_desc, client_sock, c;
|
struct sigaction sq;
|
||||||
|
int client_sock, c;
|
||||||
int pid;
|
int pid;
|
||||||
struct sockaddr_in server, client;
|
struct sockaddr_in server, client;
|
||||||
|
|
||||||
@ -638,6 +643,14 @@ void server(int port) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sq.sa_handler = sigterm_handler;
|
||||||
|
sigemptyset(&sq.sa_mask);
|
||||||
|
if (sigaction(SIGQUIT, &sq, NULL) == -1) {
|
||||||
|
remove(conf.pid_file);
|
||||||
|
perror("sigaction");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
if (conf.ssh_server) {
|
if (conf.ssh_server) {
|
||||||
// fork ssh server
|
// fork ssh server
|
||||||
ssh_pid = fork();
|
ssh_pid = fork();
|
||||||
@ -651,8 +664,8 @@ void server(int port) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
|
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (socket_desc == -1) {
|
if (server_socket == -1) {
|
||||||
remove(conf.pid_file);
|
remove(conf.pid_file);
|
||||||
fprintf(stderr, "Couldn't create socket..\n");
|
fprintf(stderr, "Couldn't create socket..\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -663,16 +676,17 @@ void server(int port) {
|
|||||||
server.sin_addr.s_addr = INADDR_ANY;
|
server.sin_addr.s_addr = INADDR_ANY;
|
||||||
server.sin_port = htons(port);
|
server.sin_port = htons(port);
|
||||||
|
|
||||||
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
if (bind(server_socket, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
||||||
perror("Bind Failed, Error\n");
|
perror("Bind Failed, Error\n");
|
||||||
|
remove(conf.pid_file);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
listen(socket_desc, 3);
|
listen(server_socket, 3);
|
||||||
|
|
||||||
c = sizeof(struct sockaddr_in);
|
c = sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
while ((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c))) {
|
while ((client_sock = accept(server_socket, (struct sockaddr *)&client, (socklen_t *)&c))) {
|
||||||
if (client_sock == -1) {
|
if (client_sock == -1) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
continue;
|
continue;
|
||||||
@ -688,7 +702,8 @@ void server(int port) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
close(socket_desc);
|
close(server_socket);
|
||||||
|
server_socket = -1;
|
||||||
runbbs(client_sock, strdup(inet_ntoa(client.sin_addr)));
|
runbbs(client_sock, strdup(inet_ntoa(client.sin_addr)));
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
|
Reference in New Issue
Block a user