From 7e05e809b7579ad0473fff6ce466c19d417d4e93 Mon Sep 17 00:00:00 2001 From: Sebastian Lackner Date: Sat, 5 Jan 2019 15:44:32 +0100 Subject: main: Run 'ensure fds' code early during the program startup. The files are apparently processed in alphabetic order, so cli_args.go is processed before main.go. In order to run before the go-fuse imports, put the 'ensure fds' code in a separate package. Debug messages are omitted to avoid additional imports (that might contain other code messing up our file descriptors). --- main.go | 25 ------------------------- 1 file changed, 25 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 619b1ce..a376356 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ import ( "runtime" "strconv" "strings" - "syscall" "github.com/hanwen/go-fuse/fuse" @@ -150,7 +149,6 @@ func printVersion() { } func main() { - ensureStdFds() mxp := runtime.GOMAXPROCS(0) if mxp < 4 { // On a 2-core machine, setting maxprocs to 4 gives 10% better performance @@ -330,26 +328,3 @@ func main() { os.Exit(0) } } - -// ensureStdFds ensures that file descriptors 0,1,2 are open. The Go stdlib, -// as well as the gocryptfs code, relies on the fact that fds 0,1,2 are always -// open. -// See https://github.com/rfjakob/gocryptfs/issues/320 for details. -// -// This function should be called as the first thing from main(). -func ensureStdFds() { - fd, err := syscall.Open("/dev/null", syscall.O_RDWR, 0) - if err != nil { - tlog.Fatal.Printf("ensureStdFds: open /dev/null failed: %v", err) - os.Exit(exitcodes.DevNull) - } - for fd <= 2 { - fd, err = syscall.Dup(fd) - if err != nil { - tlog.Fatal.Printf("ensureStdFds: dup failed: %v", err) - os.Exit(exitcodes.DevNull) - } - } - // Close excess fd (usually fd 3) - syscall.Close(fd) -} -- cgit v1.2.3