Updating Darwin OS detection

This commit is contained in:
Louis DeJardin 2014-10-20 16:11:48 -07:00
parent dcf55abc9f
commit e550d1f1ec
3 changed files with 46 additions and 12 deletions

View File

@ -44,19 +44,19 @@ namespace Microsoft.AspNet.Server.Kestrel
architecture, architecture,
"libuv.dll"); "libuv.dll");
} }
else if ((int)Environment.OSVersion.Platform == 4) else if (Libuv.IsDarwin)
{
libraryPath = "libuv.so.1";
}
else
{ {
libraryPath = Path.Combine( libraryPath = Path.Combine(
libraryPath, libraryPath,
"native", "native",
"darwin", "darwin",
"universal", "universal",
"libuv.dylib"); "libuv.dylib");
} }
else
{
libraryPath = "libuv.so.1";
}
} }
Libuv.Load(libraryPath); Libuv.Load(libraryPath);
} }

View File

@ -13,9 +13,15 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
public Libuv() public Libuv()
{ {
IsWindows = PlatformApis.IsWindows(); IsWindows = PlatformApis.IsWindows();
if (!IsWindows)
{
IsDarwin = PlatformApis.IsDarwin();
}
} }
public bool IsWindows; public bool IsWindows;
public bool IsDarwin;
public Func<string, IntPtr> LoadLibrary; public Func<string, IntPtr> LoadLibrary;
public Func<IntPtr, bool> FreeLibrary; public Func<IntPtr, bool> FreeLibrary;
public Func<IntPtr, string, IntPtr> GetProcAddress; public Func<IntPtr, string, IntPtr> GetProcAddress;

View File

@ -10,7 +10,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
public static class PlatformApis public static class PlatformApis
{ {
public static bool IsWindows() public static bool IsWindows()
{ {
#if ASPNETCORE50 #if ASPNETCORE50
return true; return true;
@ -20,6 +20,34 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
#endif #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) public static void Apply(Libuv libuv)
{ {
if (libuv.IsWindows) if (libuv.IsWindows)
@ -55,7 +83,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
[DllImport("libdl")] [DllImport("libdl")]
public static extern IntPtr dlopen(String fileName, int flags); public static extern IntPtr dlopen(String fileName, int flags);
[DllImport("libdl")] [DllImport("libdl")]
public static extern IntPtr dlsym(IntPtr handle, String symbol); public static extern IntPtr dlsym(IntPtr handle, String symbol);
@ -65,17 +93,17 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[DllImport("libdl")] [DllImport("libdl")]
public static extern IntPtr dlerror(); public static extern IntPtr dlerror();
public static IntPtr LoadLibrary(string dllToLoad) public static IntPtr LoadLibrary(string dllToLoad)
{ {
return dlopen(dllToLoad, 2); return dlopen(dllToLoad, 2);
} }
public static bool FreeLibrary(IntPtr hModule) public static bool FreeLibrary(IntPtr hModule)
{ {
return dlclose(hModule) == 0; return dlclose(hModule) == 0;
} }
public static IntPtr GetProcAddress(IntPtr hModule, string procedureName) public static IntPtr GetProcAddress(IntPtr hModule, string procedureName)
{ {
dlerror(); dlerror();
var res = dlsym(hModule, procedureName); var res = dlsym(hModule, procedureName);