arch/serial/sys/
linux.rs

1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::sync::Arc;
6
7use base::RawDescriptor;
8use devices::serial_device::SerialParameters;
9use devices::BusDevice;
10use devices::ProxyDevice;
11use devices::Serial;
12use minijail::Minijail;
13use sync::Mutex;
14
15use crate::DeviceRegistrationError;
16
17pub fn add_serial_device(
18    com: Serial,
19    _serial_parameters: &SerialParameters,
20    serial_jail: Option<Minijail>,
21    preserved_descriptors: Vec<RawDescriptor>,
22    #[cfg(feature = "swap")] swap_controller: &mut Option<swap::SwapController>,
23) -> std::result::Result<Arc<Mutex<dyn BusDevice>>, DeviceRegistrationError> {
24    let com: Arc<Mutex<dyn BusDevice>> = if let Some(serial_jail) = serial_jail {
25        Arc::new(Mutex::new(
26            ProxyDevice::new(
27                com,
28                serial_jail,
29                preserved_descriptors,
30                #[cfg(feature = "swap")]
31                swap_controller,
32            )
33            .map_err(DeviceRegistrationError::ProxyDeviceCreation)?,
34        ))
35    } else {
36        Arc::new(Mutex::new(com))
37    };
38    Ok(com)
39}