1 #[macro_use]
2 extern crate cfg_if;
3 #[cfg_attr(not(target_os = "redox"), macro_use)]
4 extern crate nix;
5 #[macro_use]
6 extern crate lazy_static;
7 
8 mod common;
9 mod sys;
10 #[cfg(not(target_os = "redox"))]
11 mod test_dir;
12 mod test_fcntl;
13 #[cfg(any(target_os = "android",
14           target_os = "linux"))]
15 mod test_kmod;
16 #[cfg(target_os = "freebsd")]
17 mod test_nmount;
18 #[cfg(any(target_os = "dragonfly",
19           target_os = "freebsd",
20           target_os = "fushsia",
21           target_os = "linux",
22           target_os = "netbsd"))]
23 mod test_mq;
24 #[cfg(not(target_os = "redox"))]
25 mod test_net;
26 mod test_nix_path;
27 mod test_resource;
28 mod test_poll;
29 #[cfg(not(any(target_os = "redox", target_os = "fuchsia")))]
30 mod test_pty;
31 #[cfg(any(target_os = "android",
32           target_os = "linux"))]
33 mod test_sched;
34 #[cfg(any(target_os = "android",
35           target_os = "freebsd",
36           target_os = "ios",
37           target_os = "linux",
38           target_os = "macos"))]
39 mod test_sendfile;
40 mod test_stat;
41 mod test_time;
42 mod test_unistd;
43 
44 use std::os::unix::io::RawFd;
45 use std::path::PathBuf;
46 use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
47 use nix::unistd::{chdir, getcwd, read};
48 
49 
50 /// Helper function analogous to `std::io::Read::read_exact`, but for `RawFD`s
read_exact(f: RawFd, buf: &mut [u8])51 fn read_exact(f: RawFd, buf: &mut  [u8]) {
52     let mut len = 0;
53     while len < buf.len() {
54         // get_mut would be better than split_at_mut, but it requires nightly
55         let (_, remaining) = buf.split_at_mut(len);
56         len += read(f, remaining).unwrap();
57     }
58 }
59 
60 lazy_static! {
61     /// Any test that changes the process's current working directory must grab
62     /// the RwLock exclusively.  Any process that cares about the current
63     /// working directory must grab it shared.
64     pub static ref CWD_LOCK: RwLock<()> = RwLock::new(());
65     /// Any test that creates child processes must grab this mutex, regardless
66     /// of what it does with those children.
67     pub static ref FORK_MTX: Mutex<()> = Mutex::new(());
68     /// Any test that changes the process's supplementary groups must grab this
69     /// mutex
70     pub static ref GROUPS_MTX: Mutex<()> = Mutex::new(());
71     /// Any tests that loads or unloads kernel modules must grab this mutex
72     pub static ref KMOD_MTX: Mutex<()> = Mutex::new(());
73     /// Any test that calls ptsname(3) must grab this mutex.
74     pub static ref PTSNAME_MTX: Mutex<()> = Mutex::new(());
75     /// Any test that alters signal handling must grab this mutex.
76     pub static ref SIGNAL_MTX: Mutex<()> = Mutex::new(());
77 }
78 
79 /// RAII object that restores a test's original directory on drop
80 struct DirRestore<'a> {
81     d: PathBuf,
82     _g: RwLockWriteGuard<'a, ()>
83 }
84 
85 impl<'a> DirRestore<'a> {
new() -> Self86     fn new() -> Self {
87         let guard = crate::CWD_LOCK.write();
88         DirRestore{
89             _g: guard,
90             d: getcwd().unwrap(),
91         }
92     }
93 }
94 
95 impl<'a> Drop for DirRestore<'a> {
drop(&mut self)96     fn drop(&mut self) {
97         let r = chdir(&self.d);
98         if std::thread::panicking() {
99             r.unwrap();
100         }
101     }
102 }
103