2016-07-27 14:17:44 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2019-08-29 15:52:40 -05:00
|
|
|
package daemon // import "github.com/docker/docker/testutil/daemon"
|
2016-07-27 14:17:44 -04:00
|
|
|
|
2016-08-23 16:50:15 -07:00
|
|
|
import (
|
2019-03-14 20:44:18 -07:00
|
|
|
"fmt"
|
2016-08-23 16:50:15 -07:00
|
|
|
"os"
|
2020-03-13 22:37:09 +09:00
|
|
|
"os/exec"
|
2016-08-23 16:50:15 -07:00
|
|
|
"path/filepath"
|
2019-03-14 20:44:18 -07:00
|
|
|
"strings"
|
2020-03-13 22:37:09 +09:00
|
|
|
"syscall"
|
2019-09-23 13:54:51 +02:00
|
|
|
"testing"
|
2016-08-23 16:50:15 -07:00
|
|
|
|
2020-11-10 14:46:59 +01:00
|
|
|
"github.com/moby/sys/mount"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2020-02-07 14:39:24 +01:00
|
|
|
"gotest.tools/v3/assert"
|
2016-08-23 16:50:15 -07:00
|
|
|
)
|
|
|
|
|
2020-09-19 18:45:41 +02:00
|
|
|
// cleanupMount unmounts the daemon root directory, or logs a message if
|
|
|
|
// unmounting failed.
|
|
|
|
func cleanupMount(t testing.TB, d *Daemon) {
|
|
|
|
t.Helper()
|
|
|
|
if err := mount.Unmount(d.Root); err != nil {
|
|
|
|
d.log.Logf("[%s] unable to unmount daemon root (%s): %v", d.id, d.Root, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 13:17:52 +02:00
|
|
|
func cleanupNetworkNamespace(t testing.TB, d *Daemon) {
|
2019-09-23 14:06:27 +02:00
|
|
|
t.Helper()
|
2016-08-23 16:50:15 -07:00
|
|
|
// Cleanup network namespaces in the exec root of this
|
|
|
|
// daemon because this exec root is specific to this
|
|
|
|
// daemon instance and has no chance of getting
|
|
|
|
// cleaned up when a new daemon is instantiated with a
|
|
|
|
// new exec root.
|
2019-10-09 13:17:52 +02:00
|
|
|
netnsPath := filepath.Join(d.execRoot, "netns")
|
2016-08-23 16:50:15 -07:00
|
|
|
filepath.Walk(netnsPath, func(path string, info os.FileInfo, err error) error {
|
2018-10-05 11:10:28 -07:00
|
|
|
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
|
2019-10-09 13:17:52 +02:00
|
|
|
t.Logf("[%s] unmount of %s failed: %v", d.id, path, err)
|
2016-08-23 16:50:15 -07:00
|
|
|
}
|
|
|
|
os.Remove(path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2016-07-27 14:17:44 -04:00
|
|
|
|
2019-03-14 20:44:18 -07:00
|
|
|
// CgroupNamespace returns the cgroup namespace the daemon is running in
|
2019-09-23 13:54:51 +02:00
|
|
|
func (d *Daemon) CgroupNamespace(t testing.TB) string {
|
2019-03-14 20:44:18 -07:00
|
|
|
link, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/cgroup", d.Pid()))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return strings.TrimSpace(link)
|
|
|
|
}
|
|
|
|
|
2016-12-09 10:17:53 +01:00
|
|
|
// SignalDaemonDump sends a signal to the daemon to write a dump file
|
|
|
|
func SignalDaemonDump(pid int) {
|
2017-05-23 10:22:32 -04:00
|
|
|
unix.Kill(pid, unix.SIGQUIT)
|
2016-07-27 14:17:44 -04:00
|
|
|
}
|
2016-07-28 11:58:06 -04:00
|
|
|
|
|
|
|
func signalDaemonReload(pid int) error {
|
2017-05-23 10:22:32 -04:00
|
|
|
return unix.Kill(pid, unix.SIGHUP)
|
2016-07-28 11:58:06 -04:00
|
|
|
}
|
2020-03-13 22:37:09 +09:00
|
|
|
|
|
|
|
func setsid(cmd *exec.Cmd) {
|
|
|
|
if cmd.SysProcAttr == nil {
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
}
|
|
|
|
cmd.SysProcAttr.Setsid = true
|
|
|
|
}
|