Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unistd: make getpeereid take AsFd rather than a raw FD #2137

Merged
merged 5 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased] - ReleaseDate

### Changed

- `unistd::getpeereid` now takes an implementation of `AsFd` rather than a
`RawFd` ([#2137](https://github.com/nix-rust/nix/pull/2137))

## [0.27.1] - 2023-08-28

### Fixed
Expand Down
12 changes: 10 additions & 2 deletions src/sys/socket/sockopt.rs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: Unrelated formatting changes in this file (made automatically). Happy to revert if you'd prefer to leave them out.

Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,11 @@ cfg_if! {
TcpMaxSeg, GetOnly, libc::IPPROTO_TCP, libc::TCP_MAXSEG, u32);
}
}
#[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "redox")))]
#[cfg(not(any(
target_os = "openbsd",
target_os = "haiku",
target_os = "redox"
)))]
#[cfg(feature = "net")]
sockopt_impl!(
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
Expand All @@ -572,7 +576,11 @@ sockopt_impl!(
libc::TCP_REPAIR,
u32
);
#[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "redox")))]
#[cfg(not(any(
target_os = "openbsd",
target_os = "haiku",
target_os = "redox"
)))]
#[cfg(feature = "net")]
sockopt_impl!(
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
Expand Down
4 changes: 2 additions & 2 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3943,11 +3943,11 @@ feature! {
target_os = "netbsd",
target_os = "dragonfly",
))]
pub fn getpeereid(fd: RawFd) -> Result<(Uid, Gid)> {
pub fn getpeereid<F: AsFd>(fd: &F) -> Result<(Uid, Gid)> {
woodruffw marked this conversation as resolved.
Show resolved Hide resolved
let mut uid = 1;
let mut gid = 1;

let ret = unsafe { libc::getpeereid(fd, &mut uid, &mut gid) };
let ret = unsafe { libc::getpeereid(fd.as_fd().as_raw_fd(), &mut uid, &mut gid) };

Errno::result(ret).map(|_| (Uid(uid), Gid(gid)))
}
Expand Down
18 changes: 2 additions & 16 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,8 +1269,8 @@ fn test_getpeereid() {
use std::os::unix::net::UnixStream;
let (sock_a, sock_b) = UnixStream::pair().unwrap();

let (uid_a, gid_a) = getpeereid(sock_a.as_raw_fd()).unwrap();
let (uid_b, gid_b) = getpeereid(sock_b.as_raw_fd()).unwrap();
let (uid_a, gid_a) = getpeereid(&sock_a).unwrap();
let (uid_b, gid_b) = getpeereid(&sock_b).unwrap();

let uid = geteuid();
let gid = getegid();
Expand All @@ -1281,20 +1281,6 @@ fn test_getpeereid() {
assert_eq!(gid_a, gid_b);
}

#[test]
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly",
))]
fn test_getpeereid_invalid_fd() {
// getpeereid is not POSIX, so error codes are inconsistent between different Unices.
getpeereid(-1).expect_err("assertion failed");
}

#[test]
#[cfg(not(target_os = "redox"))]
fn test_faccessat_none_not_existing() {
Expand Down