From 15893a2cef3235bd87a5c3352b1896f931159aa9 Mon Sep 17 00:00:00 2001 From: Aristarkh Zagorodnikov Date: Mon, 20 Jul 2020 20:03:14 +0300 Subject: [PATCH 1/3] Implement support for multiple LISTEN_FDS for systemd activation --- .../KestrelServerOptionsSystemdExtensions.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs b/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs index 6def39159de2..03205382dc15 100644 --- a/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs +++ b/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs @@ -13,9 +13,10 @@ public static class KestrelServerOptionsSystemdExtensions // SD_LISTEN_FDS_START https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html private const ulong SdListenFdsStart = 3; private const string ListenPidEnvVar = "LISTEN_PID"; + private const string ListenFdsEnvVar = "LISTEN_FDS"; /// - /// Open file descriptor (SD_LISTEN_FDS_START) initialized by systemd socket-based activation logic if available. + /// Open file descriptors (starting from SD_LISTEN_FDS_START) initialized by systemd socket-based activation logic if available. /// /// /// The . @@ -26,7 +27,7 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options) } /// - /// Open file descriptor (SD_LISTEN_FDS_START) initialized by systemd socket-based activation logic if available. + /// Open file descriptors (starting from SD_LISTEN_FDS_START) initialized by systemd socket-based activation logic if available. /// Specify callback to configure endpoint-specific settings. /// /// @@ -36,7 +37,17 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options, { if (string.Equals(Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture), Environment.GetEnvironmentVariable(ListenPidEnvVar), StringComparison.Ordinal)) { - options.ListenHandle(SdListenFdsStart, configure); + if (!byte.TryParse(Environment.GetEnvironmentVariable(ListenFdsEnvVar), NumberStyles.None, NumberFormatInfo.InvariantInfo, out var listenFds)) + { + listenFds = 1; + } + + for (ulong handle = SdListenFdsStart, lastHandle = SdListenFdsStart + listenFds; + handle <= lastHandle; + ++handle) + { + options.ListenHandle(handle, configure); + } } return options; From 10f3b7d9d5b475a5b5d7f5b7baa5481161702998 Mon Sep 17 00:00:00 2001 From: Aristarkh Zagorodnikov Date: Mon, 20 Jul 2020 21:03:32 +0300 Subject: [PATCH 2/3] Fix handle iteration --- .../Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs b/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs index 03205382dc15..075f3c668353 100644 --- a/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs +++ b/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs @@ -42,9 +42,7 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options, listenFds = 1; } - for (ulong handle = SdListenFdsStart, lastHandle = SdListenFdsStart + listenFds; - handle <= lastHandle; - ++handle) + for (ulong handle = SdListenFdsStart; handle < SdListenFdsStart + listenFds; ++handle) { options.ListenHandle(handle, configure); } From 457118ed6b1e721368c7ea9076f78de5f10af0b2 Mon Sep 17 00:00:00 2001 From: Aristarkh Zagorodnikov Date: Fri, 31 Jul 2020 08:56:31 +0300 Subject: [PATCH 3/3] Match %LISTEN_FDS% handling behavior to sd_listen_fds --- .../KestrelServerOptionsSystemdExtensions.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs b/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs index 075f3c668353..bf8f0fe69437 100644 --- a/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs +++ b/src/Servers/Kestrel/Core/src/Systemd/KestrelServerOptionsSystemdExtensions.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Hosting public static class KestrelServerOptionsSystemdExtensions { // SD_LISTEN_FDS_START https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html - private const ulong SdListenFdsStart = 3; + private const int SdListenFdsStart = 3; private const string ListenPidEnvVar = "LISTEN_PID"; private const string ListenFdsEnvVar = "LISTEN_FDS"; @@ -37,14 +37,15 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options, { if (string.Equals(Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture), Environment.GetEnvironmentVariable(ListenPidEnvVar), StringComparison.Ordinal)) { - if (!byte.TryParse(Environment.GetEnvironmentVariable(ListenFdsEnvVar), NumberStyles.None, NumberFormatInfo.InvariantInfo, out var listenFds)) + // This matches sd_listen_fds behavior that requires %LISTEN_FDS% to be present and in range [1;INT_MAX-SD_LISTEN_FDS_START] + if (int.TryParse(Environment.GetEnvironmentVariable(ListenFdsEnvVar), NumberStyles.None, NumberFormatInfo.InvariantInfo, out var listenFds) + && listenFds > 0 + && listenFds <= int.MaxValue - SdListenFdsStart) { - listenFds = 1; - } - - for (ulong handle = SdListenFdsStart; handle < SdListenFdsStart + listenFds; ++handle) - { - options.ListenHandle(handle, configure); + for (var handle = SdListenFdsStart; handle < SdListenFdsStart + listenFds; ++handle) + { + options.ListenHandle((ulong)handle, configure); + } } }