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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <assert.h>
#include "utils.h"
#include "log.h"
#define _POSIX_PATH_MAX 256
char *arg0 = NULL;
static int null_fd = -1;
static void sighandler_child(int signo)
{
switch (signo) {
case SIGHUP:
if (getppid() == 1) {
N("Master process %d died, exiting", getpgrp());
exit(EXIT_SUCCESS);
}
break;
}
}
int set_child_sighandler(void)
{
if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0)
return 1;
return signal(SIGHUP, sighandler_child) == SIG_ERR;
}
void set_procname(const char *new_arg0)
{
assert(arg0);
memset(arg0, 0, _POSIX_PATH_MAX);
strncpy(arg0, new_arg0, _POSIX_PATH_MAX);
}
pid_t daemonize(int stay_foreground)
{
int status = -1;
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
return pid;
/* Success: Let the parent terminate */
if (pid > 0) {
if (!stay_foreground)
exit(EXIT_SUCCESS);
waitpid(-1, &status, 0);
exit(EXIT_SUCCESS);
}
/* On success: The child process becomes session leader */
if (!stay_foreground && setsid() < 0) {
E_STRERR("setsid");
exit(EXIT_FAILURE);
}
/* Fork off for the second time*/
if (!stay_foreground) {
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
}
if (!stay_foreground && setpgrp()) {
E_STRERR("setpgrp");
exit(EXIT_FAILURE);
}
/* Set new file permissions */
umask(0);
if (!stay_foreground) {
/* Change the working directory to the root directory */
/* or another appropriated directory */
chdir("/");
/* Close all open file descriptors */
assert( close_fds_except(-1) == 0 );
} else {
assert( close_fds_except(0, 1, 2, -1) == 0 );
}
return pid;
}
int close_fds_except(int fds, ...)
{
int fd;
long max_fd;
size_t i, except_count, found;
va_list ap;
max_fd = sysconf(_SC_OPEN_MAX);
if (max_fd <= 0)
return 1;
va_start(ap, fds);
{
int *all_fds = malloc(max_fd * sizeof(*all_fds));
assert(all_fds);
memset(all_fds, -1, max_fd * sizeof(*all_fds));
except_count = 0;
while ( (fd = va_arg(ap, int)) >= 0 ) {
all_fds[except_count++] = fd;
}
for (fd = max_fd; fd >= 0; --fd) {
found = 0;
for (i = 0; i < except_count; ++i) {
if (fd == all_fds[i])
found++;
}
if (!found) {
close(fd);
}
}
free(all_fds);
}
va_end(ap);
return 0;
}
int redirect_devnull_to(int fds, ...)
{
int fd, rc = 0;
va_list ap;
if (null_fd < 0)
null_fd = open("/dev/null", O_RDWR);
assert(null_fd >= 0);
va_start(ap, fds);
{
while ( (fd = va_arg(ap, int)) >= 0 ) {
if ( dup2(null_fd, fd) < 0 )
rc++;
}
}
va_end(ap);
return rc;
}
int change_user_group(const char *user, const char *group)
{
return 0;
}
|