blob: 5676af346e67f8ab164555a84952e8f7e16776c8 [file] [log] [blame]
Vincent Palatin70ba6682016-02-12 09:50:30 -08001// Copyright 2016 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef PERMISSION_BROKER_USB_DRIVER_TRACKER_H_
6#define PERMISSION_BROKER_USB_DRIVER_TRACKER_H_
7
8#include <map>
9#include <string>
10#include <tuple>
Vincent Palatin70ba6682016-02-12 09:50:30 -080011#include <vector>
12
13#include <base/macros.h>
Qijiang Fanb4e64212020-10-15 20:32:19 +090014#include <base/memory/weak_ptr.h>
Vincent Palatin70ba6682016-02-12 09:50:30 -080015
16namespace permission_broker {
17
Nicholas Verne2e60b0d2020-09-29 11:43:31 +100018constexpr const int kInvalidLifelineFD = -1;
19
Vincent Palatin70ba6682016-02-12 09:50:30 -080020class UsbDriverTracker {
21 public:
22 UsbDriverTracker();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090023 UsbDriverTracker(const UsbDriverTracker&) = delete;
24 UsbDriverTracker& operator=(const UsbDriverTracker&) = delete;
25
Vincent Palatin70ba6682016-02-12 09:50:30 -080026 ~UsbDriverTracker();
27
28 // Detach all the interfaces of the USB device at |path| from their
29 // kernel drivers using the |fd| file descriptor pointing to the devfs node.
Nicholas Verne2e60b0d2020-09-29 11:43:31 +100030 // Monitor |lifeline_fd| to reattach kernel drivers on close.
31 bool DetachPathFromKernel(int fd, int lifeline_fd, const std::string& path);
Vincent Palatin70ba6682016-02-12 09:50:30 -080032
Nicholas Verne2e60b0d2020-09-29 11:43:31 +100033 // Try to attach kernel drivers to the interface numbers in |ifaces|
Vincent Palatin70ba6682016-02-12 09:50:30 -080034 // of the USB device at |path|.
35 bool ReAttachPathToKernel(const std::string& path,
36 const std::vector<uint8_t>& ifaces);
37
38 private:
Hidehiko Abe689e7572019-08-15 14:50:48 +090039 struct UsbInterfaces;
Vincent Palatin70ba6682016-02-12 09:50:30 -080040
Nicholas Verne2e60b0d2020-09-29 11:43:31 +100041 void HandleClosedFd(int fd);
42 void WatchLifelineFd(int lifeline_fd,
43 std::string path,
44 std::vector<uint8_t> ifaces);
Vincent Palatin70ba6682016-02-12 09:50:30 -080045
46 // File descriptors watcher callback.
Tom Hughes4f300e52020-08-24 18:08:59 -070047 static void OnFdEvent(UsbDriverTracker* obj, int fd);
Vincent Palatin70ba6682016-02-12 09:50:30 -080048
Hidehiko Abe689e7572019-08-15 14:50:48 +090049 std::map<int, UsbInterfaces> dev_fds_;
Vincent Palatin70ba6682016-02-12 09:50:30 -080050
Nicholas Verne2e60b0d2020-09-29 11:43:31 +100051 base::WeakPtrFactory<UsbDriverTracker> weak_ptr_factory_{this};
Vincent Palatin70ba6682016-02-12 09:50:30 -080052};
53
54} // namespace permission_broker
55
56#endif // PERMISSION_BROKER_USB_DRIVER_TRACKER_H_