github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/testutil/daemon/daemon_unix.go (about) 1 // +build !windows 2 3 package daemon // import "github.com/demonoid81/moby/testutil/daemon" 4 5 import ( 6 "fmt" 7 "os" 8 "os/exec" 9 "path/filepath" 10 "strings" 11 "syscall" 12 "testing" 13 14 "golang.org/x/sys/unix" 15 "gotest.tools/v3/assert" 16 ) 17 18 func cleanupNetworkNamespace(t testing.TB, d *Daemon) { 19 t.Helper() 20 // Cleanup network namespaces in the exec root of this 21 // daemon because this exec root is specific to this 22 // daemon instance and has no chance of getting 23 // cleaned up when a new daemon is instantiated with a 24 // new exec root. 25 netnsPath := filepath.Join(d.execRoot, "netns") 26 filepath.Walk(netnsPath, func(path string, info os.FileInfo, err error) error { 27 if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT { 28 t.Logf("[%s] unmount of %s failed: %v", d.id, path, err) 29 } 30 os.Remove(path) 31 return nil 32 }) 33 } 34 35 // CgroupNamespace returns the cgroup namespace the daemon is running in 36 func (d *Daemon) CgroupNamespace(t testing.TB) string { 37 link, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/cgroup", d.Pid())) 38 assert.NilError(t, err) 39 40 return strings.TrimSpace(link) 41 } 42 43 // SignalDaemonDump sends a signal to the daemon to write a dump file 44 func SignalDaemonDump(pid int) { 45 unix.Kill(pid, unix.SIGQUIT) 46 } 47 48 func signalDaemonReload(pid int) error { 49 return unix.Kill(pid, unix.SIGHUP) 50 } 51 52 func setsid(cmd *exec.Cmd) { 53 if cmd.SysProcAttr == nil { 54 cmd.SysProcAttr = &syscall.SysProcAttr{} 55 } 56 cmd.SysProcAttr.Setsid = true 57 }