Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
Suchang Woo | 10c6374 | 2021-05-13 18:56:31 +0900 | [diff] [blame] | 19 | #include <grp.h> |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 20 | #include <pwd.h> |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 21 | |
Jooyung Han | 21cad32 | 2020-09-18 14:41:22 +0900 | [diff] [blame] | 22 | #include <functional> |
Tom Cherry | 7421fa1 | 2018-07-13 15:32:02 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 26 | #include "result.h" |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 27 | #include "uevent.h" |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 28 | #include "uevent_handler.h" |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 29 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace init { |
| 32 | |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 33 | struct ExternalFirmwareHandler { |
Suchang Woo | 22fdd0a | 2021-04-06 11:22:49 +0900 | [diff] [blame] | 34 | ExternalFirmwareHandler(std::string devpath, uid_t uid, std::string handler_path); |
Suchang Woo | 10c6374 | 2021-05-13 18:56:31 +0900 | [diff] [blame] | 35 | ExternalFirmwareHandler(std::string devpath, uid_t uid, gid_t gid, std::string handler_path); |
Suchang Woo | 22fdd0a | 2021-04-06 11:22:49 +0900 | [diff] [blame] | 36 | |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 37 | std::string devpath; |
| 38 | uid_t uid; |
Suchang Woo | 10c6374 | 2021-05-13 18:56:31 +0900 | [diff] [blame] | 39 | gid_t gid; |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 40 | std::string handler_path; |
Suchang Woo | 22fdd0a | 2021-04-06 11:22:49 +0900 | [diff] [blame] | 41 | |
| 42 | std::function<bool(const std::string&)> match; |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 45 | class FirmwareHandler : public UeventHandler { |
| 46 | public: |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 47 | FirmwareHandler(std::vector<std::string> firmware_directories, |
| 48 | std::vector<ExternalFirmwareHandler> external_firmware_handlers); |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 49 | virtual ~FirmwareHandler() = default; |
Tom Cherry | 7421fa1 | 2018-07-13 15:32:02 -0700 | [diff] [blame] | 50 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 51 | void HandleUevent(const Uevent& uevent) override; |
| 52 | |
| 53 | private: |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 54 | friend void FirmwareTestWithExternalHandler(const std::string& test_name, |
| 55 | bool expect_new_firmware); |
| 56 | |
Suchang Woo | 10c6374 | 2021-05-13 18:56:31 +0900 | [diff] [blame] | 57 | Result<std::string> RunExternalHandler(const std::string& handler, uid_t uid, gid_t gid, |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 58 | const Uevent& uevent) const; |
| 59 | std::string GetFirmwarePath(const Uevent& uevent) const; |
| 60 | void ProcessFirmwareEvent(const std::string& root, const std::string& firmware) const; |
Jooyung Han | 21cad32 | 2020-09-18 14:41:22 +0900 | [diff] [blame] | 61 | bool ForEachFirmwareDirectory(std::function<bool(const std::string&)> handler) const; |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 62 | |
| 63 | std::vector<std::string> firmware_directories_; |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 64 | std::vector<ExternalFirmwareHandler> external_firmware_handlers_; |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 65 | }; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 66 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 67 | } // namespace init |
| 68 | } // namespace android |