From f8d21954d173c47d06e063945804cc8296e4528e Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 19 Oct 2023 20:01:38 +0200 Subject: [PATCH] common/reexec: move unused method --- common/reexec/reexec.go | 23 +---------------------- common/reexec/self_linux.go | 5 +++++ common/reexec/self_others.go | 25 ++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 23 deletions(-) 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 }