66
77use anyhow:: { Context , Result } ;
88use futures:: stream:: TryStreamExt ;
9- use nix:: mount:: { MsFlags , mount , umount} ;
9+ use nix:: mount:: umount;
1010use nix:: sched:: { CloneFlags , setns} ;
11- use rtnetlink:: { Handle , IpVersion , new_connection} ;
11+ use rtnetlink:: { Handle , new_connection} ;
1212use std:: fs;
1313use std:: net:: Ipv4Addr ;
1414use std:: os:: unix:: io:: AsRawFd ;
15- use std:: path:: { Path , PathBuf } ;
16- use tracing:: { debug, info} ;
15+ use std:: os:: unix:: process:: ExitStatusExt ;
16+ use std:: path:: PathBuf ;
17+ use tracing:: debug;
1718
1819const NETNS_RUN_DIR : & str = "/var/run/netns" ;
1920
@@ -77,7 +78,7 @@ pub fn create_netns(name: &str) -> Result<()> {
7778 }
7879 }
7980
80- info ! ( "Created network namespace: {}" , name) ;
81+ debug ! ( "Created network namespace: {}" , name) ;
8182 Ok ( ( ) )
8283}
8384
@@ -124,13 +125,13 @@ where
124125 fs:: File :: open ( "/proc/self/ns/net" ) . context ( "Failed to open current network namespace" ) ?;
125126
126127 // Enter the target namespace
127- setns ( netns_fd. as_raw_fd ( ) , CloneFlags :: CLONE_NEWNET ) . context ( "Failed to enter namespace" ) ?;
128+ setns ( & netns_fd, CloneFlags :: CLONE_NEWNET ) . context ( "Failed to enter namespace" ) ?;
128129
129130 // Execute the function
130131 let result = f ( ) ;
131132
132133 // Return to original namespace
133- let _ = setns ( current_ns. as_raw_fd ( ) , CloneFlags :: CLONE_NEWNET ) ;
134+ let _ = setns ( & current_ns, CloneFlags :: CLONE_NEWNET ) ;
134135
135136 result
136137}
@@ -267,14 +268,14 @@ pub async fn get_handle_in_netns(name: &str) -> Result<Handle> {
267268 fs:: File :: open ( "/proc/self/ns/net" ) . context ( "Failed to open current network namespace" ) ?;
268269
269270 // Enter the target namespace
270- setns ( netns_fd. as_raw_fd ( ) , CloneFlags :: CLONE_NEWNET ) . context ( "Failed to enter namespace" ) ?;
271+ setns ( & netns_fd, CloneFlags :: CLONE_NEWNET ) . context ( "Failed to enter namespace" ) ?;
271272
272273 // Create connection in this namespace
273274 let ( connection, handle, _) = new_connection ( ) ?;
274275 tokio:: spawn ( connection) ;
275276
276277 // Return to original namespace
277- let _ = setns ( current_ns. as_raw_fd ( ) , CloneFlags :: CLONE_NEWNET ) ;
278+ let _ = setns ( & current_ns, CloneFlags :: CLONE_NEWNET ) ;
278279
279280 Ok ( handle)
280281}
@@ -298,15 +299,16 @@ pub fn execute_in_netns(
298299 let netns_path = PathBuf :: from ( NETNS_RUN_DIR ) . join ( namespace_name) ;
299300 let netns_fd = std:: fs:: File :: open ( & netns_path)
300301 . with_context ( || format ! ( "Failed to open namespace {:?}" , netns_path) ) ?;
302+ let netns_raw_fd = netns_fd. as_raw_fd ( ) ;
301303
302304 // Fork and exec in the namespace
303305 unsafe {
304306 match libc:: fork ( ) {
305307 -1 => anyhow:: bail!( "fork() failed: {}" , std:: io:: Error :: last_os_error( ) ) ,
306308 0 => {
307309 // Child process
308- // Enter the network namespace
309- if setns ( netns_fd . as_raw_fd ( ) , CloneFlags :: CLONE_NEWNET ) . is_err ( ) {
310+ // Enter the network namespace using raw libc call
311+ if libc :: setns ( netns_raw_fd , libc :: CLONE_NEWNET ) != 0 {
310312 libc:: _exit ( 127 ) ;
311313 }
312314
0 commit comments