Debian Codegraph @

src/sources/ping/pipe.rs

raw
1 //! Pipe based implementation of the ping event source, using the pipe or pipe2
2 //! syscall. Sending a ping involves writing to one end of a pipe, and the other
3 //! end becoming readable is what wakes up the event loop.
4
5 use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
6 use std::sync::Arc;
7
8 use rustix::io::{read, write, Errno};
9 use tracing::warn;
10
11 use super::PingError;
12 use crate::{
13 generic::Generic, EventSource, Interest, Mode, Poll, PostAction, Readiness, Token, TokenFactory,
14 };
15
16 #[cfg(target_os = "macos")]
17 #[inline]
18 fn make_ends() -> std::io::Result<(OwnedFd, OwnedFd)> {
19 use rustix::fs::{fcntl_getfl, fcntl_setfl, OFlags};
20 use rustix::pipe::pipe;
21
22 let (read, write) = pipe()?;
23
24 let set_flags = |fd| fcntl_setfl(fd, fcntl_getfl(fd)? | OFlags::CLOEXEC | OFlags::NONBLOCK);
25
26 set_flags(&read)?;
27 set_flags(&write)?;
28
29 Ok((read, write))
30 }
31
32 #[cfg(not(target_os = "macos"))]
33 #[inline]
34 fn make_ends() -> std::io::Result<(OwnedFd, OwnedFd)> {
35 use rustix::pipe::{pipe_with, PipeFlags};
36 Ok(pipe_with(PipeFlags::CLOEXEC | PipeFlags::NONBLOCK)?)
37 }
38
39 #[inline]
40 pub fn make_ping() -> std::io::Result<(Ping, PingSource)> {
41 let (read, write) = make_ends()?;
42
43 let source = PingSource {
44 pipe: Generic::new(read, Interest::READ, Mode::Level),
45 };
46 let ping = Ping {
47 pipe: Arc::new(write),
48 };
49 Ok((ping, source))
50 }
51
52 // Helper functions for the event source IO.
53
54 #[inline]
55 fn send_ping(fd: BorrowedFd<'_>) -> std::io::Result<()> {
56 write(fd, &[0u8])?;
57 Ok(())
58 }
59
60 // The event source is simply a generic source with the FD of the read end of
61 // the pipe.
62 #[derive(Debug)]
63 pub struct PingSource {
64 pipe: Generic<OwnedFd>,
65 }
66
67 impl EventSource for PingSource {
68 type Event = ();
69 type Metadata = ();
70 type Ret = ();
71 type Error = PingError;
72
73 fn process_events<C>(
74 &mut self,
75 readiness: Readiness,
76 token: Token,
77 mut callback: C,
78 ) -> Result<PostAction, Self::Error>
79 where
80 C: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
81 {
82 self.pipe
83 .process_events(readiness, token, |_, fd| {
84 let mut buf = [0u8; 32];
85 let mut read_something = false;
86 let mut action = PostAction::Continue;
87
88 loop {
89 match read(&fd, &mut buf) {
90 Ok(0) => {
91 // The other end of the pipe was closed, mark ourselves
92 // for removal.
93 action = PostAction::Remove;
94 break;
95 }
96
97 // Got one or more pings.
98 Ok(_) => read_something = true,
99
100 // Nothing more to read.
101 Err(Errno::AGAIN) => break,
102
103 // Propagate error.
104 Err(e) => return Err(e.into()),
105 }
106 }
107
108 if read_something {
109 callback((), &mut ());
110 }
111 Ok(action)
112 })
113 .map_err(|e| PingError(e.into()))
114 }
115
116 fn register(&mut self, poll: &mut Poll, token_factory: &mut TokenFactory) -> crate::Result<()> {
117 self.pipe.register(poll, token_factory)
118 }
119
120 fn reregister(
121 &mut self,
122 poll: &mut Poll,
123 token_factory: &mut TokenFactory,
124 ) -> crate::Result<()> {
125 self.pipe.reregister(poll, token_factory)
126 }
127
128 fn unregister(&mut self, poll: &mut Poll) -> crate::Result<()> {
129 self.pipe.unregister(poll)
130 }
131 }
132
133 // The sending end of the ping writes zeroes to the write end of the pipe.
134 #[derive(Clone, Debug)]
135 pub struct Ping {
136 pipe: Arc<OwnedFd>,
137 }
138
139 // The sending end of the ping writes zeroes to the write end of the pipe.
140 impl Ping {
141 /// Send a ping to the `PingSource`
142 pub fn ping(&self) {
143 if let Err(e) = send_ping(self.pipe.as_fd()) {
144 warn!("Failed to write a ping: {e:?}");
145 }
146 }
147 }