aboutsummaryrefslogtreecommitdiff
path: root/host/frontend/webrtc/kernel_log_events_handler.cpp
blob: ca14e6f050d630f057d7bcc95101f3ae6bd13b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "host/frontend/webrtc/kernel_log_events_handler.h"

#include <android-base/logging.h>

#include <common/libs/fs/shared_select.h>
#include <host/commands/kernel_log_monitor/kernel_log_server.h>
#include <host/commands/kernel_log_monitor/utils.h>
#include <host/libs/config/cuttlefish_config.h>

using namespace android;

namespace cuttlefish {

KernelLogEventsHandler::KernelLogEventsHandler(
    SharedFD kernel_log_fd)
    : kernel_log_fd_(kernel_log_fd),
      eventfd_(SharedFD::Event()),
      running_(true),
      read_thread_([this]() { ReadLoop(); }) {}

KernelLogEventsHandler::~KernelLogEventsHandler() {
  running_ = false;
  eventfd_->EventfdWrite(1);
  read_thread_.join();
}

void KernelLogEventsHandler::ReadLoop() {
  CHECK(eventfd_->IsOpen()) << "Failed to create event fd: "
                           << eventfd_->StrError();
  while (running_) {
    SharedFDSet read_set;
    read_set.Set(eventfd_);
    read_set.Set(kernel_log_fd_);
    auto select_ret = Select(&read_set, nullptr, nullptr, nullptr);
    if (select_ret < 0) {
      LOG(ERROR) << "Error on select call";
      break;
    }
    if (read_set.IsSet(eventfd_)) {
      eventfd_t evt;
      (void)eventfd_->EventfdRead(&evt);
      if (!running_) {
        // There won't be anyone listening for kernel log events if the thread
        // was asked to stop, so break out of the loop without reading.
        break;
      }
    }
    if (read_set.IsSet(kernel_log_fd_)) {
      std::optional<monitor::ReadEventResult> read_result =
          monitor::ReadEvent(kernel_log_fd_);
      if (!read_result) {
        LOG(ERROR) << "Failed to read kernel log event: "
                   << kernel_log_fd_->StrError();
        break;
      }

      if (read_result->event == monitor::Event::BootStarted) {
        Json::Value message;
        message["event"] = kBootStartedMessage;
        DeliverEvent(message);
      }
      if (read_result->event == monitor::Event::BootCompleted) {
        Json::Value message;
        message["event"] = kBootCompletedMessage;
        DeliverEvent(message);
      }
      if (read_result->event == monitor::Event::ScreenChanged) {
        Json::Value message;
        message["event"] = kScreenChangedMessage;
        message["metadata"] = read_result->metadata;
        DeliverEvent(message);
      }
    }
  }
}

int KernelLogEventsHandler::AddSubscriber(
    std::function<void(const Json::Value&)> subscriber) {
  std::lock_guard<std::mutex> lock(subscribers_mtx_);
  subscribers_[++last_subscriber_id_] = subscriber;
  return last_subscriber_id_;
}

void KernelLogEventsHandler::Unsubscribe(int subscriber_id) {
  std::lock_guard<std::mutex> lock(subscribers_mtx_);
  subscribers_.erase(subscriber_id);
}

void KernelLogEventsHandler::DeliverEvent(const Json::Value& event) {
  std::lock_guard<std::mutex> lock(subscribers_mtx_);
  for (const auto& entry : subscribers_) {
    entry.second(event);
  }
}

}  // namespace cuttlefish