Skip to content

Commit d8dc8d8

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

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/error.rs

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pub enum FromEnvErrorKind {
2424
CannotOpenPath,
2525
/// Cannot open file descriptor from the jobserver environment variable value.
2626
CannotOpenFd,
27+
/// The jobserver style is a simple pipe, but at least one of the file descriptors
28+
/// is negative, which means it is disabled for this process
29+
/// ([GNU `make` manual: POSIX Jobserver Interaction](https://www.gnu.org/software/make/manual/make.html#POSIX-Jobserver)).
30+
NegativeFd,
2731
/// File descriptor from the jobserver environment variable value is not a pipe.
2832
NotAPipe,
2933
/// Jobserver inheritance is not supported on this platform.
@@ -39,6 +43,7 @@ impl FromEnvError {
3943
FromEnvErrorInner::CannotParse(_) => FromEnvErrorKind::CannotParse,
4044
FromEnvErrorInner::CannotOpenPath(..) => FromEnvErrorKind::CannotOpenPath,
4145
FromEnvErrorInner::CannotOpenFd(..) => FromEnvErrorKind::CannotOpenFd,
46+
FromEnvErrorInner::NegativeFd(..) => FromEnvErrorKind::NegativeFd,
4247
FromEnvErrorInner::NotAPipe(..) => FromEnvErrorKind::NotAPipe,
4348
FromEnvErrorInner::Unsupported => FromEnvErrorKind::Unsupported,
4449
}
@@ -53,6 +58,7 @@ impl std::fmt::Display for FromEnvError {
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}"),
61+
FromEnvErrorInner::NegativeFd(fd) => write!(f, "file descriptor {fd} from the jobserver environment variable value is negative"),
5662
FromEnvErrorInner::NotAPipe(fd, None) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe"),
5763
FromEnvErrorInner::NotAPipe(fd, Some(err)) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe: {err}"),
5864
FromEnvErrorInner::Unsupported => write!(f, "jobserver inheritance is not supported on this platform"),
@@ -79,6 +85,7 @@ pub(crate) enum FromEnvErrorInner {
7985
CannotParse(String),
8086
CannotOpenPath(String, std::io::Error),
8187
CannotOpenFd(RawFd, std::io::Error),
88+
NegativeFd(RawFd),
8289
NotAPipe(RawFd, Option<std::io::Error>),
8390
Unsupported,
8491
}

src/unix.rs

+9
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ impl Client {
130130
.parse()
131131
.map_err(|e| FromEnvErrorInner::CannotParse(format!("cannot parse `write` fd: {e}")))?;
132132

133+
// If either or both of these file descriptors are negative,
134+
// it means the jobserver is disabled for this process.
135+
if read < 0 {
136+
return Err(FromEnvErrorInner::NegativeFd(read));
137+
}
138+
if write < 0 {
139+
return Err(FromEnvErrorInner::NegativeFd(write));
140+
}
141+
133142
// Ok so we've got two integers that look like file descriptors, but
134143
// for extra sanity checking let's see if they actually look like
135144
// valid files and instances of a pipe if feature enabled before we

0 commit comments

Comments
 (0)