forked from ideawu/icomet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.h
More file actions
51 lines (45 loc) · 669 Bytes
/
daemon.h
File metadata and controls
51 lines (45 loc) · 669 Bytes
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
#ifndef UTIL_DAEMON_H
#define UTIL_DAEMON_H
#include <unistd.h>
int daemonize(const char *dir=NULL){
switch(fork()){
case -1:
return -1;
case 0:
break;
default:
exit(0);
}
if(setsid() == -1){
exit(0);
}
if(dir != NULL){
if(chdir(dir) == -1){
exit(0);
}
}
if(close(STDIN_FILENO) == -1){
exit(0);
}
if(close(STDOUT_FILENO) == -1){
exit(0);
}
if(close(STDERR_FILENO) == -1){
exit(0);
}
int fd = open("/dev/null", O_RDWR, 0);
if(fd == -1){
exit(0);
}
if(dup2(fd, STDIN_FILENO) == -1){
exit(0);
}
if(dup2(fd, STDOUT_FILENO) == -1){
exit(0);
}
if(dup2(fd, STDERR_FILENO) == -1){
exit(0);
}
return 0;
}
#endif