-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
71 lines (58 loc) · 1.48 KB
/
util.cpp
File metadata and controls
71 lines (58 loc) · 1.48 KB
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
#include "util.h"
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#define LOG_FILE "/var/log/webserver.log"
void Daemonize() {
pid_t pid = fork();
if (pid < 0) {
std::cout << "Cannot create another process" << std::endl;
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
if (!freopen(LOG_FILE, "w", stdout)) {
perror("Failed");
exit(EXIT_FAILURE);
}
pid_t sid = setsid();
if (sid < 0) {
perror("Failed to initialize sid");
exit(EXIT_FAILURE);
}
if (chdir("/") < 0) {
perror("Failed chdir");
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDERR_FILENO);
}
Params ParseParams(int argc, char** argv) {
int c;
Params params;
while ((c = getopt(argc, argv, "h:p:d:")) != -1) {
switch (c) {
case 'h':
params.host = std::string(optarg);
break;
case 'p':
params.port = atoi(optarg);
break;
case 'd':
params.dir = std::string(optarg);
break;
default:
std::cout << "Unknown cmd parameters " << c << std::endl;
exit(EXIT_FAILURE);
}
}
if ((params.host == "") || (params.dir == "") || (params.port == 0)) {
std::cout << "Not all parameters set!" << std::endl;
exit(EXIT_FAILURE);
}
return params;
}