From cba39f2fefc7dd1f38ffdae8451e530f16c06038 Mon Sep 17 00:00:00 2001 From: "Jorge E. Moreira" Date: Sun, 8 Mar 2020 01:26:27 +0000 Subject: [PATCH] Allow all serial port types to read from stdin Bug=b/148677254 Change-Id: I1fa38bc95ca303c7a2c38dbe4b938a6042c910c6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2093800 Reviewed-by: Zach Reizner Reviewed-by: Daniel Verkamp Tested-by: Zach Reizner Tested-by: kokoro Commit-Queue: Zach Reizner Auto-Submit: Jorge Moreira Broche --- devices/src/serial.rs | 44 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/devices/src/serial.rs b/devices/src/serial.rs index 98f39dc9d7..6629d7eca0 100644 --- a/devices/src/serial.rs +++ b/devices/src/serial.rs @@ -149,37 +149,35 @@ impl SerialParameters { ) -> std::result::Result { let evt_fd = evt_fd.try_clone().map_err(Error::CloneEventFd)?; keep_fds.push(evt_fd.as_raw_fd()); + let input: Option> = if self.stdin { + keep_fds.push(stdin().as_raw_fd()); + // This wrapper is used in place of the libstd native version because we don't want + // buffering for stdin. + struct StdinWrapper; + impl io::Read for StdinWrapper { + fn read(&mut self, out: &mut [u8]) -> io::Result { + read_raw_stdin(out).map_err(|e| e.into()) + } + } + Some(Box::new(StdinWrapper)) + } else { + None + }; match self.type_ { SerialType::Stdout => { keep_fds.push(stdout().as_raw_fd()); - if self.stdin { - keep_fds.push(stdin().as_raw_fd()); - // This wrapper is used in place of the libstd native version because we don't - // want buffering for stdin. - struct StdinWrapper; - impl io::Read for StdinWrapper { - fn read(&mut self, out: &mut [u8]) -> io::Result { - read_raw_stdin(out).map_err(|e| e.into()) - } - } - Ok(Serial::new_in_out( - evt_fd, - Box::new(StdinWrapper), - Box::new(stdout()), - )) - } else { - Ok(Serial::new_out(evt_fd, Box::new(stdout()))) - } + Ok(Serial::new(evt_fd, input, Some(Box::new(stdout())))) } - SerialType::Sink => Ok(Serial::new_sink(evt_fd)), + SerialType::Sink => Ok(Serial::new(evt_fd, input, None)), SerialType::Syslog => { syslog::push_fds(keep_fds); - Ok(Serial::new_out( + Ok(Serial::new( evt_fd, - Box::new(syslog::Syslogger::new( + input, + Some(Box::new(syslog::Syslogger::new( syslog::Priority::Info, syslog::Facility::Daemon, - )), + ))), )) } SerialType::File => match &self.path { @@ -187,7 +185,7 @@ impl SerialParameters { Some(path) => { let file = File::create(path.as_path()).map_err(Error::FileError)?; keep_fds.push(file.as_raw_fd()); - Ok(Serial::new_out(evt_fd, Box::new(file))) + Ok(Serial::new(evt_fd, input, Some(Box::new(file)))) } }, SerialType::UnixSocket => Err(Error::Unimplemented(SerialType::UnixSocket)),