Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 1 | // 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 Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
| 13 | #include <base/macros.h> |
Qijiang Fan | b4e6421 | 2020-10-15 20:32:19 +0900 | [diff] [blame] | 14 | #include <base/memory/weak_ptr.h> |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 15 | |
| 16 | namespace permission_broker { |
| 17 | |
Nicholas Verne | 2e60b0d | 2020-09-29 11:43:31 +1000 | [diff] [blame] | 18 | constexpr const int kInvalidLifelineFD = -1; |
| 19 | |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 20 | class UsbDriverTracker { |
| 21 | public: |
| 22 | UsbDriverTracker(); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 23 | UsbDriverTracker(const UsbDriverTracker&) = delete; |
| 24 | UsbDriverTracker& operator=(const UsbDriverTracker&) = delete; |
| 25 | |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 26 | ~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 Verne | 2e60b0d | 2020-09-29 11:43:31 +1000 | [diff] [blame] | 30 | // Monitor |lifeline_fd| to reattach kernel drivers on close. |
| 31 | bool DetachPathFromKernel(int fd, int lifeline_fd, const std::string& path); |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 32 | |
Nicholas Verne | 2e60b0d | 2020-09-29 11:43:31 +1000 | [diff] [blame] | 33 | // Try to attach kernel drivers to the interface numbers in |ifaces| |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 34 | // of the USB device at |path|. |
| 35 | bool ReAttachPathToKernel(const std::string& path, |
| 36 | const std::vector<uint8_t>& ifaces); |
| 37 | |
| 38 | private: |
Hidehiko Abe | 689e757 | 2019-08-15 14:50:48 +0900 | [diff] [blame] | 39 | struct UsbInterfaces; |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 40 | |
Nicholas Verne | 2e60b0d | 2020-09-29 11:43:31 +1000 | [diff] [blame] | 41 | void HandleClosedFd(int fd); |
| 42 | void WatchLifelineFd(int lifeline_fd, |
| 43 | std::string path, |
| 44 | std::vector<uint8_t> ifaces); |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 45 | |
| 46 | // File descriptors watcher callback. |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 47 | static void OnFdEvent(UsbDriverTracker* obj, int fd); |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 48 | |
Hidehiko Abe | 689e757 | 2019-08-15 14:50:48 +0900 | [diff] [blame] | 49 | std::map<int, UsbInterfaces> dev_fds_; |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 50 | |
Nicholas Verne | 2e60b0d | 2020-09-29 11:43:31 +1000 | [diff] [blame] | 51 | base::WeakPtrFactory<UsbDriverTracker> weak_ptr_factory_{this}; |
Vincent Palatin | 70ba668 | 2016-02-12 09:50:30 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | } // namespace permission_broker |
| 55 | |
| 56 | #endif // PERMISSION_BROKER_USB_DRIVER_TRACKER_H_ |