-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
61 lines (50 loc) · 1.21 KB
/
main.cpp
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
#include "http_server.h"
#include <sys/types.h>
#include <sys/stat.h>
void parse_cmd(int argc, char* argv[], in_addr_t &ip, uint &port, string &dir);
int main(int argc, char* argv[]) {
in_addr_t ip;
uint port;
string dir;
parse_cmd(argc, argv, ip, port, dir);
pid_t process_id = 0;
pid_t sid = 0;
process_id = fork();
if (process_id > 0)
{
//printf("process_id of child process %d \n", process_id);
exit(0);
}
umask(0);
sid = setsid();
if(sid < 0)
{
exit(1);
}
chdir("/");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
http_server hs(ip, port, dir);
hs.start();
return 0;
}
void parse_cmd(int argc, char* argv[], in_addr_t &ip, uint &port, string &dir){
int opt;
while((opt = getopt(argc, argv, "h:p:d:")) != -1 ){
switch (opt){
case 'h':
ip = inet_addr(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 'd':
dir = optarg;
break;
default:
cerr << "error getopt" << endl;
return;
}
}
}