Files
wehub-resource-sync 36b3af2e3d
PR Check / Code Quality: Format (push) Failing after 1s
PR Check / Code Quality: Lint (darwin) (push) Failing after 0s
PR Check / Code Quality: Lint (freebsd) (push) Failing after 1s
PR Check / Code Quality: Lint (windows) (push) Failing after 1s
PR Check / Code Quality: Lint (linux) (push) Failing after 1s
PR Check / Security: Vulnerability Scan (push) Failing after 0s
Update Documentation / update-docs (push) Failing after 2s
PR Check / Code Quality: Vendor (push) Failing after 1s
PR Check / Code Quality: Coverage (push) Failing after 0s
PR Check / Tests: Unit (macos-latest) (push) Has been cancelled
PR Check / Tests: Unit (ubuntu-24.04) (push) Has been cancelled
PR Check / Tests: Unit (ubuntu-24.04-arm) (push) Has been cancelled
PR Check / Tests: Unit (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:22:06 +08:00

63 lines
1.5 KiB
Go

//go:build !windows
package proc
import (
"context"
"os"
"os/exec"
"os/user"
"strconv"
"syscall"
)
// commandAsOriginalUser builds an *exec.Cmd that, when invoked under sudo,
// runs as the original (non-root) user. This matters for tools like rootless
// podman and nerdctl whose state lives in $HOME/.local — invisible to root.
//
// When not running as root, or when SUDO_UID/SUDO_GID/SUDO_USER aren't set,
// it behaves identically to exec.CommandContext.
func commandAsOriginalUser(ctx context.Context, bin string, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, bin, args...)
if os.Geteuid() != 0 {
return cmd
}
uidStr := os.Getenv("SUDO_UID")
gidStr := os.Getenv("SUDO_GID")
sudoUser := os.Getenv("SUDO_USER")
if uidStr == "" || gidStr == "" || sudoUser == "" {
return cmd
}
uid, err1 := strconv.ParseUint(uidStr, 10, 32)
gid, err2 := strconv.ParseUint(gidStr, 10, 32)
if err1 != nil || err2 != nil || uid == 0 {
return cmd
}
u, err := user.Lookup(sudoUser)
if err != nil {
return cmd
}
cmd.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
},
}
// Rootless container tools resolve sockets and state under HOME and
// XDG_RUNTIME_DIR. Re-derive both so the child sees the original user's
// environment, not root's.
env := os.Environ()
env = append(env, "HOME="+u.HomeDir)
env = append(env, "USER="+sudoUser)
env = append(env, "LOGNAME="+sudoUser)
env = append(env, "XDG_RUNTIME_DIR=/run/user/"+uidStr)
cmd.Env = env
return cmd
}