go-systemd icon indicating copy to clipboard operation
go-systemd copied to clipboard

dbus: Add GetUnitByPID

Open APTy opened this issue 7 years ago • 6 comments

This implements the org.freedesktop.systemd1.Manager.GetUnitByPID method from https://www.freedesktop.org/wiki/Software/systemd/dbus/

The test is a very rough outline - I couldn't get it to run locally. Any tips on that?

APTy avatar Dec 13 '18 18:12 APTy

Thanks for the PR! Code looks mostly ok. I think that test is failing because you didn't specify an absolute path for the binary.

If path lookup gets too complex, you can try re-arranging the test so that it just runs a sleeping service. That way you can get the service by name, then lookup its main PID from properties, then use your method with the PID to get the path, and finally verify that it matches with the service property.

lucab avatar Dec 14 '18 08:12 lucab

That way you can get the service by name, then lookup its main PID from properties, then use your method with the PID to get the path, and finally verify that it matches with the service property.

I'm having trouble looking up the pid from properties with GetUnitProperties or GetUnitProperty. Is it "MainPID" that I should be looking for?

APTy avatar Dec 14 '18 19:12 APTy

@APTy yes, MainPID tells you the pid of the top-level process.

lucab avatar Dec 15 '18 15:12 lucab

Got it, thanks. Do you happen to know if it's only available after certain versions of systemd? I tried using GetUnitProperty to look it up (and could find other properties like Id) but couldn't find MainPID. I can follow up with some repro steps but just wanted to check first

APTy avatar Dec 15 '18 16:12 APTy

Ah, I got it - needed to use GetServiceProperty not GetUnitProperty, since MainPID is defined on the org.freedesktop.systemd1.Service interface, not org.freedesktop.systemd1.Unit.

Working through a final compatibility issue that seems to appear only on ubuntu:18.04 that results in:

    expected
        "/org/freedesktop/systemd1/unit/_2d_2eslice"
    to equal
        "/org/freedesktop/systemd1/unit/get_2dunit_2dpid_2eservice"

APTy avatar Dec 17 '18 01:12 APTy

Seems like the call to GetUnitByPID() on 18.04 is returning the object path to the root slice configuration unit -.slice (http://manpages.ubuntu.com/manpages/bionic/man5/systemd.slice.5.html), looking into this more

APTy avatar Dec 17 '18 01:12 APTy