Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbus: add GetUnitProcessesContext to list any unit's running processes #379

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
35 changes: 35 additions & 0 deletions dbus/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,38 @@ func (c *Conn) FreezeUnit(ctx context.Context, unit string) error {
func (c *Conn) ThawUnit(ctx context.Context, unit string) error {
return c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.ThawUnit", 0, unit).Store()
}

type Process struct {
Path string // Where this process exists in the unit/cgroup hierarchy
PID uint64 // The numeric process ID (PID)
Command string // The process command and arguments as a string
}

// GetUnitProcessesContext returns an array with all currently running processes in a unit *including* its child units.
func (c *Conn) GetUnitProcessesContext(ctx context.Context, unit string) ([]Process, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can directly call this GetUnitProcesses, plus avoid the separate *Internal helper.

return c.getUnitProcessesInternal(ctx, unit)
}

func (c *Conn) getUnitProcessesInternal(ctx context.Context, unit string) ([]Process, error) {
result := make([][]interface{}, 0)
if err := c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.GetUnitProcesses", 0, unit).Store(&result); err != nil {
return nil, err
}

resultInterface := make([]interface{}, len(result))
for i := range result {
resultInterface[i] = result[i]
}

process := make([]Process, len(result))
processInterface := make([]interface{}, len(process))
for i := range process {
processInterface[i] = &process[i]
}

if err := dbus.Store(resultInterface, processInterface...); err != nil {
return nil, err
}

return process, nil
}
40 changes: 40 additions & 0 deletions dbus/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"path"
"path/filepath"
"reflect"
"strings"
"syscall"
"testing"
"time"

"github.com/coreos/go-systemd/v22/util"
"github.com/godbus/dbus/v5"
)

Expand Down Expand Up @@ -1657,3 +1659,41 @@ func TestFreezer(t *testing.T) {

runStopUnit(t, conn, TrUnitProp{target, nil})
}

func TestListUnitProcesses(t *testing.T) {
target, err := util.GetRunningSlice() // This test should still pass even if the cmd is spawned in a child unit (i.e. session Scope) of the current Slice
if err != nil {
t.Fatal(err)
}

conn := setupConn(t)
defer conn.Close()

cmd := exec.Command("/bin/sleep", "400")
err = cmd.Start()
if err != nil {
t.Fatal(err)
}
defer cmd.Process.Kill()

pid := uint64(cmd.Process.Pid)

ctx := context.Background()
processes, err := conn.GetUnitProcessesContext(ctx, target)

if err != nil {
t.Fatal(err)
}

exists := false
for _, p := range processes {
if p.PID == pid && strings.HasPrefix(p.Command, "/bin/sleep") {
exists = true
t.Logf("Found %v\n", p)
}
}

if !exists {
t.Errorf("PID %d ('/bin/sleep 400') not found in current Slice unit's process list", pid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this test is failing on some CI runs. I suspect there is a race between spawning the child sleep and getting unit processes. It could be useful to retry the GetUnitProcessesContext() a few times until we get a non-empty set with /bin/sleep, or eventually timeout if we never get that.

}
}