|
| 1 | +//@ignore-target: windows # No libc fstat on non-file FDs on Windows |
| 2 | +//@compile-flags: -Zmiri-disable-isolation |
| 3 | + |
| 4 | +use std::mem::MaybeUninit; |
| 5 | + |
| 6 | +#[path = "../../utils/libc.rs"] |
| 7 | +mod libc_utils; |
| 8 | +use libc_utils::errno_check; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + test_fstat_socketpair(); |
| 12 | + test_fstat_pipe(); |
| 13 | + #[cfg(target_os = "linux")] |
| 14 | + test_fstat_eventfd(); |
| 15 | + #[cfg(target_os = "linux")] |
| 16 | + test_fstat_epoll(); |
| 17 | +} |
| 18 | + |
| 19 | +/// Calls fstat and returns a reference to the result. |
| 20 | +/// We use `assume_init_ref` rather than `assume_init` because not all fields |
| 21 | +/// of `libc::stat` may be written by fstat (e.g. `st_lspare` on macOS). |
| 22 | +fn do_fstat(fd: i32, buf: &mut MaybeUninit<libc::stat>) -> &libc::stat { |
| 23 | + let res = unsafe { libc::fstat(fd, buf.as_mut_ptr()) }; |
| 24 | + assert_eq!(res, 0, "fstat failed on fd {}", fd); |
| 25 | + unsafe { buf.assume_init_ref() } |
| 26 | +} |
| 27 | + |
| 28 | +fn assert_stat_fields_are_accessible(stat: &libc::stat) { |
| 29 | + let _st_nlink = stat.st_nlink; |
| 30 | + let _st_blksize = stat.st_blksize; |
| 31 | + let _st_blocks = stat.st_blocks; |
| 32 | + let _st_ino = stat.st_ino; |
| 33 | + let _st_dev = stat.st_dev; |
| 34 | + let _st_uid = stat.st_uid; |
| 35 | + let _st_gid = stat.st_gid; |
| 36 | + let _st_rdev = stat.st_rdev; |
| 37 | + let _st_atime = stat.st_atime; |
| 38 | + let _st_mtime = stat.st_mtime; |
| 39 | + let _st_ctime = stat.st_ctime; |
| 40 | + let _st_atime_nsec = stat.st_atime_nsec; |
| 41 | + let _st_mtime_nsec = stat.st_mtime_nsec; |
| 42 | + let _st_ctime_nsec = stat.st_ctime_nsec; |
| 43 | +} |
| 44 | + |
| 45 | +/// Test fstat on socketpair file descriptors. |
| 46 | +fn test_fstat_socketpair() { |
| 47 | + let mut fds = [0i32; 2]; |
| 48 | + errno_check(unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) }); |
| 49 | + |
| 50 | + for fd in fds.iter() { |
| 51 | + let mut buf = MaybeUninit::uninit(); |
| 52 | + let stat = do_fstat(*fd, &mut buf); |
| 53 | + assert_eq!( |
| 54 | + stat.st_mode & libc::S_IFMT, |
| 55 | + libc::S_IFSOCK, |
| 56 | + "socketpair should have S_IFSOCK mode" |
| 57 | + ); |
| 58 | + assert_eq!(stat.st_size, 0, "socketpair should have size 0"); |
| 59 | + assert_stat_fields_are_accessible(stat); |
| 60 | + } |
| 61 | + |
| 62 | + errno_check(unsafe { libc::close(fds[0]) }); |
| 63 | + errno_check(unsafe { libc::close(fds[1]) }); |
| 64 | +} |
| 65 | + |
| 66 | +/// Test fstat on pipe file descriptors. |
| 67 | +fn test_fstat_pipe() { |
| 68 | + let mut fds = [0i32; 2]; |
| 69 | + errno_check(unsafe { libc::pipe(fds.as_mut_ptr()) }); |
| 70 | + |
| 71 | + for fd in fds.iter() { |
| 72 | + let mut buf = MaybeUninit::uninit(); |
| 73 | + let stat = do_fstat(*fd, &mut buf); |
| 74 | + assert_eq!(stat.st_mode & libc::S_IFMT, libc::S_IFIFO, "pipe should have S_IFIFO mode"); |
| 75 | + assert_eq!(stat.st_size, 0, "pipe should have size 0"); |
| 76 | + assert_stat_fields_are_accessible(stat); |
| 77 | + } |
| 78 | + |
| 79 | + errno_check(unsafe { libc::close(fds[0]) }); |
| 80 | + errno_check(unsafe { libc::close(fds[1]) }); |
| 81 | +} |
| 82 | + |
| 83 | +/// Test fstat on eventfd file descriptors (Linux only). |
| 84 | +#[cfg(target_os = "linux")] |
| 85 | +fn test_fstat_eventfd() { |
| 86 | + let flags = libc::EFD_CLOEXEC | libc::EFD_NONBLOCK; |
| 87 | + let fd = libc_utils::errno_result(unsafe { libc::eventfd(0, flags) }).unwrap(); |
| 88 | + |
| 89 | + let mut buf = MaybeUninit::uninit(); |
| 90 | + let stat = do_fstat(fd, &mut buf); |
| 91 | + assert_eq!(stat.st_size, 0, "eventfd should have size 0"); |
| 92 | + assert_stat_fields_are_accessible(stat); |
| 93 | + |
| 94 | + errno_check(unsafe { libc::close(fd) }); |
| 95 | +} |
| 96 | + |
| 97 | +/// Test fstat on epoll file descriptors (Linux only). |
| 98 | +#[cfg(target_os = "linux")] |
| 99 | +fn test_fstat_epoll() { |
| 100 | + let fd = libc_utils::errno_result(unsafe { libc::epoll_create1(libc::EPOLL_CLOEXEC) }).unwrap(); |
| 101 | + |
| 102 | + let mut buf = MaybeUninit::uninit(); |
| 103 | + let stat = do_fstat(fd, &mut buf); |
| 104 | + assert_eq!(stat.st_size, 0, "epoll should have size 0"); |
| 105 | + assert_stat_fields_are_accessible(stat); |
| 106 | + |
| 107 | + errno_check(unsafe { libc::close(fd) }); |
| 108 | +} |
0 commit comments