ipc: umount all mounts before removing temp dirs

This is needed if unshare(CLONE_NEWNS) is not implemented.
Otherwise, os.RemoveAll fails.
This commit is contained in:
Dmitry Vyukov 2016-01-11 17:28:34 +01:00
parent 4f3c86c950
commit 31d1087c3f
2 changed files with 17 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import (
"strconv"
"sync"
"syscall"
"unsafe"
)
var copyMu sync.Mutex
@ -103,3 +104,16 @@ func ProcessTempDir(where string) (string, int, error) {
}
return "", 0, fmt.Errorf("too many live instances")
}
// UmountAll recurusively unmounts all mounts in dir.
func UmountAll(dir string) {
files, _ := ioutil.ReadDir(dir)
for _, f := range files {
name := filepath.Join(dir, f.Name())
if f.IsDir() {
UmountAll(name)
}
fn := []byte(name + "\x00")
syscall.Syscall(syscall.SYS_UMOUNT2, uintptr(unsafe.Pointer(&fn[0])), syscall.MNT_FORCE, 0)
}
}

View File

@ -16,6 +16,7 @@ import (
"syscall"
"time"
"github.com/google/syzkaller/fileutil"
"github.com/google/syzkaller/prog"
)
@ -356,6 +357,7 @@ func (c *command) close() {
c.kill()
c.cmd.Wait()
}
fileutil.UmountAll(c.dir)
os.RemoveAll(c.dir)
if c.rp != nil {
c.rp.Close()
@ -400,6 +402,7 @@ func (c *command) exec() (output, strace []byte, failed, hanged bool, err0 error
//!!! handle c.rp overflow
_, readErr := c.inrp.Read(tmp[:])
close(done)
fileutil.UmountAll(c.dir)
os.RemoveAll(c.dir)
if err := os.Mkdir(c.dir, 0777); err != nil {
<-hang