daemon_unix.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "github.com/moby/sys/mount"
  12. "golang.org/x/sys/unix"
  13. "gotest.tools/v3/assert"
  14. )
  15. // cleanupMount unmounts the daemon root directory, or logs a message if
  16. // unmounting failed.
  17. func cleanupMount(t testing.TB, d *Daemon) {
  18. t.Helper()
  19. if err := mount.Unmount(d.Root); err != nil {
  20. d.log.Logf("[%s] unable to unmount daemon root (%s): %v", d.id, d.Root, err)
  21. }
  22. }
  23. func cleanupNetworkNamespace(t testing.TB, d *Daemon) {
  24. t.Helper()
  25. // Cleanup network namespaces in the exec root of this
  26. // daemon because this exec root is specific to this
  27. // daemon instance and has no chance of getting
  28. // cleaned up when a new daemon is instantiated with a
  29. // new exec root.
  30. netnsPath := filepath.Join(d.execRoot, "netns")
  31. filepath.Walk(netnsPath, func(path string, info os.FileInfo, err error) error {
  32. if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
  33. t.Logf("[%s] unmount of %s failed: %v", d.id, path, err)
  34. }
  35. os.Remove(path)
  36. return nil
  37. })
  38. }
  39. // CgroupNamespace returns the cgroup namespace the daemon is running in
  40. func (d *Daemon) CgroupNamespace(t testing.TB) string {
  41. link, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/cgroup", d.Pid()))
  42. assert.NilError(t, err)
  43. return strings.TrimSpace(link)
  44. }
  45. // SignalDaemonDump sends a signal to the daemon to write a dump file
  46. func SignalDaemonDump(pid int) {
  47. unix.Kill(pid, unix.SIGQUIT)
  48. }
  49. func signalDaemonReload(pid int) error {
  50. return unix.Kill(pid, unix.SIGHUP)
  51. }
  52. func setsid(cmd *exec.Cmd) {
  53. if cmd.SysProcAttr == nil {
  54. cmd.SysProcAttr = &syscall.SysProcAttr{}
  55. }
  56. cmd.SysProcAttr.Setsid = true
  57. }