Skip to content

Commit cd21973

Browse files
committed
pkg/util: use code from c/storage
[NO NEW TESTS NEEDED] no new functionalities are added Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 478afa7 commit cd21973

File tree

15 files changed

+28
-115
lines changed

15 files changed

+28
-115
lines changed

cmd/podman/machine/machine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func resolveEventSock() ([]string, error) {
150150
}
151151

152152
func eventSockDir() (string, error) {
153-
xdg, err := util.GetRuntimeDir()
153+
xdg, err := util.GetRootlessRuntimeDir()
154154
if err != nil {
155155
return "", err
156156
}

cmd/podman/registry/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func setXdgDirs() error {
134134

135135
// Set up XDG_RUNTIME_DIR
136136
if _, found := os.LookupEnv("XDG_RUNTIME_DIR"); !found {
137-
dir, err := util.GetRuntimeDir()
137+
dir, err := util.GetRootlessRuntimeDir()
138138
if err != nil {
139139
return err
140140
}

cmd/podman/registry/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func GetContext() context.Context {
9999

100100
func DefaultAPIAddress() string {
101101
if rootless.IsRootless() {
102-
xdg, err := util.GetRuntimeDir()
102+
xdg, err := util.GetRootlessRuntimeDir()
103103
if err != nil {
104104
logrus.Warnf("Failed to get rootless runtime dir for DefaultAPIAddress: %s", err)
105105
return DefaultRootAPIAddress

cmd/podman/system/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func resolveAPIURI(uri []string) (string, error) {
130130
logrus.Info("Using systemd socket activation to determine API endpoint")
131131
return "", nil
132132
case rootless.IsRootless():
133-
xdg, err := util.GetRuntimeDir()
133+
xdg, err := util.GetRootlessRuntimeDir()
134134
if err != nil {
135135
return "", err
136136
}

contrib/tmpfile/podman.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# /tmp/podman-run-* directory can contain content for Podman containers that have run
22
# for many days. This following line prevents systemd from removing this content.
33
x /tmp/podman-run-*
4+
x /tmp/storage-run-*
45
x /tmp/containers-user-*
56
x /tmp/run-*/libpod
67
D! /var/lib/containers/storage/tmp 0700 root root

libpod/container_top_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func (c *Container) execPS(psArgs []string) ([]string, bool, error) {
328328
cmd.Stdout = wPipe
329329
cmd.Stderr = &errBuf
330330
// nil means use current env so explicitly unset all, to not leak any sensitive env vars
331-
cmd.Env = []string{}
331+
cmd.Env = []string{fmt.Sprintf("HOME=%s", os.Getenv("HOME"))}
332332

333333
retryContainerExec := true
334334
err = cmd.Run()

libpod/networking_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (r *RootlessNetNS) Do(toRun func() error) error {
8282
return fmt.Errorf("cannot create a new mount namespace: %w", err)
8383
}
8484

85-
xdgRuntimeDir, err := util.GetRuntimeDir()
85+
xdgRuntimeDir, err := util.GetRootlessRuntimeDir()
8686
if err != nil {
8787
return fmt.Errorf("could not get runtime directory: %w", err)
8888
}

libpod/oci_conmon_common.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (r *ConmonOCIRuntime) CreateContainer(ctr *Container, restoreOptions *Conta
209209
// status, but will instead only check for the existence of the conmon exit file
210210
// and update state to stopped if it exists.
211211
func (r *ConmonOCIRuntime) UpdateContainerStatus(ctr *Container) error {
212-
runtimeDir, err := util.GetRuntimeDir()
212+
runtimeDir, err := util.GetRootlessRuntimeDir()
213213
if err != nil {
214214
return err
215215
}
@@ -290,7 +290,7 @@ func (r *ConmonOCIRuntime) UpdateContainerStatus(ctr *Container) error {
290290
// Sets time the container was started, but does not save it.
291291
func (r *ConmonOCIRuntime) StartContainer(ctr *Container) error {
292292
// TODO: streams should probably *not* be our STDIN/OUT/ERR - redirect to buffers?
293-
runtimeDir, err := util.GetRuntimeDir()
293+
runtimeDir, err := util.GetRootlessRuntimeDir()
294294
if err != nil {
295295
return err
296296
}
@@ -309,7 +309,7 @@ func (r *ConmonOCIRuntime) StartContainer(ctr *Container) error {
309309

310310
// UpdateContainer updates the given container's cgroup configuration
311311
func (r *ConmonOCIRuntime) UpdateContainer(ctr *Container, resources *spec.LinuxResources) error {
312-
runtimeDir, err := util.GetRuntimeDir()
312+
runtimeDir, err := util.GetRootlessRuntimeDir()
313313
if err != nil {
314314
return err
315315
}
@@ -368,7 +368,7 @@ func (r *ConmonOCIRuntime) KillContainer(ctr *Container, signal uint, all bool)
368368
// *bytes.buffer and returned; otherwise, it is set to os.Stderr.
369369
func (r *ConmonOCIRuntime) killContainer(ctr *Container, signal uint, all, captureStderr bool) (*bytes.Buffer, error) {
370370
logrus.Debugf("Sending signal %d to container %s", signal, ctr.ID())
371-
runtimeDir, err := util.GetRuntimeDir()
371+
runtimeDir, err := util.GetRootlessRuntimeDir()
372372
if err != nil {
373373
return nil, err
374374
}
@@ -504,7 +504,7 @@ func (r *ConmonOCIRuntime) StopContainer(ctr *Container, timeout uint, all bool)
504504

505505
// DeleteContainer deletes a container from the OCI runtime.
506506
func (r *ConmonOCIRuntime) DeleteContainer(ctr *Container) error {
507-
runtimeDir, err := util.GetRuntimeDir()
507+
runtimeDir, err := util.GetRootlessRuntimeDir()
508508
if err != nil {
509509
return err
510510
}
@@ -514,7 +514,7 @@ func (r *ConmonOCIRuntime) DeleteContainer(ctr *Container) error {
514514

515515
// PauseContainer pauses the given container.
516516
func (r *ConmonOCIRuntime) PauseContainer(ctr *Container) error {
517-
runtimeDir, err := util.GetRuntimeDir()
517+
runtimeDir, err := util.GetRootlessRuntimeDir()
518518
if err != nil {
519519
return err
520520
}
@@ -524,7 +524,7 @@ func (r *ConmonOCIRuntime) PauseContainer(ctr *Container) error {
524524

525525
// UnpauseContainer unpauses the given container.
526526
func (r *ConmonOCIRuntime) UnpauseContainer(ctr *Container) error {
527-
runtimeDir, err := util.GetRuntimeDir()
527+
runtimeDir, err := util.GetRootlessRuntimeDir()
528528
if err != nil {
529529
return err
530530
}
@@ -851,7 +851,7 @@ func (r *ConmonOCIRuntime) CheckpointContainer(ctr *Container, options Container
851851
args = append(args, ctr.ID())
852852
logrus.Debugf("the args to checkpoint: %s %s", r.path, strings.Join(args, " "))
853853

854-
runtimeDir, err := util.GetRuntimeDir()
854+
runtimeDir, err := util.GetRootlessRuntimeDir()
855855
if err != nil {
856856
return 0, err
857857
}
@@ -1324,7 +1324,7 @@ func (r *ConmonOCIRuntime) configureConmonEnv() ([]string, error) {
13241324
}
13251325
res = append(res, v)
13261326
}
1327-
runtimeDir, err := util.GetRuntimeDir()
1327+
runtimeDir, err := util.GetRootlessRuntimeDir()
13281328
if err != nil {
13291329
return nil, err
13301330
}

libpod/reset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (r *Runtime) reset(ctx context.Context) error {
202202
prevError = err
203203
}
204204
}
205-
runtimeDir, err := util.GetRuntimeDir()
205+
runtimeDir, err := util.GetRootlessRuntimeDir()
206206
if err != nil {
207207
return err
208208
}

libpod/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func SetXdgDirs() error {
134134

135135
if runtimeDir == "" {
136136
var err error
137-
runtimeDir, err = util.GetRuntimeDir()
137+
runtimeDir, err = util.GetRootlessRuntimeDir()
138138
if err != nil {
139139
return err
140140
}

pkg/domain/infra/abi/system.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (ic *ContainerEngine) Info(ctx context.Context) (*define.Info, error) {
3636
// service may be run with another URI.
3737
if ic.Libpod.RemoteURI() == "" {
3838
xdg := defaultRunPath
39-
if path, err := util.GetRuntimeDir(); err != nil {
39+
if path, err := util.GetRootlessRuntimeDir(); err != nil {
4040
// Info is as good as we can guess...
4141
return info, err
4242
} else if path != "" {

pkg/machine/qemu/options_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ func getRuntimeDir() (string, error) {
99
if !rootless.IsRootless() {
1010
return "/run", nil
1111
}
12-
return util.GetRuntimeDir()
12+
return util.GetRootlessRuntimeDir()
1313
}

pkg/util/utils.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"sort"
1414
"strconv"
1515
"strings"
16-
"sync"
1716
"syscall"
1817
"time"
1918

@@ -920,13 +919,6 @@ func ParseIDMapping(mode namespaces.UsernsMode, uidMapSlice, gidMapSlice []strin
920919
return &options, nil
921920
}
922921

923-
var (
924-
rootlessConfigHomeDirOnce sync.Once
925-
rootlessConfigHomeDir string
926-
rootlessRuntimeDirOnce sync.Once
927-
rootlessRuntimeDir string
928-
)
929-
930922
type tomlOptionsConfig struct {
931923
MountProgram string `toml:"mount_program"`
932924
}

pkg/util/utils_supported.go

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,109 +7,29 @@ package util
77
// should work to take darwin from this
88

99
import (
10-
"errors"
11-
"fmt"
12-
"os"
1310
"path/filepath"
14-
"strconv"
15-
"syscall"
1611

1712
"github.com/containers/podman/v4/pkg/rootless"
18-
"github.com/sirupsen/logrus"
13+
"github.com/containers/storage/pkg/homedir"
1914
)
2015

21-
// GetRuntimeDir returns the runtime directory
22-
func GetRuntimeDir() (string, error) {
23-
var rootlessRuntimeDirError error
24-
16+
// GetRootlessRuntimeDir returns the runtime directory when running as non root
17+
func GetRootlessRuntimeDir() (string, error) {
2518
if !rootless.IsRootless() {
2619
return "", nil
2720
}
28-
29-
rootlessRuntimeDirOnce.Do(func() {
30-
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
31-
32-
if runtimeDir != "" {
33-
rootlessRuntimeDir, rootlessRuntimeDirError = filepath.EvalSymlinks(runtimeDir)
34-
return
35-
}
36-
37-
uid := strconv.Itoa(rootless.GetRootlessUID())
38-
if runtimeDir == "" {
39-
tmpDir := filepath.Join("/run", "user", uid)
40-
if err := os.MkdirAll(tmpDir, 0700); err != nil {
41-
logrus.Debug(err)
42-
}
43-
st, err := os.Stat(tmpDir)
44-
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && (st.Mode().Perm()&0700 == 0700) {
45-
runtimeDir = tmpDir
46-
}
47-
}
48-
if runtimeDir == "" {
49-
tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("podman-run-%s", uid))
50-
if err := os.MkdirAll(tmpDir, 0700); err != nil {
51-
logrus.Debug(err)
52-
}
53-
st, err := os.Stat(tmpDir)
54-
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && (st.Mode().Perm()&0700 == 0700) {
55-
runtimeDir = tmpDir
56-
}
57-
}
58-
if runtimeDir == "" {
59-
home := os.Getenv("HOME")
60-
if home == "" {
61-
rootlessRuntimeDirError = errors.New("neither XDG_RUNTIME_DIR nor HOME was set non-empty")
62-
return
63-
}
64-
resolvedHome, err := filepath.EvalSymlinks(home)
65-
if err != nil {
66-
rootlessRuntimeDirError = fmt.Errorf("cannot resolve %s: %w", home, err)
67-
return
68-
}
69-
runtimeDir = filepath.Join(resolvedHome, "rundir")
70-
}
71-
rootlessRuntimeDir = runtimeDir
72-
})
73-
74-
if rootlessRuntimeDirError != nil {
75-
return "", rootlessRuntimeDirError
76-
}
77-
return rootlessRuntimeDir, nil
21+
return homedir.GetRuntimeDir()
7822
}
7923

8024
// GetRootlessConfigHomeDir returns the config home directory when running as non root
8125
func GetRootlessConfigHomeDir() (string, error) {
82-
var rootlessConfigHomeDirError error
83-
84-
rootlessConfigHomeDirOnce.Do(func() {
85-
cfgHomeDir := os.Getenv("XDG_CONFIG_HOME")
86-
if cfgHomeDir == "" {
87-
home := os.Getenv("HOME")
88-
resolvedHome, err := filepath.EvalSymlinks(home)
89-
if err != nil {
90-
rootlessConfigHomeDirError = fmt.Errorf("cannot resolve %s: %w", home, err)
91-
return
92-
}
93-
tmpDir := filepath.Join(resolvedHome, ".config")
94-
st, err := os.Stat(tmpDir)
95-
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && st.Mode().Perm() >= 0700 {
96-
cfgHomeDir = tmpDir
97-
}
98-
}
99-
rootlessConfigHomeDir = cfgHomeDir
100-
})
101-
102-
if rootlessConfigHomeDirError != nil {
103-
return "", rootlessConfigHomeDirError
104-
}
105-
106-
return rootlessConfigHomeDir, nil
26+
return homedir.GetConfigHome()
10727
}
10828

10929
// GetRootlessPauseProcessPidPath returns the path to the file that holds the pid for
11030
// the pause process.
11131
func GetRootlessPauseProcessPidPath() (string, error) {
112-
runtimeDir, err := GetRuntimeDir()
32+
runtimeDir, err := GetRootlessRuntimeDir()
11333
if err != nil {
11434
return "", err
11535
}

pkg/util/utils_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func GetRootlessPauseProcessPidPath() (string, error) {
3030
return "", fmt.Errorf("GetRootlessPauseProcessPidPath: %w", errNotImplemented)
3131
}
3232

33-
// GetRuntimeDir returns the runtime directory
34-
func GetRuntimeDir() (string, error) {
33+
// GetRootlessRuntimeDir returns the runtime directory
34+
func GetRootlessRuntimeDir() (string, error) {
3535
data, err := homedir.GetDataHome()
3636
if err != nil {
3737
return "", err

0 commit comments

Comments
 (0)