-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
76 lines (68 loc) · 1.71 KB
/
main.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
74
75
76
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <ctype.h>
#include "proxy.h"
#include "util.h"
#include "proxy_clientside.h"
#include "midlayer.h"
/* sigChldHandler
*
* Handler for incoming signals
*/
void sigChldHandler(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;
}
/* initSigHandlers
*
* Set up handler functions for signals sent to the process
* Reap dead processes/threads left behind
*/
int initSigHandlers(void)
{
struct sigaction sa;
sa.sa_handler = sigChldHandler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
perror("sigaction");
return -1;
}
return 0;
}
/* main
*
* Entry point for the proxy
*
* @param argc Number of commandline parameters
* @param argv Array containing commandline parameters
*/
int main(int argc, char *argv[])
{
if(argc >= 2)
{
for(size_t i = 0; i < strlen(argv[1]); ++i)
{
if(!isdigit(argv[1][i]))
{
printf("ERROR: Provided port may only contain digits\n");
return -1;
}
}
initSigHandlers();
startProxy(argv[1]);
}
else
{
printf("Usage: %s <port>\n", argv[0]);
}
}