1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// This file is part of file-descriptors. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/file-descriptors/master/COPYRIGHT. No part of file-descriptors, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2018-2019 The developers of file-descriptors. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/file-descriptors/master/COPYRIGHT.


#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![deny(missing_docs)]
#![deny(unreachable_patterns)]
#![feature(core_intrinsics)]
#![feature(read_initializer)]
#![feature(try_from)]


//! #file-descriptors
//!
//! file-descriptors is a Rust crate wrapping the various kinds of file descriptors with safe abstractions, including IPv4 / IPv6 sockets, Unix domain sockets, epoll, timerfd, signalfd, eventfd, POSIX message queues, pipes, FIFOs, terminals (and serial ports), character devices, inotify, fanotify and Files.
//!
//! There is a particularly extensive and safe wrapper for signals and terminals.
//!
//!
//! ## Supported File Descriptors
//!
//! * character devices.
//! * epoll.
//! * eventfd.
//! * fanotify.
//! * inotify.
//! * POSIX message queues (<(https://linux.die.net/man/7/mq_overview>).
//! * pipes and FIFOs (anonymous and named FIFOs), including support for splice, vmsplice and tee.
//! * signalfd.
//! * sockets (TCP, UDP and the equivalent over Unix Domain Sockets; sendfile supported).
//! * terminals (serial ports and modems).
//! * timerfd.
//!
//! Additionally, extensions (`SendFile`, `SpliceRecipient` and `SpliceSender`) are implemented for Rust's `File`.
//!
//!
//! ## Unix Domain Sockets
//!
//!
//! ### When using file paths
//!
//! * Every effort is made to create the socket file path cleanly;
//! * To make sure all parent folders exist;
//! * To make sure parent folder permissions are correctly set;
//! * To remove any stale files;
//! * To remove socket file paths on drop (close).
//!
//! The above features may not work correctly after the use of `seccomp` to lock down system calls (particularly the attempt to delete a socket file path on close).
//!
//!
//! ## Pipes
//!
//! * The use of `splice()`, `vmsplice()` and `tee()` are supported for all file descriptors where possible (including Rust's `::std::fs::File`).
//! * To be able to use epoll with standard in (`stdin`), use `pipes_and_fifos::ReceivePipeFileDescriptor::standard_in()`.
//! * To be able to use epoll with standard out (`stdout`), use `pipes_and_fifos::SendPipeFileDescriptor::standard_out()`.
//! * To be able to use epoll with standard error (`stderr`), use `pipes_and_fifos::SendPipeFileDescriptor::standard_error()`.
//!
//!
//! ## Unsupported for now
//!
//! * Linux zero copy send (`MSG_ZEROCOPY`) and receive (`SO_ZEROCOPY`), mostly because they have a horrible, hacky API.
//! * `SO_BUSY_POLL` and `SO_INCOMING_CPU`.
//! * Unix Domain Sockets using `autobind`; setting of the `SO_PASSCRED` socket option.
//! * Receiving credentials over Unix Domain Sockets using `recvmsg()`.
//! * `mkfifo()`.
//! * `mknod()`.
//! * `copy_file_range()`.
//! * infiniband sockets.
//! * canbus (SocketCAN sockets and can4linux <http://can-wiki.info/can4linux/man/can4linux_8h_source.html> character device drivers).
//!
//! ## Licensing
//!
//! The license for this project is MIT.


#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux", target_os = "solaris", target_env = "uclibc"))] extern crate arrayvec;
#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux", target_os = "solaris", target_env = "uclibc"))] #[macro_use] extern crate bitflags;
#[macro_use] extern crate cfg_if;
#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux", target_os = "solaris", target_env = "uclibc"))] extern crate errno;
#[cfg(unix)] extern crate libc;
#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux", target_os = "solaris", target_env = "uclibc"))] #[macro_use] extern crate likely;
#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux", target_os = "solaris", target_env = "uclibc"))] extern crate memchr;
#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux", target_os = "solaris", target_env = "uclibc"))] extern crate strum;
#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux", target_os = "solaris", target_env = "uclibc"))] #[macro_use] extern crate strum_macros;

cfg_if!
{
	if #[cfg(unix)]
	{
		use ::libc::close;
		use ::std::os::unix::io::RawFd;
		include!("RawFdExt.rs");
	}
}

