Skip to content

Commit 2ecd026

Browse files
Avoid allocating in pre_exec closure (#340)
1 parent d1aad75 commit 2ecd026

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

vopono_core/src/network/application_wrapper.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl ApplicationWrapper {
173173

174174
let netns_path_cstr = CString::new(format!("/var/run/netns/{}", netns.name))?;
175175
let want_controlling_tty = take_controlling_tty;
176+
let root_c = CString::new("/").unwrap();
176177
// Prepare bind-mount sources for /etc overlay
177178
let etc_ns_dir = format!("/etc/netns/{}", netns.name);
178179
let resolv_src = CString::new(format!("{}/resolv.conf", &etc_ns_dir)).ok();
@@ -181,6 +182,7 @@ impl ApplicationWrapper {
181182
let resolv_dst = CString::new("/etc/resolv.conf").unwrap();
182183
let hosts_dst = CString::new("/etc/hosts").unwrap();
183184
let nsswitch_dst = CString::new("/etc/nsswitch.conf").unwrap();
185+
let ping_path = CString::new("/proc/sys/net/ipv4/ping_group_range").unwrap();
184186

185187
unsafe {
186188
handle.pre_exec(move || {
@@ -189,26 +191,19 @@ impl ApplicationWrapper {
189191
setns(
190192
ns_fd.try_clone().expect("Clone failed"),
191193
CloneFlags::CLONE_NEWNET,
192-
)
193-
.map_err(|e| std::io::Error::other(format!("pre_exec: setns failed: {e}")))?;
194+
)?;
194195
close(ns_fd)?;
195196

196197
// Create a private mount namespace for the child to safely overlay /etc files
197-
unshare(CloneFlags::CLONE_NEWNS).map_err(|e| {
198-
std::io::Error::other(format!("pre_exec: unshare(CLONE_NEWNS) failed: {e}"))
199-
})?;
198+
unshare(CloneFlags::CLONE_NEWNS)?;
200199
// Make mounts private to avoid propagating to the host
201-
let root_c = CString::new("/").unwrap();
202200
mount::<std::ffi::CStr, std::ffi::CStr, std::ffi::CStr, std::ffi::CStr>(
203201
None,
204202
root_c.as_c_str(),
205203
None,
206204
MsFlags::MS_REC | MsFlags::MS_PRIVATE,
207205
None,
208-
)
209-
.map_err(|e| {
210-
std::io::Error::other(format!("pre_exec: mount MS_PRIVATE failed: {e}"))
211-
})?;
206+
)?;
212207

213208
// Helper to bind a file if the source exists
214209
let bind_if_exists =
@@ -227,12 +222,7 @@ impl ApplicationWrapper {
227222
None,
228223
MsFlags::MS_BIND,
229224
None,
230-
)
231-
.map_err(|e| {
232-
std::io::Error::other(format!(
233-
"pre_exec: bind mount failed: {e}"
234-
))
235-
})?;
225+
)?;
236226
}
237227
Ok(())
238228
};
@@ -244,7 +234,6 @@ impl ApplicationWrapper {
244234

245235
// Enable unprivileged ping inside the netns by widening ping_group_range
246236
// Write "0 2147483647" to /proc/sys/net/ipv4/ping_group_range via raw syscalls
247-
let ping_path = CString::new("/proc/sys/net/ipv4/ping_group_range").unwrap();
248237
let fd = libc::open(ping_path.as_ptr(), libc::O_WRONLY);
249238
if fd >= 0 {
250239
let data = b"0 2147483647\n";

0 commit comments

Comments
 (0)