-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCreateDaemonProcess.c
54 lines (41 loc) · 969 Bytes
/
CreateDaemonProcess.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/stat.h>
int daemon_init(const char *pname, int facility)
{
pid_t pid;
if ((pid = fork()) < 0) {
perror("fork error!");
return -1;
} else if (pid)
_exit(0);
if (setsid() < 0)
return -1;
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
return -1;
if ((pid = fork()) < 0) {
perror("fork error");
return -1;
} else if (pid)
_exit(0);
chdir("/");
for (int i = 0; i < 64; i++)
close(i);
open("/dev/null", O_RDONLY); // stdin
open("/dev/null", O_RDWR); // stdout
open("/dev/null", O_RDWR); // stderr
openlog(pname, LOG_PID, facility);
return 0;
}
int main(int argc, char const *argv[])
{
daemon_init(argv[0], LOG_USER);
syslog(LOG_INFO, "hahahahahah");
while (1);
return 0;
}