Skip to content

Commit 0d1639c

Browse files
committed
Add set_no_peercred for cygwin
1 parent e379cf7 commit 0d1639c

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

src/sys/unix.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ pub(crate) use libc::{TCP_KEEPCNT, TCP_KEEPINTVL};
282282
// See this type in the Windows file.
283283
pub(crate) type Bool = c_int;
284284

285+
#[cfg(target_os = "cygwin")]
286+
use libc::SO_PEERCRED;
285287
#[cfg(any(
286288
target_os = "ios",
287289
target_os = "visionos",
@@ -1499,6 +1501,20 @@ impl crate::Socket {
14991501
}
15001502
}
15011503

1504+
/// Sets `SO_PEERCRED` to null on the socket.
1505+
/// It disables the initial handshake of unix domain sockets.
1506+
#[cfg(target_os = "cygwin")]
1507+
pub fn set_no_peercred(&self) -> io::Result<()> {
1508+
syscall!(setsockopt(
1509+
self.as_raw(),
1510+
SOL_SOCKET,
1511+
SO_PEERCRED,
1512+
ptr::null_mut(),
1513+
0,
1514+
))
1515+
.map(|_| ())
1516+
}
1517+
15021518
/// Sets `SO_NOSIGPIPE` on the socket.
15031519
#[cfg(all(
15041520
feature = "all",

tests/socket.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -557,24 +557,6 @@ fn unix_sockets_supported() -> bool {
557557
true
558558
}
559559

560-
// An extension on cygwin. It disables initial handshake on AF_UNIX sockets.
561-
// https://cygwin.com/git/?p=newlib-cygwin.git;a=commit;h=697b9afe0
562-
#[cfg(target_os = "cygwin")]
563-
fn set_no_peercred(s: &Socket) {
564-
let res = unsafe {
565-
libc::setsockopt(
566-
s.as_raw_fd(),
567-
libc::SOL_SOCKET,
568-
libc::SO_PEERCRED,
569-
std::ptr::null(),
570-
0,
571-
)
572-
};
573-
if res == -1 {
574-
panic!("{:?}", std::io::Error::last_os_error());
575-
}
576-
}
577-
578560
#[test]
579561
fn unix() {
580562
if !unix_sockets_supported() {
@@ -590,13 +572,13 @@ fn unix() {
590572

591573
let listener = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
592574
#[cfg(target_os = "cygwin")]
593-
set_no_peercred(&listener);
575+
listener.set_no_peercred().unwrap();
594576
listener.bind(&addr).unwrap();
595577
listener.listen(10).unwrap();
596578

597579
let mut a = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
598580
#[cfg(target_os = "cygwin")]
599-
set_no_peercred(&a);
581+
a.set_no_peercred().unwrap();
600582
a.connect(&addr).unwrap();
601583
let mut b = listener.accept().unwrap().0;
602584

0 commit comments

Comments
 (0)