@@ -13,8 +13,17 @@ use serde::Deserialize;
13
13
use tokio:: fs:: File ;
14
14
use tokio:: io:: { AsyncBufReadExt , AsyncWriteExt , BufReader } ;
15
15
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
+ }
18
27
19
28
/// An extension trait for `tokio::process::Command`.
20
29
pub trait CommandExt {
@@ -32,15 +41,15 @@ impl CommandExt for tokio::process::Command {
32
41
let sync_fd = sync. child_fd ;
33
42
34
43
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 )
37
46
. pre_exec ( move || {
38
- if libc:: dup2 ( start_fd, START_PIPE_FD ) == -1 {
47
+ if libc:: dup2 ( start_fd, START_PIPE_FD . 0 ) == -1 {
39
48
eprintln ! ( "failed to duplicate start pipe file descriptor" ) ;
40
49
return Err ( std:: io:: Error :: last_os_error ( ) ) ;
41
50
}
42
51
43
- if libc:: dup2 ( sync_fd, SYNC_PIPE_FD ) == -1 {
52
+ if libc:: dup2 ( sync_fd, SYNC_PIPE_FD . 0 ) == -1 {
44
53
eprintln ! ( "failed to duplicate sync pipe file descriptor" ) ;
45
54
return Err ( std:: io:: Error :: last_os_error ( ) ) ;
46
55
}
0 commit comments