Skip to content

Commit fd56b73

Browse files
author
Dylan Ratcliffe
committed
dbus: add options for name & path
PID-based queries can now return either a name or a path
1 parent 6bdf704 commit fd56b73

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

dbus/methods.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -417,22 +417,27 @@ func (c *Conn) listUnitsInternal(f storeFunc) ([]UnitStatus, error) {
417417
return status, nil
418418
}
419419

420-
func (c *Conn) getUnitInternal(f storeFunc) (string, error) {
420+
// GetUnitByPID returns the unit object path of the unit a process ID
421+
// belongs to. It takes a UNIX PID and returns the object path. The PID must
422+
// refer to an existing system process
423+
func (c *Conn) GetUnitByPID(ctx context.Context, pid uint32) (dbus.ObjectPath, error) {
421424
var result dbus.ObjectPath
422425

423-
err := f(&result)
424-
425-
// Nothing in this library actually accepts a dbus.ObjectPath, so it's much
426-
// more useful as a name
427-
name := unitName(result)
426+
err := c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.GetUnitByPID", 0, pid).Store(&result)
428427

429-
return name, err
428+
return result, err
430429
}
431430

432-
// GetUnitByPIDContext returns the unit name for a given PID. The PID must refer
433-
// to an existing system process
434-
func (c *Conn) GetUnitByPIDContext(ctx context.Context, pid uint32) (string, error) {
435-
return c.getUnitInternal(c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.GetUnitByPID", 0, pid).Store)
431+
// GetUnitNameByPID returns the name of the unit a process ID belongs to. It
432+
// takes a UNIX PID and returns the object path. The PID must refer to an
433+
// existing system process
434+
func (c *Conn) GetUnitNameByPID(ctx context.Context, pid uint32) (string, error) {
435+
path, err := c.GetUnitByPID(ctx, pid)
436+
if err != nil {
437+
return "", err
438+
}
439+
440+
return unitName(path), nil
436441
}
437442

438443
// Deprecated: use ListUnitsContext instead.

dbus/methods_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,28 @@ func TestReloadOrRestartUnit(t *testing.T) {
450450
}
451451
}
452452

453-
// Ensure that GetUnitByPIDContext works.
454-
func TestGetUnitByPIDContext(t *testing.T) {
453+
// Ensure that GetUnitByPID works.
454+
func TestGetUnitByPID(t *testing.T) {
455455
conn := setupConn(t)
456456
defer conn.Close()
457457

458-
name, err := conn.GetUnitByPIDContext(context.Background(), 1)
458+
path, err := conn.GetUnitByPID(context.Background(), 1)
459+
460+
if err != nil {
461+
t.Error(err)
462+
}
463+
464+
if path == "" {
465+
t.Fatal("path is empty")
466+
}
467+
}
468+
469+
// Ensure that GetUnitNameByPID works.
470+
func TestGetUnitNameByPID(t *testing.T) {
471+
conn := setupConn(t)
472+
defer conn.Close()
473+
474+
name, err := conn.GetUnitNameByPID(context.Background(), 1)
459475

460476
if err != nil {
461477
t.Error(err)

0 commit comments

Comments
 (0)