Commit 1f4e9748 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Find fusermount/umount on the fly, and return error if not found.

This stops programs from crashing if fusermount is not there.
parent 6fc3efaa
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"path" "path"
"bytes" "bytes"
"fmt" "fmt"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
...@@ -12,9 +11,6 @@ import ( ...@@ -12,9 +11,6 @@ import (
"unsafe" "unsafe"
) )
var fusermountBinary string
var umountBinary string
func unixgramSocketpair() (l, r *os.File, err error) { func unixgramSocketpair() (l, r *os.File, err error) {
fd, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_SEQPACKET, 0) fd, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_SEQPACKET, 0)
if err != nil { if err != nil {
...@@ -37,12 +33,17 @@ func mount(mountPoint string, options string) (fd int, err error) { ...@@ -37,12 +33,17 @@ func mount(mountPoint string, options string) (fd int, err error) {
defer local.Close() defer local.Close()
defer remote.Close() defer remote.Close()
cmd := []string{fusermountBinary, mountPoint} bin, err := fusermountBinary()
if err != nil {
return 0, err
}
cmd := []string{bin, mountPoint}
if options != "" { if options != "" {
cmd = append(cmd, "-o") cmd = append(cmd, "-o")
cmd = append(cmd, options) cmd = append(cmd, options)
} }
proc, err := os.StartProcess(fusermountBinary, proc, err := os.StartProcess(bin,
cmd, cmd,
&os.ProcAttr{ &os.ProcAttr{
Env: []string{"_FUSE_COMMFD=3"}, Env: []string{"_FUSE_COMMFD=3"},
...@@ -66,8 +67,13 @@ func mount(mountPoint string, options string) (fd int, err error) { ...@@ -66,8 +67,13 @@ func mount(mountPoint string, options string) (fd int, err error) {
func privilegedUnmount(mountPoint string) error { func privilegedUnmount(mountPoint string) error {
dir, _ := filepath.Split(mountPoint) dir, _ := filepath.Split(mountPoint)
proc, err := os.StartProcess(umountBinary, bin, err := umountBinary()
[]string{umountBinary, mountPoint}, if err != nil {
return err
}
proc, err := os.StartProcess(bin,
[]string{bin, mountPoint},
&os.ProcAttr{Dir: dir, Files: []*os.File{nil, nil, os.Stderr}}) &os.ProcAttr{Dir: dir, Files: []*os.File{nil, nil, os.Stderr}})
if err != nil { if err != nil {
return err return err
...@@ -83,8 +89,12 @@ func unmount(mountPoint string) (err error) { ...@@ -83,8 +89,12 @@ func unmount(mountPoint string) (err error) {
if os.Geteuid() == 0 { if os.Geteuid() == 0 {
return privilegedUnmount(mountPoint) return privilegedUnmount(mountPoint)
} }
bin, err := fusermountBinary()
if err != nil {
return err
}
errBuf := bytes.Buffer{} errBuf := bytes.Buffer{}
cmd := exec.Command(fusermountBinary, "-u", mountPoint) cmd := exec.Command(bin, "-u", mountPoint)
cmd.Stderr = &errBuf cmd.Stderr = &errBuf
err = cmd.Run() err = cmd.Run()
if errBuf.Len() > 0 { if errBuf.Len() > 0 {
...@@ -133,14 +143,10 @@ func lookPathFallback(file string, fallbackDir string) (string, error) { ...@@ -133,14 +143,10 @@ func lookPathFallback(file string, fallbackDir string) (string, error) {
return exec.LookPath(abs) return exec.LookPath(abs)
} }
func init() { func fusermountBinary() (string, error) {
var err error return lookPathFallback("fusermount", "/bin")
fusermountBinary, err = lookPathFallback("fusermount", "/bin") }
if err != nil {
log.Fatalf("Could not find fusermount binary: %v", err) func umountBinary() (string, error) {
} return lookPathFallback("umount", "/bin")
umountBinary, err = lookPathFallback("umount", "/bin")
if err != nil {
log.Fatalf("Could not find umount binary: %v", err)
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment