Skip to content

Commit ed3ed6b

Browse files
authored
fcntl update following up with F_GETPATH but with FreeBSD's F_KINFO flag support instead (#2152)
1 parent 6906a61 commit ed3ed6b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
6666
MacOS, FreeBSD, DragonflyBSD, Android, iOS and Haiku.
6767
([#2074](https://github.com/nix-rust/nix/pull/2074))
6868

69+
- Added `F_KINFO` FcntlFlags entry on FreeBSD for `::nix::fcntl`.
70+
([#2152](https://github.com/nix-rust/nix/pull/2152))
71+
6972
## [0.27.1] - 2023-08-28
7073

7174
### Fixed

src/fcntl.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use crate::errno::Errno;
2+
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
3+
use core::slice;
24
use libc::{self, c_int, c_uint, size_t, ssize_t};
35
#[cfg(any(
46
target_os = "netbsd",
57
target_os = "macos",
68
target_os = "ios",
79
target_os = "dragonfly",
10+
all(target_os = "freebsd", target_arch = "x86_64"),
811
))]
912
use std::ffi::CStr;
1013
use std::ffi::OsString;
@@ -18,6 +21,7 @@ use std::os::unix::io::RawFd;
1821
target_os = "macos",
1922
target_os = "ios",
2023
target_os = "dragonfly",
24+
all(target_os = "freebsd", target_arch = "x86_64"),
2125
))]
2226
use std::path::PathBuf;
2327
#[cfg(any(
@@ -505,6 +509,8 @@ pub enum FcntlArg<'a> {
505509
F_SETPIPE_SZ(c_int),
506510
#[cfg(any(target_os = "netbsd", target_os = "dragonfly", target_os = "macos", target_os = "ios"))]
507511
F_GETPATH(&'a mut PathBuf),
512+
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
513+
F_KINFO(&'a mut PathBuf),
508514
// TODO: Rest of flags
509515
}
510516

@@ -574,6 +580,18 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
574580
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
575581
return Ok(ok_res)
576582
},
583+
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
584+
F_KINFO(path) => {
585+
let mut info: libc::kinfo_file = std::mem::zeroed();
586+
info.kf_structsize = std::mem::size_of::<libc::kinfo_file>() as i32;
587+
let res = libc::fcntl(fd, libc::F_KINFO, &mut info);
588+
let ok_res = Errno::result(res)?;
589+
let p = info.kf_path;
590+
let u8_slice = slice::from_raw_parts(p.as_ptr().cast(), p.len());
591+
let optr = CStr::from_bytes_until_nul(u8_slice).unwrap();
592+
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
593+
return Ok(ok_res)
594+
},
577595
}
578596
};
579597

test/test_fcntl.rs

+19
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,22 @@ fn test_f_get_path() {
584584
tmp.path().canonicalize().unwrap()
585585
);
586586
}
587+
588+
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
589+
#[test]
590+
fn test_f_kinfo() {
591+
use nix::fcntl::*;
592+
use std::{os::unix::io::AsRawFd, path::PathBuf};
593+
594+
let tmp = NamedTempFile::new().unwrap();
595+
// With TMPDIR set with UFS, the vnode name is not entered
596+
// into the name cache thus path is always empty.
597+
// Therefore, we reopen the tempfile a second time for the test
598+
// to pass.
599+
let tmp2 = File::open(tmp.path()).unwrap();
600+
let fd = tmp2.as_raw_fd();
601+
let mut path = PathBuf::new();
602+
let res = fcntl(fd, FcntlArg::F_KINFO(&mut path)).expect("get path failed");
603+
assert_ne!(res, -1);
604+
assert_eq!(path, tmp.path());
605+
}

0 commit comments

Comments
 (0)