From 8a9a11d6d91ba3131f8f33e1c46138932a8357c9 Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Fri, 16 Jun 2023 21:57:39 +0800 Subject: [PATCH] log: enable output log to console when run as daemon. --- src/smartdns.c | 4 ++-- src/util.c | 24 +++++++++++++----------- src/util.h | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/smartdns.c b/src/smartdns.c index 983d1c4..68ebbd1 100644 --- a/src/smartdns.c +++ b/src/smartdns.c @@ -855,7 +855,7 @@ int main(int argc, char *argv[]) } if (daemon_ret > 0) { - ret = daemon_kickoff(daemon_ret, 0); + ret = daemon_kickoff(daemon_ret, 0, dns_conf_log_console | verbose_screen); if (ret != 0) { goto errout; } @@ -867,7 +867,7 @@ int main(int argc, char *argv[]) return ret; errout: if (daemon_ret > 0) { - daemon_kickoff(daemon_ret, ret); + daemon_kickoff(daemon_ret, ret, dns_conf_log_console | verbose_screen); } smartdns_test_notify(2); return 1; diff --git a/src/util.c b/src/util.c index 56c131e..3be1004 100644 --- a/src/util.c +++ b/src/util.c @@ -1560,7 +1560,7 @@ errout: return; } -int daemon_kickoff(int fd, int status) +int daemon_kickoff(int fd, int status, int no_close) { if (fd <= 0) { return -1; @@ -1571,18 +1571,20 @@ int daemon_kickoff(int fd, int status) return -1; } - int fd_null = open("/dev/null", O_RDWR); - if (fd_null < 0) { - fprintf(stderr, "open /dev/null failed, %s\n", strerror(errno)); - return -1; - } + if (no_close == 0) { + int fd_null = open("/dev/null", O_RDWR); + if (fd_null < 0) { + fprintf(stderr, "open /dev/null failed, %s\n", strerror(errno)); + return -1; + } - dup2(fd_null, STDIN_FILENO); - dup2(fd_null, STDOUT_FILENO); - dup2(fd_null, STDERR_FILENO); + dup2(fd_null, STDIN_FILENO); + dup2(fd_null, STDOUT_FILENO); + dup2(fd_null, STDERR_FILENO); - if (fd_null > 2) { - close(fd_null); + if (fd_null > 2) { + close(fd_null); + } } close(fd); diff --git a/src/util.h b/src/util.h index fa5f4d3..a18667d 100644 --- a/src/util.h +++ b/src/util.h @@ -144,7 +144,7 @@ void close_all_fd(int keepfd); int run_daemon(void); -int daemon_kickoff(int fd, int status); +int daemon_kickoff(int fd, int status, int no_close); int write_file(const char *filename, void *data, int data_len);