Skip to content

Commit

Permalink
MSG_NOSIGNAL on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
kali committed Sep 28, 2016
1 parent 6f6e261 commit ed5e542
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/libstd/sys/common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ use sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP;
target_os = "solaris", target_os = "haiku")))]
use sys::net::netc::IPV6_DROP_MEMBERSHIP;

#[cfg(target_os = "linux")]
const MSG_NOSIGNAL: c_int = 0x4000;
#[cfg(not(target_os = "linux"))]
const MSG_NOSIGNAL: c_int = 0x0; // unused dummy value

////////////////////////////////////////////////////////////////////////////////
// sockaddr and misc bindings
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -221,11 +226,12 @@ impl TcpStream {

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
let flags = if cfg!(target_os = "linux") { MSG_NOSIGNAL } else { 0 };
let ret = cvt(unsafe {
c::send(*self.inner.as_inner(),
buf.as_ptr() as *const c_void,
len,
0)
flags)
})?;
Ok(ret as usize)
}
Expand Down Expand Up @@ -446,10 +452,11 @@ impl UdpSocket {
pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
let (dstp, dstlen) = dst.into_inner();
let flags = if cfg!(target_os = "linux") { MSG_NOSIGNAL } else { 0 };
let ret = cvt(unsafe {
c::sendto(*self.inner.as_inner(),
buf.as_ptr() as *const c_void, len,
0, dstp, dstlen)
flags, dstp, dstlen)
})?;
Ok(ret as usize)
}
Expand Down Expand Up @@ -569,11 +576,12 @@ impl UdpSocket {

pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
let flags = if cfg!(target_os = "linux") { MSG_NOSIGNAL } else { 0 };
let ret = cvt(unsafe {
c::send(*self.inner.as_inner(),
buf.as_ptr() as *const c_void,
len,
0)
flags)
})?;
Ok(ret as usize)
}
Expand Down
8 changes: 7 additions & 1 deletion src/libstd/sys/unix/ext/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ use sys::cvt;
use sys::net::Socket;
use sys_common::{AsInner, FromInner, IntoInner};

#[cfg(target_os = "linux")]
const MSG_NOSIGNAL: libc::c_int = 0x4000;
#[cfg(not(target_os = "linux"))]
const MSG_NOSIGNAL: libc::c_int = 0x0; // unused dummy value

fn sun_path_offset() -> usize {
unsafe {
// Work with an actual instance of the type since using a null pointer is UB
Expand Down Expand Up @@ -686,11 +691,12 @@ impl UnixDatagram {
fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
unsafe {
let (addr, len) = sockaddr_un(path)?;
let flags = if cfg!(target_os = "linux") { MSG_NOSIGNAL } else { 0 };

let count = cvt(libc::sendto(*d.0.as_inner(),
buf.as_ptr() as *const _,
buf.len(),
0,
flags,
&addr as *const _ as *const _,
len))?;
Ok(count as usize)
Expand Down

0 comments on commit ed5e542

Please sign in to comment.