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

Etienne Doms etienne.doms at gmail.com
Tue Nov 10 19:01:37 UTC 2020


Ok it seems /org/freedesktop/systemd1/unit/self is exactly what I was
looking for.

Thank you for your fast response.

Le mar. 10 nov. 2020 à 18:23, Mantas Mikulėnas <grawity at gmail.com> a écrit :
>
> 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


More information about the systemd-devel mailing list