daemon_unix.go 1.3 KB

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