2019-01-01 22:04:36 +01:00
|
|
|
package test_helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
2019-01-01 23:57:44 +01:00
|
|
|
"strings"
|
2019-01-01 22:04:36 +01:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-05-17 21:37:36 +02:00
|
|
|
// gocryptfs may hold up to maxCacheFds open for caching
|
|
|
|
// Keep in sync with fusefrontend.dirCacheSize
|
|
|
|
// TODO: How to share this constant without causing an import cycle?!
|
|
|
|
const maxCacheFds = 20
|
|
|
|
|
2019-01-01 22:04:36 +01:00
|
|
|
// Indexed by mountpoint. Initialized in doInit().
|
|
|
|
var MountInfo map[string]mountInfo
|
|
|
|
|
|
|
|
type mountInfo struct {
|
|
|
|
// PID of the running gocryptfs process. Set by Mount().
|
2019-01-01 23:01:06 +01:00
|
|
|
Pid int
|
2019-01-01 22:04:36 +01:00
|
|
|
// List of open FDs of the running gocrypts process. Set by Mount().
|
2019-01-01 23:01:06 +01:00
|
|
|
Fds []string
|
2019-01-01 22:04:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount CIPHERDIR "c" on PLAINDIR "p"
|
|
|
|
// Creates "p" if it does not exist.
|
2019-05-01 12:47:54 +02:00
|
|
|
//
|
|
|
|
// Contrary to InitFS(), you MUST passt "-extpass=echo test" (or another way for
|
2020-05-10 00:25:49 +02:00
|
|
|
// getting the master key) explicitly.
|
2019-01-01 22:04:36 +01:00
|
|
|
func Mount(c string, p string, showOutput bool, extraArgs ...string) error {
|
|
|
|
args := []string{"-q", "-wpanic", "-nosyslog", "-fg", fmt.Sprintf("-notifypid=%d", os.Getpid())}
|
|
|
|
args = append(args, extraArgs...)
|
|
|
|
//args = append(args, "-fusedebug")
|
|
|
|
//args = append(args, "-d")
|
|
|
|
args = append(args, c, p)
|
|
|
|
|
|
|
|
if _, err := os.Stat(p); err != nil {
|
|
|
|
err = os.Mkdir(p, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(GocryptfsBinary, args...)
|
|
|
|
if showOutput {
|
|
|
|
// The Go test logic waits for our stdout to close, and when we share
|
|
|
|
// it with the subprocess, it will wait for it to close it as well.
|
|
|
|
// Use an intermediate pipe so the tests do not hang when unmouting
|
|
|
|
// fails.
|
|
|
|
pr, pw, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We can close the fd after cmd.Run() has executed
|
|
|
|
defer pw.Close()
|
|
|
|
cmd.Stderr = pw
|
|
|
|
cmd.Stdout = pw
|
|
|
|
go func() {
|
|
|
|
io.Copy(os.Stdout, pr)
|
|
|
|
pr.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two things can happen:
|
|
|
|
// 1) The mount fails and the process exits
|
|
|
|
// 2) The mount succeeds and the process sends us USR1
|
|
|
|
chanExit := make(chan error, 1)
|
|
|
|
chanUsr1 := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(chanUsr1, syscall.SIGUSR1)
|
|
|
|
|
|
|
|
// Start the process and save the PID
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pid := cmd.Process.Pid
|
|
|
|
|
|
|
|
// Wait for exit or usr1
|
|
|
|
go func() {
|
|
|
|
chanExit <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-chanExit:
|
|
|
|
return err
|
|
|
|
case <-chanUsr1:
|
|
|
|
// noop
|
2019-01-15 19:40:53 +01:00
|
|
|
case <-time.After(2 * time.Second):
|
2019-01-01 22:04:36 +01:00
|
|
|
log.Panicf("Timeout waiting for process %d", pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save PID and open FDs
|
2019-10-06 19:04:16 +02:00
|
|
|
MountInfo[p] = mountInfo{pid, ListFds(pid, "")}
|
2019-01-01 22:04:36 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountOrExit calls Mount() and exits on failure.
|
2019-05-01 12:47:54 +02:00
|
|
|
//
|
|
|
|
// Contrary to InitFS(), you MUST passt "-extpass=echo test" (or another way for
|
2020-05-10 00:25:49 +02:00
|
|
|
// getting the master key) explicitly.
|
2019-01-01 22:04:36 +01:00
|
|
|
func MountOrExit(c string, p string, extraArgs ...string) {
|
|
|
|
err := Mount(c, p, true, extraArgs...)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("mount failed: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountOrFatal calls Mount() and calls t.Fatal() on failure.
|
2019-05-01 12:47:54 +02:00
|
|
|
//
|
|
|
|
// Contrary to InitFS(), you MUST passt "-extpass=echo test" (or another way for
|
2020-05-10 00:25:49 +02:00
|
|
|
// getting the master key) explicitly.
|
2019-01-01 22:04:36 +01:00
|
|
|
func MountOrFatal(t *testing.T, c string, p string, extraArgs ...string) {
|
2020-09-09 11:15:54 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2019-01-01 22:04:36 +01:00
|
|
|
err := Mount(c, p, true, extraArgs...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("mount failed: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmountPanic tries to umount "dir" and panics on error.
|
|
|
|
func UnmountPanic(dir string) {
|
|
|
|
err := UnmountErr(dir)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("UnmountPanic: %v. Running lsof %s\n", err, dir)
|
|
|
|
cmd := exec.Command("lsof", dir)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2020-05-09 19:03:16 +02:00
|
|
|
cmd.Start()
|
|
|
|
timer := time.AfterFunc(1*time.Second, func() {
|
|
|
|
fmt.Printf("timeout!")
|
|
|
|
cmd.Process.Kill()
|
|
|
|
})
|
|
|
|
cmd.Wait()
|
|
|
|
timer.Stop()
|
2019-01-01 22:04:36 +01:00
|
|
|
panic("UnmountPanic: unmount failed: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmountErr tries to unmount "dir", retrying 10 times, and returns the
|
|
|
|
// resulting error.
|
|
|
|
func UnmountErr(dir string) (err error) {
|
|
|
|
var fdsNow []string
|
2019-01-01 23:01:06 +01:00
|
|
|
pid := MountInfo[dir].Pid
|
|
|
|
fds := MountInfo[dir].Fds
|
2019-01-14 22:11:15 +01:00
|
|
|
if pid <= 0 && runtime.GOOS == "linux" {
|
|
|
|
// The FD leak check only works on Linux.
|
2019-01-01 22:04:36 +01:00
|
|
|
fmt.Printf("UnmountErr: %q was not found in MountInfo, cannot check for FD leaks\n", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
max := 10
|
|
|
|
// When a new filesystem is mounted, Gnome tries to read files like
|
|
|
|
// .xdg-volume-info, autorun.inf, .Trash.
|
|
|
|
// If we try to unmount before Gnome is done, the unmount fails with
|
|
|
|
// "Device or resource busy", causing spurious test failures.
|
|
|
|
// Retry a few times to hide that problem.
|
|
|
|
for i := 1; i <= max; i++ {
|
|
|
|
if pid > 0 {
|
2019-01-02 21:03:26 +01:00
|
|
|
for j := 1; j <= max; j++ {
|
2019-01-02 01:09:09 +01:00
|
|
|
// File close on FUSE is asynchronous, closing a socket
|
2019-01-02 21:03:26 +01:00
|
|
|
// when testing "-ctlsock" is as well. Wait a little and
|
|
|
|
// hope that all close commands get through to the gocryptfs
|
2019-01-02 01:09:09 +01:00
|
|
|
// process.
|
2019-10-06 19:04:16 +02:00
|
|
|
fdsNow = ListFds(pid, "")
|
2019-01-03 15:38:51 +01:00
|
|
|
if len(fdsNow) <= len(fds)+maxCacheFds {
|
2019-01-02 21:03:26 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Printf("UnmountErr: fdsOld=%d fdsNow=%d, retrying\n", len(fds), len(fdsNow))
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2019-10-06 19:04:16 +02:00
|
|
|
fdsNow = ListFds(pid, "")
|
2019-01-02 01:09:09 +01:00
|
|
|
}
|
2019-01-01 22:04:36 +01:00
|
|
|
}
|
|
|
|
cmd := exec.Command(UnmountScript, "-u", dir)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err = cmd.Run()
|
|
|
|
if err == nil {
|
2019-01-03 15:38:51 +01:00
|
|
|
if len(fdsNow) > len(fds)+maxCacheFds {
|
2019-10-06 18:48:09 +02:00
|
|
|
return fmt.Errorf("fd leak in gocryptfs process? pid=%d dir=%q, fds:\nold=%v \nnew=%v\n", pid, dir, fds, fdsNow)
|
2019-01-01 22:04:36 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
code := ExtractCmdExitCode(err)
|
|
|
|
fmt.Printf("UnmountErr: got exit code %d, retrying (%d/%d)\n", code, i, max)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListFds lists the open file descriptors for process "pid". Pass pid=0 for
|
2019-10-06 19:04:16 +02:00
|
|
|
// ourselves. Pass a prefix to ignore all paths that do not start with "prefix".
|
|
|
|
func ListFds(pid int, prefix string) []string {
|
2019-01-01 22:04:36 +01:00
|
|
|
// We need /proc to get the list of fds for other processes. Only exists
|
|
|
|
// on Linux.
|
|
|
|
if runtime.GOOS != "linux" && pid > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Both Linux and MacOS have /dev/fd
|
|
|
|
dir := "/dev/fd"
|
|
|
|
if pid > 0 {
|
|
|
|
dir = fmt.Sprintf("/proc/%d/fd", pid)
|
|
|
|
}
|
|
|
|
f, err := os.Open(dir)
|
|
|
|
if err != nil {
|
2019-09-08 16:15:35 +02:00
|
|
|
fmt.Printf("ListFds: %v\n", err)
|
|
|
|
return nil
|
2019-01-01 22:04:36 +01:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2019-01-01 22:27:40 +01:00
|
|
|
// Note: Readdirnames filters "." and ".."
|
2019-01-01 22:04:36 +01:00
|
|
|
names, err := f.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2019-01-01 22:27:40 +01:00
|
|
|
var out []string
|
2019-10-06 19:04:16 +02:00
|
|
|
var filtered []string
|
2019-01-01 22:27:40 +01:00
|
|
|
for _, n := range names {
|
|
|
|
fdPath := dir + "/" + n
|
|
|
|
fi, err := os.Lstat(fdPath)
|
|
|
|
if err != nil {
|
|
|
|
// fd was closed in the meantime
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fi.Mode()&0400 > 0 {
|
|
|
|
n += "r"
|
|
|
|
}
|
|
|
|
if fi.Mode()&0200 > 0 {
|
|
|
|
n += "w"
|
|
|
|
}
|
|
|
|
target, err := os.Readlink(fdPath)
|
|
|
|
if err != nil {
|
|
|
|
// fd was closed in the meantime
|
|
|
|
continue
|
|
|
|
}
|
2019-01-01 23:57:44 +01:00
|
|
|
if strings.HasPrefix(target, "pipe:") || strings.HasPrefix(target, "anon_inode:[eventpoll]") {
|
|
|
|
// The Go runtime creates pipes on demand for splice(), which
|
|
|
|
// creates spurious test failures. Ignore all pipes.
|
|
|
|
// Also get rid of the "eventpoll" fd that is always there and not
|
|
|
|
// interesting.
|
2019-10-06 19:04:16 +02:00
|
|
|
filtered = append(filtered, target)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prefix != "" && !strings.HasPrefix(target, prefix) {
|
|
|
|
filtered = append(filtered, target)
|
2019-01-01 23:57:44 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-01-01 22:27:40 +01:00
|
|
|
out = append(out, n+"="+target)
|
2019-01-01 22:04:36 +01:00
|
|
|
}
|
2019-10-06 19:04:16 +02:00
|
|
|
out = append(out, fmt.Sprintf("(filtered: %s)", strings.Join(filtered, ", ")))
|
2019-01-01 22:27:40 +01:00
|
|
|
return out
|
2019-01-01 22:04:36 +01:00
|
|
|
}
|