usb_util: add open fd patch to libusb

open fd allow sandbox libusb.

BUG=chromium:831850
TEST=local build

Change-Id: Icda555936dbee3e9a56321ae616845c4310f20da
Reviewed-on: https://chromium-review.googlesource.com/1327512
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Jingkui Wang <jkwang@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
This commit is contained in:
Jingkui Wang 2018-11-07 20:54:41 -08:00 committed by chrome-bot
parent 13b8c090bb
commit f8a6bdddb7
2 changed files with 19 additions and 1 deletions

View file

@ -4184,6 +4184,13 @@ extern "C" {
dev_handle: *mut *mut libusb_device_handle,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn libusb_open_fd(
dev: *mut libusb_device,
fd: ::std::os::raw::c_int,
handle: *mut *mut libusb_device_handle,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn libusb_close(dev_handle: *mut libusb_device_handle);
}

View file

@ -9,6 +9,7 @@ use config_descriptor::ConfigDescriptor;
use device_handle::DeviceHandle;
use error::{Error, Result};
use libusb_context::LibUsbContextInner;
use std::os::unix::io::RawFd;
use std::sync::Arc;
use types::Speed;
@ -101,9 +102,19 @@ impl LibUsbDevice {
/// Get device handle of this device.
pub fn open(&self) -> Result<DeviceHandle> {
let mut handle: *mut bindings::libusb_device_handle = std::ptr::null_mut();
// Safe because 'self.device' is valid and handle is on stack.
// Safe because 'self.device' is constructed from libusb device list and handle is on stack.
try_libusb!(unsafe { bindings::libusb_open(self.device, &mut handle) });
// Safe because handle points to valid memory.
Ok(unsafe { DeviceHandle::new(self._context.clone(), handle) })
}
/// Get device handle of this device. Take an external fd. This function is only safe when fd
/// is an fd of this usb device.
pub unsafe fn open_fd(&self, fd: RawFd) -> Result<DeviceHandle> {
let mut handle: *mut bindings::libusb_device_handle = std::ptr::null_mut();
// Safe because 'self.device' is constructed from libusb device list and handle is on stack.
try_libusb!(unsafe { bindings::libusb_open_fd(self.device, fd, &mut handle) });
// Safe because handle is successfully initialized with libusb_open_fd.
Ok(unsafe { DeviceHandle::new(self._context.clone(), handle) })
}
}