diff options
-rw-r--r-- | daemonize.go | 13 |
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) |