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"), 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
36extern crate libc;
37
38/// Get errno as io::Error on -1.
39macro_rules! cvt {($syscall:expr) => {
40    match $syscall 
41    {
42        -1 => Err(io::Error::last_os_error()),
43        ok => Ok(ok),
44    }
45}}
46
47/// Get errno as io::Error on -1 and retry on EINTR.
48macro_rules! cvt_r {($syscall:expr) => {
49    loop 
50    {
51        let result = $syscall;
52        if result != -1 
53        {
54            break Ok(result);
55        }
56
57        let err = io::Error::last_os_error();
58
59        if err.kind() != std::io::ErrorKind::Interrupted 
60        {
61            break Err(err);
62        }
63    }
64}}
65
66mod addr;
67mod credentials;
68mod helpers;
69mod ancillary;
70mod traits;
71mod seqpacket;
72
73pub use addr::{UnixSocketAddr, UnixSocketAddrRef, AddrName};
74pub use traits::{UnixListenerExt, UnixStreamExt, UnixDatagramExt};
75pub use seqpacket::{UnixSeqpacketListener, UnixSeqpacketConn};
76pub use credentials::ConnCredentials;
77
78pub mod nonblocking {
79    pub use crate::seqpacket::NonblockingUnixSeqpacketListener as UnixSeqpacketListener;
80    pub use crate::seqpacket::NonblockingUnixSeqpacketConn as UnixSeqpacketConn;
81}
82
83#[cfg(debug_assertions)]
84mod doctest_md_files {
85    macro_rules! mdfile {($content:expr, $(#[$meta:meta])* $attach_to:ident) => {
86        #[doc=$content]
87        #[allow(unused)]
88        $(#[$meta])* // can't #[cfg_attr(, doc=)] in .md file
89        enum $attach_to {}
90    }}
91    mdfile!{
92        include_str!("../README.md"),
93        #[cfg(any(target_os="linux", target_os="android"))] // uses abstract addrs
94        Readme
95    }
96}