cfg_if!
{
	if #[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux", target_os = "solaris", target_env = "uclibc"))]
	{
		use self::terminal::TerminalSettingsError;
		use ::arrayvec::Array;
		use ::arrayvec::ArrayVec;
		use ::errno::errno;
		use ::errno::Errno;
		use ::libc::c_char;
		use ::libc::c_int;
		use ::libc::c_void;
		use ::std::ffi::CStr;
		use ::libc::EACCES;
		use ::libc::EAGAIN;
		use ::libc::EBADF;
		use ::libc::ECANCELED;
		use ::libc::ECONNREFUSED;
		use ::libc::ECONNRESET;
		use ::libc::EDESTADDRREQ;
		use ::libc::EDQUOT;
		use ::libc::EEXIST;
		use ::libc::EFAULT;
		use ::libc::EFBIG;
		use ::libc::EISCONN;
		use ::libc::EINTR;
		use ::libc::EINVAL;
		use ::libc::EIO;
		use ::libc::EISDIR;
		use ::libc::ELOOP;
		use ::libc::EMFILE;
		use ::libc::EMSGSIZE;
		use ::libc::ENFILE;
		use ::libc::ENODEV;
		use ::libc::ENOBUFS;
		use ::libc::ENOENT;
		use ::libc::ENOMEM;
		use ::libc::ENOSPC;
		use ::libc::ENOSYS;
		use ::libc::ENOTCONN;
		use ::libc::ENOTDIR;
		use ::libc::ENOTSOCK;
		use ::libc::EOPNOTSUPP;
		use ::libc::EPERM;
		use ::libc::EPIPE;
		use ::libc::getpid;
		use ::libc::O_CLOEXEC;
		use ::libc::O_NONBLOCK;
		use ::libc::O_RDONLY;
		use ::libc::O_RDWR;
		use ::libc::O_WRONLY;
		use ::libc::int32_t;
		use ::libc::iovec;
		use ::libc::pid_t;
		use ::libc::read;
		use ::libc::readv;
		use ::libc::sigset_t;
		use ::libc::size_t;
		use ::libc::uint8_t;
		use ::libc::uint16_t;
		use ::libc::uint32_t;
		use ::libc::uint64_t;
		use ::libc::write;
		use ::libc::writev;
		use ::memchr::memchr;
		use ::std::cmp::Ordering;
		use ::std::hash::Hash;
		use ::std::hash::Hasher;
		use ::std::error;
		use ::std::fmt;
		use ::std::fmt::Debug;
		use ::std::fmt::Display;
		use ::std::fmt::Formatter;
		use ::std::fs::File;
		use ::std::io;
		use ::std::io::BufRead;
		use ::std::io::BufReader;
		use ::std::io::ErrorKind;
		use ::std::io::Initializer;
		use ::std::io::Read;
		use ::std::io::Write;
		use ::std::mem::size_of;
		use ::std::mem::transmute;
		use ::std::mem::uninitialized;
		use ::std::mem::zeroed;
		use ::std::os::unix::ffi::OsStrExt;
		use ::std::os::unix::io::AsRawFd;
		use ::std::os::unix::io::FromRawFd;
		use ::std::os::unix::io::IntoRawFd;
		use ::std::path::Path;
		use ::std::ptr::null;
		use ::std::ptr::null_mut;
		use ::std::str::from_utf8;


		/// Character device file descriptors.
		pub mod character_device;


		/// EPoll file descriptors.
		pub mod epoll;


		/// Event file descriptors.
		pub mod eventfd;


		#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux"))]
		/// inotify file descriptors.
		pub mod inotify;


		#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux"))]
		/// POSIX message queue file descriptors.
		pub mod posix_message_queues;


		/// Anonymous and named, connected unidirectional pipes_and_fifos (act like TCP connected sockets).
		///
		/// Since Linux 2.6.35, the default pipe capacity is 16 pages (which are 4096 bytes on x86-64), but the capacity can be queried and set using the `fcntl()` `F_GETPIPE_SZ` and `F_SETPIPE_SZ` operations.
		///
		/// The unread bytes in a pipe can be obtained using the `fcntl()` operation `FIONREAD`.
		///
		/// The maximum capacity that can be set for a non-privileged process (one without the `CAP_SYS_RESOURCE` capability) is specified in the file `/proc/sys/fs/pipe-max-size`; it defaults to 1Mb.
		///
		/// Writes of less than `PIPE_BUF` bytes are atomic; `PIPE_BUF` is 4096 bytes on Linux.
		///
		/// Atomic writes are significant when there are many writers sharing one named pipe (FIFO).
		pub mod pipes_and_fifos;


		#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux"))]
		/// fanotify file descriptors.
		pub mod fanotify;


		/// File descriptor information obtained from `/proc`.
		pub mod file_descriptor_information;


		/// Additional support for using `sendfile()` with Rust's File and our sockets.
		#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux"))]
		pub mod sendfile;


		/// Signal file descriptors.
		pub mod signalfd;


		#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuschia", target_os = "linux"))]
		/// Socket file descriptors.
		pub mod socket;


		/// Terminal (serial port and modem) file descriptors.
		///
		/// Create an instance of `TerminalFileDescriptor` to get started with a terminal or serial port.
		pub mod terminal;


		/// Timer file descriptors.
		pub mod timerfd;


		include!("CreationError.rs");
		include!("InvalidPathReason.rs");
		include!("path_bytes_without_trailing_nul.rs");
		include!("SpecialFileOpenError.rs");
		include!("StructReadError.rs");
		include!("StructWriteError.rs");
		include!("VectoredRead.rs");
		include!("VectoredWrite.rs");
	}
}