Skip to content

Commit 498af46

Browse files
committed
disable jobserver on unix, if file descriptors are negative
1 parent d357534 commit 498af46

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub enum FromEnvErrorKind {
1818
/// There is no jobserver in the environment variable.
1919
/// Variables associated with Make can be used for passing data other than jobserver info.
2020
NoJobserver,
21+
/// Environment variable contains negative file descriptors which means
22+
/// jobserver is disabled for this process
23+
NegativeFd,
2124
/// Cannot parse jobserver environment variable value, incorrect format.
2225
CannotParse,
2326
/// Cannot open path or name from the jobserver environment variable value.
@@ -36,6 +39,7 @@ impl FromEnvError {
3639
match self.inner {
3740
FromEnvErrorInner::NoEnvVar => FromEnvErrorKind::NoEnvVar,
3841
FromEnvErrorInner::NoJobserver => FromEnvErrorKind::NoJobserver,
42+
FromEnvErrorInner::NegativeFd(..) => FromEnvErrorKind::NegativeFd,
3943
FromEnvErrorInner::CannotParse(_) => FromEnvErrorKind::CannotParse,
4044
FromEnvErrorInner::CannotOpenPath(..) => FromEnvErrorKind::CannotOpenPath,
4145
FromEnvErrorInner::CannotOpenFd(..) => FromEnvErrorKind::CannotOpenFd,
@@ -50,6 +54,7 @@ impl std::fmt::Display for FromEnvError {
5054
match &self.inner {
5155
FromEnvErrorInner::NoEnvVar => write!(f, "there is no environment variable that describes jobserver to inherit"),
5256
FromEnvErrorInner::NoJobserver => write!(f, "there is no `--jobserver-fds=` or `--jobserver-auth=` in the environment variable"),
57+
FromEnvErrorInner::NegativeFd(read, write) => write!(f, "either or both passed file descriptors are negative: {read}, {write}"),
5358
FromEnvErrorInner::CannotParse(s) => write!(f, "cannot parse jobserver environment variable value: {s}"),
5459
FromEnvErrorInner::CannotOpenPath(s, err) => write!(f, "cannot open path or name {s} from the jobserver environment variable value: {err}"),
5560
FromEnvErrorInner::CannotOpenFd(fd, err) => write!(f, "cannot open file descriptor {fd} from the jobserver environment variable value: {err}"),
@@ -76,6 +81,7 @@ impl std::error::Error for FromEnvError {
7681
pub(crate) enum FromEnvErrorInner {
7782
NoEnvVar,
7883
NoJobserver,
84+
NegativeFd(RawFd, RawFd),
7985
CannotParse(String),
8086
CannotOpenPath(String, std::io::Error),
8187
CannotOpenFd(RawFd, std::io::Error),

src/unix.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ impl Client {
116116
}
117117

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

137+
if read < 0 || write < 0 {
138+
return Err(FromEnvErrorInner::NegativeFd(read, write));
139+
}
140+
133141
// Ok so we've got two integers that look like file descriptors, but
134142
// for extra sanity checking let's see if they actually look like
135143
// valid files and instances of a pipe if feature enabled before we

0 commit comments

Comments
 (0)