MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / main

Function main

examples/port.rs:8–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6
7#[cfg(all(solarish, feature = "event", feature = "fs"))]
8fn main() -> std::io::Result<()> {
9 use rustix::buffer::spare_capacity;
10 use rustix::event::port;
11 use rustix::fd::AsRawFd;
12 use rustix::fs;
13 use std::ptr::null_mut;
14
15 let port = port::create()?;
16 let mut out = Vec::with_capacity(10);
17
18 let tmpdir = tempfile::tempdir()?;
19 let fifo_path = tmpdir.path().join("fifo");
20 fs::mknodat(
21 fs::CWD,
22 &fifo_path,
23 fs::FileType::Fifo,
24 fs::Mode::RUSR | fs::Mode::WUSR,
25 0,
26 )?;
27
28 eprintln!("Listening for data on fifo {}", fifo_path.display());
29
30 let fifo = fs::openat(fs::CWD, &fifo_path, fs::OFlags::RDONLY, fs::Mode::empty())?;
31
32 loop {
33 // Associate `some_fd` with the port.
34 unsafe {
35 port::associate_fd(&port, fifo.as_raw_fd(), port::PollFlags::IN, null_mut())?;
36 }
37
38 port::getn(&port, spare_capacity(&mut out), 1, None)?;
39
40 for event in out.drain(..) {
41 dbg!(event.events(), event.object(), event.userdata());
42
43 let mut buf = [0_u8; 32];
44 loop {
45 match rustix::io::read(&fifo, &mut buf) {
46 Ok(0) => return Ok(()),
47 Ok(n) => {
48 dbg!(&buf[..n]);
49 break;
50 }
51 Err(rustix::io::Errno::INTR) => continue,
52 Err(err) => Err(err).unwrap(),
53 }
54 }
55 }
56 }
57}
58
59#[cfg(not(all(solarish, feature = "event", feature = "fs")))]
60fn main() -> Result<(), &'static str> {

Callers

nothing calls this directly

Calls 10

associate_fdFunction · 0.85
getnFunction · 0.85
spare_capacityFunction · 0.85
drainMethod · 0.80
readFunction · 0.70
createFunction · 0.50
mknodatFunction · 0.50
openatFunction · 0.50
pathMethod · 0.45
as_raw_fdMethod · 0.45

Tested by

no test coverage detected