From d6bb000115530fc164aefe644674d658f85a4a96 Mon Sep 17 00:00:00 2001 From: ftasnetamot Date: Fri, 16 Aug 2024 16:39:35 +0200 Subject: [PATCH] close std-filehandles when daemonize --- sslh-fork.c | 2 +- sslh-main.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sslh-fork.c b/sslh-fork.c index 40a1b5a..dfe39a4 100644 --- a/sslh-fork.c +++ b/sslh-fork.c @@ -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 */ diff --git a/sslh-main.c b/sslh-main.c index e4eb490..5c6f9c4 100644 --- a/sslh-main.c +++ b/sslh-main.c @@ -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) {