-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathpidfd.rs
More file actions
93 lines (81 loc) · 2.53 KB
/
Copy pathpidfd.rs
File metadata and controls
93 lines (81 loc) · 2.53 KB
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
//! Tests for the `pidfd` type.
use libc::{kill, SIGSTOP};
#[cfg(feature = "event")]
use rustix::event;
use rustix::fd::AsFd;
use rustix::{io, process};
use serial_test::serial;
use std::process::Command;
#[test]
#[serial]
fn test_pidfd_waitid() {
// Create a new process.
let child = Command::new("yes")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("failed to execute child");
// Create a pidfd for the child process.
let pid = process::Pid::from_child(&child);
let pidfd = match process::pidfd_open(pid, process::PidfdFlags::empty()) {
Ok(pidfd) => pidfd,
Err(io::Errno::NOSYS) => {
// The kernel does not support pidfds.
return;
}
Err(e) => panic!("failed to open pidfd: {}", e),
};
// Wait for the child process to stop.
unsafe { kill(child.id() as _, SIGSTOP) };
let status = process::waitid(
process::WaitId::PidFd(pidfd.as_fd()),
process::WaitIdOptions::STOPPED,
)
.expect("failed to wait")
.unwrap();
// TODO
let _ = status;
}
#[cfg(feature = "event")]
#[test]
#[serial]
fn test_pidfd_poll() {
// Create a new process.
let child = Command::new("sleep")
.arg("1")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("failed to execute child");
// Create a pidfd for the child process.
let pid = process::Pid::from_child(&child);
let pidfd = match process::pidfd_open(pid, process::PidfdFlags::NONBLOCK) {
Ok(pidfd) => pidfd,
Err(io::Errno::NOSYS) | Err(io::Errno::INVAL) => {
// The kernel does not support non-blocking pidfds.
return;
}
Err(e) => panic!("failed to open pidfd: {}", e),
};
// The child process should not have exited yet.
match process::waitid(
process::WaitId::PidFd(pidfd.as_fd()),
process::WaitIdOptions::EXITED,
) {
Err(io::Errno::AGAIN) => (),
Err(e) => panic!("unexpected result: {:?}", e),
Ok(_) => panic!("unexpected success"),
}
// Wait for the child process to exit.
let pfd = event::PollFd::new(&pidfd, event::PollFlags::IN);
event::poll(&mut [pfd], -1).unwrap();
// The child process should have exited.
let status = process::waitid(
process::WaitId::PidFd(pidfd.as_fd()),
process::WaitIdOptions::EXITED,
)
.expect("failed to wait")
.unwrap();
// TODO
let _ = status;
}