-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.c
61 lines (46 loc) · 1.15 KB
/
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
#include <unistd.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdlib.h>
void skeleton_daemon()
{
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* On success: The child process becomes session leader */
if (setsid() < 0)
exit(EXIT_FAILURE);
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* Set new file permissions */
umask(0);
/* Change the working directory to the root directory */
/* or another appropriated directory */
if(chdir("/") != 0) exit(EXIT_FAILURE);
/* Close all open file descriptors */
/*
int x;
for (x = sysconf(_SC_OPEN_MAX); x>0; x--)
{
close(x);
}*/
close(0); close(1); close(2); //stdin stdout stderr
/* Open the log file */
//openlog("ddnsclient", LOG_PID, LOG_DAEMON);
}