-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
75 lines (60 loc) · 1.16 KB
/
Util.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
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Util.cpp
// sniffer
//
// Created by ziteng on 18-11-12
//
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <fcntl.h>
#include "Util.h"
CLock::CLock()
{
pthread_mutex_init(&m_lock, NULL);
}
CLock::~CLock()
{
pthread_mutex_destroy(&m_lock);
}
void CLock::lock()
{
pthread_mutex_lock(&m_lock);
}
void CLock::unlock()
{
pthread_mutex_unlock(&m_lock);
}
int daemonize()
{
pid_t pid = fork();
if (pid == -1) {
fprintf(stderr, "fork failed\n");
return -1;
} else if (pid > 0) {
exit(0);
}
umask(0);
setsid();
// close all open file
struct rlimit rl;
getrlimit(RLIMIT_NOFILE, &rl);
if (rl.rlim_max > 1024) {
rl.rlim_max = 1024;
}
for (int i = 0; i < rl.rlim_max; i++) {
close(i);
}
// attach file descriptor 0, 1, 2 to "dev/null"
int fd = open("/dev/null", O_RDWR, 0666);
if (fd == -1) {
fprintf(stderr, "open failed\n");
return -1;
}
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
return 0;
}