-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemonize.c
51 lines (42 loc) · 1.2 KB
/
daemonize.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
/***********************************************/
/* Jakub Mackovič - xmacko00 */
/* Jakub Pastuszek - xpastu00 */
/* VUT FIT Brno */
/* Export informací o překladu adres */
/* Květen 2017 */
/* daemonize.c */
/***********************************************/
#include <pthread.h>
#include <unistd.h>
#include "daemonize.h"
int daemonize(void)
{
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
{
return 0;
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0)
{
exit(0);
}
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
return 0;
}
/* Close out the standard file descriptors */
//Because daemons generally dont interact directly with user so there is no need of keeping these open
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
return 1;
}