mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
net: report PID in UCred for Solaris and Illumos. (#3085)
This commit is contained in:
parent
f51ddc5958
commit
90c2a510e2
@ -24,7 +24,8 @@ impl UCred {
|
|||||||
|
|
||||||
/// Gets PID (process ID) of the process.
|
/// Gets PID (process ID) of the process.
|
||||||
///
|
///
|
||||||
/// This is only implemented under linux, android, IOS and MacOS
|
/// This is only implemented under Linux, Android, iOS, macOS, Solaris and
|
||||||
|
/// Illumos. On other plaforms this will always return `None`.
|
||||||
pub fn pid(&self) -> Option<pid_t> {
|
pub fn pid(&self) -> Option<pid_t> {
|
||||||
self.pid
|
self.pid
|
||||||
}
|
}
|
||||||
@ -186,35 +187,24 @@ pub(crate) mod impl_solaris {
|
|||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
enum ucred_t {}
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
fn ucred_free(cred: *mut ucred_t);
|
|
||||||
fn ucred_geteuid(cred: *const ucred_t) -> super::uid_t;
|
|
||||||
fn ucred_getegid(cred: *const ucred_t) -> super::gid_t;
|
|
||||||
|
|
||||||
fn getpeerucred(fd: std::os::raw::c_int, cred: *mut *mut ucred_t) -> std::os::raw::c_int;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_peer_cred(sock: &UnixStream) -> io::Result<super::UCred> {
|
pub(crate) fn get_peer_cred(sock: &UnixStream) -> io::Result<super::UCred> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let raw_fd = sock.as_raw_fd();
|
let raw_fd = sock.as_raw_fd();
|
||||||
|
|
||||||
let mut cred = ptr::null_mut::<*mut ucred_t>() as *mut ucred_t;
|
let mut cred = ptr::null_mut();
|
||||||
|
let ret = libc::getpeerucred(raw_fd, &mut cred);
|
||||||
let ret = getpeerucred(raw_fd, &mut cred);
|
|
||||||
|
|
||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
let uid = ucred_geteuid(cred);
|
let uid = libc::ucred_geteuid(cred);
|
||||||
let gid = ucred_getegid(cred);
|
let gid = libc::ucred_getegid(cred);
|
||||||
|
let pid = libc::ucred_getpid(cred);
|
||||||
|
|
||||||
ucred_free(cred);
|
libc::ucred_free(cred);
|
||||||
|
|
||||||
Ok(super::UCred {
|
Ok(super::UCred {
|
||||||
uid,
|
uid,
|
||||||
gid,
|
gid,
|
||||||
pid: None,
|
pid: Some(pid),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(io::Error::last_os_error())
|
Err(io::Error::last_os_error())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user