[systemd-devel] How to dynamically retrieve my service name?

Mantas Mikulėnas grawity at gmail.com
Tue Nov 10 17:23:21 UTC 2020


You can call org.freedesktop.systemd1.Manager.GetUnitByPID() to directly
get the D-Bus object path based on your PID.

There is also the magic path "/org/freedesktop/systemd1/unit/self" which
always gives properties of the same service (or scope) that you're in.

Finally, it is possible to call sd_pid_get_unit() from sd-login.h to get
your unit name (straight from /proc/<pid>/cgroup, with no D-Bus yet), then
call .Manager.GetUnit() to translate the name into an object path.



On Tue, Nov 10, 2020 at 6:28 PM Etienne Doms <etienne.doms at gmail.com> wrote:

> Hello,
>
> My service needs to behave a bit differently when it has been
> automatically because of a software fault. I use the
> "Restart=on-failure", and I understand that I can read the "NRestarts"
> property which is incremented whenever the service is restarted.
>
> The thing is, inside my service, I have no idea if I'm foobar.service,
> barfoo.service, etc. and I believe I should be agnostic of that.
>
> Is there a way to dynamically retrieve
> /org/freedesktop/systemd1/unit/foobar_2eservice, so that I can ask
> org.freedesktop.systemd1 the NRestarts property value of the
> org.freedesktop.systemd1.Service interface?
>
> Maybe I'm just over-engineering and should just hardcode
> "foobar.service" inside my service, but it feels a bit odd to me...
> Maybe also I understand nothing about D-Bus, sorry about that.
>
> Thank you for your support.
>
> Best regards,
> Etienne Doms
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel
>


-- 
Mantas Mikulėnas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20201110/866f1282/attachment.htm>


More information about the systemd-devel mailing list