-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
prereqs.rs
373 lines (321 loc) · 9.92 KB
/
prereqs.rs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
use std::cmp::Ordering;
use super::command::capture_command;
use crate::constants::QUALITYLESS_SERVER_NAME;
use crate::update_service::Platform;
use lazy_static::lazy_static;
use regex::bytes::Regex as BinRegex;
use regex::Regex;
use tokio::fs;
use super::errors::CodeError;
lazy_static! {
static ref LDCONFIG_STDC_RE: Regex = Regex::new(r"libstdc\+\+.* => (.+)").unwrap();
static ref LDD_VERSION_RE: BinRegex = BinRegex::new(r"^ldd.*(.+)\.(.+)\s").unwrap();
static ref GENERIC_VERSION_RE: Regex = Regex::new(r"^([0-9]+)\.([0-9]+)$").unwrap();
static ref LIBSTD_CXX_VERSION_RE: BinRegex =
BinRegex::new(r"GLIBCXX_([0-9]+)\.([0-9]+)(?:\.([0-9]+))?").unwrap();
static ref MIN_CXX_VERSION: SimpleSemver = SimpleSemver::new(3, 4, 19);
static ref MIN_LDD_VERSION: SimpleSemver = SimpleSemver::new(2, 17, 0);
}
const NIXOS_TEST_PATH: &str = "/etc/NIXOS";
pub struct PreReqChecker {}
impl Default for PreReqChecker {
fn default() -> Self {
Self::new()
}
}
impl PreReqChecker {
pub fn new() -> PreReqChecker {
PreReqChecker {}
}
#[cfg(not(target_os = "linux"))]
pub async fn verify(&self) -> Result<Platform, CodeError> {
Platform::env_default().ok_or_else(|| {
CodeError::UnsupportedPlatform(format!(
"{} {}",
std::env::consts::OS,
std::env::consts::ARCH
))
})
}
#[cfg(target_os = "linux")]
pub async fn verify(&self) -> Result<Platform, CodeError> {
let (is_nixos, skip_glibc_checks, or_musl) = tokio::join!(
check_is_nixos(),
skip_requirements_check(),
check_musl_interpreter()
);
let (gnu_a, gnu_b) = if !skip_glibc_checks {
tokio::join!(check_glibc_version(), check_glibcxx_version())
} else {
println!("!!! WARNING: Skipping server pre-requisite check !!!");
println!("!!! Server stability is not guaranteed. Proceed at your own risk. !!!");
(Ok(()), Ok(()))
};
if (gnu_a.is_ok() && gnu_b.is_ok()) || is_nixos {
return Ok(if cfg!(target_arch = "x86_64") {
Platform::LinuxX64
} else if cfg!(target_arch = "arm") {
Platform::LinuxARM32
} else {
Platform::LinuxARM64
});
}
if or_musl.is_ok() {
return Ok(if cfg!(target_arch = "x86_64") {
Platform::LinuxAlpineX64
} else {
Platform::LinuxAlpineARM64
});
}
let mut errors: Vec<String> = vec![];
if let Err(e) = gnu_a {
errors.push(e);
} else if let Err(e) = gnu_b {
errors.push(e);
}
if let Err(e) = or_musl {
errors.push(e);
}
let bullets = errors
.iter()
.map(|e| format!(" - {}", e))
.collect::<Vec<String>>()
.join("\n");
Err(CodeError::PrerequisitesFailed {
bullets,
name: QUALITYLESS_SERVER_NAME,
})
}
}
#[allow(dead_code)]
async fn check_musl_interpreter() -> Result<(), String> {
const MUSL_PATH: &str = if cfg!(target_arch = "aarch64") {
"/lib/ld-musl-aarch64.so.1"
} else {
"/lib/ld-musl-x86_64.so.1"
};
if fs::metadata(MUSL_PATH).await.is_err() {
return Err(format!(
"find {}, which is required to run the {} in musl environments",
MUSL_PATH, QUALITYLESS_SERVER_NAME
));
}
Ok(())
}
#[allow(dead_code)]
async fn check_glibc_version() -> Result<(), String> {
#[cfg(target_env = "gnu")]
let version = {
let v = unsafe { libc::gnu_get_libc_version() };
let v = unsafe { std::ffi::CStr::from_ptr(v) };
let v = v.to_str().unwrap();
extract_generic_version(v)
};
#[cfg(not(target_env = "gnu"))]
let version = {
capture_command("ldd", ["--version"])
.await
.ok()
.and_then(|o| extract_ldd_version(&o.stdout))
};
if let Some(v) = version {
return if v >= *MIN_LDD_VERSION {
Ok(())
} else {
Err(format!(
"find GLIBC >= {} (but found {} instead) for GNU environments",
*MIN_LDD_VERSION, v
))
};
}
Ok(())
}
/// Check for nixos to avoid mandating glibc versions. See:
/// https://github.com/microsoft/vscode-remote-release/issues/7129
#[allow(dead_code)]
async fn check_is_nixos() -> bool {
fs::metadata(NIXOS_TEST_PATH).await.is_ok()
}
/// Do not remove this check.
/// Provides a way to skip the server glibc requirements check from
/// outside the install flow. A system process can create this
/// file before the server is downloaded and installed.
#[cfg(not(windows))]
pub async fn skip_requirements_check() -> bool {
fs::metadata("/tmp/vscode-skip-server-requirements-check")
.await
.is_ok()
}
#[cfg(windows)]
pub async fn skip_requirements_check() -> bool {
false
}
#[allow(dead_code)]
async fn check_glibcxx_version() -> Result<(), String> {
let mut libstdc_path: Option<String> = None;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
const DEFAULT_LIB_PATH: &str = "/usr/lib64/libstdc++.so.6";
#[cfg(any(target_arch = "x86", target_arch = "arm"))]
const DEFAULT_LIB_PATH: &str = "/usr/lib/libstdc++.so.6";
const LDCONFIG_PATH: &str = "/sbin/ldconfig";
if fs::metadata(DEFAULT_LIB_PATH).await.is_ok() {
libstdc_path = Some(DEFAULT_LIB_PATH.to_owned());
} else if fs::metadata(LDCONFIG_PATH).await.is_ok() {
libstdc_path = capture_command(LDCONFIG_PATH, ["-p"])
.await
.ok()
.and_then(|o| extract_libstd_from_ldconfig(&o.stdout));
}
match libstdc_path {
Some(path) => match fs::read(&path).await {
Ok(contents) => check_for_sufficient_glibcxx_versions(contents),
Err(e) => Err(format!(
"validate GLIBCXX version for GNU environments, but could not: {}",
e
)),
},
None => Err("find libstdc++.so or ldconfig for GNU environments".to_owned()),
}
}
#[allow(dead_code)]
fn check_for_sufficient_glibcxx_versions(contents: Vec<u8>) -> Result<(), String> {
let all_versions: Vec<SimpleSemver> = LIBSTD_CXX_VERSION_RE
.captures_iter(&contents)
.map(|m| SimpleSemver {
major: m.get(1).map_or(0, |s| u32_from_bytes(s.as_bytes())),
minor: m.get(2).map_or(0, |s| u32_from_bytes(s.as_bytes())),
patch: m.get(3).map_or(0, |s| u32_from_bytes(s.as_bytes())),
})
.collect();
if !all_versions.iter().any(|v| &*MIN_CXX_VERSION >= v) {
return Err(format!(
"find GLIBCXX >= {} (but found {} instead) for GNU environments",
*MIN_CXX_VERSION,
all_versions
.iter()
.map(String::from)
.collect::<Vec<String>>()
.join(", ")
));
}
Ok(())
}
#[allow(dead_code)]
fn extract_ldd_version(output: &[u8]) -> Option<SimpleSemver> {
LDD_VERSION_RE.captures(output).map(|m| SimpleSemver {
major: m.get(1).map_or(0, |s| u32_from_bytes(s.as_bytes())),
minor: m.get(2).map_or(0, |s| u32_from_bytes(s.as_bytes())),
patch: 0,
})
}
#[allow(dead_code)]
fn extract_generic_version(output: &str) -> Option<SimpleSemver> {
GENERIC_VERSION_RE.captures(output).map(|m| SimpleSemver {
major: m.get(1).map_or(0, |s| s.as_str().parse().unwrap()),
minor: m.get(2).map_or(0, |s| s.as_str().parse().unwrap()),
patch: 0,
})
}
fn extract_libstd_from_ldconfig(output: &[u8]) -> Option<String> {
String::from_utf8_lossy(output)
.lines()
.find_map(|l| LDCONFIG_STDC_RE.captures(l))
.and_then(|cap| cap.get(1))
.map(|cap| cap.as_str().to_owned())
}
fn u32_from_bytes(b: &[u8]) -> u32 {
String::from_utf8_lossy(b).parse::<u32>().unwrap_or(0)
}
#[derive(Debug, Default, PartialEq, Eq)]
struct SimpleSemver {
major: u32,
minor: u32,
patch: u32,
}
impl PartialOrd for SimpleSemver {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for SimpleSemver {
fn cmp(&self, other: &Self) -> Ordering {
let major = self.major.cmp(&other.major);
if major != Ordering::Equal {
return major;
}
let minor = self.minor.cmp(&other.minor);
if minor != Ordering::Equal {
return minor;
}
self.patch.cmp(&other.patch)
}
}
impl From<&SimpleSemver> for String {
fn from(s: &SimpleSemver) -> Self {
format!("v{}.{}.{}", s.major, s.minor, s.patch)
}
}
impl std::fmt::Display for SimpleSemver {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", String::from(self))
}
}
#[allow(dead_code)]
impl SimpleSemver {
fn new(major: u32, minor: u32, patch: u32) -> SimpleSemver {
SimpleSemver {
major,
minor,
patch,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_libstd_from_ldconfig() {
let actual = "
libstoken.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libstoken.so.1
libstemmer.so.0d (libc6,x86-64) => /lib/x86_64-linux-gnu/libstemmer.so.0d
libstdc++.so.6 (libc6,x86-64) => /lib/x86_64-linux-gnu/libstdc++.so.6
libstartup-notification-1.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libstartup-notification-1.so.0
libssl3.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libssl3.so
".to_owned().into_bytes();
assert_eq!(
extract_libstd_from_ldconfig(&actual),
Some("/lib/x86_64-linux-gnu/libstdc++.so.6".to_owned()),
);
assert_eq!(
extract_libstd_from_ldconfig(&"nothing here!".to_owned().into_bytes()),
None,
);
}
#[test]
fn test_gte() {
assert!(SimpleSemver::new(1, 2, 3) >= SimpleSemver::new(1, 2, 3));
assert!(SimpleSemver::new(1, 2, 3) >= SimpleSemver::new(0, 10, 10));
assert!(SimpleSemver::new(1, 2, 3) >= SimpleSemver::new(1, 1, 10));
assert!(SimpleSemver::new(1, 2, 3) < SimpleSemver::new(1, 2, 10));
assert!(SimpleSemver::new(1, 2, 3) < SimpleSemver::new(1, 3, 1));
assert!(SimpleSemver::new(1, 2, 3) < SimpleSemver::new(2, 2, 1));
}
#[test]
fn check_for_sufficient_glibcxx_versions() {
let actual = "ldd (Ubuntu GLIBC 2.31-0ubuntu9.7) 2.31
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper."
.to_owned()
.into_bytes();
assert_eq!(
extract_ldd_version(&actual),
Some(SimpleSemver::new(2, 31, 0)),
);
}
}