diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/redox_syscall/src/daemon.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/redox_syscall/src/daemon.rs')
-rw-r--r-- | vendor/redox_syscall/src/daemon.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/vendor/redox_syscall/src/daemon.rs b/vendor/redox_syscall/src/daemon.rs new file mode 100644 index 000000000..6433bcd7d --- /dev/null +++ b/vendor/redox_syscall/src/daemon.rs @@ -0,0 +1,62 @@ +use core::convert::Infallible; + +use super::{ + clone, + CloneFlags, + close, + EIO, + Error, + exit, + pipe2, + read, + Result, + write, +}; + +#[must_use = "Daemon::ready must be called"] +pub struct Daemon { + write_pipe: usize, +} + +impl Daemon { + pub fn new<F: FnOnce(Daemon) -> Infallible>(f: F) -> Result<Infallible> { + let mut pipes = [0; 2]; + pipe2(&mut pipes, 0)?; + + let [read_pipe, write_pipe] = pipes; + + if unsafe { clone(CloneFlags::empty())? } == 0 { + let _ = close(read_pipe); + + f(Daemon { + write_pipe, + }); + // TODO: Replace Infallible with the never type once it is stabilized. + unreachable!(); + } else { + let _ = close(write_pipe); + + let mut data = [0]; + let res = read(read_pipe, &mut data); + let _ = close(read_pipe); + + if res? == 1 { + exit(data[0] as usize)?; + unreachable!(); + } else { + Err(Error::new(EIO)) + } + } + } + + pub fn ready(self) -> Result<()> { + let res = write(self.write_pipe, &[0]); + let _ = close(self.write_pipe); + + if res? == 1 { + Ok(()) + } else { + Err(Error::new(EIO)) + } + } +} |