aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Unterwurzacher2017-07-23 23:55:59 +0200
committerJakob Unterwurzacher2017-07-24 00:05:39 +0200
commit496968e06c09f896ed04ad0939242e1ae75cde53 (patch)
treea6cfce40f0709167c30a7128281c53b354ebfe99
parent318c41827cf23476b9705081f63b7f93fecba09a (diff)
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 .
-rw-r--r--daemonize.go13
1 files 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)