github.com/lacework-dev/go-moby@v20.10.12+incompatible/testutil/daemon/daemon_unix.go (about)

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