Skip to content

Commit 24ce55f

Browse files
committed
Auto merge of #2105 - tormol:udx, r=JohnTitor
Add more unix domain socket peer information to more targets * Add NetBSD peer credentials struct `unpcbid` and socket option `SO_PEEREID` (similar to Linux struct `ucred`). (and some other socket options too along the way) * Add `struct xucred`, `LOCAL_PEERCRED` and related to DragonFly BSD too. * Add new `cr_pid` field to FreeBSD `struct xucred` (added in FreeBSD 13). It is added through an union as in the C header to make sure the alignment is correct, but could probably be a simple field with padding too. * Add `SO_PEERSEC` and `SO_PASSEC` to Android and all Linux architectures that they hadn't already been added to.
2 parents fc51a0f + 3f62e51 commit 24ce55f

File tree

21 files changed

+147
-13
lines changed

21 files changed

+147
-13
lines changed

libc-test/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,9 @@ fn test_freebsd(target: &str) {
18211821
});
18221822

18231823
cfg.skip_struct(move |ty| {
1824+
if ty.starts_with("__c_anonymous_") {
1825+
return true;
1826+
}
18241827
match ty {
18251828
// `mmsghdr` is not available in FreeBSD 10
18261829
"mmsghdr" if Some(10) == freebsd_ver => true,
@@ -1898,6 +1901,9 @@ fn test_freebsd(target: &str) {
18981901
("Elf32_Phdr", "p_type") => true,
18991902
("Elf64_Phdr", "p_type") => true,
19001903

1904+
// not available until FreeBSD 12, and is an anonymous union there.
1905+
("xucred", "cr_pid__c_anonymous_union") => true,
1906+
19011907
_ => false,
19021908
}
19031909
});

src/unix/bsd/freebsdlike/dragonfly/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ s! {
171171
pub sdl_route: [::c_ushort; 16],
172172
}
173173

174+
pub struct xucred {
175+
pub cr_version: ::c_uint,
176+
pub cr_uid: ::uid_t,
177+
pub cr_ngroups: ::c_short,
178+
pub cr_groups: [::gid_t; 16],
179+
__cr_unused1: *mut ::c_void,
180+
}
181+
174182
pub struct stack_t {
175183
pub ss_sp: *mut ::c_char,
176184
pub ss_size: ::size_t,
@@ -238,7 +246,6 @@ s_no_extra_traits! {
238246
pub sigev_value: ::sigval,
239247
__unused3: *mut ::c_void //actually a function pointer
240248
}
241-
242249
}
243250

244251
cfg_if! {

src/unix/bsd/freebsdlike/freebsd/mod.rs

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ s! {
8989
pub msg_ctime: ::time_t,
9090
}
9191

92-
pub struct xucred {
93-
pub cr_version: ::c_uint,
94-
pub cr_uid: ::uid_t,
95-
pub cr_ngroups: ::c_short,
96-
pub cr_groups: [::gid_t;16],
97-
__cr_unused1: *mut ::c_void,
98-
}
99-
10092
pub struct stack_t {
10193
pub ss_sp: *mut ::c_void,
10294
pub ss_size: ::size_t,
@@ -143,6 +135,23 @@ s_no_extra_traits! {
143135
pub __ut_spare: [::c_char; 64],
144136
}
145137

138+
#[cfg(libc_union)]
139+
pub union __c_anonymous_cr_pid {
140+
__cr_unused: *mut ::c_void,
141+
pub cr_pid: ::pid_t,
142+
}
143+
144+
pub struct xucred {
145+
pub cr_version: ::c_uint,
146+
pub cr_uid: ::uid_t,
147+
pub cr_ngroups: ::c_short,
148+
pub cr_groups: [::gid_t; 16],
149+
#[cfg(libc_union)]
150+
pub cr_pid__c_anonymous_union: __c_anonymous_cr_pid,
151+
#[cfg(not(libc_union))]
152+
__cr_unused1: *mut ::c_void,
153+
}
154+
146155
pub struct sockaddr_dl {
147156
pub sdl_len: ::c_uchar,
148157
pub sdl_family: ::c_uchar,
@@ -225,6 +234,73 @@ cfg_if! {
225234
}
226235
}
227236

237+
#[cfg(libc_union)]
238+
impl PartialEq for __c_anonymous_cr_pid {
239+
fn eq(&self, other: &__c_anonymous_cr_pid) -> bool {
240+
unsafe { self.cr_pid == other.cr_pid}
241+
}
242+
}
243+
#[cfg(libc_union)]
244+
impl Eq for __c_anonymous_cr_pid {}
245+
#[cfg(libc_union)]
246+
impl ::fmt::Debug for __c_anonymous_cr_pid {
247+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
248+
f.debug_struct("cr_pid")
249+
.field("cr_pid", unsafe { &self.cr_pid })
250+
.finish()
251+
}
252+
}
253+
#[cfg(libc_union)]
254+
impl ::hash::Hash for __c_anonymous_cr_pid {
255+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
256+
unsafe { self.cr_pid.hash(state) };
257+
}
258+
}
259+
260+
impl PartialEq for xucred {
261+
fn eq(&self, other: &xucred) -> bool {
262+
#[cfg(libc_union)]
263+
let equal_cr_pid = self.cr_pid__c_anonymous_union
264+
== other.cr_pid__c_anonymous_union;
265+
#[cfg(not(libc_union))]
266+
let equal_cr_pid = self.__cr_unused1 == other.__cr_unused1;
267+
268+
self.cr_version == other.cr_version
269+
&& self.cr_uid == other.cr_uid
270+
&& self.cr_ngroups == other.cr_ngroups
271+
&& self.cr_groups == other.cr_groups
272+
&& equal_cr_pid
273+
}
274+
}
275+
impl Eq for xucred {}
276+
impl ::fmt::Debug for xucred {
277+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
278+
let mut struct_formatter = f.debug_struct("xucred");
279+
struct_formatter.field("cr_version", &self.cr_version);
280+
struct_formatter.field("cr_uid", &self.cr_uid);
281+
struct_formatter.field("cr_ngroups", &self.cr_ngroups);
282+
struct_formatter.field("cr_groups", &self.cr_groups);
283+
#[cfg(libc_union)]
284+
struct_formatter.field(
285+
"cr_pid__c_anonymous_union",
286+
&self.cr_pid__c_anonymous_union
287+
);
288+
struct_formatter.finish()
289+
}
290+
}
291+
impl ::hash::Hash for xucred {
292+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
293+
self.cr_version.hash(state);
294+
self.cr_uid.hash(state);
295+
self.cr_ngroups.hash(state);
296+
self.cr_groups.hash(state);
297+
#[cfg(libc_union)]
298+
self.cr_pid__c_anonymous_union.hash(state);
299+
#[cfg(not(libc_union))]
300+
self.__cr_unused1.hash(state);
301+
}
302+
}
303+
228304
impl PartialEq for sockaddr_dl {
229305
fn eq(&self, other: &sockaddr_dl) -> bool {
230306
self.sdl_len == other.sdl_len
@@ -629,7 +705,6 @@ pub const SO_PROTOCOL: ::c_int = 0x1016;
629705
pub const SO_PROTOTYPE: ::c_int = SO_PROTOCOL;
630706
pub const SO_VENDOR: ::c_int = 0x80000000;
631707

632-
pub const LOCAL_PEERCRED: ::c_int = 1;
633708
pub const LOCAL_CREDS: ::c_int = 2;
634709
pub const LOCAL_CONNWAIT: ::c_int = 4;
635710
pub const LOCAL_VENDOR: ::c_int = SO_VENDOR;
@@ -1103,9 +1178,6 @@ pub const _PC_ACL_NFS4: ::c_int = 64;
11031178

11041179
pub const _SC_CPUSET_SIZE: ::c_int = 122;
11051180

1106-
pub const XU_NGROUPS: ::c_int = 16;
1107-
pub const XUCRED_VERSION: ::c_uint = 0;
1108-
11091181
// Flags which can be passed to pdfork(2)
11101182
pub const PD_DAEMON: ::c_int = 0x00000001;
11111183
pub const PD_CLOEXEC: ::c_int = 0x00000002;

src/unix/bsd/freebsdlike/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,8 @@ pub const SO_RCVTIMEO: ::c_int = 0x1006;
947947
pub const SO_ERROR: ::c_int = 0x1007;
948948
pub const SO_TYPE: ::c_int = 0x1008;
949949

950+
pub const LOCAL_PEERCRED: ::c_int = 1;
951+
950952
pub const SHUT_RD: ::c_int = 0;
951953
pub const SHUT_WR: ::c_int = 1;
952954
pub const SHUT_RDWR: ::c_int = 2;
@@ -1133,6 +1135,9 @@ pub const ST_NOSUID: ::c_ulong = 2;
11331135

11341136
pub const NI_MAXHOST: ::size_t = 1025;
11351137

1138+
pub const XU_NGROUPS: ::c_int = 16;
1139+
pub const XUCRED_VERSION: ::c_uint = 0;
1140+
11361141
pub const RTLD_LOCAL: ::c_int = 0;
11371142
pub const RTLD_NODELETE: ::c_int = 0x1000;
11381143
pub const RTLD_NOLOAD: ::c_int = 0x2000;

src/unix/bsd/netbsdlike/netbsd/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ s! {
299299
pub sc_groups: [::gid_t; 1],
300300
}
301301

302+
pub struct unpcbid {
303+
pub unp_pid: ::pid_t,
304+
pub unp_euid: ::uid_t,
305+
pub unp_egid: ::gid_t,
306+
}
307+
302308
pub struct sockaddr_dl {
303309
pub sdl_len: ::c_uchar,
304310
pub sdl_family: ::c_uchar,
@@ -1047,6 +1053,12 @@ pub const SO_TIMESTAMP: ::c_int = 0x2000;
10471053
pub const SO_OVERFLOWED: ::c_int = 0x1009;
10481054
pub const SO_NOHEADER: ::c_int = 0x100a;
10491055

1056+
// http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/sys/un.h?annotate
1057+
pub const LOCAL_OCREDS: ::c_int = 0x0001; // pass credentials to receiver
1058+
pub const LOCAL_CONNWAIT: ::c_int = 0x0002; // connects block until accepted
1059+
pub const LOCAL_PEEREID: ::c_int = 0x0003; // get peer identification
1060+
pub const LOCAL_CREDS: ::c_int = 0x0004; // pass credentials to receiver
1061+
10501062
// https://github.com/NetBSD/src/blob/trunk/sys/net/if.h#L373
10511063
pub const IFF_UP: ::c_int = 0x0001; // interface is up
10521064
pub const IFF_BROADCAST: ::c_int = 0x0002; // broadcast address valid

src/unix/linux_like/android/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,10 @@ pub const SO_SNDTIMEO: ::c_int = 21;
10781078
pub const SO_BINDTODEVICE: ::c_int = 25;
10791079
pub const SO_TIMESTAMP: ::c_int = 29;
10801080
pub const SO_ACCEPTCONN: ::c_int = 30;
1081+
pub const SO_PEERSEC: ::c_int = 31;
10811082
pub const SO_SNDBUFFORCE: ::c_int = 32;
10821083
pub const SO_RCVBUFFORCE: ::c_int = 33;
1084+
pub const SO_PASSSEC: ::c_int = 34;
10831085
pub const SO_MARK: ::c_int = 36;
10841086
pub const SO_PROTOCOL: ::c_int = 38;
10851087
pub const SO_DOMAIN: ::c_int = 39;

src/unix/linux_like/linux/gnu/b32/arm/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,10 @@ pub const SO_RCVLOWAT: ::c_int = 18;
321321
pub const SO_SNDLOWAT: ::c_int = 19;
322322
pub const SO_RCVTIMEO: ::c_int = 20;
323323
pub const SO_SNDTIMEO: ::c_int = 21;
324+
pub const SO_PEERSEC: ::c_int = 31;
324325
pub const SO_SNDBUFFORCE: ::c_int = 32;
325326
pub const SO_RCVBUFFORCE: ::c_int = 33;
327+
pub const SO_PASSSEC: ::c_int = 34;
326328

327329
pub const SA_SIGINFO: ::c_int = 0x00000004;
328330
pub const SA_NOCLDWAIT: ::c_int = 0x00000002;

src/unix/linux_like/linux/gnu/b32/powerpc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ pub const SO_RCVTIMEO: ::c_int = 18;
325325
pub const SO_SNDTIMEO: ::c_int = 19;
326326
pub const SO_PASSCRED: ::c_int = 20;
327327
pub const SO_PEERCRED: ::c_int = 21;
328+
pub const SO_PEERSEC: ::c_int = 31;
329+
pub const SO_PASSSEC: ::c_int = 34;
328330

329331
pub const SA_SIGINFO: ::c_int = 0x00000004;
330332
pub const SA_NOCLDWAIT: ::c_int = 0x00000002;

src/unix/linux_like/linux/gnu/b32/sparc/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ pub const SOL_SOCKET: ::c_int = 0xffff;
324324

325325
pub const SO_PASSCRED: ::c_int = 2;
326326
pub const SO_REUSEADDR: ::c_int = 4;
327+
pub const SO_PEERSEC: ::c_int = 0x001e;
328+
pub const SO_PASSSEC: ::c_int = 0x001f;
327329
pub const SO_TYPE: ::c_int = 0x1008;
328330
pub const SO_ERROR: ::c_int = 0x1007;
329331
pub const SO_DONTROUTE: ::c_int = 16;

src/unix/linux_like/linux/gnu/b32/x86/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ pub const SO_RCVLOWAT: ::c_int = 18;
526526
pub const SO_SNDLOWAT: ::c_int = 19;
527527
pub const SO_RCVTIMEO: ::c_int = 20;
528528
pub const SO_SNDTIMEO: ::c_int = 21;
529+
pub const SO_PEERSEC: ::c_int = 31;
530+
pub const SO_PASSSEC: ::c_int = 34;
529531

530532
pub const SA_SIGINFO: ::c_int = 0x00000004;
531533
pub const SA_NOCLDWAIT: ::c_int = 0x00000002;

src/unix/linux_like/linux/gnu/b64/s390x.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,10 @@ pub const SO_PEERCRED: ::c_int = 17;
486486
pub const SO_RCVLOWAT: ::c_int = 18;
487487
pub const SO_SNDLOWAT: ::c_int = 19;
488488
pub const SO_ACCEPTCONN: ::c_int = 30;
489+
pub const SO_PEERSEC: ::c_int = 31;
489490
pub const SO_SNDBUFFORCE: ::c_int = 32;
490491
pub const SO_RCVBUFFORCE: ::c_int = 33;
492+
pub const SO_PASSSEC: ::c_int = 34;
491493

492494
pub const SIGTTIN: ::c_int = 21;
493495
pub const SIGTTOU: ::c_int = 22;

src/unix/linux_like/linux/gnu/b64/sparc64/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ pub const SO_PASSCRED: ::c_int = 2;
334334
pub const SO_REUSEADDR: ::c_int = 4;
335335
pub const SO_BINDTODEVICE: ::c_int = 0x000d;
336336
pub const SO_TIMESTAMP: ::c_int = 0x001d;
337+
pub const SO_PEERSEC: ::c_int = 0x001e;
338+
pub const SO_PASSSEC: ::c_int = 0x001f;
337339
pub const SO_MARK: ::c_int = 0x0022;
338340
pub const SO_RXQ_OVFL: ::c_int = 0x0024;
339341
pub const SO_PEEK_OFF: ::c_int = 0x0026;

src/unix/linux_like/linux/musl/b32/arm/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,10 @@ pub const SO_SNDLOWAT: ::c_int = 19;
377377
pub const SO_RCVTIMEO: ::c_int = 20;
378378
pub const SO_SNDTIMEO: ::c_int = 21;
379379
pub const SO_ACCEPTCONN: ::c_int = 30;
380+
pub const SO_PEERSEC: ::c_int = 31;
380381
pub const SO_SNDBUFFORCE: ::c_int = 32;
381382
pub const SO_RCVBUFFORCE: ::c_int = 33;
383+
pub const SO_PASSSEC: ::c_int = 34;
382384
pub const SO_PROTOCOL: ::c_int = 38;
383385
pub const SO_DOMAIN: ::c_int = 39;
384386

src/unix/linux_like/linux/musl/b32/mips/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,10 @@ pub const SO_PRIORITY: ::c_int = 12;
388388
pub const SO_BSDCOMPAT: ::c_int = 14;
389389
pub const SO_PASSCRED: ::c_int = 17;
390390
pub const SO_PEERCRED: ::c_int = 18;
391+
pub const SO_PEERSEC: ::c_int = 30;
391392
pub const SO_SNDBUFFORCE: ::c_int = 31;
392393
pub const SO_RCVBUFFORCE: ::c_int = 33;
394+
pub const SO_PASSSEC: ::c_int = 34;
393395

394396
pub const SA_ONSTACK: ::c_int = 0x08000000;
395397
pub const SA_SIGINFO: ::c_int = 8;

src/unix/linux_like/linux/musl/b32/powerpc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,10 @@ pub const SO_SNDTIMEO: ::c_int = 19;
379379
pub const SO_PASSCRED: ::c_int = 20;
380380
pub const SO_PEERCRED: ::c_int = 21;
381381
pub const SO_ACCEPTCONN: ::c_int = 30;
382+
pub const SO_PEERSEC: ::c_int = 31;
382383
pub const SO_SNDBUFFORCE: ::c_int = 32;
383384
pub const SO_RCVBUFFORCE: ::c_int = 33;
385+
pub const SO_PASSSEC: ::c_int = 34;
384386
pub const SO_PROTOCOL: ::c_int = 38;
385387
pub const SO_DOMAIN: ::c_int = 39;
386388

src/unix/linux_like/linux/musl/b32/x86/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,10 @@ pub const SO_SNDLOWAT: ::c_int = 19;
437437
pub const SO_RCVTIMEO: ::c_int = 20;
438438
pub const SO_SNDTIMEO: ::c_int = 21;
439439
pub const SO_ACCEPTCONN: ::c_int = 30;
440+
pub const SO_PEERSEC: ::c_int = 31;
440441
pub const SO_SNDBUFFORCE: ::c_int = 32;
441442
pub const SO_RCVBUFFORCE: ::c_int = 33;
443+
pub const SO_PASSSEC: ::c_int = 34;
442444
pub const SO_PROTOCOL: ::c_int = 38;
443445
pub const SO_DOMAIN: ::c_int = 39;
444446

src/unix/linux_like/linux/musl/b64/aarch64/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ pub const SO_RCVLOWAT: ::c_int = 18;
598598
pub const SO_SNDLOWAT: ::c_int = 19;
599599
pub const SO_RCVTIMEO: ::c_int = 20;
600600
pub const SO_SNDTIMEO: ::c_int = 21;
601+
pub const SO_PEERSEC: ::c_int = 31;
602+
pub const SO_PASSSEC: ::c_int = 34;
601603
pub const EXTPROC: ::tcflag_t = 0x00010000;
602604
pub const VEOL: usize = 11;
603605
pub const VEOL2: usize = 16;

src/unix/linux_like/linux/musl/b64/powerpc64.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,8 @@ pub const SO_RCVLOWAT: ::c_int = 16;
613613
pub const SO_SNDLOWAT: ::c_int = 17;
614614
pub const SO_RCVTIMEO: ::c_int = 18;
615615
pub const SO_SNDTIMEO: ::c_int = 19;
616+
pub const SO_PEERSEC: ::c_int = 31;
617+
pub const SO_PASSSEC: ::c_int = 34;
616618
pub const EXTPROC: ::tcflag_t = 0x10000000;
617619
pub const VEOL: usize = 6;
618620
pub const VEOL2: usize = 8;

src/unix/linux_like/linux/musl/b64/x86_64/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,8 @@ pub const SO_RCVLOWAT: ::c_int = 18;
903903
pub const SO_SNDLOWAT: ::c_int = 19;
904904
pub const SO_RCVTIMEO: ::c_int = 20;
905905
pub const SO_SNDTIMEO: ::c_int = 21;
906+
pub const SO_PEERSEC: ::c_int = 31;
907+
pub const SO_PASSSEC: ::c_int = 34;
906908
pub const EXTPROC: ::tcflag_t = 0x00010000;
907909
pub const VEOL: usize = 11;
908910
pub const VEOL2: usize = 16;

src/unix/linux_like/linux/uclibc/arm/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ pub const MAP_HUGETLB: ::c_int = 0x040000; // from linux/other/mod.rs
262262
pub const SO_BUSY_POLL: ::c_int = 46; // from src/unix/linux_like/mod.rs
263263
pub const SO_PEEK_OFF: ::c_int = 42; // from src/unix/linux_like/mod.rs
264264
pub const SO_REUSEPORT: ::c_int = 15; // from src/unix/linux_like/mod.rs
265+
pub const SO_PEERSEC: ::c_int = 31;
266+
pub const SO_PASSSEC: ::c_int = 34;
265267

266268
// autogenerated constants with hand tuned types
267269
pub const B0: ::speed_t = 0;

src/unix/linux_like/linux/uclibc/x86_64/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ pub const SO_RCVTIMEO: ::c_int = 20;
324324
pub const SO_REUSEADDR: ::c_int = 2;
325325
pub const SO_SNDTIMEO: ::c_int = 21;
326326
pub const SO_TIMESTAMP: ::c_int = 0x1d;
327+
pub const SO_PEERSEC: ::c_int = 31;
328+
pub const SO_PASSSEC: ::c_int = 34;
327329
pub const RLIM_INFINITY: u64 = 0xffffffffffffffff;
328330
pub const __SIZEOF_PTHREAD_COND_T: usize = 48;
329331
pub const __SIZEOF_PTHREAD_CONDATTR_T: usize = 4;

0 commit comments

Comments
 (0)