diff --git a/common/reexec/reexec.go b/common/reexec/reexec.go index 9f14515229..db7f23317a 100644 --- a/common/reexec/reexec.go +++ b/common/reexec/reexec.go @@ -12,8 +12,6 @@ package reexec import ( "fmt" "os" - "os/exec" - "path/filepath" ) var registeredInitializers = make(map[string]func()) @@ -23,34 +21,15 @@ func Register(name string, initializer func()) { if _, exists := registeredInitializers[name]; exists { panic(fmt.Sprintf("reexec func already registered under name %q", name)) } - registeredInitializers[name] = initializer } // Init is called as the first part of the exec process and returns true if an // initialization function was called. func Init() bool { - initializer, exists := registeredInitializers[os.Args[0]] - if exists { + if initializer, ok := registeredInitializers[os.Args[0]]; ok { initializer() - return true } return false } - -func naiveSelf() string { - name := os.Args[0] - if filepath.Base(name) == name { - if lp, err := exec.LookPath(name); err == nil { - return lp - } - } - // handle conversion of relative paths to absolute - if absName, err := filepath.Abs(name); err == nil { - return absName - } - // if we couldn't get absolute name, return original - // (NOTE: Go only errors on Abs() if os.Getwd fails) - return name -} diff --git a/common/reexec/self_linux.go b/common/reexec/self_linux.go index c19e989e77..2abec29bb2 100644 --- a/common/reexec/self_linux.go +++ b/common/reexec/self_linux.go @@ -1,3 +1,8 @@ +// This file originates from Docker/Moby, +// https://github.com/moby/moby/blob/master/pkg/reexec/ +// Licensed under AGPL V2: https://github.com/moby/moby/blob/master/LICENSE +// Copyright 2013-2018 Docker, Inc. + //go:build linux package reexec diff --git a/common/reexec/self_others.go b/common/reexec/self_others.go index 6f17263674..ef9964127c 100644 --- a/common/reexec/self_others.go +++ b/common/reexec/self_others.go @@ -1,9 +1,32 @@ +// This file originates from Docker/Moby, +// https://github.com/moby/moby/blob/master/pkg/reexec/ +// Licensed under AGPL V2: https://github.com/moby/moby/blob/master/LICENSE +// Copyright 2013-2018 Docker, Inc. + //go:build !linux package reexec +import ( + "os" + "os/exec" + "path/filepath" +) + // Self returns the path to the current process's binary. // Uses os.Args[0]. func Self() string { - return naiveSelf() + name := os.Args[0] + if filepath.Base(name) == name { + if lp, err := exec.LookPath(name); err == nil { + return lp + } + } + // handle conversion of relative paths to absolute + if absName, err := filepath.Abs(name); err == nil { + return absName + } + // if we couldn't get absolute name, return original + // (NOTE: Go only errors on Abs() if os.Getwd fails) + return name }