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 GetUnitByPID #298

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions dbus/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ func (c *Conn) listUnitsInternal(f storeFunc) ([]UnitStatus, error) {
return status, nil
}

// GetUnitByPID returns an array with all currently loaded units. Note that
Copy link
Contributor

Choose a reason for hiding this comment

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

There is part of a sentence missing after "Note that".

Copy link
Author

Choose a reason for hiding this comment

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

ack

func (c *Conn) GetUnitByPID(pid int) (dbus.ObjectPath, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Input parameter should be a uint32 to match DBus signature.

Copy link
Author

Choose a reason for hiding this comment

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

I agree. I was thinking of using int because its what the go stdlib uses for pids (oddly enough), but I'd prefer to adhere to the dbus signature

var op dbus.ObjectPath
if err := c.sysobj.Call("org.freedesktop.systemd1.Manager.GetUnitByPID", 0, uint(pid)).Store(&op); err != nil {
return dbus.ObjectPath(""), err
}
return op, nil
}

// ListUnits returns an array with all currently loaded units. Note that
// units may be known by multiple names at the same time, and hence there might
// be more unit names loaded than actual units behind them.
Expand Down
43 changes: 43 additions & 0 deletions dbus/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ package dbus

import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"strconv"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -132,6 +135,18 @@ func runStopUnit(t *testing.T, conn *Conn, trTarget TrUnitProp) error {
return nil
}

func assertNoError(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}

func assertEqualStr(t *testing.T, shouldBe, target string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It is ok to inline this and the helper above directly inside the test function.

if target != shouldBe {
t.Fatalf("expected %q to equal %q", target, shouldBe)
}
}

// Ensure that basic unit starting and stopping works.
func TestStartStopUnit(t *testing.T) {
target := "start-stop.service"
Expand Down Expand Up @@ -1539,3 +1554,31 @@ func TestUnitName(t *testing.T) {
}
}
}

func TestGetUnitByPID(t *testing.T) {
target := "get-unit-pid.service"
conn := setupConn(t)
defer conn.Close()

setupUnit(target, conn, t)
linkUnit(target, conn, t)

reschan := make(chan string)
_, err := conn.StartUnit(target, "replace", reschan)
assertNoError(t, err)

job := <-reschan
if job != "done" {
t.Fatal("Job is not done:", job)
}

pidStr, err := ioutil.ReadFile("../fixtures/get-unit-pid.pid")
assertNoError(t, err)
pid, err := strconv.Atoi(string(pidStr))
assertNoError(t, err)
objectPath, err := conn.GetUnitByPID(pid)
if err != nil {
log.Fatalf("conn.getunitbypid ntp: %v", err)
}
assertEqualStr(t, "foo", string(objectPath))
}
5 changes: 5 additions & 0 deletions fixtures/get-unit-pid.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Unit]
Description=get unit pid

[Service]
ExecStart=get-unit-pid.sh
Copy link
Contributor

Choose a reason for hiding this comment

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

This should specify the full absolute path to the binary.

2 changes: 2 additions & 0 deletions fixtures/get-unit-pid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/echo ${PID} >get-unit-pid.pid
Copy link
Contributor

Choose a reason for hiding this comment

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

This should have a #!/usr/bin/env bash shebang.

/bin/sleep 400