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