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