Skip to content

Commit

Permalink
propagate NONBLOCK flag to msg_send/recv
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Jun 15, 2024
1 parent c661d38 commit 198b410
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 27 deletions.
8 changes: 5 additions & 3 deletions cykusz-rs/src/arch/x86_64/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ fn prepare_tls(vm: &VM, p_table: &mut P4Table, tls: &TlsVmInfo) -> VirtAddr {
let rem = tls.file_size - offset;
let to_read = core::cmp::min(PAGE_SIZE, rem);

if let Ok(r) = tls.file.inode().read_at(tls.file_offset + offset, unsafe {
frame.address_mapped().as_bytes_mut(to_read)
}, OpenFlags::empty()) {
if let Ok(r) = tls.file.inode().read_at(
tls.file_offset + offset,
unsafe { frame.address_mapped().as_bytes_mut(to_read) },
OpenFlags::empty(),
) {
if r != to_read {
panic!("Failed to read tls data");
}
Expand Down
12 changes: 10 additions & 2 deletions cykusz-rs/src/drivers/ps2/kbd/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ impl Device for KbdState {
}

impl INode for KbdState {
fn read_at(&self, _offset: usize, buf: &mut [u8], flags: OpenFlags) -> crate::kernel::fs::vfs::Result<usize> {
fn read_at(
&self,
_offset: usize,
buf: &mut [u8],
flags: OpenFlags,
) -> crate::kernel::fs::vfs::Result<usize> {
if buf.len() % core::mem::size_of::<Event>() != 0 {
Err(FsError::InvalidParam)
} else {
Ok(self.buf.read_data_flags(buf, WaitQueueFlags::IRQ_DISABLE | WaitQueueFlags::from(flags))?)
Ok(self.buf.read_data_flags(
buf,
WaitQueueFlags::IRQ_DISABLE | WaitQueueFlags::from(flags),
)?)
}
}

Expand Down
12 changes: 10 additions & 2 deletions cykusz-rs/src/drivers/ps2/mouse/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,19 @@ impl Device for MouseState {
}

impl INode for MouseState {
fn read_at(&self, _offset: usize, buf: &mut [u8], flags: OpenFlags) -> crate::kernel::fs::vfs::Result<usize> {
fn read_at(
&self,
_offset: usize,
buf: &mut [u8],
flags: OpenFlags,
) -> crate::kernel::fs::vfs::Result<usize> {
if buf.len() % core::mem::size_of::<Event>() != 0 {
Err(FsError::InvalidParam)
} else {
Ok(self.buf.read_data_flags(buf, WaitQueueFlags::IRQ_DISABLE | WaitQueueFlags::from(flags))?)
Ok(self.buf.read_data_flags(
buf,
WaitQueueFlags::IRQ_DISABLE | WaitQueueFlags::from(flags),
)?)
}
}

Expand Down
7 changes: 6 additions & 1 deletion cykusz-rs/src/drivers/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ impl Random {
}

impl INode for Random {
fn read_at(&self, _offset: usize, buf: &mut [u8], _flags: OpenFlags) -> crate::kernel::fs::vfs::Result<usize> {
fn read_at(
&self,
_offset: usize,
buf: &mut [u8],
_flags: OpenFlags,
) -> crate::kernel::fs::vfs::Result<usize> {
self.rng.lock().fill_bytes(buf);

Ok(buf.len())
Expand Down
8 changes: 5 additions & 3 deletions cykusz-rs/src/drivers/tty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ impl Tty {
}
let mut buffer = self
.wait_queue
.wait_lock_for(WaitQueueFlags::IRQ_DISABLE | WaitQueueFlags::from(flags), &self.buffer, |lck| {
lck.has_data()
})?
.wait_lock_for(
WaitQueueFlags::IRQ_DISABLE | WaitQueueFlags::from(flags),
&self.buffer,
|lck| lck.has_data(),
)?
.ok_or(FsError::WouldBlock)?;

Ok(buffer.read(buf, len))
Expand Down
4 changes: 1 addition & 3 deletions cykusz-rs/src/kernel/fs/ext2/inode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,8 @@ impl INode for LockedExt2INode {
}

let new_inode = self.mk_inode(FileType::Symlink)?;

self.ext2_fs().free_inode(&new_inode.as_ext2_inode());
if let Err(e) = new_inode.write_at(0, target.as_bytes(), OpenFlags::empty()) {
self.ext2_fs().free_inode(&new_inode.as_ext2_inode());

return Err(e);
}

Expand Down
6 changes: 5 additions & 1 deletion cykusz-rs/src/kernel/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ pub fn read_link(inode: &Arc<dyn INode>) -> Result<String> {
let mut offset = 0;

loop {
offset += inode.read_at(offset, &mut path.as_mut_slice()[offset..], OpenFlags::empty())?;
offset += inode.read_at(
offset,
&mut path.as_mut_slice()[offset..],
OpenFlags::empty(),
)?;

if offset == path.len() {
path.resize(offset + 128, 0);
Expand Down
2 changes: 1 addition & 1 deletion cykusz-rs/src/kernel/fs/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum FsError {
IsPipe,
Interrupted,
NoSuchDevice,
WouldBlock
WouldBlock,
}

impl From<FsError> for syscall_defs::SyscallError {
Expand Down
4 changes: 3 additions & 1 deletion cykusz-rs/src/kernel/net/tcp/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,9 @@ impl INode for Socket {

//println!("[ TCP ] Proxy Buffer avail: {}", data.proxy_buffer.available_size());

while data.proxy_buffer.available_size() < buf.len() && !flags.contains(OpenFlags::NONBLOCK) {
while data.proxy_buffer.available_size() < buf.len()
&& !flags.contains(OpenFlags::NONBLOCK)
{
if let Err(e) = WaitQueue::wait_lock(data) {
data = self.data.lock();

Expand Down
22 changes: 18 additions & 4 deletions cykusz-rs/src/kernel/net/unix/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,18 +357,32 @@ impl INode for Socket {
Ok(stat)
}

fn read_at(&self, _offset: usize, buf: &mut [u8], flags: OpenFlags) -> crate::kernel::fs::vfs::Result<usize> {
Ok(self.buffer.read_data_flags(buf, WaitQueueFlags::from(flags))?)
fn read_at(
&self,
_offset: usize,
buf: &mut [u8],
flags: OpenFlags,
) -> crate::kernel::fs::vfs::Result<usize> {
Ok(self
.buffer
.read_data_flags(buf, WaitQueueFlags::from(flags))?)
}

fn write_at(&self, _offset: usize, buf: &[u8], flags: OpenFlags) -> crate::kernel::fs::vfs::Result<usize> {
fn write_at(
&self,
_offset: usize,
buf: &[u8],
flags: OpenFlags,
) -> crate::kernel::fs::vfs::Result<usize> {
let target = if let SocketState::Connected(target) = &*self.data.lock() {
target.clone()
} else {
return Err(FsError::NotSupported);
};

Ok(target.buffer.append_data_flags(buf, WaitQueueFlags::from(flags))?)
Ok(target
.buffer
.append_data_flags(buf, WaitQueueFlags::from(flags))?)
}

fn poll(
Expand Down
12 changes: 10 additions & 2 deletions cykusz-rs/src/kernel/syscall/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,11 @@ pub fn sys_msg_recv(sockfd: u64, hdr: u64, flags: u64) -> SyscallResult {

let hdr = unsafe { VirtAddr(hdr as usize).read_mut::<MsgHdr>() };

let sock = get_socket(sockfd as usize)?;
let task = current_task_ref();

let sock = task
.get_handle(sockfd as usize)
.ok_or(SyscallError::EBADFD)?;

logln5!("msg_recv got socket!");

Expand All @@ -897,7 +901,11 @@ pub fn sys_msg_send(sockfd: u64, hdr: u64, flags: u64) -> SyscallResult {
hdr.msg_name.addr()
);

let sock = get_socket(sockfd as usize)?;
let task = current_task_ref();

let sock = task
.get_handle(sockfd as usize)
.ok_or(SyscallError::EBADFD)?;

logln!("sys_msg_send got socket!");

Expand Down
17 changes: 17 additions & 0 deletions cykusz-rs/src/kernel/task/filetable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloc::sync::Arc;
use alloc::vec::Vec;
use core::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

use syscall_defs::net::{MsgFlags, MsgHdr};
use syscall_defs::poll::PollEventFlags;
use syscall_defs::{
FDFlags, FileType, OpenFlags, SeekWhence, SysDirEntry, SyscallError, SyscallResult,
Expand Down Expand Up @@ -94,6 +95,22 @@ impl FileHandle {
})
}

pub fn msg_recv(&self, hdr: &mut MsgHdr, flags: MsgFlags) -> SyscallResult {
let sock = self.get_inode().as_socket().ok_or(SyscallError::EINVAL)?;

sock.msg_recv(hdr, flags | MsgFlags::from(self.flags()))
.map_err(|e| match e {
SyscallError::EAGAIN => SyscallError::ENOMSG,
e => e,
})
}

pub fn msg_send(&self, hdr: &MsgHdr, flags: MsgFlags) -> SyscallResult {
let sock = self.get_inode().as_socket().ok_or(SyscallError::EINVAL)?;

sock.msg_send(hdr, flags | MsgFlags::from(self.flags()))
}

pub fn seek(&self, off: isize, whence: syscall_defs::SeekWhence) -> Result<usize> {
let meta = self.inode.metadata().ok().ok_or(FsError::IsPipe)?;

Expand Down
9 changes: 6 additions & 3 deletions cykusz-rs/src/kernel/utils/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::kernel::fs::vfs;
use alloc::vec::Vec;
use core::sync::atomic::{AtomicBool, Ordering};
use crate::kernel::fs::vfs;

use crate::kernel::fs::vfs::FsError;
use crate::kernel::signal::SignalResult;
Expand Down Expand Up @@ -155,7 +155,11 @@ impl BufferQueue {
written
}

pub fn append_data_flags(&self, data: &[u8], flags: WaitQueueFlags) -> crate::kernel::fs::vfs::Result<usize> {
pub fn append_data_flags(
&self,
data: &[u8],
flags: WaitQueueFlags,
) -> crate::kernel::fs::vfs::Result<usize> {
if data.is_empty() {
return Ok(0);
}
Expand Down Expand Up @@ -184,7 +188,6 @@ impl BufferQueue {
self.reader_queue.notify_one();

Ok(written)

}

pub fn append_data(&self, data: &[u8]) -> crate::kernel::fs::vfs::Result<usize> {
Expand Down
7 changes: 6 additions & 1 deletion syscall-defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,12 @@ impl From<MsgFlags> for OpenFlags {

impl OpenFlags {
pub fn set_fd_flags_mask() -> usize {
(OpenFlags::APPEND | OpenFlags::ASYNC | OpenFlags::DIRECT | OpenFlags::NOATIME | OpenFlags::NONBLOCK).bits()
(OpenFlags::APPEND
| OpenFlags::ASYNC
| OpenFlags::DIRECT
| OpenFlags::NOATIME
| OpenFlags::NONBLOCK)
.bits()
}

pub fn is_open_mode(&self, open_mode: OpenFlags) -> bool {
Expand Down

0 comments on commit 198b410

Please sign in to comment.