Skip to content

Commit

Permalink
Add uid,huid descriptors
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Resztak <[email protected]>
  • Loading branch information
presztak committed Oct 8, 2022
1 parent 6853da1 commit 3fc6319
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ The ps library is compatible with all AIX format descriptors of the ps command-l
- The corresponding host PID of a container process.
- **huser**
- The corresponding effective user of a container process on the host.
- **huid**
- The corresponding host UID of a container process.
- **label**
- Current security attributes of the process.
- **seccomp**
Expand Down
33 changes: 33 additions & 0 deletions psgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ var (
header: "USER",
procFn: processUSER,
},
{
normal: "uid",
header: "UID",
procFn: processUID,
},
{
code: "%a",
normal: "args",
Expand Down Expand Up @@ -294,6 +299,12 @@ var (
onHost: true,
procFn: processHUSER,
},
{
normal: "huid",
header: "HUID",
onHost: true,
procFn: processHUID,
},
{
normal: "hgroup",
header: "HGROUP",
Expand Down Expand Up @@ -648,6 +659,11 @@ func processUSER(p *process.Process, ctx *psContext) (string, error) {
return process.LookupUID(p.Status.Uids[1])
}

// processUID returns the effective UID of the process as the decimal representation.
func processUID(p *process.Process, ctx *psContext) (string, error) {
return p.Status.Uids[1], nil
}

// processRUSER returns the effective user name of the process. This will be
// the textual user ID, if it can be obtained, or a decimal representation
// otherwise.
Expand Down Expand Up @@ -857,6 +873,23 @@ func processHUSER(p *process.Process, ctx *psContext) (string, error) {
return "?", nil
}

// processHUID returns the effective UID of the corresponding host process
// of the (container) as the decimal representation or "?" if no corresponding
// process could be found.
func processHUID(p *process.Process, ctx *psContext) (string, error) {
if hp := findHostProcess(p, ctx); hp != nil {
if ctx.opts != nil && len(ctx.opts.UIDMap) > 0 {
// Return uid without searching its textual representation.
lookupFunc := func(uid string) (string, error) {
return uid, nil
}
return findID(hp.Status.Uids[1], ctx.opts.UIDMap, lookupFunc, "/proc/sys/fs/overflowuid")
}
return hp.Status.Uids[1], nil
}
return "?", nil
}

// processHGROUP returns the effective group ID of the corresponding host
// process of the (container) or "?" if no corresponding process could be
// found.
Expand Down
18 changes: 17 additions & 1 deletion test/format.bats
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
[[ ${lines[0]} =~ "USER" ]]
}

@test "UID header" {
run ./bin/psgo -format "uid"
[ "$status" -eq 0 ]
[[ ${lines[0]} =~ "UID" ]]
}

@test "COMMAND (args) header" {
run ./bin/psgo -format "%a"
[ "$status" -eq 0 ]
Expand Down Expand Up @@ -211,6 +217,14 @@
[[ ${lines[1]} =~ "?" ]]
}

@test "HUID header" {
run ./bin/psgo -format "huid"
[ "$status" -eq 0 ]
[[ ${lines[0]} =~ "HUID" ]]
# host UIDs are only extracted with `-pid`
[[ ${lines[1]} =~ "?" ]]
}

@test "HGROUP header" {
run ./bin/psgo -format "hgroup"
[ "$status" -eq 0 ]
Expand Down Expand Up @@ -265,14 +279,15 @@ function is_labeling_enabled() {
}

@test "ALL header" {
run ./bin/psgo -format "pcpu, group, groups, ppid, user, args, comm, rgroup, nice, pid, pgid, etime, ruser, time, tty, vsz, capamb, capinh, capprm, capeff, capbnd, seccomp, hpid, huser, hgroup, hgroups, rss, state"
run ./bin/psgo -format "pcpu, group, groups, ppid, user, uid, args, comm, rgroup, nice, pid, pgid, etime, ruser, time, tty, vsz, capamb, capinh, capprm, capeff, capbnd, seccomp, hpid, huser, huid, hgroup, hgroups, rss, state"
[ "$status" -eq 0 ]

[[ ${lines[0]} =~ "%CPU" ]]
[[ ${lines[0]} =~ "GROUP" ]]
[[ ${lines[0]} =~ "GROUPS" ]]
[[ ${lines[0]} =~ "PPID" ]]
[[ ${lines[0]} =~ "USER" ]]
[[ ${lines[0]} =~ "UID" ]]
[[ ${lines[0]} =~ "COMMAND" ]]
[[ ${lines[0]} =~ "COMMAND" ]]
[[ ${lines[0]} =~ "RGROUP" ]]
Expand All @@ -291,6 +306,7 @@ function is_labeling_enabled() {
[[ ${lines[0]} =~ "SECCOMP" ]]
[[ ${lines[0]} =~ "HPID" ]]
[[ ${lines[0]} =~ "HUSER" ]]
[[ ${lines[0]} =~ "HUID" ]]
[[ ${lines[0]} =~ "HGROUP" ]]
[[ ${lines[0]} =~ "HGROUPS" ]]
[[ ${lines[0]} =~ "RSS" ]]
Expand Down
2 changes: 1 addition & 1 deletion test/list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
@test "List descriptors" {
run ./bin/psgo -list
[ "$status" -eq 0 ]
[[ ${lines[0]} =~ "args, capamb, capbnd, capeff, capinh, capprm, comm, etime, group, groups, hgroup, hgroups, hpid, huser, label, nice, pcpu, pgid, pid, ppid, rgroup, rss, ruser, seccomp, state, stime, time, tty, user, vsz" ]]
[[ ${lines[0]} =~ "args, capamb, capbnd, capeff, capinh, capprm, comm, etime, group, groups, hgroup, hgroups, hpid, huid, huser, label, nice, pcpu, pgid, pid, ppid, rgroup, rss, ruser, seccomp, state, stime, time, tty, uid, user, vsz" ]]
}

0 comments on commit 3fc6319

Please sign in to comment.