|
4 | 4 | use {Error, Result};
|
5 | 5 | use errno::Errno;
|
6 | 6 | 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}; |
8 | 8 | use std::{mem, ptr, slice};
|
9 | 9 | use std::os::unix::io::RawFd;
|
10 | 10 | use sys::time::TimeVal;
|
@@ -174,6 +174,53 @@ libc_bitflags!{
|
174 | 174 | }
|
175 | 175 | }
|
176 | 176 |
|
| 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 | + |
177 | 224 | /// Copy the in-memory representation of src into the byte slice dst,
|
178 | 225 | /// updating the slice to point to the remainder of dst only. Unsafe
|
179 | 226 | /// 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> {
|
799 | 846 | Errno::result(ret).map(|r| r as usize)
|
800 | 847 | }
|
801 | 848 |
|
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 |
| - |
810 | 849 | /*
|
811 | 850 | *
|
812 | 851 | * ===== Socket Options =====
|
|
0 commit comments