Skip to content

Commit

Permalink
refactor(fd): replace ObjectInterface::ioctl with set_status_flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroening committed Mar 4, 2025
1 parent ef5537f commit 042f31b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 71 deletions.
13 changes: 3 additions & 10 deletions src/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ pub(crate) enum SocketOption {
TcpNoDelay,
}

#[allow(dead_code)]
#[derive(Debug, PartialEq)]
pub(crate) enum IoCtl {
NonBlocking,
}

pub(crate) type FileDescriptor = i32;

bitflags! {
Expand All @@ -71,7 +65,7 @@ bitflags! {
}

bitflags! {
/// File status flags
/// File status flags.
#[derive(Debug, Copy, Clone, Default)]
pub struct StatusFlags: i32 {
const O_APPEND = 0o2000;
Expand Down Expand Up @@ -259,9 +253,8 @@ pub(crate) trait ObjectInterface: Sync + Send + core::fmt::Debug {
Err(io::Error::ENOSYS)
}

/// The `ioctl` function manipulates the underlying device parameters of special
/// files.
async fn ioctl(&self, _cmd: IoCtl, _value: bool) -> io::Result<()> {
/// Sets the file status flags.
async fn set_status_flags(&self, _status_flags: StatusFlags) -> io::Result<()> {
Err(io::Error::ENOSYS)
}

Expand Down
23 changes: 6 additions & 17 deletions src/fd/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use smoltcp::time::Duration;

use crate::executor::block_on;
use crate::executor::network::{Handle, NIC};
use crate::fd::{Endpoint, IoCtl, ListenEndpoint, ObjectInterface, PollEvent, SocketOption};
use crate::fd::{self, Endpoint, ListenEndpoint, ObjectInterface, PollEvent, SocketOption};
use crate::{DEFAULT_KEEP_ALIVE_INTERVAL, io};

/// further receives will be disallowed
Expand Down Expand Up @@ -434,20 +434,9 @@ impl Socket {
}
}

async fn ioctl(&mut self, cmd: IoCtl, value: bool) -> io::Result<()> {
if cmd == IoCtl::NonBlocking {
if value {
trace!("set device to nonblocking mode");
self.is_nonblocking = true;
} else {
trace!("set device to blocking mode");
self.is_nonblocking = false;
}

Ok(())
} else {
Err(io::Error::EINVAL)
}
async fn set_status_flags(&mut self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.is_nonblocking = status_flags.contains(fd::StatusFlags::O_NONBLOCK);
Ok(())
}
}

Expand Down Expand Up @@ -513,7 +502,7 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
self.read().await.shutdown(how).await
}

async fn ioctl(&self, cmd: IoCtl, value: bool) -> io::Result<()> {
self.write().await.ioctl(cmd, value).await
async fn set_status_flags(&self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.write().await.set_status_flags(status_flags).await
}
}
23 changes: 6 additions & 17 deletions src/fd/socket/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use smoltcp::wire::IpEndpoint;

use crate::executor::block_on;
use crate::executor::network::{Handle, NIC};
use crate::fd::{Endpoint, IoCtl, ListenEndpoint, ObjectInterface, PollEvent};
use crate::fd::{self, Endpoint, ListenEndpoint, ObjectInterface, PollEvent};
use crate::io;

#[derive(Debug)]
Expand Down Expand Up @@ -215,20 +215,9 @@ impl Socket {
}
}

async fn ioctl(&mut self, cmd: IoCtl, value: bool) -> io::Result<()> {
if cmd == IoCtl::NonBlocking {
if value {
info!("set device to nonblocking mode");
self.nonblocking = true;
} else {
info!("set device to blocking mode");
self.nonblocking = false;
}

Ok(())
} else {
Err(io::Error::EINVAL)
}
async fn set_status_flags(&mut self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.nonblocking = status_flags.contains(fd::StatusFlags::O_NONBLOCK);
Ok(())
}
}

Expand Down Expand Up @@ -269,7 +258,7 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
self.read().await.write(buf).await
}

async fn ioctl(&self, cmd: IoCtl, value: bool) -> io::Result<()> {
self.write().await.ioctl(cmd, value).await
async fn set_status_flags(&self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.write().await.set_status_flags(status_flags).await
}
}
23 changes: 6 additions & 17 deletions src/fd/socket/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::arch::kernel::mmio as hardware;
#[cfg(feature = "pci")]
use crate::drivers::pci as hardware;
use crate::executor::vsock::{VSOCK_MAP, VsockState};
use crate::fd::{Endpoint, IoCtl, ListenEndpoint, ObjectInterface, PollEvent};
use crate::fd::{self, Endpoint, ListenEndpoint, ObjectInterface, PollEvent};
use crate::io::{self, Error};

#[derive(Debug)]
Expand Down Expand Up @@ -296,20 +296,9 @@ impl Socket {
Ok(())
}

