Skip to content

Commit

Permalink
Replace custom ucred struct with newtype wrapper around libc::ucred
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryant Mairs committed Dec 9, 2017
1 parent fa792ff commit c25f551
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#739](https://github.com/nix-rust/nix/pull/739))
- Expose `signalfd` module on Android as well.
([#739](https://github.com/nix-rust/nix/pull/739))
- Added nix::sys::ptrace::detach.
- Added nix::sys::ptrace::detach.
([#749](https://github.com/nix-rust/nix/pull/749))
- Added timestamp socket control message variant:
`nix::sys::socket::ControlMessage::ScmTimestamp`
Expand Down Expand Up @@ -75,11 +75,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#731](https://github.com/nix-rust/nix/pull/731))
- Marked `pty::ptsname` function as `unsafe`
([#744](https://github.com/nix-rust/nix/pull/744))
- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly.
- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly.
([#749](https://github.com/nix-rust/nix/pull/749))
- `AioCb::Drop` will now panic if the `AioCb` is still in-progress ([#715](https://github.com/nix-rust/nix/pull/715))
- Restricted `nix::sys::socket::UnixAddr::new_abstract` to Linux and Android only.
([#785](https://github.com/nix-rust/nix/pull/785))
- The `ucred` struct has been removed in favor of a `UserCredentials` struct that
contains only getters for its fields.
([#814](https://github.com/nix-rust/nix/pull/814))


### Fixed
Expand Down
57 changes: 48 additions & 9 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use {Error, Result};
use errno::Errno;
use features;
use libc::{self, c_void, c_int, socklen_t, size_t, pid_t, uid_t, gid_t};
use libc::{self, c_void, c_int, socklen_t, size_t};
use std::{mem, ptr, slice};
use std::os::unix::io::RawFd;
use sys::time::TimeVal;
Expand Down Expand Up @@ -174,6 +174,53 @@ libc_bitflags!{
}
}

cfg_if! {
if #[cfg(all(target_os = "linux", not(target_arch = "arm")))] {
use std::fmt;

/// Unix credentials of the sending process.
///
/// This struct is used with the `SO_PEERCRED` ancillary message for UNIX sockets.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct UnixCredentials(libc::ucred);

impl UnixCredentials {
/// Returns the process identifier
pub fn pid(&self) -> libc::pid_t {
self.0.pid
}

/// Returns the user identifier
pub fn uid(&self) -> libc::uid_t {
self.0.uid
}

/// Returns the group identifier
pub fn gid(&self) -> libc::gid_t {
self.0.gid
}
}

impl PartialEq for UnixCredentials {
fn eq(&self, other: &Self) -> bool {
self.0.pid == other.0.pid && self.0.uid == other.0.uid && self.0.gid == other.0.gid
}
}
impl Eq for UnixCredentials {}

impl fmt::Debug for UnixCredentials {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("UnixCredentials")
.field("pid", &self.0.pid)
.field("uid", &self.0.uid)
.field("gid", &self.0.gid)
.finish()
}
}
}
}

/// Copy the in-memory representation of src into the byte slice dst,
/// updating the slice to point to the remainder of dst only. Unsafe
/// because it exposes all bytes in src, which may be UB if some of them
Expand Down Expand Up @@ -799,14 +846,6 @@ pub fn send(fd: RawFd, buf: &[u8], flags: MsgFlags) -> Result<usize> {
Errno::result(ret).map(|r| r as usize)
}

#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ucred {
pid: pid_t,
uid: uid_t,
gid: gid_t,
}

/*
*
* ===== Socket Options =====
Expand Down
4 changes: 2 additions & 2 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ sockopt_impl!(Both, OobInline, libc::SOL_SOCKET, libc::SO_OOBINLINE, bool);
sockopt_impl!(GetOnly, SocketError, libc::SOL_SOCKET, libc::SO_ERROR, i32);
sockopt_impl!(Both, KeepAlive, libc::SOL_SOCKET, libc::SO_KEEPALIVE, bool);
#[cfg(all(target_os = "linux", not(target_arch="arm")))]
sockopt_impl!(GetOnly, PeerCredentials, libc::SOL_SOCKET, libc::SO_PEERCRED, super::ucred);
sockopt_impl!(GetOnly, PeerCredentials, libc::SOL_SOCKET, libc::SO_PEERCRED, super::UnixCredentials);
#[cfg(any(target_os = "macos",
target_os = "ios"))]
sockopt_impl!(Both, TcpKeepAlive, libc::IPPROTO_TCP, libc::TCP_KEEPALIVE, u32);
Expand Down Expand Up @@ -384,7 +384,7 @@ mod test {
let a_cred = getsockopt(a, super::PeerCredentials).unwrap();
let b_cred = getsockopt(b, super::PeerCredentials).unwrap();
assert_eq!(a_cred, b_cred);
assert!(a_cred.pid != 0);
assert!(a_cred.pid() != 0);
}

#[test]
Expand Down

0 comments on commit c25f551

Please sign in to comment.