#[macro_use]
extern crate cfg_if;
#[cfg_attr(not(target_os = "redox"), macro_use)]
extern crate nix;
#[macro_use]
extern crate lazy_static;
mod common;
mod sys;
#[cfg(not(target_os = "redox"))]
mod test_dir;
mod test_fcntl;
#[cfg(any(target_os = "android",
target_os = "linux"))]
mod test_kmod;
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "fushsia",
target_os = "linux",
target_os = "netbsd"))]
mod test_mq;
#[cfg(not(target_os = "redox"))]
mod test_net;
mod test_nix_path;
mod test_poll;
#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))]
mod test_pty;
#[cfg(any(target_os = "android",
target_os = "linux"))]
mod test_sched;
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
mod test_sendfile;
mod test_stat;
mod test_time;
mod test_unistd;
use std::os::unix::io::RawFd;
use std::path::PathBuf;
use std::sync::{Mutex, RwLock, RwLockWriteGuard};
use nix::unistd::{chdir, getcwd, read};
fn read_exact(f: RawFd, buf: &mut [u8]) {
let mut len = 0;
while len < buf.len() {
let (_, remaining) = buf.split_at_mut(len);
len += read(f, remaining).unwrap();
}
}
lazy_static! {
pub static ref CWD_LOCK: RwLock<()> = RwLock::new(());
pub static ref FORK_MTX: Mutex<()> = Mutex::new(());
pub static ref GROUPS_MTX: Mutex<()> = Mutex::new(());
pub static ref KMOD_MTX: Mutex<()> = Mutex::new(());
pub static ref PTSNAME_MTX: Mutex<()> = Mutex::new(());
pub static ref SIGNAL_MTX: Mutex<()> = Mutex::new(());
}
struct DirRestore<'a> {
d: PathBuf,
_g: RwLockWriteGuard<'a, ()>
}
impl<'a> DirRestore<'a> {
fn new() -> Self {
let guard = crate::CWD_LOCK.write()
.expect("Lock got poisoned by another test");
DirRestore{
_g: guard,
d: getcwd().unwrap(),
}
}
}
impl<'a> Drop for DirRestore<'a> {
fn drop(&mut self) {
let r = chdir(&self.d);
if std::thread::panicking() {
r.unwrap();
}
}
}