blob: 5676af346e67f8ab164555a84952e8f7e16776c8 [file] [log] [blame]
// Copyright 2016 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <string>
#include <tuple>
#include <vector>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
namespace permission_broker {
constexpr const int kInvalidLifelineFD = -1;
class UsbDriverTracker {
UsbDriverTracker(const UsbDriverTracker&) = delete;
UsbDriverTracker& operator=(const UsbDriverTracker&) = delete;
// Detach all the interfaces of the USB device at |path| from their
// kernel drivers using the |fd| file descriptor pointing to the devfs node.
// Monitor |lifeline_fd| to reattach kernel drivers on close.
bool DetachPathFromKernel(int fd, int lifeline_fd, const std::string& path);
// Try to attach kernel drivers to the interface numbers in |ifaces|
// of the USB device at |path|.
bool ReAttachPathToKernel(const std::string& path,
const std::vector<uint8_t>& ifaces);
struct UsbInterfaces;
void HandleClosedFd(int fd);
void WatchLifelineFd(int lifeline_fd,
std::string path,
std::vector<uint8_t> ifaces);
// File descriptors watcher callback.
static void OnFdEvent(UsbDriverTracker* obj, int fd);
std::map<int, UsbInterfaces> dev_fds_;
base::WeakPtrFactory<UsbDriverTracker> weak_ptr_factory_{this};
} // namespace permission_broker