Skip to content

Commit 791c8c3

Browse files
committed
Revert "use GetRuntimeDir() from c/common"
This reverts commit fc5cf81. [NO NEW TESTS NEEDED] Signed-off-by: Brent Baude <[email protected]>
1 parent cc4a70c commit 791c8c3

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

pkg/util/utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ func ParseIDMapping(mode namespaces.UsernsMode, uidMapSlice, gidMapSlice []strin
463463
var (
464464
rootlessConfigHomeDirOnce sync.Once
465465
rootlessConfigHomeDir string
466+
rootlessRuntimeDirOnce sync.Once
467+
rootlessRuntimeDir string
466468
)
467469

468470
type tomlOptionsConfig struct {

pkg/util/utils_supported.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,67 @@ package util
66
// should work to take darwin from this
77

88
import (
9+
"fmt"
910
"os"
1011
"path/filepath"
1112
"syscall"
1213

13-
cutil "github.com/containers/common/pkg/util"
1414
"github.com/containers/podman/v4/pkg/rootless"
1515
"github.com/pkg/errors"
16+
"github.com/sirupsen/logrus"
1617
)
1718

1819
// GetRuntimeDir returns the runtime directory
1920
func GetRuntimeDir() (string, error) {
21+
var rootlessRuntimeDirError error
22+
2023
if !rootless.IsRootless() {
2124
return "", nil
2225
}
23-
return cutil.GetRuntimeDir()
26+
27+
rootlessRuntimeDirOnce.Do(func() {
28+
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
29+
uid := fmt.Sprintf("%d", rootless.GetRootlessUID())
30+
if runtimeDir == "" {
31+
tmpDir := filepath.Join("/run", "user", uid)
32+
if err := os.MkdirAll(tmpDir, 0700); err != nil {
33+
logrus.Debug(err)
34+
}
35+
st, err := os.Stat(tmpDir)
36+
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && (st.Mode().Perm()&0700 == 0700) {
37+
runtimeDir = tmpDir
38+
}
39+
}
40+
if runtimeDir == "" {
41+
tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("podman-run-%s", uid))
42+
if err := os.MkdirAll(tmpDir, 0700); err != nil {
43+
logrus.Debug(err)
44+
}
45+
st, err := os.Stat(tmpDir)
46+
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && (st.Mode().Perm()&0700 == 0700) {
47+
runtimeDir = tmpDir
48+
}
49+
}
50+
if runtimeDir == "" {
51+
home := os.Getenv("HOME")
52+
if home == "" {
53+
rootlessRuntimeDirError = fmt.Errorf("neither XDG_RUNTIME_DIR nor HOME was set non-empty")
54+
return
55+
}
56+
resolvedHome, err := filepath.EvalSymlinks(home)
57+
if err != nil {
58+
rootlessRuntimeDirError = errors.Wrapf(err, "cannot resolve %s", home)
59+
return
60+
}
61+
runtimeDir = filepath.Join(resolvedHome, "rundir")
62+
}
63+
rootlessRuntimeDir = runtimeDir
64+
})
65+
66+
if rootlessRuntimeDirError != nil {
67+
return "", rootlessRuntimeDirError
68+
}
69+
return rootlessRuntimeDir, nil
2470
}
2571

2672
// GetRootlessConfigHomeDir returns the config home directory when running as non root

0 commit comments

Comments
 (0)