|
| 1 | +cfg_if::cfg_if! { |
| 2 | + if #[cfg(any( |
| 3 | + target_os = "linux", target_os = "android", |
| 4 | + target_os = "hurd", |
| 5 | + target_os = "dragonfly", target_os = "freebsd", |
| 6 | + target_os = "openbsd", target_os = "netbsd", |
| 7 | + target_os = "solaris", target_os = "illumos", |
| 8 | + target_os = "haiku", target_os = "nto", |
| 9 | + target_os = "cygwin"))] { |
| 10 | + use libc::MSG_NOSIGNAL; |
| 11 | + } else { |
| 12 | + const MSG_NOSIGNAL: core::ffi::c_int = 0x0; |
| 13 | + } |
| 14 | +} |
| 15 | + |
1 | 16 | use super::{SocketAddr, sockaddr_un};
|
2 | 17 | #[cfg(any(doc, target_os = "android", target_os = "linux"))]
|
3 | 18 | use super::{SocketAncillary, recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to};
|
@@ -41,6 +56,12 @@ use crate::time::Duration;
|
41 | 56 | /// Ok(())
|
42 | 57 | /// }
|
43 | 58 | /// ```
|
| 59 | +/// |
| 60 | +/// # `SIGPIPE` |
| 61 | +/// |
| 62 | +/// Writes to the underlying socket in `SOCK_STREAM` mode are made with `MSG_NOSIGNAL` flag. |
| 63 | +/// This suppresses the emission of the `SIGPIPE` signal when writing to disconnected socket. |
| 64 | +/// In some cases getting a `SIGPIPE` would trigger process termination. |
44 | 65 | #[stable(feature = "unix_socket", since = "1.10.0")]
|
45 | 66 | pub struct UnixStream(pub(super) Socket);
|
46 | 67 |
|
@@ -633,7 +654,7 @@ impl io::Write for UnixStream {
|
633 | 654 | #[stable(feature = "unix_socket", since = "1.10.0")]
|
634 | 655 | impl<'a> io::Write for &'a UnixStream {
|
635 | 656 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
636 |
| - self.0.write(buf) |
| 657 | + self.0.send_with_flags(buf, MSG_NOSIGNAL) |
637 | 658 | }
|
638 | 659 |
|
639 | 660 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
|
0 commit comments