Skip to content

Commit

Permalink
Merge pull request containers#1127 from filbranden/bindir1
Browse files Browse the repository at this point in the history
config: Allow specifying helper dirs with $BINDIR as base directory
  • Loading branch information
openshift-merge-robot authored Aug 23, 2022
2 parents def7fd8 + 4e68288 commit b3ac39a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
_configPath = "containers/containers.conf"
// UserOverrideContainersConfig holds the containers config path overridden by the rootless user
UserOverrideContainersConfig = ".config/" + _configPath
// Token prefix for looking for helper binary under $BINDIR
bindirPrefix = "$BINDIR"
)

// RuntimeStateStore is a constant indicating which state store implementation
Expand Down Expand Up @@ -1248,17 +1250,62 @@ func (c *Config) ActiveDestination() (uri, identity string, err error) {
return "", "", errors.New("no service destination configured")
}

var (
bindirFailed = false
bindirCached = ""
)

func findBindir() string {
if bindirCached != "" || bindirFailed {
return bindirCached
}
execPath, err := os.Executable()
if err == nil {
// Resolve symbolic links to find the actual binary file path.
execPath, err = filepath.EvalSymlinks(execPath)
}
if err != nil {
// If failed to find executable (unlikely to happen), warn about it.
// The bindirFailed flag will track this, so we only warn once.
logrus.Warnf("Failed to find $BINDIR: %v", err)
bindirFailed = true
return ""
}
bindirCached = filepath.Dir(execPath)
return bindirCached
}

// FindHelperBinary will search the given binary name in the configured directories.
// If searchPATH is set to true it will also search in $PATH.
func (c *Config) FindHelperBinary(name string, searchPATH bool) (string, error) {
dirList := c.Engine.HelperBinariesDir
bindirPath := ""
bindirSearched := false

// If set, search this directory first. This is used in testing.
if dir, found := os.LookupEnv("CONTAINERS_HELPER_BINARY_DIR"); found {
dirList = append([]string{dir}, dirList...)
}

for _, path := range dirList {
if path == bindirPrefix || strings.HasPrefix(path, bindirPrefix+string(filepath.Separator)) {
// Calculate the path to the executable first time we encounter a $BINDIR prefix.
if !bindirSearched {
bindirSearched = true
bindirPath = findBindir()
}
// If there's an error, don't stop the search for the helper binary.
// findBindir() will have warned once during the first failure.
if bindirPath == "" {
continue
}
// Replace the $BINDIR prefix with the path to the directory of the current binary.
if path == bindirPrefix {
path = bindirPath
} else {
path = filepath.Join(bindirPath, strings.TrimPrefix(path, bindirPrefix+string(filepath.Separator)))
}
}
fullpath := filepath.Join(path, name)
if fi, err := os.Stat(fullpath); err == nil && fi.Mode().IsRegular() {
return fullpath, nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ var defaultHelperBinariesDir = []string{
"/usr/local/lib/podman",
"/usr/libexec/podman",
"/usr/lib/podman",
// Relative to the binary directory
"$BINDIR/../libexec/podman",
}

0 comments on commit b3ac39a

Please sign in to comment.