File: test_spawn.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 (196 lines) | stat: -rw-r--r-- 5,683 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#![cfg(feature = "process")]
use super::FORK_MTX;
use nix::errno::Errno;
use nix::spawn::{self, PosixSpawnAttr, PosixSpawnFileActions};
use nix::sys::signal;
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
use std::ffi::{CStr, CString};

/// Helper function to find a binary in the $PATH
fn which(exe_name: &str) -> Option<std::path::PathBuf> {
    std::env::var_os("PATH").and_then(|paths| {
        std::env::split_paths(&paths)
            .filter_map(|dir| {
                let full_path = dir.join(exe_name);
                if full_path.is_file() {
                    Some(full_path)
                } else {
                    None
                }
            })
            .next()
    })
}

#[test]
fn spawn_true() {
    let _guard = FORK_MTX.lock();

    let bin = which("true").unwrap();
    let args = &[
        CString::new("true").unwrap(),
        CString::new("story").unwrap(),
    ];
    let vars: &[CString] = &[];
    let actions = PosixSpawnFileActions::init().unwrap();
    let attr = PosixSpawnAttr::init().unwrap();

    let pid =
        spawn::posix_spawn(bin.as_path(), &actions, &attr, args, vars).unwrap();

    let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap();

    match status {
        WaitStatus::Exited(wpid, ret) => {
            assert_eq!(pid, wpid);
            assert_eq!(ret, 0);
        }
        _ => {
            panic!("Invalid WaitStatus");
        }
    };
}

#[test]
#[cfg(feature = "signal")]
fn spawn_sleep() {
    let _guard = FORK_MTX.lock();

    let bin = which("sleep").unwrap();
    let args = &[CString::new("sleep").unwrap(), CString::new("30").unwrap()];
    let vars: &[CString] = &[];
    let actions = PosixSpawnFileActions::init().unwrap();
    let attr = PosixSpawnAttr::init().unwrap();

    let pid =
        spawn::posix_spawn(bin.as_path(), &actions, &attr, args, vars).unwrap();

    let status =
        waitpid(pid, WaitPidFlag::from_bits(WaitPidFlag::WNOHANG.bits()))
            .unwrap();
    match status {
        WaitStatus::StillAlive => {}
        _ => {
            panic!("Invalid WaitStatus");
        }
    };

    signal::kill(pid, signal::SIGTERM).unwrap();

    let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap();
    match status {
        WaitStatus::Signaled(wpid, wsignal, _) => {
            assert_eq!(pid, wpid);
            assert_eq!(wsignal, signal::SIGTERM);
        }
        _ => {
            panic!("Invalid WaitStatus");
        }
    };
}

#[test]
// `posix_spawn(path_not_exist)` succeeds under QEMU, so ignore the test. No need
// to investigate the root cause, this test still works in native environments, which
// is sufficient to test the binding.
#[cfg_attr(qemu, ignore)]
fn spawn_cmd_does_not_exist() {
    let _guard = FORK_MTX.lock();

    let args = &[CString::new("buzz").unwrap()];
    let envs: &[CString] = &[];
    let actions = PosixSpawnFileActions::init().unwrap();
    let attr = PosixSpawnAttr::init().unwrap();

    let bin = "2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf";
    let errno =
        spawn::posix_spawn(bin, &actions, &attr, args, envs).unwrap_err();
    assert_eq!(errno, Errno::ENOENT);
}

#[test]
fn spawnp_true() {
    let _guard = FORK_MTX.lock();

    let bin = &CString::new("true").unwrap();
    let args = &[
        CString::new("true").unwrap(),
        CString::new("story").unwrap(),
    ];
    let vars: &[CString] = &[];
    let actions = PosixSpawnFileActions::init().unwrap();
    let attr = PosixSpawnAttr::init().unwrap();

    let pid = spawn::posix_spawnp(bin, &actions, &attr, args, vars).unwrap();

    let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap();

    match status {
        WaitStatus::Exited(wpid, ret) => {
            assert_eq!(pid, wpid);
            assert_eq!(ret, 0);
        }
        _ => {
            panic!("Invalid WaitStatus");
        }
    };
}

#[test]
#[cfg(feature = "signal")]
fn spawnp_sleep() {
    let _guard = FORK_MTX.lock();

    let bin = &CString::new("sleep").unwrap();
    let args = &[CString::new("sleep").unwrap(), CString::new("30").unwrap()];
    let vars: &[CString] = &[];
    let actions = PosixSpawnFileActions::init().unwrap();
    let attr = PosixSpawnAttr::init().unwrap();

    let pid = spawn::posix_spawnp(bin, &actions, &attr, args, vars).unwrap();

    let status =
        waitpid(pid, WaitPidFlag::from_bits(WaitPidFlag::WNOHANG.bits()))
            .unwrap();
    match status {
        WaitStatus::StillAlive => {}
        _ => {
            panic!("Invalid WaitStatus");
        }
    };

    signal::kill(pid, signal::SIGTERM).unwrap();

    let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap();
    match status {
        WaitStatus::Signaled(wpid, wsignal, _) => {
            assert_eq!(pid, wpid);
            assert_eq!(wsignal, signal::SIGTERM);
        }
        _ => {
            panic!("Invalid WaitStatus");
        }
    };
}

#[test]
// `posix_spawnp(bin_not_exist)` succeeds under QEMU, so ignore the test. No need
// to investigate the root cause, this test still works in native environments, which
// is sufficient to test the binding.
#[cfg_attr(qemu, ignore)]
fn spawnp_cmd_does_not_exist() {
    let _guard = FORK_MTX.lock();

    let args = &[CString::new("buzz").unwrap()];
    let envs: &[CString] = &[];
    let actions = PosixSpawnFileActions::init().unwrap();
    let attr = PosixSpawnAttr::init().unwrap();

    let bin = CStr::from_bytes_with_nul(
        "2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf\0".as_bytes(),
    )
    .unwrap();
    let errno =
        spawn::posix_spawnp(bin, &actions, &attr, args, envs).unwrap_err();
    assert_eq!(errno, Errno::ENOENT);
}