File: test.rs

package info (click to toggle)
rust-nix 0.30.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,248 kB
  • sloc: ansic: 18; makefile: 7
file content (118 lines) | stat: -rw-r--r-- 3,192 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#[macro_use]
extern crate cfg_if;
#[cfg_attr(not(any(target_os = "redox")), macro_use)]
extern crate nix;

#[macro_use]
mod common;
mod mount;
mod sys;
#[cfg(not(target_os = "redox"))]
mod test_dir;
mod test_errno;
mod test_fcntl;
#[cfg(linux_android)]
mod test_kmod;
#[cfg(any(
    freebsdlike,
    all(target_os = "linux", not(target_env = "ohos")),
    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",
    target_os = "haiku"
)))]
mod test_pty;
#[cfg(any(
    linux_android,
    target_os = "dragonfly",
    all(target_os = "freebsd", fbsd14),
))]
mod test_sched;
#[cfg(any(linux_android, freebsdlike, apple_targets, solarish))]
mod test_sendfile;
#[cfg(any(
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "linux",
    target_os = "netbsd",
    apple_targets
))]
mod test_spawn;

mod test_syslog;

mod test_time;
mod test_unistd;

#[cfg(feature = "fs")]
use nix::unistd::{chdir, getcwd};
#[cfg(any(feature = "fs",feature = "term"))]
use nix::unistd::read;
use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
use std::os::unix::io::AsFd;
use std::path::PathBuf;

/// Helper function analogous to `std::io::Read::read_exact`, but for `Fd`s
#[cfg(any(feature = "fs",feature = "term"))]
fn read_exact<Fd: AsFd>(f: Fd, buf: &mut [u8]) {
    let mut len = 0;
    while len < buf.len() {
        // get_mut would be better than split_at_mut, but it requires nightly
        let (_, remaining) = buf.split_at_mut(len);
        len += read(&f, remaining).unwrap();
    }
}

/// Any test that creates child processes or can be affected by child processes
/// must grab this mutex, regardless of what it does with those children.
///
/// It must hold the mutex until the child processes are waited upon.
pub static FORK_MTX: Mutex<()> = Mutex::new(());
/// Any test that changes the process's current working directory must grab
/// the RwLock exclusively.  Any process that cares about the current
/// working directory must grab it shared.
pub static CWD_LOCK: RwLock<()> = RwLock::new(());
/// Any test that changes the process's supplementary groups must grab this
/// mutex
pub static GROUPS_MTX: Mutex<()> = Mutex::new(());
/// Any tests that loads or unloads kernel modules must grab this mutex
pub static KMOD_MTX: Mutex<()> = Mutex::new(());
/// Any test that calls ptsname(3) must grab this mutex.
pub static PTSNAME_MTX: Mutex<()> = Mutex::new(());
/// Any test that alters signal handling must grab this mutex.
pub static SIGNAL_MTX: Mutex<()> = Mutex::new(());

/// RAII object that restores a test's original directory on drop
#[cfg(feature = "fs")]
struct DirRestore<'a> {
    d: PathBuf,
    _g: RwLockWriteGuard<'a, ()>,
}

#[cfg(feature = "fs")]
impl DirRestore<'_> {
    fn new() -> Self {
        let guard = crate::CWD_LOCK.write();
        DirRestore {
            _g: guard,
            d: getcwd().unwrap(),
        }
    }
}

#[cfg(feature = "fs")]
impl Drop for DirRestore<'_> {
    fn drop(&mut self) {
        let r = chdir(&self.d);
        if std::thread::panicking() {
            r.unwrap();
        }
    }
}