6 use crate::io
::seq_packet
::SeqPacketSocket
;
7 use crate::lxcseccomp
::ProxyMessageBuffer
;
8 use crate::syscall
::{self, Syscall, SyscallStatus}
;
11 socket
: SeqPacketSocket
,
15 pub fn new(socket
: SeqPacketSocket
) -> Arc
<Self> {
16 Arc
::new(Self { socket }
)
19 /// Wrap futures returning a `Result` so if they fail we `shutdown()` the socket to drop the
21 async
fn wrap_error
<F
>(self: Arc
<Self>, fut
: F
)
23 F
: std
::future
::Future
<Output
= Result
<(), Error
>>,
25 if let Err(err
) = fut
.await
{
26 eprintln
!("client error, dropping connection: {}", err
);
27 if let Err(err
) = self.socket
.shutdown(nix
::sys
::socket
::Shutdown
::Both
) {
28 eprintln
!(" (error shutting down client socket: {})", err
);
33 pub async
fn main(self: Arc
<Self>) {
34 self.clone().wrap_error(self.main_do()).await
37 async
fn main_do(self: Arc
<Self>) -> Result
<(), Error
> {
38 let mut msg
= ProxyMessageBuffer
::new(64);
40 if !msg
.recv(&self.socket
).await?
{
44 self.handle_syscall(&mut msg
).await?
;
48 async
fn handle_syscall(&self, msg
: &mut ProxyMessageBuffer
) -> Result
<(), Error
> {
49 let result
= match Self::handle_syscall_do(&msg
).await
{
52 // handle the various kinds of errors we may get:
53 if let Some(errno
) = err
.downcast_ref
::<nix
::errno
::Errno
>() {
54 SyscallStatus
::Err(*errno
as _
)
55 } else if let Some(nix
::Error
::Sys(errno
)) = err
.downcast_ref
::<nix
::Error
>() {
56 SyscallStatus
::Err(*errno
as _
)
57 } else if let Some(ioerr
) = err
.downcast_ref
::<std
::io
::Error
>() {
58 if let Some(errno
) = ioerr
.raw_os_error() {
59 SyscallStatus
::Err(errno
)
69 let resp
= msg
.response_mut();
71 SyscallStatus
::Ok(val
) => {
75 SyscallStatus
::Err(err
) => {
81 msg
.respond(&self.socket
).await
.map_err(Error
::from
)
84 async
fn handle_syscall_do(msg
: &ProxyMessageBuffer
) -> Result
<SyscallStatus
, Error
> {
85 let (arch
, sysnr
) = (msg
.request().data
.arch
, msg
.request().data
.nr
);
87 let syscall_nr
= match syscall
::translate_syscall(arch
, sysnr
) {
89 None
=> return Ok(Errno
::ENOSYS
.into()),
93 Syscall
::Mknod
=> crate::sys_mknod
::mknod(msg
).await
,
94 Syscall
::MknodAt
=> crate::sys_mknod
::mknodat(msg
).await
,
95 Syscall
::Quotactl
=> crate::sys_quotactl
::quotactl(msg
).await
,