tmds / Tmds.Systemd

.NET Core library for interacting with systemd
Other
128 stars 17 forks source link

The ServiceManager.IsRunningAsService result can be misleading (or wrong) when socket activation is used #31

Closed onyxmaster closed 5 years ago

onyxmaster commented 5 years ago

Currently, the ServiceManager.IsRunningAsService checks INVOCATION_ID environment variable. Unfortunately, for socket-activated services this variable is not always set. Maybe the ServiceManager.IsRunningAsService check should also verify the presence of LISTEN_... environment variables?

tmds commented 5 years ago

I wonder if this is by design or if it is a systemd bug? Any idea? If not, we can ask at https://github.com/systemd/systemd. I'm fine with checking additional envvars. Can you have a look at the complete list for a socket activated service and see which seems most appropriate? It could be the LISTEN_ ones. We need to take into account that those get cleared: https://github.com/tmds/Tmds.Systemd/blob/604064bb1ae0a632061fcb938b4bad47f3e8bfe9/src/Tmds.Systemd/ServiceManager.Socket.cs#L82-L83

onyxmaster commented 5 years ago

I'm not sure whether this is by design.

These are the variables that are present on our socket-activated service:

PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG en_US.UTF-8
USER <user>
LISTEN_FDS 1
HOME <user-home>
LOGNAME <user>
LANGUAGE en_US:en
LISTEN_FDNAMES <service-name>.socket
SHELL /usr/sbin/nologin
LISTEN_PID <pid>
onyxmaster commented 5 years ago

Actually, I did the following:

~# grep INVOCATION_ID /proc/*/environ | wc -l
0

I think this means that systemd in my system does not use INVOCATION_ID at all for any installed services.

~# uname -a
Linux antares 4.15.0-38-generic #41~16.04.1-Ubuntu SMP Wed Oct 10 20:16:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
~# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.5 LTS"
~# systemd --version
systemd 229
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN
tmds commented 5 years ago

I think this means that systemd in my system does not use INVOCATION_ID at all for any installed services.

Yes, for me these commands return (on Fedora 28):

$ sudo grep INVOCATION_ID /proc/*/environ | wc -l
89
$ systemctl --version
systemd 238
$ uname -a
Linux rex 4.16.16-300.fc28.x86_64 #1 SMP Sun Jun 17 03:02:42 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Maybe it will work with a more recent version of systemd.

tmds commented 5 years ago

INVOCATION_ID was introduced in 232 (https://github.com/systemd/systemd/blob/master/NEWS), which was released 2016-11-03.

Based on that, I prefer to not extend the implementation.