diff --git a/src/fd/mod.rs b/src/fd/mod.rs index f5900a7cda..342a166f66 100644 --- a/src/fd/mod.rs +++ b/src/fd/mod.rs @@ -380,19 +380,29 @@ pub(crate) fn poll(fds: &mut [PollFd], timeout: i32) -> Result<(), IoError> { } } +#[inline] +async fn async_get_object(fd: FileDescriptor) -> Result, IoError> { + Ok((*(OBJECT_MAP.read().await.get(&fd).ok_or(IoError::EINVAL)?)).clone()) +} + pub(crate) fn get_object(fd: FileDescriptor) -> Result, IoError> { block_on(async_get_object(fd), None) } -async fn async_get_object(fd: FileDescriptor) -> Result, IoError> { - Ok((*(OBJECT_MAP.read().await.get(&fd).ok_or(IoError::EINVAL)?)).clone()) +#[inline] +async fn async_insert_object( + fd: FileDescriptor, + obj: Arc, +) -> Result<(), IoError> { + let _ = OBJECT_MAP.write().await.insert(fd, obj); + Ok(()) } pub(crate) fn insert_object( fd: FileDescriptor, obj: Arc, -) -> Option> { - block_on(async { Ok(OBJECT_MAP.write().await.insert(fd, obj)) }, None).unwrap() +) -> Result<(), IoError> { + block_on(async_insert_object(fd, obj), None) } // The dup system call allocates a new file descriptor that refers diff --git a/src/lib.rs b/src/lib.rs index 2d5de36e6c..9d388fac70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -283,7 +283,7 @@ extern "C" fn initd(_arg: usize) { riscv64::kernel::init_drivers(); syscalls::init(); - fd::init().unwrap(); + fd::init().expect("Unable to initialized standard file descriptors"); fs::init(); // Get the application arguments and environment variables. diff --git a/src/syscalls/net.rs b/src/syscalls/net.rs index 3d90bc9856..03789f9f85 100644 --- a/src/syscalls/net.rs +++ b/src/syscalls/net.rs @@ -270,7 +270,7 @@ extern "C" fn __sys_socket(domain: i32, type_: i32, protocol: i32) -> i32 { drop(guard); let socket = udp::Socket::new(handle); - insert_object(fd, Arc::new(socket)); + insert_object(fd, Arc::new(socket)).expect("FD is already used"); return fd; } @@ -281,7 +281,7 @@ extern "C" fn __sys_socket(domain: i32, type_: i32, protocol: i32) -> i32 { drop(guard); let socket = tcp::Socket::new(handle); - insert_object(fd, Arc::new(socket)); + insert_object(fd, Arc::new(socket)).expect("FD is already used"); return fd; } @@ -302,9 +302,9 @@ extern "C" fn __sys_accept(fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t |e| -num::ToPrimitive::to_i32(&e).unwrap(), |endpoint| { let new_obj = dyn_clone::clone_box(&*v); - insert_object(fd, Arc::from(new_obj)); + insert_object(fd, Arc::from(new_obj)).expect("FD is already used"); let new_fd = FD_COUNTER.fetch_add(1, Ordering::SeqCst); - insert_object(new_fd, v.clone()); + insert_object(new_fd, v.clone()).expect("FD is already used"); if !addr.is_null() && !addrlen.is_null() { let addrlen = unsafe { &mut *addrlen };