Skip to content

Commit

Permalink
Merge branch 'master' into xxxat
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC committed Oct 6, 2023
2 parents cc831fb + e7c877a commit e90391e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 11 deletions.
37 changes: 26 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,37 @@ This project adheres to [Semantic Versioning](https://semver.org/).

- Fixed the function signature of `recvmmsg`, potentially causing UB
([#2119](https://github.com/nix-rust/nix/issues/2119))
### Added

- Added `impl From<Signal> for SigSet`.
([#1959](https://github.com/nix-rust/nix/pull/1959))

- Added `impl std::ops::BitOr for SigSet`.
([#1959](https://github.com/nix-rust/nix/pull/1959))

- Added `impl std::ops::BitOr for Signal`.
([#1959](https://github.com/nix-rust/nix/pull/1959))

- Added `impl std::ops::BitOr<Signal> for SigSet`
([#1959](https://github.com/nix-rust/nix/pull/1959))

- Fix `SignalFd::set_mask`. In 0.27.0 it would actually close the file
descriptor.
([#2141](https://github.com/nix-rust/nix/pull/2141))

- Added `Icmp` and `IcmpV6` to `SockProtocol`.
(#[2103](https://github.com/nix-rust/nix/pull/2103))

- Added `F_GETPATH` FcntlFlags entry on Apple/NetBSD/DragonflyBSD for `::nix::fcntl`.
([#2142](https://github.com/nix-rust/nix/pull/2142))

- Added `Ipv6HopLimit` to `::nix::sys::socket::ControlMessage` for Linux,
MacOS, FreeBSD, DragonflyBSD, Android, iOS and Haiku.
([#2074](https://github.com/nix-rust/nix/pull/2074))

- Added `F_KINFO` FcntlFlags entry on FreeBSD for `::nix::fcntl`.
([#2152](https://github.com/nix-rust/nix/pull/2152))

### Changed

- The MSRV is now 1.69
Expand Down Expand Up @@ -55,17 +81,6 @@ This project adheres to [Semantic Versioning](https://semver.org/).

([#2157](https://github.com/nix-rust/nix/pull/2157))

### Added
- Added `Icmp` and `IcmpV6` to `SockProtocol`.
(#[2103](https://github.com/nix-rust/nix/pull/2103))

- Added `F_GETPATH` FcntlFlags entry on Apple/NetBSD/DragonflyBSD for `::nix::fcntl`.
([#2142](https://github.com/nix-rust/nix/pull/2142))

- Added `Ipv6HopLimit` to `::nix::sys::socket::ControlMessage` for Linux,
MacOS, FreeBSD, DragonflyBSD, Android, iOS and Haiku.
([#2074](https://github.com/nix-rust/nix/pull/2074))

## [0.27.1] - 2023-08-28

### Fixed
Expand Down
18 changes: 18 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::errno::Errno;
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
use core::slice;
use libc::{self, c_int, c_uint, size_t, ssize_t};
#[cfg(any(
target_os = "netbsd",
target_os = "macos",
target_os = "ios",
target_os = "dragonfly",
all(target_os = "freebsd", target_arch = "x86_64"),
))]
use std::ffi::CStr;
use std::ffi::OsString;
Expand All @@ -18,6 +21,7 @@ use std::os::unix::io::RawFd;
target_os = "macos",
target_os = "ios",
target_os = "dragonfly",
all(target_os = "freebsd", target_arch = "x86_64"),
))]
use std::path::PathBuf;
#[cfg(any(
Expand Down Expand Up @@ -506,6 +510,8 @@ pub enum FcntlArg<'a> {
F_SETPIPE_SZ(c_int),
#[cfg(any(target_os = "netbsd", target_os = "dragonfly", target_os = "macos", target_os = "ios"))]
F_GETPATH(&'a mut PathBuf),
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
F_KINFO(&'a mut PathBuf),
// TODO: Rest of flags
}

Expand Down Expand Up @@ -575,6 +581,18 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
return Ok(ok_res)
},
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
F_KINFO(path) => {
let mut info: libc::kinfo_file = std::mem::zeroed();
info.kf_structsize = std::mem::size_of::<libc::kinfo_file>() as i32;
let res = libc::fcntl(fd, libc::F_KINFO, &mut info);
let ok_res = Errno::result(res)?;
let p = info.kf_path;
let u8_slice = slice::from_raw_parts(p.as_ptr().cast(), p.len());
let optr = CStr::from_bytes_until_nul(u8_slice).unwrap();
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
return Ok(ok_res)
},
}
};

Expand Down
37 changes: 37 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cfg_if::cfg_if;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::mem;
use std::ops::BitOr;
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
use std::os::unix::io::RawFd;
use std::ptr;
Expand Down Expand Up @@ -604,6 +605,42 @@ impl SigSet {
}
}

impl From<Signal> for SigSet {
fn from(signal: Signal) -> SigSet {
let mut sigset = SigSet::empty();
sigset.add(signal);
sigset
}
}

impl BitOr for Signal {
type Output = SigSet;

fn bitor(self, rhs: Self) -> Self::Output {
let mut sigset = SigSet::empty();
sigset.add(self);
sigset.add(rhs);
sigset
}
}

impl BitOr<Signal> for SigSet {
type Output = SigSet;

fn bitor(mut self, rhs: Signal) -> Self::Output {
self.add(rhs);
self
}
}

impl BitOr for SigSet {
type Output = Self;

fn bitor(self, rhs: Self) -> Self::Output {
self.iter().chain(rhs.iter()).collect()
}
}

impl AsRef<libc::sigset_t> for SigSet {
fn as_ref(&self) -> &libc::sigset_t {
&self.sigset
Expand Down
19 changes: 19 additions & 0 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,22 @@ fn test_f_get_path() {
tmp.path().canonicalize().unwrap()
);
}

#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
#[test]
fn test_f_kinfo() {
use nix::fcntl::*;
use std::{os::unix::io::AsRawFd, path::PathBuf};

let tmp = NamedTempFile::new().unwrap();
// With TMPDIR set with UFS, the vnode name is not entered
// into the name cache thus path is always empty.
// Therefore, we reopen the tempfile a second time for the test
// to pass.
let tmp2 = File::open(tmp.path()).unwrap();
let fd = tmp2.as_raw_fd();
let mut path = PathBuf::new();
let res = fcntl(fd, FcntlArg::F_KINFO(&mut path)).expect("get path failed");
assert_ne!(res, -1);
assert_eq!(path, tmp.path());
}

0 comments on commit e90391e

Please sign in to comment.