1 //===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Driver/Distro.h"
10 #include "clang/Basic/LLVM.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Support/ErrorOr.h"
16 #include "llvm/Support/Host.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/Threading.h"
19 
20 using namespace clang::driver;
21 using namespace clang;
22 
23 static Distro::DistroType DetectOsRelease(llvm::vfs::FileSystem &VFS) {
24   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
25       VFS.getBufferForFile("/etc/os-release");
26   if (!File)
27     File = VFS.getBufferForFile("/usr/lib/os-release");
28   if (!File)
29     return Distro::UnknownDistro;
30 
31   SmallVector<StringRef, 16> Lines;
32   File.get()->getBuffer().split(Lines, "\n");
33   Distro::DistroType Version = Distro::UnknownDistro;
34 
35   // Obviously this can be improved a lot.
36   for (StringRef Line : Lines)
37     if (Version == Distro::UnknownDistro && Line.startswith("ID="))
38       Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(3))
39                     .Case("fedora", Distro::Fedora)
40                     .Case("gentoo", Distro::Gentoo)
41                     .Case("arch", Distro::ArchLinux)
42                     // On SLES, /etc/os-release was introduced in SLES 11.
43                     .Case("sles", Distro::OpenSUSE)
44                     .Case("opensuse", Distro::OpenSUSE)
45                     .Case("exherbo", Distro::Exherbo)
46                     .Default(Distro::UnknownDistro);
47   return Version;
48 }
49 
50 static Distro::DistroType DetectLsbRelease(llvm::vfs::FileSystem &VFS) {
51   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
52       VFS.getBufferForFile("/etc/lsb-release");
53   if (!File)
54     return Distro::UnknownDistro;
55 
56   SmallVector<StringRef, 16> Lines;
57   File.get()->getBuffer().split(Lines, "\n");
58   Distro::DistroType Version = Distro::UnknownDistro;
59 
60   for (StringRef Line : Lines)
61     if (Version == Distro::UnknownDistro &&
62         Line.startswith("DISTRIB_CODENAME="))
63       Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
64                     .Case("hardy", Distro::UbuntuHardy)
65                     .Case("intrepid", Distro::UbuntuIntrepid)
66                     .Case("jaunty", Distro::UbuntuJaunty)
67                     .Case("karmic", Distro::UbuntuKarmic)
68                     .Case("lucid", Distro::UbuntuLucid)
69                     .Case("maverick", Distro::UbuntuMaverick)
70                     .Case("natty", Distro::UbuntuNatty)
71                     .Case("oneiric", Distro::UbuntuOneiric)
72                     .Case("precise", Distro::UbuntuPrecise)
73                     .Case("quantal", Distro::UbuntuQuantal)
74                     .Case("raring", Distro::UbuntuRaring)
75                     .Case("saucy", Distro::UbuntuSaucy)
76                     .Case("trusty", Distro::UbuntuTrusty)
77                     .Case("utopic", Distro::UbuntuUtopic)
78                     .Case("vivid", Distro::UbuntuVivid)
79                     .Case("wily", Distro::UbuntuWily)
80                     .Case("xenial", Distro::UbuntuXenial)
81                     .Case("yakkety", Distro::UbuntuYakkety)
82                     .Case("zesty", Distro::UbuntuZesty)
83                     .Case("artful", Distro::UbuntuArtful)
84                     .Case("bionic", Distro::UbuntuBionic)
85                     .Case("cosmic", Distro::UbuntuCosmic)
86                     .Case("disco", Distro::UbuntuDisco)
87                     .Case("eoan", Distro::UbuntuEoan)
88                     .Case("focal", Distro::UbuntuFocal)
89                     .Case("groovy", Distro::UbuntuGroovy)
90                     .Default(Distro::UnknownDistro);
91   return Version;
92 }
93 
94 static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
95   Distro::DistroType Version = Distro::UnknownDistro;
96 
97   // Newer freedesktop.org's compilant systemd-based systems
98   // should provide /etc/os-release or /usr/lib/os-release.
99   Version = DetectOsRelease(VFS);
100   if (Version != Distro::UnknownDistro)
101     return Version;
102 
103   // Older systems might provide /etc/lsb-release.
104   Version = DetectLsbRelease(VFS);
105   if (Version != Distro::UnknownDistro)
106     return Version;
107 
108   // Otherwise try some distro-specific quirks for RedHat...
109   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
110       VFS.getBufferForFile("/etc/redhat-release");
111 
112   if (File) {
113     StringRef Data = File.get()->getBuffer();
114     if (Data.startswith("Fedora release"))
115       return Distro::Fedora;
116     if (Data.startswith("Red Hat Enterprise Linux") ||
117         Data.startswith("CentOS") || Data.startswith("Scientific Linux")) {
118       if (Data.find("release 7") != StringRef::npos)
119         return Distro::RHEL7;
120       else if (Data.find("release 6") != StringRef::npos)
121         return Distro::RHEL6;
122       else if (Data.find("release 5") != StringRef::npos)
123         return Distro::RHEL5;
124     }
125     return Distro::UnknownDistro;
126   }
127 
128   // ...for Debian
129   File = VFS.getBufferForFile("/etc/debian_version");
130   if (File) {
131     StringRef Data = File.get()->getBuffer();
132     // Contents: < major.minor > or < codename/sid >
133     int MajorVersion;
134     if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
135       switch (MajorVersion) {
136       case 5:
137         return Distro::DebianLenny;
138       case 6:
139         return Distro::DebianSqueeze;
140       case 7:
141         return Distro::DebianWheezy;
142       case 8:
143         return Distro::DebianJessie;
144       case 9:
145         return Distro::DebianStretch;
146       case 10:
147         return Distro::DebianBuster;
148       case 11:
149         return Distro::DebianBullseye;
150       default:
151         return Distro::UnknownDistro;
152       }
153     }
154     return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
155         .Case("squeeze/sid", Distro::DebianSqueeze)
156         .Case("wheezy/sid", Distro::DebianWheezy)
157         .Case("jessie/sid", Distro::DebianJessie)
158         .Case("stretch/sid", Distro::DebianStretch)
159         .Case("buster/sid", Distro::DebianBuster)
160         .Case("bullseye/sid", Distro::DebianBullseye)
161         .Default(Distro::UnknownDistro);
162   }
163 
164   // ...for SUSE
165   File = VFS.getBufferForFile("/etc/SuSE-release");
166   if (File) {
167     StringRef Data = File.get()->getBuffer();
168     SmallVector<StringRef, 8> Lines;
169     Data.split(Lines, "\n");
170     for (const StringRef &Line : Lines) {
171       if (!Line.trim().startswith("VERSION"))
172         continue;
173       std::pair<StringRef, StringRef> SplitLine = Line.split('=');
174       // Old versions have split VERSION and PATCHLEVEL
175       // Newer versions use VERSION = x.y
176       std::pair<StringRef, StringRef> SplitVer =
177           SplitLine.second.trim().split('.');
178       int Version;
179 
180       // OpenSUSE/SLES 10 and older are not supported and not compatible
181       // with our rules, so just treat them as Distro::UnknownDistro.
182       if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
183         return Distro::OpenSUSE;
184       return Distro::UnknownDistro;
185     }
186     return Distro::UnknownDistro;
187   }
188 
189   // ...and others.
190   if (VFS.exists("/etc/exherbo-release"))
191     return Distro::Exherbo;
192 
193   if (VFS.exists("/etc/alpine-release"))
194     return Distro::AlpineLinux;
195 
196   if (VFS.exists("/etc/arch-release"))
197     return Distro::ArchLinux;
198 
199   if (VFS.exists("/etc/gentoo-release"))
200     return Distro::Gentoo;
201 
202   return Distro::UnknownDistro;
203 }
204 
205 static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS,
206                                     const llvm::Triple &TargetOrHost) {
207   // If we don't target Linux, no need to check the distro. This saves a few
208   // OS calls.
209   if (!TargetOrHost.isOSLinux())
210     return Distro::UnknownDistro;
211 
212   // True if we're backed by a real file system.
213   const bool onRealFS = (llvm::vfs::getRealFileSystem() == &VFS);
214 
215   // If the host is not running Linux, and we're backed by a real file
216   // system, no need to check the distro. This is the case where someone
217   // is cross-compiling from BSD or Windows to Linux, and it would be
218   // meaningless to try to figure out the "distro" of the non-Linux host.
219   llvm::Triple HostTriple(llvm::sys::getProcessTriple());
220   if (!HostTriple.isOSLinux() && onRealFS)
221     return Distro::UnknownDistro;
222 
223   if (onRealFS) {
224     // If we're backed by a real file system, perform
225     // the detection only once and save the result.
226     static Distro::DistroType LinuxDistro = DetectDistro(VFS);
227     return LinuxDistro;
228   }
229   // This is mostly for passing tests which uses llvm::vfs::InMemoryFileSystem,
230   // which is not "real".
231   return DetectDistro(VFS);
232 }
233 
234 Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
235     : DistroVal(GetDistro(VFS, TargetOrHost)) {}
236