2018-02-05 16:05:59 -05:00
|
|
|
package reexec // import "github.com/docker/docker/pkg/reexec"
|
2014-08-08 16:18:18 -04:00
|
|
|
|
2014-08-11 21:13:27 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-08-12 17:57:09 -04:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2014-08-11 21:13:27 -04:00
|
|
|
)
|
2014-08-08 16:18:18 -04:00
|
|
|
|
|
|
|
var registeredInitializers = make(map[string]func())
|
|
|
|
|
|
|
|
// Register adds an initialization func under the specified name
|
|
|
|
func Register(name string, initializer func()) {
|
2014-08-11 21:13:27 -04:00
|
|
|
if _, exists := registeredInitializers[name]; exists {
|
2016-04-09 09:18:15 -04:00
|
|
|
panic(fmt.Sprintf("reexec func already registered under name %q", name))
|
2014-08-11 21:13:27 -04:00
|
|
|
}
|
|
|
|
|
2014-08-08 16:18:18 -04:00
|
|
|
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
|
|
|
|
}
|
2014-08-12 17:57:09 -04:00
|
|
|
|
2015-07-24 13:51:51 -04:00
|
|
|
func naiveSelf() string {
|
2014-08-12 17:57:09 -04:00
|
|
|
name := os.Args[0]
|
|
|
|
if filepath.Base(name) == name {
|
|
|
|
if lp, err := exec.LookPath(name); err == nil {
|
2015-03-16 15:54:35 -04:00
|
|
|
return lp
|
2014-08-12 17:57:09 -04:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 15:54:35 -04:00
|
|
|
// handle conversion of relative paths to absolute
|
|
|
|
if absName, err := filepath.Abs(name); err == nil {
|
|
|
|
return absName
|
|
|
|
}
|
2015-12-13 11:00:39 -05:00
|
|
|
// if we couldn't get absolute name, return original
|
2015-03-16 15:54:35 -04:00
|
|
|
// (NOTE: Go only errors on Abs() if os.Getwd fails)
|
2014-08-12 17:57:09 -04:00
|
|
|
return name
|
|
|
|
}
|