diff --git a/src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs b/src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs index 8d58604ccd..31861d023a 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs @@ -44,19 +44,19 @@ namespace Microsoft.AspNet.Server.Kestrel architecture, "libuv.dll"); } - else if ((int)Environment.OSVersion.Platform == 4) - { - libraryPath = "libuv.so.1"; - } - else + else if (Libuv.IsDarwin) { libraryPath = Path.Combine( libraryPath, - "native", + "native", "darwin", - "universal", + "universal", "libuv.dylib"); } + else + { + libraryPath = "libuv.so.1"; + } } Libuv.Load(libraryPath); } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Networking/Libuv.cs b/src/Microsoft.AspNet.Server.Kestrel/Networking/Libuv.cs index c777af7998..741c3b48f8 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Networking/Libuv.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Networking/Libuv.cs @@ -13,9 +13,15 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking public Libuv() { IsWindows = PlatformApis.IsWindows(); + if (!IsWindows) + { + IsDarwin = PlatformApis.IsDarwin(); + } } public bool IsWindows; + public bool IsDarwin; + public Func LoadLibrary; public Func FreeLibrary; public Func GetProcAddress; diff --git a/src/Microsoft.AspNet.Server.Kestrel/Networking/PlatformApis.cs b/src/Microsoft.AspNet.Server.Kestrel/Networking/PlatformApis.cs index d7907e4d88..ff7c4fa12c 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Networking/PlatformApis.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Networking/PlatformApis.cs @@ -10,7 +10,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking { public static class PlatformApis { - public static bool IsWindows() + public static bool IsWindows() { #if ASPNETCORE50 return true; @@ -20,6 +20,34 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking #endif } + [DllImport("libc")] + static extern int uname(IntPtr buf); + + static unsafe string GetUname() + { + var buffer = new byte[8192]; + try + { + fixed (byte* buf = buffer) + { + if (uname((IntPtr)buf) == 0) + { + return Marshal.PtrToStringAnsi((IntPtr)buf); + } + } + return string.Empty; + } + catch + { + return string.Empty; + } + } + + public static bool IsDarwin() + { + return string.Equals(GetUname(), "Darwin", StringComparison.Ordinal); + } + public static void Apply(Libuv libuv) { if (libuv.IsWindows) @@ -55,7 +83,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking { [DllImport("libdl")] public static extern IntPtr dlopen(String fileName, int flags); - + [DllImport("libdl")] public static extern IntPtr dlsym(IntPtr handle, String symbol); @@ -65,17 +93,17 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking [DllImport("libdl")] public static extern IntPtr dlerror(); - public static IntPtr LoadLibrary(string dllToLoad) + public static IntPtr LoadLibrary(string dllToLoad) { return dlopen(dllToLoad, 2); } - public static bool FreeLibrary(IntPtr hModule) + public static bool FreeLibrary(IntPtr hModule) { return dlclose(hModule) == 0; } - public static IntPtr GetProcAddress(IntPtr hModule, string procedureName) + public static IntPtr GetProcAddress(IntPtr hModule, string procedureName) { dlerror(); var res = dlsym(hModule, procedureName);