You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UseSystemd can detect an interactive shell as running as systemd service #2525
I have my embedded platform and configure the IHostBuilder to .UseSystemd().
Using the application as systemd service always works correctly.
When I log into my system using SSH, I can run my application on the interactive prompt and it's using ConsoleLifetime and console logging format.
When I use the tty on the serial port to run the application, that's detected as systemd environment and I get the systemd log format and termination behavior (no SIGINT handler)
The reason for the latter case is that my bash instance on the serial port inherits the INVOCATION_ID environment variable through login:
Looking in to this deeper, it looks like a tricky situation. Right now we're using the INVOCATION_ID as the primary detector. If it's present, we don't look deeper, but if the shell is started from systemd, the env vars will flow from process to process.
To fix this, we could check if the process itself or any of its parents is a terminal. If that is the case, we're not a systemd service.
Something like this:
I wonder if redirected input/output should be considered as the "terminal" case.
Maybe drop the INVOCATION_ID check altogether and always go through the "slow" path with detecting the name of the parent process? Yes, I see that parent pid might actually be not named with systemd, but it looks less complex.
Also, checking the TERM envvar might be useful, if it's not empty, it usually means that there is some terminal is attached to the session.
@Tragetaschen no, since it would not handle the user units. Also, if systemd is not an init process it would be a false positive. Take a look at the current implementation, we could skip the current short-circuit with INVOCATION_ID presence check, but I'm still not sure if it is the right way (see my previous comment).
I concur. This is a one-time check at app startup. "Slow" is very relative here. Perhaps a rough measurement would be useful, but I'm not worried. The parent process check seems right to me.
I think we should also add some kind of override. For example UseSystemd(bool force). If force is true, the check is skipped and we assume the service is a Systemd service. That way we could be a little more conservative and users could always force it on in other places. Users already have a workaround for adding custom "disable" logic (add their logic and skip the call to UseSystemd if they don't believe they're in systemd). Adding that would give a way to have custom "enable" logic as well.
@tmds Thoughts on the action we take here? To me, checking the parent process name (and providing a force override) seems right. I can find someone to do the work, but I'd appreciate your thoughts as someone with a little more systemd experience 😄.
The TERM envvar is a best indicator of the terminal being present. Oh the other hand, the method checks for process being run is a systemd service.
Maybe UseSystemd should check for parent file name (ignoring INVOCATION_ID altogether), but I don't think checking for systemd presence and terminal availability should influence each other.
I don't see for example why a systemd service cannot (in theory) have a terminal attached to it. I agree that it should use different logging format, yes, but it would not stop being a systemd service.
So, maybe adjust "UseSystemd" to skip the INVOCATION_ID "shortcut" and always go the full path, along with adding some kind of a similar check SystemdHelpers.HasTerminalAttached and adding checking it it around here?
P.S. I'm not Tom, but since at least part of the code in question was written by me I feel an obligation at least to provide relevant (to the best of my knowledge) input on the topic.
I'd like to stress the fact that being a systemd service and being attached to terminal are not mutually exclusive. I think treating them as a same "flag" could be wrong for certain kinds of applications.
But yes, adding the ability to override the logic behind both of these flags would be very helpful in non-standard scenarios.
Maybe make this a try-state? force on, force off, 'default'
We already have force off and 'default' today. To force off, just check before calling UseSystemd. @Tratcher pointed out that we also have a form of force on today by just putting the content of UseSystemd in your own ConfigureServices (everything is public):
UseSystemd(SystemdMode.Off) seems weird to me, even if it's UseSystemd(ShouldEnableSystemd()). Having a parameter that means "make this method no-op" feels... off. Also, the Windows Service equivalent does not have a force option either and does a similar kind of parent process check.
Given the existing workaround and the fact that in general we're going to try and do the right thing. I could probably go without the force parameter entirely.
There's a clear workaround here though, which is to have your own conditional logic prior to calling UseSystemd. Generally that means it wouldn't meet the bar (especially given that it does have a breaking change angle).
Activity
analogrelay commentedon Nov 7, 2019
@tmds do you have thoughts here? You're our semi-resident systemd expert ;). We can take a look as well but you might have some insight here.
analogrelay commentedon Nov 20, 2019
Looking in to this deeper, it looks like a tricky situation. Right now we're using the
INVOCATION_IDas the primary detector. If it's present, we don't look deeper, but if the shell is started from systemd, the env vars will flow from process to process.We do have our fallback detection logic that looks at
/proc/[pid]/comm, but some sources show that to be risky at times.One option is, of course, to perform your own validation and use that to determine whether to call
UseSystemd.tmds commentedon Nov 20, 2019
@anurse I had missed your notification.
I will take a closer look.
Strange enough,
INVOCATION_IDis even set when I run an interactive terminal on my Fedora 31. I'm pretty sure this wasn't always like this.tmds commentedon Nov 20, 2019
To fix this, we could check if the process itself or any of its parents is a terminal. If that is the case, we're not a systemd service.
Something like this:
@anurse wdyt?
@onyxmaster do you have some thoughts on the issue, or suggested solution?
onyxmaster commentedon Nov 20, 2019
I wonder if redirected input/output should be considered as the "terminal" case.
Maybe drop the INVOCATION_ID check altogether and always go through the "slow" path with detecting the name of the parent process? Yes, I see that parent pid might actually be not named with
systemd, but it looks less complex.Also, checking the
TERMenvvar might be useful, if it's not empty, it usually means that there is some terminal is attached to the session.Tragetaschen commentedon Nov 20, 2019
Would
INVOCATION_ID != null && GetPPid() == 1work as detector for systemd?onyxmaster commentedon Nov 20, 2019
@Tragetaschen no, since it would not handle the user units. Also, if systemd is not an init process it would be a false positive. Take a look at the current implementation, we could skip the current short-circuit with
INVOCATION_IDpresence check, but I'm still not sure if it is the right way (see my previous comment).Tragetaschen commentedon Nov 20, 2019
Ah, forgot about user units
tmds commentedon Nov 20, 2019
I'm not concerned with the speed. This does require the systemd process to be the direct parent. Maybe that is ok.
tmds commentedon Nov 20, 2019
And a much simpler check than what I had in mind.
analogrelay commentedon Nov 20, 2019
I concur. This is a one-time check at app startup. "Slow" is very relative here. Perhaps a rough measurement would be useful, but I'm not worried. The parent process check seems right to me.
I think we should also add some kind of override. For example
UseSystemd(bool force). Ifforceistrue, the check is skipped and we assume the service is a Systemd service. That way we could be a little more conservative and users could always force it on in other places. Users already have a workaround for adding custom "disable" logic (add their logic and skip the call toUseSystemdif they don't believe they're in systemd). Adding that would give a way to have custom "enable" logic as well.analogrelay commentedon Nov 22, 2019
@tmds Thoughts on the action we take here? To me, checking the parent process name (and providing a
forceoverride) seems right. I can find someone to do the work, but I'd appreciate your thoughts as someone with a little more systemd experience 😄.onyxmaster commentedon Nov 22, 2019
The
TERMenvvar is a best indicator of the terminal being present. Oh the other hand, the method checks for process being run is a systemd service.Maybe UseSystemd should check for parent file name (ignoring
INVOCATION_IDaltogether), but I don't think checking for systemd presence and terminal availability should influence each other.I don't see for example why a systemd service cannot (in theory) have a terminal attached to it. I agree that it should use different logging format, yes, but it would not stop being a systemd service.
So, maybe adjust "UseSystemd" to skip the
INVOCATION_ID"shortcut" and always go the full path, along with adding some kind of a similar checkSystemdHelpers.HasTerminalAttachedand adding checking it it around here?P.S. I'm not Tom, but since at least part of the code in question was written by me I feel an obligation at least to provide relevant (to the best of my knowledge) input on the topic.
tmds commentedon Nov 22, 2019
Yes, that is what we'll do.
The more liberal check would be:
INVOCATION_IDset,TERMnot set.Maybe make this a try-state? force on, force off, 'default'
I can look into it next week also.
onyxmaster commentedon Nov 22, 2019
I'd like to stress the fact that being a systemd service and being attached to terminal are not mutually exclusive. I think treating them as a same "flag" could be wrong for certain kinds of applications.
But yes, adding the ability to override the logic behind both of these flags would be very helpful in non-standard scenarios.
analogrelay commentedon Nov 23, 2019
We already have force off and 'default' today. To force off, just check before calling
UseSystemd. @Tratcher pointed out that we also have a form of force on today by just putting the content ofUseSystemdin your ownConfigureServices(everything is public):UseSystemd(SystemdMode.Off)seems weird to me, even if it'sUseSystemd(ShouldEnableSystemd()). Having a parameter that means "make this method no-op" feels... off. Also, the Windows Service equivalent does not have aforceoption either and does a similar kind of parent process check.Given the existing workaround and the fact that in general we're going to try and do the right thing. I could probably go without the
forceparameter entirely.Cool, just let me know if you need some help!
davidfowl commentedon Dec 3, 2019
This feels patch worthy...
analogrelay commentedon Dec 5, 2019
There's a clear workaround here though, which is to have your own conditional logic prior to calling
UseSystemd. Generally that means it wouldn't meet the bar (especially given that it does have a breaking change angle).davidfowl commentedon Dec 12, 2019
@anurse should we doc this then?
analogrelay commentedon Dec 12, 2019
We don't have a Systemd doc that I can find.