async fn ioctl(&mut self, cmd: IoCtl, value: bool) -> io::Result<()> {
if cmd == IoCtl::NonBlocking {
if value {
trace!("set vsock device to nonblocking mode");
self.is_nonblocking = true;
} else {
trace!("set vsock device to blocking mode");
self.is_nonblocking = false;
}

Ok(())
} else {
Err(io::Error::EINVAL)
}
async fn set_status_flags(&mut self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.is_nonblocking = status_flags.contains(fd::StatusFlags::O_NONBLOCK);
Ok(())
}

async fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
Expand Down Expand Up @@ -461,7 +450,7 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
self.read().await.shutdown(how).await
}

async fn ioctl(&self, cmd: IoCtl, value: bool) -> io::Result<()> {
self.write().await.ioctl(cmd, value).await
async fn set_status_flags(&self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.write().await.set_status_flags(status_flags).await
}
}
18 changes: 13 additions & 5 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use self::tasks::*;
pub use self::timer::*;
use crate::executor::block_on;
use crate::fd::{
self, AccessPermission, EventFlags, FileDescriptor, IoCtl, OpenOption, PollFd, dup_object,
self, AccessPermission, EventFlags, FileDescriptor, OpenOption, PollFd, dup_object,
dup_object2, get_object, isatty, remove_object,
};
use crate::fs::{self, FileAttr};
Expand Down Expand Up @@ -513,12 +513,17 @@ pub unsafe extern "C" fn sys_ioctl(

if cmd == FIONBIO {
let value = unsafe { *(argp as *const i32) };
let status_flags = if value != 0 {
fd::StatusFlags::O_NONBLOCK
} else {
fd::StatusFlags::empty()
};

let obj = get_object(fd);
obj.map_or_else(
|e| -num::ToPrimitive::to_i32(&e).unwrap(),
|v| {
block_on((*v).ioctl(IoCtl::NonBlocking, value != 0), None)
block_on((*v).set_status_flags(status_flags), None)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |()| 0)
},
)
Expand All @@ -537,13 +542,16 @@ pub extern "C" fn sys_fcntl(fd: i32, cmd: i32, arg: i32) -> i32 {

if cmd == F_SETFD && arg == FD_CLOEXEC {
0
} else if cmd == F_SETFL && arg == fd::StatusFlags::O_NONBLOCK.bits() {
} else if cmd == F_SETFL {
let obj = get_object(fd);
obj.map_or_else(
|e| -num::ToPrimitive::to_i32(&e).unwrap(),
|v| {
block_on((*v).ioctl(IoCtl::NonBlocking, true), None)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |()| 0)
block_on(
(*v).set_status_flags(fd::StatusFlags::from_bits_retain(arg)),
None,
)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |()| 0)
},
)
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/syscalls/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::fd::socket::udp;
#[cfg(feature = "vsock")]
use crate::fd::socket::vsock::{self, VsockEndpoint, VsockListenEndpoint};
use crate::fd::{
Endpoint, ListenEndpoint, ObjectInterface, SocketOption, get_object, insert_object,
self, Endpoint, ListenEndpoint, ObjectInterface, SocketOption, get_object, insert_object,
};
use crate::syscalls::{IoCtl, block_on};
use crate::syscalls::block_on;

pub const AF_INET: i32 = 0;
pub const AF_INET6: i32 = 1;
Expand Down Expand Up @@ -427,7 +427,7 @@ pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32
let socket = Arc::new(async_lock::RwLock::new(vsock::Socket::new()));

if type_.contains(SockType::SOCK_NONBLOCK) {
block_on(socket.ioctl(IoCtl::NonBlocking, true), None).unwrap();
block_on(socket.set_status_flags(fd::StatusFlags::O_NONBLOCK), None).unwrap();
}

let fd = insert_object(socket).expect("FD is already used");
Expand All @@ -449,7 +449,7 @@ pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32
let socket = Arc::new(async_lock::RwLock::new(udp::Socket::new(handle)));

if type_.contains(SockType::SOCK_NONBLOCK) {
block_on(socket.ioctl(IoCtl::NonBlocking, true), None).unwrap();
block_on(socket.set_status_flags(fd::StatusFlags::O_NONBLOCK), None).unwrap();
}

let fd = insert_object(socket).expect("FD is already used");
Expand All @@ -464,7 +464,7 @@ pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32
let socket = Arc::new(async_lock::RwLock::new(tcp::Socket::new(handle)));

if type_.contains(SockType::SOCK_NONBLOCK) {
block_on(socket.ioctl(IoCtl::NonBlocking, true), None).unwrap();
block_on(socket.set_status_flags(fd::StatusFlags::O_NONBLOCK), None).unwrap();
}

let fd = insert_object(socket).expect("FD is already used");
Expand Down

0 comments on commit 042f31b

Please sign in to comment.