Skip to content

Commit

Permalink
pkg/util: use code from c/storage
Browse files Browse the repository at this point in the history
[NO NEW TESTS NEEDED] no new functionalities are added

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Nov 23, 2023
1 parent 478afa7 commit cd21973
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 115 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func resolveEventSock() ([]string, error) {
}

func eventSockDir() (string, error) {
xdg, err := util.GetRuntimeDir()
xdg, err := util.GetRootlessRuntimeDir()
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/registry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func setXdgDirs() error {

// Set up XDG_RUNTIME_DIR
if _, found := os.LookupEnv("XDG_RUNTIME_DIR"); !found {
dir, err := util.GetRuntimeDir()
dir, err := util.GetRootlessRuntimeDir()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func GetContext() context.Context {

func DefaultAPIAddress() string {
if rootless.IsRootless() {
xdg, err := util.GetRuntimeDir()
xdg, err := util.GetRootlessRuntimeDir()
if err != nil {
logrus.Warnf("Failed to get rootless runtime dir for DefaultAPIAddress: %s", err)
return DefaultRootAPIAddress
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/system/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func resolveAPIURI(uri []string) (string, error) {
logrus.Info("Using systemd socket activation to determine API endpoint")
return "", nil
case rootless.IsRootless():
xdg, err := util.GetRuntimeDir()
xdg, err := util.GetRootlessRuntimeDir()
if err != nil {
return "", err
}
Expand Down
1 change: 1 addition & 0 deletions contrib/tmpfile/podman.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# /tmp/podman-run-* directory can contain content for Podman containers that have run
# for many days. This following line prevents systemd from removing this content.
x /tmp/podman-run-*
x /tmp/storage-run-*
x /tmp/containers-user-*
x /tmp/run-*/libpod
D! /var/lib/containers/storage/tmp 0700 root root
Expand Down
2 changes: 1 addition & 1 deletion libpod/container_top_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (c *Container) execPS(psArgs []string) ([]string, bool, error) {
cmd.Stdout = wPipe
cmd.Stderr = &errBuf
// nil means use current env so explicitly unset all, to not leak any sensitive env vars
cmd.Env = []string{}
cmd.Env = []string{fmt.Sprintf("HOME=%s", os.Getenv("HOME"))}

retryContainerExec := true
err = cmd.Run()
Expand Down
2 changes: 1 addition & 1 deletion libpod/networking_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (r *RootlessNetNS) Do(toRun func() error) error {
return fmt.Errorf("cannot create a new mount namespace: %w", err)
}

xdgRuntimeDir, err := util.GetRuntimeDir()
xdgRuntimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return fmt.Errorf("could not get runtime directory: %w", err)
}
Expand Down
18 changes: 9 additions & 9 deletions libpod/oci_conmon_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (r *ConmonOCIRuntime) CreateContainer(ctr *Container, restoreOptions *Conta
// status, but will instead only check for the existence of the conmon exit file
// and update state to stopped if it exists.
func (r *ConmonOCIRuntime) UpdateContainerStatus(ctr *Container) error {
runtimeDir, err := util.GetRuntimeDir()
runtimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return err
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (r *ConmonOCIRuntime) UpdateContainerStatus(ctr *Container) error {
// Sets time the container was started, but does not save it.
func (r *ConmonOCIRuntime) StartContainer(ctr *Container) error {
// TODO: streams should probably *not* be our STDIN/OUT/ERR - redirect to buffers?
runtimeDir, err := util.GetRuntimeDir()
runtimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return err
}
Expand All @@ -309,7 +309,7 @@ func (r *ConmonOCIRuntime) StartContainer(ctr *Container) error {

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

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

// PauseContainer pauses the given container.
func (r *ConmonOCIRuntime) PauseContainer(ctr *Container) error {
runtimeDir, err := util.GetRuntimeDir()
runtimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return err
}
Expand All @@ -524,7 +524,7 @@ func (r *ConmonOCIRuntime) PauseContainer(ctr *Container) error {

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

runtimeDir, err := util.GetRuntimeDir()
runtimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -1324,7 +1324,7 @@ func (r *ConmonOCIRuntime) configureConmonEnv() ([]string, error) {
}
res = append(res, v)
}
runtimeDir, err := util.GetRuntimeDir()
runtimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion libpod/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (r *Runtime) reset(ctx context.Context) error {
prevError = err
}
}
runtimeDir, err := util.GetRuntimeDir()
runtimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion libpod/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func SetXdgDirs() error {

if runtimeDir == "" {
var err error
runtimeDir, err = util.GetRuntimeDir()
runtimeDir, err = util.GetRootlessRuntimeDir()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/abi/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (ic *ContainerEngine) Info(ctx context.Context) (*define.Info, error) {
// service may be run with another URI.
if ic.Libpod.RemoteURI() == "" {
xdg := defaultRunPath
if path, err := util.GetRuntimeDir(); err != nil {
if path, err := util.GetRootlessRuntimeDir(); err != nil {
// Info is as good as we can guess...
return info, err
} else if path != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/qemu/options_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ func getRuntimeDir() (string, error) {
if !rootless.IsRootless() {
return "/run", nil
}
return util.GetRuntimeDir()
return util.GetRootlessRuntimeDir()
}
8 changes: 0 additions & 8 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -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"`
}
Expand Down
92 changes: 6 additions & 86 deletions pkg/util/utils_supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,109 +7,29 @@ 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

// GetRootlessRuntimeDir returns the runtime directory when running as non root
func GetRootlessRuntimeDir() (string, 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
// the pause process.
func GetRootlessPauseProcessPidPath() (string, error) {
runtimeDir, err := GetRuntimeDir()
runtimeDir, err := GetRootlessRuntimeDir()
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func GetRootlessPauseProcessPidPath() (string, error) {
return "", fmt.Errorf("GetRootlessPauseProcessPidPath: %w", errNotImplemented)
}

// GetRuntimeDir returns the runtime directory
func GetRuntimeDir() (string, error) {
// GetRootlessRuntimeDir returns the runtime directory
func GetRootlessRuntimeDir() (string, error) {
data, err := homedir.GetDataHome()
if err != nil {
return "", err
Expand Down

0 comments on commit cd21973

Please sign in to comment.