/
server_f.c
73 lines (56 loc) · 1.39 KB
/
server_f.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "serverHelpers.h"
#include "requestHelpers.h"
static void kidhandler(int signum) {
/* signal handler for SIGCHLD */
waitpid(WAIT_ANY, NULL, WNOHANG);
}
int main(int argc, char *argv[])
{
serverInputs inputs;
struct sigaction sa;
int sd;
pid_t pid;
logger * logg;
/*Parse all Arguments*/
memset(&inputs, 0, sizeof(serverInputs));
parseArgs(&inputs, argc, argv);
printf("Log File Path: %s\n", inputs.logPath);
printf("Documents Path: %s\n", inputs.directory);
logg = init_logger(inputs.logPath);
sd = init_socket(&inputs);
sa.sa_handler = kidhandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1)
err(1, "sigaction failed");
printf("Server up and listening on port: %d\n", inputs.port);
daemon(1,1);
for(;;) {
request req;
memset(&req, 0, sizeof(req));
req.l = logg;
int clientsd = accept_connection(sd, &req.client);
req.inputsDIR = inputs.directory;
req.requestSD = &clientsd;
pid = fork();
if (pid == -1)
err(1, "fork failed");
if(pid == 0) {
handle_request(&req);
exit(0);
}
close(clientsd);
}
}