-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathlibc_asset.rs
114 lines (107 loc) · 4.09 KB
/
libc_asset.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
#[cfg(all(target_os = "linux", target_env = "musl"))]
#[cfg(target_arch = "x86_64")]
#[derive(rust_embed::RustEmbed)]
#[folder = "src/libc/x86_64/"]
struct Asset;
#[cfg(all(target_os = "linux", target_env = "musl"))]
#[cfg(target_arch = "aarch64")]
#[derive(rust_embed::RustEmbed)]
#[folder = "src/libc/aarch64/"]
struct Asset;
#[cfg(target_os = "linux")]
pub(crate) fn ld_env(envs: &mut std::collections::HashMap<String, String>) -> anyhow::Result<()> {
use crate::{env, util};
use anyhow::Context;
use std::ffi::CString;
use std::ops::Not;
use std::path::Path;
if is_musl()?.not() {
log::debug!("[Asset] Run on glibc environment");
return Ok(());
}
log::debug!("[Asset] Run on musl environment");
#[cfg(target_arch = "x86_64")]
const LD: &str = "ld-linux-x86-64.so.2";
#[cfg(target_arch = "aarch64")]
const LD: &str = "ld-linux-aarch64.so.1";
let syno_lib_path = std::path::Path::new(env::SYNOPKG_LIB);
if !syno_lib_path.exists() {
std::fs::create_dir(&syno_lib_path).context(format!(
"[Asset] Failed to create directory: {}",
syno_lib_path.display()
))?;
}
for filename in Asset::iter()
.map(|v| v.into_owned())
.collect::<Vec<String>>()
{
let target_file = syno_lib_path.join(&filename);
if !target_file.exists() {
let file = Asset::get(&filename).context("[Asset] Failed to get bin asset")?;
util::write_file(&target_file, file.data, 0o755)?;
}
}
for sys_lib in env::SYS_LIB_ARRAY {
let sys_lib_path = Path::new(sys_lib);
let sys_ld_path = sys_lib_path.join(LD);
let output = std::process::Command::new("ldd")
.arg(env::LAUNCHER_EXE)
.output()
.expect("[Asset] Failed to execute ldd command");
let stdout = String::from_utf8(output.stdout)?;
log::debug!("[Asset] ldd stdout: {}", &stdout);
match output.status.success()
&& stdout.contains(format!("{}", sys_ld_path.display()).as_str())
{
true => {
if sys_lib_path.exists().not() {
util::create_dir_all(&sys_lib_path, 0o755)?
}
// Compatible MUSL systems may come with libc
if sys_ld_path.exists() {
let real_ld_path = std::fs::canonicalize(&sys_ld_path)?;
let real_lib_path = real_ld_path.parent().context(format!(
"[Asset] The library path does not exist: {}",
real_ld_path.display()
))?;
log::info!(
"[Asset] Real path of the symlink {}: {}",
sys_ld_path.display(),
real_ld_path.display()
);
envs.insert(
String::from("LD_LIBRARY_PATH"),
format!("{}", real_lib_path.display()),
);
log::info!(
"[Asset] LD_LIBRARY_PATH={}",
format!("{}", real_lib_path.display())
);
return Ok(());
}
let syno_ld_path = Path::new(env::SYNOPKG_LIB).join(LD);
nix::unistd::symlinkat(syno_ld_path, None, &sys_ld_path)?;
envs.insert(
String::from("LD_LIBRARY_PATH"),
env::SYNOPKG_LIB.to_string(),
);
log::info!("[Asset] LD_LIBRARY_PATH={}", env::SYNOPKG_LIB);
return Ok(());
}
false => {}
}
}
Ok(())
}
fn is_musl() -> anyhow::Result<bool> {
let output = std::process::Command::new("sh")
.args(["-c", "ldd --version"])
.output()
.unwrap();
let out = match output.status.success() {
true => String::from_utf8(output.stdout).unwrap(),
false => String::from_utf8(output.stderr).unwrap(),
};
log::debug!("[Asset] ldd --version stdout: {}", out);
Ok(out.to_lowercase().contains("musl"))
}