Debian Codegraph @

third_party/rust/nix/test/test.rs

raw
1 #[macro_use]
2 extern crate cfg_if;
3 #[cfg_attr(not(any(target_os = "redox")), 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(
17 freebsdlike,
18 all(target_os = "linux", not(target_env = "ohos")),
19 target_os = "netbsd"
20 ))]
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(
27 target_os = "redox",
28 target_os = "fuchsia",
29 target_os = "haiku"
30 )))]
31 mod test_pty;
32 #[cfg(any(
33 linux_android,
34 target_os = "dragonfly",
35 all(target_os = "freebsd", fbsd14),
36 ))]
37 mod test_sched;
38 #[cfg(any(linux_android, freebsdlike, apple_targets, solarish))]
39 mod test_sendfile;
40 #[cfg(any(
41 target_os = "freebsd",
42 target_os = "haiku",
43 target_os = "linux",
44 target_os = "netbsd",
45 apple_targets
46 ))]
47 mod test_spawn;
48
49 mod test_syslog;
50
51 mod test_time;
52 mod test_unistd;
53
54 use nix::unistd::{chdir, getcwd, read};
55 use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
56 use std::os::unix::io::AsFd;
57 use std::path::PathBuf;
58
59 /// Helper function analogous to `std::io::Read::read_exact`, but for `Fd`s
60 fn read_exact<Fd: AsFd>(f: Fd, buf: &mut [u8]) {
61 let mut len = 0;
62 while len < buf.len() {
63 // get_mut would be better than split_at_mut, but it requires nightly
64 let (_, remaining) = buf.split_at_mut(len);
65 len += read(&f, remaining).unwrap();
66 }
67 }
68
69 /// Any test that creates child processes or can be affected by child processes
70 /// must grab this mutex, regardless of what it does with those children.
71 ///
72 /// It must hold the mutex until the child processes are waited upon.
73 pub static FORK_MTX: Mutex<()> = Mutex::new(());
74 /// Any test that changes the process's current working directory must grab
75 /// the RwLock exclusively. Any process that cares about the current
76 /// working directory must grab it shared.
77 pub static CWD_LOCK: RwLock<()> = RwLock::new(());
78 /// Any test that changes the process's supplementary groups must grab this
79 /// mutex
80 pub static GROUPS_MTX: Mutex<()> = Mutex::new(());
81 /// Any tests that loads or unloads kernel modules must grab this mutex
82 pub static KMOD_MTX: Mutex<()> = Mutex::new(());
83 /// Any test that calls ptsname(3) must grab this mutex.
84 pub static PTSNAME_MTX: Mutex<()> = Mutex::new(());
85 /// Any test that alters signal handling must grab this mutex.
86 pub static SIGNAL_MTX: Mutex<()> = Mutex::new(());
87
88 /// RAII object that restores a test's original directory on drop
89 struct DirRestore<'a> {
90 d: PathBuf,
91 _g: RwLockWriteGuard<'a, ()>,
92 }
93
94 impl DirRestore<'_> {
95 fn new() -> Self {
96 let guard = crate::CWD_LOCK.write();
97 DirRestore {
98 _g: guard,
99 d: getcwd().unwrap(),
100 }
101 }
102 }
103
104 impl Drop for DirRestore<'_> {
105 fn drop(&mut self) {
106 let r = chdir(&self.d);
107 if std::thread::panicking() {
108 r.unwrap();
109 }
110 }
111 }