if fork CHECKED setpgid setsid sigset_t mask CHECKED sigemptyset mask

 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
if (fork() == 0) {
CHECKED(setpgid(0, 0));
setsid();
sigset_t mask;
CHECKED(sigemptyset(&mask));
CHECKED(sigprocmask(SIG_BLOCK, &mask, nullptr));
setCGroups(cgroups);
chdir(homedir.c_str());
int fd = open("/dev/null", O_WRONLY | O_APPEND, 0600); // no O_CLOEXEC
if (fd >= 0) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
fcntl(STDIN_FILENO, F_SETFD, 0);
fcntl(STDOUT_FILENO, F_SETFD, 0);
fcntl(STDERR_FILENO, F_SETFD, 0);
}
close_file_descriptors(fd >= 3);
if (fork() == 0) {
CHECKED(setpgid(0, 0));
char * argv[] = { (char *)path.c_str(), (char *)homedir_arg.c_str(), (char *)pid_flag.c_str(), nullptr };
clearenv();
Permissions::dropToUserPermanently(uid);
execve(path.c_str(), argv, env);
_exit(0);
}
_exit(0);
} else {
wait(NULL);
}