@@ -11,11 +11,12 @@ namespace Microsoft.AspNetCore.Hosting
11
11
public static class KestrelServerOptionsSystemdExtensions
12
12
{
13
13
// SD_LISTEN_FDS_START https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html
14
- private const ulong SdListenFdsStart = 3 ;
14
+ private const int SdListenFdsStart = 3 ;
15
15
private const string ListenPidEnvVar = "LISTEN_PID" ;
16
+ private const string ListenFdsEnvVar = "LISTEN_FDS" ;
16
17
17
18
/// <summary>
18
- /// Open file descriptor ( SD_LISTEN_FDS_START) initialized by systemd socket-based activation logic if available.
19
+ /// Open file descriptors (starting from SD_LISTEN_FDS_START) initialized by systemd socket-based activation logic if available.
19
20
/// </summary>
20
21
/// <returns>
21
22
/// The <see cref="KestrelServerOptions"/>.
@@ -26,7 +27,7 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options)
26
27
}
27
28
28
29
/// <summary>
29
- /// Open file descriptor ( SD_LISTEN_FDS_START) initialized by systemd socket-based activation logic if available.
30
+ /// Open file descriptors (starting from SD_LISTEN_FDS_START) initialized by systemd socket-based activation logic if available.
30
31
/// Specify callback to configure endpoint-specific settings.
31
32
/// </summary>
32
33
/// <returns>
@@ -36,7 +37,16 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options,
36
37
{
37
38
if ( string . Equals ( Process . GetCurrentProcess ( ) . Id . ToString ( CultureInfo . InvariantCulture ) , Environment . GetEnvironmentVariable ( ListenPidEnvVar ) , StringComparison . Ordinal ) )
38
39
{
39
- options . ListenHandle ( SdListenFdsStart , configure ) ;
40
+ // This matches sd_listen_fds behavior that requires %LISTEN_FDS% to be present and in range [1;INT_MAX-SD_LISTEN_FDS_START]
41
+ if ( int . TryParse ( Environment . GetEnvironmentVariable ( ListenFdsEnvVar ) , NumberStyles . None , NumberFormatInfo . InvariantInfo , out var listenFds )
42
+ && listenFds > 0
43
+ && listenFds <= int . MaxValue - SdListenFdsStart )
44
+ {
45
+ for ( var handle = SdListenFdsStart ; handle < SdListenFdsStart + listenFds ; ++ handle )
46
+ {
47
+ options . ListenHandle ( ( ulong ) handle , configure ) ;
48
+ }
49
+ }
40
50
}
41
51
42
52
return options ;
0 commit comments