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#[cfg(feature="xio-rs")]
50pub extern crate xio_rs;
51
52#[cfg(target_family = "unix")]
53extern crate libc;
54
55#[cfg(target_family = "windows")]
56extern crate windows_sys;
57
58#[cfg(target_family = "windows")]
59extern crate tempfile;
60
61/// Get errno as io::Error on -1.
62macro_rules! cvt {($syscall:expr) => {
63    match $syscall 
64    {
65        -1 => Err(io::Error::last_os_error()),
66        ok => Ok(ok),
67    }
68}}
69
70/// Get errno as io::Error on -1 and retry on EINTR.
71macro_rules! cvt_r {($syscall:expr) => {
72    loop 
73    {
74        let result = $syscall;
75        if result != -1 
76        {
77            break Ok(result);
78        }
79
80        let err = io::Error::last_os_error();
81
82        if err.kind() != std::io::ErrorKind::Interrupted 
83        {
84            break Err(err);
85        }
86    }
87}}
88
89#[cfg(target_family = "windows")]
90mod windows_unixstream;
91
92mod addr;
93
94#[cfg(feature = "unsatable_preview")]
95mod ancillary_rust;
96
97#[cfg(target_family = "unix")]
98mod credentials;
99
100#[cfg(target_family = "unix")]
101mod helpers;
102
103#[cfg(target_family = "unix")]
104mod ancillary;
105
106#[cfg(target_family = "unix")]
107mod traits;
108
109#[cfg(target_family = "unix")]
110mod seqpacket;
111
112
113pub(crate) const LISTEN_BACKLOG: std::ffi::c_int = 128;//10; // what std uses, I think
114
115
116pub use addr::{UnixSocketAddr, UnixSocketAddrRef, AddrName};
117
118#[cfg(windows)]
119pub use windows_unixstream::{WindowsUnixListener, WindowsUnixStream, RecvFlags};
120
121#[cfg(windows)]
122pub use windows_unixstream::{get_socket_family, get_socket_type};
123
124#[cfg(target_family = "unix")]
125pub use ancillary::{AncillaryBuf, Ancillary, AncillaryItem};
126
127#[cfg(target_family = "unix")]
128pub use traits::{UnixListenerExt, UnixStreamExt, UnixDatagramExt};
129
130#[cfg(target_family = "unix")]
131pub use seqpacket::{UnixSeqpacketListener, UnixSeqpacketConn};
132
133#[cfg(target_family = "unix")]
134pub use credentials::ConnCredentials;
135
136#[cfg(target_family = "unix")]
137pub mod nonblocking {
138    pub use crate::seqpacket::NonblockingUnixSeqpacketListener as UnixSeqpacketListener;
139    pub use crate::seqpacket::NonblockingUnixSeqpacketConn as UnixSeqpacketConn;
140}
141
142#[cfg(debug_assertions)]
143mod doctest_md_files {
144    macro_rules! mdfile {($content:expr, $(#[$meta:meta])* $attach_to:ident) => {
145        #[doc=$content]
146        #[allow(unused)]
147        $(#[$meta])* // can't #[cfg_attr(, doc=)] in .md file
148        enum $attach_to {}
149    }}
150    mdfile!{
151        include_str!("../README.md"),
152        #[cfg(any(target_os="linux", target_os="android"))] // uses abstract addrs
153        Readme
154    }
155}