Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SO_NOSIGPIPE and MSG_NOSIGNAL (rebased #36426) #36824

Merged
merged 3 commits into from
Oct 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/liblibc
11 changes: 8 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")]
use libc::MSG_NOSIGNAL;
Copy link
Member

@nagisa nagisa Sep 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • DragonFlyBSD/NetBSD/OpenBSD/Bitrig: MSG_NOSIGNAL = 0x400;
  • FreeBSD MSG_NOSIGNAL = 0x20000;
  • Android MSG_NOSIGNAL = 0x4000;
  • Haiku MSG_NOSIGNAL = 0x0800;

Would be nice if those were included as well.

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

////////////////////////////////////////////////////////////////////////////////
// sockaddr and misc bindings
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -225,7 +230,7 @@ impl TcpStream {
c::send(*self.inner.as_inner(),
buf.as_ptr() as *const c_void,
len,
0)
MSG_NOSIGNAL)
})?;
Ok(ret as usize)
}
Expand Down Expand Up @@ -449,7 +454,7 @@ impl UdpSocket {
let ret = cvt(unsafe {
c::sendto(*self.inner.as_inner(),
buf.as_ptr() as *const c_void, len,
0, dstp, dstlen)
MSG_NOSIGNAL, dstp, dstlen)
})?;
Ok(ret as usize)
}
Expand Down Expand Up @@ -573,7 +578,7 @@ impl UdpSocket {
c::send(*self.inner.as_inner(),
buf.as_ptr() as *const c_void,
len,
0)
MSG_NOSIGNAL)
})?;
Ok(ret as usize)
}
Expand Down
7 changes: 6 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")]
use libc::MSG_NOSIGNAL;
#[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 @@ -690,7 +695,7 @@ impl UnixDatagram {
let count = cvt(libc::sendto(*d.0.as_inner(),
buf.as_ptr() as *const _,
buf.len(),
0,
MSG_NOSIGNAL,
&addr as *const _ as *const _,
len))?;
Ok(count as usize)
Expand Down
14 changes: 13 additions & 1 deletion src/libstd/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ use libc::SOCK_CLOEXEC;
#[cfg(not(target_os = "linux"))]
const SOCK_CLOEXEC: c_int = 0;

// Another conditional contant for name resolution: Macos et iOS use
// SO_NOSIGPIPE as a setsockopt flag to disable SIGPIPE emission on socket.
// Other platforms do otherwise.
#[cfg(target_vendor = "apple")]
use libc::SO_NOSIGPIPE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • FreeBSD SO_NOSIGPIPE = 0x0800

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Android is covered by target_os="linux", and the value is correct.

I don't have easy access to the other OS listed here. I can offer a "blind" untested patch, but I'm not really in a position to test the behaviour.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android is covered by target_os="linux",

To the best of my knowledge its not true, because value of target_os on Android is android, not linux.

Blind is good enough; better than feature disparity IMO.

#[cfg(not(target_vendor = "apple"))]
const SO_NOSIGPIPE: c_int = 0;

pub struct Socket(FileDesc);

pub fn init() {}
Expand Down Expand Up @@ -81,7 +89,11 @@ impl Socket {
let fd = cvt(libc::socket(fam, ty, 0))?;
let fd = FileDesc::new(fd);
fd.set_cloexec()?;
Ok(Socket(fd))
let socket = Socket(fd);
if cfg!(target_vendor = "apple") {
setsockopt(&socket, libc::SOL_SOCKET, SO_NOSIGPIPE, 1)?;
}
Ok(socket)
}
}

Expand Down