Skip to content

Commit f8107e8

Browse files
committed
stdbuf: Search libstdbuf without /proc
1 parent 1340183 commit f8107e8

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

.vscode/cspell.dictionaries/workspace.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ advapi32-sys
88
aho-corasick
99
backtrace
1010
blake2b_simd
11+
rustix
1112

1213
# * uutils project
1314
uutils

Cargo.lock

Lines changed: 9 additions & 8 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 = "1.1.4", features = ["param"] }
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: 3 additions & 0 deletions
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 }
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:

src/uu/stdbuf/src/stdbuf.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore (ToDO) tempdir dyld dylib optgrps libstdbuf
6+
// spell-checker:ignore (ToDO) tempdir dyld dylib optgrps libstdbuf execfn
77

88
use clap::{Arg, ArgAction, ArgMatches, Command};
99
use std::ffi::OsString;
@@ -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)