daemon_unix.go 1.4 KB

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