Browse code

libnetwork: create netns without reexec

Signed-off-by: Cory Snider <csnider@mirantis.com>

Cory Snider authored on 2022/10/19 02:51:50
Showing 1 changed files
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"syscall"
15 15
 	"time"
16 16
 
17
+	"github.com/docker/docker/internal/unshare"
17 18
 	"github.com/docker/docker/libnetwork/ns"
18 19
 	"github.com/docker/docker/libnetwork/osl/kernel"
19 20
 	"github.com/docker/docker/libnetwork/types"
... ...
@@ -70,10 +71,6 @@ func SetBasePath(path string) {
70 70
 	prefix = path
71 71
 }
72 72
 
73
-func init() {
74
-	reexec.Register("netns-create", reexecCreateNamespace)
75
-}
76
-
77 73
 func basePath() string {
78 74
 	return filepath.Join(prefix, "netns")
79 75
 }
... ...
@@ -301,35 +298,18 @@ func GetSandboxForExternalKey(basePath string, key string) (Sandbox, error) {
301 301
 	return n, nil
302 302
 }
303 303
 
304
-func reexecCreateNamespace() {
305
-	if len(os.Args) < 2 {
306
-		logrus.Fatal("no namespace path provided")
307
-	}
308
-	if err := mountNetworkNamespace("/proc/self/ns/net", os.Args[1]); err != nil {
309
-		logrus.Fatal(err)
310
-	}
311
-}
312
-
313 304
 func createNetworkNamespace(path string, osCreate bool) error {
314 305
 	if err := createNamespaceFile(path); err != nil {
315 306
 		return err
316 307
 	}
317 308
 
318
-	cmd := &exec.Cmd{
319
-		Path:   reexec.Self(),
320
-		Args:   append([]string{"netns-create"}, path),
321
-		Stdout: os.Stdout,
322
-		Stderr: os.Stderr,
309
+	do := func() error {
310
+		return mountNetworkNamespace(fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid()), path)
323 311
 	}
324 312
 	if osCreate {
325
-		cmd.SysProcAttr = &syscall.SysProcAttr{}
326
-		cmd.SysProcAttr.Cloneflags = syscall.CLONE_NEWNET
313
+		return unshare.Go(unix.CLONE_NEWNET, do, nil)
327 314
 	}
328
-	if err := cmd.Run(); err != nil {
329
-		return fmt.Errorf("namespace creation reexec command failed: %v", err)
330
-	}
331
-
332
-	return nil
315
+	return do()
333 316
 }
334 317
 
335 318
 func unmountNamespaceFile(path string) {