github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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 return lp 39 } 40 } 41 // handle conversion of relative paths to absolute 42 if absName, err := filepath.Abs(name); err == nil { 43 return absName 44 } 45 // if we coudn't get absolute name, return original 46 // (NOTE: Go only errors on Abs() if os.Getwd fails) 47 return name 48 }