Automatically lazy-unmount when we get SIGINT or SIGTERM

This hides the dangling "Transport endpoint is not connected"
mountpoint for everyone but processes that have file open inside
the mountpoint.
This commit is contained in:
Jakob Unterwurzacher 2015-11-01 13:28:58 +01:00
parent e31d319c39
commit af923d2d16
1 changed files with 31 additions and 3 deletions

34
main.go
View File

@ -4,9 +4,12 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"syscall"
"time"
"github.com/rfjakob/gocryptfs/cryptfs"
@ -188,10 +191,16 @@ func main() {
}
fmt.Println("Filesystem ready.")
// Send notification to our parent
sendUsr1()
// Jump into server loop
if !foreground {
// Send notification to our parent
sendUsr1()
}
// Wait for SIGING in the background and unmount ourselves if we get it
// This prevents a dangling "Transport endpoint is not connected" mountpoint
handleSigint(srv, mountpoint)
// Jump into server loop. Returns when it gets an umount request from the kernel.
srv.Serve()
// main returns with code 0
}
func pathfsFrontend(key []byte, cipherdir string, mountpoint string, debug bool, openssl bool) *fuse.Server {
@ -224,3 +233,22 @@ func pathfsFrontend(key []byte, cipherdir string, mountpoint string, debug bool,
return srv
}
func handleSigint(srv *fuse.Server, mountpoint string) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, syscall.SIGTERM)
go func() {
<-ch
err := srv.Unmount()
if err != nil {
fmt.Print(err)
fmt.Printf("Trying lazy unmount\n")
cmd := exec.Command("fusermount", "-u", "-z", mountpoint)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
os.Exit(1)
}()
}