From b409d1eda10aa0464f4b28c4356da0687d9ef4d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20El=20Amri?= Date: Mon, 12 Apr 2021 00:23:57 +0200 Subject: [PATCH] First step in fixing #334 reexec.Init() wasn't called in img's main, while buildkit solver enventually reexec'd in the registered initialization function "docker-untar" This is a quick-and-dirty fix to prevent an unecessary namespace unshare --- internal/unshare/unshare.c | 17 +++++++++++++++++ internal/unshare/unshare.go | 6 ++++-- main.go | 5 +++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/unshare/unshare.c b/internal/unshare/unshare.c index a53804772..8bab5917d 100644 --- a/internal/unshare/unshare.c +++ b/internal/unshare/unshare.c @@ -503,3 +503,20 @@ void nsexec(void) /* Should never be reached. */ bail("should never be reached"); } + + +/* + * FIXME(lel-amri): This is a quick-and-dirty fix + * up to "End of the quick-and-dirty fix #1". + */ +int is_careless_reexec(int argc, char** argv) +{ + if (argc < 1) + return 1; + if (!strncmp(argv[0], "docker-untar", 13)) + return 1; + return 0; +} +/* + * End of the quick-and-dirty fix #1 + */ diff --git a/internal/unshare/unshare.go b/internal/unshare/unshare.go index fb8b61d5f..2d16333a1 100644 --- a/internal/unshare/unshare.go +++ b/internal/unshare/unshare.go @@ -3,8 +3,10 @@ package unshare /* #cgo CFLAGS: -Wall extern void nsexec(); -void __attribute__((constructor)) init(void) { - nsexec(); +extern int is_careless_reexec(int, char**); +void __attribute__((constructor)) init(int argc, char** argv) { + if (!is_careless_reexec(argc, argv)) + nsexec(); } */ import "C" diff --git a/main.go b/main.go index ee4f3a235..d20b6af69 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/genuinetools/img/version" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + dockerreexec "github.com/docker/docker/pkg/reexec" ) const ( @@ -50,6 +51,10 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e ` func main() { + if dockerreexec.Init() { + return + } + var printVersionAndExit bool cmd := &cobra.Command{