Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ 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";

/// <summary>
/// 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.
/// </summary>
/// <returns>
/// The <see cref="KestrelServerOptions"/>.
Expand All @@ -26,7 +27,7 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options)
}

/// <summary>
/// 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.
/// </summary>
/// <returns>
Expand All @@ -36,7 +37,16 @@ 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);
// 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)
{
for (var handle = SdListenFdsStart; handle < SdListenFdsStart + listenFds; ++handle)
{
options.ListenHandle((ulong)handle, configure);
}
}
}

return options;
Expand Down