fix: dup_into closes existing fd before inserting (POSIX dup3 semantics)

dup_into previously returned EEXIST when the target fd slot was already
occupied, causing procmgr panics during bootstrap. The fix matches dup2
behavior: close the target fd first, then insert the new one.

Also removes diagnostic crate::info! logging added during root cause
investigation of the FD table desync between userspace FILETABLE and
kernel posix_fdtbl.
This commit is contained in:
2026-07-12 01:51:00 +03:00
parent 0f3840a5b5
commit 7a4bd3d491
2 changed files with 13 additions and 28 deletions
-9
View File
@@ -854,15 +854,6 @@ impl FdTbl {
self.active_count += 1;
Some(i)
} else {
crate::info!("insert_file EEXIST: index={:#x} real_index={} fdtbl.len()={} is_upper={} posix_fdtbl.len()={} upper_fdtbl.len()={} active_count={}", index, real_index, fdtbl.len(), index & UPPER_FDTBL_TAG != 0, self.posix_fdtbl.len(), self.upper_fdtbl.len(), self.active_count);
// Dump the posix fdtbl contents
for (i, slot) in self.posix_fdtbl.iter().enumerate() {
if slot.is_some() {
crate::info!(" posix_fdtbl[{}] = Some(<file>)", i);
} else {
crate::info!(" posix_fdtbl[{}] = None", i);
}
}
None
}
}
+13 -19
View File
@@ -309,34 +309,28 @@ pub fn dup(fd: FileHandle, buf: UserSliceRo, token: &mut CleanLockToken) -> Resu
}
/// Duplicate a file descriptor, placing into a specific fd slot (upstream 0.9.0).
/// If the target slot is occupied, the existing descriptor is closed first
/// (POSIX `dup3` semantics, consistent with `dup2` below).
pub fn dup_into(
fd: FileHandle,
new_cd: FileHandle,
buf: UserSliceRo,
token: &mut CleanLockToken,
) -> Result<FileHandle> {
crate::info!("dup_into: fd={:#x} new_cd={:#x} buf_len={}", fd.get(), new_cd.get(), buf.len());
let new_file = match duplicate_file(fd, buf, false, token) {
Ok(f) => f,
Err(e) => {
crate::info!("dup_into: duplicate_file failed: {}", e);
return Err(e);
}
};
crate::info!("dup_into: duplicate_file ok, inserting at new_cd={:#x}", new_cd.get());
if fd == new_cd {
return Ok(new_cd);
}
let _ = close(new_cd, token);
let new_file = duplicate_file(fd, buf, false, token)?;
let current_lock = context::current();
let mut current = current_lock.read(token.token());
let (context, mut token) = current.token_split();
match context.insert_file(new_cd, new_file, &mut token) {
Some(h) => {
crate::info!("dup_into: insert ok, handle={:#x}", h.get());
Ok(h)
}
None => {
crate::info!("dup_into: insert_file returned None (EEXIST)");
Err(Error::new(EEXIST))
}
}
context
.insert_file(new_cd, new_file, &mut token)
.ok_or(Error::new(EMFILE))
}
/// Duplicate file descriptor, replacing another