From 333e98dc86fa085741c7f906f4ec146d46e0fd1c Mon Sep 17 00:00:00 2001 From: Max Lv Date: Mon, 28 Jan 2019 21:59:30 +0800 Subject: [PATCH] Redirect standard fd to /dev/null by default --- src/utils.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/utils.c b/src/utils.c index 0100c256..ac52e7da 100644 --- a/src/utils.c +++ b/src/utils.c @@ -38,6 +38,7 @@ #include #include +#include #include @@ -464,10 +465,19 @@ daemonize(const char *path) exit(EXIT_FAILURE); } - /* Close out the standard file descriptors */ + int dev_null = open("/dev/null", O_WRONLY); + if (dev_null) { + /* Redirect to null device */ + dup2(dev_null, STDOUT_FILENO); + dup2(dev_null, STDERR_FILENO); + } else { + /* Close the standard file descriptors */ + close(STDOUT_FILENO); + close(STDERR_FILENO); + } + + /* Close the standard file descriptors */ close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); #else LOGE("daemonize(): not implemented in MinGW port"); #endif