Skip to content

Commit

Permalink
pkg/homedir: new function GetRuntimeDirUser()
Browse files Browse the repository at this point in the history
[NO NEW TESTS NEEDED] moved from Podman

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Oct 30, 2023
1 parent 235f4d4 commit 2ae9667
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/homedir/homedir_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ func StickRuntimeDirContents(files []string) ([]string, error) {
func GetRootlessConfigHomeDir() (string, error) {
return "", errors.New("homedir.GetRootlessConfigHomeDir() is not supported on this system")
}

// GetRuntimeDirUser is unsupported on non-linux system.
func GetRuntimeDirUser(rootless bool, rootlessUID int) (string, error) {
return "", errors.New("homedir.GetRuntimeDirUser() is not supported on this system")
}
63 changes: 63 additions & 0 deletions pkg/homedir/homedir_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"

"github.com/containers/storage/pkg/unshare"
"github.com/sirupsen/logrus"
)

// Key returns the env var name for the user's home dir based on
Expand Down Expand Up @@ -101,6 +103,8 @@ func stick(f string) error {
var (
rootlessConfigHomeDirOnce sync.Once
rootlessConfigHomeDir string
rootlessRuntimeDirOnce sync.Once
rootlessRuntimeDir string
)

// GetRootlessConfigHomeDir returns the config home directory when running as non root
Expand Down Expand Up @@ -131,3 +135,62 @@ func GetRootlessConfigHomeDir() (string, error) {

return rootlessConfigHomeDir, nil
}

// GetRuntimeDirUser returns the runtime directory
func GetRuntimeDirUser(rootless bool, rootlessUID int) (string, error) {
var rootlessRuntimeDirError error

if !rootless {
return "", nil
}

rootlessRuntimeDirOnce.Do(func() {
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")

if runtimeDir != "" {
rootlessRuntimeDir, rootlessRuntimeDirError = filepath.EvalSymlinks(runtimeDir)
return
}

uid := strconv.Itoa(rootlessUID)
if runtimeDir == "" {
tmpDir := filepath.Join("/run", "user", uid)
if err := os.MkdirAll(tmpDir, 0o700); 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()&0o700 == 0o700) {
runtimeDir = tmpDir
}
}
if runtimeDir == "" {
tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("storage-run-%s", uid))
if err := os.MkdirAll(tmpDir, 0o700); 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()&0o700 == 0o700) {
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
}

0 comments on commit 2ae9667

Please sign in to comment.