Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions systemd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@ func newUserSystemdDbus() (*systemdDbus.Conn, error) {

// DetectUID detects UID from the OwnerUID field of `busctl --user status`
// if running in userNS. The value corresponds to sd_bus_creds_get_owner_uid(3) .
// If running under RootlessKit, the value of $ROOTLESSKIT_PARENT_EUID is
// used instead.
//
// Otherwise returns os.Getuid() .
func DetectUID() (int, error) {
if !userns.RunningInUserNS() {
return os.Getuid(), nil
}
// RootlessKit sets $ROOTLESSKIT_PARENT_EUID for its child process.
// Prefer it to busctl, which fails to obtain the OwnerUID when
// /proc is mounted with hidepid=2 (or hidepid=invisible).
if env := os.Getenv("ROOTLESSKIT_PARENT_EUID"); env != "" {
uid, err := strconv.Atoi(env)
if err != nil {
return -1, fmt.Errorf("invalid $ROOTLESSKIT_PARENT_EUID value %q: %w", env, err)
}
return uid, nil
}
b, err := exec.Command("busctl", "--user", "--no-pager", "status").CombinedOutput()
if err != nil {
return -1, fmt.Errorf("could not execute `busctl --user --no-pager status` (output: %q): %w", string(b), err)
Expand Down
Loading