0.2.11 - add daemon abstraction

This commit is contained in:
Jeremy Soller
2022-02-28 15:03:51 -07:00
parent 14796acfed
commit 87ffd8b862
3 changed files with 64 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "redox_syscall"
version = "0.2.10"
version = "0.2.11"
description = "A Rust library to access raw Redox system calls"
license = "MIT"
authors = ["Jeremy Soller <jackpot51@gmail.com>"]

58
src/daemon.rs Normal file
View File

@@ -0,0 +1,58 @@
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) -> !>(f: F) -> Result<!> {
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,
});
} 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))
}
}
}

View File

@@ -1,5 +1,6 @@
#![feature(asm)]
#![feature(llvm_asm)]
#![feature(never_type)]
#![cfg_attr(not(test), no_std)]
#[cfg(test)]
@@ -7,6 +8,7 @@ extern crate core;
pub use self::arch::*;
pub use self::call::*;
pub use self::daemon::*;
pub use self::data::*;
pub use self::error::*;
pub use self::flag::*;
@@ -44,6 +46,9 @@ pub mod call;
/// Complex structures that are used for some system calls
pub mod data;
/// Wrapper to make daemons easier to write
pub mod daemon;
/// All errors that can be generated by a system call
pub mod error;