From 72d8e4ca21c747dec2e818e9345affc5791bdfec Mon Sep 17 00:00:00 2001 From: BelovDV <70999565+BelovDV@users.noreply.github.com> Date: Thu, 23 Mar 2023 12:15:52 +0300 Subject: [PATCH] [temp] switch 'check_pipe' to runtime option --- Cargo.toml | 6 ++---- src/lib.rs | 14 +++++++++----- src/unix.rs | 19 ++++++++----------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f41f4076..6fd6c03a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,4 @@ + [package] name = "jobserver" version = "0.1.26" @@ -14,9 +15,6 @@ edition = "2018" [target.'cfg(unix)'.dependencies] libc = "0.2.50" -[features] -do_not_check_pipe = [] - [dev-dependencies] futures = "0.1" num_cpus = "1.0" @@ -45,4 +43,4 @@ harness = false [[test]] name = "helper" -path = "tests/helper.rs" +path = "tests/helper.rs" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 680c6b67..5b680ee7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -250,8 +250,8 @@ impl Client { /// with `CLOEXEC` so they're not automatically inherited by spawned /// children. /// - /// There is a feature to configure, will this function check if provided - /// files are actually pipes. + /// On unix if `unix_check_is_pipe` enabled this function will check if + /// provided files are actually pipes. /// /// # Safety /// @@ -269,7 +269,7 @@ impl Client { /// /// Note, though, that on Windows it should be safe to call this function /// any number of times. - pub unsafe fn from_env_ext() -> Result { + pub unsafe fn from_env_ext(check_pipe: bool) -> Result { let (env, var) = ["CARGO_MAKEFLAGS", "MAKEFLAGS", "MFLAGS"] .iter() .map(|&env| env::var(env).map(|var| (env, var))) @@ -283,7 +283,11 @@ impl Client { .ok_or(ErrFromEnv::IsNotConfigured)?; let s = var[pos + arg.len()..].split(' ').next().unwrap(); - match imp::Client::open(s) { + #[cfg(unix)] + let imp_client = imp::Client::open(s, check_pipe); + #[cfg(not(unix))] + let imp_client = imp::Client::open(s); + match imp_client { Ok(c) => Ok(Client { inner: Arc::new(c) }), Err(err) => Err(ErrFromEnv::PlatformSpecific { err, env, var }), } @@ -294,7 +298,7 @@ impl Client { /// /// Wraps `from_env_ext` and discards error details. pub unsafe fn from_env() -> Option { - Self::from_env_ext().ok() + Self::from_env_ext(false).ok() } /// Acquires a token from this jobserver client. diff --git a/src/unix.rs b/src/unix.rs index 0a50b699..89918259 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -81,11 +81,11 @@ impl Client { Ok(Client::from_fds(pipes[0], pipes[1])) } - pub unsafe fn open(s: &str) -> io::Result { + pub unsafe fn open(s: &str, check_pipe: bool) -> io::Result { if let Some(client) = Self::from_fifo(s)? { return Ok(client); } - if let Some(client) = Self::from_pipe(s)? { + if let Some(client) = Self::from_pipe(s, check_pipe)? { return Ok(client); } Err(io::Error::new( @@ -111,7 +111,7 @@ impl Client { } /// `--jobserver-auth=R,W` - unsafe fn from_pipe(s: &str) -> io::Result> { + unsafe fn from_pipe(s: &str, check_pipe: bool) -> io::Result> { let mut parts = s.splitn(2, ','); let read = parts.next().unwrap(); let write = match parts.next() { @@ -133,8 +133,8 @@ impl Client { // If we're called from `make` *without* the leading + on our rule // then we'll have `MAKEFLAGS` env vars but won't actually have // access to the file descriptors. - check_fd(read)?; - check_fd(write)?; + check_fd(read, check_pipe)?; + check_fd(write, check_pipe)?; drop(set_cloexec(read, true)); drop(set_cloexec(write, true)); Ok(Some(Client::from_fds(read, write))) @@ -386,9 +386,8 @@ impl Helper { } } -fn check_fd(fd: c_int) -> io::Result<()> { - #[cfg(not(feature = "do_not_check_pipe"))] - unsafe { +unsafe fn check_fd(fd: c_int, check_pipe: bool) -> io::Result<()> { + if check_pipe { let mut stat = mem::zeroed(); if libc::fstat(fd, &mut stat) == -1 { Err(io::Error::last_os_error()) @@ -404,9 +403,7 @@ fn check_fd(fd: c_int) -> io::Result<()> { } Err(io::Error::last_os_error()) // } - } - #[cfg(feature = "do_not_check_pipe")] - unsafe { + } else { match libc::fcntl(fd, libc::F_GETFD) { r if r == -1 => Err(io::Error::new( io::ErrorKind::InvalidData,