aboutsummaryrefslogtreecommitdiff
path: root/devices/src/virtio/console/sys/windows.rs
blob: c238dfd773cc6fb89b38ffc76b692dbe5568c6fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::collections::VecDeque;
use std::io;
use std::sync::Arc;
use std::thread;
use std::time::Duration;

use base::error;
use base::named_pipes;
use base::Event;
use base::FileSync;
use base::RawDescriptor;
use base::WorkerThread;
use sync::Mutex;

use crate::serial_device::SerialInput;
use crate::virtio::console::Console;
use crate::virtio::console::ConsoleInput;
use crate::virtio::ProtectionType;
use crate::SerialDevice;

impl SerialDevice for Console {
    fn new(
        protection_type: ProtectionType,
        _event: Event,
        _input: Option<Box<dyn SerialInput>>,
        out: Option<Box<dyn io::Write + Send>>,
        // TODO(b/171331752): connect filesync functionality.
        _sync: Option<Box<dyn FileSync + Send>>,
        _out_timestamp: bool,
        keep_rds: Vec<RawDescriptor>,
    ) -> Console {
        Console::new(protection_type, None, out, keep_rds)
    }

    /// Constructs a console with named pipe as input/output connections.
    fn new_with_pipe(
        protection_type: ProtectionType,
        _interrupt_evt: Event,
        pipe_in: named_pipes::PipeConnection,
        pipe_out: named_pipes::PipeConnection,
        keep_rds: Vec<RawDescriptor>,
    ) -> Console {
        Console::new(
            protection_type,
            Some(ConsoleInput::FromRead(Box::new(pipe_in))),
            Some(Box::new(pipe_out)),
            keep_rds,
        )
    }
}

/// Platform-specific function to add a delay for reading rx.
///
/// We can't issue blocking reads here and overlapped I/O is
/// incompatible with the call site where writes to this pipe are being
/// made, so instead we issue a small wait to prevent us from hogging
/// the CPU. This 20ms delay while typing doesn't seem to be noticeable.
fn read_delay_if_needed() {
    thread::sleep(Duration::from_millis(20));
}

fn is_a_fatal_input_error(e: &io::Error) -> bool {
    !matches!(
        e.kind(),
        // Being interrupted is not an error.
        io::ErrorKind::Interrupted |
        // Ignore two kinds of errors on Windows.
        //   - ErrorKind::Other when reading a named pipe before a client connects.
        //   - ErrorKind::WouldBlock when reading a named pipe (we don't use non-blocking I/O).
        io::ErrorKind::Other | io::ErrorKind::WouldBlock
    )
    // Everything else is a fatal input error.
}

/// Starts a thread that reads rx and sends the input back via the returned buffer.
///
/// The caller should listen on `in_avail_evt` for events. When `in_avail_evt` signals that data
/// is available, the caller should lock the returned `Mutex` and read data out of the inner
/// `VecDeque`. The data should be removed from the beginning of the `VecDeque` as it is processed.
///
/// # Arguments
///
/// * `rx` - Data source that the reader thread will wait on to send data back to the buffer
/// * `in_avail_evt` - Event triggered by the thread when new input is available on the buffer
pub(in crate::virtio::console) fn spawn_input_thread(
    mut rx: Box<named_pipes::PipeConnection>,
    in_avail_evt: &Event,
    input_buffer: VecDeque<u8>,
) -> (Arc<Mutex<VecDeque<u8>>>, WorkerThread<()>) {
    let buffer = Arc::new(Mutex::new(input_buffer));
    let buffer_cloned = buffer.clone();

    let thread_in_avail_evt = in_avail_evt
        .try_clone()
        .expect("failed to clone in_avail_evt");

    let buffer_max_size = 1 << 12;
    let mut rx_buf = Vec::with_capacity(buffer_max_size);
    let res = WorkerThread::start("v_console_input", move |kill_evt| {
        // If there is already data, signal immediately.
        if !buffer.lock().is_empty() {
            thread_in_avail_evt.signal().unwrap();
        }

        match rx.wait_for_client_connection_overlapped_blocking(&kill_evt) {
            Err(e) if e.kind() == io::ErrorKind::Interrupted => return,
            Err(e) => panic!("failed to wait for client: {}", e),
            Ok(()) => (),
        }

        let mut read_overlapped =
            named_pipes::OverlappedWrapper::new(true).expect("failed to create OverlappedWrapper");
        loop {
            let size = rx
                .get_available_byte_count()
                .expect("failed to get available byte count") as usize;
            // Clamp to [1, buffer capacity]. Need to read at least one byte so that the read call
            // blocks until data is available.
            let size = std::cmp::min(std::cmp::max(size, 1), buffer_max_size);
            rx_buf.resize(size, Default::default());
            let res = rx.read_overlapped_blocking(&mut rx_buf, &mut read_overlapped, &kill_evt);

            match res {
                Ok(()) => {
                    buffer.lock().extend(&rx_buf[..]);
                    thread_in_avail_evt.signal().unwrap();
                }
                Err(e) if e.kind() == io::ErrorKind::Interrupted => {
                    // Exit event triggered
                    break;
                }
                Err(e) => {
                    // Being interrupted is not an error, but everything else is.
                    if is_a_fatal_input_error(&e) {
                        error!(
                            "failed to read for bytes to queue into console device: {}",
                            e
                        );
                        break;
                    }
                }
            }

            // Depending on the platform, a short sleep is needed here (ie. Windows).
            read_delay_if_needed();
        }
    });
    (buffer_cloned, res)
}