@@ -22,6 +22,7 @@ use std::os::unix::fs::PermissionsExt as _;
2222use std:: os:: unix:: net:: { UnixDatagram , UnixListener , UnixStream } ;
2323use std:: path:: { Path , PathBuf } ;
2424use std:: sync:: Arc ;
25+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
2526use std:: time:: Duration ;
2627
2728use async_trait:: async_trait;
@@ -591,34 +592,27 @@ impl ReadyBoundary for DockerReady {
591592 listener_guard,
592593 mediation,
593594 } = * self ;
594- let expected_metadata = topology. listener_metadata . clone ( ) ;
595- let expected_container = topology. container_id . clone ( ) ;
596- let accept_task = tokio:: task:: spawn_blocking ( move || {
597- let ( stream, _) = listener. accept ( ) . map_err ( |error| {
598- BackendError :: Process ( format ! ( "accept Docker seccomp listener: {error}" ) )
599- } ) ?;
600- receive_listener_fd ( stream, & expected_metadata, & expected_container)
601- } ) ;
595+ let ( listener_runtime, first_listener) = DockerListenerRuntime :: start (
596+ listener,
597+ topology. listener_metadata . clone ( ) ,
598+ topology. container_id . clone ( ) ,
599+ mediation. clone ( ) ,
600+ ) ?;
602601
603602 if let Err ( error) = docker. start_container ( & topology. container_id , None ) . await {
604- accept_task. abort ( ) ;
605603 return Err ( docker_error ( "start Docker boundary" , error) ) ;
606604 }
607605
608- let listener_fd = tokio:: time:: timeout ( LISTENER_ACCEPT_TIMEOUT , accept_task )
606+ tokio:: time:: timeout ( LISTENER_ACCEPT_TIMEOUT , first_listener )
609607 . await
610608 . map_err ( |_| {
611609 BackendError :: Process (
612610 "timed out waiting for Docker seccomp listener FD" . to_string ( ) ,
613611 )
614612 } ) ?
615613 . map_err ( |error| {
616- BackendError :: Process ( format ! ( "Docker seccomp listener task failed : {error}" ) )
614+ BackendError :: Process ( format ! ( "Docker seccomp listener stopped : {error}" ) )
617615 } ) ??;
618- let mediation_for_worker = mediation. clone ( ) ;
619- tokio:: task:: spawn_blocking ( move || {
620- run_notification_worker ( listener_fd, mediation_for_worker) ;
621- } ) ;
622616
623617 let process = Arc :: new ( DockerProcess {
624618 docker : docker. clone ( ) ,
@@ -633,15 +627,90 @@ impl ReadyBoundary for DockerReady {
633627 } ) ,
634628 port_forward : Arc :: new ( UnsupportedDockerPortForward ) ,
635629 _listener_guard : listener_guard,
630+ _listener_runtime : listener_runtime,
636631 } ) )
637632 }
638633}
639634
635+ struct DockerListenerRuntime {
636+ shutdown : Arc < AtomicBool > ,
637+ task : tokio:: task:: JoinHandle < ( ) > ,
638+ }
639+
640+ impl DockerListenerRuntime {
641+ fn start (
642+ listener : UnixListener ,
643+ expected_metadata : String ,
644+ expected_container : String ,
645+ mediation : Arc < DockerMediation > ,
646+ ) -> Result < ( Self , oneshot:: Receiver < Result < ( ) , BackendError > > ) , BackendError > {
647+ listener. set_nonblocking ( true ) . map_err ( |error| {
648+ BackendError :: Process ( format ! ( "prepare Docker seccomp listener: {error}" ) )
649+ } ) ?;
650+ let shutdown = Arc :: new ( AtomicBool :: new ( false ) ) ;
651+ let worker_shutdown = shutdown. clone ( ) ;
652+ let runtime = tokio:: runtime:: Handle :: current ( ) ;
653+ let ( first_tx, first_rx) = oneshot:: channel ( ) ;
654+ let task = tokio:: task:: spawn_blocking ( move || {
655+ let mut first_tx = Some ( first_tx) ;
656+ while !worker_shutdown. load ( Ordering :: Acquire ) {
657+ let stream = match listener. accept ( ) {
658+ Ok ( ( stream, _) ) => stream,
659+ Err ( error) if error. kind ( ) == std:: io:: ErrorKind :: WouldBlock => {
660+ std:: thread:: sleep ( Duration :: from_millis ( 10 ) ) ;
661+ continue ;
662+ }
663+ Err ( error) => {
664+ if let Some ( first_tx) = first_tx. take ( ) {
665+ let _ = first_tx. send ( Err ( BackendError :: Process ( format ! (
666+ "accept Docker seccomp listener: {error}"
667+ ) ) ) ) ;
668+ }
669+ break ;
670+ }
671+ } ;
672+ if let Err ( error) = stream. set_nonblocking ( false ) {
673+ if let Some ( first_tx) = first_tx. take ( ) {
674+ let _ = first_tx. send ( Err ( BackendError :: Process ( format ! (
675+ "prepare accepted Docker seccomp listener: {error}"
676+ ) ) ) ) ;
677+ }
678+ break ;
679+ }
680+ let listener_fd =
681+ match receive_listener_fd ( stream, & expected_metadata, & expected_container) {
682+ Ok ( listener_fd) => listener_fd,
683+ Err ( error) => {
684+ if let Some ( first_tx) = first_tx. take ( ) {
685+ let _ = first_tx. send ( Err ( error) ) ;
686+ }
687+ break ;
688+ }
689+ } ;
690+ let mediation = mediation. clone ( ) ;
691+ runtime. spawn_blocking ( move || run_notification_worker ( listener_fd, mediation) ) ;
692+ if let Some ( first_tx) = first_tx. take ( ) {
693+ let _ = first_tx. send ( Ok ( ( ) ) ) ;
694+ }
695+ }
696+ } ) ;
697+ Ok ( ( Self { shutdown, task } , first_rx) )
698+ }
699+ }
700+
701+ impl Drop for DockerListenerRuntime {
702+ fn drop ( & mut self ) {
703+ self . shutdown . store ( true , Ordering :: Release ) ;
704+ self . task . abort ( ) ;
705+ }
706+ }
707+
640708struct DockerRunning {
641709 process : Arc < DockerProcess > ,
642710 exec : Arc < DockerExec > ,
643711 port_forward : Arc < UnsupportedDockerPortForward > ,
644712 _listener_guard : Arc < ListenerPathGuard > ,
713+ _listener_runtime : DockerListenerRuntime ,
645714}
646715
647716impl RunningBoundary for DockerRunning {
@@ -974,7 +1043,7 @@ struct UnsupportedDockerPortForward;
9741043impl BoundaryPortForward for UnsupportedDockerPortForward {
9751044 async fn connect ( & self , _target : LoopbackTarget ) -> Result < BoundaryDuplexStream , BackendError > {
9761045 Err ( BackendError :: Unsupported (
977- "Docker create proof does not implement port forwarding" . to_string ( ) ,
1046+ "Docker attach proof does not implement port forwarding" . to_string ( ) ,
9781047 ) )
9791048 }
9801049}
@@ -1103,25 +1172,25 @@ const IOC_READ: u32 = 2;
11031172
11041173// Linux reserves IOC_SIZEBITS for the payload size. Both fixed seccomp
11051174// notification structs are far smaller than u32::MAX on supported targets.
1106- #[ allow( clippy:: cast_possible_truncation) ]
1107- const fn ioctl_read_write < T > ( kind : u8 , number : u8 ) -> libc:: c_ulong {
1175+ #[ allow( clippy:: cast_possible_truncation, clippy :: cast_possible_wrap ) ]
1176+ const fn ioctl_read_write < T > ( kind : u8 , number : u8 ) -> libc:: Ioctl {
11081177 ( ( IOC_READ | IOC_WRITE ) << IOC_DIRSHIFT
11091178 | ( kind as u32 ) << IOC_TYPESHIFT
11101179 | ( number as u32 ) << IOC_NRSHIFT
1111- | ( size_of :: < T > ( ) as u32 ) << IOC_SIZESHIFT ) as libc:: c_ulong
1180+ | ( size_of :: < T > ( ) as u32 ) << IOC_SIZESHIFT ) as libc:: Ioctl
11121181}
11131182
1114- #[ allow( clippy:: cast_possible_truncation) ]
1115- const fn ioctl_write < T > ( kind : u8 , number : u8 ) -> libc:: c_ulong {
1183+ #[ allow( clippy:: cast_possible_truncation, clippy :: cast_possible_wrap ) ]
1184+ const fn ioctl_write < T > ( kind : u8 , number : u8 ) -> libc:: Ioctl {
11161185 ( IOC_WRITE << IOC_DIRSHIFT
11171186 | ( kind as u32 ) << IOC_TYPESHIFT
11181187 | ( number as u32 ) << IOC_NRSHIFT
1119- | ( size_of :: < T > ( ) as u32 ) << IOC_SIZESHIFT ) as libc:: c_ulong
1188+ | ( size_of :: < T > ( ) as u32 ) << IOC_SIZESHIFT ) as libc:: Ioctl
11201189}
11211190
1122- const SECCOMP_IOCTL_NOTIF_RECV : libc:: c_ulong = ioctl_read_write :: < SeccompNotif > ( b'!' , 0 ) ;
1123- const SECCOMP_IOCTL_NOTIF_SEND : libc:: c_ulong = ioctl_read_write :: < SeccompNotifResp > ( b'!' , 1 ) ;
1124- const SECCOMP_IOCTL_NOTIF_ADDFD : libc:: c_ulong = ioctl_write :: < SeccompNotifAddfd > ( b'!' , 3 ) ;
1191+ const SECCOMP_IOCTL_NOTIF_RECV : libc:: Ioctl = ioctl_read_write :: < SeccompNotif > ( b'!' , 0 ) ;
1192+ const SECCOMP_IOCTL_NOTIF_SEND : libc:: Ioctl = ioctl_read_write :: < SeccompNotifResp > ( b'!' , 1 ) ;
1193+ const SECCOMP_IOCTL_NOTIF_ADDFD : libc:: Ioctl = ioctl_write :: < SeccompNotifAddfd > ( b'!' , 3 ) ;
11251194const SECCOMP_USER_NOTIF_FLAG_CONTINUE : u32 = 1 ;
11261195const SECCOMP_ADDFD_FLAG_SEND : u32 = 2 ;
11271196
0 commit comments