From d6096098275fb0bc4331b40a6d6f6f82d11c7fc1 Mon Sep 17 00:00:00 2001 From: Andreas Economides Date: Wed, 18 Aug 2021 13:19:51 +0100 Subject: [PATCH] server: reopen standard streams to /dev/null For some custom ioengines (see https://github.com/spdk/spdk/issues/1118), it's not trivial to suppress output to stdout and stderr, so they would write to some other file descriptor fio had opened - which is bad. This change ensures that fd's 0, 1, and 2 (stdin, stdout, stderr) are always valid and can be used without any unintended consequences. Signed-off-by: Andreas Economides --- server.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/server.c b/server.c index 42eaa4b1..859a401b 100644 --- a/server.c +++ b/server.c @@ -2565,6 +2565,7 @@ static int write_pid(pid_t pid, const char *pidfile) */ int fio_start_server(char *pidfile) { + FILE *file; pid_t pid; int ret; @@ -2597,14 +2598,28 @@ int fio_start_server(char *pidfile) setsid(); openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); log_syslog = true; - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); + + file = freopen("/dev/null", "r", stdin); + if (!file) + perror("freopen"); + + file = freopen("/dev/null", "w", stdout); + if (!file) + perror("freopen"); + + file = freopen("/dev/null", "w", stderr); + if (!file) + perror("freopen"); + f_out = NULL; f_err = NULL; ret = fio_server(); + fclose(stdin); + fclose(stdout); + fclose(stderr); + closelog(); unlink(pidfile); free(pidfile); -- 2.25.1