posix_socket/lib.rs
1//! Thin wrapper around POSIX sockets.
2//!
3//! The standard library sockets are good for dealing with TCP, UDP and Unix streaming and datagram sockets.
4//! However, for other sockets, you will get no help from the standard library.
5//!
6//! Additionally, the standard library sockets don't always expose all underlying features of the sockets.
7//! For example, you can not send file descriptors over the standard library sockets without resorting to `libc`.
8//!
9//! This library intends to expose the POSIX socket API to Rust without cutting features.
10//! It is currently still a work in progress.
11
12mod address;
13pub use address::*;
14
15mod socket;
16pub use socket::*;
17
18pub mod ancillary;
19
20#[cfg(fceature = "mio")]
21pub mod mio;
22
23pub type UnixSocket = Socket<UnixSocketAddress>;
24pub type Inet4Socket = Socket<Inet4SocketAddress>;
25pub type Inet6Socket = Socket<Inet6SocketAddress>;
26
27/// Disable SIGPIPE for the current process.
28///
29/// Writing to a closed socket may cause a SIGPIPE to be sent to the process (depending on the socket type).
30/// On most platforms this is prevented, either by using the `MSG_NOSIGNAL` flag when writing
31/// or by setting the `SO_NOSIGPIPE` socket option.
32///
33/// However, if a platform does not support `MSG_NOSIGNAL` or `SO_NOGSISPIPE`,
34/// the signal needs to be handled or the process will be terminated by the kernel.
35/// Calling [`disable_sigpipe()`] make sure the signal is ignored without terminating the process.
36pub fn disable_sigpipe() -> std::io::Result<()> {
37 unsafe {
38 if libc::signal(libc::SIGPIPE, libc::SIG_IGN) == libc::SIG_ERR {
39 Err(std::io::Error::last_os_error())
40 } else {
41 Ok(())
42 }
43 }
44}