blob: d2f7347cec634cafff3fb9003e4bf3e6d8727ab2 [file] [log] [blame]
Tom Cherryed506f72017-05-25 15:58:59 -07001/*
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 Cherrydcb3d152019-08-07 16:02:28 -070017#pragma once
18
Suchang Woo10c63742021-05-13 18:56:31 +090019#include <grp.h>
Tom Cherrydcb3d152019-08-07 16:02:28 -070020#include <pwd.h>
Tom Cherryed506f72017-05-25 15:58:59 -070021
Jooyung Han21cad322020-09-18 14:41:22 +090022#include <functional>
Tom Cherry7421fa12018-07-13 15:32:02 -070023#include <string>
24#include <vector>
25
Tom Cherrydcb3d152019-08-07 16:02:28 -070026#include "result.h"
Tom Cherryed506f72017-05-25 15:58:59 -070027#include "uevent.h"
Tom Cherry457e28f2018-08-01 13:12:20 -070028#include "uevent_handler.h"
Tom Cherryed506f72017-05-25 15:58:59 -070029
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030namespace android {
31namespace init {
32
Tom Cherrydcb3d152019-08-07 16:02:28 -070033struct ExternalFirmwareHandler {
Suchang Woo22fdd0a2021-04-06 11:22:49 +090034 ExternalFirmwareHandler(std::string devpath, uid_t uid, std::string handler_path);
Suchang Woo10c63742021-05-13 18:56:31 +090035 ExternalFirmwareHandler(std::string devpath, uid_t uid, gid_t gid, std::string handler_path);
Suchang Woo22fdd0a2021-04-06 11:22:49 +090036
Tom Cherrydcb3d152019-08-07 16:02:28 -070037 std::string devpath;
38 uid_t uid;
Suchang Woo10c63742021-05-13 18:56:31 +090039 gid_t gid;
Tom Cherrydcb3d152019-08-07 16:02:28 -070040 std::string handler_path;
Suchang Woo22fdd0a2021-04-06 11:22:49 +090041
42 std::function<bool(const std::string&)> match;
Tom Cherrydcb3d152019-08-07 16:02:28 -070043};
44
Tom Cherry457e28f2018-08-01 13:12:20 -070045class FirmwareHandler : public UeventHandler {
46 public:
Tom Cherrydcb3d152019-08-07 16:02:28 -070047 FirmwareHandler(std::vector<std::string> firmware_directories,
48 std::vector<ExternalFirmwareHandler> external_firmware_handlers);
Tom Cherry457e28f2018-08-01 13:12:20 -070049 virtual ~FirmwareHandler() = default;
Tom Cherry7421fa12018-07-13 15:32:02 -070050
Tom Cherry457e28f2018-08-01 13:12:20 -070051 void HandleUevent(const Uevent& uevent) override;
52
53 private:
Tom Cherrydcb3d152019-08-07 16:02:28 -070054 friend void FirmwareTestWithExternalHandler(const std::string& test_name,
55 bool expect_new_firmware);
56
Suchang Woo10c63742021-05-13 18:56:31 +090057 Result<std::string> RunExternalHandler(const std::string& handler, uid_t uid, gid_t gid,
Tom Cherrydcb3d152019-08-07 16:02:28 -070058 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 Han21cad322020-09-18 14:41:22 +090061 bool ForEachFirmwareDirectory(std::function<bool(const std::string&)> handler) const;
Tom Cherry457e28f2018-08-01 13:12:20 -070062
63 std::vector<std::string> firmware_directories_;
Tom Cherrydcb3d152019-08-07 16:02:28 -070064 std::vector<ExternalFirmwareHandler> external_firmware_handlers_;
Tom Cherry457e28f2018-08-01 13:12:20 -070065};
Tom Cherryed506f72017-05-25 15:58:59 -070066
Tom Cherry81f5d3e2017-06-22 12:53:17 -070067} // namespace init
68} // namespace android