File tree 3 files changed +61
-0
lines changed
3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
7
7
### Added
8
8
- Added ` mremap ` (#[ 1306] ( https://github.com/nix-rust/nix/pull/1306 ) )
9
9
10
+ - Added ` getpeereid ` (#[ 1342] ( https://github.com/nix-rust/nix/pull/1342 ) )
11
+ ### Changed
10
12
### Fixed
11
13
### Changed
12
14
Original file line number Diff line number Diff line change @@ -2795,3 +2795,23 @@ pub fn ttyname(fd: RawFd) -> Result<PathBuf> {
2795
2795
buf. truncate ( nul) ;
2796
2796
Ok ( OsString :: from_vec ( buf) . into ( ) )
2797
2797
}
2798
+
2799
+ /// Get the effective user ID and group ID associated with a Unix domain socket.
2800
+ ///
2801
+ /// See also [getpeereid(3)](https://www.freebsd.org/cgi/man.cgi?query=getpeereid)
2802
+ #[ cfg( any(
2803
+ target_os = "macos" ,
2804
+ target_os = "ios" ,
2805
+ target_os = "freebsd" ,
2806
+ target_os = "openbsd" ,
2807
+ target_os = "netbsd" ,
2808
+ target_os = "dragonfly" ,
2809
+ ) ) ]
2810
+ pub fn getpeereid ( fd : RawFd ) -> Result < ( Uid , Gid ) > {
2811
+ let mut uid = 1 ;
2812
+ let mut gid = 1 ;
2813
+
2814
+ let ret = unsafe { libc:: getpeereid ( fd, & mut uid, & mut gid) } ;
2815
+
2816
+ Errno :: result ( ret) . map ( |_| ( Uid ( uid) , Gid ( gid) ) )
2817
+ }
Original file line number Diff line number Diff line change @@ -1068,3 +1068,42 @@ fn test_ttyname_not_pty() {
1068
1068
fn test_ttyname_invalid_fd ( ) {
1069
1069
assert_eq ! ( ttyname( -1 ) , Err ( Error :: Sys ( Errno :: EBADF ) ) ) ;
1070
1070
}
1071
+
1072
+ #[ test]
1073
+ #[ cfg( any(
1074
+ target_os = "macos" ,
1075
+ target_os = "ios" ,
1076
+ target_os = "freebsd" ,
1077
+ target_os = "openbsd" ,
1078
+ target_os = "netbsd" ,
1079
+ target_os = "dragonfly" ,
1080
+ ) ) ]
1081
+ fn test_getpeereid ( ) {
1082
+ use std:: os:: unix:: net:: UnixStream ;
1083
+ let ( sock_a, sock_b) = UnixStream :: pair ( ) . unwrap ( ) ;
1084
+
1085
+ let ( uid_a, gid_a) = getpeereid ( sock_a. as_raw_fd ( ) ) . unwrap ( ) ;
1086
+ let ( uid_b, gid_b) = getpeereid ( sock_b. as_raw_fd ( ) ) . unwrap ( ) ;
1087
+
1088
+ let uid = geteuid ( ) ;
1089
+ let gid = getegid ( ) ;
1090
+
1091
+ assert_eq ! ( uid, uid_a) ;
1092
+ assert_eq ! ( gid, gid_a) ;
1093
+ assert_eq ! ( uid_a, uid_b) ;
1094
+ assert_eq ! ( gid_a, gid_b) ;
1095
+ }
1096
+
1097
+ #[ test]
1098
+ #[ cfg( any(
1099
+ target_os = "macos" ,
1100
+ target_os = "ios" ,
1101
+ target_os = "freebsd" ,
1102
+ target_os = "openbsd" ,
1103
+ target_os = "netbsd" ,
1104
+ target_os = "dragonfly" ,
1105
+ ) ) ]
1106
+ fn test_getpeereid_invalid_fd ( ) {
1107
+ // getpeereid is not POSIX, so error codes are inconsistent between different Unices.
1108
+ assert ! ( getpeereid( -1 ) . is_err( ) ) ;
1109
+ }
You can’t perform that action at this time.
0 commit comments