Skip to content

Unify descriptor managment into LiteBox core#722

Open
jaybosamiya-ms wants to merge 13 commits intomainfrom
jayb/fd-cleanup
Open

Unify descriptor managment into LiteBox core#722
jaybosamiya-ms wants to merge 13 commits intomainfrom
jayb/fd-cleanup

Conversation

@jaybosamiya-ms
Copy link
Member

@jaybosamiya-ms jaybosamiya-ms commented Mar 13, 2026

This PR fully eliminates the descriptor table indirection we had in the shim, now relying entirely on the LiteBox core to manage file descriptors both for raw FDs as well as the typed FDs.

To do this, I've made the following changes at the core litebox crate:

  • Explicitly started allowing for non-litebox crates to define FdEnabledSubsystems, as a stabilized interface
  • Exposed some public interfaces to interact more with the descriptor tables
  • Introduced a EntryHandle interface, that allows entry-specific locking outside litebox without maintaining a full descriptor table lock

At the shim, roughly it boils down to "remove Descriptors + Descriptor" and deal with the consequences. The changes are quite extensive but are not particularly insightful in any major way. I did clean up a few bugs along the way where (say) rlimits might not have been adhered to, or if two descriptors are made and the second one hit an rlimit issue, then there could have been a leak, etc.

Despite removing a chunk of code, some other code did get a tiny bit more bloated, but the level of indirection is smaller. I think we can further clean this up by removing some of the redundancy, but I think merging this in sooner than that is probably the better move, thus I've opened the PR in the current state.


Related: #31

These were intended to eventually be public, but were locked down in the past.  Now that things are a bit more stable, we can actually make these public.
This was a non-trivial change since it required migrating a bunch of code up to actually use raw FDs everywhere, but this should make the next commit (hopefully) simpler to read.
This gets rid of the `__Unused`, but introduces some clippy things to be cleaned up, but I wanted to keep this particular commit quite "obvious" on its own to see in the diff.
@jaybosamiya-ms jaybosamiya-ms marked this pull request as ready for review March 13, 2026 02:30
@jaybosamiya-ms jaybosamiya-ms requested a review from wdcui March 13, 2026 02:31
@github-actions
Copy link

🤖 SemverChecks 🤖 No breaking API changes detected

Note: this does not mean API is unchanged, or even that there are no breaking changes; simply, none of the detections triggered.

}
// Close whatever is at newfd before duping into it
let newfd_usize = usize::try_from(newfd).or(Err(Errno::EBADF))?;
let _ = self.do_close(newfd_usize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check if the oldfd is valid before closing the newfd?

@@ -1895,30 +2017,19 @@ impl<FS: ShimFS> Task<FS> {
Ok(oldfd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check oldfd is valid?

.fd_into_raw_integer(socket),
)
let Ok(raw_fd) = files.insert_raw_fd(socket) else {
unimplemented!()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return EMFILE instead of panic?

peer_addr,
))
let Ok(raw_fd) = files.insert_raw_fd(accepted_file) else {
unimplemented!()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return EMFILE?

}
Ok(target)
} else {
Ok(rds.fd_into_raw_integer(fd))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call may return a fd that's less than the min_fd checked in fcntl?

.raw_descriptor_store
.read()
.fd_from_raw_integer::<crate::syscalls::unix::UnixSocketSubsystem<FS>>(raw_fd)
.map_err(|_| Errno::ENOTSOCK)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the socket doesn't exist? Should we return EBADF instead of ENOTSOCK?

let raw_fd = sockfd as usize;
let inet_fd = {
let rds = self.raw_descriptor_store.read();
rds.fd_from_raw_integer(raw_fd).ok()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to handle the error path here?

let max_fd = self.max_fd.load(Ordering::Relaxed);
if raw_fd > max_fd {
let orig = rds.fd_consume_raw_integer::<Subsystem>(raw_fd).unwrap();
return Err(alloc::sync::Arc::into_inner(orig).unwrap());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unwrap() may panic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants