@@ -251,14 +251,12 @@ impl DomainSocketListenerBuilder {
251
251
252
252
impl GetMetadata for UnixStream {
253
253
fn metadata ( & self ) -> Option < ConnectionMetadata > {
254
- let ucred = self
255
- . peer_cred ( )
254
+ let ucred = peer_credentials:: peer_cred ( self )
256
255
. map_err ( |err| {
257
256
format_error ! (
258
257
"Failed to grab peer credentials metadata from UnixStream" ,
259
258
err
260
- ) ;
261
- err
259
+ )
262
260
} )
263
261
. ok ( ) ?;
264
262
@@ -268,3 +266,123 @@ impl GetMetadata for UnixStream {
268
266
} )
269
267
}
270
268
}
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
+ #[ deny( trivial_casts) ]
315
+ pub mod impl_linux {
316
+ use super :: UCred ;
317
+ use libc:: { c_void, getsockopt, socklen_t, ucred, SOL_SOCKET , SO_PEERCRED } ;
318
+ use std:: os:: unix:: io:: AsRawFd ;
319
+ use std:: os:: unix:: net:: UnixStream ;
320
+ use std:: { io, mem} ;
321
+
322
+ pub fn peer_cred ( socket : & UnixStream ) -> io:: Result < UCred > {
323
+ let ucred_size = mem:: size_of :: < ucred > ( ) ;
324
+
325
+ // Trivial sanity checks.
326
+ assert ! ( mem:: size_of:: <u32 >( ) <= mem:: size_of:: <usize >( ) ) ;
327
+ assert ! ( ucred_size <= u32 :: MAX as usize ) ;
328
+
329
+ let mut ucred_size = ucred_size as socklen_t ;
330
+ let mut ucred: ucred = ucred {
331
+ pid : 1 ,
332
+ uid : 1 ,
333
+ gid : 1 ,
334
+ } ;
335
+
336
+ unsafe {
337
+ let ret = getsockopt (
338
+ socket. as_raw_fd ( ) ,
339
+ SOL_SOCKET ,
340
+ SO_PEERCRED ,
341
+ & mut ucred as * mut ucred as * mut c_void ,
342
+ & mut ucred_size,
343
+ ) ;
344
+
345
+ if ret == 0 && ucred_size as usize == mem:: size_of :: < ucred > ( ) {
346
+ Ok ( UCred {
347
+ uid : ucred. uid ,
348
+ gid : ucred. gid ,
349
+ pid : Some ( ucred. pid ) ,
350
+ } )
351
+ } else {
352
+ Err ( io:: Error :: last_os_error ( ) )
353
+ }
354
+ }
355
+ }
356
+ }
357
+
358
+ #[ cfg( any(
359
+ target_os = "dragonfly" ,
360
+ target_os = "macos" ,
361
+ target_os = "ios" ,
362
+ target_os = "freebsd" ,
363
+ target_os = "openbsd"
364
+ ) ) ]
365
+ pub mod impl_bsd {
366
+ use super :: UCred ;
367
+ use std:: io;
368
+ use std:: os:: unix:: io:: AsRawFd ;
369
+ use std:: os:: unix:: net:: UnixStream ;
370
+
371
+ pub fn peer_cred ( socket : & UnixStream ) -> io:: Result < UCred > {
372
+ let mut cred = UCred {
373
+ uid : 1 ,
374
+ gid : 1 ,
375
+ pid : None ,
376
+ } ;
377
+ unsafe {
378
+ let ret = libc:: getpeereid ( socket. as_raw_fd ( ) , & mut cred. uid , & mut cred. gid ) ;
379
+
380
+ if ret == 0 {
381
+ Ok ( cred)
382
+ } else {
383
+ Err ( io:: Error :: last_os_error ( ) )
384
+ }
385
+ }
386
+ }
387
+ }
388
+ }
0 commit comments