diff --git a/libpod/runtime.go b/libpod/runtime.go index 8af1864843df..c7e9a54383c2 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -38,6 +38,7 @@ import ( "github.com/containers/podman/v4/pkg/util" "github.com/containers/podman/v4/utils" "github.com/containers/storage" + "github.com/containers/storage/pkg/homedir" "github.com/containers/storage/pkg/lockfile" "github.com/containers/storage/pkg/unshare" "github.com/docker/docker/pkg/namesgenerator" @@ -121,48 +122,6 @@ type Runtime struct { secretsManager *secrets.SecretsManager } -// SetXdgDirs ensures the XDG_RUNTIME_DIR env and XDG_CONFIG_HOME variables are set. -// containers/image uses XDG_RUNTIME_DIR to locate the auth file, XDG_CONFIG_HOME is -// use for the containers.conf configuration file. -func SetXdgDirs() error { - if !rootless.IsRootless() { - return nil - } - - // Set up XDG_RUNTIME_DIR - runtimeDir := os.Getenv("XDG_RUNTIME_DIR") - - if runtimeDir == "" { - var err error - runtimeDir, err = util.GetRuntimeDir() - if err != nil { - return err - } - } - if err := os.Setenv("XDG_RUNTIME_DIR", runtimeDir); err != nil { - return fmt.Errorf("cannot set XDG_RUNTIME_DIR: %w", err) - } - - if rootless.IsRootless() && os.Getenv("DBUS_SESSION_BUS_ADDRESS") == "" { - sessionAddr := filepath.Join(runtimeDir, "bus") - if _, err := os.Stat(sessionAddr); err == nil { - os.Setenv("DBUS_SESSION_BUS_ADDRESS", fmt.Sprintf("unix:path=%s", sessionAddr)) - } - } - - // Set up XDG_CONFIG_HOME - if cfgHomeDir := os.Getenv("XDG_CONFIG_HOME"); cfgHomeDir == "" { - cfgHomeDir, err := util.GetRootlessConfigHomeDir() - if err != nil { - return err - } - if err := os.Setenv("XDG_CONFIG_HOME", cfgHomeDir); err != nil { - return fmt.Errorf("cannot set XDG_CONFIG_HOME: %w", err) - } - } - return nil -} - // NewRuntime creates a new container runtime // Options can be passed to override the default configuration for the runtime func NewRuntime(ctx context.Context, options ...RuntimeOption) (*Runtime, error) { @@ -195,7 +154,7 @@ func newRuntimeFromConfig(conf *config.Config, options ...RuntimeOption) (*Runti runtime.config = conf - if err := SetXdgDirs(); err != nil { + if err := homedir.SetXdgDirs(); err != nil { return nil, err } diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 679110df2fe1..0d4fa4ef4c75 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -13,7 +13,6 @@ import ( "sort" "strconv" "strings" - "sync" "syscall" "time" @@ -920,13 +919,6 @@ func ParseIDMapping(mode namespaces.UsernsMode, uidMapSlice, gidMapSlice []strin return &options, nil } -var ( - rootlessConfigHomeDirOnce sync.Once - rootlessConfigHomeDir string - rootlessRuntimeDirOnce sync.Once - rootlessRuntimeDir string -) - type tomlOptionsConfig struct { MountProgram string `toml:"mount_program"` } diff --git a/pkg/util/utils_supported.go b/pkg/util/utils_supported.go index 406d56ce6fed..9b5a63fef430 100644 --- a/pkg/util/utils_supported.go +++ b/pkg/util/utils_supported.go @@ -7,103 +7,19 @@ package util // should work to take darwin from this import ( - "errors" - "fmt" - "os" "path/filepath" - "strconv" - "syscall" - "github.com/containers/podman/v4/pkg/rootless" - "github.com/sirupsen/logrus" + "github.com/containers/storage/pkg/homedir" ) // GetRuntimeDir returns the runtime directory func GetRuntimeDir() (string, error) { - var rootlessRuntimeDirError error - - if !rootless.IsRootless() { - return "", nil - } - - rootlessRuntimeDirOnce.Do(func() { - runtimeDir := os.Getenv("XDG_RUNTIME_DIR") - - if runtimeDir != "" { - rootlessRuntimeDir, rootlessRuntimeDirError = filepath.EvalSymlinks(runtimeDir) - return - } - - uid := strconv.Itoa(rootless.GetRootlessUID()) - if runtimeDir == "" { - tmpDir := filepath.Join("/run", "user", uid) - if err := os.MkdirAll(tmpDir, 0700); err != nil { - logrus.Debug(err) - } - st, err := os.Stat(tmpDir) - if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && (st.Mode().Perm()&0700 == 0700) { - runtimeDir = tmpDir - } - } - if runtimeDir == "" { - tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("podman-run-%s", uid)) - if err := os.MkdirAll(tmpDir, 0700); err != nil { - logrus.Debug(err) - } - st, err := os.Stat(tmpDir) - if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && (st.Mode().Perm()&0700 == 0700) { - runtimeDir = tmpDir - } - } - if runtimeDir == "" { - home := os.Getenv("HOME") - if home == "" { - rootlessRuntimeDirError = errors.New("neither XDG_RUNTIME_DIR nor HOME was set non-empty") - return - } - resolvedHome, err := filepath.EvalSymlinks(home) - if err != nil { - rootlessRuntimeDirError = fmt.Errorf("cannot resolve %s: %w", home, err) - return - } - runtimeDir = filepath.Join(resolvedHome, "rundir") - } - rootlessRuntimeDir = runtimeDir - }) - - if rootlessRuntimeDirError != nil { - return "", rootlessRuntimeDirError - } - return rootlessRuntimeDir, nil + return homedir.GetRuntimeDir() } // GetRootlessConfigHomeDir returns the config home directory when running as non root func GetRootlessConfigHomeDir() (string, error) { - var rootlessConfigHomeDirError error - - rootlessConfigHomeDirOnce.Do(func() { - cfgHomeDir := os.Getenv("XDG_CONFIG_HOME") - if cfgHomeDir == "" { - home := os.Getenv("HOME") - resolvedHome, err := filepath.EvalSymlinks(home) - if err != nil { - rootlessConfigHomeDirError = fmt.Errorf("cannot resolve %s: %w", home, err) - return - } - tmpDir := filepath.Join(resolvedHome, ".config") - st, err := os.Stat(tmpDir) - if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && st.Mode().Perm() >= 0700 { - cfgHomeDir = tmpDir - } - } - rootlessConfigHomeDir = cfgHomeDir - }) - - if rootlessConfigHomeDirError != nil { - return "", rootlessConfigHomeDirError - } - - return rootlessConfigHomeDir, nil + return homedir.GetConfigHome() } // GetRootlessPauseProcessPidPath returns the path to the file that holds the pid for