Skip to content

Commit

Permalink
Replace accept4 with accept and fcntl
Browse files Browse the repository at this point in the history
Since macOS does not have `accept4`, this commit replaces it with
`accept` and `fcntl` to avoid fd leak to child processes.

Signed-off-by: Akira Moroo <[email protected]>
  • Loading branch information
retrage committed Apr 19, 2024
1 parent f817535 commit b9890f1
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
//! Virtio socket support for Rust.
use libc::{
accept4, ioctl, sa_family_t, sockaddr, sockaddr_vm, socklen_t, suseconds_t, timeval, AF_VSOCK,
FIONBIO, SOCK_CLOEXEC,
accept, fcntl, ioctl, sa_family_t, sockaddr, sockaddr_vm, socklen_t, suseconds_t, timeval,
AF_VSOCK, FD_CLOEXEC, FIONBIO, F_SETFD,
};
use nix::{
ioctl_read_bad,
Expand All @@ -28,6 +28,7 @@ use nix::{
sockopt::{ReceiveTimeout, SendTimeout, SocketError},
AddressFamily, Backlog, GetSockOpt, MsgFlags, SetSockOpt, SockFlag, SockType,
},
unistd::close,
};
use std::mem::size_of;
use std::net::Shutdown;
Expand Down Expand Up @@ -123,14 +124,17 @@ impl VsockListener {
};
let mut vsock_addr_len = size_of::<sockaddr_vm>() as socklen_t;
let socket = unsafe {
accept4(
accept(
self.socket.as_raw_fd(),
&mut vsock_addr as *mut _ as *mut sockaddr,
&mut vsock_addr_len,
SOCK_CLOEXEC,
)
};
if socket < 0 {
return Err(Error::last_os_error());
}
if unsafe { fcntl(socket, F_SETFD, FD_CLOEXEC) } < 0 {
close(socket)?;
Err(Error::last_os_error())
} else {
Ok((
Expand Down

0 comments on commit b9890f1

Please sign in to comment.