Skip to content

Commit 09a1eae

Browse files
committed
stdbuf: Search libstdbuf without /proc
1 parent 60b4d1b commit 09a1eae

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

Cargo.lock

Lines changed: 34 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ regex = "1.10.4"
427427
rlimit = "0.11.0"
428428
rstest = "0.26.0"
429429
rustc-hash = "2.1.1"
430+
rustix = { version = "0.38", features = ["param", "runtime"] }
430431
rust-ini = "0.21.0"
431432
same-file = "1.0.6"
432433
self_cell = "1.0.4"

src/uu/stdbuf/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ uucore = { workspace = true, features = ["parser-size"] }
2727
thiserror = { workspace = true }
2828
fluent = { workspace = true }
2929

30+
[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
31+
rustix = { workspace = true, optional = true }
32+
3033
# "feat_external_libstdbuf": use an external libstdbuf.so for stdbuf instead of embedding it into
3134
# the stdbuf binary.
3235
# There are 2 use-cases:
@@ -45,7 +48,7 @@ fluent = { workspace = true }
4548
# installation directory during the build. E.g. LIBSTDBUF_DIR="/usr/lib"
4649

4750
[features]
48-
feat_external_libstdbuf = []
51+
feat_external_libstdbuf = ["dep:rustix"]
4952

5053
[[bin]]
5154
name = "stdbuf"

src/uu/stdbuf/src/stdbuf.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,26 @@ fn get_preload_env(_tmp_dir: &TempDir) -> UResult<(String, PathBuf)> {
183183
// Search paths in order:
184184
// 1. Directory where stdbuf is located (program_path)
185185
// 2. Compile-time directory from LIBSTDBUF_DIR
186-
let mut search_paths: Vec<PathBuf> = Vec::new();
187-
186+
let mut search_paths: Vec<PathBuf> = Vec::with_capacity(2);
188187
// First, try to get the directory where stdbuf is running from
189-
if let Ok(exe_path) = std::env::current_exe() {
190-
if let Some(exe_dir) = exe_path.parent() {
188+
// current_exe() depends on /proc at Linux...
189+
#[cfg(any(target_os = "linux", target_os = "android"))]
190+
use std::os::unix::ffi::OsStrExt;
191+
{
192+
let exe_path = rustix::param::linux_execfn().to_bytes();
193+
if let Some(exe_dir) = std::path::Path::new(std::ffi::OsStr::from_bytes(exe_path)).parent()
194+
{
191195
search_paths.push(exe_dir.to_path_buf());
192196
}
193197
}
198+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
199+
{
200+
if let Ok(exe_path) = std::env::current_exe() {
201+
if let Some(exe_dir) = exe_path.parent() {
202+
search_paths.push(exe_dir.to_path_buf());
203+
}
204+
}
205+
}
194206

195207
// Add the compile-time directory as fallback
196208
search_paths.push(PathBuf::from(LIBSTDBUF_DIR));

0 commit comments

Comments
 (0)