diff options
| author | Jakob Unterwurzacher | 2015-11-01 13:28:58 +0100 | 
|---|---|---|
| committer | Jakob Unterwurzacher | 2015-11-01 13:28:58 +0100 | 
| commit | af923d2d16e0eedc7d2c203e28a42b6af49a51f5 (patch) | |
| tree | 62d8e28fd768c779047fe9c71f87b6b858caaeeb | |
| parent | e31d319c399e619e4dec2f71782f1412a46f2d36 (diff) | |
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.
| -rw-r--r-- | main.go | 34 | 
1 files changed, 31 insertions, 3 deletions
@@ -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) +	}() +}  | 
