Skip to content

Commit c25f551

Browse files
author
Bryant Mairs
committed
Replace custom ucred struct with newtype wrapper around libc::ucred
1 parent fa792ff commit c25f551

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2222
([#739](https://github.com/nix-rust/nix/pull/739))
2323
- Expose `signalfd` module on Android as well.
2424
([#739](https://github.com/nix-rust/nix/pull/739))
25-
- Added nix::sys::ptrace::detach.
25+
- Added nix::sys::ptrace::detach.
2626
([#749](https://github.com/nix-rust/nix/pull/749))
2727
- Added timestamp socket control message variant:
2828
`nix::sys::socket::ControlMessage::ScmTimestamp`
@@ -75,11 +75,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
7575
([#731](https://github.com/nix-rust/nix/pull/731))
7676
- Marked `pty::ptsname` function as `unsafe`
7777
([#744](https://github.com/nix-rust/nix/pull/744))
78-
- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly.
78+
- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly.
7979
([#749](https://github.com/nix-rust/nix/pull/749))
8080
- `AioCb::Drop` will now panic if the `AioCb` is still in-progress ([#715](https://github.com/nix-rust/nix/pull/715))
8181
- Restricted `nix::sys::socket::UnixAddr::new_abstract` to Linux and Android only.
8282
([#785](https://github.com/nix-rust/nix/pull/785))
83+
- The `ucred` struct has been removed in favor of a `UserCredentials` struct that
84+
contains only getters for its fields.
85+
([#814](https://github.com/nix-rust/nix/pull/814))
8386

8487

8588
### Fixed

src/sys/socket/mod.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use {Error, Result};
55
use errno::Errno;
66
use features;
7-
use libc::{self, c_void, c_int, socklen_t, size_t, pid_t, uid_t, gid_t};
7+
use libc::{self, c_void, c_int, socklen_t, size_t};
88
use std::{mem, ptr, slice};
99
use std::os::unix::io::RawFd;
1010
use sys::time::TimeVal;
@@ -174,6 +174,53 @@ libc_bitflags!{
174174
}
175175
}
176176

177+
cfg_if! {
178+
if #[cfg(all(target_os = "linux", not(target_arch = "arm")))] {
179+
use std::fmt;
180+
181+
/// Unix credentials of the sending process.
182+
///
183+
/// This struct is used with the `SO_PEERCRED` ancillary message for UNIX sockets.
184+
#[repr(C)]
185+
#[derive(Clone, Copy)]
186+
pub struct UnixCredentials(libc::ucred);
187+
188+
impl UnixCredentials {
189+
/// Returns the process identifier
190+
pub fn pid(&self) -> libc::pid_t {
191+
self.0.pid
192+
}
193+
194+
/// Returns the user identifier
195+
pub fn uid(&self) -> libc::uid_t {
196+
self.0.uid
197+
}
198+
199+
/// Returns the group identifier
200+
pub fn gid(&self) -> libc::gid_t {
201+
self.0.gid
202+
}
203+
}
204+
205+
impl PartialEq for UnixCredentials {
206+
fn eq(&self, other: &Self) -> bool {
207+
self.0.pid == other.0.pid && self.0.uid == other.0.uid && self.0.gid == other.0.gid
208+
}
209+
}
210+
impl Eq for UnixCredentials {}
211+
212+
impl fmt::Debug for UnixCredentials {
213+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
214+
f.debug_struct("UnixCredentials")
215+
.field("pid", &self.0.pid)
216+
.field("uid", &self.0.uid)
217+
.field("gid", &self.0.gid)
218+
.finish()
219+
}
220+
}
221+
}
222+
}
223+
177224
/// Copy the in-memory representation of src into the byte slice dst,
178225
/// updating the slice to point to the remainder of dst only. Unsafe
179226
/// because it exposes all bytes in src, which may be UB if some of them
@@ -799,14 +846,6 @@ pub fn send(fd: RawFd, buf: &[u8], flags: MsgFlags) -> Result<usize> {
799846
Errno::result(ret).map(|r| r as usize)
800847
}
801848

802-
#[repr(C)]
803-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
804-
pub struct ucred {
805-
pid: pid_t,
806-
uid: uid_t,
807-
gid: gid_t,
808-
}
809-
810849
/*
811850
*
812851
* ===== Socket Options =====

src/sys/socket/sockopt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ sockopt_impl!(Both, OobInline, libc::SOL_SOCKET, libc::SO_OOBINLINE, bool);
152152
sockopt_impl!(GetOnly, SocketError, libc::SOL_SOCKET, libc::SO_ERROR, i32);
153153
sockopt_impl!(Both, KeepAlive, libc::SOL_SOCKET, libc::SO_KEEPALIVE, bool);
154154
#[cfg(all(target_os = "linux", not(target_arch="arm")))]
155-
sockopt_impl!(GetOnly, PeerCredentials, libc::SOL_SOCKET, libc::SO_PEERCRED, super::ucred);
155+
sockopt_impl!(GetOnly, PeerCredentials, libc::SOL_SOCKET, libc::SO_PEERCRED, super::UnixCredentials);
156156
#[cfg(any(target_os = "macos",
157157
target_os = "ios"))]
158158
sockopt_impl!(Both, TcpKeepAlive, libc::IPPROTO_TCP, libc::TCP_KEEPALIVE, u32);
@@ -384,7 +384,7 @@ mod test {
384384
let a_cred = getsockopt(a, super::PeerCredentials).unwrap();
385385
let b_cred = getsockopt(b, super::PeerCredentials).unwrap();
386386
assert_eq!(a_cred, b_cred);
387-
assert!(a_cred.pid != 0);
387+
assert!(a_cred.pid() != 0);
388388
}
389389

390390
#[test]

0 commit comments

Comments
 (0)