Skip to content

Commit 16d348b

Browse files
committed
Add Socket::set_nosigpipe
1 parent aea3eeb commit 16d348b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/sys/unix.rs

+22
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ impl crate::Socket {
254254
pub fn set_cloexec(&self) -> io::Result<()> {
255255
fcntl_add(self.inner, libc::FD_CLOEXEC)
256256
}
257+
258+
/// Sets `SO_NOSIGPIPE` to one.
259+
#[cfg(target_vendor = "apple")]
260+
pub fn set_nosigpipe(&self) -> io::Result<()> {
261+
unsafe { setsockopt(self.inner, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1i32) }
262+
}
257263
}
258264

259265
fn fcntl_add(fd: SysSocket, flag: c_int) -> io::Result<()> {
@@ -267,6 +273,22 @@ fn fcntl_add(fd: SysSocket, flag: c_int) -> io::Result<()> {
267273
}
268274
}
269275

276+
#[cfg(target_vendor = "apple")]
277+
unsafe fn setsockopt<T>(fd: SysSocket, opt: c_int, val: c_int, payload: T) -> io::Result<()>
278+
where
279+
T: Copy,
280+
{
281+
let payload = &payload as *const T as *const c_void;
282+
syscall!(setsockopt(
283+
fd,
284+
opt,
285+
val,
286+
payload,
287+
mem::size_of::<T>() as libc::socklen_t,
288+
))?;
289+
Ok(())
290+
}
291+
270292
#[repr(transparent)] // Required during rewriting.
271293
pub struct Socket {
272294
fd: SysSocket,

0 commit comments

Comments
 (0)