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

add pid to tokio::net::unix::UCred #2633

Merged
merged 1 commit into from Oct 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 68 additions & 7 deletions tokio/src/net/unix/ucred.rs
@@ -1,8 +1,10 @@
use libc::{gid_t, uid_t};
use libc::{gid_t, pid_t, uid_t};

/// Credentials of a process
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct UCred {
/// PID (process ID) of the process
pid: Option<pid_t>,
/// UID (user ID) of the process
uid: uid_t,
/// GID (group ID) of the process
Expand All @@ -19,19 +21,27 @@ impl UCred {
pub fn gid(&self) -> gid_t {
self.gid
}

/// Gets PID (process ID) of the process.
///
/// This is only implemented under linux, android, IOS and MacOS
pub fn pid(&self) -> Option<pid_t> {
self.pid
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub(crate) use self::impl_linux::get_peer_cred;

#[cfg(any(
target_os = "dragonfly",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
pub(crate) use self::impl_bsd::get_peer_cred;

#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) use self::impl_macos::get_peer_cred;

#[cfg(any(target_os = "solaris", target_os = "illumos"))]
Expand Down Expand Up @@ -77,6 +87,7 @@ pub(crate) mod impl_linux {
Ok(super::UCred {
uid: ucred.uid,
gid: ucred.gid,
pid: Some(ucred.pid),
})
} else {
Err(io::Error::last_os_error())
Expand All @@ -87,13 +98,11 @@ pub(crate) mod impl_linux {

#[cfg(any(
target_os = "dragonfly",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
pub(crate) mod impl_macos {
pub(crate) mod impl_bsd {
use crate::net::unix::UnixStream;

use libc::getpeereid;
Expand All @@ -114,6 +123,54 @@ pub(crate) mod impl_macos {
Ok(super::UCred {
uid: uid.assume_init(),
gid: gid.assume_init(),
pid: None,
})
} else {
Err(io::Error::last_os_error())
}
}
}
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) mod impl_macos {
use crate::net::unix::UnixStream;

use libc::{c_void, getpeereid, getsockopt, pid_t, LOCAL_PEEREPID, SOL_LOCAL};
use std::io;
use std::mem::size_of;
use std::mem::MaybeUninit;
use std::os::unix::io::AsRawFd;

pub(crate) fn get_peer_cred(sock: &UnixStream) -> io::Result<super::UCred> {
unsafe {
let raw_fd = sock.as_raw_fd();

let mut uid = MaybeUninit::uninit();
let mut gid = MaybeUninit::uninit();
let mut pid: MaybeUninit<pid_t> = MaybeUninit::uninit();
let mut pid_size: MaybeUninit<u32> = MaybeUninit::new(size_of::<pid_t>() as u32);

if getsockopt(
raw_fd,
SOL_LOCAL,
LOCAL_PEEREPID,
pid.as_mut_ptr() as *mut c_void,
pid_size.as_mut_ptr(),
) != 0
{
return Err(io::Error::last_os_error());
}

assert!(pid_size.assume_init() == (size_of::<pid_t>() as u32));

let ret = getpeereid(raw_fd, uid.as_mut_ptr(), gid.as_mut_ptr());

if ret == 0 {
Ok(super::UCred {
uid: uid.assume_init(),
gid: gid.assume_init(),
pid: Some(pid.assume_init()),
})
} else {
Err(io::Error::last_os_error())
Expand Down Expand Up @@ -154,7 +211,11 @@ pub(crate) mod impl_solaris {

ucred_free(cred);

Ok(super::UCred { uid, gid })
Ok(super::UCred {
uid,
gid,
pid: None,
})
} else {
Err(io::Error::last_os_error())
}
Expand Down