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 in `std::os::unix::net` and from [mio](https://github.com/tokio-rs/mio)
15//! (the latter is opt-in and must be enabled with `features=["mio_08"]` in Cargo.toml).
16//!
17//! See README for status of operating system support and other general info.
18
19//#![cfg(unix)] // compile as empty crate on windows
20
21// Too many features unavailable on solarish to bother cfg()ing individually.
22#![cfg_attr(any(target_os="illumos", target_os="solaris", target_os="windows"), allow(unused))]
23
24#![allow(
25    clippy::cast_lossless, // improves portability when values are limited by the OS anyway
26    clippy::unnecessary_cast, // not necessarily unnecessary on other OSes
27    clippy::len_zero, // the `!` in `if !foo.is_empty()` can be easy to miss
28    clippy::needless_return, // consistency with early returns, and to not look incomplete
29    clippy::redundant_closure, // avoiding auto-functions of tuple structs and enum variants
30    clippy::needless_lifetimes, // explicity when useful
31    clippy::needless_borrow, // dereferencing one field from a raw pointer
32    clippy::bool_to_int_with_if, // clearer than usize::from()
33    // more lints are disabled inside ancillary.rs and credentials.rs
34)]
35
36#[cfg(target_family = "unix")]
37extern crate libc;
38
39#[cfg(target_family = "windows")]
40extern crate windows_sys;
41
42/// Get errno as io::Error on -1.
43macro_rules! cvt {($syscall:expr) => {
44    match $syscall 
45    {
46        -1 => Err(io::Error::last_os_error()),
47        ok => Ok(ok),
48    }
49}}
50
51/// Get errno as io::Error on -1 and retry on EINTR.
52macro_rules! cvt_r {($syscall:expr) => {
53    loop 
54    {
55        let result = $syscall;
56        if result != -1 
57        {
58            break Ok(result);
59        }
60
61        let err = io::Error::last_os_error();
62
63        if err.kind() != std::io::ErrorKind::Interrupted 
64        {
65            break Err(err);
66        }
67    }
68}}
69
70#[cfg(target_family = "windows")]
71mod windows_unixstream;
72
73mod addr;
74
75#[cfg(target_family = "unix")]
76mod credentials;
77
78#[cfg(target_family = "unix")]
79mod helpers;
80
81#[cfg(target_family = "unix")]
82mod ancillary;
83
84#[cfg(target_family = "unix")]
85mod traits;
86
87#[cfg(target_family = "unix")]
88mod seqpacket;
89
90
91pub(crate) const LISTEN_BACKLOG: std::ffi::c_int = 128;//10; // what std uses, I think
92
93
94pub use addr::{UnixSocketAddr, UnixSocketAddrRef, AddrName};
95
96#[cfg(windows)]
97pub use windows_unixstream::{WindowsUnixListener, WindowsUnixStream, RecvFlags};
98
99#[cfg(windows)]
100pub use windows_unixstream::{get_socket_family, get_socket_type};
101
102#[cfg(target_family = "unix")]
103pub use traits::{UnixListenerExt, UnixStreamExt, UnixDatagramExt};
104
105#[cfg(target_family = "unix")]
106pub use seqpacket::{UnixSeqpacketListener, UnixSeqpacketConn};
107
108#[cfg(target_family = "unix")]
109pub use credentials::ConnCredentials;
110
111#[cfg(target_family = "unix")]
112pub mod nonblocking {
113    pub use crate::seqpacket::NonblockingUnixSeqpacketListener as UnixSeqpacketListener;
114    pub use crate::seqpacket::NonblockingUnixSeqpacketConn as UnixSeqpacketConn;
115}
116
117#[cfg(debug_assertions)]
118mod doctest_md_files {
119    macro_rules! mdfile {($content:expr, $(#[$meta:meta])* $attach_to:ident) => {
120        #[doc=$content]
121        #[allow(unused)]
122        $(#[$meta])* // can't #[cfg_attr(, doc=)] in .md file
123        enum $attach_to {}
124    }}
125    mdfile!{
126        include_str!("../README.md"),
127        #[cfg(any(target_os="linux", target_os="android"))] // uses abstract addrs
128        Readme
129    }
130}