2014-10-30 12:48:30 +00:00
|
|
|
package reexec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
var registeredInitializers = make(map[string]func())
|
|
|
|
|
|
|
|
// Register adds an initialization func under the specified name
|
|
|
|
func Register(name string, initializer func()) {
|
|
|
|
if _, exists := registeredInitializers[name]; exists {
|
|
|
|
panic(fmt.Sprintf("reexec func already registred 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 {
|
|
|
|
initializer()
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:51:51 +00:00
|
|
|
func naiveSelf() string {
|
2014-10-30 12:48:30 +00:00
|
|
|
name := os.Args[0]
|
|
|
|
if filepath.Base(name) == name {
|
|
|
|
if lp, err := exec.LookPath(name); err == nil {
|
2015-03-16 19:54:35 +00:00
|
|
|
return lp
|
2014-10-30 12:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 19:54:35 +00:00
|
|
|
// handle conversion of relative paths to absolute
|
|
|
|
if absName, err := filepath.Abs(name); err == nil {
|
|
|
|
return absName
|
|
|
|
}
|
|
|
|
// if we coudn't get absolute name, return original
|
|
|
|
// (NOTE: Go only errors on Abs() if os.Getwd fails)
|
2014-10-30 12:48:30 +00:00
|
|
|
return name
|
|
|
|
}
|