2016-03-22 01:48:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2016-03-27 11:53:46 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <signal.h>
|
2016-03-22 01:48:59 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2016-03-27 11:53:46 +00:00
|
|
|
#include <errno.h>
|
2016-04-10 04:10:18 +00:00
|
|
|
#include <arpa/inet.h>
|
2016-03-22 01:48:59 +00:00
|
|
|
#include "bbs.h"
|
|
|
|
|
2016-03-27 11:53:46 +00:00
|
|
|
void sigchld_handler(int s)
|
|
|
|
{
|
|
|
|
// waitpid() might overwrite errno, so we save and restore it:
|
|
|
|
int saved_errno = errno;
|
|
|
|
|
|
|
|
while(waitpid(-1, NULL, WNOHANG) > 0);
|
|
|
|
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-22 01:48:59 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int socket_desc, client_sock, c, *new_sock;
|
|
|
|
int pid;
|
|
|
|
struct sockaddr_in server, client;
|
2016-03-23 04:21:08 +00:00
|
|
|
int port;
|
2016-03-27 11:53:46 +00:00
|
|
|
struct sigaction sa;
|
|
|
|
|
2016-03-23 04:21:08 +00:00
|
|
|
if (argc < 3) {
|
2016-03-23 08:23:24 +00:00
|
|
|
printf("Usage ./magicka config/bbs.ini port\n");
|
2016-03-22 01:48:59 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-03-27 11:53:46 +00:00
|
|
|
sa.sa_handler = sigchld_handler; // reap all dead processes
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
|
|
|
|
perror("sigaction");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-03-22 01:48:59 +00:00
|
|
|
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (socket_desc == -1) {
|
|
|
|
printf("Couldn't create socket..\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-03-23 04:21:08 +00:00
|
|
|
port = atoi(argv[2]);
|
2016-03-22 01:48:59 +00:00
|
|
|
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_addr.s_addr = INADDR_ANY;
|
2016-03-23 04:21:08 +00:00
|
|
|
server.sin_port = htons(port);
|
2016-03-22 01:48:59 +00:00
|
|
|
|
|
|
|
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
|
|
|
perror("Bind Failed, Error\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
listen(socket_desc, 3);
|
|
|
|
|
|
|
|
c = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
while ((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c))) {
|
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
if (pid < 0) {
|
|
|
|
perror("Error on fork\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
close(socket_desc);
|
|
|
|
runbbs(client_sock, argv[1]);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
close(client_sock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|