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