1 /* 2 * Copyright (C) 2019 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 <atomic> 20 #include <memory> 21 #include <mutex> 22 #include <thread> 23 #include <map> 24 25 #include <json/json.h> 26 27 #include "common/libs/fs/shared_fd.h" 28 29 namespace cuttlefish { 30 31 // Listen to kernel log events and report them to clients. 32 struct KernelLogEventsHandler { 33 explicit KernelLogEventsHandler(SharedFD kernel_log_fd); 34 35 ~KernelLogEventsHandler(); 36 37 int AddSubscriber(std::function<void(const Json::Value&)> subscriber); 38 void Unsubscribe(int subscriber_id); 39 private: 40 void ReadLoop(); 41 void DeliverEvent(const Json::Value& event); 42 43 SharedFD kernel_log_fd_; 44 SharedFD eventfd_; 45 std::atomic<bool> running_; 46 std::thread read_thread_; 47 std::map<int, std::function<void(const Json::Value&)>> subscribers_; 48 int last_subscriber_id_ = 0; 49 std::mutex subscribers_mtx_; 50 }; 51 52 } // namespace cuttlefish 53