From 496968e06c09f896ed04ad0939242e1ae75cde53 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 23 Jul 2017 23:55:59 +0200 Subject: main: redirectStdFds: keep logger from holding stdout open We passed our stdout and stderr to the new logger instance, which makes sense to see any error message, but also means that the fd is kept open even when we close it. Fixes the new TestMountBackground test and https://github.com/rfjakob/gocryptfs/issues/130 . --- daemonize.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/daemonize.go b/daemonize.go index 886a229..1f7b699 100644 --- a/daemonize.go +++ b/daemonize.go @@ -56,7 +56,9 @@ func forkChild() int { // redirectStdFds redirects stderr and stdout to syslog; stdin to /dev/null func redirectStdFds() { - // stderr and stdout + // Create a pipe pair "pw" -> "pr" and start logger reading from "pr". + // We do it ourselves instead of using StdinPipe() because we need access + // to the fd numbers. pr, pw, err := os.Pipe() if err != nil { tlog.Warn.Printf("redirectStdFds: could not create pipe: %v\n", err) @@ -64,14 +66,15 @@ func redirectStdFds() { } tag := fmt.Sprintf("gocryptfs-%d-logger", os.Getpid()) cmd := exec.Command("logger", "-t", tag) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr cmd.Stdin = pr err = cmd.Start() if err != nil { tlog.Warn.Printf("redirectStdFds: could not start logger: %v\n", err) + return } + // The logger now reads on "pr". We can close it. pr.Close() + // Redirect stout and stderr to "pw". err = syscallcompat.Dup3(int(pw.Fd()), 1, 0) if err != nil { tlog.Warn.Printf("redirectStdFds: stdout dup error: %v\n", err) @@ -80,9 +83,9 @@ func redirectStdFds() { if err != nil { tlog.Warn.Printf("redirectStdFds: stderr dup error: %v\n", err) } + // Our stout and stderr point to "pw". We can close the extra copy. pw.Close() - - // stdin + // Redirect stdin to /dev/null nullFd, err := os.Open("/dev/null") if err != nil { tlog.Warn.Printf("redirectStdFds: could not open /dev/null: %v\n", err) -- cgit v1.2.3