Skip to content

Replace direct dependency on libc with rustix #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
os: [ubuntu-latest, windows-latest]
# When updating this, the reminder to update the minimum supported
# Rust version in Cargo.toml.
rust: ['1.47']
rust: ['1.48']
steps:
- uses: actions/checkout@v3
- name: Install Rust
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "async-process"
version = "1.6.0"
authors = ["Stjepan Glavina <[email protected]>"]
edition = "2018"
rust-version = "1.47"
rust-version = "1.48"
description = "Async interface for working with processes"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/smol-rs/async-process"
Expand All @@ -25,7 +25,7 @@ autocfg = "1"

[target.'cfg(unix)'.dependencies]
async-io = "1.8"
libc = "0.2.88"
rustix = { version = "0.36", default-features = false, features = ["std", "fs"] }

[target.'cfg(unix)'.dependencies.signal-hook]
version = "0.3.0"
Expand Down
26 changes: 6 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ impl Child {
(s, r)
}


// Called when a child exits.
unsafe extern "system" fn callback(_: *mut c_void, _: BOOLEAN) {
callback_channel().0.try_send(()).ok();
Expand Down Expand Up @@ -485,7 +484,7 @@ impl ChildStdin {
Ok(self.0.into_inner().await.into())
} else if #[cfg(unix)] {
let child_stdin = self.0.into_inner()?;
blocking_fd(child_stdin.as_raw_fd())?;
blocking_fd(rustix::fd::AsFd::as_fd(&child_stdin))?;
Ok(child_stdin.into())
}
}
Expand Down Expand Up @@ -577,7 +576,7 @@ impl ChildStdout {
Ok(self.0.into_inner().await.into())
} else if #[cfg(unix)] {
let child_stdout = self.0.into_inner()?;
blocking_fd(child_stdout.as_raw_fd())?;
blocking_fd(rustix::fd::AsFd::as_fd(&child_stdout))?;
Ok(child_stdout.into())
}
}
Expand Down Expand Up @@ -650,7 +649,7 @@ impl ChildStderr {
Ok(self.0.into_inner().await.into())
} else if #[cfg(unix)] {
let child_stderr = self.0.into_inner()?;
blocking_fd(child_stderr.as_raw_fd())?;
blocking_fd(rustix::fd::AsFd::as_fd(&child_stderr))?;
Ok(child_stderr.into())
}
}
Expand Down Expand Up @@ -1063,22 +1062,9 @@ impl fmt::Debug for Command {

/// Moves `Fd` out of non-blocking mode.
#[cfg(unix)]
fn blocking_fd(fd: std::os::unix::io::RawFd) -> io::Result<()> {
// Helper macro to execute a system call that returns an `io::Result`.
macro_rules! syscall {
($fn:ident ( $($arg:expr),* $(,)? ) ) => {{
let res = unsafe { libc::$fn($($arg, )*) };
if res == -1 {
return Err(std::io::Error::last_os_error());
} else {
res
}
}};
}

let res = syscall!(fcntl(fd, libc::F_GETFL));
syscall!(fcntl(fd, libc::F_SETFL, res & !libc::O_NONBLOCK));

fn blocking_fd(fd: rustix::fd::BorrowedFd<'_>) -> io::Result<()> {
let flags = rustix::fs::fcntl_getfl(fd)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at libstd, on Linux it uses FIONBIO instead of this strategy. We might want to try that instead.

Copy link
Collaborator Author

@taiki-e taiki-e Dec 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That behavior in the standard library seems to have been introduced in rust-lang/rust@efeb42b.

I wondered why it is linux-only, since FIONBIO is available on several other unix-like systems. After a little looking into it, in the standard library it seems mainly because it could not find docs on other operating systems (rust-lang/rust#39514 (comment)), and also this comment in libuv indicates that there are cases where it is not supported, at least on BSD. Also, this commit in mio indicates that it is not working as we expected on illumos.

So, for now, it seems to make sense to do it only on linux.

rustix::fs::fcntl_setfl(fd, flags & !rustix::fs::OFlags::NONBLOCK)?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion tests/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ fn test_override_env() {
let mut cmd = env_cmd();
cmd.env_clear().env("RUN_TEST_NEW_ENV", "123");
if let Some(p) = env::var_os("PATH") {
cmd.env("PATH", &p);
cmd.env("PATH", p);
}
let result = cmd.output().await.unwrap();
let output = String::from_utf8_lossy(&result.stdout).to_string();
Expand Down