-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
PlatformApis.cs
251 lines (228 loc) · 9.07 KB
/
PlatformApis.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Microsoft.DotNet.PlatformAbstractions.Native
{
internal static class PlatformApis
{
private class DistroInfo
{
public string Id;
public string VersionId;
}
private static readonly Lazy<Platform> _platform = new Lazy<Platform>(DetermineOSPlatform);
private static readonly Lazy<DistroInfo> _distroInfo = new Lazy<DistroInfo>(LoadDistroInfo);
public static string GetOSName()
{
switch (GetOSPlatform())
{
case Platform.Windows:
return "Windows";
case Platform.Linux:
return GetDistroId() ?? "Linux";
case Platform.Darwin:
return "Mac OS X";
case Platform.FreeBSD:
return "FreeBSD";
default:
return "Unknown";
}
}
public static string GetOSVersion()
{
switch (GetOSPlatform())
{
case Platform.Windows:
return NativeMethods.Windows.RtlGetVersion() ?? string.Empty;
case Platform.Linux:
return GetDistroVersionId() ?? string.Empty;
case Platform.Darwin:
return GetDarwinVersion() ?? string.Empty;
case Platform.FreeBSD:
return GetFreeBSDVersion() ?? string.Empty;
default:
return string.Empty;
}
}
private static string GetDarwinVersion()
{
Version version;
var kernelRelease = NativeMethods.Darwin.GetKernelRelease();
if (!Version.TryParse(kernelRelease, out version) || version.Major < 5)
{
// 10.0 covers all versions prior to Darwin 5
// Similarly, if the version is not a valid version number, but we have still detected that it is Darwin, we just assume
// it is OS X 10.0
return "10.0";
}
else
{
// Mac OS X 10.1 mapped to Darwin 5.x, and the mapping continues that way
// So just subtract 4 from the Darwin version.
// https://en.wikipedia.org/wiki/Darwin_%28operating_system%29
return $"10.{version.Major - 4}";
}
}
private static string GetFreeBSDVersion()
{
// This is same as sysctl kern.version
// FreeBSD 11.0-RELEASE-p1 FreeBSD 11.0-RELEASE-p1 #0 r306420: Thu Sep 29 01:43:23 UTC 2016 root@releng2.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC
// What we want is major release as minor releases should be compatible.
String version = RuntimeInformation.OSDescription;
try
{
// second token up to first dot
return RuntimeInformation.OSDescription.Split()[1].Split('.')[0];
}
catch
{
}
return string.Empty;
}
public static Platform GetOSPlatform()
{
return _platform.Value;
}
private static string GetDistroId()
{
return _distroInfo.Value?.Id;
}
private static string GetDistroVersionId()
{
return _distroInfo.Value?.VersionId;
}
private static DistroInfo LoadDistroInfo()
{
DistroInfo result = null;
// Sample os-release file:
// NAME="Ubuntu"
// VERSION = "14.04.3 LTS, Trusty Tahr"
// ID = ubuntu
// ID_LIKE = debian
// PRETTY_NAME = "Ubuntu 14.04.3 LTS"
// VERSION_ID = "14.04"
// HOME_URL = "http://www.ubuntu.com/"
// SUPPORT_URL = "http://help.ubuntu.com/"
// BUG_REPORT_URL = "http://bugs.launchpad.net/ubuntu/"
// We use ID and VERSION_ID
if (File.Exists("/etc/os-release"))
{
var lines = File.ReadAllLines("/etc/os-release");
result = new DistroInfo();
foreach (var line in lines)
{
if (line.StartsWith("ID=", StringComparison.Ordinal))
{
result.Id = line.Substring(3).Trim('"', '\'');
}
else if (line.StartsWith("VERSION_ID=", StringComparison.Ordinal))
{
result.VersionId = line.Substring(11).Trim('"', '\'');
}
}
}
else if (File.Exists("/etc/redhat-release"))
{
var lines = File.ReadAllLines("/etc/redhat-release");
if (lines.Length >= 1)
{
string line = lines[0];
if (line.StartsWith("Red Hat Enterprise Linux Server release 6.") ||
line.StartsWith("CentOS release 6."))
{
result = new DistroInfo();
result.Id = "rhel";
result.VersionId = "6";
}
}
}
if (result != null)
{
result = NormalizeDistroInfo(result);
}
return result;
}
// For some distros, we don't want to use the full version from VERSION_ID. One example is
// Red Hat Enterprise Linux, which includes a minor version in their VERSION_ID but minor
// versions are backwards compatable.
//
// In this case, we'll normalized RIDs like 'rhel.7.2' and 'rhel.7.3' to a generic
// 'rhel.7'. This brings RHEL in line with other distros like CentOS or Debian which
// don't put minor version numbers in their VERSION_ID fields because all minor versions
// are backwards compatible.
private static DistroInfo NormalizeDistroInfo(DistroInfo distroInfo)
{
// Handle if VersionId is null by just setting the index to -1.
int lastVersionNumberSeparatorIndex = distroInfo.VersionId?.IndexOf('.') ?? -1;
if (lastVersionNumberSeparatorIndex != -1 && distroInfo.Id == "alpine")
{
// For Alpine, the version reported has three components, so we need to find the second version separator
lastVersionNumberSeparatorIndex = distroInfo.VersionId.IndexOf('.', lastVersionNumberSeparatorIndex + 1);
}
if (lastVersionNumberSeparatorIndex != -1 && (distroInfo.Id == "rhel" || distroInfo.Id == "alpine"))
{
distroInfo.VersionId = distroInfo.VersionId.Substring(0, lastVersionNumberSeparatorIndex);
}
return distroInfo;
}
// I could probably have just done one method signature and put the #if inside the body but the implementations
// are just completely different so I wanted to make that clear by putting the whole thing inside the #if.
#if NET45
private static Platform DetermineOSPlatform()
{
var platform = (int)Environment.OSVersion.Platform;
var isWindows = (platform != 4) && (platform != 6) && (platform != 128);
if (isWindows)
{
return Platform.Windows;
}
else
{
try
{
var uname = NativeMethods.Unix.GetUname();
if (string.Equals(uname, "Darwin", StringComparison.OrdinalIgnoreCase))
{
return Platform.Darwin;
}
if (string.Equals(uname, "Linux", StringComparison.OrdinalIgnoreCase))
{
return Platform.Linux;
}
if (string.Equals(uname, "FreeBSD", StringComparison.OrdinalIgnoreCase))
{
return Platform.FreeBSD;
}
}
catch
{
}
return Platform.Unknown;
}
}
#else
private static Platform DetermineOSPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Platform.Windows;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return Platform.Linux;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return Platform.Darwin;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")))
{
return Platform.FreeBSD;
}
return Platform.Unknown;
}
#endif
}
}