blob: 7b8e736ee0fd809c725cd67ca35d4e227ab2b722 (
plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/**** POSIX *******************************************************************/
#include <sys/types.h>
#include <unistd.h>
/**** RELABSD *****************************************************************/
#include <relabsd/config.h>
#include <relabsd/debug.h>
#include <relabsd/server.h>
#include <relabsd/config/parameters.h>
/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/
/******************************************************************************/
/**** EXPORTED FUNCTIONS ******************************************************/
/******************************************************************************/
/*
* Daemon creation function made using the instructions for old-school daemons
* at https://www.freedesktop.org/software/systemd/man/daemon.html
* Only meant to be used before things really start. This won't properly create
* a daemon on an already running server instance with a virtual device and so
* on...
*/
int relabsd_server_create_daemon (void)
{
pid_t proc_id;
/* 1/ Close all open file descriptors ... **********************************/
/* None were opened at this point. */
/* 2/ Reset all signal handlers ... ****************************************/
/* Those were not modified at this point. */
/* 3/ Reset the signal mask using sigprocmask() ****************************/
/* Not modified at this point. */
/* 4/ Sanitize the environment block ... ***********************************/
/* What? */
/* 5/ fork() ***************************************************************/
errno = 0;
proc_id = fork();
if (proc_id == ((pid_t) -1))
{
RELABSD_FATAL
(
"Step 5 of the daemon creation process, fork(), failed: %s.",
strerror(errno)
);
return -1;
}
if (proc_id != ((pid_t) 0))
{
/* Awaiting step 14...*/
/* TODO: insert unnamed pipe */
/* 15/ Original process exits *******************************************/
exit();
}
/* 6/ setsid() *************************************************************/
errno = 0;
proc_id = setsid();
if (proc_id == ((pid_t) -1))
{
RELABSD_FATAL
(
"Step 6 of the daemon creation process, setsid(), failed: %s.",
strerror(errno)
);
return -1;
}
/* 7/ fork() again *********************************************************/
errno = 0;
proc_id = fork();
if (proc_id == ((pid_t) -1))
{
RELABSD_FATAL
(
"Step 5 of the daemon creation process, fork(), failed: %s.",
strerror(errno)
);
return -1;
}
if (proc_id != ((pid_t) 0))
{
/* 8/ First child process exits *****************************************/
exit();
}
/* 9/ /dev/null for standard input/outputs *********************************/
/* 10/ reset umask to 0 ****************************************************/
/* 11/ Set current directory to / ******************************************/
errno = 0;
if (chdir("/") == -1)
{
// Can't print an error message at that point though...
return -1;
}
/* 12/ lock file using PID to assert single instance ***********************/
/* Don't want to limit to a single instance. */
/* 13/ Drop privileges *****************************************************/
/* 14/ Signal completion ***************************************************/
/* Step 15 is done on the very first process. */
return 0;
}
|