mirror of https://github.com/containers/podman.git
utils: call GetRootlessRuntimeDir once
use a sync.Once to potentially avoid multiple system calls everytime the function is called. Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
parent
2fa9861d78
commit
ca38ca49b8
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -181,38 +182,54 @@ func ParseIDMapping(UIDMapSlice, GIDMapSlice []string, subUIDMap, subGIDMap stri
|
||||||
return &options, nil
|
return &options, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
rootlessRuntimeDirOnce sync.Once
|
||||||
|
rootlessRuntimeDir string
|
||||||
|
)
|
||||||
|
|
||||||
// GetRootlessRuntimeDir returns the runtime directory when running as non root
|
// GetRootlessRuntimeDir returns the runtime directory when running as non root
|
||||||
func GetRootlessRuntimeDir() (string, error) {
|
func GetRootlessRuntimeDir() (string, error) {
|
||||||
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
|
var rootlessRuntimeDirError error
|
||||||
uid := fmt.Sprintf("%d", rootless.GetRootlessUID())
|
|
||||||
if runtimeDir == "" {
|
rootlessRuntimeDirOnce.Do(func() {
|
||||||
tmpDir := filepath.Join("/run", "user", uid)
|
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
|
||||||
os.MkdirAll(tmpDir, 0700)
|
uid := fmt.Sprintf("%d", rootless.GetRootlessUID())
|
||||||
st, err := os.Stat(tmpDir)
|
if runtimeDir == "" {
|
||||||
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && st.Mode().Perm() == 0700 {
|
tmpDir := filepath.Join("/run", "user", uid)
|
||||||
runtimeDir = tmpDir
|
os.MkdirAll(tmpDir, 0700)
|
||||||
|
st, err := os.Stat(tmpDir)
|
||||||
|
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && st.Mode().Perm() == 0700 {
|
||||||
|
runtimeDir = tmpDir
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if runtimeDir == "" {
|
||||||
|
tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("run-%s", uid))
|
||||||
|
os.MkdirAll(tmpDir, 0700)
|
||||||
|
st, err := os.Stat(tmpDir)
|
||||||
|
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && st.Mode().Perm() == 0700 {
|
||||||
|
runtimeDir = tmpDir
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if runtimeDir == "" {
|
||||||
|
home := os.Getenv("HOME")
|
||||||
|
if home == "" {
|
||||||
|
rootlessRuntimeDirError = fmt.Errorf("neither XDG_RUNTIME_DIR nor HOME was set non-empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resolvedHome, err := filepath.EvalSymlinks(home)
|
||||||
|
if err != nil {
|
||||||
|
rootlessRuntimeDirError = errors.Wrapf(err, "cannot resolve %s", home)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
runtimeDir = filepath.Join(resolvedHome, "rundir")
|
||||||
|
}
|
||||||
|
rootlessRuntimeDir = runtimeDir
|
||||||
|
})
|
||||||
|
|
||||||
|
if rootlessRuntimeDirError != nil {
|
||||||
|
return "", rootlessRuntimeDirError
|
||||||
}
|
}
|
||||||
if runtimeDir == "" {
|
return rootlessRuntimeDir, nil
|
||||||
tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("run-%s", uid))
|
|
||||||
os.MkdirAll(tmpDir, 0700)
|
|
||||||
st, err := os.Stat(tmpDir)
|
|
||||||
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && st.Mode().Perm() == 0700 {
|
|
||||||
runtimeDir = tmpDir
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if runtimeDir == "" {
|
|
||||||
home := os.Getenv("HOME")
|
|
||||||
if home == "" {
|
|
||||||
return "", fmt.Errorf("neither XDG_RUNTIME_DIR nor HOME was set non-empty")
|
|
||||||
}
|
|
||||||
resolvedHome, err := filepath.EvalSymlinks(home)
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.Wrapf(err, "cannot resolve %s", home)
|
|
||||||
}
|
|
||||||
runtimeDir = filepath.Join(resolvedHome, "rundir")
|
|
||||||
}
|
|
||||||
return runtimeDir, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRootlessDirInfo returns the parent path of where the storage for containers and
|
// GetRootlessDirInfo returns the parent path of where the storage for containers and
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue