Enter this network namespace. Must be called from the child process after fork, before exec. Uses `setns()` to switch the calling process into the namespace. # Errors Returns an error if setns fails. # Safety This function should only be called in a `pre_exec` context after fork.
(&self)
| 218 | /// |
| 219 | /// This function should only be called in a `pre_exec` context after fork. |
| 220 | pub fn enter(&self) -> Result<()> { |
| 221 | if let Some(fd) = self.ns_fd { |
| 222 | debug!(namespace = %self.name, "Entering network namespace via setns"); |
| 223 | // SAFETY: setns is safe to call after fork, before exec |
| 224 | // libc/syscall FFI requires unsafe |
| 225 | #[allow(unsafe_code)] |
| 226 | let result = unsafe { libc::setns(fd, libc::CLONE_NEWNET) }; |
| 227 | if result != 0 { |
| 228 | return Err(miette::miette!( |
| 229 | "setns failed: {}", |
| 230 | std::io::Error::last_os_error() |
| 231 | )); |
| 232 | } |
| 233 | Ok(()) |
| 234 | } else { |
| 235 | Err(miette::miette!( |
| 236 | "No namespace file descriptor available for setns" |
| 237 | )) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// Get the namespace file descriptor for use with clone/unshare. |
| 242 | #[must_use] |
no outgoing calls