@@ -13,8 +13,17 @@ use serde::Deserialize;
1313use tokio:: fs:: File ;
1414use tokio:: io:: { AsyncBufReadExt , AsyncWriteExt , BufReader } ;
1515
16- const START_PIPE_FD : RawFd = 3 ;
17- const SYNC_PIPE_FD : RawFd = 4 ;
16+ // We embed the stringified file descriptors as consts to avoid dynamic string allocations.
17+ macro_rules! define_fds {
18+ ( $( const $name: ident = $fd: expr; ) +) => {
19+ $( const $name: ( RawFd , & str ) = ( $fd, stringify!( $fd) ) ; ) +
20+ } ;
21+ }
22+
23+ define_fds ! {
24+ const START_PIPE_FD = 3 ;
25+ const SYNC_PIPE_FD = 4 ;
26+ }
1827
1928/// An extension trait for `tokio::process::Command`.
2029pub trait CommandExt {
@@ -32,15 +41,15 @@ impl CommandExt for tokio::process::Command {
3241 let sync_fd = sync. child_fd ;
3342
3443 unsafe {
35- self . env ( "_OCI_STARTPIPE" , START_PIPE_FD . to_string ( ) )
36- . env ( "_OCI_SYNCPIPE" , SYNC_PIPE_FD . to_string ( ) )
44+ self . env ( "_OCI_STARTPIPE" , START_PIPE_FD . 1 )
45+ . env ( "_OCI_SYNCPIPE" , SYNC_PIPE_FD . 1 )
3746 . pre_exec ( move || {
38- if libc:: dup2 ( start_fd, START_PIPE_FD ) == -1 {
47+ if libc:: dup2 ( start_fd, START_PIPE_FD . 0 ) == -1 {
3948 eprintln ! ( "failed to duplicate start pipe file descriptor" ) ;
4049 return Err ( std:: io:: Error :: last_os_error ( ) ) ;
4150 }
4251
43- if libc:: dup2 ( sync_fd, SYNC_PIPE_FD ) == -1 {
52+ if libc:: dup2 ( sync_fd, SYNC_PIPE_FD . 0 ) == -1 {
4453 eprintln ! ( "failed to duplicate sync pipe file descriptor" ) ;
4554 return Err ( std:: io:: Error :: last_os_error ( ) ) ;
4655 }
0 commit comments