• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #include "host/frontend/webrtc/kernel_log_events_handler.h"
18 
19 #include <android-base/logging.h>
20 
21 #include <common/libs/fs/shared_select.h>
22 #include <host/commands/kernel_log_monitor/kernel_log_server.h>
23 #include <host/commands/kernel_log_monitor/utils.h>
24 #include <host/libs/config/cuttlefish_config.h>
25 
26 using namespace android;
27 
28 namespace cuttlefish {
29 
KernelLogEventsHandler(SharedFD kernel_log_fd)30 KernelLogEventsHandler::KernelLogEventsHandler(
31     SharedFD kernel_log_fd)
32     : kernel_log_fd_(kernel_log_fd),
33       eventfd_(SharedFD::Event()),
34       running_(true),
35       read_thread_([this]() { ReadLoop(); }) {}
36 
~KernelLogEventsHandler()37 KernelLogEventsHandler::~KernelLogEventsHandler() {
38   running_ = false;
39   eventfd_->EventfdWrite(1);
40   read_thread_.join();
41 }
42 
ReadLoop()43 void KernelLogEventsHandler::ReadLoop() {
44   CHECK(eventfd_->IsOpen()) << "Failed to create event fd: "
45                            << eventfd_->StrError();
46   while (running_) {
47     SharedFDSet read_set;
48     read_set.Set(eventfd_);
49     read_set.Set(kernel_log_fd_);
50     auto select_ret = Select(&read_set, nullptr, nullptr, nullptr);
51     if (select_ret < 0) {
52       LOG(ERROR) << "Error on select call";
53       break;
54     }
55     if (read_set.IsSet(eventfd_)) {
56       eventfd_t evt;
57       (void)eventfd_->EventfdRead(&evt);
58       if (!running_) {
59         // There won't be anyone listening for kernel log events if the thread
60         // was asked to stop, so break out of the loop without reading.
61         break;
62       }
63     }
64     if (read_set.IsSet(kernel_log_fd_)) {
65       std::optional<monitor::ReadEventResult> read_result =
66           monitor::ReadEvent(kernel_log_fd_);
67       if (!read_result) {
68         LOG(ERROR) << "Failed to read kernel log event: "
69                    << kernel_log_fd_->StrError();
70         break;
71       }
72 
73       if (read_result->event == monitor::Event::BootStarted) {
74         Json::Value message;
75         message["event"] = kBootStartedMessage;
76         DeliverEvent(message);
77       }
78       if (read_result->event == monitor::Event::BootCompleted) {
79         Json::Value message;
80         message["event"] = kBootCompletedMessage;
81         DeliverEvent(message);
82       }
83       if (read_result->event == monitor::Event::ScreenChanged) {
84         Json::Value message;
85         message["event"] = kScreenChangedMessage;
86         message["metadata"] = read_result->metadata;
87         DeliverEvent(message);
88       }
89     }
90   }
91 }
92 
AddSubscriber(std::function<void (const Json::Value &)> subscriber)93 int KernelLogEventsHandler::AddSubscriber(
94     std::function<void(const Json::Value&)> subscriber) {
95   std::lock_guard<std::mutex> lock(subscribers_mtx_);
96   subscribers_[++last_subscriber_id_] = subscriber;
97   return last_subscriber_id_;
98 }
99 
Unsubscribe(int subscriber_id)100 void KernelLogEventsHandler::Unsubscribe(int subscriber_id) {
101   std::lock_guard<std::mutex> lock(subscribers_mtx_);
102   subscribers_.erase(subscriber_id);
103 }
104 
DeliverEvent(const Json::Value & event)105 void KernelLogEventsHandler::DeliverEvent(const Json::Value& event) {
106   std::lock_guard<std::mutex> lock(subscribers_mtx_);
107   for (const auto& entry : subscribers_) {
108     entry.second(event);
109   }
110 }
111 
112 }  // namespace cuttlefish
113