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 .
This commit is contained in:
Jakob Unterwurzacher 2017-07-23 23:55:59 +02:00
parent 318c41827c
commit 496968e06c
1 changed files with 8 additions and 5 deletions

View File

@ -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)