@@ -251,14 +251,12 @@ impl DomainSocketListenerBuilder {
251251
252252impl GetMetadata for UnixStream {
253253 fn metadata ( & self ) -> Option < ConnectionMetadata > {
254- let ucred = self
255- . peer_cred ( )
254+ let ucred = peer_credentials:: peer_cred ( self )
256255 . map_err ( |err| {
257256 format_error ! (
258257 "Failed to grab peer credentials metadata from UnixStream" ,
259258 err
260- ) ;
261- err
259+ )
262260 } )
263261 . ok ( ) ?;
264262
@@ -268,3 +266,122 @@ impl GetMetadata for UnixStream {
268266 } )
269267 }
270268}
269+
270+ // == IMPORTANT NOTE ==
271+ //
272+ // The code below has been cherry-picked from the following PR:
273+ //
274+ // https://github.com/rust-lang/rust/pull/75148
275+ //
276+ // At the time of writing (16/09/20), this patch is in the nightly Rust channel. To avoid needing
277+ // to use the nightly compiler to build Parsec, we have instead opted to cherry-pick the change
278+ // from the patch to allow us to use this feature 'early'.
279+ //
280+ // Once the feature hits stable, it should be safe to revert the commit that introduced the changes
281+ // below with `git revert`.
282+ mod peer_credentials {
283+ use libc:: { gid_t, pid_t, uid_t} ;
284+
285+ /// Credentials for a UNIX process for credentials passing.
286+ #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
287+ pub struct UCred {
288+ /// The UID part of the peer credential. This is the effective UID of the process at the domain
289+ /// socket's endpoint.
290+ pub uid : uid_t ,
291+ /// The GID part of the peer credential. This is the effective GID of the process at the domain
292+ /// socket's endpoint.
293+ pub gid : gid_t ,
294+ /// The PID part of the peer credential. This field is optional because the PID part of the
295+ /// peer credentials is not supported on every platform. On platforms where the mechanism to
296+ /// discover the PID exists, this field will be populated to the PID of the process at the
297+ /// domain socket's endpoint. Otherwise, it will be set to None.
298+ pub pid : Option < pid_t > ,
299+ }
300+
301+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
302+ pub use self :: impl_linux:: peer_cred;
303+
304+ #[ cfg( any(
305+ target_os = "dragonfly" ,
306+ target_os = "freebsd" ,
307+ target_os = "ios" ,
308+ target_os = "macos" ,
309+ target_os = "openbsd"
310+ ) ) ]
311+ pub use self :: impl_bsd:: peer_cred;
312+
313+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
314+ pub mod impl_linux {
315+ use super :: UCred ;
316+ use libc:: { c_void, getsockopt, socklen_t, ucred, SOL_SOCKET , SO_PEERCRED } ;
317+ use std:: os:: unix:: io:: AsRawFd ;
318+ use std:: os:: unix:: net:: UnixStream ;
319+ use std:: { io, mem} ;
320+
321+ pub fn peer_cred ( socket : & UnixStream ) -> io:: Result < UCred > {
322+ let ucred_size = mem:: size_of :: < ucred > ( ) ;
323+
324+ // Trivial sanity checks.
325+ assert ! ( mem:: size_of:: <u32 >( ) <= mem:: size_of:: <usize >( ) ) ;
326+ assert ! ( ucred_size <= u32 :: MAX as usize ) ;
327+
328+ let mut ucred_size = ucred_size as socklen_t ;
329+ let mut ucred: ucred = ucred {
330+ pid : 1 ,
331+ uid : 1 ,
332+ gid : 1 ,
333+ } ;
334+
335+ unsafe {
336+ let ret = getsockopt (
337+ socket. as_raw_fd ( ) ,
338+ SOL_SOCKET ,
339+ SO_PEERCRED ,
340+ & mut ucred as * mut ucred as * mut c_void ,
341+ & mut ucred_size,
342+ ) ;
343+
344+ if ret == 0 && ucred_size as usize == mem:: size_of :: < ucred > ( ) {
345+ Ok ( UCred {
346+ uid : ucred. uid ,
347+ gid : ucred. gid ,
348+ pid : Some ( ucred. pid ) ,
349+ } )
350+ } else {
351+ Err ( io:: Error :: last_os_error ( ) )
352+ }
353+ }
354+ }
355+ }
356+
357+ #[ cfg( any(
358+ target_os = "dragonfly" ,
359+ target_os = "macos" ,
360+ target_os = "ios" ,
361+ target_os = "freebsd" ,
362+ target_os = "openbsd"
363+ ) ) ]
364+ pub mod impl_bsd {
365+ use super :: UCred ;
366+ use std:: io;
367+ use std:: os:: unix:: io:: AsRawFd ;
368+ use std:: os:: unix:: net:: UnixStream ;
369+
370+ pub fn peer_cred ( socket : & UnixStream ) -> io:: Result < UCred > {
371+ let mut cred = UCred {
372+ uid : 1 ,
373+ gid : 1 ,
374+ pid : None ,
375+ } ;
376+ unsafe {
377+ let ret = libc:: getpeereid ( socket. as_raw_fd ( ) , & mut cred. uid , & mut cred. gid ) ;
378+
379+ if ret == 0 {
380+ Ok ( cred)
381+ } else {
382+ Err ( io:: Error :: last_os_error ( ) )
383+ }
384+ }
385+ }
386+ }
387+ }
0 commit comments