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