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