Skip to main content

uds_fork/
lib.rs

1/* Copyright (c) 2019-2023 Torbjørn Birch Moltu, 2020 Jon Magnuson, 2022 Jake Shadle, 2022 David Carlier, 2023 Dominik Maier
2 *
3 * Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4 * http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5 * http://opensource.org/licenses/MIT>, at your option. This file may not be
6 * copied, modified, or distributed except according to those terms.
7 */
8
9//! A unix domain sockets library that supports abstract addresses, fd-passing,
10//! SOCK_SEQPACKET sockets and more.
11//!
12//! File-descriptor passing and abstract socket support
13//! for stream and datagram sockets is provided via extension traits for
14//! existing types.
15//! 
16//! Also it support UnixStream for Windows i.e AF_INET SOCK_STREAM with some limitations.
17//!
18//! See README for status of operating system support and other general info.
19//! 
20//! In seqpacket.rs a [UnixSeqpacketConn]:
21//! 
22//! If the feature `unsatable_preview` is enabled, a `send_vectored_with_ancillary_to`, 
23//! `send_vectored_with_ancillary`, `recv_vectored_with_ancillary_from`,
24//! `recv_vectored_with_ancillary` are enabled.
25//! 
26//! A legacy (method provided not by std but this crate) is `recv_vectored_ancillary`.
27
28#![cfg_attr(feature = "unsatable_preview", feature(unix_socket_ancillary_data))]
29
30
31// Too many features unavailable on solarish to bother cfg()ing individually.
32#![cfg_attr(any(target_os="illumos", target_os="solaris", target_os="windows"), allow(unused))]
33
34#![allow(
35    clippy::cast_lossless, // improves portability when values are limited by the OS anyway
36    clippy::unnecessary_cast, // not necessarily unnecessary on other OSes
37    clippy::len_zero, // the `!` in `if !foo.is_empty()` can be easy to miss
38    clippy::needless_return, // consistency with early returns, and to not look incomplete
39    clippy::redundant_closure, // avoiding auto-functions of tuple structs and enum variants
40    clippy::needless_lifetimes, // explicity when useful
41    clippy::needless_borrow, // dereferencing one field from a raw pointer
42    clippy::bool_to_int_with_if, // clearer than usize::from()
43    // more lints are disabled inside ancillary.rs and credentials.rs
44)]
45
46#[cfg(feature="mio")]
47pub extern crate mio;
48
49//temporary disabled until xio fix comilation errors on netbsd
50/*#[cfg(feature="xio-rs")]
51pub extern crate xio_rs;*/
52
53#[cfg(target_family = "unix")]
54extern crate libc;
55
56#[cfg(target_family = "windows")]
57extern crate windows_sys;
58
59#[cfg(target_family = "windows")]
60extern crate tempfile;
61
62/// Get errno as io::Error on -1.
63macro_rules! cvt {($syscall:expr) => {
64    match $syscall 
65    {
66        -1 => Err(io::Error::last_os_error()),
67        ok => Ok(ok),
68    }
69}}
70
71/// Get errno as io::Error on -1 and retry on EINTR.
72macro_rules! cvt_r {($syscall:expr) => {
73    loop 
74    {
75        let result = $syscall;
76        if result != -1 
77        {
78            break Ok(result);
79        }
80
81        let err = io::Error::last_os_error();
82
83        if err.kind() != std::io::ErrorKind::Interrupted 
84        {
85            break Err(err);
86        }
87    }
88}}
89
90#[cfg(target_family = "windows")]
91mod windows_unixstream;
92
93mod addr;
94
95#[cfg(feature = "unsatable_preview")]
96mod ancillary_rust;
97
98#[cfg(target_family = "unix")]
99mod credentials;
100
101#[cfg(target_family = "unix")]
102mod helpers;
103
104#[cfg(target_family = "unix")]
105mod ancillary;
106
107#[cfg(target_family = "unix")]
108mod traits;
109
110#[cfg(target_family = "unix")]
111mod seqpacket;
112
113
114pub(crate) const LISTEN_BACKLOG: std::ffi::c_int = 128;//10; // what std uses, I think
115
116
117pub use addr::{UnixSocketAddr, UnixSocketAddrRef, AddrName};
118
119#[cfg(windows)]
120pub use windows_unixstream::{WindowsUnixListener, WindowsUnixStream, RecvFlags};
121
122#[cfg(windows)]
123pub use windows_unixstream::{get_socket_family, get_socket_type};
124
125#[cfg(target_family = "unix")]
126pub use ancillary::{AncillaryBuf, Ancillary, AncillaryItem};
127
128#[cfg(target_family = "unix")]
129pub use traits::{UnixListenerExt, UnixStreamExt, UnixDatagramExt};
130
131#[cfg(target_family = "unix")]
132pub use seqpacket::{UnixSeqpacketListener, UnixSeqpacketConn};
133
134#[cfg(target_family = "unix")]
135pub use credentials::ConnCredentials;
136
137#[cfg(target_family = "unix")]
138pub mod nonblocking {
139    pub use crate::seqpacket::NonblockingUnixSeqpacketListener as UnixSeqpacketListener;
140    pub use crate::seqpacket::NonblockingUnixSeqpacketConn as UnixSeqpacketConn;
141}
142
143#[cfg(debug_assertions)]
144mod doctest_md_files {
145    macro_rules! mdfile {($content:expr, $(#[$meta:meta])* $attach_to:ident) => {
146        #[doc=$content]
147        #[allow(unused)]
148        $(#[$meta])* // can't #[cfg_attr(, doc=)] in .md file
149        enum $attach_to {}
150    }}
151    mdfile!{
152        include_str!("../README.md"),
153        #[cfg(any(target_os="linux", target_os="android"))] // uses abstract addrs
154        Readme
155    }
156}