Skip to content

Commit 975f18b

Browse files
committed
Support fcntl 'F_GETPIPE_SZ'
1 parent ff8141f commit 975f18b

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/main/host/descriptor/pipe.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ impl PipeFile {
5050
self.mode
5151
}
5252

53+
pub fn max_size(&self) -> usize {
54+
self.buffer.borrow().max_len()
55+
}
56+
5357
pub fn close(&mut self, event_queue: &mut EventQueue) -> SyscallResult {
5458
// set the closed flag and remove the active flag
5559
self.copy_state(
@@ -227,6 +231,10 @@ impl SharedBuf {
227231
self.queue.len() == 0
228232
}
229233

234+
pub fn max_len(&self) -> usize {
235+
self.max_len
236+
}
237+
230238
pub fn space_available(&self) -> usize {
231239
self.max_len - self.queue.len()
232240
}

src/main/host/syscall/fcntl.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cshadow;
22
use crate::host::context::{ThreadContext, ThreadContextObjs};
3-
use crate::host::descriptor::{CompatDescriptor, DescriptorFlags, FileStatus};
3+
use crate::host::descriptor::{CompatDescriptor, DescriptorFlags, FileStatus, PosixFile};
44
use crate::host::syscall;
55
use crate::host::syscall_types::SyscallResult;
66
use crate::host::syscall_types::{SysCallArgs, SysCallReg};
@@ -93,6 +93,15 @@ fn fcntl(ctx: &mut ThreadContext, args: &SysCallArgs) -> SyscallResult {
9393
.register_descriptor_with_min_fd(new_desc, min_fd);
9494
SysCallReg::from(i32::try_from(new_fd).unwrap())
9595
}
96+
libc::F_GETPIPE_SZ =>
97+
{
98+
#[allow(irrefutable_let_patterns)]
99+
if let PosixFile::Pipe(pipe) = desc.get_file() {
100+
SysCallReg::from(i32::try_from(pipe.borrow().max_size()).unwrap())
101+
} else {
102+
Err(Errno::EINVAL)?
103+
}
104+
}
96105
_ => Err(Errno::EINVAL)?,
97106
})
98107
}

src/test/pipe/test_pipe.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ fn get_tests() -> Vec<test_utils::ShadowTest<(), String>> {
6161
test_read_from_write_end,
6262
set![TestEnv::Libc, TestEnv::Shadow],
6363
),
64+
test_utils::ShadowTest::new(
65+
"test_get_size",
66+
test_get_size,
67+
set![TestEnv::Libc, TestEnv::Shadow],
68+
),
6469
];
6570

6671
tests
@@ -296,3 +301,23 @@ fn test_read_from_write_end() -> Result<(), String> {
296301
Ok(())
297302
})
298303
}
304+
305+
fn test_get_size() -> Result<(), String> {
306+
let mut fds = [0 as libc::c_int; 2];
307+
test_utils::check_system_call!(|| { unsafe { libc::pipe(fds.as_mut_ptr()) } }, &[])?;
308+
309+
test_utils::result_assert(fds[0] > 0, "fds[0] not set")?;
310+
test_utils::result_assert(fds[1] > 0, "fds[1] not set")?;
311+
312+
let (read_fd, write_fd) = (fds[0], fds[1]);
313+
314+
test_utils::run_and_close_fds(&[write_fd, read_fd], || {
315+
let size = test_utils::check_system_call!(
316+
|| unsafe { libc::fcntl(read_fd, libc::F_GETPIPE_SZ) },
317+
&[]
318+
)?;
319+
assert!(size > 0);
320+
321+
Ok(())
322+
})
323+
}

0 commit comments

Comments
 (0)