server: reopen standard streams to /dev/null
authorAndreas Economides <andreas.economides@nutanix.com>
Wed, 18 Aug 2021 12:19:51 +0000 (13:19 +0100)
committerAndreas Economides <andreas.economides@nutanix.com>
Wed, 18 Aug 2021 16:43:59 +0000 (16:43 +0000)
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 <andreas.economides@nutanix.com>
server.c

index 42eaa4b1026cc7d63ae4715148355a6e813012cd..859a401b2e528da99c41674c6145514883e62953 100644 (file)
--- 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);