From 08be41eccfffb42b93946d0a46849a43f8f128b0 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Tue, 17 Mar 2020 17:43:21 -0300 Subject: [PATCH 01/14] Update libc to 0.2.68 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7327987b56..9d4711f1ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ exclude = [ ] [dependencies] -libc = { version = "0.2.60", features = [ "extra_traits" ] } +libc = { version = "0.2.68", features = [ "extra_traits" ] } bitflags = "1.1" cfg-if = "0.1.10" void = "1.0.2" From 360bdfb789714ac1e87208cdf4b45942e4f495f7 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Tue, 17 Mar 2020 17:47:26 -0300 Subject: [PATCH 02/14] Handle open file descriptor locks in fcntl --- src/fcntl.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/fcntl.rs b/src/fcntl.rs index 1d66eb75d4..4754dd8ea2 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -286,6 +286,12 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result { F_SETLKW(flock) => libc::fcntl(fd, libc::F_SETLKW, flock), F_GETLK(flock) => libc::fcntl(fd, libc::F_GETLK, flock), #[cfg(any(target_os = "android", target_os = "linux"))] + F_OFD_SETLK(flock) => libc::fcntl(fd, libc::F_OFD_SETLK, flock), + #[cfg(any(target_os = "android", target_os = "linux"))] + F_OFD_SETLKW(flock) => libc::fcntl(fd, libc::F_OFD_SETLKW, flock), + #[cfg(any(target_os = "android", target_os = "linux"))] + F_OFD_GETLK(flock) => libc::fcntl(fd, libc::F_OFD_GETLK, flock), + #[cfg(any(target_os = "android", target_os = "linux"))] F_ADD_SEALS(flag) => libc::fcntl(fd, libc::F_ADD_SEALS, flag.bits()), #[cfg(any(target_os = "android", target_os = "linux"))] F_GET_SEALS => libc::fcntl(fd, libc::F_GET_SEALS), @@ -295,8 +301,6 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result { F_GETPIPE_SZ => libc::fcntl(fd, libc::F_GETPIPE_SZ), #[cfg(any(target_os = "linux", target_os = "android"))] F_SETPIPE_SZ(size) => libc::fcntl(fd, libc::F_SETPIPE_SZ, size), - #[cfg(any(target_os = "linux", target_os = "android"))] - _ => unimplemented!() } }; From b56fb4c2cfe52b62410a6a479b70c1da3600e49c Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Tue, 17 Mar 2020 18:55:46 -0300 Subject: [PATCH 03/14] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d31a09aa1c..d144c5b19c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] - ReleaseDate ### Added +- Added support for `F_OFD_*` `fcntl` commands on Linux and Android. + (#[1195](https://github.com/nix-rust/nix/pull/1195)) - Added `env::clearenv()`: calls `libc::clearenv` on platforms where it's available, and clears the environment of all variables via `std::env::vars` and `std::env::remove_var` on others. From 2ae45b8792371ba173877b3a4fd8925cc6e8ac72 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Wed, 18 Mar 2020 10:14:09 -0300 Subject: [PATCH 04/14] Add fcntl tests for OFD locks --- test/test_fcntl.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 38a1e7b95a..56782e0d28 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -65,13 +65,15 @@ fn test_readlink() { #[cfg(any(target_os = "linux", target_os = "android"))] mod linux_android { + use std::fs::File; use std::io::prelude::*; - use std::io::SeekFrom; + use std::io::{BufRead, BufReader, SeekFrom}; use std::os::unix::prelude::*; use libc::loff_t; use nix::fcntl::*; + use nix::sys::stat::fstat; use nix::sys::uio::IoVec; use nix::unistd::{close, pipe, read, write}; @@ -197,6 +199,54 @@ mod linux_android { let mut buf = [0u8; 200]; assert_eq!(100, read(fd, &mut buf).unwrap()); } + + #[test] + fn test_ofd_locks() { + let tmp = NamedTempFile::new().unwrap(); + + let fd = tmp.as_raw_fd(); + let inode = fstat(fd).unwrap().st_ino; + + let mut flock = libc::flock { + l_type: libc::F_WRLCK as libc::c_short, + l_whence: libc::SEEK_SET as libc::c_short, + l_start: 0, + l_len: 0, + l_pid: 0, + }; + fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); + assert_eq!(Some(("OFDLCK".to_string(), "WRITE".to_string())), lock_info(inode)); + + flock.l_type = libc::F_UNLCK as libc::c_short; + fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); + assert_eq!(None, lock_info(inode)); + + flock.l_type = libc::F_RDLCK as libc::c_short; + fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); + assert_eq!(Some(("OFDLCK".to_string(), "READ".to_string())), lock_info(inode)); + + flock.l_type = libc::F_UNLCK as libc::c_short; + fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); + assert_eq!(None, lock_info(inode)); + } + + fn lock_info(inode: u64) -> Option<(String, String)> { + let file = File::open("/proc/locks").unwrap(); + let buf = BufReader::new(file); + + for line in buf.lines() { + let line = line.unwrap(); + let parts: Vec<_> = line.split_whitespace().collect(); + let lock_type = parts[1]; + let lock_access = parts[3]; + let ino_parts: Vec<_> = parts[5].split(':').collect(); + let ino: u64 = ino_parts[2].parse().unwrap(); + if ino == inode { + return Some((lock_type.to_string(), lock_access.to_string())) + } + } + None + } } #[cfg(any(target_os = "linux", From 82521425eece08005e371ba1e7f0e8c7ba3e554f Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Wed, 18 Mar 2020 11:08:41 -0300 Subject: [PATCH 05/14] Try to get tests working on 32 bits --- test/test_fcntl.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 56782e0d28..3f69efe8a7 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -205,7 +205,7 @@ mod linux_android { let tmp = NamedTempFile::new().unwrap(); let fd = tmp.as_raw_fd(); - let inode = fstat(fd).unwrap().st_ino; + let inode = fstat(fd).unwrap().st_ino as usize; let mut flock = libc::flock { l_type: libc::F_WRLCK as libc::c_short, @@ -230,7 +230,7 @@ mod linux_android { assert_eq!(None, lock_info(inode)); } - fn lock_info(inode: u64) -> Option<(String, String)> { + fn lock_info(inode: usize) -> Option<(String, String)> { let file = File::open("/proc/locks").unwrap(); let buf = BufReader::new(file); @@ -240,7 +240,7 @@ mod linux_android { let lock_type = parts[1]; let lock_access = parts[3]; let ino_parts: Vec<_> = parts[5].split(':').collect(); - let ino: u64 = ino_parts[2].parse().unwrap(); + let ino: usize = ino_parts[2].parse().unwrap(); if ino == inode { return Some((lock_type.to_string(), lock_access.to_string())) } From ef01f462545ed814fa01028ed2ef206ba4ec2b41 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Wed, 18 Mar 2020 11:56:22 -0300 Subject: [PATCH 06/14] Add missing libc::flock fields in mips --- test/test_fcntl.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 3f69efe8a7..b6f9932080 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -213,6 +213,10 @@ mod linux_android { l_start: 0, l_len: 0, l_pid: 0, + #[cfg(target_arch="mips")] + l_sysid: 0, + #[cfg(target_arch="mips")] + pad: 0, }; fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); assert_eq!(Some(("OFDLCK".to_string(), "WRITE".to_string())), lock_info(inode)); From 6075ce0df79e079bd5cc98dc3f6f699291898bc4 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Wed, 18 Mar 2020 12:39:28 -0300 Subject: [PATCH 07/14] Fix type of pad field --- test/test_fcntl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index b6f9932080..984b346d83 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -216,7 +216,7 @@ mod linux_android { #[cfg(target_arch="mips")] l_sysid: 0, #[cfg(target_arch="mips")] - pad: 0, + pad: [0; 4], }; fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); assert_eq!(Some(("OFDLCK".to_string(), "WRITE".to_string())), lock_info(inode)); From 87f33c34342c0d2cf02fa8ffef3ba00cb401a190 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Wed, 18 Mar 2020 13:51:44 -0300 Subject: [PATCH 08/14] Don't test on mips; replace unwrap for expect --- test/test_fcntl.rs | 133 +++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 60 deletions(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 984b346d83..7ea93f8e17 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -1,12 +1,12 @@ -use nix::Error; use nix::errno::*; -use nix::fcntl::{openat, open, OFlag, readlink, readlinkat, renameat}; +use nix::fcntl::{open, openat, readlink, readlinkat, renameat, OFlag}; use nix::sys::stat::Mode; use nix::unistd::{close, read}; -use tempfile::{self, NamedTempFile}; +use nix::Error; use std::fs::File; use std::io::prelude::*; use std::os::unix::fs; +use tempfile::{self, NamedTempFile}; #[test] fn test_openat() { @@ -14,13 +14,14 @@ fn test_openat() { let mut tmp = NamedTempFile::new().unwrap(); tmp.write_all(CONTENTS).unwrap(); - let dirfd = open(tmp.path().parent().unwrap(), - OFlag::empty(), - Mode::empty()).unwrap(); - let fd = openat(dirfd, - tmp.path().file_name().unwrap(), - OFlag::O_RDONLY, - Mode::empty()).unwrap(); + let dirfd = open(tmp.path().parent().unwrap(), OFlag::empty(), Mode::empty()).unwrap(); + let fd = openat( + dirfd, + tmp.path().file_name().unwrap(), + OFlag::O_RDONLY, + Mode::empty(), + ) + .unwrap(); let mut buf = [0u8; 1024]; assert_eq!(4, read(fd, &mut buf).unwrap()); @@ -39,8 +40,10 @@ fn test_renameat() { let new_dir = tempfile::tempdir().unwrap(); let new_dirfd = open(new_dir.path(), OFlag::empty(), Mode::empty()).unwrap(); renameat(Some(old_dirfd), "old", Some(new_dirfd), "new").unwrap(); - assert_eq!(renameat(Some(old_dirfd), "old", Some(new_dirfd), "new").unwrap_err(), - Error::Sys(Errno::ENOENT)); + assert_eq!( + renameat(Some(old_dirfd), "old", Some(new_dirfd), "new").unwrap_err(), + Error::Sys(Errno::ENOENT) + ); close(old_dirfd).unwrap(); close(new_dirfd).unwrap(); assert!(new_dir.path().join("new").exists()); @@ -53,14 +56,14 @@ fn test_readlink() { let dst = tempdir.path().join("b"); println!("a: {:?}, b: {:?}", &src, &dst); fs::symlink(&src.as_path(), &dst.as_path()).unwrap(); - let dirfd = open(tempdir.path(), - OFlag::empty(), - Mode::empty()).unwrap(); + let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); let expected_dir = src.to_str().unwrap(); assert_eq!(readlink(&dst).unwrap().to_str().unwrap(), expected_dir); - assert_eq!(readlinkat(dirfd, "b").unwrap().to_str().unwrap(), expected_dir); - + assert_eq!( + readlinkat(dirfd, "b").unwrap().to_str().unwrap(), + expected_dir + ); } #[cfg(any(target_os = "linux", target_os = "android"))] @@ -125,8 +128,15 @@ mod linux_android { let (rd, wr) = pipe().unwrap(); let mut offset: loff_t = 5; - let res = splice(tmp.as_raw_fd(), Some(&mut offset), - wr, None, 2, SpliceFFlags::empty()).unwrap(); + let res = splice( + tmp.as_raw_fd(), + Some(&mut offset), + wr, + None, + 2, + SpliceFFlags::empty(), + ) + .unwrap(); assert_eq!(2, res); @@ -201,11 +211,12 @@ mod linux_android { } #[test] + #[cfg(not(target_arch = "mips"))] fn test_ofd_locks() { let tmp = NamedTempFile::new().unwrap(); let fd = tmp.as_raw_fd(); - let inode = fstat(fd).unwrap().st_ino as usize; + let inode = fstat(fd).expect("fstat failed").st_ino as usize; let mut flock = libc::flock { l_type: libc::F_WRLCK as libc::c_short, @@ -213,29 +224,31 @@ mod linux_android { l_start: 0, l_len: 0, l_pid: 0, - #[cfg(target_arch="mips")] - l_sysid: 0, - #[cfg(target_arch="mips")] - pad: [0; 4], }; - fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); - assert_eq!(Some(("OFDLCK".to_string(), "WRITE".to_string())), lock_info(inode)); + fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).expect("write lock failed"); + assert_eq!( + Some(("OFDLCK".to_string(), "WRITE".to_string())), + lock_info(inode) + ); flock.l_type = libc::F_UNLCK as libc::c_short; - fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); + fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).expect("write unlock failed"); assert_eq!(None, lock_info(inode)); flock.l_type = libc::F_RDLCK as libc::c_short; - fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); - assert_eq!(Some(("OFDLCK".to_string(), "READ".to_string())), lock_info(inode)); + fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).expect("read lock failed"); + assert_eq!( + Some(("OFDLCK".to_string(), "READ".to_string())), + lock_info(inode) + ); flock.l_type = libc::F_UNLCK as libc::c_short; - fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).unwrap(); + fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).expect("read unlock failed"); assert_eq!(None, lock_info(inode)); } fn lock_info(inode: usize) -> Option<(String, String)> { - let file = File::open("/proc/locks").unwrap(); + let file = File::open("/proc/locks").expect("open /proc/locks failed"); let buf = BufReader::new(file); for line in buf.lines() { @@ -246,27 +259,29 @@ mod linux_android { let ino_parts: Vec<_> = parts[5].split(':').collect(); let ino: usize = ino_parts[2].parse().unwrap(); if ino == inode { - return Some((lock_type.to_string(), lock_access.to_string())) + return Some((lock_type.to_string(), lock_access.to_string())); } } None } } -#[cfg(any(target_os = "linux", - target_os = "android", - target_os = "emscripten", - target_os = "fuchsia", - any(target_os = "wasi", target_env = "wasi"), - target_env = "uclibc", - target_env = "freebsd"))] +#[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "emscripten", + target_os = "fuchsia", + any(target_os = "wasi", target_env = "wasi"), + target_env = "uclibc", + target_env = "freebsd" +))] mod test_posix_fadvise { - use tempfile::NamedTempFile; - use std::os::unix::io::{RawFd, AsRawFd}; use nix::errno::Errno; use nix::fcntl::*; use nix::unistd::pipe; + use std::os::unix::io::{AsRawFd, RawFd}; + use tempfile::NamedTempFile; #[test] fn test_success() { @@ -280,25 +295,30 @@ mod test_posix_fadvise { #[test] fn test_errno() { let (rd, _wr) = pipe().unwrap(); - let errno = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED) - .unwrap(); + let errno = + posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED).unwrap(); assert_eq!(errno, Errno::ESPIPE as i32); } } -#[cfg(any(target_os = "linux", - target_os = "android", - target_os = "emscripten", - target_os = "fuchsia", - any(target_os = "wasi", target_env = "wasi"), - target_os = "freebsd"))] +#[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "emscripten", + target_os = "fuchsia", + any(target_os = "wasi", target_env = "wasi"), + target_os = "freebsd" +))] mod test_posix_fallocate { - use tempfile::NamedTempFile; - use std::{io::Read, os::unix::io::{RawFd, AsRawFd}}; use nix::errno::Errno; use nix::fcntl::*; use nix::unistd::pipe; + use std::{ + io::Read, + os::unix::io::{AsRawFd, RawFd}, + }; + use tempfile::NamedTempFile; #[test] fn success() { @@ -330,15 +350,8 @@ mod test_posix_fallocate { let err = posix_fallocate(rd as RawFd, 0, 100).unwrap_err(); use nix::Error::Sys; match err { - Sys(Errno::EINVAL) - | Sys(Errno::ENODEV) - | Sys(Errno::ESPIPE) - | Sys(Errno::EBADF) => (), - errno => - panic!( - "unexpected errno {}", - errno, - ), + Sys(Errno::EINVAL) | Sys(Errno::ENODEV) | Sys(Errno::ESPIPE) | Sys(Errno::EBADF) => (), + errno => panic!("unexpected errno {}", errno,), } } } From 2d90033ed418f014c07096c4fc1942be9410fbb8 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Thu, 19 Mar 2020 09:03:21 -0300 Subject: [PATCH 09/14] Exclude targets failing on CI --- test/test_fcntl.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 7ea93f8e17..ab3a7eefba 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -211,7 +211,14 @@ mod linux_android { } #[test] - #[cfg(not(target_arch = "mips"))] + #[cfg(not(any(target_arch = "aarch64", + target_arch = "arm", + target_arch = "armv7", + target_arch = "i686", + target_arch = "mips", + target_arch = "mips64", + target_arch = "mips64el", + target_arch = "powerpc64")))] fn test_ofd_locks() { let tmp = NamedTempFile::new().unwrap(); From 3332c08793750ab1cd94fb9406c154fe5efa53f6 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Thu, 19 Mar 2020 09:30:27 -0300 Subject: [PATCH 10/14] One more arch exclusion --- test/test_fcntl.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index ab3a7eefba..892355277a 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -218,7 +218,8 @@ mod linux_android { target_arch = "mips", target_arch = "mips64", target_arch = "mips64el", - target_arch = "powerpc64")))] + target_arch = "powerpc64", + target_arch = "powerpc64le")))] fn test_ofd_locks() { let tmp = NamedTempFile::new().unwrap(); From 1478315e2caea2b5320d4ff834e19f819fe8a61d Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Fri, 27 Mar 2020 19:54:41 -0300 Subject: [PATCH 11/14] Undo rustfmt reformating --- test/test_fcntl.rs | 104 ++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 57 deletions(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 892355277a..6b78bc703a 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -1,12 +1,12 @@ +use nix::Error; use nix::errno::*; -use nix::fcntl::{open, openat, readlink, readlinkat, renameat, OFlag}; +use nix::fcntl::{openat, open, OFlag, readlink, readlinkat, renameat}; use nix::sys::stat::Mode; use nix::unistd::{close, read}; -use nix::Error; +use tempfile::{self, NamedTempFile}; use std::fs::File; use std::io::prelude::*; use std::os::unix::fs; -use tempfile::{self, NamedTempFile}; #[test] fn test_openat() { @@ -14,14 +14,13 @@ fn test_openat() { let mut tmp = NamedTempFile::new().unwrap(); tmp.write_all(CONTENTS).unwrap(); - let dirfd = open(tmp.path().parent().unwrap(), OFlag::empty(), Mode::empty()).unwrap(); - let fd = openat( - dirfd, - tmp.path().file_name().unwrap(), - OFlag::O_RDONLY, - Mode::empty(), - ) - .unwrap(); + let dirfd = open(tmp.path().parent().unwrap(), + OFlag::empty(), + Mode::empty()).unwrap(); + let fd = openat(dirfd, + tmp.path().file_name().unwrap(), + OFlag::O_RDONLY, + Mode::empty()).unwrap(); let mut buf = [0u8; 1024]; assert_eq!(4, read(fd, &mut buf).unwrap()); @@ -40,10 +39,8 @@ fn test_renameat() { let new_dir = tempfile::tempdir().unwrap(); let new_dirfd = open(new_dir.path(), OFlag::empty(), Mode::empty()).unwrap(); renameat(Some(old_dirfd), "old", Some(new_dirfd), "new").unwrap(); - assert_eq!( - renameat(Some(old_dirfd), "old", Some(new_dirfd), "new").unwrap_err(), - Error::Sys(Errno::ENOENT) - ); + assert_eq!(renameat(Some(old_dirfd), "old", Some(new_dirfd), "new").unwrap_err(), + Error::Sys(Errno::ENOENT)); close(old_dirfd).unwrap(); close(new_dirfd).unwrap(); assert!(new_dir.path().join("new").exists()); @@ -56,14 +53,14 @@ fn test_readlink() { let dst = tempdir.path().join("b"); println!("a: {:?}, b: {:?}", &src, &dst); fs::symlink(&src.as_path(), &dst.as_path()).unwrap(); - let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); + let dirfd = open(tempdir.path(), + OFlag::empty(), + Mode::empty()).unwrap(); let expected_dir = src.to_str().unwrap(); assert_eq!(readlink(&dst).unwrap().to_str().unwrap(), expected_dir); - assert_eq!( - readlinkat(dirfd, "b").unwrap().to_str().unwrap(), - expected_dir - ); + assert_eq!(readlinkat(dirfd, "b").unwrap().to_str().unwrap(), expected_dir); + } #[cfg(any(target_os = "linux", target_os = "android"))] @@ -128,15 +125,8 @@ mod linux_android { let (rd, wr) = pipe().unwrap(); let mut offset: loff_t = 5; - let res = splice( - tmp.as_raw_fd(), - Some(&mut offset), - wr, - None, - 2, - SpliceFFlags::empty(), - ) - .unwrap(); + let res = splice(tmp.as_raw_fd(), Some(&mut offset), + wr, None, 2, SpliceFFlags::empty()).unwrap(); assert_eq!(2, res); @@ -274,22 +264,20 @@ mod linux_android { } } -#[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "emscripten", - target_os = "fuchsia", - any(target_os = "wasi", target_env = "wasi"), - target_env = "uclibc", - target_env = "freebsd" -))] +#[cfg(any(target_os = "linux", + target_os = "android", + target_os = "emscripten", + target_os = "fuchsia", + any(target_os = "wasi", target_env = "wasi"), + target_env = "uclibc", + target_env = "freebsd"))] mod test_posix_fadvise { + use tempfile::NamedTempFile; + use std::os::unix::io::{RawFd, AsRawFd}; use nix::errno::Errno; use nix::fcntl::*; use nix::unistd::pipe; - use std::os::unix::io::{AsRawFd, RawFd}; - use tempfile::NamedTempFile; #[test] fn test_success() { @@ -303,30 +291,25 @@ mod test_posix_fadvise { #[test] fn test_errno() { let (rd, _wr) = pipe().unwrap(); - let errno = - posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED).unwrap(); + let errno = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED) + .unwrap(); assert_eq!(errno, Errno::ESPIPE as i32); } } -#[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "emscripten", - target_os = "fuchsia", - any(target_os = "wasi", target_env = "wasi"), - target_os = "freebsd" -))] +#[cfg(any(target_os = "linux", + target_os = "android", + target_os = "emscripten", + target_os = "fuchsia", + any(target_os = "wasi", target_env = "wasi"), + target_os = "freebsd"))] mod test_posix_fallocate { + use tempfile::NamedTempFile; + use std::{io::Read, os::unix::io::{RawFd, AsRawFd}}; use nix::errno::Errno; use nix::fcntl::*; use nix::unistd::pipe; - use std::{ - io::Read, - os::unix::io::{AsRawFd, RawFd}, - }; - use tempfile::NamedTempFile; #[test] fn success() { @@ -358,8 +341,15 @@ mod test_posix_fallocate { let err = posix_fallocate(rd as RawFd, 0, 100).unwrap_err(); use nix::Error::Sys; match err { - Sys(Errno::EINVAL) | Sys(Errno::ENODEV) | Sys(Errno::ESPIPE) | Sys(Errno::EBADF) => (), - errno => panic!("unexpected errno {}", errno,), + Sys(Errno::EINVAL) + | Sys(Errno::ENODEV) + | Sys(Errno::ESPIPE) + | Sys(Errno::EBADF) => (), + errno => + panic!( + "unexpected errno {}", + errno, + ), } } } From 4c4df3c0b616fe0e6a94f9107e7213a338b62981 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Fri, 27 Mar 2020 20:16:23 -0300 Subject: [PATCH 12/14] Explain why OFD tests are disabled in some archs --- test/test_fcntl.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 6b78bc703a..e988802042 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -200,6 +200,9 @@ mod linux_android { assert_eq!(100, read(fd, &mut buf).unwrap()); } + // This test is disabled for the target architectures below + // due to OFD locks not being available in the kernel/libc + // versions used in the CI environment. #[test] #[cfg(not(any(target_arch = "aarch64", target_arch = "arm", From 5e2d14bef9268729e1eb4fc9fee30cdd3cedea17 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Fri, 27 Mar 2020 20:36:19 -0300 Subject: [PATCH 13/14] i686 -> x86 --- test/test_fcntl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index e988802042..2e0f6478c0 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -207,7 +207,7 @@ mod linux_android { #[cfg(not(any(target_arch = "aarch64", target_arch = "arm", target_arch = "armv7", - target_arch = "i686", + target_arch = "x86", target_arch = "mips", target_arch = "mips64", target_arch = "mips64el", From f7f1d09ae2b73131c18e1eb9a9f8bbce65eafb16 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Wed, 1 Apr 2020 08:10:08 -0300 Subject: [PATCH 14/14] Split read and write OFD lock tests --- test/test_fcntl.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 2e0f6478c0..691236eacf 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -200,9 +200,11 @@ mod linux_android { assert_eq!(100, read(fd, &mut buf).unwrap()); } - // This test is disabled for the target architectures below + // The tests below are disabled for the listed targets // due to OFD locks not being available in the kernel/libc - // versions used in the CI environment. + // versions used in the CI environment, probably because + // they run under QEMU. + #[test] #[cfg(not(any(target_arch = "aarch64", target_arch = "arm", @@ -213,7 +215,7 @@ mod linux_android { target_arch = "mips64el", target_arch = "powerpc64", target_arch = "powerpc64le")))] - fn test_ofd_locks() { + fn test_ofd_write_lock() { let tmp = NamedTempFile::new().unwrap(); let fd = tmp.as_raw_fd(); @@ -235,8 +237,31 @@ mod linux_android { flock.l_type = libc::F_UNLCK as libc::c_short; fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).expect("write unlock failed"); assert_eq!(None, lock_info(inode)); + } - flock.l_type = libc::F_RDLCK as libc::c_short; + #[test] + #[cfg(not(any(target_arch = "aarch64", + target_arch = "arm", + target_arch = "armv7", + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", + target_arch = "mips64el", + target_arch = "powerpc64", + target_arch = "powerpc64le")))] + fn test_ofd_read_lock() { + let tmp = NamedTempFile::new().unwrap(); + + let fd = tmp.as_raw_fd(); + let inode = fstat(fd).expect("fstat failed").st_ino as usize; + + let mut flock = libc::flock { + l_type: libc::F_RDLCK as libc::c_short, + l_whence: libc::SEEK_SET as libc::c_short, + l_start: 0, + l_len: 0, + l_pid: 0, + }; fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).expect("read lock failed"); assert_eq!( Some(("OFDLCK".to_string(), "READ".to_string())),