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