Skip to content

Commit f3c5527

Browse files
authored
Add support for SO_NOSIGPIPE on targeted OSes (#533)
* Add SO_NOSIGPIPE * NetBSD doesn't support it yet, actually
1 parent 233114c commit f3c5527

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/backend/libc/net/syscalls.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,30 @@ pub(crate) mod sockopt {
636636
}
637637
}
638638

639+
#[cfg(any(
640+
target_os = "freebsd",
641+
target_os = "macos",
642+
target_os = "ios",
643+
target_os = "tvos",
644+
target_os = "watchos"
645+
))]
646+
#[inline]
647+
pub(crate) fn getsockopt_nosigpipe(fd: BorrowedFd<'_>) -> io::Result<bool> {
648+
getsockopt(fd, c::SOL_SOCKET, c::SO_NOSIGPIPE).map(to_bool)
649+
}
650+
651+
#[cfg(any(
652+
target_os = "freebsd",
653+
target_os = "macos",
654+
target_os = "ios",
655+
target_os = "tvos",
656+
target_os = "watchos"
657+
))]
658+
#[inline]
659+
pub(crate) fn setsockopt_nosigpipe(fd: BorrowedFd<'_>, val: bool) -> io::Result<()> {
660+
setsockopt(fd, c::SOL_SOCKET, c::SO_NOSIGPIPE, from_bool(val))
661+
}
662+
639663
#[inline]
640664
pub(crate) fn set_ip_ttl(fd: BorrowedFd<'_>, ttl: u32) -> io::Result<()> {
641665
setsockopt(fd, c::IPPROTO_IP as _, c::IP_TTL, ttl)

src/net/sockopt.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,61 @@ pub fn set_tcp_nodelay<Fd: AsFd>(fd: Fd, nodelay: bool) -> io::Result<()> {
642642
pub fn get_tcp_nodelay<Fd: AsFd>(fd: Fd) -> io::Result<bool> {
643643
backend::net::syscalls::sockopt::get_tcp_nodelay(fd.as_fd())
644644
}
645+
646+
/// `getsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE)`
647+
///
648+
/// # References
649+
/// - [POSIX `getsockopt`]
650+
/// - [POSIX `netinet/tcp.h`]
651+
/// - [Linux `getsockopt`]
652+
/// - [Linux `tcp`]
653+
/// - [Winsock2 `getsockopt`]
654+
/// - [Winsock2 `IPPROTO_TCP` options]
655+
///
656+
/// [POSIX `getsockopt`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html
657+
/// [POSIX `netinet/tcp.h`]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_tcp.h.html
658+
/// [Linux `getsockopt`]: https://man7.org/linux/man-pages/man2/getsockopt.2.html
659+
/// [Linux `tcp`]: https://man7.org/linux/man-pages/man7/tcp.7.html
660+
/// [Winsock2 `setsockopt`]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-setsockopt
661+
/// [Winsock2 `IPPROTO_TCP` options]: https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options
662+
#[cfg(any(
663+
target_os = "freebsd",
664+
target_os = "macos",
665+
target_os = "ios",
666+
target_os = "tvos",
667+
target_os = "watchos"
668+
))]
669+
#[doc(alias = "SO_NOSIGPIPE")]
670+
#[inline]
671+
pub fn getsockopt_nosigpipe<Fd: AsFd>(fd: Fd) -> io::Result<bool> {
672+
backend::net::syscalls::sockopt::getsockopt_nosigpipe(fd.as_fd())
673+
}
674+
675+
/// `setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, val)`
676+
///
677+
/// # References
678+
/// - [POSIX `getsockopt`]
679+
/// - [POSIX `netinet/tcp.h`]
680+
/// - [Linux `getsockopt`]
681+
/// - [Linux `tcp`]
682+
/// - [Winsock2 `getsockopt`]
683+
/// - [Winsock2 `IPPROTO_TCP` options]
684+
///
685+
/// [POSIX `getsockopt`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html
686+
/// [POSIX `netinet/tcp.h`]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_tcp.h.html
687+
/// [Linux `getsockopt`]: https://man7.org/linux/man-pages/man2/getsockopt.2.html
688+
/// [Linux `tcp`]: https://man7.org/linux/man-pages/man7/tcp.7.html
689+
/// [Winsock2 `setsockopt`]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-setsockopt
690+
/// [Winsock2 `IPPROTO_TCP` options]: https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options
691+
#[cfg(any(
692+
target_os = "freebsd",
693+
target_os = "macos",
694+
target_os = "ios",
695+
target_os = "tvos",
696+
target_os = "watchos"
697+
))]
698+
#[doc(alias = "SO_NOSIGPIPE")]
699+
#[inline]
700+
pub fn setsockopt_nosigpipe<Fd: AsFd>(fd: Fd, val: bool) -> io::Result<()> {
701+
backend::net::syscalls::sockopt::setsockopt_nosigpipe(fd.as_fd(), val)
702+
}

0 commit comments

Comments
 (0)