gopkg.in/docker/docker.v20@v20.10.27/testutil/daemon/daemon_unix.go (about)

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