forked from gnbdev/opengnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnb_daemon.c
90 lines (54 loc) · 965 Bytes
/
gnb_daemon.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "gnb_platform.h"
#ifdef __UNIX_LIKE_OS__
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#ifdef __UNIX_LIKE_OS__
int gnb_daemon(){
int ret;
int fd;
pid_t pid;
pid = fork();
if ( 0 != pid ){
exit(0);
}
setsid();
signal(SIGHUP,SIG_IGN);
pid = fork();
if ( 0 != pid ){
exit(0);
}
ret = chdir("/");
umask(0);
fd = open("/dev/null", O_RDWR);
if ( -1 == fd ){
return -1;
}
ret = dup2(fd, STDIN_FILENO);
if ( -1 == ret ) {
return -2;
}
ret = dup2(fd, STDOUT_FILENO);
if ( -1 == ret ) {
return -3;
}
return 0;
}
void save_pid(const char *pid_file){
FILE *file;
file = fopen(pid_file,"w");
if (NULL==file){
printf("open pid file[%s] err\n",pid_file);
exit(1);
}
int pid = getpid();
fprintf(file,"%d",pid);
fclose(file);
}
#endif