github.com/nalind/docker@v1.5.0/pkg/reexec/reexec.go (about)

     1  package reexec
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  )
     9  
    10  var registeredInitializers = make(map[string]func())
    11  
    12  // Register adds an initialization func under the specified name
    13  func Register(name string, initializer func()) {
    14  	if _, exists := registeredInitializers[name]; exists {
    15  		panic(fmt.Sprintf("reexec func already registred under name %q", name))
    16  	}
    17  
    18  	registeredInitializers[name] = initializer
    19  }
    20  
    21  // Init is called as the first part of the exec process and returns true if an
    22  // initialization function was called.
    23  func Init() bool {
    24  	initializer, exists := registeredInitializers[os.Args[0]]
    25  	if exists {
    26  		initializer()
    27  
    28  		return true
    29  	}
    30  	return false
    31  }
    32  
    33  // Self returns the path to the current processes binary
    34  func Self() string {
    35  	name := os.Args[0]
    36  	if filepath.Base(name) == name {
    37  		if lp, err := exec.LookPath(name); err == nil {
    38  			name = lp
    39  		}
    40  	}
    41  	return name
    42  }