diff --git a/src/main.rs b/src/main.rs index 6ba2757..fd42a54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,7 @@ async fn main() -> Result<(), Box> { println!("Exiting child with msg: {}", std::str::from_utf8(&buf[..]).expect("C Corruption")); } - })?.await??; + }).await?; println!("Child: {:?}", child); { diff --git a/src/sys/fork.rs b/src/sys/fork.rs index 0811dbe..6a9e520 100644 --- a/src/sys/fork.rs +++ b/src/sys/fork.rs @@ -124,54 +124,55 @@ pub struct Parent { comm: Pipe, } -pub struct Fork(Option>>, Errno>>); +pub struct Fork(Result>>, Errno>); impl Future for Fork { type Output = Result>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll { - 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 Fu + Send + 'static, Fu: Future>(as_uid: Option, as_gid: Option, into: F) -> Result> -/// ``` -/// /// # 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 Fu + Send + 'static, Fu: Future>(as_uid: Option, as_gid: Option, into: F) -> Fork +#[inline] pub async fn detach_closure Fu + Send + 'static, Fu: Future>(as_uid: Option, as_gid: Option, into: F) -> Result> { - //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 Fu + Send + 'static, Fu: Future>(as_uid: Option, as_gid: Option, into: F) -> Result>>, Errno> { +fn detach_closure_internal Fu + Send + 'static, Fu: Future>(as_uid: Option, as_gid: Option, into: F) -> Result>>, Errno> { // let (rx, tx) = unix_pipe().map_inner(|x| Error::from(x))?; let (mut ttx,mut trx) = pipe::multi().map_err(|x| Error::from(x))?;