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 17 #pragma once 18 19 #include <grp.h> 20 #include <pwd.h> 21 22 #include <functional> 23 #include <string> 24 #include <vector> 25 26 #include "result.h" 27 #include "uevent.h" 28 #include "uevent_handler.h" 29 30 namespace android { 31 namespace init { 32 33 struct ExternalFirmwareHandler { 34 ExternalFirmwareHandler(std::string devpath, uid_t uid, std::string handler_path); 35 ExternalFirmwareHandler(std::string devpath, uid_t uid, gid_t gid, std::string handler_path); 36 37 std::string devpath; 38 uid_t uid; 39 gid_t gid; 40 std::string handler_path; 41 42 std::function<bool(const std::string&)> match; 43 }; 44 45 class FirmwareHandler : public UeventHandler { 46 public: 47 FirmwareHandler(std::vector<std::string> firmware_directories, 48 std::vector<ExternalFirmwareHandler> external_firmware_handlers); 49 virtual ~FirmwareHandler() = default; 50 51 void HandleUevent(const Uevent& uevent) override; 52 53 private: 54 friend void FirmwareTestWithExternalHandler(const std::string& test_name, 55 bool expect_new_firmware); 56 57 Result<std::string> RunExternalHandler(const std::string& handler, uid_t uid, gid_t gid, 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; 61 bool ForEachFirmwareDirectory(std::function<bool(const std::string&)> handler) const; 62 63 std::vector<std::string> firmware_directories_; 64 std::vector<ExternalFirmwareHandler> external_firmware_handlers_; 65 }; 66 67 } // namespace init 68 } // namespace android 69