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")); println!("Exiting child with msg: {}", std::str::from_utf8(&buf[..]).expect("C Corruption"));
} }
})?.await??; }).await?;
println!("Child: {:?}", child); println!("Child: {:?}", child);
{ {

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

Loading…
Cancel
Save