github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/util/utils_supported.go (about)

     1  // +build linux darwin
     2  
     3  package util
     4  
     5  // TODO once rootless function is consolidated under libpod, we
     6  //  should work to take darwin from this
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  	"syscall"
    13  
    14  	"github.com/containers/libpod/pkg/rootless"
    15  	"github.com/pkg/errors"
    16  	"github.com/sirupsen/logrus"
    17  )
    18  
    19  // GetRuntimeDir returns the runtime directory
    20  func GetRuntimeDir() (string, error) {
    21  	var rootlessRuntimeDirError error
    22  
    23  	if !rootless.IsRootless() {
    24  		return "", nil
    25  	}
    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.Debugf("unable to make temp dir %s", tmpDir)
    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("run-%s", uid))
    42  			if err := os.MkdirAll(tmpDir, 0700); err != nil {
    43  				logrus.Debugf("unable to make temp dir %s", tmpDir)
    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
    70  }
    71  
    72  // GetRootlessConfigHomeDir returns the config home directory when running as non root
    73  func GetRootlessConfigHomeDir() (string, error) {
    74  	var rootlessConfigHomeDirError error
    75  
    76  	rootlessConfigHomeDirOnce.Do(func() {
    77  		cfgHomeDir := os.Getenv("XDG_CONFIG_HOME")
    78  		if cfgHomeDir == "" {
    79  			home := os.Getenv("HOME")
    80  			resolvedHome, err := filepath.EvalSymlinks(home)
    81  			if err != nil {
    82  				rootlessConfigHomeDirError = errors.Wrapf(err, "cannot resolve %s", home)
    83  				return
    84  			}
    85  			tmpDir := filepath.Join(resolvedHome, ".config")
    86  			if err := os.MkdirAll(tmpDir, 0755); err != nil {
    87  				logrus.Errorf("unable to make temp dir %s", tmpDir)
    88  			}
    89  			st, err := os.Stat(tmpDir)
    90  			if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && st.Mode().Perm() >= 0700 {
    91  				cfgHomeDir = tmpDir
    92  			}
    93  		}
    94  		rootlessConfigHomeDir = cfgHomeDir
    95  	})
    96  
    97  	if rootlessConfigHomeDirError != nil {
    98  		return "", rootlessConfigHomeDirError
    99  	}
   100  
   101  	return rootlessConfigHomeDir, nil
   102  }
   103  
   104  // GetRootlessPauseProcessPidPath returns the path to the file that holds the pid for
   105  // the pause process
   106  func GetRootlessPauseProcessPidPath() (string, error) {
   107  	runtimeDir, err := GetRuntimeDir()
   108  	if err != nil {
   109  		return "", err
   110  	}
   111  	return filepath.Join(runtimeDir, "libpod", "pause.pid"), nil
   112  }