-
Notifications
You must be signed in to change notification settings - Fork 306
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
2fa0854
802a0d9
698ff26
20f22b5
c95fcd8
0f72dcf
a00023f
921543a
568f78e
302951d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
func (c *Conn) GetUnitByPID(pid int) (dbus.ObjectPath, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Input parameter should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I was thinking of using |
||
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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,14 @@ package dbus | |
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"reflect" | ||
"strconv" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
@@ -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)) | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should specify the full absolute path to the binary. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/bin/echo ${PID} >get-unit-pid.pid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have a |
||
/bin/sleep 400 |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack