Skip to main content

vm_ch/
serial.rs

1use std::os::fd::RawFd;
2
3pub struct FileHandleSerialAttachment {
4    pub(crate) read_fd: Option<RawFd>,
5    pub(crate) write_fd: Option<RawFd>,
6}
7
8impl FileHandleSerialAttachment {
9    pub fn new(read_fd: RawFd, write_fd: RawFd) -> Self {
10        FileHandleSerialAttachment {
11            read_fd: Some(read_fd),
12            write_fd: Some(write_fd),
13        }
14    }
15
16    /// Serial attachment with no read handle (stdin disconnected).
17    /// Output goes to the given write fd.
18    pub fn new_write_only(write_fd: RawFd) -> Self {
19        FileHandleSerialAttachment {
20            read_fd: None,
21            write_fd: Some(write_fd),
22        }
23    }
24}
25
26pub struct VirtioConsoleSerialPort {
27    pub(crate) read_fd: Option<RawFd>,
28    pub(crate) write_fd: Option<RawFd>,
29}
30
31impl VirtioConsoleSerialPort {
32    pub fn new() -> Self {
33        VirtioConsoleSerialPort {
34            read_fd: None,
35            write_fd: None,
36        }
37    }
38
39    pub fn new_with_attachment(attachment: &FileHandleSerialAttachment) -> Self {
40        VirtioConsoleSerialPort {
41            read_fd: attachment.read_fd,
42            write_fd: attachment.write_fd,
43        }
44    }
45
46    pub fn set_attachment(&mut self, attachment: &FileHandleSerialAttachment) {
47        self.read_fd = attachment.read_fd;
48        self.write_fd = attachment.write_fd;
49    }
50}
51
52impl Default for VirtioConsoleSerialPort {
53    fn default() -> Self {
54        Self::new()
55    }
56}