Skip to content

Commit

Permalink
disable jobserver on unix, if file descriptors are negative
Browse files Browse the repository at this point in the history
  • Loading branch information
belovdv committed Feb 1, 2024
1 parent d357534 commit d4107e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub enum FromEnvErrorKind {
/// There is no jobserver in the environment variable.
/// Variables associated with Make can be used for passing data other than jobserver info.
NoJobserver,
/// Environment variable contains negative file descriptors which means
/// jobserver is disabled for this process
NegativeFd,
/// Cannot parse jobserver environment variable value, incorrect format.
CannotParse,
/// Cannot open path or name from the jobserver environment variable value.
Expand All @@ -36,6 +39,7 @@ impl FromEnvError {
match self.inner {
FromEnvErrorInner::NoEnvVar => FromEnvErrorKind::NoEnvVar,
FromEnvErrorInner::NoJobserver => FromEnvErrorKind::NoJobserver,
FromEnvErrorInner::NegativeFd(..) => FromEnvErrorKind::NegativeFd,
FromEnvErrorInner::CannotParse(_) => FromEnvErrorKind::CannotParse,
FromEnvErrorInner::CannotOpenPath(..) => FromEnvErrorKind::CannotOpenPath,
FromEnvErrorInner::CannotOpenFd(..) => FromEnvErrorKind::CannotOpenFd,
Expand All @@ -50,6 +54,7 @@ impl std::fmt::Display for FromEnvError {
match &self.inner {
FromEnvErrorInner::NoEnvVar => write!(f, "there is no environment variable that describes jobserver to inherit"),
FromEnvErrorInner::NoJobserver => write!(f, "there is no `--jobserver-fds=` or `--jobserver-auth=` in the environment variable"),
FromEnvErrorInner::NegativeFd(read, write) => write!(f, "either or both passed file descriptors are negative: {read}, {write}"),
FromEnvErrorInner::CannotParse(s) => write!(f, "cannot parse jobserver environment variable value: {s}"),
FromEnvErrorInner::CannotOpenPath(s, err) => write!(f, "cannot open path or name {s} from the jobserver environment variable value: {err}"),
FromEnvErrorInner::CannotOpenFd(fd, err) => write!(f, "cannot open file descriptor {fd} from the jobserver environment variable value: {err}"),
Expand All @@ -76,6 +81,7 @@ impl std::error::Error for FromEnvError {
pub(crate) enum FromEnvErrorInner {
NoEnvVar,
NoJobserver,
NegativeFd(RawFd, RawFd),
CannotParse(String),
CannotOpenPath(String, std::io::Error),
CannotOpenFd(RawFd, std::io::Error),
Expand Down
8 changes: 8 additions & 0 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ impl Client {
}

/// `--jobserver-auth=R,W`
///
/// If either or both of these file descriptors are negative,
/// it means the jobserver is disabled for this process
/// (See ["GNU male manual: POSIX Jobserver Interaction"](https://www.gnu.org/software/make/manual/make.html#POSIX-Jobserver)).
unsafe fn from_pipe(s: &str, check_pipe: bool) -> Result<Option<Client>, FromEnvErrorInner> {
let mut parts = s.splitn(2, ',');
let read = parts.next().unwrap();
Expand All @@ -130,6 +134,10 @@ impl Client {
.parse()
.map_err(|e| FromEnvErrorInner::CannotParse(format!("cannot parse `write` fd: {e}")))?;

if read < 0 || write < 0 {
return Err(FromEnvErrorInner::NegativeFd(read, write));
}

// Ok so we've got two integers that look like file descriptors, but
// for extra sanity checking let's see if they actually look like
// valid files and instances of a pipe if feature enabled before we
Expand Down

0 comments on commit d4107e4

Please sign in to comment.