fix fork_closure

master
Avril 4 years ago
parent b3430ca19d
commit 9c2d31353a
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -97,7 +97,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Exiting child with msg: {}", std::str::from_utf8(&buf[..]).expect("C Corruption"));
}
})?.await??;
}).await?;
println!("Child: {:?}", child);
{

@ -124,54 +124,55 @@ pub struct Parent {
comm: Pipe,
}
pub struct Fork(Option<Result<JoinHandle<Result<Child, Errno<Error>>>, Errno<Error>>>);
pub struct Fork(Result<JoinHandle<Result<Child, Errno<Error>>>, Errno<Error>>);
impl Future for Fork
{
type Output = Result<Child, Errno<Error>>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output>
{
let res = std::mem::replace(&mut self.0, None).unwrap();
match res {
Ok(handle) => {
println!("Spawn ok");
let future = async move {
println!("r");
let uh = handle.await.unwrap_or(Err(Errno::new(Error::Unknown)));
println!("What?");
uh
};
tokio::pin!(future);
future.poll(cx)
},
Err(err) => Poll::Ready(Err(err))
}
let v = std::mem::replace(&mut self.0, Err(Errno::new(Error::Unknown)));
let future = async move {
match v {
Ok(val) => {
val.await.expect("uhh")
},
Err(err) => {
Err(err)
},
}
};
tokio::pin!(future);
future.poll(cx)
}
}
/// Run a closure as a fork and then exit, optionally trying to set `uid` and `gid`.
///
/// The signature is the same as
///
/// ```
/// async fn detach_closure<F: FnOnce(Parent) -> Fu + Send + 'static, Fu: Future>(as_uid: Option<u32>, as_gid: Option<u32>, into: F) -> Result<Child, Errno<Error>>
/// ```
///
/// # Notes
/// This function spawns a speperate async runtime and blocks on it in the Child. Async/await should be safe here.
#[inline] pub fn detach_closure_test<F: FnOnce(Parent) -> Fu + Send + 'static, Fu: Future>(as_uid: Option<u32>, as_gid: Option<u32>, into: F) -> Fork
#[inline] pub async fn detach_closure<F: FnOnce(Parent) -> Fu + Send + 'static, Fu: Future>(as_uid: Option<u32>, as_gid: Option<u32>, into: F) -> Result<Child, Errno<Error>>
{
//this hangs?
//Fork(Some(detach_closure_internal(as_uid, as_gid, into)))
todo!()
match detach_closure_internal(as_uid, as_gid, into) {
Ok(handle) => {
if let Ok(ok) = handle.await {
ok
} else {
Err(Errno::with_errno(Error::Unknown, 0)) //TODO: Change to new error varient
}
},
Err(err) => {
Err(err)
}
}
}
/// Run a closure as a fork and then exit, optionally trying to set `uid` and `gid`.
///
/// # Notes
/// This function spawns a speperate async runtime and blocks on it in the Child. Async/await should be safe here.
pub fn detach_closure<F: FnOnce(Parent) -> Fu + Send + 'static, Fu: Future>(as_uid: Option<u32>, as_gid: Option<u32>, into: F) -> Result<JoinHandle<Result<Child, Errno<Error>>>, Errno<Error>> {
fn detach_closure_internal<F: FnOnce(Parent) -> Fu + Send + 'static, Fu: Future>(as_uid: Option<u32>, as_gid: Option<u32>, into: F) -> Result<JoinHandle<Result<Child, Errno<Error>>>, Errno<Error>> {
// let (rx, tx) = unix_pipe().map_inner(|x| Error::from(x))?;
let (mut ttx,mut trx) = pipe::multi().map_err(|x| Error::from(x))?;

Loading…
Cancel
Save