Skip to content

Commit d7f675f

Browse files
committed
activation: keep going on PID mismatch
This tweaks the file descriptor logic in order to avoid spurioous errors when there is a `$LISTEN_PID` mismatch, or when the variables are unset.
1 parent 79e15f4 commit d7f675f

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

src/activation.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,33 @@ pub fn receive_descriptors(unset_env: bool) -> Result<Vec<FileDescriptor>, SdErr
8989
env::remove_var("LISTEN_FDNAMES");
9090
}
9191

92+
// Parse `$LISTEN_PID` if present.
93+
if let Err(env::VarError::NotPresent) = pid {
94+
return Ok(vec![]);
95+
}
9296
let pid = pid
9397
.context("failed to get LISTEN_PID")?
9498
.parse::<u32>()
9599
.context("failed to parse LISTEN_PID")?;
100+
let current_pid = process::id();
101+
if pid != current_pid {
102+
log::warn!(
103+
"PID mismatch, $LISTEN_PID is {} but current PID is {}",
104+
pid,
105+
current_pid
106+
);
107+
return Ok(vec![]);
108+
}
109+
110+
// Parse `$LISTEN_FDS` if present.
111+
if let Err(env::VarError::NotPresent) = fds {
112+
return Ok(vec![]);
113+
}
96114
let fds = fds
97115
.context("failed to get LISTEN_FDS")?
98116
.parse::<usize>()
99117
.context("failed to parse LISTEN_FDS")?;
100118

101-
if process::id() != pid {
102-
return Err("PID mismatch".into());
103-
}
104-
105119
socks_from_fds(fds)
106120
}
107121

@@ -128,21 +142,40 @@ pub fn receive_descriptors_with_names(
128142
env::remove_var("LISTEN_FDNAMES");
129143
}
130144

145+
// Parse `$LISTEN_PID` if present.
146+
if let Err(env::VarError::NotPresent) = pid {
147+
return Ok(vec![]);
148+
}
131149
let pid = pid
132150
.context("failed to get LISTEN_PID")?
133151
.parse::<u32>()
134152
.context("failed to parse LISTEN_PID")?;
153+
let current_pid = process::id();
154+
if pid != current_pid {
155+
log::warn!(
156+
"PID mismatch, $LISTEN_PID is {} but current PID is {}",
157+
pid,
158+
current_pid
159+
);
160+
return Ok(vec![]);
161+
}
162+
163+
// Parse `$LISTEN_FDS` if present.
164+
if let Err(env::VarError::NotPresent) = fds {
165+
return Ok(vec![]);
166+
}
135167
let fds = fds
136168
.context("failed to get LISTEN_FDS")?
137169
.parse::<usize>()
138170
.context("failed to parse LISTEN_FDS")?;
139171

140-
if process::id() != pid {
141-
return Err("PID mismatch".into());
172+
// Parse `$LISTEN_FDNAMES` if present.
173+
if let Err(env::VarError::NotPresent) = fdnames {
174+
return Ok(vec![]);
142175
}
143-
144176
let fdnames = fdnames.context("failed to get LISTEN_FDNAMES")?;
145177
let names = fdnames.split(':').map(String::from);
178+
146179
let vec = socks_from_fds(fds).context("failed to get sockets from file descriptor")?;
147180
let out = vec.into_iter().zip(names).collect();
148181

0 commit comments

Comments
 (0)