Skip to content

Commit

Permalink
Merge pull request #68 from utam0k/handle_wouldblock
Browse files Browse the repository at this point in the history
add handling of WouldBlock error.
  • Loading branch information
Furisto authored Jun 6, 2021
2 parents 27dc6ed + 8652f27 commit adb4310
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/process/child.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::{MAX_EVENTS, WAIT_FOR_INIT};
use std::io::ErrorKind;
use std::io::Read;
use std::io::Write;

use anyhow::{bail, Result};
use mio::unix::pipe;
use mio::unix::pipe::Receiver;
use mio::unix::pipe::Sender;
use mio::{Events, Interest, Poll, Token};
use nix::unistd::Pid;
use std::io::Read;
use std::io::Write;

use super::{MAX_EVENTS, WAIT_FOR_INIT};
use crate::process::message::Message;

// Token is used to identify which socket generated an event
Expand Down Expand Up @@ -97,7 +99,15 @@ impl ChildProcess {
if let CHILD = event.token() {
// read message from the init process
let mut buf = [0; 1];
receiver.read_exact(&mut buf)?;
match receiver.read_exact(&mut buf) {
// This error simply means that there are no more incoming connections waiting to be accepted at this point.
Err(ref e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!(
"Failed to receive a message from the child process. {:?}",
e
),
_ => (),
}
match Message::from(u8::from_be_bytes(buf)) {
Message::InitReady => return Ok(()),
msg => bail!("receive unexpected message {:?} in child process", msg),
Expand Down
21 changes: 19 additions & 2 deletions src/process/parent.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io::ErrorKind;
use std::io::Read;

use super::{MAX_EVENTS, WAIT_FOR_CHILD};
Expand Down Expand Up @@ -47,13 +48,29 @@ impl ParentProcess {
if let PARENT = event.token() {
// read data from pipe
let mut buf = [0; 1];
self.receiver.read_exact(&mut buf)?;
match self.receiver.read_exact(&mut buf) {
// This error simply means that there are no more incoming connections waiting to be accepted at this point.
Err(ref e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!(
"Failed to receive a message from the child process. {:?}",
e
),
_ => (),
};
// convert to Message wrapper
match Message::from(u8::from_be_bytes(buf)) {
Message::ChildReady => {
// read pid of init process forked by child, 4 bytes as the type is i32
let mut buf = [0; 4];
self.receiver.read_exact(&mut buf)?;
match self.receiver.read_exact(&mut buf) {
// This error simply means that there are no more incoming connections waiting to be accepted at this point.
Err(ref e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!(
"Failed to receive a message from the child process. {:?}",
e
),
_ => (),
}
return Ok(i32::from_be_bytes(buf));
}
msg => bail!("receive unexpected message {:?} in parent process", msg),
Expand Down

0 comments on commit adb4310

Please sign in to comment.