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