Skip to content

Commit

Permalink
fcntl update following up with F_GETPATH but with FreeBSD's F_KINFO f…
Browse files Browse the repository at this point in the history
…lag support instead (#2152)
  • Loading branch information
devnexen committed Oct 6, 2023
1 parent 6906a61 commit ed3ed6b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
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))

## [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 @@ -505,6 +509,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 @@ -574,6 +580,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
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 ed3ed6b

Please sign in to comment.