@@ -6,21 +6,67 @@ package util
6
6
// should work to take darwin from this
7
7
8
8
import (
9
+ "fmt"
9
10
"os"
10
11
"path/filepath"
11
12
"syscall"
12
13
13
- cutil "github.com/containers/common/pkg/util"
14
14
"github.com/containers/podman/v4/pkg/rootless"
15
15
"github.com/pkg/errors"
16
+ "github.com/sirupsen/logrus"
16
17
)
17
18
18
19
// GetRuntimeDir returns the runtime directory
19
20
func GetRuntimeDir () (string , error ) {
21
+ var rootlessRuntimeDirError error
22
+
20
23
if ! rootless .IsRootless () {
21
24
return "" , nil
22
25
}
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
24
70
}
25
71
26
72
// GetRootlessConfigHomeDir returns the config home directory when running as non root
0 commit comments