Skip to content

Commit

Permalink
Add Socket::set_nosigpipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasdezeeuw committed Oct 8, 2020
1 parent b184ab5 commit 5b6eaae
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ impl crate::Socket {
pub fn set_cloexec(&self) -> io::Result<()> {
fcntl_add(self.inner, libc::FD_CLOEXEC)
}

/// Sets `SO_NOSIGPIPE` to one.
#[cfg(target_vendor = "apple")]
pub fn set_nosigpipe(&self) -> io::Result<()> {
unsafe { setsockopt(self.inner, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1i32) }
}
}

fn fcntl_add(fd: SysSocket, flag: c_int) -> io::Result<()> {
Expand All @@ -267,6 +273,22 @@ fn fcntl_add(fd: SysSocket, flag: c_int) -> io::Result<()> {
}
}

#[cfg(target_vendor = "apple")]
unsafe fn setsockopt<T>(fd: SysSocket, opt: c_int, val: c_int, payload: T) -> io::Result<()>
where
T: Copy,
{
let payload = &payload as *const T as *const c_void;
syscall!(setsockopt(
fd,
opt,
val,
payload,
mem::size_of::<T>() as libc::socklen_t,
))?;
Ok(())
}

#[repr(transparent)] // Required during rewriting.
pub struct Socket {
fd: SysSocket,
Expand Down

0 comments on commit 5b6eaae

Please sign in to comment.