1- // Copyright (c) .NET Foundation. All rights reserved.
1+ // Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
@@ -13,9 +13,10 @@ public static class KestrelServerOptionsSystemdExtensions
1313 // SD_LISTEN_FDS_START https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html
1414 private const ulong SdListenFdsStart = 3 ;
1515 private const string ListenPidEnvVar = "LISTEN_PID" ;
16+ private const string ListenFdsEnvVar = "LISTEN_FDS" ;
1617
1718 /// <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.
1920 /// </summary>
2021 /// <returns>
2122 /// The <see cref="KestrelServerOptions"/>.
@@ -26,7 +27,7 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options)
2627 }
2728
2829 /// <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.
3031 /// Specify callback to configure endpoint-specific settings.
3132 /// </summary>
3233 /// <returns>
@@ -36,7 +37,17 @@ public static KestrelServerOptions UseSystemd(this KestrelServerOptions options,
3637 {
3738 if ( string . Equals ( Process . GetCurrentProcess ( ) . Id . ToString ( CultureInfo . InvariantCulture ) , Environment . GetEnvironmentVariable ( ListenPidEnvVar ) , StringComparison . Ordinal ) )
3839 {
39- options . ListenHandle ( SdListenFdsStart , configure ) ;
40+ if ( ! byte . TryParse ( Environment . GetEnvironmentVariable ( ListenFdsEnvVar ) , NumberStyles . None , NumberFormatInfo . InvariantInfo , out var listenFds ) )
41+ {
42+ listenFds = 1 ;
43+ }
44+
45+ for ( ulong handle = SdListenFdsStart , lastHandle = SdListenFdsStart + listenFds ;
46+ handle <= lastHandle ;
47+ ++ handle )
48+ {
49+ options . ListenHandle ( handle , configure ) ;
50+ }
4051 }
4152
4253 return options ;
0 commit comments