close std-filehandles when daemonize

This commit is contained in:
ftasnetamot 2024-08-16 16:39:35 +02:00 committed by Yves Rutschle
parent 18a9a882f5
commit d6bb000115
2 changed files with 11 additions and 1 deletions

View File

@ -272,7 +272,7 @@ void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen)
wait(NULL);
}
/* The actual main is in common.c: it's the same for both version of
/* The actual main() is in sslh_main.c: it's the same for all versions of
* the server
*/

View File

@ -287,6 +287,16 @@ int main(int argc, char *argv[], char* envp[])
if (!cfg.foreground) {
if (fork() > 0) exit(0); /* Detach */
// close stdin, stderr, stdout
int newfd;
if (newfd = open("/dev/null", O_RDWR)) {
dup2 (newfd, STDIN_FILENO);
dup2 (newfd, STDOUT_FILENO);
dup2 (newfd, STDERR_FILENO);
close(newfd);
} else {
print_message(msg_config, "Error closing standard filehandles for background daemon\n");
}
/* New session -- become group leader */
if (getuid() == 0) {